Chromium Code Reviews| 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 "SkLightingShader.h" | |
| 10 #include "SkNormalSource.h" | |
|
robertphillips
2016/08/16 17:02:42
Do we need the next three includes ?
dvonbeck
2016/08/16 19:08:50
I do need that last one, the other 2 are from a pr
| |
| 11 #include "SkPictureRecorder.h" | |
| 12 #include "SkSurface.h" | |
| 13 #include "sk_tool_utils.h" | |
| 14 | |
| 15 | |
| 16 class BevelView : public SampleView { | |
| 17 public: | |
| 18 BevelView() | |
|
robertphillips
2016/08/16 17:02:42
I don't know if you're getting any mileage out of
dvonbeck
2016/08/16 19:08:50
I also use them to keep track of the state of ligh
| |
| 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 fRect = SkRect::MakeIWH(kRectSize, kRectSize); | |
| 33 | |
| 34 // Controls | |
| 35 | |
| 36 int currY = kSliderHeight; | |
| 37 | |
|
robertphillips
2016/08/16 17:02:42
it is odd that you touch fCtrlRangeRects twice wit
dvonbeck
2016/08/16 19:08:50
True! I changed it
| |
| 38 fCtrlRangeRects[0] = SkRect::MakeIWH(kCtrlRange + kSliderWidth, kSliderH eight); | |
| 39 fWidthCtrlRect = SkRect::MakeIWH(kSliderWidth, kSliderHeight); | |
| 40 fCtrlRangeRects[0].offset(0, SkIntToScalar(currY)); | |
| 41 fWidthCtrlRect.offset(0.3*kCtrlRange, SkIntToScalar(currY)); | |
| 42 fBevelWidth = 0.3*kBevelWidthMax; | |
| 43 currY += 2 * kSliderHeight; | |
| 44 | |
| 45 fCtrlRangeRects[1] = SkRect::MakeIWH(kCtrlRange + kSliderWidth, kSliderH eight); | |
| 46 fHeightCtrlRect = SkRect::MakeIWH(kSliderWidth, kSliderHeight); | |
| 47 fCtrlRangeRects[1].offset(0, SkIntToScalar(currY)); | |
| 48 fHeightCtrlRect.offset(0.3*kCtrlRange, SkIntToScalar(currY)); | |
| 49 fBevelHeight = 0.3*kBevelHeightMax; | |
| 50 currY += 2 * kSliderHeight; | |
| 51 | |
| 52 fCtrlRangeRects[2] = SkRect::MakeIWH(kCtrlRange + kSliderWidth, kSliderH eight); | |
| 53 fTypeCtrlRect = SkRect::MakeIWH(kSliderWidth, kSliderHeight); | |
| 54 fCtrlRangeRects[2].offset(0, SkIntToScalar(currY)); | |
| 55 fTypeCtrlRect.offset((1.0f/6.0f)*kCtrlRange, SkIntToScalar(currY)); | |
| 56 fBevelType = (SkNormalSource::BevelType) 0; | |
| 57 currY += 2 * kSliderHeight; | |
| 58 | |
| 59 fSelectedRect = nullptr; | |
| 60 } | |
| 61 | |
| 62 protected: | |
| 63 bool onQuery(SkEvent *evt) override { | |
| 64 if (SampleCode::TitleQ(*evt)) { | |
| 65 SampleCode::TitleR(evt, "bevel"); | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 return this->INHERITED::onQuery(evt); | |
| 70 } | |
| 71 | |
| 72 enum Shape { | |
| 73 kCircle_Shape, | |
| 74 kRect_Shape, | |
| 75 kLast_Shape = kRect_Shape | |
| 76 }; | |
| 77 void drawShape(enum Shape shape, SkCanvas* canvas) { | |
| 78 canvas->save(); | |
| 79 | |
| 80 SkPaint paint; | |
| 81 | |
| 82 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeBevel(fBevelTyp e, fBevelWidth, | |
| 83 fBevelHei ght); | |
| 84 | |
| 85 paint.setShader(SkLightingShader::Make(nullptr, std::move(normalSource), fLights)); | |
| 86 paint.setAntiAlias(true); | |
| 87 paint.setColor(0xFFDDDDDD); | |
|
robertphillips
2016/08/16 17:02:42
missing ' ' here ?
dvonbeck
2016/08/16 19:08:50
Done.
| |
| 88 switch(shape) { | |
| 89 case kCircle_Shape: | |
| 90 canvas->drawCircle(fRect.centerX(), fRect.centerY(), fRect.width ()/2.0f, paint); | |
| 91 break; | |
| 92 case kRect_Shape: | |
| 93 canvas->drawRect(fRect, paint); | |
| 94 break; | |
| 95 default: | |
| 96 SkDEBUGFAIL("Invalid shape enum for drawShape"); | |
| 97 } | |
| 98 | |
| 99 canvas->restore(); | |
| 100 } | |
| 101 | |
| 102 void onDrawContent(SkCanvas *canvas) override { | |
| 103 // Draw controls | |
| 104 SkPaint ctrlRectPaint; | |
| 105 ctrlRectPaint.setColor(0xFFF3F3F3); | |
| 106 canvas->drawRect(fWidthCtrlRect, ctrlRectPaint); | |
| 107 canvas->drawRect(fHeightCtrlRect, ctrlRectPaint); | |
| 108 canvas->drawRect(fTypeCtrlRect, ctrlRectPaint); | |
| 109 | |
| 110 SkPaint ctrlRectRangePaint; | |
| 111 ctrlRectRangePaint.setColor(0xFFFFFFFF); | |
| 112 ctrlRectRangePaint.setStyle(SkPaint::kStroke_Style); | |
| 113 ctrlRectRangePaint.setStrokeWidth(2.0f); | |
| 114 | |
|
robertphillips
2016/08/16 17:02:42
use kNumControls ?
dvonbeck
2016/08/16 19:08:50
Done.
| |
| 115 #define ARRAY_LEN(x) (sizeof(x)/sizeof(x[0])) | |
| 116 for (size_t i = 0; i < ARRAY_LEN(fCtrlRangeRects); i++) { | |
| 117 canvas->drawRect(fCtrlRangeRects[i], ctrlRectRangePaint); | |
| 118 } | |
| 119 | |
| 120 // Draw labels | |
| 121 constexpr SkScalar kTextSize = 12.0f; | |
| 122 SkString widthLabel, heightLabel, typeLabel; | |
| 123 SkPaint labelPaint; | |
| 124 labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-ser if", | |
| 125 SkFontSty le())); | |
| 126 labelPaint.setAntiAlias(true); | |
| 127 labelPaint.setColor(0xFFFFFFFF); | |
| 128 labelPaint.setTextSize(kTextSize); | |
| 129 | |
| 130 widthLabel.appendf("BevelWidth: %f", fBevelWidth); | |
| 131 heightLabel.appendf("BevelHeight: %f", fBevelHeight); | |
| 132 typeLabel.append("BevelType: "); | |
| 133 | |
| 134 switch (fBevelType) { | |
| 135 case SkNormalSource::BevelType::kLinear: | |
| 136 typeLabel.append("Linear"); | |
| 137 break; | |
| 138 case SkNormalSource::BevelType::kRoundedIn: | |
| 139 typeLabel.append("RoundedIn"); | |
| 140 break; | |
| 141 case SkNormalSource::BevelType::kRoundedOut: | |
| 142 typeLabel.append("RoundedOut"); | |
| 143 break; | |
| 144 } | |
| 145 | |
| 146 canvas->drawText(widthLabel.c_str(), widthLabel.size(), 0, | |
| 147 fWidthCtrlRect.fTop - kTextSize/2.0f, labelPaint); | |
| 148 canvas->drawText(heightLabel.c_str(), heightLabel.size(), 0, | |
| 149 fHeightCtrlRect.fTop - kTextSize/2.0f, labelPaint); | |
| 150 canvas->drawText(typeLabel.c_str(), typeLabel.size(), 0, | |
| 151 fTypeCtrlRect.fTop - kTextSize/2.0f, labelPaint); | |
| 152 | |
| 153 // Draw shapes | |
| 154 SkScalar xPos = kCtrlRange + 25; | |
| 155 SkScalar yPos = fRect.height(); | |
|
robertphillips
2016/08/16 17:02:42
for (auto shape : { kCircle_Shape, kRect_Shape })
dvonbeck
2016/08/16 19:08:50
Done.
| |
| 156 for (int shapeInt = 0; shapeInt <= kLast_Shape; shapeInt++) { | |
| 157 Shape shape = (Shape)shapeInt; | |
| 158 | |
| 159 canvas->save(); | |
| 160 canvas->translate(xPos, yPos); | |
| 161 this->drawShape(shape, canvas); | |
| 162 canvas->restore(); | |
| 163 | |
| 164 xPos += 1.2f * fRect.width(); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { | |
| 169 return new SkView::Click(this); | |
| 170 } | |
| 171 | |
| 172 bool onClick(Click *click) override { | |
| 173 SkScalar x = click->fCurr.fX; | |
| 174 SkScalar y = click->fCurr.fY; | |
| 175 | |
| 176 SkScalar dx = x - click->fPrev.fX; | |
| 177 SkScalar dy = y - click->fPrev.fY; | |
| 178 | |
| 179 // Control deselection | |
| 180 if (Click::State::kUp_State == click->fState) { | |
| 181 fSelectedRect = nullptr; | |
| 182 return true; | |
| 183 } | |
| 184 | |
| 185 // Control selection | |
| 186 if (nullptr == fSelectedRect && Click::State::kDown_State == click->fSta te) { | |
| 187 if (fWidthCtrlRect.contains(SkRect::MakeXYWH(x, y, 1, 1))) { | |
| 188 fSelectedRect = &fWidthCtrlRect; | |
| 189 } else if (fHeightCtrlRect.contains(SkRect::MakeXYWH(x, y, 1, 1))) { | |
| 190 fSelectedRect = &fHeightCtrlRect; | |
| 191 } else if (fTypeCtrlRect.contains(SkRect::MakeXYWH(x, y, 1, 1))) { | |
| 192 fSelectedRect = &fTypeCtrlRect; | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 // Control modification | |
| 197 if (nullptr != fSelectedRect) { | |
| 198 fSelectedRect->offsetTo(SkScalarPin(x, 0.0f, kCtrlRange), fSelectedR ect->fTop); | |
| 199 | |
| 200 fBevelHeight = (fHeightCtrlRect.fLeft / kCtrlRange) * kBevelHeightMa x; | |
| 201 fBevelWidth = (fWidthCtrlRect.fLeft / kCtrlRange) * kBevelWidthMax; | |
| 202 fBevelType = (SkNormalSource::BevelType)SkTMin( | |
| 203 SkScalarFloorToInt(3.0f * fTypeCtrlRect.fLeft / kCtrlRange), | |
| 204 2); | |
| 205 | |
| 206 // Snap type controls to 3 positions | |
| 207 fTypeCtrlRect.offsetTo(kCtrlRange * ( ((int)fBevelType)/3.0f + 1.0f/ 6.0f ), | |
| 208 fTypeCtrlRect.fTop); | |
| 209 | |
| 210 // Ensuring width is non-zero | |
| 211 fBevelWidth = SkMaxScalar(1.0f, fBevelWidth); | |
| 212 | |
| 213 this->inval(nullptr); | |
| 214 return true; | |
| 215 } | |
| 216 | |
| 217 // Moving light | |
| 218 if (nullptr == fSelectedRect) { | |
| 219 if (dx != 0 || dy != 0) { | |
| 220 float recipX = 1.0f / kAppWidth; | |
| 221 float recipY = 1.0f / kAppHeight; | |
| 222 | |
| 223 if (0 == click->fModifierKeys) { // No modifier | |
| 224 fBlueLight = SkLights::Light::MakeDirectional(fBlueLight.col or(), | |
| 225 SkVector3::Make((kAppWidth/2.0f - x) * recipX * -3.0 f, | |
| 226 (kAppHeight/2.0f - y) * recipY * -3. 0f, | |
| 227 1.0f)); | |
| 228 } else if (1 == click->fModifierKeys) { // Shift key | |
| 229 fRedLight = SkLights::Light::MakeDirectional(fRedLight.color (), | |
| 230 SkVector3::Make((kAppWidth/2.0f - x) * recipX * -3.0 f, | |
| 231 (kAppHeight/2.0f - y) * recipY * -3. 0f, | |
| 232 1.0f)); | |
| 233 } | |
| 234 | |
| 235 SkLights::Builder builder; | |
| 236 builder.add(fRedLight); | |
| 237 builder.add(fBlueLight); | |
| 238 builder.add(SkLights::Light::MakeAmbient( | |
| 239 SkColor3f::Make(0.4f, 0.4f, 0.4f))); | |
| 240 fLights = builder.finish(); | |
| 241 | |
| 242 this->inval(nullptr); | |
| 243 } | |
| 244 return true; | |
| 245 } | |
| 246 | |
| 247 return true; | |
| 248 } | |
| 249 | |
| 250 private: | |
| 251 static constexpr int kNumTestRects = 3; | |
| 252 | |
| 253 static const int kAppWidth = 400; | |
| 254 static const int kAppHeight = 400; | |
| 255 static const int kRectSize = 120; | |
| 256 | |
| 257 static const int kCtrlRange = 150; | |
| 258 static const int kBevelWidthMax = 50; | |
| 259 static const int kBevelHeightMax = 50; | |
| 260 | |
| 261 static const int kSliderHeight = 20; | |
| 262 static const int kSliderWidth = 10; | |
| 263 | |
|
robertphillips
2016/08/16 17:02:42
What's this for ?
dvonbeck
2016/08/16 19:08:50
Oops, leftover from old code. Removed.
| |
| 264 sk_sp<SkPicture> fPicture; | |
| 265 | |
|
robertphillips
2016/08/16 17:02:42
more descriptive name for 'fRect' ?
can this be c
dvonbeck
2016/08/16 19:08:50
Done.
| |
| 266 SkRect fRect; | |
| 267 sk_sp<SkLights> fLights; | |
| 268 | |
| 269 SkRect* fSelectedRect; | |
| 270 SkRect fWidthCtrlRect; | |
| 271 SkRect fHeightCtrlRect; | |
| 272 SkRect fTypeCtrlRect; | |
|
robertphillips
2016/08/16 17:02:42
Add kNumControls ?
dvonbeck
2016/08/16 19:08:50
Done.
| |
| 273 SkRect fCtrlRangeRects[3]; | |
| 274 | |
| 275 SkScalar fBevelWidth; | |
| 276 SkScalar fBevelHeight; | |
| 277 SkNormalSource::BevelType fBevelType; | |
| 278 | |
| 279 SkLights::Light fRedLight; | |
| 280 SkLights::Light fBlueLight; | |
| 281 | |
| 282 typedef SampleView INHERITED; | |
| 283 }; | |
| 284 | |
| 285 ////////////////////////////////////////////////////////////////////////////// | |
| 286 | |
| 287 static SkView* MyFactory() { return new BevelView; } | |
| 288 static SkViewRegister reg(MyFactory); | |
| 289 | |
| OLD | NEW |