1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SampleCode.h"
#include "SkCanvas.h"
#include "SkLightingShader.h"
#include "SkNormalSource.h"
#include "sk_tool_utils.h"
class ParentControl;
// Abstract base class for all components that a control panel must have
class Control : public SkRefCnt {
public:
Control(SkString name)
: fName(name)
, fParent(nullptr)
, fRelativePos(SkPoint::Make(0.0f, 0.0f)) {}
// Use this to propagate a click's position down to a control. Gets modulated by the component's
// relative position
bool click(const SkPoint& clickPos) {
SkPoint relativeClickPos = SkPoint::Make(clickPos.fX - fRelativePos.fX,
clickPos.fY - fRelativePos.fY);
return this->onClick(relativeClickPos);
}
// Use this to draw the control and its appropriate children. Gets modulated by the component's
// relative position.
void drawContent(SkCanvas *canvas) {
canvas->save();
canvas->translate(fRelativePos.fX, fRelativePos.fY);
this->onDrawContent(canvas);
canvas->restore();
}
/* Returns true when click position argumend lands over a control region in this control. Click
* position gets modulated by the component's relative position.
*
* @param click The position of the click in the coordinate space relative to the parent
*/
bool isInCtrlRegion(const SkPoint& click) {
SkPoint relativeClickPos = SkPoint::Make(click.fX - fRelativePos.fX,
click.fY - fRelativePos.fY);
return this->onIsInCtrlRegion(relativeClickPos);
}
// Returns height of content drawn
virtual SkScalar height() const = 0;
// Sets the parent of this component. May only be used once. Height must remain constant after
// parent is set.
void setParent(ParentControl *parent, const SkPoint& relativePos) {
SkASSERT(parent);
SkASSERT(!fParent); // No chidren transfer since relativeY would get invalid for younger kid
fParent = parent;
fRelativePos = relativePos;
this->onSetParent();
}
// Overriden by sub-classes that need to recompute fields after parent is set. Called after
// setting fParent.
virtual void onSetParent() {}
// Overriden by sub-classes that need to know when a click is released.
virtual void onClickRelease() {}
protected:
// Draws a label for the component, using its name and a passed value. Does NOT modulate by
// relative height, expects CTM to have been adjusted in advance.
void drawLabel(SkCanvas *canvas, const SkString& valueStr) const {
// TODO Cache this
sk_sp<SkTypeface> fLabelTypeface =
sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle());
SkString label;
label.append(fName);
label.append(": ");
label.append(valueStr);
SkPaint labelPaint;
labelPaint.setTypeface(fLabelTypeface);
labelPaint.setAntiAlias(true);
labelPaint.setColor(0xFFFFFFFF);
labelPaint.setTextSize(12.0f);
canvas->drawText(label.c_str(), label.size(), 0, kLabelHeight - 6.0f, labelPaint);
}
SkString fName;
ParentControl* fParent;
static constexpr SkScalar kLabelHeight = 20.0f;
private:
// Overriden by sub-class to draw component. Do not call directly, drawContent() modulates by
// relative position.
virtual void onDrawContent(SkCanvas *canvas) = 0;
// Overriden by sub-class to handle clicks. Do not call directly, click() modulates by relative
// position. Return true if holding mouse capture
virtual bool onClick(const SkPoint& clickPos) { return false; }
// Overriden by sub-classes with controls. Should return true if clickPos lands inside a control
// region, to enable mouse caputre.
virtual bool onIsInCtrlRegion(const SkPoint& clickPos) const { return false; }
// The position of the control relative to it's parent
SkPoint fRelativePos;
};
class ParentControl : public Control { // Interface for all controls that have children
public:
ParentControl(const SkString& name) : INHERITED(name) {}
// Adds a child
virtual void add(sk_sp<Control> control) = 0;
// Returns the control's width. Used to propagate width down to components that don't specify it
virtual SkScalar width() const = 0;
private:
typedef Control INHERITED;
};
class ControlPanel : public ParentControl {
public:
ControlPanel(SkScalar width)
: ParentControl(SkString("ControlPanel"))
, fWidth(width)
, fHeight(0.0f)
, fSelectedControl(-1) {}
// Width unspecified, expectation is inheritance from parent
ControlPanel() : ControlPanel(-1.0f) {}
// Use this for introducing clicks on a ControlPanel from outside of the framework. It
// propagates click release or position down the chain. Returns false when click capture is
// being released.
bool inClick(SkView::Click *inClick) {
if (SkView::Click::State::kUp_State == inClick->fState) {
this->onClickRelease();
return false;
}
return this->click(inClick->fCurr);
}
// Add children
void add(sk_sp<Control> control) override {
SkASSERT(!fParent); // Validity of parent's relativeY and fHeight depends on immutability
fControls.push_back(control);
control->setParent(this, SkPoint::Make(0.0f, fHeight));
fHeight += control->height();
}
SkScalar width() const override {
return fParent ? fParent->width() : fWidth; // Width inherited from parent if there is one
}
SkScalar height() const override {
return fHeight;
}
// Propagate click release to selected control, deselect control
void onClickRelease() override {
if (fSelectedControl >= 0) {
fControls[fSelectedControl]->onClickRelease();
}
fSelectedControl = -1;
}
// Propagate onSetParent() down to children, some might need fParent->width() refresh
void onSetParent() override {
for (int i = 0; i < fControls.count(); i++) {
fControls[i]->onSetParent();
}
}
// Holds a vertical shelf of controls. Can't be hierarchy root if not given a width value.
static sk_sp<ParentControl> Make() {
return sk_sp<ParentControl>(new ControlPanel());
}
// Holds a vertical shelf of controls. Only control that can be hooked from outside the
// framework.
static sk_sp<ParentControl> Make(SkScalar width) {
return sk_sp<ParentControl>(new ControlPanel(width));
}
protected:
// Returns true if control panel has mouse captured, false when it is ready to release
// capture
bool onClick(const SkPoint& click) override {
if (fSelectedControl == -1) { // If no child control selected, check every child
for (int i = 0; i < fControls.count(); i++) {
if (fControls[i]->isInCtrlRegion(click)) {
fSelectedControl = i;
break;
}
}
}
if (fSelectedControl >= 0) { // If child control selected, propagate click
bool keepSelection = fControls[fSelectedControl]->click(click);
if (!keepSelection) {
fSelectedControl = -1;
}
return keepSelection;
}
return false;
}
// Draw all children
void onDrawContent(SkCanvas* canvas) override {
canvas->save();
for (int i = 0; i < fControls.count(); i++) {
fControls[i]->drawContent(canvas);
}
canvas->restore();
}
// Check all children's control regions
bool onIsInCtrlRegion(const SkPoint& clickPos) const override {
for (int i = 0; i < fControls.count(); i++) {
if (fControls[i]->isInCtrlRegion(clickPos)) {
return true;
}
}
return false;
}
private:
SkScalar fWidth;
SkScalar fHeight;
SkTArray<sk_sp<Control>> fControls;
int fSelectedControl;
};
class DiscreteSliderControl : public Control {
public:
SkScalar height() const override {
return 2.0f * kLabelHeight;
}
// Set width-dependant variables when new parent is set
void onSetParent() override {
fCtrlRegion = SkRect::MakeXYWH(0.0f, kLabelHeight, fParent->width(), kSliderHeight);
fSliderRange = fParent->width() - kSliderWidth;
}
/* Make a slider for an integer value. Snaps to discrete positions.
*
* @params name The name of the control, displayed in the label
* @params output Pointer to the integer that will be set by the slider
* @params min Min value for output.
* @params max Max value for output.
*/
static sk_sp<Control> Make(SkString name, int* output, int min, int max) {
return sk_sp<Control>(new DiscreteSliderControl(name, output, min, max));
}
protected:
void onDrawContent(SkCanvas* canvas) override {
SkASSERT(fParent);
int numChoices = fMax - fMin + 1;
fSlider.offsetTo(fSliderRange * ( (*fOutput)/SkIntToScalar(numChoices)
+ 1.0f/(2.0f * numChoices) ),
fSlider.fTop);
SkString valueStr;
valueStr.appendS32(*fOutput);
this->drawLabel(canvas, valueStr);
SkPaint sliderPaint;
sliderPaint.setColor(0xFFF3F3F3);
canvas->drawRect(fSlider, sliderPaint);
SkPaint ctrlRegionPaint;
ctrlRegionPaint.setColor(0xFFFFFFFF);
ctrlRegionPaint.setStyle(SkPaint::kStroke_Style);
ctrlRegionPaint.setStrokeWidth(2.0f);
canvas->drawRect(fCtrlRegion, ctrlRegionPaint);
}
bool onClick(const SkPoint& clickPos) override {
SkASSERT(fParent);
SkScalar x = SkScalarPin(clickPos.fX, 0.0f, fSliderRange);
int numChoices = fMax - fMin + 1;
*fOutput = SkTMin(SkScalarFloorToInt(numChoices * x / fSliderRange) + fMin, fMax);
return true;
}
bool onIsInCtrlRegion(const SkPoint& clickPos) const override {
SkASSERT(fParent);
return fCtrlRegion.contains(SkRect::MakeXYWH(clickPos.fX, clickPos.fY, 1, 1));
}
private:
DiscreteSliderControl(SkString name, int* output, int min, int max)
: INHERITED(name)
, fOutput(output)
, fMin(min)
, fMax(max) {
fSlider = SkRect::MakeXYWH(0, kLabelHeight, kSliderWidth, kSliderHeight);
}
int* fOutput;
int fMin;
int fMax;
SkRect fSlider; // The rectangle that slides
// The region in which the rectangle slides. Also the region in which mouse is caputred
SkRect fCtrlRegion;
SkScalar fSliderRange; // The width in pixels over which the slider can slide
static constexpr SkScalar kSliderHeight = 20.0f;
static constexpr SkScalar kSliderWidth = 10.0f;
typedef Control INHERITED;
};
class ControlSwitcher : public ParentControl {
public:
// Add children
void add(sk_sp<Control> control) override {
SkASSERT(!fParent); // Validity of parent's relativeY and fHeight depends on immutability
fControls.push_back(control);
control->setParent(this, SkPoint::Make(0.0f, kSelectorHeight));
fHeight = SkMaxScalar(fHeight, control->height()); // Setting height to max child height.
}
SkScalar width() const override { return fParent ? (fParent->width()) : 0; }
SkScalar height() const override {
return fHeight;
}
// Propagate onClickRelease to control that currently captures mouse
void onClickRelease() override {
if (fCtrlOnClick) {
fCtrlOnClick->onClickRelease();
}
fCtrlOnClick = nullptr;
}
void onSetParent() override {
for (int i = 0; i < fControls.count(); i++) {
fControls[i]->onSetParent(); // Propagate to children
}
// Finalize control selector
// TODO can be moved to constructor if list-initialized
if (!finalizedChildren) {
fControlSelector = DiscreteSliderControl::Make(
SkString(fName), &fSelectedControl, 0, fControls.count()-1);
fControlSelector->setParent(this, SkPoint::Make(0.0f, 0.0f));
fHeight += kSelectorHeight;
SkASSERT(fControlSelector->height() <= kSelectorHeight);
}
}
/* A set of a selector and a list of controls. Displays the control from the list of controls
* with the index set by the aforementioned selector.
*
* @param name The name of the switcher. Will be displayed in the selector's label.
*/
static sk_sp<ParentControl> Make(const SkString& name) {
return sk_sp<ParentControl>(new ControlSwitcher(name));
}
protected:
// Draw selector and currently selected control
void onDrawContent(SkCanvas* canvas) override {
fControlSelector->drawContent(canvas);
fControls[fSelectedControl]->drawContent(canvas);
}
// Returns true if control panel has mouse captured, false when it is ready to release
// capture
bool onClick(const SkPoint& click) override {
if (!fCtrlOnClick) {
if (fControlSelector->isInCtrlRegion(click)) {
fCtrlOnClick = fControlSelector.get();
} else if (fControls[fSelectedControl]->isInCtrlRegion(click)) {
fCtrlOnClick = fControls[fSelectedControl].get();
}
}
if (fCtrlOnClick) {
return fCtrlOnClick->click(click);
}
return false;
}
// Is in control region of selector or currently selected control
bool onIsInCtrlRegion(const SkPoint& clickPos) const override {
if (fControlSelector->isInCtrlRegion(clickPos)) {
return true;
}
if (fControls[fSelectedControl]->isInCtrlRegion(clickPos)) {
return true;
}
return false;
}
private:
ControlSwitcher(const SkString& name)
: INHERITED(name)
, fHeight(0.0)
, fSelectedControl(0)
, fCtrlOnClick(nullptr){}
bool finalizedChildren = false;
sk_sp<Control> fControlSelector;
SkScalar fHeight;
SkTArray<sk_sp<Control>> fControls;
int fSelectedControl;
Control* fCtrlOnClick;
static constexpr SkScalar kSelectorHeight = 40.0f;
typedef ParentControl INHERITED;
};
class ContinuousSliderControl : public Control {
public:
SkScalar height() const override {
return 2.0f * kLabelHeight;
}
void onSetParent() override {
fSlider = SkRect::MakeXYWH(0, kLabelHeight, kSliderWidth, kSliderHeight);
fCtrlRegion = SkRect::MakeXYWH(0.0f, kLabelHeight, fParent->width(), kSliderHeight);
fSliderRange = fParent->width() - kSliderWidth;
}
/* Make a slider for an SkScalar.
*
* @params name The name of the control, displayed in the label
* @params output Pointer to the SkScalar that will be set by the slider
* @params min Min value for output
* @params max Max value for output
*/
static sk_sp<Control> Make(const SkString& name, SkScalar* output, SkScalar min, SkScalar max) {
return sk_sp<Control>(new ContinuousSliderControl(name, output, min, max));
}
protected:
void onDrawContent(SkCanvas* canvas) override {
SkASSERT(fParent);
SkScalar x = fSliderRange * (*fOutput - fMin) / (fMax - fMin);
fSlider.offsetTo(SkScalarPin(x, 0.0f, fSliderRange), fSlider.fTop);
SkString valueStr;
valueStr.appendScalar(*fOutput);
this->drawLabel(canvas, valueStr);
SkPaint sliderPaint;
sliderPaint.setColor(0xFFF3F3F3);
canvas->drawRect(fSlider, sliderPaint);
SkPaint ctrlRegionPaint;
ctrlRegionPaint.setColor(0xFFFFFFFF);
ctrlRegionPaint.setStyle(SkPaint::kStroke_Style);
ctrlRegionPaint.setStrokeWidth(2.0f);
canvas->drawRect(fCtrlRegion, ctrlRegionPaint);
}
bool onClick(const SkPoint& clickPos) override {
SkASSERT(fParent);
SkScalar x = SkScalarPin(clickPos.fX, 0.0f, fSliderRange);
*fOutput = (x/fSliderRange) * (fMax - fMin) + fMin;
return true;
}
bool onIsInCtrlRegion(const SkPoint& clickPos) const override {
SkASSERT(fParent);
return fCtrlRegion.contains(SkRect::MakeXYWH(clickPos.fX, clickPos.fY, 1, 1));
}
private:
ContinuousSliderControl(const SkString& name, SkScalar* output, SkScalar min, SkScalar max)
: INHERITED(name)
, fOutput(output)
, fMin(min)
, fMax(max) {}
SkScalar* fOutput;
SkScalar fMin;
SkScalar fMax;
SkRect fSlider;
SkRect fCtrlRegion;
SkScalar fSliderRange;
static constexpr SkScalar kSliderHeight = 20.0f;
static constexpr SkScalar kSliderWidth = 10.0f;
typedef Control INHERITED;
};
class RadialDirectionControl : public Control {
public:
SkScalar height() const override {
return kLabelHeight + 2.0f * kRegionRadius;
}
/* Make a direction selector.
*
* @params name The name of the control, displayed in the label
* @params output Pointer to the SkVector that will be set by the slider
*/
static sk_sp<Control> Make(const SkString& name, SkVector* output) {
return sk_sp<Control>(new RadialDirectionControl(name, output));
}
protected:
void onDrawContent(SkCanvas* canvas) override {
SkASSERT(fParent);
SkString valueStr;
valueStr.appendf("%.2f, %.2f", fOutput->fX, fOutput->fY);
this->drawLabel(canvas, valueStr);
SkPoint lineEnd = SkPoint::Make(fCtrlRegion.centerX(), fCtrlRegion.centerY())
+ (*fOutput * (kRegionRadius - kCapRadius));
SkPaint linePaint;
linePaint.setColor(0xFFF3F3F3);
linePaint.setStrokeWidth(kStrokeWidth);
linePaint.setAntiAlias(true);
linePaint.setStrokeCap(SkPaint::kRound_Cap);
canvas->drawLine(fCtrlRegion.centerX(), fCtrlRegion.centerY(),
lineEnd.fX, lineEnd.fY, linePaint);
SkPaint ctrlRegionPaint;
ctrlRegionPaint.setColor(0xFFFFFFFF);
ctrlRegionPaint.setStyle(SkPaint::kStroke_Style);
ctrlRegionPaint.setStrokeWidth(2.0f);
ctrlRegionPaint.setAntiAlias(true);
canvas->drawCircle(fCtrlRegion.centerX(), fCtrlRegion.centerY(), kRegionRadius,
ctrlRegionPaint);
}
bool onClick(const SkPoint& clickPos) override {
SkASSERT(fParent);
fOutput->fX = clickPos.fX - fCtrlRegion.centerX();
fOutput->fY = clickPos.fY - fCtrlRegion.centerY();
fOutput->normalize();
return true;
}
bool onIsInCtrlRegion(const SkPoint& clickPos) const override {
SkASSERT(fParent);
return fCtrlRegion.contains(SkRect::MakeXYWH(clickPos.fX, clickPos.fY,
1, 1));
}
private:
RadialDirectionControl(const SkString& name, SkVector* output)
: INHERITED(name)
, fOutput(output) {
fCtrlRegion = SkRect::MakeXYWH(0.0f, kLabelHeight,
kRegionRadius * 2.0f, kRegionRadius * 2.0f);
}
SkVector* fOutput;
SkRect fCtrlRegion;
static constexpr SkScalar kRegionRadius = 50.0f;
static constexpr SkScalar kStrokeWidth = 6.0f;
static constexpr SkScalar kCapRadius = kStrokeWidth / 2.0f;
typedef Control INHERITED;
};
class ColorDisplay: public Control {
public:
SkScalar height() const override {
return kHeight;
}
void onSetParent() override {
fDisplayRect = SkRect::MakeXYWH(0.0f, kPadding, fParent->width(), kHeight - kPadding);
}
/* Make a display that shows an SkColor3f.
*
* @params output Pointer to the SkColor3f that will be displayed
*/
static sk_sp<Control> Make(SkColor3f* input) {
return sk_sp<Control>(new ColorDisplay(SkString("ColorDisplay"), input));
}
protected:
void onDrawContent(SkCanvas* canvas) override {
SkASSERT(fParent);
SkPaint displayPaint;
displayPaint.setColor(SkColor4f::FromColor3f(*fInput, 1.0f).toSkColor());
canvas->drawRect(fDisplayRect, displayPaint);
}
private:
ColorDisplay(const SkString& name, SkColor3f* input)
: INHERITED(name)
, fInput(input) {}
SkColor3f* fInput;
SkRect fDisplayRect;
static constexpr SkScalar kHeight = 24.0f;
static constexpr SkScalar kPadding = 4.0f;
typedef Control INHERITED;
};
class BevelView : public SampleView {
public:
BevelView()
: fShapeBounds(SkRect::MakeWH(kShapeBoundsSize, kShapeBoundsSize))
, fControlPanel(kCtrlRange) {
this->setBGColor(0xFF666868); // Slightly colorized gray for contrast
// Controls
fBevelWidth = 25.0f;
fBevelHeight = 25.0f;
fBevelType = 0;
int currLight = 0;
fLightDefs[currLight++] =
{SkVector::Make(0.0f, 1.0f), 1.0f, SkColor3f::Make(0.6f, 0.45f, 0.3f)};
fLightDefs[currLight++] =
{SkVector::Make(0.0f, -1.0f), 1.0f, SkColor3f::Make(0.3f, 0.45f, 0.6f)};
fLightDefs[currLight++] =
{SkVector::Make(1.0f, 0.0f), 1.0f, SkColor3f::Make(0.0f, 0.0f, 0.0f)};
// Making sure we initialized all lights
SkASSERT(currLight == kNumLights);
fControlPanel.add(ContinuousSliderControl::Make(SkString("BevelWidth"), &fBevelWidth,
1.0f, kShapeBoundsSize));
fControlPanel.add(ContinuousSliderControl::Make(SkString("BevelHeight"), &fBevelHeight,
-50.0f, 50.0f));
fControlPanel.add(DiscreteSliderControl::Make(SkString("BevelType"), &fBevelType,
0, 2));
sk_sp<ParentControl> lightCtrlSelector = ControlSwitcher::Make(SkString("SelectedLight"));
for (int i = 0; i < kNumLights; i++) {
SkString name("Light");
name.appendS32(i);
sk_sp<ParentControl> currLightPanel = ControlPanel::Make();
SkString dirName(name);
dirName.append("Dir");
currLightPanel->add(RadialDirectionControl::Make(dirName, &(fLightDefs[i].fDirXY)));
SkString heightName(name);
heightName.append("Height");
currLightPanel->add(ContinuousSliderControl::Make(heightName, &(fLightDefs[i].fDirZ),
0.0f, 2.0f));
SkString redName(name);
redName.append("Red");
currLightPanel->add(ContinuousSliderControl::Make(redName, &(fLightDefs[i].fColor.fX),
0.0f, 1.0f));
SkString greenName(name);
greenName.append("Green");
currLightPanel->add(ContinuousSliderControl::Make(greenName, &(fLightDefs[i].fColor.fY),
0.0f, 1.0f));
SkString blueName(name);
blueName.append("Blue");
currLightPanel->add(ContinuousSliderControl::Make(blueName, &(fLightDefs[i].fColor.fZ),
0.0f, 1.0f));
currLightPanel->add(ColorDisplay::Make(&(fLightDefs[i].fColor)));
lightCtrlSelector->add(currLightPanel);
}
fControlPanel.add(lightCtrlSelector);
fControlPanelSelected = false;
fDirtyNormalSource = true;
fLabelTypeface = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle());
}
protected:
bool onQuery(SkEvent *evt) override {
if (SampleCode::TitleQ(*evt)) {
SampleCode::TitleR(evt, "Bevel");
return true;
}
return this->INHERITED::onQuery(evt);
}
enum Shape {
kCircle_Shape,
kRect_Shape,
};
void drawShape(enum Shape shape, SkCanvas* canvas) {
canvas->save();
SkPaint paint;
if (fDirtyNormalSource) {
fNormalSource = SkNormalSource::MakeBevel((SkNormalSource::BevelType)fBevelType,
fBevelWidth, fBevelHeight);
fDirtyNormalSource = false;
}
paint.setShader(SkLightingShader::Make(nullptr, fNormalSource, fLights));
paint.setAntiAlias(true);
paint.setColor(0xFFDDDDDD);
switch (shape) {
case kCircle_Shape:
canvas->drawCircle(fShapeBounds.centerX(), fShapeBounds.centerY(),
fShapeBounds.width()/2.0f, paint);
break;
case kRect_Shape:
canvas->drawRect(fShapeBounds, paint);
break;
default:
SkDEBUGFAIL("Invalid shape enum for drawShape");
}
canvas->restore();
}
void onDrawContent(SkCanvas *canvas) override {
canvas->save();
canvas->resetMatrix(); // Force static control panel position
fControlPanel.drawContent(canvas);
canvas->restore();
SkLights::Builder builder;
for (int i = 0; i < kNumLights; i++) {
builder.add(SkLights::Light::MakeDirectional(fLightDefs[i].fColor,
SkPoint3::Make(fLightDefs[i].fDirXY.fX,
fLightDefs[i].fDirXY.fY,
fLightDefs[i].fDirZ)));
}
builder.setAmbientLightColor(SkColor3f::Make(0.4f, 0.4f, 0.4f));
fLights = builder.finish();
// Draw shapes
SkScalar xPos = kCtrlRange + 25.0f;
SkScalar yPos = fShapeBounds.height();
for (Shape shape : { kCircle_Shape, kRect_Shape }) {
canvas->save();
canvas->translate(xPos, yPos);
this->drawShape(shape, canvas);
canvas->restore();
xPos += 1.2f * fShapeBounds.width();
}
}
SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
return new SkView::Click(this);
}
bool onClick(Click *click) override {
// Control panel mouse handling
fControlPanelSelected = fControlPanel.inClick(click);
if (fControlPanelSelected) { // Control modification
fDirtyNormalSource = true;
this->inval(nullptr);
return true;
}
// TODO move shapes
this->inval(nullptr);
return true;
}
private:
static constexpr int kNumTestRects = 3;
static constexpr SkScalar kShapeBoundsSize = 120.0f;
static constexpr SkScalar kCtrlRange = 150.0f;
static constexpr int kNumLights = 3;
const SkRect fShapeBounds;
SkScalar fBevelWidth;
SkScalar fBevelHeight;
int fBevelType;
sk_sp<SkNormalSource> fNormalSource;
bool fDirtyNormalSource;
sk_sp<SkLights> fLights;
struct LightDef {
SkVector fDirXY;
SkScalar fDirZ;
SkColor3f fColor;
LightDef() {}
LightDef(SkVector dirXY, SkScalar dirZ, SkColor3f color)
: fDirXY(dirXY)
, fDirZ(dirZ)
, fColor(color) {}
};
LightDef fLightDefs[kNumLights];
ControlPanel fControlPanel;
bool fControlPanelSelected;
sk_sp<SkTypeface> fLabelTypeface;
typedef SampleView INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
static SkView* MyFactory() { return new BevelView; }
static SkViewRegister reg(MyFactory);
|