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 
6 
7 namespace impeller {
8 
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
int32_t x
Point DrawPlaygroundPoint(PlaygroundPoint &point)
Definition: widgets.cc:9
std::tuple< Point, Point > DrawPlaygroundLine(PlaygroundPoint &point_a, PlaygroundPoint &point_b)
Definition: widgets.cc:50
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
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