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 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame^] | 30 | bool SampleSlide::animate(double nanos) { return fSample->animate(nanos); } |
| 31 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 32 | void SampleSlide::draw(SkCanvas* canvas) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 33 | SkASSERT(fSample); |
| 34 | fSample->draw(canvas); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void SampleSlide::load(SkScalar winWidth, SkScalar winHeight) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 38 | fSample.reset(fSampleFactory()); |
| 39 | fSample->setSize(winWidth, winHeight); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void SampleSlide::unload() { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 43 | fSample.reset(); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 46 | bool SampleSlide::onChar(SkUnichar c) { |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 47 | return fSample && fSample->onChar(c); |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 50 | bool SampleSlide::onMouse(SkScalar x, SkScalar y, Window::InputState state, |
Hal Canary | 3a85ed1 | 2019-07-08 16:07:57 -0400 | [diff] [blame] | 51 | ModifierKey modifierKeys) { |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 52 | bool handled = false; |
| 53 | switch (state) { |
| 54 | case Window::kDown_InputState: { |
| 55 | delete fClick; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 56 | fClick = fSample->findClickHandler(SkIntToScalar(x), SkIntToScalar(y), modifierKeys); |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 57 | if (fClick) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 58 | Sample::DoClickDown(fClick, x, y, modifierKeys); |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 59 | handled = true; |
| 60 | } |
| 61 | break; |
| 62 | } |
| 63 | case Window::kMove_InputState: { |
| 64 | if (fClick) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 65 | Sample::DoClickMoved(fClick, x, y, modifierKeys); |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 66 | handled = true; |
| 67 | } |
| 68 | break; |
| 69 | } |
| 70 | case Window::kUp_InputState: { |
| 71 | if (fClick) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 72 | Sample::DoClickUp(fClick, x, y, modifierKeys); |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 73 | delete fClick; |
| 74 | fClick = nullptr; |
| 75 | handled = true; |
| 76 | } |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return handled; |
| 82 | } |