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 "Sample.h"
9 #include "SkCanvas.h"
10 #include "SkString.h"
11
12 #if SK_SUPPORT_GPU
13 # include "GrContext.h"
14 #else
15 class GrContext;
16 #endif
17
18 //////////////////////////////////////////////////////////////////////////////
19
Event()20 Sample::Event::Event() : Event("") {}
21
Event(const Event & that)22 Sample::Event::Event(const Event& that) {
23 *this = that;
24 }
25
Event(const char type[])26 Sample::Event::Event(const char type[]) : fType(type), f32(0) {
27 SkASSERT(type);
28 }
29
~Event()30 Sample::Event::~Event() {}
31
isType(const char type[]) const32 bool Sample::Event::isType(const char type[]) const {
33 return fType.equals(type);
34 }
35
36 const char* Sample::kCharEvtName = "SampleCode_Char_Event";
37 const char* Sample::kTitleEvtName = "SampleCode_Title_Event";
38
CharQ(const Event & evt,SkUnichar * outUni)39 bool 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
TitleQ(const Event & evt)49 bool Sample::TitleQ(const Event& evt) {
50 return evt.isType(kTitleEvtName);
51 }
52
TitleR(Event * evt,const char title[])53 void Sample::TitleR(Event* evt, const char title[]) {
54 SkASSERT(evt && TitleQ(*evt));
55 evt->setString(kTitleEvtName, title);
56 }
57
RequestTitle(Sample * view,SkString * title)58 bool 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
doEvent(const Event & evt)69 bool Sample::doEvent(const Event& evt) {
70 return this->onEvent(evt);
71 }
72
onEvent(const Event &)73 bool Sample::onEvent(const Event&) {
74 return false;
75 }
76
doQuery(Event * evt)77 bool Sample::doQuery(Event* evt) {
78 SkASSERT(evt);
79 return this->onQuery(evt);
80 }
81
onQuery(Sample::Event * evt)82 bool Sample::onQuery(Sample::Event* evt) {
83 return false;
84 }
85
86 ////////////////////////////////////////////////////////////////////////
87
88
setSize(SkScalar width,SkScalar height)89 void 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
draw(SkCanvas * canvas)101 void 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
Click(Sample * target)133 Sample::Click::Click(Sample* target) {
134 SkASSERT(target);
135 fTarget = sk_ref_sp(target);
136 }
137
~Click()138 Sample::Click::~Click() {}
139
findClickHandler(SkScalar x,SkScalar y,unsigned modi)140 Sample::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
DoClickDown(Click * click,int x,int y,unsigned modi)148 void 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
DoClickMoved(Click * click,int x,int y,unsigned modi)167 void 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
DoClickUp(Click * click,int x,int y,unsigned modi)186 void 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
onSizeChange()207 void Sample::onSizeChange() {}
208
onFindClickHandler(SkScalar x,SkScalar y,unsigned modi)209 Sample::Click* Sample::onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) {
210 return nullptr;
211 }
212
onClick(Click *)213 bool Sample::onClick(Click*) {
214 return false;
215 }
216
onDrawBackground(SkCanvas * canvas)217 void Sample::onDrawBackground(SkCanvas* canvas) {
218 canvas->drawColor(fBGColor);
219 }
220
221 // need to explicitly declare this, or we get some weird infinite loop llist
222 template SampleRegistry* SampleRegistry::gHead;
223