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" |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 12 | #include "SkKey.h" |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 13 | #include "SkOSFile.h" |
| 14 | #include "SkStream.h" |
| 15 | |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 16 | using namespace sk_app; |
| 17 | |
| 18 | SampleSlide::SampleSlide(const SkViewFactory* factory) |
| 19 | : fViewFactory(factory) |
| 20 | , fClick(nullptr) { |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 21 | SkView* view = (*factory)(); |
| 22 | SampleCode::RequestTitle(view, &fName); |
| 23 | view->unref(); |
| 24 | } |
| 25 | |
Jim Van Verth | c9e7f9c | 2017-11-02 09:46:49 -0400 | [diff] [blame] | 26 | SampleSlide::~SampleSlide() { delete fClick; } |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 27 | |
| 28 | void SampleSlide::draw(SkCanvas* canvas) { |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 29 | SkASSERT(fView); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 30 | fView->draw(canvas); |
| 31 | } |
| 32 | |
| 33 | void SampleSlide::load(SkScalar winWidth, SkScalar winHeight) { |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 34 | fView.reset((*fViewFactory)()); |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 35 | fView->setVisibleP(true); |
| 36 | fView->setClipToBounds(false); |
| 37 | fView->setSize(winWidth, winHeight); |
| 38 | } |
| 39 | |
| 40 | void SampleSlide::unload() { |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 41 | fView.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) { |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 45 | if (!fView) { |
| 46 | return false; |
| 47 | } |
Jim Van Verth | 6f44969 | 2017-02-14 15:16:46 -0500 | [diff] [blame] | 48 | SkEvent evt(gCharEvtName); |
| 49 | evt.setFast32(c); |
| 50 | return fView->doQuery(&evt); |
| 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) { |
| 55 | // map to SkView modifiers |
| 56 | unsigned modifierKeys = 0; |
Brian Osman | 6f3eac9 | 2017-12-04 16:59:36 -0500 | [diff] [blame^] | 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; |
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; |
| 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 | } |