Euphoria
canvas.cc
Go to the documentation of this file.
1 #include "window/canvas.h"
2 
3 #define IMGUI_DEFINE_MATH_OPERATORS
4 #include "imgui/imgui_internal.h"
5 
6 #include "window/imgui_extra.h"
7 
8 using namespace eu::core;
9 
10 namespace eu::window
11 {
12  void
13  Canvas::show_grid(const CanvasConfig& cc) const
14  {
15  const auto size = ImGui::GetWindowSize();
16  ImDrawList* draw_list = ImGui::GetWindowDrawList();
17 
18  const float scaled_grid_size = cc.grid_size * view.scale;
19 
20  for(float x = fmodf(view.scroll.x, scaled_grid_size); x < size.x; x += scaled_grid_size)
21  {
22  draw_list->AddLine
23  (
24  ImVec2(x, 0.0f) + position,
25  ImVec2(x, size.y) + position,
26  cc.grid_color
27  );
28  }
29 
30  for(float y = fmodf(view.scroll.y, scaled_grid_size); y < size.y; y += scaled_grid_size)
31  {
32  draw_list->AddLine
33  (
34  ImVec2(0.0f, y) + position,
35  ImVec2(size.x, y) + position,
36  cc.grid_color
37  );
38  }
39  }
40 
41  void
42  Canvas::draw_vertical_line(float rx, ImU32 grid_color) const
43  {
44  const auto x = view.scroll.x + rx * view.scale;
45  const auto size = ImGui::GetWindowSize();
46  ImDrawList* draw_list = ImGui::GetWindowDrawList();
47  draw_list->AddLine
48  (
49  ImVec2(x, 0.0f) + position,
50  ImVec2(x, size.y) + position,
51  grid_color
52  );
53  }
54 
55  void
56  Canvas::draw_horizontal_line(float ry, ImU32 grid_color) const
57  {
58  const auto y = view.scroll.y + ry * view.scale;
59  const auto size = ImGui::GetWindowSize();
60  ImDrawList* draw_list = ImGui::GetWindowDrawList();
61  draw_list->AddLine
62  (
63  ImVec2(0.0f, y) + position,
64  ImVec2(size.x, y) + position,
65  grid_color
66  );
67  }
68 
69  void
70  Canvas::show_ruler(float ruler_interval, ImU32 ruler_color, float length) const
71  {
72  const auto size = ImGui::GetWindowSize();
73  ImDrawList* draw_list = ImGui::GetWindowDrawList();
74 
75  const float scaled_grid_size = ruler_interval * view.scale;
76 
77  for(float x = fmodf(view.scroll.x, scaled_grid_size); x < size.x;
78  x += scaled_grid_size)
79  {
80  draw_list->AddLine
81  (
82  ImVec2(x, 0.0f) + position,
83  ImVec2(x, length) + position,
84  ruler_color
85  );
86  }
87 
88  for(float y = fmodf(view.scroll.y, scaled_grid_size); y < size.y;
89  y += scaled_grid_size)
90  {
91  draw_list->AddLine
92  (
93  ImVec2(0.0f, y) + position,
94  ImVec2(length, y) + position,
95  ruler_color
96  );
97  }
98  }
99 
100  void
101  Canvas::show_ruler(const CanvasConfig& cc) const
102  {
103  show_ruler(5.0f, cc.grid_color, 10.0f);
104  }
105 
106  void
108  {
109  ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(1, 1));
110  ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
111  ImGui::PushStyleColor(ImGuiCol_ChildBg, cc.background_color);
112 
113  ImGui::BeginChild
114  (
115  "scrolling_region",
116  ImVec2(0, 0),
117  true,
118  ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoMove
119  );
120 
121  position = ImGui::GetCursorScreenPos();
122  }
123 
124  void
126  {
127  if(ImGui::IsWindowHovered() && !ImGui::IsAnyItemActive() && ImGui::IsMouseDragging(2, 0.0f))
128  {
129  canvas->view.pan(con(ImGui::GetIO().MouseDelta));
130  }
131  }
132 
133  void
135  {
136  if(ImGui::IsWindowHovered() && !ImGui::IsAnyItemActive())
137  {
138  const auto mouse = ImGui::GetIO().MousePos - canvas->position;
139  const auto zoom = ImGui::GetIO().MouseWheel;
140  canvas->view.zoom(con(mouse), zoom * cc.zoom_speed);
141  }
142  }
143 
144  void
145  Canvas::end(const CanvasConfig& cc)
146  {
148  react_to_canvas_zoom_events(this, cc);
149 
150  ImGui::EndChild();
151 
152  ImGui::PopStyleColor();
153  ImGui::PopStyleVar(2);
154  }
155 
156  ImVec2
157  Canvas::from_world_to_screen(const ImVec2& v) const
158  {
159  return con(view.from_world_to_screen(con(v))) + position;
160  }
161 
162  ImVec2
163  Canvas::from_world_to_screen_size(const ImVec2& v) const
164  {
165  return v * view.scale;
166  }
167 
168  ImVec2
169  Canvas::from_screen_to_world(const ImVec2& v) const
170  {
171  return con(view.from_screen_to_world(con(v - position)));
172  }
173 
174  ImVec2
175  Canvas::get_mouse() const
176  {
177  return ImGui::GetMousePos() - position;
178  }
179 }
constexpr Rgbi con(unsigned char r, unsigned char g, unsigned char b)
void react_to_canvas_zoom_events(Canvas *canvas, const CanvasConfig &cc)
Definition: canvas.cc:134
void react_to_canvas_scroll_events(Canvas *canvas)
Definition: canvas.cc:125
void pan(const vec2f &p)
Definition: canvaslogic.cc:7
void zoom(const vec2f &mouse, float zoom)
Definition: canvaslogic.cc:13
core::CanvasLogic view
Definition: canvas.h:19
ImVec2 position
Definition: canvas.h:20