Skip to content

Commit 4be3053

Browse files
bderodnfield
authored andcommitted
Add immediate mode manipulator widget macros for the playground (#28)
1 parent f41c0a9 commit 4be3053

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

impeller/playground/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ impeller_component("playground") {
1111
sources = [
1212
"playground.h",
1313
"playground.mm",
14+
"widgets.cc",
15+
"widgets.h",
1416
]
1517

1618
public_deps = [

impeller/playground/widgets.cc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 "widgets.h"
6+
7+
namespace impeller {
8+
9+
//
10+
11+
} // namespace impeller

impeller/playground/widgets.h

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

0 commit comments

Comments
 (0)