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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tools/viewer/SampleSlide.h" |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkStream.h" |
| 12 | #include "src/core/SkOSFile.h" |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 13 | |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 14 | using namespace sk_app; |
| 15 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 16 | SampleSlide::SampleSlide(const SampleFactory factory) |
| 17 | : fSampleFactory(factory) |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame^] | 18 | , fClick(nullptr) |
| 19 | { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 20 | sk_sp<Sample> sample(factory()); |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame^] | 21 | fName = sample->name(); |
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 | |
Ben Wagner | 2115112 | 2018-09-04 18:35:20 -0400 | [diff] [blame] | 26 | SkISize SampleSlide::getDimensions() const { |
| 27 | return SkISize::Make(SkScalarCeilToInt(fSample->width()), SkScalarCeilToInt(fSample->height())); |
| 28 | } |
| 29 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 30 | void SampleSlide::draw(SkCanvas* canvas) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 31 | SkASSERT(fSample); |
| 32 | fSample->draw(canvas); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void SampleSlide::load(SkScalar winWidth, SkScalar winHeight) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 36 | fSample.reset(fSampleFactory()); |
| 37 | fSample->setSize(winWidth, winHeight); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void SampleSlide::unload() { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 41 | fSample.reset(); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 44 | bool SampleSlide::onChar(SkUnichar c) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 45 | if (!fSample) { |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 46 | return false; |
| 47 | } |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 48 | Sample::Event evt(Sample::kCharEvtName); |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 49 | evt.setFast32(c); |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 50 | return fSample->doQuery(&evt); |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 51 | } |
| 52 | |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 53 | bool SampleSlide::onMouse(SkScalar x, SkScalar y, Window::InputState state, |
| 54 | uint32_t modifiers) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 55 | // map to Sample::Click modifiers |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 56 | unsigned modifierKeys = 0; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 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; |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 61 | |
| 62 | bool handled = false; |
| 63 | switch (state) { |
| 64 | case Window::kDown_InputState: { |
| 65 | delete fClick; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 66 | fClick = fSample->findClickHandler(SkIntToScalar(x), SkIntToScalar(y), modifierKeys); |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 67 | if (fClick) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 68 | Sample::DoClickDown(fClick, x, y, modifierKeys); |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 69 | handled = true; |
| 70 | } |
| 71 | break; |
| 72 | } |
| 73 | case Window::kMove_InputState: { |
| 74 | if (fClick) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 75 | Sample::DoClickMoved(fClick, x, y, modifierKeys); |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 76 | handled = true; |
| 77 | } |
| 78 | break; |
| 79 | } |
| 80 | case Window::kUp_InputState: { |
| 81 | if (fClick) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 82 | Sample::DoClickUp(fClick, x, y, modifierKeys); |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 83 | delete fClick; |
| 84 | fClick = nullptr; |
| 85 | handled = true; |
| 86 | } |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return handled; |
| 92 | } |