caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBlendMode.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
| 12 | #include "include/core/SkImageInfo.h" |
| 13 | #include "include/core/SkMatrix.h" |
| 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkPath.h" |
| 16 | #include "include/core/SkPathMeasure.h" |
| 17 | #include "include/core/SkPoint.h" |
| 18 | #include "include/core/SkRRect.h" |
| 19 | #include "include/core/SkRect.h" |
| 20 | #include "include/core/SkRefCnt.h" |
| 21 | #include "include/core/SkScalar.h" |
| 22 | #include "include/core/SkShader.h" |
| 23 | #include "include/core/SkString.h" |
| 24 | #include "include/core/SkSurface.h" |
| 25 | #include "include/core/SkTypes.h" |
| 26 | #include "include/private/SkTArray.h" |
| 27 | #include "include/private/SkTemplates.h" |
| 28 | #include "include/utils/SkTextUtils.h" |
| 29 | #include "samplecode/Sample.h" |
| 30 | #include "src/core/SkGeometry.h" |
Chris Dalton | de50037 | 2020-05-05 15:06:30 -0600 | [diff] [blame] | 31 | #include "src/core/SkPathPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 32 | #include "src/core/SkPointPriv.h" |
| 33 | #include "src/core/SkStroke.h" |
| 34 | #include "tools/ToolUtils.h" |
Ben Wagner | cb3d49c | 2018-03-14 15:07:43 -0400 | [diff] [blame] | 35 | |
| 36 | #include <cfloat> |
| 37 | |
| 38 | class SkEvent; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 39 | |
| 40 | static bool hittest(const SkPoint& target, SkScalar x, SkScalar y) { |
| 41 | const SkScalar TOL = 7; |
| 42 | return SkPoint::Distance(target, SkPoint::Make(x, y)) <= TOL; |
| 43 | } |
| 44 | |
| 45 | static int getOnCurvePoints(const SkPath& path, SkPoint storage[]) { |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 46 | int count = 0; |
Chris Dalton | de50037 | 2020-05-05 15:06:30 -0600 | [diff] [blame] | 47 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 48 | switch (verb) { |
Chris Dalton | de50037 | 2020-05-05 15:06:30 -0600 | [diff] [blame] | 49 | case SkPathVerb::kMove: |
| 50 | case SkPathVerb::kLine: |
| 51 | case SkPathVerb::kQuad: |
| 52 | case SkPathVerb::kConic: |
| 53 | case SkPathVerb::kCubic: |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 54 | storage[count++] = pts[0]; |
| 55 | break; |
| 56 | default: |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | return count; |
| 61 | } |
| 62 | |
| 63 | static void getContourCounts(const SkPath& path, SkTArray<int>* contourCounts) { |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 64 | int count = 0; |
Chris Dalton | de50037 | 2020-05-05 15:06:30 -0600 | [diff] [blame] | 65 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 66 | switch (verb) { |
Chris Dalton | de50037 | 2020-05-05 15:06:30 -0600 | [diff] [blame] | 67 | case SkPathVerb::kMove: |
| 68 | case SkPathVerb::kLine: |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 69 | count += 1; |
| 70 | break; |
Chris Dalton | de50037 | 2020-05-05 15:06:30 -0600 | [diff] [blame] | 71 | case SkPathVerb::kQuad: |
| 72 | case SkPathVerb::kConic: |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 73 | count += 2; |
| 74 | break; |
Chris Dalton | de50037 | 2020-05-05 15:06:30 -0600 | [diff] [blame] | 75 | case SkPathVerb::kCubic: |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 76 | count += 3; |
| 77 | break; |
Chris Dalton | de50037 | 2020-05-05 15:06:30 -0600 | [diff] [blame] | 78 | case SkPathVerb::kClose: |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 79 | contourCounts->push_back(count); |
| 80 | count = 0; |
| 81 | break; |
| 82 | default: |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | if (count > 0) { |
| 87 | contourCounts->push_back(count); |
| 88 | } |
| 89 | } |
| 90 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 91 | static void erase(const sk_sp<SkSurface>& surface) { |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 92 | SkCanvas* canvas = surface->getCanvas(); |
| 93 | if (canvas) { |
| 94 | canvas->clear(SK_ColorTRANSPARENT); |
| 95 | } |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | struct StrokeTypeButton { |
| 99 | SkRect fBounds; |
| 100 | char fLabel; |
| 101 | bool fEnabled; |
| 102 | }; |
| 103 | |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 104 | struct CircleTypeButton : public StrokeTypeButton { |
| 105 | bool fFill; |
| 106 | }; |
| 107 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 108 | class QuadStrokerView : public Sample { |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 109 | enum { |
| 110 | SKELETON_COLOR = 0xFF0000FF, |
| 111 | WIREFRAME_COLOR = 0x80FF0000 |
| 112 | }; |
| 113 | |
| 114 | enum { |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 115 | kCount = 18 |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 116 | }; |
| 117 | SkPoint fPts[kCount]; |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 118 | SkRect fWeightControl; |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 119 | SkRect fRadiusControl; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 120 | SkRect fErrorControl; |
| 121 | SkRect fWidthControl; |
| 122 | SkRect fBounds; |
| 123 | SkMatrix fMatrix, fInverse; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 124 | sk_sp<SkShader> fShader; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 125 | sk_sp<SkSurface> fMinSurface; |
| 126 | sk_sp<SkSurface> fMaxSurface; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 127 | StrokeTypeButton fCubicButton; |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 128 | StrokeTypeButton fConicButton; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 129 | StrokeTypeButton fQuadButton; |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 130 | StrokeTypeButton fArcButton; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 131 | StrokeTypeButton fRRectButton; |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 132 | CircleTypeButton fCircleButton; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 133 | StrokeTypeButton fTextButton; |
| 134 | SkString fText; |
| 135 | SkScalar fTextSize; |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 136 | SkScalar fWeight; |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 137 | SkScalar fRadius; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 138 | SkScalar fWidth, fDWidth; |
| 139 | SkScalar fWidthScale; |
| 140 | int fW, fH, fZoom; |
| 141 | bool fAnimate; |
| 142 | bool fDrawRibs; |
| 143 | bool fDrawTangents; |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 144 | bool fDrawTDivs; |
caryclark | a76b7a3b | 2015-05-22 06:26:52 -0700 | [diff] [blame] | 145 | #ifdef SK_DEBUG |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 146 | #define kStrokerErrorMin 0.001f |
| 147 | #define kStrokerErrorMax 5 |
| 148 | #endif |
| 149 | #define kWidthMin 1 |
| 150 | #define kWidthMax 100 |
| 151 | public: |
| 152 | QuadStrokerView() { |
| 153 | this->setBGColor(SK_ColorLTGRAY); |
| 154 | |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 155 | fPts[0].set(50, 200); // cubic |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 156 | fPts[1].set(50, 100); |
| 157 | fPts[2].set(150, 50); |
| 158 | fPts[3].set(300, 50); |
| 159 | |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 160 | fPts[4].set(350, 200); // conic |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 161 | fPts[5].set(350, 100); |
| 162 | fPts[6].set(450, 50); |
| 163 | |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 164 | fPts[7].set(150, 300); // quad |
| 165 | fPts[8].set(150, 200); |
| 166 | fPts[9].set(250, 150); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 167 | |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 168 | fPts[10].set(250, 200); // arc |
| 169 | fPts[11].set(250, 300); |
| 170 | fPts[12].set(150, 350); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 171 | |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 172 | fPts[13].set(200, 200); // rrect |
| 173 | fPts[14].set(400, 400); |
| 174 | |
| 175 | fPts[15].set(250, 250); // oval |
| 176 | fPts[16].set(450, 450); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 177 | |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 178 | fText = "a"; |
| 179 | fTextSize = 12; |
| 180 | fWidth = 50; |
| 181 | fDWidth = 0.25f; |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 182 | fWeight = 1; |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 183 | fRadius = 150; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 184 | |
| 185 | fCubicButton.fLabel = 'C'; |
| 186 | fCubicButton.fEnabled = false; |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 187 | fConicButton.fLabel = 'K'; |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 188 | fConicButton.fEnabled = false; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 189 | fQuadButton.fLabel = 'Q'; |
| 190 | fQuadButton.fEnabled = false; |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 191 | fArcButton.fLabel = 'A'; |
| 192 | fArcButton.fEnabled = true; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 193 | fRRectButton.fLabel = 'R'; |
| 194 | fRRectButton.fEnabled = false; |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 195 | fCircleButton.fLabel = 'O'; |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 196 | fCircleButton.fEnabled = true; |
| 197 | fCircleButton.fFill = true; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 198 | fTextButton.fLabel = 'T'; |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 199 | fTextButton.fEnabled = false; |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 200 | fAnimate = false; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 201 | setAsNeeded(); |
| 202 | } |
| 203 | |
| 204 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 205 | SkString name() override { return SkString("QuadStroker"); } |
| 206 | |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 207 | bool onChar(SkUnichar uni) override { |
| 208 | if (fTextButton.fEnabled) { |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 209 | switch (uni) { |
| 210 | case ' ': |
| 211 | fText = ""; |
| 212 | break; |
| 213 | case '-': |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 214 | fTextSize = std::max(1.0f, fTextSize - 1); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 215 | break; |
| 216 | case '+': |
| 217 | case '=': |
| 218 | fTextSize += 1; |
| 219 | break; |
| 220 | default: |
| 221 | fText.appendUnichar(uni); |
| 222 | } |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 223 | return true; |
| 224 | } |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 225 | return false; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 226 | } |
| 227 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 228 | void onSizeChange() override { |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 229 | fRadiusControl.setXYWH(this->width() - 200, 30, 30, 400); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 230 | fWeightControl.setXYWH(this->width() - 150, 30, 30, 400); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 231 | fErrorControl.setXYWH(this->width() - 100, 30, 30, 400); |
| 232 | fWidthControl.setXYWH(this->width() - 50, 30, 30, 400); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 233 | int buttonOffset = 450; |
| 234 | fCubicButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30); |
| 235 | buttonOffset += 50; |
| 236 | fConicButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30); |
| 237 | buttonOffset += 50; |
| 238 | fQuadButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30); |
| 239 | buttonOffset += 50; |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 240 | fArcButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30); |
| 241 | buttonOffset += 50; |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 242 | fRRectButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30); |
| 243 | buttonOffset += 50; |
| 244 | fCircleButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30); |
| 245 | buttonOffset += 50; |
| 246 | fTextButton.fBounds.setXYWH(this->width() - 50, SkIntToScalar(buttonOffset), 30, 30); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 247 | this->INHERITED::onSizeChange(); |
| 248 | } |
| 249 | |
| 250 | void copyMinToMax() { |
| 251 | erase(fMaxSurface); |
| 252 | SkCanvas* canvas = fMaxSurface->getCanvas(); |
| 253 | canvas->save(); |
| 254 | canvas->concat(fMatrix); |
Mike Reed | b746b1f | 2021-01-06 08:43:51 -0500 | [diff] [blame] | 255 | fMinSurface->draw(canvas, 0, 0); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 256 | canvas->restore(); |
| 257 | |
| 258 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 259 | paint.setBlendMode(SkBlendMode::kClear); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 260 | for (int iy = 1; iy < fH; ++iy) { |
| 261 | SkScalar y = SkIntToScalar(iy * fZoom); |
| 262 | canvas->drawLine(0, y - SK_ScalarHalf, 999, y - SK_ScalarHalf, paint); |
| 263 | } |
| 264 | for (int ix = 1; ix < fW; ++ix) { |
| 265 | SkScalar x = SkIntToScalar(ix * fZoom); |
| 266 | canvas->drawLine(x - SK_ScalarHalf, 0, x - SK_ScalarHalf, 999, paint); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void setWHZ(int width, int height, int zoom) { |
| 271 | fZoom = zoom; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 272 | fBounds.setIWH(width * zoom, height * zoom); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 273 | fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom)); |
| 274 | fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 275 | fShader = ToolUtils::create_checkerboard_shader(0xFFCCCCCC, 0xFFFFFFFF, zoom); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 276 | |
| 277 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 278 | fMinSurface = SkSurface::MakeRaster(info); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 279 | info = info.makeWH(width * zoom, height * zoom); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 280 | fMaxSurface = SkSurface::MakeRaster(info); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void draw_points(SkCanvas* canvas, const SkPath& path, SkColor color, |
| 284 | bool show_lines) { |
| 285 | SkPaint paint; |
| 286 | paint.setColor(color); |
| 287 | paint.setAlpha(0x80); |
| 288 | paint.setAntiAlias(true); |
| 289 | int n = path.countPoints(); |
| 290 | SkAutoSTArray<32, SkPoint> pts(n); |
| 291 | if (show_lines && fDrawTangents) { |
| 292 | SkTArray<int> contourCounts; |
| 293 | getContourCounts(path, &contourCounts); |
| 294 | SkPoint* ptPtr = pts.get(); |
| 295 | for (int i = 0; i < contourCounts.count(); ++i) { |
| 296 | int count = contourCounts[i]; |
| 297 | path.getPoints(ptPtr, count); |
| 298 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, count, ptPtr, paint); |
| 299 | ptPtr += count; |
| 300 | } |
| 301 | } else { |
| 302 | n = getOnCurvePoints(path, pts.get()); |
| 303 | } |
| 304 | paint.setStrokeWidth(5); |
| 305 | canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts.get(), paint); |
| 306 | } |
| 307 | |
| 308 | void draw_ribs(SkCanvas* canvas, const SkPath& path, SkScalar width, |
| 309 | SkColor color) { |
| 310 | const SkScalar radius = width / 2; |
| 311 | |
| 312 | SkPathMeasure meas(path, false); |
| 313 | SkScalar total = meas.getLength(); |
| 314 | |
| 315 | SkScalar delta = 8; |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 316 | SkPaint paint, labelP; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 317 | paint.setColor(color); |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 318 | labelP.setColor(color & 0xff5f9f5f); |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 319 | SkFont font; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 320 | SkPoint pos, tan; |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 321 | int index = 0; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 322 | for (SkScalar dist = 0; dist <= total; dist += delta) { |
| 323 | if (meas.getPosTan(dist, &pos, &tan)) { |
| 324 | tan.scale(radius); |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 325 | SkPointPriv::RotateCCW(&tan); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 326 | canvas->drawLine(pos.x() + tan.x(), pos.y() + tan.y(), |
| 327 | pos.x() - tan.x(), pos.y() - tan.y(), paint); |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 328 | if (0 == index % 10) { |
| 329 | SkString label; |
| 330 | label.appendS32(index); |
| 331 | SkRect dot = SkRect::MakeXYWH(pos.x() - 2, pos.y() - 2, 4, 4); |
| 332 | canvas->drawRect(dot, labelP); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 333 | canvas->drawString(label, |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 334 | pos.x() - tan.x() * 1.25f, pos.y() - tan.y() * 1.25f, font, labelP); |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | ++index; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | void draw_t_divs(SkCanvas* canvas, const SkPath& path, SkScalar width, SkColor color) { |
| 342 | const SkScalar radius = width / 2; |
| 343 | SkPaint paint; |
| 344 | paint.setColor(color); |
| 345 | SkPathMeasure meas(path, false); |
| 346 | SkScalar total = meas.getLength(); |
| 347 | SkScalar delta = 8; |
| 348 | int ribs = 0; |
| 349 | for (SkScalar dist = 0; dist <= total; dist += delta) { |
| 350 | ++ribs; |
| 351 | } |
Chris Dalton | de50037 | 2020-05-05 15:06:30 -0600 | [diff] [blame] | 352 | const uint8_t* verbs = SkPathPriv::VerbData(path); |
| 353 | if (path.countVerbs() < 2 || SkPath::kMove_Verb != verbs[0]) { |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 354 | SkASSERT(0); |
| 355 | return; |
| 356 | } |
Chris Dalton | de50037 | 2020-05-05 15:06:30 -0600 | [diff] [blame] | 357 | auto verb = static_cast<SkPath::Verb>(verbs[1]); |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 358 | SkASSERT(SkPath::kLine_Verb <= verb && verb <= SkPath::kCubic_Verb); |
Chris Dalton | de50037 | 2020-05-05 15:06:30 -0600 | [diff] [blame] | 359 | const SkPoint* pts = SkPathPriv::PointData(path); |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 360 | SkPoint pos, tan; |
| 361 | for (int index = 0; index < ribs; ++index) { |
| 362 | SkScalar t = (SkScalar) index / ribs; |
| 363 | switch (verb) { |
| 364 | case SkPath::kLine_Verb: |
| 365 | tan = pts[1] - pts[0]; |
| 366 | pos = pts[0]; |
| 367 | pos.fX += tan.fX * t; |
| 368 | pos.fY += tan.fY * t; |
| 369 | break; |
| 370 | case SkPath::kQuad_Verb: |
| 371 | pos = SkEvalQuadAt(pts, t); |
| 372 | tan = SkEvalQuadTangentAt(pts, t); |
| 373 | break; |
| 374 | case SkPath::kConic_Verb: { |
Chris Dalton | de50037 | 2020-05-05 15:06:30 -0600 | [diff] [blame] | 375 | SkConic conic(pts, SkPathPriv::ConicWeightData(path)[0]); |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 376 | pos = conic.evalAt(t); |
| 377 | tan = conic.evalTangentAt(t); |
| 378 | } break; |
| 379 | case SkPath::kCubic_Verb: |
| 380 | SkEvalCubicAt(pts, t, &pos, &tan, nullptr); |
| 381 | break; |
| 382 | default: |
| 383 | SkASSERT(0); |
| 384 | return; |
| 385 | } |
| 386 | tan.setLength(radius); |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 387 | SkPointPriv::RotateCCW(&tan); |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 388 | canvas->drawLine(pos.x() + tan.x(), pos.y() + tan.y(), |
| 389 | pos.x() - tan.x(), pos.y() - tan.y(), paint); |
| 390 | if (0 == index % 10) { |
| 391 | SkString label; |
| 392 | label.appendS32(index); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 393 | canvas->drawString(label, |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 394 | pos.x() + tan.x() * 1.25f, pos.y() + tan.y() * 1.25f, SkFont(), paint); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 399 | void draw_stroke(SkCanvas* canvas, const SkPath& path, SkScalar width, SkScalar scale, |
| 400 | bool drawText) { |
caryclark | 612f70d | 2015-05-19 11:05:37 -0700 | [diff] [blame] | 401 | if (path.isEmpty()) { |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 402 | return; |
| 403 | } |
caryclark | 612f70d | 2015-05-19 11:05:37 -0700 | [diff] [blame] | 404 | SkRect bounds = path.getBounds(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 405 | this->setWHZ(SkScalarCeilToInt(bounds.right()), drawText |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 406 | ? SkScalarRoundToInt(scale * 3 / 2) : SkScalarRoundToInt(scale), |
| 407 | SkScalarRoundToInt(950.0f / scale)); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 408 | erase(fMinSurface); |
| 409 | SkPaint paint; |
| 410 | paint.setColor(0x1f1f0f0f); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 411 | paint.setStyle(SkPaint::kStroke_Style); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 412 | paint.setStrokeWidth(width * scale * scale); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 413 | paint.setColor(0x3f0f1f3f); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 414 | if (drawText) { |
| 415 | fMinSurface->getCanvas()->drawPath(path, paint); |
| 416 | this->copyMinToMax(); |
Mike Reed | b746b1f | 2021-01-06 08:43:51 -0500 | [diff] [blame] | 417 | fMaxSurface->draw(canvas, 0, 0); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 418 | } |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 419 | paint.setAntiAlias(true); |
| 420 | paint.setStyle(SkPaint::kStroke_Style); |
| 421 | paint.setStrokeWidth(1); |
| 422 | |
| 423 | paint.setColor(SKELETON_COLOR); |
| 424 | SkPath scaled; |
| 425 | SkMatrix matrix; |
| 426 | matrix.reset(); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 427 | matrix.setScale(950 / scale, 950 / scale); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 428 | if (drawText) { |
| 429 | path.transform(matrix, &scaled); |
| 430 | } else { |
| 431 | scaled = path; |
| 432 | } |
| 433 | canvas->drawPath(scaled, paint); |
| 434 | draw_points(canvas, scaled, SKELETON_COLOR, true); |
| 435 | |
| 436 | if (fDrawRibs) { |
| 437 | draw_ribs(canvas, scaled, width, 0xFF00FF00); |
| 438 | } |
| 439 | |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 440 | if (fDrawTDivs) { |
| 441 | draw_t_divs(canvas, scaled, width, 0xFF3F3F00); |
| 442 | } |
| 443 | |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 444 | SkPath fill; |
| 445 | |
| 446 | SkPaint p; |
| 447 | p.setStyle(SkPaint::kStroke_Style); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 448 | if (drawText) { |
| 449 | p.setStrokeWidth(width * scale * scale); |
| 450 | } else { |
| 451 | p.setStrokeWidth(width); |
| 452 | } |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 453 | p.getFillPath(path, &fill); |
| 454 | SkPath scaledFill; |
| 455 | if (drawText) { |
| 456 | fill.transform(matrix, &scaledFill); |
| 457 | } else { |
| 458 | scaledFill = fill; |
| 459 | } |
| 460 | paint.setColor(WIREFRAME_COLOR); |
| 461 | canvas->drawPath(scaledFill, paint); |
| 462 | draw_points(canvas, scaledFill, WIREFRAME_COLOR, false); |
| 463 | } |
| 464 | |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 465 | void draw_fill(SkCanvas* canvas, const SkRect& rect, SkScalar width) { |
| 466 | if (rect.isEmpty()) { |
| 467 | return; |
| 468 | } |
| 469 | SkPaint paint; |
| 470 | paint.setColor(0x1f1f0f0f); |
| 471 | paint.setStyle(SkPaint::kStroke_Style); |
| 472 | paint.setStrokeWidth(width); |
| 473 | SkPath path; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 474 | SkScalar maxSide = std::max(rect.width(), rect.height()) / 2; |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 475 | SkPoint center = { rect.fLeft + maxSide, rect.fTop + maxSide }; |
| 476 | path.addCircle(center.fX, center.fY, maxSide); |
| 477 | canvas->drawPath(path, paint); |
| 478 | paint.setStyle(SkPaint::kFill_Style); |
| 479 | path.reset(); |
| 480 | path.addCircle(center.fX, center.fY, maxSide - width / 2); |
| 481 | paint.setColor(0x3f0f1f3f); |
| 482 | canvas->drawPath(path, paint); |
| 483 | path.reset(); |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 484 | path.setFillType(SkPathFillType::kEvenOdd); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 485 | path.addCircle(center.fX, center.fY, maxSide + width / 2); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 486 | SkRect outside = SkRect::MakeXYWH(center.fX - maxSide - width, center.fY - maxSide - width, |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 487 | (maxSide + width) * 2, (maxSide + width) * 2); |
| 488 | path.addRect(outside); |
| 489 | canvas->drawPath(path, paint); |
| 490 | } |
| 491 | |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 492 | void draw_button(SkCanvas* canvas, const StrokeTypeButton& button) { |
| 493 | SkPaint paint; |
| 494 | paint.setAntiAlias(true); |
| 495 | paint.setStyle(SkPaint::kStroke_Style); |
| 496 | paint.setColor(button.fEnabled ? 0xFF3F0000 : 0x6F3F0000); |
| 497 | canvas->drawRect(button.fBounds, paint); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 498 | paint.setColor(button.fEnabled ? 0xFF3F0000 : 0x6F3F0000); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 499 | paint.setStyle(SkPaint::kFill_Style); |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 500 | SkFont font; |
| 501 | font.setSize(25.0f); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 502 | SkTextUtils::Draw(canvas, &button.fLabel, 1, SkTextEncoding::kUTF8, |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 503 | button.fBounds.centerX(), button.fBounds.fBottom - 5, |
| 504 | font, paint, SkTextUtils::kCenter_Align); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | void draw_control(SkCanvas* canvas, const SkRect& bounds, SkScalar value, |
| 508 | SkScalar min, SkScalar max, const char* name) { |
| 509 | SkPaint paint; |
| 510 | paint.setAntiAlias(true); |
| 511 | paint.setStyle(SkPaint::kStroke_Style); |
| 512 | canvas->drawRect(bounds, paint); |
| 513 | SkScalar scale = max - min; |
| 514 | SkScalar yPos = bounds.fTop + (value - min) * bounds.height() / scale; |
| 515 | paint.setColor(0xFFFF0000); |
| 516 | canvas->drawLine(bounds.fLeft - 5, yPos, bounds.fRight + 5, yPos, paint); |
| 517 | SkString label; |
| 518 | label.printf("%0.3g", value); |
| 519 | paint.setColor(0xFF000000); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 520 | paint.setStyle(SkPaint::kFill_Style); |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 521 | SkFont font(nullptr, 11.0f); |
| 522 | canvas->drawString(label, bounds.fLeft + 5, yPos - 5, font, paint); |
| 523 | font.setSize(13.0f); |
| 524 | canvas->drawString(name, bounds.fLeft, bounds.bottom() + 11, font, paint); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | void setForGeometry() { |
| 528 | fDrawRibs = true; |
| 529 | fDrawTangents = true; |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 530 | fDrawTDivs = false; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 531 | fWidthScale = 1; |
| 532 | } |
| 533 | |
| 534 | void setForText() { |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 535 | fDrawRibs = fDrawTangents = fDrawTDivs = false; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 536 | fWidthScale = 0.002f; |
| 537 | } |
| 538 | |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 539 | void setForSingles() { |
| 540 | setForGeometry(); |
| 541 | fDrawTDivs = true; |
| 542 | } |
| 543 | |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 544 | void setAsNeeded() { |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 545 | if (fConicButton.fEnabled || fCubicButton.fEnabled || fQuadButton.fEnabled) { |
| 546 | setForSingles(); |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 547 | } else if (fRRectButton.fEnabled || fCircleButton.fEnabled || fArcButton.fEnabled) { |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 548 | setForGeometry(); |
| 549 | } else { |
| 550 | setForText(); |
| 551 | } |
| 552 | } |
| 553 | |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 554 | bool arcCenter(SkPoint* center) { |
| 555 | SkPath path; |
| 556 | path.moveTo(fPts[10]); |
| 557 | path.arcTo(fPts[11], fPts[12], fRadius); |
| 558 | SkPath::Iter iter(path, false); |
| 559 | SkPoint pts[4]; |
| 560 | iter.next(pts); |
| 561 | if (SkPath::kLine_Verb == iter.next(pts)) { |
| 562 | iter.next(pts); |
| 563 | } |
| 564 | SkVector before = pts[0] - pts[1]; |
| 565 | SkVector after = pts[1] - pts[2]; |
| 566 | before.setLength(fRadius); |
| 567 | after.setLength(fRadius); |
| 568 | SkVector beforeCCW, afterCCW; |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 569 | SkPointPriv::RotateCCW(before, &beforeCCW); |
| 570 | SkPointPriv::RotateCCW(after, &afterCCW); |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 571 | beforeCCW += pts[0]; |
| 572 | afterCCW += pts[2]; |
| 573 | *center = beforeCCW; |
| 574 | if (SkScalarNearlyEqual(beforeCCW.fX, afterCCW.fX) |
| 575 | && SkScalarNearlyEqual(beforeCCW.fY, afterCCW.fY)) { |
| 576 | return true; |
| 577 | } |
| 578 | SkVector beforeCW, afterCW; |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 579 | SkPointPriv::RotateCW(before, &beforeCW); |
| 580 | SkPointPriv::RotateCW(after, &afterCW); |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 581 | beforeCW += pts[0]; |
| 582 | afterCW += pts[2]; |
| 583 | *center = beforeCW; |
| 584 | return SkScalarNearlyEqual(beforeCW.fX, afterCW.fX) |
| 585 | && SkScalarNearlyEqual(beforeCCW.fY, afterCW.fY); |
| 586 | } |
| 587 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 588 | void onDrawContent(SkCanvas* canvas) override { |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 589 | SkPath path; |
| 590 | SkScalar width = fWidth; |
| 591 | |
| 592 | if (fCubicButton.fEnabled) { |
| 593 | path.moveTo(fPts[0]); |
| 594 | path.cubicTo(fPts[1], fPts[2], fPts[3]); |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 595 | setForSingles(); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 596 | draw_stroke(canvas, path, width, 950, false); |
| 597 | } |
| 598 | |
| 599 | if (fConicButton.fEnabled) { |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 600 | path.reset(); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 601 | path.moveTo(fPts[4]); |
| 602 | path.conicTo(fPts[5], fPts[6], fWeight); |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 603 | setForSingles(); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 604 | draw_stroke(canvas, path, width, 950, false); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | if (fQuadButton.fEnabled) { |
| 608 | path.reset(); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 609 | path.moveTo(fPts[7]); |
| 610 | path.quadTo(fPts[8], fPts[9]); |
caryclark | b6474dd | 2016-01-19 08:07:49 -0800 | [diff] [blame] | 611 | setForSingles(); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 612 | draw_stroke(canvas, path, width, 950, false); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 613 | } |
| 614 | |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 615 | if (fArcButton.fEnabled) { |
| 616 | path.reset(); |
| 617 | path.moveTo(fPts[10]); |
| 618 | path.arcTo(fPts[11], fPts[12], fRadius); |
| 619 | setForGeometry(); |
| 620 | draw_stroke(canvas, path, width, 950, false); |
| 621 | SkPath pathPts; |
| 622 | pathPts.moveTo(fPts[10]); |
| 623 | pathPts.lineTo(fPts[11]); |
| 624 | pathPts.lineTo(fPts[12]); |
| 625 | draw_points(canvas, pathPts, SK_ColorDKGRAY, true); |
| 626 | } |
| 627 | |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 628 | if (fRRectButton.fEnabled) { |
| 629 | SkScalar rad = 32; |
| 630 | SkRect r; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 631 | r.setBounds(&fPts[13], 2); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 632 | path.reset(); |
| 633 | SkRRect rr; |
| 634 | rr.setRectXY(r, rad, rad); |
| 635 | path.addRRect(rr); |
| 636 | setForGeometry(); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 637 | draw_stroke(canvas, path, width, 950, false); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 638 | |
| 639 | path.reset(); |
| 640 | SkRRect rr2; |
| 641 | rr.inset(width/2, width/2, &rr2); |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 642 | path.addRRect(rr2, SkPathDirection::kCCW); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 643 | rr.inset(-width/2, -width/2, &rr2); |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 644 | path.addRRect(rr2, SkPathDirection::kCW); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 645 | SkPaint paint; |
| 646 | paint.setAntiAlias(true); |
| 647 | paint.setColor(0x40FF8844); |
| 648 | canvas->drawPath(path, paint); |
| 649 | } |
| 650 | |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 651 | if (fCircleButton.fEnabled) { |
| 652 | path.reset(); |
| 653 | SkRect r; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 654 | r.setBounds(&fPts[15], 2); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 655 | path.addOval(r); |
| 656 | setForGeometry(); |
| 657 | if (fCircleButton.fFill) { |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 658 | if (fArcButton.fEnabled) { |
| 659 | SkPoint center; |
| 660 | if (arcCenter(¢er)) { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 661 | r.setLTRB(center.fX - fRadius, center.fY - fRadius, |
| 662 | center.fX + fRadius, center.fY + fRadius); |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 663 | } |
| 664 | } |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 665 | draw_fill(canvas, r, width); |
| 666 | } else { |
| 667 | draw_stroke(canvas, path, width, 950, false); |
| 668 | } |
| 669 | } |
| 670 | |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 671 | if (fTextButton.fEnabled) { |
| 672 | path.reset(); |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 673 | SkFont font; |
| 674 | font.setSize(fTextSize); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 675 | SkTextUtils::GetPath(fText.c_str(), fText.size(), SkTextEncoding::kUTF8, |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 676 | 0, fTextSize, font, &path); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 677 | setForText(); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 678 | draw_stroke(canvas, path, width * fWidthScale / fTextSize, fTextSize, true); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | if (fAnimate) { |
| 682 | fWidth += fDWidth; |
| 683 | if (fDWidth > 0 && fWidth > kWidthMax) { |
| 684 | fDWidth = -fDWidth; |
| 685 | } else if (fDWidth < 0 && fWidth < kWidthMin) { |
| 686 | fDWidth = -fDWidth; |
| 687 | } |
| 688 | } |
| 689 | setAsNeeded(); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 690 | if (fConicButton.fEnabled) { |
| 691 | draw_control(canvas, fWeightControl, fWeight, 0, 5, "weight"); |
| 692 | } |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 693 | if (fArcButton.fEnabled) { |
| 694 | draw_control(canvas, fRadiusControl, fRadius, 0, 500, "radius"); |
| 695 | } |
caryclark | a76b7a3b | 2015-05-22 06:26:52 -0700 | [diff] [blame] | 696 | #ifdef SK_DEBUG |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 697 | draw_control(canvas, fErrorControl, gDebugStrokerError, kStrokerErrorMin, kStrokerErrorMax, |
| 698 | "error"); |
| 699 | #endif |
| 700 | draw_control(canvas, fWidthControl, fWidth * fWidthScale, kWidthMin * fWidthScale, |
| 701 | kWidthMax * fWidthScale, "width"); |
| 702 | draw_button(canvas, fQuadButton); |
| 703 | draw_button(canvas, fCubicButton); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 704 | draw_button(canvas, fConicButton); |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 705 | draw_button(canvas, fArcButton); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 706 | draw_button(canvas, fRRectButton); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 707 | draw_button(canvas, fCircleButton); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 708 | draw_button(canvas, fTextButton); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | class MyClick : public Click { |
| 712 | public: |
| 713 | int fIndex; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 714 | MyClick(int index) : fIndex(index) {} |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 715 | }; |
| 716 | |
John Stiles | 1cf2c8d | 2020-08-13 22:58:04 -0400 | [diff] [blame] | 717 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override { |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 718 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); ++i) { |
| 719 | if (hittest(fPts[i], x, y)) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 720 | return new MyClick((int)i); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | const SkRect& rectPt = SkRect::MakeXYWH(x, y, 1, 1); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 724 | if (fWeightControl.contains(rectPt)) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 725 | return new MyClick((int) SK_ARRAY_COUNT(fPts) + 1); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 726 | } |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 727 | if (fRadiusControl.contains(rectPt)) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 728 | return new MyClick((int) SK_ARRAY_COUNT(fPts) + 2); |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 729 | } |
caryclark | a76b7a3b | 2015-05-22 06:26:52 -0700 | [diff] [blame] | 730 | #ifdef SK_DEBUG |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 731 | if (fErrorControl.contains(rectPt)) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 732 | return new MyClick((int) SK_ARRAY_COUNT(fPts) + 3); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 733 | } |
| 734 | #endif |
| 735 | if (fWidthControl.contains(rectPt)) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 736 | return new MyClick((int) SK_ARRAY_COUNT(fPts) + 4); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 737 | } |
| 738 | if (fCubicButton.fBounds.contains(rectPt)) { |
| 739 | fCubicButton.fEnabled ^= true; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 740 | return new MyClick((int) SK_ARRAY_COUNT(fPts) + 5); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 741 | } |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 742 | if (fConicButton.fBounds.contains(rectPt)) { |
| 743 | fConicButton.fEnabled ^= true; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 744 | return new MyClick((int) SK_ARRAY_COUNT(fPts) + 6); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 745 | } |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 746 | if (fQuadButton.fBounds.contains(rectPt)) { |
| 747 | fQuadButton.fEnabled ^= true; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 748 | return new MyClick((int) SK_ARRAY_COUNT(fPts) + 7); |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 749 | } |
| 750 | if (fArcButton.fBounds.contains(rectPt)) { |
| 751 | fArcButton.fEnabled ^= true; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 752 | return new MyClick((int) SK_ARRAY_COUNT(fPts) + 8); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 753 | } |
| 754 | if (fRRectButton.fBounds.contains(rectPt)) { |
| 755 | fRRectButton.fEnabled ^= true; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 756 | return new MyClick((int) SK_ARRAY_COUNT(fPts) + 9); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 757 | } |
| 758 | if (fCircleButton.fBounds.contains(rectPt)) { |
| 759 | bool wasEnabled = fCircleButton.fEnabled; |
| 760 | fCircleButton.fEnabled = !fCircleButton.fFill; |
| 761 | fCircleButton.fFill = wasEnabled && !fCircleButton.fFill; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 762 | return new MyClick((int) SK_ARRAY_COUNT(fPts) + 10); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 763 | } |
| 764 | if (fTextButton.fBounds.contains(rectPt)) { |
| 765 | fTextButton.fEnabled ^= true; |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 766 | return new MyClick((int) SK_ARRAY_COUNT(fPts) + 11); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 767 | } |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 768 | return nullptr; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 771 | static SkScalar MapScreenYtoValue(SkScalar y, const SkRect& control, SkScalar min, |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 772 | SkScalar max) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 773 | return (y - control.fTop) / control.height() * (max - min) + min; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 774 | } |
| 775 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 776 | bool onClick(Click* click) override { |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 777 | int index = ((MyClick*)click)->fIndex; |
| 778 | if (index < (int) SK_ARRAY_COUNT(fPts)) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 779 | fPts[index].offset(click->fCurr.fX - click->fPrev.fX, |
| 780 | click->fCurr.fY - click->fPrev.fY); |
caryclark | 04e4d08 | 2015-02-20 06:33:57 -0800 | [diff] [blame] | 781 | } else if (index == (int) SK_ARRAY_COUNT(fPts) + 1) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 782 | fWeight = MapScreenYtoValue(click->fCurr.fY, fWeightControl, 0, 5); |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 783 | } else if (index == (int) SK_ARRAY_COUNT(fPts) + 2) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 784 | fRadius = MapScreenYtoValue(click->fCurr.fY, fRadiusControl, 0, 500); |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 785 | } |
caryclark | a76b7a3b | 2015-05-22 06:26:52 -0700 | [diff] [blame] | 786 | #ifdef SK_DEBUG |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 787 | else if (index == (int) SK_ARRAY_COUNT(fPts) + 3) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 788 | gDebugStrokerError = std::max(FLT_EPSILON, MapScreenYtoValue(click->fCurr.fY, |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 789 | fErrorControl, kStrokerErrorMin, kStrokerErrorMax)); |
| 790 | gDebugStrokerErrorSet = true; |
| 791 | } |
| 792 | #endif |
caryclark | 88651ae | 2016-01-20 11:55:11 -0800 | [diff] [blame] | 793 | else if (index == (int) SK_ARRAY_COUNT(fPts) + 4) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 794 | fWidth = std::max(FLT_EPSILON, MapScreenYtoValue(click->fCurr.fY, fWidthControl, |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 795 | kWidthMin, kWidthMax)); |
| 796 | fAnimate = fWidth <= kWidthMin; |
| 797 | } |
| 798 | return true; |
| 799 | } |
| 800 | |
| 801 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 802 | using INHERITED = Sample; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 803 | }; |
| 804 | |
| 805 | /////////////////////////////////////////////////////////////////////////////// |
| 806 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 807 | DEF_SAMPLE( return new QuadStrokerView(); ) |