jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 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 | |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 15 | using namespace sk_app; |
| 16 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 17 | SampleSlide::SampleSlide(const SampleFactory factory) |
| 18 | : fSampleFactory(factory) |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 19 | , fClick(nullptr) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 20 | sk_sp<Sample> sample(factory()); |
| 21 | Sample::RequestTitle(sample.get(), &fName); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 24 | SampleSlide::~SampleSlide() { delete fClick; } |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 25 | |
| 26 | void SampleSlide::draw(SkCanvas* canvas) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 27 | SkASSERT(fSample); |
| 28 | fSample->draw(canvas); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void SampleSlide::load(SkScalar winWidth, SkScalar winHeight) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 32 | fSample.reset(fSampleFactory()); |
| 33 | fSample->setSize(winWidth, winHeight); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void SampleSlide::unload() { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 37 | fSample.reset(); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 40 | bool SampleSlide::onChar(SkUnichar c) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 41 | if (!fSample) { |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 42 | return false; |
| 43 | } |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 44 | Sample::Event evt(Sample::kCharEvtName); |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 45 | evt.setFast32(c); |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 46 | return fSample->doQuery(&evt); |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 47 | } |
| 48 | |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 49 | bool SampleSlide::onMouse(SkScalar x, SkScalar y, Window::InputState state, |
| 50 | uint32_t modifiers) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 51 | // map to Sample::Click modifiers |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 52 | unsigned modifierKeys = 0; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 53 | modifierKeys |= (modifiers & Window::kShift_ModifierKey) ? Sample::Click::kShift_ModifierKey : 0; |
| 54 | modifierKeys |= (modifiers & Window::kControl_ModifierKey) ? Sample::Click::kControl_ModifierKey : 0; |
| 55 | modifierKeys |= (modifiers & Window::kOption_ModifierKey) ? Sample::Click::kOption_ModifierKey : 0; |
| 56 | modifierKeys |= (modifiers & Window::kCommand_ModifierKey) ? Sample::Click::kCommand_ModifierKey : 0; |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 57 | |
| 58 | bool handled = false; |
| 59 | switch (state) { |
| 60 | case Window::kDown_InputState: { |
| 61 | delete fClick; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 62 | fClick = fSample->findClickHandler(SkIntToScalar(x), SkIntToScalar(y), modifierKeys); |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 63 | if (fClick) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 64 | Sample::DoClickDown(fClick, x, y, modifierKeys); |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 65 | handled = true; |
| 66 | } |
| 67 | break; |
| 68 | } |
| 69 | case Window::kMove_InputState: { |
| 70 | if (fClick) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 71 | Sample::DoClickMoved(fClick, x, y, modifierKeys); |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 72 | handled = true; |
| 73 | } |
| 74 | break; |
| 75 | } |
| 76 | case Window::kUp_InputState: { |
| 77 | if (fClick) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 78 | Sample::DoClickUp(fClick, x, y, modifierKeys); |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 79 | delete fClick; |
| 80 | fClick = nullptr; |
| 81 | handled = true; |
| 82 | } |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return handled; |
| 88 | } |