blob: 7c6fe11feafc5c4e17e19dfe32f9a2d7918946ce [file] [log] [blame]
jvanverthc7027ab2016-06-16 09:52:35 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
8#include "SampleSlide.h"
9
10#include "SkCanvas.h"
11#include "SkCommonFlags.h"
Brian Osmanede860e2017-11-22 16:36:07 -050012#include "SkKey.h"
jvanverthc7027ab2016-06-16 09:52:35 -070013#include "SkOSFile.h"
14#include "SkStream.h"
15
Jim Van Verthc9e7f9c2017-11-02 09:46:49 -040016using namespace sk_app;
17
18SampleSlide::SampleSlide(const SkViewFactory* factory)
19 : fViewFactory(factory)
20 , fClick(nullptr) {
jvanverthc7027ab2016-06-16 09:52:35 -070021 SkView* view = (*factory)();
22 SampleCode::RequestTitle(view, &fName);
23 view->unref();
24}
25
Jim Van Verthc9e7f9c2017-11-02 09:46:49 -040026SampleSlide::~SampleSlide() { delete fClick; }
jvanverthc7027ab2016-06-16 09:52:35 -070027
28void SampleSlide::draw(SkCanvas* canvas) {
Christopher Dalton443ec1b2017-02-24 13:22:53 -070029 SkASSERT(fView);
jvanverthc7027ab2016-06-16 09:52:35 -070030 fView->draw(canvas);
31}
32
33void SampleSlide::load(SkScalar winWidth, SkScalar winHeight) {
Christopher Dalton443ec1b2017-02-24 13:22:53 -070034 fView.reset((*fViewFactory)());
jvanverthc7027ab2016-06-16 09:52:35 -070035 fView->setVisibleP(true);
36 fView->setClipToBounds(false);
37 fView->setSize(winWidth, winHeight);
38}
39
40void SampleSlide::unload() {
Christopher Dalton443ec1b2017-02-24 13:22:53 -070041 fView.reset();
jvanverthc7027ab2016-06-16 09:52:35 -070042}
43
Jim Van Verth6f449692017-02-14 15:16:46 -050044bool SampleSlide::onChar(SkUnichar c) {
Christopher Dalton443ec1b2017-02-24 13:22:53 -070045 if (!fView) {
46 return false;
47 }
Jim Van Verth6f449692017-02-14 15:16:46 -050048 SkEvent evt(gCharEvtName);
49 evt.setFast32(c);
50 return fView->doQuery(&evt);
51}
52
Jim Van Verthc9e7f9c2017-11-02 09:46:49 -040053bool SampleSlide::onMouse(SkScalar x, SkScalar y, Window::InputState state,
54 uint32_t modifiers) {
55 // map to SkView modifiers
56 unsigned modifierKeys = 0;
57 modifierKeys |= (state & Window::kShift_ModifierKey) ? kShift_SkModifierKey : 0;
58 modifierKeys |= (state & Window::kControl_ModifierKey) ? kControl_SkModifierKey : 0;
59 modifierKeys |= (state & Window::kOption_ModifierKey) ? kOption_SkModifierKey : 0;
60 modifierKeys |= (state & Window::kCommand_ModifierKey) ? kCommand_SkModifierKey : 0;
61
62 bool handled = false;
63 switch (state) {
64 case Window::kDown_InputState: {
65 delete fClick;
66 fClick = fView->findClickHandler(SkIntToScalar(x), SkIntToScalar(y), modifierKeys);
67 if (fClick) {
68 SkView::DoClickDown(fClick, x, y, modifierKeys);
69 handled = true;
70 }
71 break;
72 }
73 case Window::kMove_InputState: {
74 if (fClick) {
75 SkView::DoClickMoved(fClick, x, y, modifierKeys);
76 handled = true;
77 }
78 break;
79 }
80 case Window::kUp_InputState: {
81 if (fClick) {
82 SkView::DoClickUp(fClick, x, y, modifierKeys);
83 delete fClick;
84 fClick = nullptr;
85 handled = true;
86 }
87 break;
88 }
89 }
90
91 return handled;
92}