| Flutter Impeller |

widgets.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/playground/widgets.h"
6
7 #include <format>
8
9 namespace impeller {
10
11 Point DrawPlaygroundPoint(PlaygroundPoint& point) {
12 if (ImGui::GetCurrentContext()) {
13 impeller::Point mouse_pos(ImGui::GetMousePos().x, ImGui::GetMousePos().y);
14 if (!point.prev_mouse_pos.has_value()) {
15 point.prev_mouse_pos = mouse_pos;
16 }
17
18 if (ImGui::IsKeyPressed(ImGuiKey_R)) {
19 point.position = point.reset_position;
20 point.dragging = false;
21 }
22
23 bool hovering =
24 point.position.GetDistance(mouse_pos) < point.radius &&
25 point.position.GetDistance(point.prev_mouse_pos.value()) < point.radius;
26 if (!ImGui::IsMouseDown(0)) {
27 point.dragging = false;
28 } else if (hovering && ImGui::IsMouseClicked(0)) {
29 point.dragging = true;
30 }
31 if (point.dragging) {
32 point.position += mouse_pos - point.prev_mouse_pos.value();
33 }
34 ImGui::GetBackgroundDrawList()->AddCircleFilled(
35 {point.position.x, point.position.y}, point.radius,
36 ImColor(point.color.red, point.color.green, point.color.blue,
37 (hovering || point.dragging) ? 0.6f : 0.3f));
38 if (hovering || point.dragging) {
39 ImGui::GetBackgroundDrawList()->AddText(
40 {point.position.x - point.radius,
41 point.position.y + point.radius + 10},
42 ImColor(point.color.red, point.color.green, point.color.blue, 1.0f),
43 std::format("x:{:.3f} y:{:.3f}", point.position.x, point.position.y)
44 .c_str());
45 }
46 point.prev_mouse_pos = mouse_pos;
47 }
48 return point.position;
49 }
50
51 std::tuple<Point, Point> DrawPlaygroundLine(PlaygroundPoint& point_a,
52 PlaygroundPoint& point_b) {
53 Point position_a = DrawPlaygroundPoint(point_a);
54 Point position_b = DrawPlaygroundPoint(point_b);
55
56 if (ImGui::GetCurrentContext()) {
57 auto dir = (position_b - position_a).Normalize() * point_a.radius;
58 auto line_a = position_a + dir;
59 auto line_b = position_b - dir;
60 ImGui::GetBackgroundDrawList()->AddLine(
61 {line_a.x, line_a.y}, {line_b.x, line_b.y},
62 ImColor(point_b.color.red, point_b.color.green, point_b.color.blue,
63 0.3f));
64 }
65 return std::make_tuple(position_a, position_b);
66 }
67 //
68
69 } // namespace impeller
x
int32_t x
Definition: dl_golden_unittests.cc:458
impeller
Definition: allocation.cc:12
impeller::DrawPlaygroundPoint
Point DrawPlaygroundPoint(PlaygroundPoint &point)
Definition: widgets.cc:11
impeller::DrawPlaygroundLine
std::tuple< Point, Point > DrawPlaygroundLine(PlaygroundPoint &point_a, PlaygroundPoint &point_b)
Definition: widgets.cc:51
impeller::Color::blue
Scalar blue
Definition: color.h:138
impeller::Color::red
Scalar red
Definition: color.h:128
impeller::Color::green
Scalar green
Definition: color.h:133
impeller::PlaygroundPoint
Definition: widgets.h:21
impeller::PlaygroundPoint::color
Color color
Definition: widgets.h:32
impeller::PlaygroundPoint::reset_position
Point reset_position
Definition: widgets.h:28
impeller::PlaygroundPoint::position
Point position
Definition: widgets.h:27
impeller::PlaygroundPoint::radius
Scalar radius
Definition: widgets.h:31
impeller::PlaygroundPoint::prev_mouse_pos
std::optional< Point > prev_mouse_pos
Definition: widgets.h:30
impeller::PlaygroundPoint::dragging
bool dragging
Definition: widgets.h:29
impeller::TPoint< Scalar >
impeller::TPoint::GetDistance
constexpr Type GetDistance(const TPoint &p) const
Definition: point.h:201
impeller::TPoint::y
Type y
Definition: point.h:32
impeller::TPoint::x
Type x
Definition: point.h:31
widgets.h