OLD | NEW |
---|---|
(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 | |
robertphillips
2016/08/17 18:11:43
fuse these two lines with MakeLTRB ?
& for other
dvonbeck
2016/08/17 18:28:40
I feel like it is easier to read if I put it in 2
egdaniel
2016/08/17 18:34:12
There is a MakeXYWH which I think is the best opti
dvonbeck
2016/08/17 18:49:53
Done.
| |
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 // Draw controls | |
102 SkPaint ctrlRectPaint; | |
103 ctrlRectPaint.setColor(0xFFF3F3F3); | |
104 canvas->drawRect(fWidthCtrlRect, ctrlRectPaint); | |
105 canvas->drawRect(fHeightCtrlRect, ctrlRectPaint); | |
106 canvas->drawRect(fTypeCtrlRect, ctrlRectPaint); | |
107 | |
108 SkPaint ctrlRectRangePaint; | |
109 ctrlRectRangePaint.setColor(0xFFFFFFFF); | |
110 ctrlRectRangePaint.setStyle(SkPaint::kStroke_Style); | |
111 ctrlRectRangePaint.setStrokeWidth(2.0f); | |
112 | |
113 for (size_t i = 0; i < kNumControls; i++) { | |
114 canvas->drawRect(fCtrlRangeRects[i], ctrlRectRangePaint); | |
115 } | |
116 | |
117 // Draw labels | |
118 constexpr SkScalar kTextSize = 12.0f; | |
119 SkString widthLabel, heightLabel, typeLabel; | |
120 SkPaint labelPaint; | |
121 labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-ser if", | |
122 SkFontSty le())); | |
123 labelPaint.setAntiAlias(true); | |
124 labelPaint.setColor(0xFFFFFFFF); | |
125 labelPaint.setTextSize(kTextSize); | |
126 | |
127 widthLabel.appendf("BevelWidth: %f", fBevelWidth); | |
128 heightLabel.appendf("BevelHeight: %f", fBevelHeight); | |
129 typeLabel.append("BevelType: "); | |
130 | |
131 switch (fBevelType) { | |
132 case SkNormalSource::BevelType::kLinear: | |
133 typeLabel.append("Linear"); | |
134 break; | |
135 case SkNormalSource::BevelType::kRoundedIn: | |
136 typeLabel.append("RoundedIn"); | |
137 break; | |
138 case SkNormalSource::BevelType::kRoundedOut: | |
139 typeLabel.append("RoundedOut"); | |
140 break; | |
141 } | |
142 | |
143 canvas->drawText(widthLabel.c_str(), widthLabel.size(), 0, | |
144 fWidthCtrlRect.fTop - kTextSize/2.0f, labelPaint); | |
145 canvas->drawText(heightLabel.c_str(), heightLabel.size(), 0, | |
146 fHeightCtrlRect.fTop - kTextSize/2.0f, labelPaint); | |
147 canvas->drawText(typeLabel.c_str(), typeLabel.size(), 0, | |
148 fTypeCtrlRect.fTop - kTextSize/2.0f, labelPaint); | |
149 | |
150 // Draw shapes | |
151 SkScalar xPos = kCtrlRange + 25; | |
152 SkScalar yPos = fShapeBounds.height(); | |
153 for (Shape shape : { kCircle_Shape, kRect_Shape }) { | |
154 canvas->save(); | |
155 canvas->translate(xPos, yPos); | |
156 this->drawShape(shape, canvas); | |
157 canvas->restore(); | |
158 | |
159 xPos += 1.2f * fShapeBounds.width(); | |
160 } | |
161 } | |
162 | |
163 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { | |
164 return new SkView::Click(this); | |
165 } | |
166 | |
167 bool onClick(Click *click) override { | |
168 SkScalar x = click->fCurr.fX; | |
169 SkScalar y = click->fCurr.fY; | |
170 | |
171 SkScalar dx = x - click->fPrev.fX; | |
172 SkScalar dy = y - click->fPrev.fY; | |
173 | |
174 // Control deselection | |
175 if (Click::State::kUp_State == click->fState) { | |
176 fSelectedCtrlRect = nullptr; | |
177 return true; | |
178 } | |
179 | |
180 // Control selection | |
181 if (nullptr == fSelectedCtrlRect && Click::State::kDown_State == click-> fState) { | |
182 if (fWidthCtrlRect.contains(SkRect::MakeXYWH(x, y, 1, 1))) { | |
183 fSelectedCtrlRect = &fWidthCtrlRect; | |
184 } else if (fHeightCtrlRect.contains(SkRect::MakeXYWH(x, y, 1, 1))) { | |
185 fSelectedCtrlRect = &fHeightCtrlRect; | |
186 } else if (fTypeCtrlRect.contains(SkRect::MakeXYWH(x, y, 1, 1))) { | |
187 fSelectedCtrlRect = &fTypeCtrlRect; | |
188 } | |
189 } | |
190 | |
191 // Control modification | |
192 if (nullptr != fSelectedCtrlRect) { | |
193 fSelectedCtrlRect->offsetTo(SkScalarPin(x, 0.0f, kCtrlRange), fSelec tedCtrlRect->fTop); | |
194 | |
195 fBevelHeight = (fHeightCtrlRect.fLeft / kCtrlRange) * kBevelHeightMa x; | |
196 fBevelWidth = (fWidthCtrlRect.fLeft / kCtrlRange) * kBevelWidthMax; | |
197 fBevelType = (SkNormalSource::BevelType)SkTMin( | |
198 SkScalarFloorToInt(3.0f * fTypeCtrlRect.fLeft / kCtrlRange), | |
199 2); | |
200 | |
201 // Snap type controls to 3 positions | |
202 fTypeCtrlRect.offsetTo(kCtrlRange * ( ((int)fBevelType)/3.0f + 1.0f/ 6.0f ), | |
203 fTypeCtrlRect.fTop); | |
204 | |
205 // Ensuring width is non-zero | |
206 fBevelWidth = SkMaxScalar(1.0f, fBevelWidth); | |
207 | |
208 this->inval(nullptr); | |
209 return true; | |
210 } | |
211 | |
212 // Moving light | |
213 if (nullptr == fSelectedCtrlRect) { | |
214 if (dx != 0 || dy != 0) { | |
215 float recipX = 1.0f / kAppWidth; | |
216 float recipY = 1.0f / kAppHeight; | |
217 | |
218 if (0 == click->fModifierKeys) { // No modifier | |
219 fBlueLight = SkLights::Light::MakeDirectional(fBlueLight.col or(), | |
220 SkVector3::Make((kAppWidth/2.0f - x) * recipX * -3.0 f, | |
221 (kAppHeight/2.0f - y) * recipY * -3. 0f, | |
222 1.0f)); | |
223 } else if (1 == click->fModifierKeys) { // Shift key | |
224 fRedLight = SkLights::Light::MakeDirectional(fRedLight.color (), | |
225 SkVector3::Make((kAppWidth/2.0f - x) * recipX * -3.0 f, | |
226 (kAppHeight/2.0f - y) * recipY * -3. 0f, | |
227 1.0f)); | |
228 } | |
229 | |
230 SkLights::Builder builder; | |
231 builder.add(fRedLight); | |
232 builder.add(fBlueLight); | |
233 builder.add(SkLights::Light::MakeAmbient( | |
234 SkColor3f::Make(0.4f, 0.4f, 0.4f))); | |
235 fLights = builder.finish(); | |
236 | |
237 this->inval(nullptr); | |
238 } | |
239 return true; | |
240 } | |
241 | |
242 return true; | |
243 } | |
244 | |
245 private: | |
246 static constexpr int kNumTestRects = 3; | |
247 | |
248 static constexpr int kAppWidth = 400; | |
249 static constexpr int kAppHeight = 400; | |
250 static constexpr int kShapeBoundsSize = 120; | |
251 | |
252 static constexpr int kCtrlRange = 150; | |
253 static constexpr int kBevelWidthMax = 50; | |
254 static constexpr int kBevelHeightMax = 50; | |
255 | |
256 static constexpr int kSliderHeight = 20; | |
257 static constexpr int kSliderWidth = 10; | |
258 | |
259 const SkRect fShapeBounds; | |
260 | |
261 static constexpr int kNumControls = 3; | |
262 SkRect fCtrlRangeRects[kNumControls]; | |
263 SkRect* fSelectedCtrlRect; | |
264 SkRect fWidthCtrlRect; | |
265 SkRect fHeightCtrlRect; | |
266 SkRect fTypeCtrlRect; | |
267 | |
268 SkScalar fBevelWidth; | |
269 SkScalar fBevelHeight; | |
270 SkNormalSource::BevelType fBevelType; | |
271 | |
272 sk_sp<SkLights> fLights; | |
273 SkLights::Light fRedLight; | |
274 SkLights::Light fBlueLight; | |
275 | |
276 typedef SampleView INHERITED; | |
277 }; | |
278 | |
279 ////////////////////////////////////////////////////////////////////////////// | |
280 | |
281 static SkView* MyFactory() { return new BevelView; } | |
282 static SkViewRegister reg(MyFactory); | |
283 | |
OLD | NEW |