1 /*
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"
12 #include "SkKey.h"
13 #include "SkOSFile.h"
14 #include "SkStream.h"
15 
16 using namespace sk_app;
17 
18 SampleSlide::SampleSlide(const SkViewFactory* factory)
19     : fViewFactory(factory)
20     , fClick(nullptr) {
21     SkView* view = (*factory)();
22     SampleCode::RequestTitle(view, &fName);
23     view->unref();
24 }
25 
26 SampleSlide::~SampleSlide() { delete fClick; }
27 
28 void SampleSlide::draw(SkCanvas* canvas) {
29     SkASSERT(fView);
30     fView->draw(canvas);
31 }
32 
33 void SampleSlide::load(SkScalar winWidth, SkScalar winHeight) {
34     fView.reset((*fViewFactory)());
35     fView->setVisibleP(true);
36     fView->setClipToBounds(false);
37     fView->setSize(winWidth, winHeight);
38 }
39 
40 void SampleSlide::unload() {
41     fView.reset();
42 }
43 
44 bool SampleSlide::onChar(SkUnichar c) {
45     if (!fView) {
46         return false;
47     }
48     SkEvent evt(gCharEvtName);
49     evt.setFast32(c);
50     return fView->doQuery(&evt);
51 }
52 
53 bool SampleSlide::onMouse(SkScalar x, SkScalar y, Window::InputState state,
54                           uint32_t modifiers) {
55     // map to SkView modifiers
56     unsigned modifierKeys = 0;
57     modifierKeys |= (modifiers & Window::kShift_ModifierKey) ? kShift_SkModifierKey : 0;
58     modifierKeys |= (modifiers & Window::kControl_ModifierKey) ? kControl_SkModifierKey : 0;
59     modifierKeys |= (modifiers & Window::kOption_ModifierKey) ? kOption_SkModifierKey : 0;
60     modifierKeys |= (modifiers & 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 }
93