Flutter Engine
 
Loading...
Searching...
No Matches
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
6
7#include <format>
8
9namespace impeller {
10
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
51std::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
int32_t x
double y
Point DrawPlaygroundPoint(PlaygroundPoint &point)
Definition widgets.cc:11
std::tuple< Point, Point > DrawPlaygroundLine(PlaygroundPoint &point_a, PlaygroundPoint &point_b)
Definition widgets.cc:51
Scalar blue
Definition color.h:138
Scalar red
Definition color.h:128
Scalar green
Definition color.h:133
std::optional< Point > prev_mouse_pos
Definition widgets.h:30
constexpr Type GetDistance(const TPoint &p) const
Definition point.h:200