blob: 10fac8203d99247701d77a1aaa0ca5f109fe1a3f [file] [log] [blame]
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkString.h"
10#include "samplecode/Sample.h"
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040011
12#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013# include "include/gpu/GrContext.h"
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040014#else
15class GrContext;
16#endif
17
18//////////////////////////////////////////////////////////////////////////////
19
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040020void Sample::setSize(SkScalar width, SkScalar height) {
Brian Osman116b33e2020-02-05 13:34:09 -050021 width = std::max(0.0f, width);
22 height = std::max(0.0f, height);
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040023
24 if (fWidth != width || fHeight != height)
25 {
26 fWidth = width;
27 fHeight = height;
28 this->onSizeChange();
29 }
30}
31
32void Sample::draw(SkCanvas* canvas) {
33 if (fWidth && fHeight) {
34 SkRect r;
Mike Reed92b33352019-08-24 19:39:13 -040035 r.setLTRB(0, 0, fWidth, fHeight);
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040036 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 Daniel0a2464f2020-05-14 15:45:44 -040054 context->flushAndSubmit();
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040055 }
56#endif
57
58 canvas->restoreToCount(sc);
59 }
60}
61
62////////////////////////////////////////////////////////////////////////////
63
Hal Canaryb1f411a2019-08-29 10:39:22 -040064bool Sample::mouse(SkPoint point, skui::InputState clickState, skui::ModifierKey modifierKeys) {
Mike Reed0ce3e882020-04-22 11:09:30 -040065 auto dispatch = [this](Click* c) {
66 return c->fHasFunc ? c->fFunc(c) : this->onClick(c);
67 };
68
Hal Canaryfcf63592019-07-12 11:32:43 -040069 switch (clickState) {
Hal Canaryb1f411a2019-08-29 10:39:22 -040070 case skui::InputState::kDown:
Hal Canaryfcf63592019-07-12 11:32:43 -040071 fClick = nullptr;
Hal Canaryfcf63592019-07-12 11:32:43 -040072 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 Canaryb1f411a2019-08-29 10:39:22 -040077 fClick->fState = skui::InputState::kDown;
Hal Canaryfcf63592019-07-12 11:32:43 -040078 fClick->fModifierKeys = modifierKeys;
Mike Reed0ce3e882020-04-22 11:09:30 -040079 dispatch(fClick.get());
Hal Canaryfcf63592019-07-12 11:32:43 -040080 return true;
Hal Canaryb1f411a2019-08-29 10:39:22 -040081 case skui::InputState::kMove:
Hal Canaryfcf63592019-07-12 11:32:43 -040082 if (fClick) {
83 fClick->fPrev = fClick->fCurr;
84 fClick->fCurr = point;
Hal Canaryb1f411a2019-08-29 10:39:22 -040085 fClick->fState = skui::InputState::kMove;
Hal Canaryfcf63592019-07-12 11:32:43 -040086 fClick->fModifierKeys = modifierKeys;
Mike Reed0ce3e882020-04-22 11:09:30 -040087 return dispatch(fClick.get());
Hal Canaryfcf63592019-07-12 11:32:43 -040088 }
89 return false;
Hal Canaryb1f411a2019-08-29 10:39:22 -040090 case skui::InputState::kUp:
Hal Canaryfcf63592019-07-12 11:32:43 -040091 if (fClick) {
92 fClick->fPrev = fClick->fCurr;
93 fClick->fCurr = point;
Hal Canaryb1f411a2019-08-29 10:39:22 -040094 fClick->fState = skui::InputState::kUp;
Hal Canaryfcf63592019-07-12 11:32:43 -040095 fClick->fModifierKeys = modifierKeys;
Mike Reed0ce3e882020-04-22 11:09:30 -040096 bool result = dispatch(fClick.get());
Hal Canaryfcf63592019-07-12 11:32:43 -040097 fClick = nullptr;
98 return result;
99 }
Hal Canary0afbd9f2019-07-16 09:43:35 -0400100 return false;
Jim Van Verthd0cf5da2019-09-09 16:53:39 -0400101 default:
102 // Ignore other cases
103 SkASSERT(false);
104 break;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400105 }
Hal Canaryfcf63592019-07-12 11:32:43 -0400106 SkASSERT(false);
107 return false;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400108}
109
110//////////////////////////////////////////////////////////////////////
111
112void Sample::onSizeChange() {}
113
Hal Canaryb1f411a2019-08-29 10:39:22 -0400114Sample::Click* Sample::onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) {
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400115 return nullptr;
116}
117
118bool Sample::onClick(Click*) {
119 return false;
120}
121
122void Sample::onDrawBackground(SkCanvas* canvas) {
123 canvas->drawColor(fBGColor);
124}
125
126// need to explicitly declare this, or we get some weird infinite loop llist
127template SampleRegistry* SampleRegistry::gHead;