Debian Codegraph @

tools/viewer/ZoomInSlide.cpp

raw
1 /*
2 * Copyright 2025 Google LLC
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 "tools/viewer/ZoomInSlide.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPathBuilder.h"
15 #include "include/core/SkPoint.h"
16
17 // An example implementation of a zoomed-in slide.
18 // Reproduces issues.skia.org/issues/451536363
19 class ZoomInSlideDemo : public ZoomInSlide {
20 public:
21 ZoomInSlideDemo() : ZoomInSlide(kScale, kMaskWidth, kMaskHeight, SkString("ZoomInDemo")) {
22 fPts[0] = {1.5, 1.5};
23 fPts[1] = {1.5, 10.5};
24 fPts[2] = {10.5, 10.5};
25 fPts[3] = {10.5, 1.5};
26 }
27
28 SkPath makePath() {
29 return SkPathBuilder()
30 .moveTo(fPts[0])
31 .lineTo(fPts[1])
32 .lineTo(fPts[2])
33 .lineTo(fPts[3])
34 .close()
35 .detach();
36 }
37
38 void drawUnderGrid(SkCanvas* canvas) override {
39 SkPaint paint;
40 paint.setStyle(SkPaint::kStroke_Style);
41 paint.setStrokeCap(SkPaint::kSquare_Cap);
42 paint.setStrokeJoin(SkPaint::kMiter_Join);
43 paint.setAntiAlias(true);
44
45 this->drawScaledPath(canvas, makePath(), paint);
46 }
47
48 void drawOverGrid(SkCanvas* canvas) override {
49 SkPaint truthPaint;
50 truthPaint.setStyle(SkPaint::Style::kStroke_Style);
51 truthPaint.setColor(SK_ColorRED);
52 truthPaint.setAntiAlias(true);
53 truthPaint.setStrokeWidth(2.f / fScale);
54
55 canvas->drawPath(makePath(), truthPaint);
56
57 SkPaint handlePaint;
58 handlePaint.setAntiAlias(true);
59 handlePaint.setStyle(SkPaint::Style::kFill_Style);
60 handlePaint.setColor(SkColorSetARGB(255, 255, 70, 10));
61 for (auto pt : fPts) {
62 canvas->drawCircle(pt, kHandleRadius / kScale, handlePaint);
63 }
64 }
65
66 void handleClick(const ScaledClick* click) override {
67 SkPoint pt = click->currScaled();
68 int closestPointIdx = 0;
69 float minDist = SkPoint::Distance(pt, fPts[0]);
70
71 for (int i = 1; i < kNumPoints; i++) {
72 float dist = SkPoint::Distance(pt, fPts[i]);
73 if (dist < minDist) {
74 minDist = dist;
75 closestPointIdx = i;
76 }
77 }
78 fPts[closestPointIdx] = pt;
79 }
80
81 private:
82 static constexpr size_t kScale = 32;
83 // Defines the region of the screen that will be zoomed in on.
84 // The path under scrutiny will be drawn in this region.
85 static constexpr int kMaskWidth = 20;
86 static constexpr int kMaskHeight = 30;
87
88 static constexpr float kHandleRadius = 4.f;
89 static constexpr int kNumPoints = 4;
90
91 SkPoint fPts[kNumPoints];
92 };
93
94 DEF_SLIDE(return new ZoomInSlideDemo();)