| Flutter Impeller |

widgets.h
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 #ifndef FLUTTER_IMPELLER_PLAYGROUND_WIDGETS_H_
6 #define FLUTTER_IMPELLER_PLAYGROUND_WIDGETS_H_
7
8 #include <tuple>
9
10 #include "impeller/base/strings.h"
11 #include "impeller/geometry/color.h"
12 #include "impeller/geometry/point.h"
13 #include "third_party/imgui/imgui.h"
14
15 #define IMPELLER_PLAYGROUND_POINT(default_position, radius, color) \
16 ({ \
17 static impeller::Point position = default_position; \
18 static impeller::Point reset_position = default_position; \
19 float radius_ = radius; \
20 impeller::Color color_ = color; \
21 \
22 static bool dragging = false; \
23 impeller::Point mouse_pos(ImGui::GetMousePos().x, ImGui::GetMousePos().y); \
24 static impeller::Point prev_mouse_pos = mouse_pos; \
25 \
26 if (ImGui::IsKeyPressed(ImGuiKey_R)) { \
27 position = reset_position; \
28 dragging = false; \
29 } \
30 \
31 bool hovering = position.GetDistance(mouse_pos) < radius_ && \
32 position.GetDistance(prev_mouse_pos) < radius_; \
33 if (!ImGui::IsMouseDown(0)) { \
34 dragging = false; \
35 } else if (hovering && ImGui::IsMouseClicked(0)) { \
36 dragging = true; \
37 } \
38 if (dragging) { \
39 position += mouse_pos - prev_mouse_pos; \
40 } \
41 ImGui::GetBackgroundDrawList()->AddCircleFilled( \
42 {position.x, position.y}, radius_, \
43 ImColor(color_.red, color_.green, color_.blue, \
44 (hovering || dragging) ? 0.6f : 0.3f)); \
45 if (hovering || dragging) { \
46 ImGui::GetBackgroundDrawList()->AddText( \
47 {position.x - radius_, position.y + radius_ + 10}, \
48 ImColor(color_.red, color.green, color.blue, 1.0f), \
49 impeller::SPrintF("x:%0.3f y:%0.3f", position.x, position.y) \
50 .c_str()); \
51 } \
52 prev_mouse_pos = mouse_pos; \
53 position; \
54 })
55
56 #define IMPELLER_PLAYGROUND_LINE(default_position_a, default_position_b, \
57 radius, color_a, color_b) \
58 ({ \
59 impeller::Point position_a = default_position_a; \
60 impeller::Point position_b = default_position_b; \
61 float r_ = radius; \
62 impeller::Color color_a_ = color_a; \
63 impeller::Color color_b_ = color_b; \
64 \
65 position_a = IMPELLER_PLAYGROUND_POINT(position_a, r_, color_a_); \
66 position_b = IMPELLER_PLAYGROUND_POINT(position_b, r_, color_b_); \
67 \
68 auto dir = (position_b - position_a).Normalize() * r_; \
69 auto line_a = position_a + dir; \
70 auto line_b = position_b - dir; \
71 ImGui::GetBackgroundDrawList()->AddLine( \
72 {line_a.x, line_a.y}, {line_b.x, line_b.y}, \
73 ImColor(color_b.red, color_b.green, color_b.blue, 0.3f)); \
74 \
75 std::make_tuple(position_a, position_b); \
76 })
77
78 #endif // FLUTTER_IMPELLER_PLAYGROUND_WIDGETS_H_
point.h
strings.h
color.h