77namespace impeller {
88
99Point DrawPlaygroundPoint (PlaygroundPoint& point) {
10- impeller::Point mouse_pos (ImGui::GetMousePos ().x , ImGui::GetMousePos ().y );
11- if (!point.prev_mouse_pos .has_value ()) {
12- point.prev_mouse_pos = mouse_pos;
13- }
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+ }
1415
15- if (ImGui::IsKeyPressed (ImGuiKey_R)) {
16- point.position = point.reset_position ;
17- point.dragging = false ;
18- }
16+ if (ImGui::IsKeyPressed (ImGuiKey_R)) {
17+ point.position = point.reset_position ;
18+ point.dragging = false ;
19+ }
1920
20- bool hovering =
21- point.position .GetDistance (mouse_pos) < point.radius &&
22- point.position .GetDistance (point.prev_mouse_pos .value ()) < point.radius ;
23- if (!ImGui::IsMouseDown (0 )) {
24- point.dragging = false ;
25- } else if (hovering && ImGui::IsMouseClicked (0 )) {
26- point.dragging = true ;
27- }
28- if (point.dragging ) {
29- point.position += mouse_pos - point.prev_mouse_pos .value ();
30- }
31- ImGui::GetBackgroundDrawList ()->AddCircleFilled (
32- {point.position .x , point.position .y }, point.radius ,
33- ImColor (point.color .red , point.color .green , point.color .blue ,
34- (hovering || point.dragging ) ? 0 .6f : 0 .3f ));
35- if (hovering || point.dragging ) {
36- ImGui::GetBackgroundDrawList ()->AddText (
37- {point.position .x - point.radius , point.position .y + point.radius + 10 },
38- ImColor (point.color .red , point.color .green , point.color .blue , 1 .0f ),
39- impeller::SPrintF (" x:%0.3f y:%0.3f" , point.position .x , point.position .y )
40- .c_str ());
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;
4146 }
42- point.prev_mouse_pos = mouse_pos;
4347 return point.position ;
4448}
4549
@@ -48,14 +52,15 @@ std::tuple<Point, Point> DrawPlaygroundLine(PlaygroundPoint& point_a,
4852 Point position_a = DrawPlaygroundPoint (point_a);
4953 Point position_b = DrawPlaygroundPoint (point_b);
5054
51- auto dir = (position_b - position_a).Normalize () * point_a.radius ;
52- auto line_a = position_a + dir;
53- auto line_b = position_b - dir;
54- ImGui::GetBackgroundDrawList ()->AddLine (
55- {line_a.x , line_a.y }, {line_b.x , line_b.y },
56- ImColor (point_b.color .red , point_b.color .green , point_b.color .blue ,
57- 0 .3f ));
58-
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+ }
5964 return std::make_tuple (position_a, position_b);
6065}
6166//
0 commit comments