Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [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 "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkString.h" |
| 10 | #include "samplecode/Sample.h" |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 11 | |
| 12 | #if SK_SUPPORT_GPU |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | # include "include/gpu/GrContext.h" |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 14 | #else |
| 15 | class GrContext; |
| 16 | #endif |
| 17 | |
| 18 | ////////////////////////////////////////////////////////////////////////////// |
| 19 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 20 | void Sample::setSize(SkScalar width, SkScalar height) { |
Brian Osman | 116b33e | 2020-02-05 13:34:09 -0500 | [diff] [blame] | 21 | width = std::max(0.0f, width); |
| 22 | height = std::max(0.0f, height); |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 23 | |
| 24 | if (fWidth != width || fHeight != height) |
| 25 | { |
| 26 | fWidth = width; |
| 27 | fHeight = height; |
| 28 | this->onSizeChange(); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void Sample::draw(SkCanvas* canvas) { |
| 33 | if (fWidth && fHeight) { |
| 34 | SkRect r; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 35 | r.setLTRB(0, 0, fWidth, fHeight); |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 36 | if (canvas->quickReject(r)) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | SkAutoCanvasRestore as(canvas, true); |
| 41 | int sc = canvas->save(); |
| 42 | |
| 43 | if (!fHaveCalledOnceBeforeDraw) { |
| 44 | fHaveCalledOnceBeforeDraw = true; |
| 45 | this->onOnceBeforeDraw(); |
| 46 | } |
| 47 | this->onDrawBackground(canvas); |
| 48 | |
| 49 | SkAutoCanvasRestore acr(canvas, true); |
| 50 | this->onDrawContent(canvas); |
| 51 | #if SK_SUPPORT_GPU |
| 52 | // Ensure the GrContext doesn't combine GrDrawOps across draw loops. |
| 53 | if (GrContext* context = canvas->getGrContext()) { |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame^] | 54 | context->flushAndSubmit(); |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 55 | } |
| 56 | #endif |
| 57 | |
| 58 | canvas->restoreToCount(sc); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | //////////////////////////////////////////////////////////////////////////// |
| 63 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 64 | bool Sample::mouse(SkPoint point, skui::InputState clickState, skui::ModifierKey modifierKeys) { |
Mike Reed | 0ce3e88 | 2020-04-22 11:09:30 -0400 | [diff] [blame] | 65 | auto dispatch = [this](Click* c) { |
| 66 | return c->fHasFunc ? c->fFunc(c) : this->onClick(c); |
| 67 | }; |
| 68 | |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 69 | switch (clickState) { |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 70 | case skui::InputState::kDown: |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 71 | fClick = nullptr; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 72 | fClick.reset(this->onFindClickHandler(point.x(), point.y(), modifierKeys)); |
| 73 | if (!fClick) { |
| 74 | return false; |
| 75 | } |
| 76 | fClick->fPrev = fClick->fCurr = fClick->fOrig = point; |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 77 | fClick->fState = skui::InputState::kDown; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 78 | fClick->fModifierKeys = modifierKeys; |
Mike Reed | 0ce3e88 | 2020-04-22 11:09:30 -0400 | [diff] [blame] | 79 | dispatch(fClick.get()); |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 80 | return true; |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 81 | case skui::InputState::kMove: |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 82 | if (fClick) { |
| 83 | fClick->fPrev = fClick->fCurr; |
| 84 | fClick->fCurr = point; |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 85 | fClick->fState = skui::InputState::kMove; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 86 | fClick->fModifierKeys = modifierKeys; |
Mike Reed | 0ce3e88 | 2020-04-22 11:09:30 -0400 | [diff] [blame] | 87 | return dispatch(fClick.get()); |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 88 | } |
| 89 | return false; |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 90 | case skui::InputState::kUp: |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 91 | if (fClick) { |
| 92 | fClick->fPrev = fClick->fCurr; |
| 93 | fClick->fCurr = point; |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 94 | fClick->fState = skui::InputState::kUp; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 95 | fClick->fModifierKeys = modifierKeys; |
Mike Reed | 0ce3e88 | 2020-04-22 11:09:30 -0400 | [diff] [blame] | 96 | bool result = dispatch(fClick.get()); |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 97 | fClick = nullptr; |
| 98 | return result; |
| 99 | } |
Hal Canary | 0afbd9f | 2019-07-16 09:43:35 -0400 | [diff] [blame] | 100 | return false; |
Jim Van Verth | d0cf5da | 2019-09-09 16:53:39 -0400 | [diff] [blame] | 101 | default: |
| 102 | // Ignore other cases |
| 103 | SkASSERT(false); |
| 104 | break; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 105 | } |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 106 | SkASSERT(false); |
| 107 | return false; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////// |
| 111 | |
| 112 | void Sample::onSizeChange() {} |
| 113 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 114 | Sample::Click* Sample::onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) { |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 115 | return nullptr; |
| 116 | } |
| 117 | |
| 118 | bool Sample::onClick(Click*) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | void Sample::onDrawBackground(SkCanvas* canvas) { |
| 123 | canvas->drawColor(fBGColor); |
| 124 | } |
| 125 | |
| 126 | // need to explicitly declare this, or we get some weird infinite loop llist |
| 127 | template SampleRegistry* SampleRegistry::gHead; |