Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(404)

Side by Side Diff: samplecode/SampleBevel.cpp

Issue 2246243002: Interactive Bevel Sample App (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: Width maximum increased, controls now stay stationary regardless of CTM Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gyp/samples.gypi ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "SampleCode.h"
9 #include "SkCanvas.h"
10 #include "SkLightingShader.h"
11 #include "SkNormalSource.h"
12 #include "sk_tool_utils.h"
13
14
15 class BevelView : public SampleView {
16 public:
17 BevelView()
18 : fShapeBounds(SkRect::MakeIWH(kShapeBoundsSize, kShapeBoundsSize))
19 , fRedLight(SkLights::Light::MakeDirectional(SkColor3f::Make(0.6f, 0.45f , 0.3f),
20 SkVector3::Make(0.0f, -5.0f , 1.0f)))
21 , fBlueLight(SkLights::Light::MakeDirectional(SkColor3f::Make(0.3f, 0.45 f, 0.6f),
22 SkVector3::Make(0.0f, 5.0f , 1.0f))) {
23 this->setBGColor(0xFF666868); // Slightly colorized gray for contrast
24
25 // Lights
26 SkLights::Builder builder;
27 builder.add(fRedLight);
28 builder.add(fBlueLight);
29 builder.add(SkLights::Light::MakeAmbient(SkColor3f::Make(0.4f, 0.4f, 0.4 f)));
30 fLights = builder.finish();
31
32 // Controls
33
34 int currY = kSliderHeight;
35
36 fCtrlRangeRects[0] = SkRect::MakeIWH(kCtrlRange + kSliderWidth, kSliderH eight);
37 fCtrlRangeRects[0].offset(0, SkIntToScalar(currY));
38 fWidthCtrlRect = SkRect::MakeIWH(kSliderWidth, kSliderHeight);
39 fWidthCtrlRect.offset(0.3*kCtrlRange, SkIntToScalar(currY));
40 fBevelWidth = 0.3*kBevelWidthMax;
41 currY += 2 * kSliderHeight;
42
43 fCtrlRangeRects[1] = SkRect::MakeIWH(kCtrlRange + kSliderWidth, kSliderH eight);
44 fCtrlRangeRects[1].offset(0, SkIntToScalar(currY));
45 fHeightCtrlRect = SkRect::MakeIWH(kSliderWidth, kSliderHeight);
46 fHeightCtrlRect.offset(0.3*kCtrlRange, SkIntToScalar(currY));
47 fBevelHeight = 0.3*kBevelHeightMax;
48 currY += 2 * kSliderHeight;
49
50 fCtrlRangeRects[2] = SkRect::MakeIWH(kCtrlRange + kSliderWidth, kSliderH eight);
51 fCtrlRangeRects[2].offset(0, SkIntToScalar(currY));
52 fTypeCtrlRect = SkRect::MakeIWH(kSliderWidth, kSliderHeight);
53 fTypeCtrlRect.offset((1.0f/6.0f)*kCtrlRange, SkIntToScalar(currY));
54 fBevelType = (SkNormalSource::BevelType) 0;
55 currY += 2 * kSliderHeight;
56
57 fSelectedCtrlRect = nullptr;
58 }
59
60 protected:
61 bool onQuery(SkEvent *evt) override {
62 if (SampleCode::TitleQ(*evt)) {
63 SampleCode::TitleR(evt, "bevel");
64 return true;
65 }
66
67 return this->INHERITED::onQuery(evt);
68 }
69
70 enum Shape {
71 kCircle_Shape,
72 kRect_Shape,
73 };
74 void drawShape(enum Shape shape, SkCanvas* canvas) {
75 canvas->save();
76
77 SkPaint paint;
78
79 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeBevel(fBevelTyp e, fBevelWidth,
80 fBevelHei ght);
81
82 paint.setShader(SkLightingShader::Make(nullptr, std::move(normalSource), fLights));
83 paint.setAntiAlias(true);
84 paint.setColor(0xFFDDDDDD);
85 switch (shape) {
86 case kCircle_Shape:
87 canvas->drawCircle(fShapeBounds.centerX(), fShapeBounds.centerY( ),
88 fShapeBounds.width()/2.0f, paint);
89 break;
90 case kRect_Shape:
91 canvas->drawRect(fShapeBounds, paint);
92 break;
93 default:
94 SkDEBUGFAIL("Invalid shape enum for drawShape");
95 }
96
97 canvas->restore();
98 }
99
100 void onDrawContent(SkCanvas *canvas) override {
101
102 canvas->save();
103 canvas->resetMatrix(); // Force static controls and labels
104
105 // Draw controls
106
107 SkPaint ctrlRectPaint;
108 ctrlRectPaint.setColor(0xFFF3F3F3);
109 canvas->drawRect(fWidthCtrlRect, ctrlRectPaint);
110 canvas->drawRect(fHeightCtrlRect, ctrlRectPaint);
111 canvas->drawRect(fTypeCtrlRect, ctrlRectPaint);
112
113 SkPaint ctrlRectRangePaint;
114 ctrlRectRangePaint.setColor(0xFFFFFFFF);
115 ctrlRectRangePaint.setStyle(SkPaint::kStroke_Style);
116 ctrlRectRangePaint.setStrokeWidth(2.0f);
117
118 for (size_t i = 0; i < kNumControls; i++) {
119 canvas->drawRect(fCtrlRangeRects[i], ctrlRectRangePaint);
120 }
121
122 // Draw labels
123 constexpr SkScalar kTextSize = 12.0f;
124 SkString widthLabel, heightLabel, typeLabel;
125 SkPaint labelPaint;
126 labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-ser if",
127 SkFontSty le()));
128 labelPaint.setAntiAlias(true);
129 labelPaint.setColor(0xFFFFFFFF);
130 labelPaint.setTextSize(kTextSize);
131
132 widthLabel.appendf("BevelWidth: %f", fBevelWidth);
133 heightLabel.appendf("BevelHeight: %f", fBevelHeight);
134 typeLabel.append("BevelType: ");
135
136 switch (fBevelType) {
137 case SkNormalSource::BevelType::kLinear:
138 typeLabel.append("Linear");
139 break;
140 case SkNormalSource::BevelType::kRoundedIn:
141 typeLabel.append("RoundedIn");
142 break;
143 case SkNormalSource::BevelType::kRoundedOut:
144 typeLabel.append("RoundedOut");
145 break;
146 }
147
148 canvas->drawText(widthLabel.c_str(), widthLabel.size(), 0,
149 fWidthCtrlRect.fTop - kTextSize/2.0f, labelPaint);
150 canvas->drawText(heightLabel.c_str(), heightLabel.size(), 0,
151 fHeightCtrlRect.fTop - kTextSize/2.0f, labelPaint);
152 canvas->drawText(typeLabel.c_str(), typeLabel.size(), 0,
153 fTypeCtrlRect.fTop - kTextSize/2.0f, labelPaint);
154
155 canvas->restore(); // Return to modified matrix when drawing shapes
156
157 // Draw shapes
158 SkScalar xPos = kCtrlRange + 25;
159 SkScalar yPos = fShapeBounds.height();
160 for (Shape shape : { kCircle_Shape, kRect_Shape }) {
161 canvas->save();
162 canvas->translate(xPos, yPos);
163 this->drawShape(shape, canvas);
164 canvas->restore();
165
166 xPos += 1.2f * fShapeBounds.width();
167 }
168 }
169
170 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride {
171 return new SkView::Click(this);
172 }
173
174 bool onClick(Click *click) override {
175 SkScalar x = click->fCurr.fX;
176 SkScalar y = click->fCurr.fY;
177
178 SkScalar dx = x - click->fPrev.fX;
179 SkScalar dy = y - click->fPrev.fY;
180
181 // Control deselection
182 if (Click::State::kUp_State == click->fState) {
183 fSelectedCtrlRect = nullptr;
184 return true;
185 }
186
187 // Control selection
188 if (nullptr == fSelectedCtrlRect && Click::State::kDown_State == click-> fState) {
189 if (fWidthCtrlRect.contains(SkRect::MakeXYWH(x, y, 1, 1))) {
190 fSelectedCtrlRect = &fWidthCtrlRect;
191 } else if (fHeightCtrlRect.contains(SkRect::MakeXYWH(x, y, 1, 1))) {
192 fSelectedCtrlRect = &fHeightCtrlRect;
193 } else if (fTypeCtrlRect.contains(SkRect::MakeXYWH(x, y, 1, 1))) {
194 fSelectedCtrlRect = &fTypeCtrlRect;
195 }
196 }
197
198 // Control modification
199 if (nullptr != fSelectedCtrlRect) {
200 fSelectedCtrlRect->offsetTo(SkScalarPin(x, 0.0f, kCtrlRange), fSelec tedCtrlRect->fTop);
201
202 fBevelHeight = (fHeightCtrlRect.fLeft / kCtrlRange) * kBevelHeightMa x;
egdaniel 2016/08/17 17:53:18 Can height get negative? If not it should be able
dvonbeck 2016/08/17 17:56:29 I tried it but all it does is look like the lights
dvonbeck 2016/08/17 18:28:40 Done.
203 fBevelWidth = (fWidthCtrlRect.fLeft / kCtrlRange) * kBevelWidthMax;
204 fBevelType = (SkNormalSource::BevelType)SkTMin(
205 SkScalarFloorToInt(3.0f * fTypeCtrlRect.fLeft / kCtrlRange),
206 2);
207
208 // Snap type controls to 3 positions
209 fTypeCtrlRect.offsetTo(kCtrlRange * ( ((int)fBevelType)/3.0f + 1.0f/ 6.0f ),
210 fTypeCtrlRect.fTop);
211
212 // Ensuring width is non-zero
213 fBevelWidth = SkMaxScalar(1.0f, fBevelWidth);
214
215 this->inval(nullptr);
216 return true;
217 }
218
219 // Moving light
220 if (nullptr == fSelectedCtrlRect) {
221 if (dx != 0 || dy != 0) {
222 float recipX = 1.0f / kAppWidth;
223 float recipY = 1.0f / kAppHeight;
224
225 if (0 == click->fModifierKeys) { // No modifier
226 fBlueLight = SkLights::Light::MakeDirectional(fBlueLight.col or(),
227 SkVector3::Make((kAppWidth/2.0f - x) * recipX * -3.0 f,
228 (kAppHeight/2.0f - y) * recipY * -3. 0f,
229 1.0f));
230 } else if (1 == click->fModifierKeys) { // Shift key
231 fRedLight = SkLights::Light::MakeDirectional(fRedLight.color (),
232 SkVector3::Make((kAppWidth/2.0f - x) * recipX * -3.0 f,
233 (kAppHeight/2.0f - y) * recipY * -3. 0f,
234 1.0f));
235 }
236
237 SkLights::Builder builder;
238 builder.add(fRedLight);
239 builder.add(fBlueLight);
240 builder.add(SkLights::Light::MakeAmbient(
241 SkColor3f::Make(0.4f, 0.4f, 0.4f)));
242 fLights = builder.finish();
243
244 this->inval(nullptr);
245 }
246 return true;
247 }
248
249 return true;
250 }
251
252 private:
253 static constexpr int kNumTestRects = 3;
254
255 static constexpr int kAppWidth = 400;
256 static constexpr int kAppHeight = 400;
257 static constexpr int kShapeBoundsSize = 120;
258
259 static constexpr int kCtrlRange = 150;
260 static constexpr int kBevelWidthMax = 100;
261 static constexpr int kBevelHeightMax = 50;
262
263 static constexpr int kSliderHeight = 20;
264 static constexpr int kSliderWidth = 10;
265
266 const SkRect fShapeBounds;
267
268 static constexpr int kNumControls = 3;
269 SkRect fCtrlRangeRects[kNumControls];
270 SkRect* fSelectedCtrlRect;
271 SkRect fWidthCtrlRect;
272 SkRect fHeightCtrlRect;
273 SkRect fTypeCtrlRect;
274
275 SkScalar fBevelWidth;
276 SkScalar fBevelHeight;
277 SkNormalSource::BevelType fBevelType;
278
279 sk_sp<SkLights> fLights;
280 SkLights::Light fRedLight;
281 SkLights::Light fBlueLight;
282
283 typedef SampleView INHERITED;
284 };
285
286 //////////////////////////////////////////////////////////////////////////////
287
288 static SkView* MyFactory() { return new BevelView; }
289 static SkViewRegister reg(MyFactory);
290
OLDNEW
« no previous file with comments | « gyp/samples.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698