| 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 namespace impeller {
8
9 Point DrawPlaygroundPoint(PlaygroundPoint& point) {
10 if (ImGui::GetCurrentContext()) {
11 impeller::Point mouse_pos(ImGui::GetMousePos().x, ImGui::GetMousePos().y);
12 if (!point.prev_mouse_pos.has_value()) {
13 point.prev_mouse_pos = mouse_pos;
14 }
15
16 if (ImGui::IsKeyPressed(ImGuiKey_R)) {
17 point.position = point.reset_position;
18 point.dragging = false;
19 }
20
21 bool hovering =
22 point.position.GetDistance(mouse_pos) < point.radius &&
23 point.position.GetDistance(point.prev_mouse_pos.value()) < point.radius;
24 if (!ImGui::IsMouseDown(0)) {
25 point.dragging = false;
26 } else if (hovering && ImGui::IsMouseClicked(0)) {
27 point.dragging = true;
28 }
29 if (point.dragging) {
30 point.position += mouse_pos - point.prev_mouse_pos.value();
31 }
32 ImGui::GetBackgroundDrawList()->AddCircleFilled(
33 {point.position.x, point.position.y}, point.radius,
34 ImColor(point.color.red, point.color.green, point.color.blue,
35 (hovering || point.dragging) ? 0.6f : 0.3f));
36 if (hovering || point.dragging) {
37 ImGui::GetBackgroundDrawList()->AddText(
38 {point.position.x - point.radius,
39 point.position.y + point.radius + 10},
40 ImColor(point.color.red, point.color.green, point.color.blue, 1.0f),
41 impeller::SPrintF("x:%0.3f y:%0.3f", point.position.x,
42 point.position.y)
43 .c_str());
44 }
45 point.prev_mouse_pos = mouse_pos;
46 }
47 return point.position;
48 }
49
50 std::tuple<Point, Point> DrawPlaygroundLine(PlaygroundPoint& point_a,
51 PlaygroundPoint& point_b) {
52 Point position_a = DrawPlaygroundPoint(point_a);
53 Point position_b = DrawPlaygroundPoint(point_b);
54
55 if (ImGui::GetCurrentContext()) {
56 auto dir = (position_b - position_a).Normalize() * point_a.radius;
57 auto line_a = position_a + dir;
58 auto line_b = position_b - dir;
59 ImGui::GetBackgroundDrawList()->AddLine(
60 {line_a.x, line_a.y}, {line_b.x, line_b.y},
61 ImColor(point_b.color.red, point_b.color.green, point_b.color.blue,
62 0.3f));
63 }
64 return std::make_tuple(position_a, position_b);
65 }
66 //
67
68 } // 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:9
impeller::DrawPlaygroundLine
std::tuple< Point, Point > DrawPlaygroundLine(PlaygroundPoint &point_a, PlaygroundPoint &point_b)
Definition: widgets.cc:50
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
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:17
impeller::PlaygroundPoint::color
Color color
Definition: widgets.h:28
impeller::PlaygroundPoint::reset_position
Point reset_position
Definition: widgets.h:24
impeller::PlaygroundPoint::position
Point position
Definition: widgets.h:23
impeller::PlaygroundPoint::radius
Scalar radius
Definition: widgets.h:27
impeller::PlaygroundPoint::prev_mouse_pos
std::optional< Point > prev_mouse_pos
Definition: widgets.h:26
impeller::PlaygroundPoint::dragging
bool dragging
Definition: widgets.h:25
impeller::TPoint< Scalar >
impeller::TPoint::GetDistance
constexpr Type GetDistance(const TPoint &p) const
Definition: point.h:200
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::TPoint::x
Type x
Definition: point.h:30
widgets.h