blob: 895cd8348519a9435a4a6cce40fc02cb6f35ea7d [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
8#include "Sample.h"
9#include "SkCanvas.h"
10#include "SkString.h"
11
12#if SK_SUPPORT_GPU
13# include "GrContext.h"
14#else
15class GrContext;
16#endif
17
18//////////////////////////////////////////////////////////////////////////////
19
20Sample::Event::Event() : Event("") {}
21
22Sample::Event::Event(const Event& that) {
23 *this = that;
24}
25
26Sample::Event::Event(const char type[]) : fType(type), f32(0) {
27 SkASSERT(type);
28}
29
30Sample::Event::~Event() {}
31
32bool Sample::Event::isType(const char type[]) const {
33 return fType.equals(type);
34}
35
36const char* Sample::kCharEvtName = "SampleCode_Char_Event";
37const char* Sample::kTitleEvtName = "SampleCode_Title_Event";
38
39bool Sample::CharQ(const Event& evt, SkUnichar* outUni) {
40 if (evt.isType(kCharEvtName)) {
41 if (outUni) {
42 *outUni = evt.getFast32();
43 }
44 return true;
45 }
46 return false;
47}
48
49bool Sample::TitleQ(const Event& evt) {
50 return evt.isType(kTitleEvtName);
51}
52
53void Sample::TitleR(Event* evt, const char title[]) {
54 SkASSERT(evt && TitleQ(*evt));
55 evt->setString(kTitleEvtName, title);
56}
57
58bool Sample::RequestTitle(Sample* view, SkString* title) {
59 Event evt(kTitleEvtName);
60 if (view->doQuery(&evt)) {
61 title->set(evt.findString(kTitleEvtName));
62 return true;
63 }
64 return false;
65}
66
67///////////////////////////////////////////////////////////////////////////////
68
69bool Sample::doEvent(const Event& evt) {
70 return this->onEvent(evt);
71}
72
73bool Sample::onEvent(const Event&) {
74 return false;
75}
76
77bool Sample::doQuery(Event* evt) {
78 SkASSERT(evt);
79 return this->onQuery(evt);
80}
81
82bool Sample::onQuery(Sample::Event* evt) {
83 return false;
84}
85
86////////////////////////////////////////////////////////////////////////
87
88
89void Sample::setSize(SkScalar width, SkScalar height) {
90 width = SkMaxScalar(0, width);
91 height = SkMaxScalar(0, height);
92
93 if (fWidth != width || fHeight != height)
94 {
95 fWidth = width;
96 fHeight = height;
97 this->onSizeChange();
98 }
99}
100
101void Sample::draw(SkCanvas* canvas) {
102 if (fWidth && fHeight) {
103 SkRect r;
104 r.set(0, 0, fWidth, fHeight);
105 if (canvas->quickReject(r)) {
106 return;
107 }
108
109 SkAutoCanvasRestore as(canvas, true);
110 int sc = canvas->save();
111
112 if (!fHaveCalledOnceBeforeDraw) {
113 fHaveCalledOnceBeforeDraw = true;
114 this->onOnceBeforeDraw();
115 }
116 this->onDrawBackground(canvas);
117
118 SkAutoCanvasRestore acr(canvas, true);
119 this->onDrawContent(canvas);
120#if SK_SUPPORT_GPU
121 // Ensure the GrContext doesn't combine GrDrawOps across draw loops.
122 if (GrContext* context = canvas->getGrContext()) {
123 context->flush();
124 }
125#endif
126
127 canvas->restoreToCount(sc);
128 }
129}
130
131////////////////////////////////////////////////////////////////////////////
132
133Sample::Click::Click(Sample* target) {
134 SkASSERT(target);
135 fTarget = sk_ref_sp(target);
136}
137
138Sample::Click::~Click() {}
139
140Sample::Click* Sample::findClickHandler(SkScalar x, SkScalar y, unsigned modi) {
141 if (x < 0 || y < 0 || x >= fWidth || y >= fHeight) {
142 return nullptr;
143 }
144
145 return this->onFindClickHandler(x, y, modi);
146}
147
148void Sample::DoClickDown(Click* click, int x, int y, unsigned modi) {
149 SkASSERT(click);
150
151 Sample* target = click->fTarget.get();
152 if (nullptr == target) {
153 return;
154 }
155
156 click->fIOrig.set(x, y);
157 click->fICurr = click->fIPrev = click->fIOrig;
158
159 click->fOrig.iset(x, y);
160 click->fPrev = click->fCurr = click->fOrig;
161
162 click->fState = Click::kDown_State;
163 click->fModifierKeys = modi;
164 target->onClick(click);
165}
166
167void Sample::DoClickMoved(Click* click, int x, int y, unsigned modi) {
168 SkASSERT(click);
169
170 Sample* target = click->fTarget.get();
171 if (nullptr == target) {
172 return;
173 }
174
175 click->fIPrev = click->fICurr;
176 click->fICurr.set(x, y);
177
178 click->fPrev = click->fCurr;
179 click->fCurr.iset(x, y);
180
181 click->fState = Click::kMoved_State;
182 click->fModifierKeys = modi;
183 target->onClick(click);
184}
185
186void Sample::DoClickUp(Click* click, int x, int y, unsigned modi) {
187 SkASSERT(click);
188
189 Sample* target = click->fTarget.get();
190 if (nullptr == target) {
191 return;
192 }
193
194 click->fIPrev = click->fICurr;
195 click->fICurr.set(x, y);
196
197 click->fPrev = click->fCurr;
198 click->fCurr.iset(x, y);
199
200 click->fState = Click::kUp_State;
201 click->fModifierKeys = modi;
202 target->onClick(click);
203}
204
205//////////////////////////////////////////////////////////////////////
206
207void Sample::onSizeChange() {}
208
209Sample::Click* Sample::onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) {
210 return nullptr;
211}
212
213bool Sample::onClick(Click*) {
214 return false;
215}
216
217void Sample::onDrawBackground(SkCanvas* canvas) {
218 canvas->drawColor(fBGColor);
219}
220
221// need to explicitly declare this, or we get some weird infinite loop llist
222template SampleRegistry* SampleRegistry::gHead;