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