OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4ConsoleQtShapes.cpp
Go to the documentation of this file.
1 /*
2 * OpenClonk, http://www.openclonk.org
3 *
4 * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5 * Copyright (c) 2013, The OpenClonk Team and contributors
6 *
7 * Distributed under the terms of the ISC license; see accompanying file
8 * "COPYING" for details.
9 *
10 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11 * See accompanying file "TRADEMARK" for details.
12 *
13 * To redistribute this file separately, substitute the full license texts
14 * for the above references.
15 */
16 
17 /* Editable shapes in the viewports (like e.g. AI guard range rectangles) */
18 
19 #include "C4Include.h"
20 #include "graphics/C4FacetEx.h"
21 #include "graphics/C4Draw.h"
22 #include "object/C4Object.h"
23 // See C4ConsoleQt.cpp on include order
24 #include "editor/C4Console.h"
27 #include "editor/C4ConsoleQtPropListViewer.h" // for C4PropertyPath
28 
29 /* Generic shape */
30 
31 C4ConsoleQtShape::C4ConsoleQtShape(C4Object *for_obj, C4PropList *props, const class C4PropertyDelegateShape *parent_delegate, class C4ConsoleQtShapes *shape_list)
32  : is_relative(true), dragging_border(-1), selected_border(-1), border_color(0xffff0000), parent_delegate(parent_delegate), shape_list(shape_list)
33 {
34  rel_obj.SetPropList(for_obj);
35  if (props)
36  {
37  is_relative = props->GetPropertyBool(P_Relative, is_relative);
38  border_color = props->GetPropertyInt(P_Color) | 0xff000000;
39  properties = C4VPropList(props);
40  }
41 }
42 
43 uint32_t C4ConsoleQtShape::GetBorderColor(int32_t border_index, bool dragging_border_is_bitmask, uint32_t default_color) const
44 {
45  // Return shape color, or dragged border color if index is the border currently being dragged
46  if (IsDragging())
47  if ((dragging_border == border_index) || (dragging_border_is_bitmask && (dragging_border & border_index)))
48  return 0xffffffff;
49  return default_color ? default_color : border_color;
50 }
51 
52 int32_t C4ConsoleQtShape::AbsX(int32_t rel_x) const
53 {
54  if (is_relative)
55  {
56  C4Object *obj = rel_obj.getObj();
57  if (obj) rel_x += obj->GetX();
58  }
59  return rel_x;
60 }
61 
62 int32_t C4ConsoleQtShape::AbsY(int32_t rel_y) const
63 {
64  if (is_relative)
65  {
66  C4Object *obj = rel_obj.getObj();
67  if (obj) rel_y += obj->GetY();
68  }
69  return rel_y;
70 }
71 
72 void C4ConsoleQtShape::StopDragging()
73 {
74  // Reset drag state and emit signal to send updated value
75  dragging_border = -1;
76  emit ShapeDragged();
77 }
78 
79 bool C4ConsoleQtShape::Select(int32_t border)
80 {
81  selected_border = border;
82  emit BorderSelectionChanged();
83  return true;
84 }
85 
86 void C4ConsoleQtShape::ResetSelection()
87 {
88  selected_border = -1;
89  emit BorderSelectionChanged();
90 }
91 
92 
93 /* Rectangular shape*/
94 
95 C4ConsoleQtRect::C4ConsoleQtRect(C4Object *for_obj, C4PropList *props, const class C4PropertyDelegateShape *parent_delegate, class C4ConsoleQtShapes *shape_list)
96  : C4ConsoleQtShape(for_obj, props, parent_delegate, shape_list), left(0), top(0), right(10), bottom(10), store_as_proplist(false), properties_lowercase(false)
97 {
98  // Def props
99  if (props)
100  {
101  C4String *storage = props->GetPropertyStr(P_Storage);
102  if (storage)
103  {
104  if (storage == &::Strings.P[P_proplist])
105  properties_lowercase = store_as_proplist = true;
106  else if (storage == &::Strings.P[P_Proplist])
107  store_as_proplist = true;
108  }
109  }
110 }
111 
112 bool C4ConsoleQtRect::IsHit(int32_t x, int32_t y, int32_t hit_range, Qt::CursorShape *drag_cursor, int32_t *drag_border, bool shift_down, bool ctrl_down)
113 {
114  // Current border pos
115  int32_t left = AbsX(this->left), top = AbsY(this->top);
116  int32_t right = AbsX(this->right), bottom = AbsY(this->bottom);
117  // Distance to each border
118  int32_t dleft = Abs<int32_t>(left - x);
119  int32_t dtop = Abs<int32_t>(top - y);
120  int32_t dright = Abs<int32_t>(right - x);
121  int32_t dbottom = Abs<int32_t>(bottom - y);
122  // In box at all?
123  if (x < left - hit_range || y < top - hit_range || x > right + hit_range || y > bottom + hit_range)
124  return false;
125  // Border hit?
126  bool hit_left = (dleft <= hit_range && dleft < dright);
127  bool hit_top = (dtop <= hit_range && dtop < dbottom);
128  bool hit_right = (!hit_left && dright <= hit_range);
129  bool hit_bottom = (!hit_top && dbottom <= hit_range);
130  // Compose cursor and drag border
131  int32_t idrag_border = (hit_left * CNAT_Left) + (hit_top * CNAT_Top) + (hit_right * CNAT_Right) + (hit_bottom * CNAT_Bottom);
132  if (idrag_border) *drag_border = idrag_border;
133  if (hit_left || hit_right)
134  if (hit_top || hit_bottom)
135  *drag_cursor = (hit_left == hit_top) ? Qt::SizeFDiagCursor : Qt::SizeBDiagCursor;
136  else
137  *drag_cursor = Qt::SizeHorCursor;
138  else if (hit_top || hit_bottom)
139  *drag_cursor = Qt::SizeVerCursor;
140  return !!idrag_border;
141 }
142 
143 void C4ConsoleQtRect::Draw(class C4TargetFacet &cgo, float line_width)
144 {
145  float left = float(AbsX(this->left)) + cgo.X - cgo.TargetX;
146  float top = float(AbsY(this->top)) + cgo.Y - cgo.TargetY;
147  float right = float(AbsX(this->right)) + cgo.X - cgo.TargetX;
148  float bottom = float(AbsY(this->bottom)) + cgo.Y - cgo.TargetY;
149  pDraw->DrawLineDw(cgo.Surface, left, top, right, top, GetBorderColor(CNAT_Top, true), line_width);
150  pDraw->DrawLineDw(cgo.Surface, right, top, right, bottom, GetBorderColor(CNAT_Right, true), line_width);
151  pDraw->DrawLineDw(cgo.Surface, right, bottom, left, bottom, GetBorderColor(CNAT_Bottom, true), line_width);
152  pDraw->DrawLineDw(cgo.Surface, left, bottom, left, top, GetBorderColor(CNAT_Left, true), line_width);
153 }
154 
155 void C4ConsoleQtRect::Drag(int32_t x, int32_t y, int32_t dx, int32_t dy, int32_t hit_range, Qt::CursorShape *drag_cursor)
156 {
157  if (dragging_border & CNAT_Left) left += dx;
158  if (dragging_border & CNAT_Top) top += dy;
159  if (dragging_border & CNAT_Right) right += dx;
160  if (dragging_border & CNAT_Bottom) bottom += dy;
161  if (left > right) std::swap(left, right);
162  if (top > bottom) std::swap(top, bottom);
163 }
164 
165 void C4ConsoleQtRect::SetValue(const C4Value &val)
166 {
167  // Expect rect to be given as proplist with properties X, Y, Wdt, Hgt or array with elements [left,top,width,height]
168  if (store_as_proplist)
169  {
170  C4PropList *vprops = val.getPropList();
171  if (vprops)
172  {
173  left = vprops->GetPropertyInt(properties_lowercase ? P_x : P_X);
174  top = vprops->GetPropertyInt(properties_lowercase ? P_y : P_Y);
175  right = left + vprops->GetPropertyInt(properties_lowercase ? P_wdt : P_Wdt) - 1;
176  bottom = top + vprops->GetPropertyInt(properties_lowercase ? P_hgt : P_Hgt) - 1;
177  }
178  }
179  else
180  {
181  C4ValueArray *varr = val.getArray();
182  if (varr && varr->GetSize() >= 4)
183  {
184  left = varr->GetItem(0).getInt();
185  top = varr->GetItem(1).getInt();
186  right = left + varr->GetItem(2).getInt() - 1; // right/bottom borders are drawn inclusively
187  bottom = top + varr->GetItem(3).getInt() - 1;
188  }
189  }
190 }
191 
192 C4Value C4ConsoleQtRect::GetValue() const
193 {
194  // Return array or proplist: Convert left/top/right/bottom (inclusive) to left/top/width/height
195  if (store_as_proplist)
196  {
197  C4PropList *pos_proplist = new C4PropListScript();
198  pos_proplist->SetProperty(properties_lowercase ? P_x : P_X, C4VInt(left));
199  pos_proplist->SetProperty(properties_lowercase ? P_y : P_Y, C4VInt(top));
200  pos_proplist->SetProperty(properties_lowercase ? P_wdt : P_Wdt, C4VInt(right - left + 1));
201  pos_proplist->SetProperty(properties_lowercase ? P_hgt : P_Hgt, C4VInt(bottom - top + 1));
202  return C4VPropList(pos_proplist);
203  }
204  else
205  {
206  C4ValueArray *pos_array = new C4ValueArray(4);
207  pos_array->SetItem(0, C4VInt(left));
208  pos_array->SetItem(1, C4VInt(top));
209  pos_array->SetItem(2, C4VInt(right - left + 1));
210  pos_array->SetItem(3, C4VInt(bottom - top + 1));
211  return C4VArray(pos_array);
212  }
213 }
214 
215 
216 /* Circle shape */
217 
218 C4ConsoleQtCircle::C4ConsoleQtCircle(class C4Object *for_obj, C4PropList *props, const class C4PropertyDelegateShape *parent_delegate, class C4ConsoleQtShapes *shape_list)
219  : C4ConsoleQtShape(for_obj, props, parent_delegate, shape_list), radius(10), cx(0), cy(0), can_move_center(false)
220 {
221  if (props)
222  {
223  can_move_center = props->GetPropertyBool(P_CanMoveCenter);
224  }
225 }
226 
227 bool C4ConsoleQtCircle::IsHit(int32_t x, int32_t y, int32_t hit_range, Qt::CursorShape *drag_cursor, int32_t *drag_border, bool shift_down, bool ctrl_down)
228 {
229  // Get relative circle center pos
230  x -= AbsX(cx);
231  y -= AbsY(cy);
232  int32_t r = x*x + y*y;
233  // Is on circle border? (Higher priority than center to allow resizing circle from 0 radius)
234  if (Inside<int32_t>(r, (radius - hit_range)*(radius - hit_range), (radius + hit_range)*(radius + hit_range)))
235  {
236  // Cursor by position on 60 deg circle segments
237  if (x * 58 / 100 / (y+!y)) // tan(30) ~= 0.58
238  *drag_cursor = Qt::CursorShape::SizeHorCursor;
239  else if (y * 58 / 100 / (x+!x))
240  *drag_cursor = Qt::CursorShape::SizeVerCursor;
241  else if (x*y > 0)
242  *drag_cursor = Qt::CursorShape::SizeFDiagCursor;
243  else
244  *drag_cursor = Qt::CursorShape::SizeBDiagCursor;
245  *drag_border = 0;
246  return true;
247  }
248  // Circle center?
249  if (can_move_center && r <= hit_range*hit_range)
250  {
251  *drag_cursor = Qt::CursorShape::SizeAllCursor;
252  *drag_border = 1;
253  return true;
254  }
255  return false;
256 }
257 
258 void C4ConsoleQtCircle::Draw(class C4TargetFacet &cgo, float line_width)
259 {
260  // Circle
261  pDraw->DrawCircleDw(cgo.Surface, AbsX(cx) + cgo.X - cgo.TargetX, AbsY(cy) + cgo.Y - cgo.TargetY, radius, GetBorderColor(0, false), line_width);
262  // Center if moveable
263  if (can_move_center)
264  pDraw->DrawCircleDw(cgo.Surface, AbsX(cx) + cgo.X - cgo.TargetX, AbsY(cy) + cgo.Y - cgo.TargetY, line_width*3, GetBorderColor(1, false), line_width);
265 }
266 
267 void C4ConsoleQtCircle::C4ConsoleQtCircle::Drag(int32_t x, int32_t y, int32_t dx, int32_t dy, int32_t hit_range, Qt::CursorShape *drag_cursor)
268 {
269  if (dragging_border == 0)
270  {
271  x -= AbsX(cx);
272  y -= AbsY(cy);
273  radius = int32_t(sqrt(double(x*x + y*y)));
274  }
275  else if (dragging_border == 1)
276  {
277  cx += dx;
278  cy += dy;
279  }
280 }
281 
282 void C4ConsoleQtCircle::SetValue(const C4Value &val)
283 {
284  // If center is moveable, expect value as [radius, center_x, center_y]
285  // Otherwise just radius
286  if (can_move_center)
287  {
288  C4ValueArray *aval = val.getArray();
289  if (aval && aval->GetSize() == 3)
290  {
291  radius = aval->GetItem(0).getInt();
292  cx = aval->GetItem(1).getInt();
293  cy = aval->GetItem(2).getInt();
294  }
295  }
296  else
297  {
298  radius = val.getInt();
299  }
300 }
301 
302 C4Value C4ConsoleQtCircle::GetValue() const
303 {
304  // Return single value for non-center-adjustable circles; return [radius, cx, cy] otherwise
305  if (can_move_center)
306  {
307  C4ValueArray *pos_array = new C4ValueArray(3);
308  pos_array->SetItem(0, C4VInt(radius));
309  pos_array->SetItem(1, C4VInt(cx));
310  pos_array->SetItem(2, C4VInt(cy));
311  return C4VArray(pos_array);
312  }
313  else
314  {
315  return C4VInt(radius);
316  }
317 }
318 
319 
320 /* Point shape */
321 
322 C4ConsoleQtPoint::C4ConsoleQtPoint(class C4Object *for_obj, C4PropList *props, const class C4PropertyDelegateShape *parent_delegate, class C4ConsoleQtShapes *shape_list)
323  : C4ConsoleQtShape(for_obj, props, parent_delegate, shape_list), cx(0), cy(0)
324 {
325  if (props)
326  {
327  horizontal_fix = props->GetPropertyBool(P_HorizontalFix);
328  vertical_fix = props->GetPropertyBool(P_VerticalFix);
329  }
330 }
331 
332 bool C4ConsoleQtPoint::IsHit(int32_t x, int32_t y, int32_t hit_range, Qt::CursorShape *drag_cursor, int32_t *drag_border, bool shift_down, bool ctrl_down)
333 {
334  // Get relative circle center pos
335  x -= AbsX(cx);
336  y -= AbsY(cy);
337  int32_t r = x*x + y*y;
338  // Hits point?
339  if (r <= hit_range*hit_range*6)
340  {
341  if (horizontal_fix && vertical_fix) *drag_cursor = Qt::CursorShape::ForbiddenCursor;
342  if (horizontal_fix && !vertical_fix) *drag_cursor = Qt::CursorShape::SizeVerCursor;
343  if (!horizontal_fix && vertical_fix) *drag_cursor = Qt::CursorShape::SizeHorCursor;
344  if (!horizontal_fix && !vertical_fix) *drag_cursor = Qt::CursorShape::SizeAllCursor;
345  *drag_border = 0;
346  return true;
347  }
348  return false;
349 }
350 
351 void C4ConsoleQtPoint::Draw(class C4TargetFacet &cgo, float line_width)
352 {
353  // Circle with cross inside
354  uint32_t clr = GetBorderColor(0, false);
355  float d = line_width * 3;
356  float dc = sqrtf(2) * d;
357  int32_t x = AbsX(cx) + cgo.X - cgo.TargetX;
358  int32_t y = AbsY(cy) + cgo.Y - cgo.TargetY;
359  if (horizontal_fix && !vertical_fix)
360  {
361  pDraw->DrawLineDw(cgo.Surface, x, y - d, x, y + d, clr, line_width);
362  pDraw->DrawLineDw(cgo.Surface, x - d / 2, y - d, x + d / 2, y - d, clr, line_width);
363  pDraw->DrawLineDw(cgo.Surface, x - d / 2, y + d, x + d / 2, y + d, clr, line_width);
364  }
365  else if (!horizontal_fix && vertical_fix)
366  {
367  pDraw->DrawLineDw(cgo.Surface, x - d, y, x + d, y, clr, line_width);
368  pDraw->DrawLineDw(cgo.Surface, x - d, y - d / 2, x - d, y + d / 2, clr, line_width);
369  pDraw->DrawLineDw(cgo.Surface, x + d, y - d / 2, x + d, y + d / 2, clr, line_width);
370  }
371  else
372  {
373  pDraw->DrawLineDw(cgo.Surface, x - d, y - d, x + d, y + d, clr, line_width);
374  if (!horizontal_fix)
375  {
376  pDraw->DrawLineDw(cgo.Surface, x - d, y + d, x + d, y - d, clr, line_width);
377  }
378  pDraw->DrawCircleDw(cgo.Surface, x, y, dc, clr, line_width);
379  }
380 }
381 
382 void C4ConsoleQtPoint::Drag(int32_t x, int32_t y, int32_t dx, int32_t dy, int32_t hit_range, Qt::CursorShape *drag_cursor)
383 {
384  if (!horizontal_fix) cx += dx;
385  if (!vertical_fix) cy += dy;
386 }
387 
388 void C4ConsoleQtPoint::SetValue(const C4Value &val)
389 {
390  // Expect value as [x,y]
391  C4ValueArray *aval = val.getArray();
392  if (aval && aval->GetSize() == 2)
393  {
394  cx = aval->GetItem(0).getInt();
395  cy = aval->GetItem(1).getInt();
396  }
397 }
398 
399 C4Value C4ConsoleQtPoint::GetValue() const
400 {
401  // Return [cx, cy]
402  C4ValueArray *pos_array = new C4ValueArray(2);
403  pos_array->SetItem(0, C4VInt(cx));
404  pos_array->SetItem(1, C4VInt(cy));
405  return C4VArray(pos_array);
406 }
407 
408 
409 /* Graph */
410 
411 bool C4ConsoleQtGraph::Edge::connects_to(int32_t vertex_index) const
412 {
413  // Check if this edge connects to given vertex
414  return vertex_indices[0] == vertex_index || vertex_indices[1] == vertex_index;
415 }
416 
417 bool C4ConsoleQtGraph::Edge::connects_to(int32_t vertex_index, int32_t *idx) const
418 {
419  // Check if this edge connects to given vertex. If so, put vertex_index index into *idx
420  assert(idx);
421  if (vertex_indices[0] == vertex_index)
422  {
423  *idx = 0;
424  }
425  else if (vertex_indices[1] == vertex_index)
426  {
427  *idx = 1;
428  }
429  else
430  {
431  return false;
432  }
433  return true;
434 }
435 
436 
437 void C4ConsoleQtGraph::GraphData::SetVerticesValue(const C4ValueArray *vvertices)
438 {
439  vertices.clear();
440  if (vvertices)
441  {
442  vertices.reserve(vvertices->GetSize());
443  for (int32_t i = 0; i < vvertices->GetSize(); ++i)
444  {
445  Vertex vtx;
446  C4PropList *vvertex = vvertices->GetItem(i).getPropList();
447  if (!vvertex) continue;
448  vtx.x = vvertex->GetPropertyInt(P_X);
449  vtx.y = vvertex->GetPropertyInt(P_Y);
450  if (vvertex->HasProperty(&::Strings.P[P_Color])) vtx.color = vvertex->GetPropertyInt(P_Color) | 0xff000000;
451  vertices.push_back(vtx);
452  }
453  }
454 }
455 
456 void C4ConsoleQtGraph::GraphData::SetEdgesValue(const C4ValueArray *vedges)
457 {
458  edges.clear();
459  if (vedges)
460  {
461  edges.reserve(vedges->GetSize());
462  for (int32_t i = 0; i < vedges->GetSize(); ++i)
463  {
464  C4ValueArray *vedgevertices = nullptr;
465  C4PropList *vedgeprops = nullptr;
466  vedgeprops = vedges->GetItem(i).getPropList();
467  if (vedgeprops)
468  {
469  vedgevertices = vedgeprops->GetPropertyArray(P_Vertices);
470  }
471  if (vedgevertices && vedgevertices->GetSize() >= 2)
472  {
473  Edge edge;
474  edge.vertex_indices[0] = vedgevertices->GetItem(0).getInt();
475  edge.vertex_indices[1] = vedgevertices->GetItem(1).getInt();
476  // Ignore invalid edge definitions
477  if (edge.vertex_indices[0] < 0 || edge.vertex_indices[1] < 0) continue;
478  if (edge.vertex_indices[0] >= vertices.size() || edge.vertex_indices[1] >= vertices.size()) continue;
479  // Optional properties
480  if (vedgeprops->HasProperty(&::Strings.P[P_Color])) edge.color = vedgeprops->GetPropertyInt(P_Color) | 0xff000000;
481  edge.line_thickness = vedgeprops->GetPropertyInt(P_LineWidth, edge.line_thickness);
482  edges.push_back(edge);
483  }
484  }
485  }
486 }
487 
488 C4ValueArray *C4ConsoleQtGraph::GraphData::GetVerticesValue() const
489 {
490  // Store vertices
491  C4ValueArray *vvertices = new C4ValueArray();
492  vvertices->SetSize(vertices.size());
493  int32_t i = 0;
494  for (const Vertex &vtx : vertices)
495  {
496  C4Value valvtx;
497  C4PropList *vvtx = C4PropList::New();
498  vvtx->SetProperty(P_X, C4VInt(vtx.x));
499  vvtx->SetProperty(P_Y, C4VInt(vtx.y));
500  if (vtx.color) vvtx->SetProperty(P_Color, C4VInt(vtx.color & 0xffffff));
501  vvertices->SetItem(i++, C4VPropList(vvtx));
502  }
503  return vvertices;
504 }
505 
506 C4ValueArray *C4ConsoleQtGraph::GraphData::GetEdgesValue() const
507 {
508  C4ValueArray *vedges = new C4ValueArray();
509  vedges->SetSize(edges.size());
510  int32_t i = 0;
511  for (const Edge &edge : edges)
512  {
513  C4Value valedge;
514  C4ValueArray *vedge = new C4ValueArray(2);
515  vedge->SetItem(0, C4VInt(edge.vertex_indices[0]));
516  vedge->SetItem(1, C4VInt(edge.vertex_indices[1]));
517  C4PropList *vedgeprops = C4PropList::New();
518  vedgeprops->SetProperty(P_Vertices, C4VArray(vedge));
519  if (edge.color) vedgeprops->SetProperty(P_Color, C4VInt(edge.color & 0xffffff));
520  if (edge.line_thickness != 1) vedgeprops->SetProperty(P_LineWidth, C4VInt(edge.line_thickness));
521  vedges->SetItem(i++, C4VPropList(vedgeprops));
522  }
523  return vedges;
524 }
525 
526 void C4ConsoleQtGraph::GraphData::SetVertexPos(int32_t vertex_index, int32_t new_x, int32_t new_y)
527 {
528  // Validity check
529  if (vertex_index < 0 || vertex_index >= vertices.size()) return;
530  // Do change
531  vertices[vertex_index].x = new_x;
532  vertices[vertex_index].y = new_y;
533 }
534 
535 void C4ConsoleQtGraph::GraphData::EditEdge(int32_t edge_index, int32_t change_vertex_index, int32_t new_vertex_index)
536 {
537  // Validity check
538  if (edge_index < 0 || edge_index >= edges.size()) return;
539  if (new_vertex_index < 0 || new_vertex_index >= vertices.size()) return;
540  if (change_vertex_index < 0 || change_vertex_index > 1) return;
541  // No loopback edges
542  Edge &edge = edges[edge_index];
543  int32_t other_vertex_index = edge.vertex_indices[!change_vertex_index];
544  if (new_vertex_index == other_vertex_index) return;
545  // Do not allow duplicates
546  for (Edge &check_edge : edges)
547  {
548  if (&check_edge != &edge)
549  {
550  if (check_edge.connects_to(new_vertex_index) && check_edge.connects_to(other_vertex_index)) return;
551  }
552  }
553  // Perform change
554  edge.vertex_indices[change_vertex_index] = new_vertex_index;
555 }
556 
557 void C4ConsoleQtGraph::GraphData::InsertEdgeBefore(int32_t insert_edge_index, int32_t vertex1, int32_t vertex2)
558 {
559  // Validity check
560  if (insert_edge_index < 0 || insert_edge_index > edges.size()) return;
561  if (vertex1 < 0 || vertex1 >= vertices.size()) return;
562  if (vertex2 < 0 || vertex2 >= vertices.size()) return;
563  // Do not allow duplicates
564  for (Edge &check_edge : edges)
565  {
566  if (check_edge.connects_to(vertex1) && check_edge.connects_to(vertex2)) return;
567  }
568  // Insert edge at position in edge list
569  Edge new_edge;
570  new_edge.vertex_indices[0] = vertex1;
571  new_edge.vertex_indices[1] = vertex2;
572  edges.insert(edges.begin() + insert_edge_index, new_edge);
573 }
574 
575 void C4ConsoleQtGraph::GraphData::InsertVertexBefore(int32_t insert_vertex_index, int32_t x, int32_t y)
576 {
577  // Validity check
578  if (insert_vertex_index < 0 || insert_vertex_index > vertices.size()) return;
579  // Insert new vertex at desired position
580  Vertex new_vertex;
581  new_vertex.x = x;
582  new_vertex.y = y;
583  vertices.insert(vertices.begin() + insert_vertex_index, new_vertex);
584  // Update all edges pointing to vertices after this one
585  for (Edge &edge : edges)
586  {
587  for (int32_t &vertex_index : edge.vertex_indices)
588  {
589  if (vertex_index >= insert_vertex_index)
590  {
591  ++vertex_index;
592  }
593  }
594  }
595 }
596 
597 void C4ConsoleQtGraph::GraphData::RemoveEdge(int32_t edge_index)
598 {
599  // Validity check
600  if (edge_index < 0 || edge_index >= edges.size()) return;
601  // Kill the edge
602  Edge removed_edge = edges[edge_index];
603  edges.erase(edges.begin() + edge_index);
604 }
605 
606 void C4ConsoleQtGraph::GraphData::RemoveVertex(int32_t remove_vertex_index)
607 {
608  // Validity vheck
609  if (remove_vertex_index < 0 || remove_vertex_index >= vertices.size()) return;
610  // Always keep at least one vertex
611  if (vertices.size() == 1) return;
612  assert(remove_vertex_index >= 0 && remove_vertex_index < vertices.size() && vertices.size() >= 2);
613  // Remove all edges involving this vertex
614  auto rm_check = [remove_vertex_index](const Edge &edge) { return edge.connects_to(remove_vertex_index); };
615  edges.erase(std::remove_if(edges.begin(), edges.end(), rm_check), edges.end());
616  // Remove the vertex itself
617  vertices.erase(vertices.begin() + remove_vertex_index);
618  // Because vertex indices changed, update all edges that pointed to higher indices
619  for (Edge &edge : edges)
620  {
621  for (int32_t &vi : edge.vertex_indices)
622  {
623  if (vi > remove_vertex_index)
624  {
625  --vi;
626  }
627  }
628  }
629 }
630 
631 int32_t C4ConsoleQtGraph::GraphData::GetEdgeCountForVertex(int32_t vertex_index) const
632 {
633  // Count all edges that connect to the given vertex
634  auto count_check = [vertex_index](const Edge &edge) { return edge.connects_to(vertex_index); };
635  return std::count_if(edges.begin(), edges.end(), count_check);
636 }
637 
638 void C4ConsoleQtGraph::GraphData::EditGraphValue_SetVertexPos(C4ValueArray *vvertices, int32_t vertex_index, int32_t new_x, int32_t new_y)
639 {
640  // Validity check
641  if (vertex_index < 0 || vertex_index >= vvertices->GetSize()) return;
642  // Do change
643  C4PropList *vvertex = vvertices->GetItem(vertex_index).getPropList();
644  if (!vvertex || vvertex->IsFrozen()) return;
645  vvertex->SetProperty(P_X, C4VInt(new_x));
646  vvertex->SetProperty(P_Y, C4VInt(new_y));
647 }
648 
649 void C4ConsoleQtGraph::GraphData::EditGraphValue_EditEdge(C4ValueArray *vvertices, C4ValueArray *vedges, int32_t edge_index, int32_t change_vertex_index, int32_t new_vertex_index)
650 {
651  // Validity check
652  if (edge_index < 0 || edge_index >= vedges->GetSize()) return;
653  if (new_vertex_index < 0 || new_vertex_index >= vvertices->GetSize()) return;
654  if (change_vertex_index < 0 || change_vertex_index > 1) return;
655  C4PropList *edge = vedges->GetItem(edge_index).getPropList();
656  if (!edge) return;
657  C4ValueArray *edge_vertices = edge->GetPropertyArray(P_Vertices);
658  if (!edge_vertices || edge_vertices->IsFrozen()) return;
659  // No loopback edges
660  int32_t other_vertex_index = edge_vertices->GetItem(!change_vertex_index).getInt();
661  if (new_vertex_index == other_vertex_index) return;
662  // Do not allow duplicates
663  for (int32_t i = 0; i < vedges->GetSize(); ++i)
664  {
665  if (i != edge_index)
666  {
667  if (EditGraphValue_EdgeConnectsTo(vedges->GetItem(i).getPropList(), other_vertex_index) && EditGraphValue_EdgeConnectsTo(vedges->GetItem(i).getPropList(), new_vertex_index))
668  {
669  return;
670  }
671  }
672  }
673  // Perform change
674  edge_vertices->SetItem(change_vertex_index, C4VInt(new_vertex_index));
675 }
676 
677 void C4ConsoleQtGraph::GraphData::EditGraphValue_InsertEdgeBefore(C4ValueArray *vvertices, C4ValueArray *vedges, int32_t insert_edge_index, int32_t vertex1, int32_t vertex2)
678 {
679  // Validity check
680  if (insert_edge_index < 0 || insert_edge_index > vedges->GetSize()) return;
681  if (vertex1 < 0 || vertex1 >= vvertices->GetSize()) return;
682  // Do not allow duplicates
683  for (int32_t i = 0; i < vedges->GetSize(); ++i)
684  {
685  if (EditGraphValue_EdgeConnectsTo(vedges->GetItem(i).getPropList(), vertex1) && EditGraphValue_EdgeConnectsTo(vedges->GetItem(i).getPropList(), vertex2))
686  {
687  return;
688  }
689  }
690  // Construct new edge
691  C4PropList *new_edge = C4PropList::New();
692  C4ValueArray *new_edge_indices = new C4ValueArray(2);
693  new_edge_indices->SetItem(0, C4VInt(vertex1));
694  new_edge_indices->SetItem(1, C4VInt(vertex2));
695  new_edge->SetProperty(P_Vertices, C4VArray(new_edge_indices));
696  // Insert edge at position in edge list by moving other edges up
697  vedges->SetSize(vedges->GetSize() + 1);
698  for (int32_t i = vedges->GetSize() - 1; i > insert_edge_index; --i)
699  {
700  vedges->SetItem(i, vedges->GetItem(i - 1));
701  }
702  vedges->SetItem(insert_edge_index, C4VPropList(new_edge));
703 }
704 
705 void C4ConsoleQtGraph::GraphData::EditGraphValue_InsertVertexBefore(C4ValueArray *vvertices, C4ValueArray *vedges, int32_t insert_vertex_index, int32_t x, int32_t y)
706 {
707  // Validity check
708  if (insert_vertex_index < 0 || insert_vertex_index > vvertices->GetSize()) return;
709  // Construct new vertex
710  C4PropList *new_vertex = C4PropList::New();
711  new_vertex->SetProperty(P_X, C4VInt(x));
712  new_vertex->SetProperty(P_Y, C4VInt(y));
713  // Insert new vertex at desired position by moving other vertices up
714  vvertices->SetSize(vvertices->GetSize() + 1);
715  for (int32_t i = vvertices->GetSize() - 1; i > insert_vertex_index; --i)
716  {
717  vvertices->SetItem(i, vvertices->GetItem(i - 1));
718  }
719  vvertices->SetItem(insert_vertex_index, C4VPropList(new_vertex));
720  // Update all edges pointing to vertices after this one
721  if (vedges)
722  {
723  for (int32_t i = 0; i < vedges->GetSize(); ++i)
724  {
725  C4PropList *edge = vedges->GetItem(i).getPropList();
726  if (edge)
727  {
728  C4ValueArray *edge_vertices = edge->GetPropertyArray(P_Vertices);
729  if (edge_vertices && edge_vertices->GetSize() >= 2)
730  {
731  for (int32_t j = 0; j < 2; ++j)
732  {
733  int32_t vertex_index = edge_vertices->GetItem(j).getInt();
734  if (vertex_index >= insert_vertex_index)
735  {
736  edge_vertices->SetItem(j, C4VInt(vertex_index + 1));
737  }
738  }
739  }
740  }
741  }
742  }
743 }
744 
745 void C4ConsoleQtGraph::GraphData::EditGraphValue_RemoveEdge(C4ValueArray *vvertices, C4ValueArray *vedges, int32_t edge_index)
746 {
747  // Validity check
748  if (edge_index < 0 || edge_index >= vedges->GetSize()) return;
749  // Kill the edge
750  for (int32_t i = edge_index; i < vedges->GetSize() - 1; ++i)
751  {
752  vedges->SetItem(i, vedges->GetItem(i + 1));
753  }
754  vedges->SetSize(vedges->GetSize() - 1);
755 }
756 
757 void C4ConsoleQtGraph::GraphData::EditGraphValue_RemoveVertex(C4ValueArray *vvertices, C4ValueArray *vedges, int32_t remove_vertex_index)
758 {
759  // Validity vheck
760  if (remove_vertex_index < 0 || remove_vertex_index >= vvertices->GetSize()) return;
761  // Always keep at least one vertex
762  if (vvertices->GetSize() == 1) return;
763  // Remove the vertex itself
764  for (int32_t i = remove_vertex_index; i < vvertices->GetSize() - 1; ++i)
765  {
766  vvertices->SetItem(i, vvertices->GetItem(i + 1));
767  }
768  vvertices->SetSize(vvertices->GetSize() - 1);
769  // Edge updates
770  if (vedges)
771  {
772  for (int32_t edge_index = vedges->GetSize(); edge_index >= 0; --edge_index)
773  {
774  // Remove all edges involving this vertex and update vertices at higher index because they have been moved down
775  C4PropList *edge = vedges->GetItem(edge_index).getPropList();
776  if (edge)
777  {
778  C4ValueArray *edge_vertices = edge->GetPropertyArray(P_Vertices);
779  if (edge_vertices && edge_vertices->GetSize() >= 2)
780  {
781  for (int32_t j = 0; j < 2; ++j)
782  {
783  int32_t v = edge_vertices->GetItem(j).getInt();
784  if (v == remove_vertex_index)
785  {
786  EditGraphValue_RemoveEdge(vvertices, vedges, edge_index);
787  break;
788  }
789  else if (v > remove_vertex_index)
790  {
791  edge_vertices->SetItem(j, C4VInt(v - 1));
792  }
793  }
794  }
795  }
796  }
797  }
798 }
799 
800 bool C4ConsoleQtGraph::GraphData::EditGraphValue_EdgeConnectsTo(C4PropList *edge, int32_t vertex_index)
801 {
802  // Check if either side of the edge connects to given vertex
803  if (!edge) return false;
804  C4ValueArray *edge_vertices = edge->GetPropertyArray(P_Vertices);
805  if (!edge_vertices || edge_vertices->GetSize() < 2) return false;
806  return edge_vertices->GetItem(0).getInt() == vertex_index || edge_vertices->GetItem(1).getInt() == vertex_index;
807 }
808 
809 
810 C4ConsoleQtGraph::C4ConsoleQtGraph(C4Object *for_obj, C4PropList *props, const class C4PropertyDelegateShape *parent_delegate, class C4ConsoleQtShapes *shape_list)
811  : C4ConsoleQtShape(for_obj, props, parent_delegate, shape_list)
812 {
813  // Def props
814  if (props)
815  {
816  props->GetProperty(P_VertexDelegate, &vertex_delegate);
817  props->GetProperty(P_EdgeDelegate, &edge_delegate);
818  allow_vertex_selection = !!vertex_delegate.getPropList();
819  allow_edge_selection = !!edge_delegate.getPropList();
820  horizontal_fix = props->GetPropertyBool(P_HorizontalFix);
821  vertical_fix = props->GetPropertyBool(P_VerticalFix);
822  structure_fix = props->GetPropertyBool(P_StructureFix);
823  draw_arrows = props->GetPropertyBool(P_DrawArrows, draw_arrows);
824  }
825 }
826 
827 bool C4ConsoleQtGraph::IsHit(int32_t x, int32_t y, int32_t hit_range, Qt::CursorShape *drag_cursor, int32_t *drag_border, bool shift_down, bool ctrl_down)
828 {
829  // Check hit on vertices
830  int32_t i = 0, best_hit_range = hit_range*hit_range * 6*6;
831  bool has_hit = false;
832  for (const Vertex &vtx : graph.vertices)
833  {
834  int32_t dx = x - AbsX(vtx.x);
835  int32_t dy = y - AbsY(vtx.y);
836  int32_t r = dx*dx + dy*dy;
837  // Is close to vertex / closer than previous hit?
838  if (r <= best_hit_range)
839  {
840  if (IsVertexHit(i, drag_cursor, shift_down, ctrl_down))
841  {
842  *drag_border = VertexToDragBorder(i);
843  best_hit_range = r;
844  has_hit = true;
845  }
846  }
847  ++i;
848  }
849  // Check hit on edge if edge selection is possible
850  if (!has_hit && (allow_edge_selection || shift_down || ctrl_down))
851  {
852  best_hit_range = hit_range*hit_range;
853  i = 0;
854  for (const Edge &edge : graph.edges)
855  {
856  // Get affected vertices
857  assert(edge.vertex_indices[0] >= 0 && edge.vertex_indices[1] >= 0);
858  assert(edge.vertex_indices[0] < graph.vertices.size() && edge.vertex_indices[1] < graph.vertices.size());
859  const Vertex &v0 = graph.vertices[edge.vertex_indices[0]];
860  const Vertex &v1 = graph.vertices[edge.vertex_indices[1]];
861  // Relative edge pos
862  int32_t dx0 = x - AbsX(v0.x);
863  int32_t dy0 = y - AbsY(v0.y);
864  int32_t dx1 = v1.x - v0.x;
865  int32_t dy1 = v1.y - v0.y;
866  // Check if within line range
867  int32_t d = dx0 * dx1 + dy0 * dy1;
868  if (d > 0 && d < dx1 * dx1 + dy1 * dy1)
869  {
870  // Get squared distance from edge
871  d = dx1 * dy0 - dy1 * dx0;
872  d = d * d / (dx1 * dx1 + dy1 * dy1);
873  // In hit range?
874  if (d <= best_hit_range)
875  {
876  if (IsEdgeHit(i, drag_cursor, shift_down, ctrl_down))
877  *drag_border = EdgeToDragBorder(i);
878  best_hit_range = d;
879  has_hit = true;
880  }
881  }
882  ++i;
883  }
884  }
885  // Check if structure change is allowed
886  if (has_hit && (shift_down || ctrl_down) && structure_fix)
887  {
888  *drag_cursor = Qt::CursorShape::ForbiddenCursor;
889  }
890  else if (*drag_cursor == Qt::CursorShape::SizeAllCursor)
891  {
892  // Adjust cursor for constrained movement
893  if (horizontal_fix && vertical_fix)
894  {
895  *drag_cursor = Qt::CursorShape::PointingHandCursor;
896  }
897  else if (horizontal_fix)
898  {
899  *drag_cursor = Qt::CursorShape::SizeVerCursor;
900  }
901  else if (vertical_fix)
902  {
903  *drag_cursor = Qt::CursorShape::SizeHorCursor;
904  }
905  }
906  return has_hit;
907 }
908 
909 bool C4ConsoleQtGraph::IsVertexHit(int32_t vertex_index, Qt::CursorShape *drag_cursor, bool shift_down, bool ctrl_down)
910 {
911  if (shift_down && !ctrl_down)
912  {
913  // Insert vertex here
914  *drag_cursor = Qt::CursorShape::DragCopyCursor;
915  }
916  else if (ctrl_down && !shift_down)
917  {
918  // Remove this vertex (unless it's the last, which cannot be removed)
919  if (graph.vertices.size() == 1)
920  {
921  *drag_cursor = Qt::CursorShape::ForbiddenCursor;
922  }
923  else
924  {
925  *drag_cursor = Qt::CursorShape::DragMoveCursor;
926  }
927  }
928  else
929  {
930  // Normal dragging
931  *drag_cursor = Qt::CursorShape::SizeAllCursor;
932  }
933  return true;
934 }
935 
936 bool C4ConsoleQtGraph::IsEdgeHit(int32_t edge_index, Qt::CursorShape *drag_cursor, bool shift_down, bool ctrl_down)
937 {
938  if (shift_down && !ctrl_down)
939  {
940  // Insert vertex here
941  *drag_cursor = Qt::CursorShape::DragCopyCursor;
942  }
943  else if (ctrl_down && !shift_down)
944  {
945  // Remove this edge
946  *drag_cursor = Qt::CursorShape::DragMoveCursor;
947  }
948  else if (allow_edge_selection)
949  {
950  // Normal selection
951  *drag_cursor = Qt::CursorShape::PointingHandCursor; // can select, but cannot move
952  }
953  else
954  {
955  // Nothing to do with the edge. Selection disabled.
956  return false;
957  }
958  return true;
959 }
960 
961 void C4ConsoleQtGraph::DrawEdge(class C4TargetFacet &cgo, const Vertex &v0, const Vertex &v1, uint32_t clr, float line_width, float edge_width, bool highlight)
962 {
963  float vx0 = AbsX(v0.x) + cgo.X - cgo.TargetX;
964  float vy0 = AbsY(v0.y) + cgo.Y - cgo.TargetY;
965  float vx1 = AbsX(v1.x) + cgo.X - cgo.TargetX;
966  float vy1 = AbsY(v1.y) + cgo.Y - cgo.TargetY;
967  float dx = v1.x - v0.x, dy = v1.y - v0.y;
968  float d = sqrt(dx*dx + dy*dy);
969  if (highlight)
970  {
971  // Selected edge is surrounded by a highlight
972  float ddx = dy / d * edge_width;
973  float ddy = dx / d * edge_width;
974  pDraw->DrawLineDw(cgo.Surface, vx0 + ddx, vy0 + ddy, vx1 + ddx, vy1 + ddy, 0xffffffff, edge_width);
975  pDraw->DrawLineDw(cgo.Surface, vx0 - ddx, vy0 - ddy, vx1 - ddx, vy1 - ddy, 0xffffffff, edge_width);
976  }
977  // Regular line draw
978  pDraw->DrawLineDw(cgo.Surface, vx0, vy0, vx1, vy1, clr, edge_width);
979  // Arrowheads
980  if (draw_arrows)
981  {
982  // Arrowhead points to outer rim of vertex circle if possible
983  float d_vtx = std::min<float>(d, line_width * 3 * sqrtf(2));
984  float arrx = vx1 - dx / d * d_vtx;
985  float arry = vy1 - dy / d * d_vtx;
986  float arrowhead_size = std::max<float>(2.0, std::min<float>(d / 2, 8.0)) * edge_width;
987  float ddx1 = (-dy / 2 - dx);
988  float ddy1 = (+dx / 2 - dy);
989  float d1 = sqrt(ddx1*ddx1 + ddy1*ddy1);
990  float ddx2 = (+dy / 2 - dx);
991  float ddy2 = (-dx / 2 - dy);
992  float d2 = sqrt(ddx2*ddx2 + ddy2*ddy2);
993  pDraw->DrawLineDw(cgo.Surface, arrx, arry, arrx + ddx1 * arrowhead_size / d1, arry + ddy1 * arrowhead_size / d1, clr, edge_width);
994  pDraw->DrawLineDw(cgo.Surface, arrx, arry, arrx + ddx2 * arrowhead_size / d2, arry + ddy2 * arrowhead_size / d2, clr, edge_width);
995  }
996 }
997 
998 void C4ConsoleQtGraph::Draw(class C4TargetFacet &cgo, float line_width)
999 {
1000  // Draw edges as lines
1001  int32_t i = 0;
1002  for (const Edge &edge : graph.edges)
1003  {
1004  uint32_t clr = GetBorderColor(EdgeToDragBorder(i), false, edge.color);
1005  assert(edge.vertex_indices[0] >= 0 && edge.vertex_indices[1] >= 0);
1006  assert(edge.vertex_indices[0] < graph.vertices.size() && edge.vertex_indices[1] < graph.vertices.size());
1007  const Vertex &v0 = graph.vertices[edge.vertex_indices[0]];
1008  const Vertex &v1 = graph.vertices[edge.vertex_indices[1]];
1009  float edge_width = line_width * edge.line_thickness;
1010  bool highlight = (IsEdgeDrag(selected_border) && DragBorderToEdge(selected_border) == i);
1011  DrawEdge(cgo, v0, v1, clr, line_width, edge_width, highlight);
1012  ++i;
1013  }
1014  // Draw vertices as circles with cross inside
1015  i = 0;
1016  for (const Vertex &vtx : graph.vertices)
1017  {
1018  uint32_t clr = GetBorderColor(VertexToDragBorder(i), false, vtx.color);
1019  float d = line_width * 3;
1020  float dc = sqrtf(2) * d;
1021  int32_t x = AbsX(vtx.x) + cgo.X - cgo.TargetX;
1022  int32_t y = AbsY(vtx.y) + cgo.Y - cgo.TargetY;
1023  pDraw->DrawLineDw(cgo.Surface, x - d, y - d, x + d, y + d, clr, line_width);
1024  pDraw->DrawLineDw(cgo.Surface, x - d, y + d, x + d, y - d, clr, line_width);
1025  // Selected vertex is surrounded by a highlight
1026  if (IsVertexDrag(selected_border) && DragBorderToVertex(selected_border) == i)
1027  {
1028  pDraw->DrawCircleDw(cgo.Surface, x, y, dc + line_width, 0xffffffff, line_width);
1029  pDraw->DrawCircleDw(cgo.Surface, x, y, dc - line_width, 0xffffffff, line_width);
1030  }
1031  pDraw->DrawCircleDw(cgo.Surface, x, y, dc, clr, line_width);
1032  ++i;
1033  }
1034 }
1035 
1036 bool C4ConsoleQtGraph::StartDragging(int32_t *border, int32_t x, int32_t y, bool shift_down, bool ctrl_down)
1037 {
1038  assert(*border != -1);
1039  drag_snap_offset_x = drag_snap_offset_y = 0;
1040  drag_snapped = false;
1041  drag_snap_vertex = -1;
1042  drag_source_vertex_index = -1;
1043  if (shift_down && !ctrl_down && !structure_fix)
1044  {
1045  // Shift: Insert vertex
1046  if (IsEdgeDrag(*border))
1047  {
1048  // Insert on edge
1049  *border = dragging_border = VertexToDragBorder(InsertVertexOnEdge(DragBorderToEdge(*border), x - AbsX(), y - AbsY()));
1050  }
1051  else
1052  {
1053  // Insert from other vertex
1054  drag_source_vertex_index = DragBorderToVertex(*border);
1055  *border = dragging_border = VertexToDragBorder(InsertVertexOnVertex(drag_source_vertex_index, x - AbsX(), y - AbsY()));
1056  }
1057  // Start dragging
1058  return true;
1059  }
1060  else if (ctrl_down && !shift_down && !structure_fix)
1061  {
1062  // Ctrl: Delete vertex or edge
1063  if (IsEdgeDrag(*border))
1064  {
1065  // Delete edge
1066  RemoveEdge(DragBorderToEdge(*border));
1067  }
1068  else
1069  {
1070  // Remove vertex unless it is the last one
1071  // If there's only one vertex left, just keep it
1072  if (graph.vertices.size() > 1) RemoveVertex(DragBorderToVertex(*border), true);
1073  }
1074  // Notify script
1075  emit ShapeDragged();
1076  // Do not start dragging or selecting
1077  *border = -1;
1078  return false;
1079  }
1080  else if (!shift_down && !ctrl_down)
1081  {
1082  // Regular dragging of vertices
1083  if (IsVertexDrag(*border))
1084  {
1085  return C4ConsoleQtShape::StartDragging(border, x, y, shift_down, ctrl_down);
1086  }
1087  else
1088  {
1089  // No dragging of edges
1090  return false;
1091  }
1092  }
1093  else
1094  {
1095  // Unknown modifiers or trying to modify a structure-fixed graph
1096  return false;
1097  }
1098 }
1099 
1100 void C4ConsoleQtGraph::Drag(int32_t x, int32_t y, int32_t dx, int32_t dy, int32_t hit_range, Qt::CursorShape *drag_cursor)
1101 {
1102  // Dragging: Only vertices
1103  if (IsVertexDrag(dragging_border) && DragBorderToVertex(dragging_border) < graph.vertices.size())
1104  {
1105  int32_t dragged_vertex_index = DragBorderToVertex(dragging_border);
1106  Vertex &dragged_vertex = graph.vertices[dragged_vertex_index];
1107  // Regular vertex movement
1108  dx -= drag_snap_offset_x;
1109  dy -= drag_snap_offset_y;
1110  if (!horizontal_fix) dragged_vertex.x += dx;
1111  if (!vertical_fix) dragged_vertex.y += dy;
1112  // Handle snap to combine with other vertices
1113  if (!IsPolyline() && !structure_fix)
1114  {
1115  int32_t i = 0;
1116  drag_snap_vertex = -1;
1117  int32_t best_hit_range_sq = hit_range * hit_range * 4;
1118  for (Vertex &check_vertex : graph.vertices)
1119  {
1120  if (i != dragged_vertex_index && i != drag_source_vertex_index)
1121  {
1122  int32_t cdx = check_vertex.x - dragged_vertex.x - dx;
1123  int32_t cdy = check_vertex.y - dragged_vertex.y - dy;
1124  int32_t cdsq = cdx*cdx + cdy*cdy;
1125  if (cdsq <= best_hit_range_sq)
1126  {
1127  drag_snap_vertex = i;
1128  best_hit_range_sq = cdsq;
1129  }
1130  }
1131  ++i;
1132  }
1133  // Snapped? Move vertex to snap vertex then
1134  drag_snapped = (drag_snap_vertex >= 0);
1135  if (drag_snapped)
1136  {
1137  drag_snap_offset_x = graph.vertices[drag_snap_vertex].x - dragged_vertex.x;
1138  drag_snap_offset_y = graph.vertices[drag_snap_vertex].y - dragged_vertex.y;
1139  dragged_vertex.x += drag_snap_offset_x;
1140  dragged_vertex.y += drag_snap_offset_y;
1141  *drag_cursor = Qt::CursorShape::DragMoveCursor;
1142  }
1143  else
1144  {
1145  drag_snap_offset_x = drag_snap_offset_y = 0;
1146  *drag_cursor = Qt::CursorShape::SizeAllCursor;
1147  }
1148  }
1149  // Regular drag: Emit signal to update value
1150  EditGraph(true, C4ControlEditGraph::Action::CEG_SetVertexPos, dragged_vertex_index, dragged_vertex.x, dragged_vertex.y);
1151  }
1152 }
1153 
1154 void C4ConsoleQtGraph::StopDragging()
1155 {
1156  // Is it a vertex recombination?
1157  if (IsVertexDrag(dragging_border))
1158  {
1159  int32_t dragged_vertex = DragBorderToVertex(dragging_border);
1160  if (drag_snapped)
1161  {
1162  if (dragged_vertex && dragged_vertex != drag_snap_vertex)
1163  {
1164  // dragged_vertex is to be merged into drag_snap_vertex (keeping drag_snap_vertex)
1165  // find all edge targets already existing for drag_snap_vertex (this may include dragged_vertex)
1166  std::set<int32_t> vertices_connected_to_snap_vertex;
1167  int32_t idx;
1168  for (Edge &edge : graph.edges)
1169  {
1170  if (edge.connects_to(drag_snap_vertex, &idx))
1171  {
1172  vertices_connected_to_snap_vertex.insert(edge.vertex_indices[!idx]);
1173  }
1174  }
1175  // make sure that any connection from dragged_vertex to drag_snap_vertex will be excluded by the check
1176  vertices_connected_to_snap_vertex.insert(drag_snap_vertex);
1177  // move all connections that did not exist yet from dragged_vertex to drag_snap_vertex
1178  int32_t i_edge = 0;
1179  for (Edge &edge : graph.edges)
1180  {
1181  if (edge.connects_to(dragged_vertex, &idx))
1182  {
1183  if (!vertices_connected_to_snap_vertex.count(edge.vertex_indices[!idx]))
1184  {
1185  EditEdge(i_edge, idx, drag_snap_vertex);
1186  }
1187  }
1188  ++i_edge;
1189  }
1190  // ...and remove the dragged vertex. This will kill any remaining connections
1191  RemoveVertex(dragged_vertex, false);
1192  return;
1193  }
1194  }
1195  // Regular drag: Emit signal to update value
1196  EditGraph(true, C4ControlEditGraph::Action::CEG_SetVertexPos, dragged_vertex, graph.vertices[dragged_vertex].x, graph.vertices[dragged_vertex].y);
1197  }
1198  drag_snapped = false;
1199  // Reset drag
1200  C4ConsoleQtShape::StopDragging();
1201 }
1202 
1203 void C4ConsoleQtGraph::SetValue(const C4Value &val)
1204 {
1205  // Load from a proplist
1206  // Expected format for graph e.g.:
1207  // { Vertices = [{ X=123, Y=456, Color=0xff0000 }, { X=789, Y=753 }], Edges = [{ Vertices=[0, 1], Color=0xff00ff, LineWidth=2 }]
1208  C4PropList *valp = val.getPropList();
1209  if (valp)
1210  {
1211  graph.SetVerticesValue(valp->GetPropertyArray(P_Vertices));
1212  graph.SetEdgesValue(valp->GetPropertyArray(P_Edges));
1213  }
1214 }
1215 
1216 C4Value C4ConsoleQtGraph::GetValue() const
1217 {
1218  // Store graph as nested arrays / proplists
1219  C4ValueArray *vvertices = graph.GetVerticesValue();
1220  C4ValueArray *vedges = graph.GetEdgesValue();
1221  C4PropList *vmain = C4PropList::New();
1222  vmain->SetProperty(P_Vertices, C4VArray(vvertices));
1223  vmain->SetProperty(P_Edges, C4VArray(vedges));
1224  return C4VPropList(vmain);
1225 }
1226 
1227 void C4ConsoleQtGraph::EditEdge(int32_t edge_index, int32_t change_vertex_index, int32_t new_vertex)
1228 {
1229  EditGraph(true, C4ControlEditGraph::Action::CEG_EditEdge, edge_index, change_vertex_index, new_vertex);
1230 }
1231 
1232 int32_t C4ConsoleQtGraph::AddVertex(int32_t new_x, int32_t new_y)
1233 {
1234  EditGraph(true, C4ControlEditGraph::Action::CEG_InsertVertex, graph.vertices.size(), new_x, new_y);
1235  return graph.vertices.size() - 1;
1236 }
1237 
1238 int32_t C4ConsoleQtGraph::AddEdge(int32_t connect_vertex_index_1, int32_t connect_vertex_index_2)
1239 {
1240  EditGraph(true, C4ControlEditGraph::Action::CEG_InsertEdge, graph.edges.size(), connect_vertex_index_1, connect_vertex_index_2);
1241  return graph.edges.size() - 1;
1242 }
1243 
1244 int32_t C4ConsoleQtGraph::InsertVertexOnEdge(int32_t split_edge_index, int32_t x, int32_t y)
1245 {
1246  assert(split_edge_index >= 0 && split_edge_index < graph.edges.size());
1247  // Insert vertex by splitting an edge
1248  int32_t new_vertex_index = AddVertex(x, y);
1249  AddEdge(new_vertex_index, graph.edges[split_edge_index].vertex_indices[1]);
1250  EditEdge(split_edge_index, 1, new_vertex_index);
1251  // Return index of newly added vertex
1252  return new_vertex_index;
1253 }
1254 
1255 int32_t C4ConsoleQtGraph::InsertVertexOnVertex(int32_t target_vertex_index, int32_t x, int32_t y)
1256 {
1257  assert(target_vertex_index >= 0 && target_vertex_index < graph.vertices.size());
1258  // Insert vertex
1259  int32_t new_vertex_index = AddVertex(x, y);
1260  // Connect new vertex to target vertex
1261  AddEdge(target_vertex_index, new_vertex_index);
1262  // Return index of newly added vertex
1263  return new_vertex_index;
1264 }
1265 
1266 void C4ConsoleQtGraph::RemoveEdge(int32_t edge_index)
1267 {
1268  assert(edge_index >= 0 && edge_index < graph.edges.size());
1269  // Remove the edge
1270  Edge removed_edge = graph.edges[edge_index];
1271  EditGraph(true, C4ControlEditGraph::Action::CEG_RemoveEdge, edge_index, 0, 0);
1272  // Kill unconnected vertices
1273  if (!graph.GetEdgeCountForVertex(removed_edge.vertex_indices[0]))
1274  {
1275  RemoveVertex(removed_edge.vertex_indices[0], false);
1276  // Removing the first vertex will have shifted the index of the second vertex
1277  if (removed_edge.vertex_indices[1] > removed_edge.vertex_indices[0]) --removed_edge.vertex_indices[1];
1278  }
1279  if (!graph.GetEdgeCountForVertex(removed_edge.vertex_indices[1]))
1280  {
1281  // (RemoveVertex will always keep at least one vertex)
1282  RemoveVertex(removed_edge.vertex_indices[1], false);
1283  }
1284 }
1285 
1286 void C4ConsoleQtGraph::RemoveVertex(int32_t remove_vertex_index, bool create_skip_connection)
1287 {
1288  assert(remove_vertex_index >= 0 && remove_vertex_index < graph.vertices.size());
1289  // Create skip connection if the vertex had exactly two edges
1290  if (create_skip_connection && graph.GetEdgeCountForVertex(remove_vertex_index) == 2)
1291  {
1292  Edge *combine_edge = nullptr;
1293  int32_t i = 0;
1294  for (Edge &edge : graph.edges)
1295  {
1296  int32_t connect_idx;
1297  if (edge.connects_to(remove_vertex_index, &connect_idx))
1298  {
1299  if (!combine_edge)
1300  {
1301  combine_edge = &edge;
1302  }
1303  else
1304  {
1305  // Let edge bridge over the removed vertex
1306  int32_t v = combine_edge->vertex_indices[combine_edge->vertex_indices[0] == remove_vertex_index];
1307  EditEdge(i, connect_idx, v);
1308  // The removal check will remove the other edge (combine_edge)
1309  break;
1310  }
1311  }
1312  ++i;
1313  }
1314  }
1315  // Remove the actual vertex
1316  EditGraph(true, C4ControlEditGraph::Action::CEG_RemoveVertex, remove_vertex_index, 0, 0);
1317 }
1318 
1319 void C4ConsoleQtGraph::InsertVertexBefore(int32_t insert_vertex_index, int32_t x, int32_t y)
1320 {
1321  // Insert vertex at position in vertex list
1322  EditGraph(true, C4ControlEditGraph::Action::CEG_InsertVertex, insert_vertex_index, x, y);
1323 }
1324 
1325 void C4ConsoleQtGraph::InsertEdgeBefore(int32_t insert_edge_index, int32_t connect_vertex_index_1, int32_t connect_vertex_index_2)
1326 {
1327  // Insert vertex at position in vertex list
1328  EditGraph(true, C4ControlEditGraph::Action::CEG_InsertEdge, insert_edge_index, connect_vertex_index_1, connect_vertex_index_2);
1329 }
1330 
1331 bool C4ConsoleQtGraph::IsSelectionAllowed(int32_t border) const
1332 {
1333  // Independent selection settings for vertices and edges
1334  if (IsVertexDrag(border)) return allow_vertex_selection;
1335  if (IsEdgeDrag(border)) return allow_edge_selection;
1336  return false;
1337 }
1338 
1339 bool C4ConsoleQtGraph::GetSelectedData(const C4Value &shape_val, const class C4PropertyPath &shape_property_path, C4PropList **shape_item_editorprops, C4PropList **shape_item_value, C4String **shape_item_name, class C4PropertyPath *shape_item_target_path) const
1340 {
1341  // Selection may either be a vertex or an edge
1342  C4PropList *delegate = nullptr;
1343  C4ValueArray *vitems = nullptr;
1344  int32_t selected_item = -1;
1345  C4PropertyPath item_array_path;
1346  if (IsVertexDrag(selected_border))
1347  {
1348  selected_item = DragBorderToVertex(selected_border);
1349  delegate = vertex_delegate.getPropList();
1350  C4PropList *shape_val_proplist = shape_val.getPropList();
1351  if (!shape_val_proplist) return false;
1352  vitems = shape_val_proplist->GetPropertyArray(P_Vertices);
1353  item_array_path = C4PropertyPath(shape_property_path, ::Strings.P[P_Vertices].GetCStr());
1354  }
1355  else if (IsEdgeDrag(selected_border))
1356  {
1357  selected_item = DragBorderToEdge(selected_border);
1358  delegate = edge_delegate.getPropList();
1359  C4PropList *shape_val_proplist = shape_val.getPropList();
1360  if (!shape_val_proplist) return false;
1361  vitems = shape_val_proplist->GetPropertyArray(P_Edges);
1362  item_array_path = C4PropertyPath(shape_property_path, ::Strings.P[P_Edges].GetCStr());
1363  }
1364  if (!delegate) return false;
1365  // Get edge/vertex value
1366  if (!vitems || vitems->GetSize() <= selected_item) return false;
1367  *shape_item_value = vitems->GetItem(selected_item).getPropList();
1368  if (!*shape_item_value) return false;
1369  // Get edge/vertex path
1370  *shape_item_target_path = C4PropertyPath(item_array_path, selected_item);
1371  // Get delegate information
1372  *shape_item_name = delegate->GetPropertyStr(P_Name);
1373  *shape_item_editorprops = delegate->GetPropertyPropList(P_EditorProps);
1374  if (!*shape_item_editorprops) return false; // required
1375  return true;
1376 }
1377 
1378 void C4ConsoleQtGraph::EditGraph(bool signal_change, C4ControlEditGraph::Action action, int32_t index, int32_t x, int32_t y)
1379 {
1380  // Execute edit operation on graph
1381  // Validity checks are done by the individual operations
1382  switch (action)
1383  {
1384  case C4ControlEditGraph::Action::CEG_None:
1385  // Should never be sent
1386  assert(false);
1387  break;
1388  case C4ControlEditGraph::Action::CEG_SetVertexPos:
1389  graph.SetVertexPos(index, x, y);
1390  break;
1391  case C4ControlEditGraph::Action::CEG_EditEdge:
1392  graph.EditEdge(index, x, y);
1393  break;
1394  case C4ControlEditGraph::Action::CEG_InsertVertex:
1395  graph.InsertVertexBefore(index, x, y);
1396  break;
1397  case C4ControlEditGraph::Action::CEG_InsertEdge:
1398  graph.InsertEdgeBefore(index, x, y);
1399  break;
1400  case C4ControlEditGraph::Action::CEG_RemoveVertex:
1401  graph.RemoveVertex(index);
1402  break;
1403  case C4ControlEditGraph::Action::CEG_RemoveEdge:
1404  graph.RemoveEdge(index);
1405  break;
1406  }
1407  // Signal change to the owner of this graph
1408  // The owner should propagate the value through the control queue to edit the underlying script data and update any other clients
1409  if (signal_change)
1410  {
1411  emit GraphEdit(action, index, x, y);
1412  }
1413 }
1414 
1415 void C4ConsoleQtGraph::EditGraphValue(C4Value &val, C4ControlEditGraph::Action action, int32_t index, int32_t x, int32_t y)
1416 {
1417  // Execute action on C4Value representing graph
1418  // Get graph props: Can be either vertices as array (for polyline/polygon) or proplist with Vertices and Edges props (for general graphs)
1419  C4ValueArray *vvertices = nullptr, *vedges = nullptr;
1420  C4PropList *vval = val.getPropList();
1421  if (vval)
1422  {
1423  if (vval->IsFrozen()) return;
1424  vvertices = vval->GetPropertyArray(P_Vertices);
1425  vedges = vval->GetPropertyArray(P_Edges);
1426  if (!vvertices || !vedges || vvertices->IsFrozen() || vedges->IsFrozen()) return;
1427  }
1428  else
1429  {
1430  vvertices = val.getArray();
1431  if (!vvertices || vvertices->IsFrozen()) return;
1432  }
1433  // Validity checks are done by the individual operations
1434  switch (action)
1435  {
1436  case C4ControlEditGraph::Action::CEG_None:
1437  // Should never be sent
1438  assert(false);
1439  break;
1440  case C4ControlEditGraph::Action::CEG_SetVertexPos:
1441  {
1442  GraphData::EditGraphValue_SetVertexPos(vvertices, index, x, y);
1443  break;
1444  }
1445  case C4ControlEditGraph::Action::CEG_EditEdge:
1446  {
1447  if (vedges) // Ignore on polyline / polygon
1448  {
1449  GraphData::EditGraphValue_EditEdge(vvertices, vedges, index, x, y);
1450  }
1451  break;
1452  }
1453  case C4ControlEditGraph::Action::CEG_InsertVertex:
1454  {
1455  GraphData::EditGraphValue_InsertVertexBefore(vvertices, vedges, index, x, y);
1456  break;
1457  }
1458  case C4ControlEditGraph::Action::CEG_InsertEdge:
1459  {
1460  if (vedges) // Ignore on polyline / polygon
1461  {
1462  GraphData::EditGraphValue_InsertEdgeBefore(vvertices, vedges, index, x, y);
1463  }
1464  break;
1465  }
1466  case C4ControlEditGraph::Action::CEG_RemoveVertex:
1467  {
1468  GraphData::EditGraphValue_RemoveVertex(vvertices, vedges, index);
1469  break;
1470  }
1471  case C4ControlEditGraph::Action::CEG_RemoveEdge:
1472  {
1473  if (vedges) // Ignore on polyline / polygon
1474  {
1475  GraphData::EditGraphValue_RemoveEdge(vvertices, vedges, index);
1476  }
1477  break;
1478  }
1479  }
1480 }
1481 
1482 
1483 /* Open poly line */
1484 
1485 C4ConsoleQtPolyline::C4ConsoleQtPolyline(class C4Object *for_obj, C4PropList *props, const class C4PropertyDelegateShape *parent_delegate, class C4ConsoleQtShapes *shape_list)
1486  : C4ConsoleQtGraph(for_obj, props, parent_delegate, shape_list)
1487 {
1488  if (props)
1489  {
1490  start_from_object = props->GetPropertyBool(P_StartFromObject, start_from_object);
1491  }
1492 }
1493 
1494 void C4ConsoleQtPolyline::Draw(class C4TargetFacet &cgo, float line_width)
1495 {
1496  // Line from object center to first vertex
1497  if (start_from_object && graph.vertices.size())
1498  {
1499  C4ConsoleQtGraph::Vertex v0;
1500  if (!is_relative)
1501  {
1502  // In non-relative mode, the root coordinate needs to be pushed to the object manually
1503  C4Object *obj = rel_obj.getObj();
1504  if (obj)
1505  {
1506  v0.x += obj->GetX();
1507  v0.y += obj->GetY();
1508  }
1509  }
1510  DrawEdge(cgo, v0, graph.vertices[0], border_color, line_width, line_width, false);
1511  }
1512  // Remaining polyline is handled by regular graph drawing
1513  C4ConsoleQtGraph::Draw(cgo, line_width);
1514 }
1515 
1516 void C4ConsoleQtPolyline::SetValue(const C4Value &val)
1517 {
1518  // Set only vertices from value. Edges just connect all vertices.
1519  graph.SetVerticesValue(val.getArray());
1520  // Init edges directly to avoid unnecessery checks done by insert edge
1521  graph.edges.clear();
1522  if (graph.vertices.size() >= 2)
1523  {
1524  graph.edges.reserve(graph.vertices.size());
1525  for (int32_t i = 0; i < graph.vertices.size() - 1; ++i)
1526  {
1527  Edge edge;
1528  edge.vertex_indices[0] = i;
1529  edge.vertex_indices[1] = i + 1;
1530  graph.edges.push_back(edge);
1531  }
1532  }
1533 }
1534 
1535 C4Value C4ConsoleQtPolyline::GetValue() const
1536 {
1537  // Polyline: Only vertices; edges are implicit
1538  return C4VArray(graph.GetVerticesValue());
1539 }
1540 
1541 int32_t C4ConsoleQtPolyline::InsertVertexOnEdge(int32_t split_edge_index, int32_t x, int32_t y)
1542 {
1543  // Split an edge
1544  InsertVertexBefore(split_edge_index + 1, x, y);
1545  InsertEdgeBefore(split_edge_index, split_edge_index, split_edge_index + 1);
1546  EditEdge(split_edge_index + 1, 0, split_edge_index + 1);
1547  return split_edge_index + 1;
1548 }
1549 
1550 int32_t C4ConsoleQtPolyline::InsertVertexOnVertex(int32_t target_vertex_index, int32_t x, int32_t y)
1551 {
1552  // Only allowed on first or last index: Extends the poly line
1553  if (!target_vertex_index)
1554  {
1555  InsertVertexBefore(0, x, y);
1556  InsertEdgeBefore(0, 0, 1);
1557  return target_vertex_index;
1558  }
1559  else if (target_vertex_index == graph.vertices.size() - 1)
1560  {
1561  InsertVertexBefore(target_vertex_index + 1, x, y);
1562  InsertEdgeBefore(target_vertex_index, target_vertex_index, target_vertex_index + 1);
1563  return target_vertex_index + 1;
1564  }
1565  else
1566  {
1567  assert(false);
1568  return 0;
1569  }
1570 }
1571 
1572 void C4ConsoleQtPolyline::RemoveEdge(int32_t edge_index)
1573 {
1574  // Find larger remaining side and keep it
1575  int32_t before_vertices = edge_index + 1;
1576  int32_t after_vertices = graph.edges.size() - edge_index;
1577  if (before_vertices > after_vertices)
1578  {
1579  // Cut everything after removed edge
1580  for (int32_t i = 0; i < after_vertices; ++i)
1581  {
1582  RemoveVertex(graph.vertices.size()-1, false);
1583  }
1584  }
1585  else
1586  {
1587  // Cut everything before removed edge
1588  for (int32_t i = 0; i < before_vertices; ++i) // O(n^2)
1589  {
1590  RemoveVertex(0, false);
1591  }
1592  }
1593 }
1594 
1595 bool C4ConsoleQtPolyline::IsVertexHit(int32_t vertex_index, Qt::CursorShape *drag_cursor, bool shift_down, bool ctrl_down)
1596 {
1597  // Cannot add vertices from other vertices; only from edges
1598  // Except for end points where it would expand the poly line
1599  if (shift_down && !ctrl_down && vertex_index && vertex_index != graph.vertices.size()-1)
1600  {
1601  return false;
1602  }
1603  return C4ConsoleQtGraph::IsVertexHit(vertex_index, drag_cursor, shift_down, ctrl_down);
1604 }
1605 
1606 
1607 /* Closed polygon */
1608 
1609 void C4ConsoleQtPolygon::SetValue(const C4Value &val)
1610 {
1611  // Set open polyline vertices and edges
1612  C4ConsoleQtPolyline::SetValue(val);
1613  // Add closing edge
1614  if (graph.vertices.size() > 2)
1615  {
1616  Edge edge;
1617  edge.vertex_indices[0] = graph.vertices.size() - 1;
1618  edge.vertex_indices[1] = 0;
1619  graph.edges.push_back(edge);
1620  }
1621 }
1622 
1623 int32_t C4ConsoleQtPolygon::InsertVertexOnEdge(int32_t split_edge_index, int32_t x, int32_t y)
1624 {
1625  // Split the edge
1626  int32_t rval = C4ConsoleQtPolyline::InsertVertexOnEdge(split_edge_index, x, y);
1627  // Close the polygon if it just became a triangle
1628  if (graph.vertices.size() == 3)
1629  {
1630  AddEdge(2, 0);
1631  }
1632  return rval;
1633 }
1634 
1635 int32_t C4ConsoleQtPolygon::InsertVertexOnVertex(int32_t target_vertex_index, int32_t x, int32_t y)
1636 {
1637  // Never called because IsHit should return false
1638  assert(false);
1639  return 0;
1640 }
1641 
1642 void C4ConsoleQtPolygon::RemoveEdge(int32_t edge_index)
1643 {
1644  // Remove both connected vertices (unless it's the last one)
1645  Edge edge = graph.edges[edge_index];
1646  int32_t vertex_index = edge.vertex_indices[1];
1647  RemoveVertex(vertex_index, true);
1648  if (graph.vertices.size() > 1)
1649  {
1650  RemoveVertex(vertex_index ? edge.vertex_indices[0] : graph.vertices.size() - 1, true);
1651  }
1652 }
1653 
1654 bool C4ConsoleQtPolygon::IsVertexHit(int32_t vertex_index, Qt::CursorShape *drag_cursor, bool shift_down, bool ctrl_down)
1655 {
1656  // Cannot add vertices from other vertices; only from edges
1657  if (shift_down && !ctrl_down)
1658  {
1659  return false;
1660  }
1661  // Skip C4ConsoleQtPolyline::IsVertexHit; it doesn't do sensible extra checks
1662  return C4ConsoleQtGraph::IsVertexHit(vertex_index, drag_cursor, shift_down, ctrl_down);
1663 }
1664 
1665 
1666 /* Shape list */
1667 
1668 C4ConsoleQtShape *C4ConsoleQtShapes::CreateShape(class C4Object *for_obj, C4PropList *props, const C4Value &val, const class C4PropertyDelegateShape *parent_delegate)
1669 {
1670  C4String *type = props->GetPropertyStr(P_Type);
1671  if (!type) return nullptr;
1672  C4ConsoleQtShape *shape = nullptr;
1673  if (type->GetData() == "rect") shape = new C4ConsoleQtRect(for_obj, props, parent_delegate, this);
1674  else if (type->GetData() == "circle") shape = new C4ConsoleQtCircle(for_obj, props, parent_delegate, this);
1675  else if (type->GetData() == "point") shape = new C4ConsoleQtPoint(for_obj, props, parent_delegate, this);
1676  else if (type->GetData() == "graph") shape = new C4ConsoleQtGraph(for_obj, props, parent_delegate, this);
1677  else if (type->GetData() == "polyline") shape = new C4ConsoleQtPolyline(for_obj, props, parent_delegate, this);
1678  else if (type->GetData() == "polygon") shape = new C4ConsoleQtPolygon(for_obj, props, parent_delegate, this);
1679  shape->SetValue(val);
1680  return shape;
1681 }
1682 
1683 void C4ConsoleQtShapes::AddShape(C4ConsoleQtShape *shape)
1684 {
1685  if (shape) shapes.emplace_back(shape);
1686 }
1687 
1688 void C4ConsoleQtShapes::RemoveShape(C4ConsoleQtShape *shape)
1689 {
1690  // Remove from list and currently moving shape
1691  shapes.remove_if([shape](auto &it) { return it.get() == shape; });
1692  if (dragging_shape == shape) dragging_shape = nullptr;
1693  if (selected_shape == shape) selected_shape = nullptr;
1694 }
1695 
1696 void C4ConsoleQtShapes::ClearShapes()
1697 {
1698  shapes.clear();
1699  dragging_shape = selected_shape = nullptr;
1700  drag_cursor = Qt::CursorShape::ArrowCursor;
1701 }
1702 
1703 void C4ConsoleQtShapes::Draw(C4TargetFacet &cgo)
1704 {
1705  // Draw all shapes with at least 1px line width
1706  ZoomDataStackItem zdsi(cgo.X, cgo.Y, cgo.Zoom);
1707  float line_width = std::max<float>(1.0f, 1.0f / cgo.Zoom);
1708  for (auto &shape : shapes) shape->Draw(cgo, line_width);
1709 }
1710 
1711 bool C4ConsoleQtShapes::MouseDown(float x, float y, float hit_range, bool shift_down, bool ctrl_down)
1712 {
1713  // Check for shape hit and start dragging if a shape is in hit range
1714  int32_t hit_range_int = std::max(int32_t(hit_range + 0.5f), 1); // Using integer hit ranges for now
1715  // Ensure no leftover other shape
1716  if (dragging_shape) MouseUp(x, y, shift_down, ctrl_down);
1717  int32_t drag_border=-1;
1718  for (auto &shape : shapes)
1719  {
1720  if (shape->IsHit(x, y, hit_range_int, &drag_cursor, &drag_border, shift_down, ctrl_down))
1721  {
1722  dragging_shape = shape.get();
1723  if (dragging_shape->StartDragging(&drag_border, int32_t(x), int32_t(y), shift_down, ctrl_down))
1724  {
1725  drag_x = x;
1726  drag_y = y;
1727  }
1728  else
1729  {
1730  // No dragging (the click may have done something else with the shape)
1731  dragging_shape = nullptr;
1732  }
1733  // Selection (independent of dragging; but drag may have changed the border)
1734  if (drag_border != -1)
1735  {
1736  if (shape->IsSelectionAllowed(drag_border))
1737  {
1738  SetSelectedShape(&*shape, drag_border);
1739  }
1740  }
1741  return true;
1742  }
1743  }
1744  return false;
1745 }
1746 
1747 void C4ConsoleQtShapes::MouseMove(float x, float y, bool left_down, float hit_range, bool shift_down, bool ctrl_down)
1748 {
1749  // Check for shape hit and start dragging if a shape is in hit range
1750  int32_t hit_range_int = std::max(int32_t(hit_range + 0.5f), 1); // Using integer hit ranges for now
1751  // mouse down move: Execute shape dragging (full pixels only)
1752  if (dragging_shape && left_down)
1753  {
1754  int32_t dx = int32_t(round(x - drag_x)),
1755  dy = int32_t(round(y - drag_y));
1756  if (dx || dy)
1757  {
1758  drag_x += dx;
1759  drag_y += dy;
1760  dragging_shape->Drag(drag_x, drag_y, dx, dy, hit_range_int, &drag_cursor);
1761  }
1762  }
1763  else if (!left_down)
1764  {
1765  // Just moving around: Update cursor
1766  drag_cursor = Qt::CursorShape::ArrowCursor;
1767  int32_t ignored;
1768  for (auto &shape : shapes) if (shape->IsHit(x, y, hit_range_int, &drag_cursor, &ignored, shift_down, ctrl_down)) break;
1769  }
1770  else
1771  {
1772  // Regular move: Reset drag cursor
1773  drag_cursor = Qt::CursorShape::ArrowCursor;
1774  }
1775 }
1776 
1777 void C4ConsoleQtShapes::MouseUp(float x, float y, bool shift_down, bool ctrl_down)
1778 {
1779  // Stop dragging
1780  if (dragging_shape)
1781  {
1782  dragging_shape->StopDragging();
1783  dragging_shape = nullptr;
1784  drag_cursor = Qt::CursorShape::ArrowCursor;
1785  }
1786 }
1787 
1788 void C4ConsoleQtShapes::SetSelectedShape(C4ConsoleQtShape *new_selection, int32_t selected_border)
1789 {
1790  // Deselect old and select new
1791  if (selected_shape) selected_shape->ResetSelection();
1792  selected_shape = new_selection;
1793  if (selected_shape)
1794  {
1795  if (!selected_shape->Select(selected_border))
1796  {
1797  // Selection failure? Deselect if still selected.
1798  if (selected_shape == new_selection) selected_shape = nullptr;
1799  }
1800  }
1801 }
1802 
1803 bool C4ConsoleQtShapes::GetSelectedShapeData(const C4Value &shape_val, const class C4PropertyPath &shape_property_path, C4PropList **shape_item_editorprops, C4PropList **shape_item_value, C4String **shape_item_name, class C4PropertyPath *shape_item_target_path) const
1804 {
1805  if (!selected_shape) return false;
1806  return selected_shape->GetSelectedData(shape_val, shape_property_path, shape_item_editorprops, shape_item_value, shape_item_name, shape_item_target_path);
1807 }
1808 
1809 
1810 /* Shape pointer holder class */
1811 
1812 bool C4ConsoleQtShapeHolder::last_visit_flag = false;
1813 
1814 void C4ConsoleQtShapeHolder::Clear()
1815 {
1816  if (shape)
1817  {
1818  ::Console.EditCursor.GetShapes()->RemoveShape(shape);
1819  shape = nullptr;
1820  }
1821 }
1822 
1823 void C4ConsoleQtShapeHolder::Set(C4ConsoleQtShape *new_shape)
1824 {
1825  if (shape == new_shape) return;
1826  Clear();
1827  shape = new_shape;
1828  if (shape) ::Console.EditCursor.GetShapes()->AddShape(shape);
1829 }
C4EditCursor EditCursor
Definition: C4Console.h:90
int32_t GetY() const
Definition: C4Object.h:287
C4String P[P_LAST]
StdStrBuf GetData() const
Definition: C4StringTable.h:50
void SetItem(int32_t iElemNr, const C4Value &Value)
float Y
Definition: C4Facet.h:118
float Zoom
Definition: C4Facet.h:165
C4Console Console
Definition: C4Globals.cpp:45
bool GetProperty(C4PropertyName k, C4Value *pResult) const
Definition: C4PropList.h:101
const char * GetCStr() const
Definition: C4StringTable.h:49
C4Value C4VInt(int32_t i)
Definition: C4Value.h:242
void MouseMove(int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam, class C4Viewport *pVP)
Definition: C4Gui.h:2829
void SetProperty(C4PropertyName k, const C4Value &to)
Definition: C4PropList.h:120
void DrawLineDw(C4Surface *sfcTarget, float x1, float y1, float x2, float y2, DWORD dwClr, float width=1.0f)
Definition: C4Draw.cpp:614
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
int32_t GetPropertyBool(C4PropertyName n, bool default_val=false) const
Definition: C4PropList.cpp:872
void DrawCircleDw(C4Surface *sfcTarget, float cx, float cy, float r, DWORD dwClr, float width=1.0f)
Definition: C4Draw.cpp:624
C4StringTable Strings
Definition: C4Globals.cpp:42
C4Value C4VPropList(C4PropList *p)
Definition: C4Value.h:245
int32_t GetX() const
Definition: C4Object.h:286
void SetSize(int32_t inSize)
C4Draw * pDraw
Definition: C4Draw.cpp:42
C4ValueArray * getArray() const
Definition: C4Value.h:118
C4Value C4VArray(C4ValueArray *pArray)
Definition: C4Value.h:249
bool IsFrozen() const
Definition: C4PropList.h:131
int32_t GetSize() const
Definition: C4ValueArray.h:36
C4ValueArray * GetPropertyArray(C4PropertyName n) const
Definition: C4PropList.cpp:789
int32_t getInt() const
Definition: C4Value.h:112
bool HasProperty(C4String *k) const
Definition: C4PropList.h:118
float TargetX
Definition: C4Facet.h:165
bool IsFrozen() const
Definition: C4ValueArray.h:63
const BYTE CNAT_Top
Definition: C4Constants.h:111
const BYTE CNAT_Right
Definition: C4Constants.h:110
int32_t GetPropertyInt(C4PropertyName k, int32_t default_val=0) const
Definition: C4PropList.cpp:886
C4Surface * Surface
Definition: C4Facet.h:117
float TargetY
Definition: C4Facet.h:165
C4String * GetPropertyStr(C4PropertyName k) const
Definition: C4PropList.cpp:775
C4PropList * GetPropertyPropList(C4PropertyName k) const
Definition: C4PropList.cpp:900
float X
Definition: C4Facet.h:118
const BYTE CNAT_Left
Definition: C4Constants.h:109
const BYTE CNAT_Bottom
Definition: C4Constants.h:112
static C4PropList * New(C4PropList *prototype=nullptr)
Definition: C4PropList.cpp:64
C4PropList * getPropList() const
Definition: C4Value.h:116