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 "SkOSFile.h"
13 #include "SkStream.h"
14
15 using namespace sk_app;
16
SampleSlide(const SampleFactory factory)17 SampleSlide::SampleSlide(const SampleFactory factory)
18 : fSampleFactory(factory)
19 , fClick(nullptr) {
20 sk_sp<Sample> sample(factory());
21 Sample::RequestTitle(sample.get(), &fName);
22 }
23
~SampleSlide()24 SampleSlide::~SampleSlide() { delete fClick; }
25
getDimensions() const26 SkISize SampleSlide::getDimensions() const {
27 return SkISize::Make(SkScalarCeilToInt(fSample->width()), SkScalarCeilToInt(fSample->height()));
28 }
29
draw(SkCanvas * canvas)30 void SampleSlide::draw(SkCanvas* canvas) {
31 SkASSERT(fSample);
32 fSample->draw(canvas);
33 }
34
load(SkScalar winWidth,SkScalar winHeight)35 void SampleSlide::load(SkScalar winWidth, SkScalar winHeight) {
36 fSample.reset(fSampleFactory());
37 fSample->setSize(winWidth, winHeight);
38 }
39
unload()40 void SampleSlide::unload() {
41 fSample.reset();
42 }
43
onChar(SkUnichar c)44 bool SampleSlide::onChar(SkUnichar c) {
45 if (!fSample) {
46 return false;
47 }
48 Sample::Event evt(Sample::kCharEvtName);
49 evt.setFast32(c);
50 return fSample->doQuery(&evt);
51 }
52
onMouse(SkScalar x,SkScalar y,Window::InputState state,uint32_t modifiers)53 bool SampleSlide::onMouse(SkScalar x, SkScalar y, Window::InputState state,
54 uint32_t modifiers) {
55 // map to Sample::Click modifiers
56 unsigned modifierKeys = 0;
57 modifierKeys |= (modifiers & Window::kShift_ModifierKey) ? Sample::Click::kShift_ModifierKey : 0;
58 modifierKeys |= (modifiers & Window::kControl_ModifierKey) ? Sample::Click::kControl_ModifierKey : 0;
59 modifierKeys |= (modifiers & Window::kOption_ModifierKey) ? Sample::Click::kOption_ModifierKey : 0;
60 modifierKeys |= (modifiers & Window::kCommand_ModifierKey) ? Sample::Click::kCommand_ModifierKey : 0;
61
62 bool handled = false;
63 switch (state) {
64 case Window::kDown_InputState: {
65 delete fClick;
66 fClick = fSample->findClickHandler(SkIntToScalar(x), SkIntToScalar(y), modifierKeys);
67 if (fClick) {
68 Sample::DoClickDown(fClick, x, y, modifierKeys);
69 handled = true;
70 }
71 break;
72 }
73 case Window::kMove_InputState: {
74 if (fClick) {
75 Sample::DoClickMoved(fClick, x, y, modifierKeys);
76 handled = true;
77 }
78 break;
79 }
80 case Window::kUp_InputState: {
81 if (fClick) {
82 Sample::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