| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#include <limits.h> |
| 14 |
#include <stddef.h> |
| 15 |
#include <util/alloc.h> |
| 16 |
#include <util/list.h> |
| 17 |
#include "selectionfuncs.h" |
| 18 |
#include "topviewfuncs.h" |
| 19 |
#include "smyrna_utils.h" |
| 20 |
|
| 21 |
static void select_node(Agraph_t* g,Agnode_t* obj,int reverse) |
| 22 |
{ |
| 23 |
Agsym_t* sel_attr = GN_selected(g); |
| 24 |
|
| 25 |
if(!sel_attr) |
| 26 |
sel_attr = GN_selected(g) = agattr_text(g, AGNODE,"selected","0"); |
| 27 |
if(!reverse) |
| 28 |
{ |
| 29 |
agxset(obj,sel_attr,"1"); |
| 30 |
ND_selected(obj) = 1; |
| 31 |
} |
| 32 |
else |
| 33 |
{ |
| 34 |
if(ND_selected(obj)==1) |
| 35 |
{ |
| 36 |
agxset(obj,sel_attr,"0"); |
| 37 |
ND_selected(obj) = 0; |
| 38 |
ND_printLabel(obj) = 0; |
| 39 |
} |
| 40 |
else |
| 41 |
{ |
| 42 |
agxset(obj,sel_attr,"1"); |
| 43 |
ND_selected(obj) = 1; |
| 44 |
} |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
} |
| 50 |
static void select_edge(Agraph_t* g,Agedge_t* obj,int reverse) |
| 51 |
{ |
| 52 |
Agsym_t* sel_attr = GE_selected(g); |
| 53 |
|
| 54 |
if (!sel_attr) |
| 55 |
sel_attr = GE_selected(g) = agattr_text(g, AGEDGE,"selected","0"); |
| 56 |
if (!reverse) |
| 57 |
{ |
| 58 |
agxset(obj,sel_attr,"1"); |
| 59 |
ED_selected(obj) = 1; |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
if (ED_selected(obj) == 1) |
| 64 |
{ |
| 65 |
agxset(obj,sel_attr,"0"); |
| 66 |
ED_selected(obj) = 0; |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
agxset(obj,sel_attr,"1"); |
| 71 |
ED_selected(obj) = 1; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
static void pick_objects_in_rect(Agraph_t *g, float x1, float y1, float x2, |
| 79 |
float y2) { |
| 80 |
Agnode_t *v; |
| 81 |
Agedge_t *e; |
| 82 |
glCompPoint posT; |
| 83 |
glCompPoint posH; |
| 84 |
glCompPoint posN; |
| 85 |
|
| 86 |
for (v = agfstnode(g); v; v = agnxtnode(g, v)) |
| 87 |
{ |
| 88 |
if (view->Topview->sel.selectNodes) { |
| 89 |
posN = ND_A(v); |
| 90 |
if(!ND_visible(v)) |
| 91 |
continue; |
| 92 |
if(is_point_in_rectangle(posN.x,posN.y,x1,y1,x2-x1,y2-y1) ) |
| 93 |
select_node(g,v,0); |
| 94 |
} |
| 95 |
if (view->Topview->sel.selectEdges) { |
| 96 |
for (e = agfstout(g, v); e; e = agnxtout(g, e)) |
| 97 |
{ |
| 98 |
posT = ED_posTail(e); |
| 99 |
posH = ED_posHead(e); |
| 100 |
if(is_point_in_rectangle(posT.x,posT.y,x1,y1,x2-x1,y2-y1)) |
| 101 |
if(is_point_in_rectangle(posH.x,posH.y,x1,y1,x2-x1,y2-y1)) |
| 102 |
select_edge(g,e,0); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
static void* pick_object(Agraph_t* g,glCompPoint p) |
| 111 |
{ |
| 112 |
double dist = DBL_MAX; |
| 113 |
double nodeSize = 0; |
| 114 |
void *rv = NULL; |
| 115 |
|
| 116 |
const int defaultNodeShape = getAttrBool(g, g, "defaultnodeshape", 0); |
| 117 |
|
| 118 |
if(defaultNodeShape==0) |
| 119 |
nodeSize=GetOGLDistance(view->nodeScale*view->Topview->fitin_zoom/view->zoom); |
| 120 |
|
| 121 |
for (Agnode_t *v = agfstnode(g); v; v = agnxtnode(g, v)) { |
| 122 |
if(!ND_visible(v)) |
| 123 |
continue; |
| 124 |
const glCompPoint posN = ND_A(v); |
| 125 |
if(defaultNodeShape==1) |
| 126 |
{ |
| 127 |
nodeSize = ND_size(v); |
| 128 |
} |
| 129 |
|
| 130 |
// node distance to point |
| 131 |
const double nd = distBetweenPts(posN , p, nodeSize); |
| 132 |
if( nd < dist ) |
| 133 |
{ |
| 134 |
rv=v;dist=nd; |
| 135 |
} |
| 136 |
|
| 137 |
for (Agedge_t *e = agfstout(g, v); e; e = agnxtout(g, e)) { |
| 138 |
const glCompPoint posT = ED_posTail(e); |
| 139 |
const glCompPoint posH = ED_posHead(e); |
| 140 |
const double ed = point_to_lineseg_dist(p, posT, posH); |
| 141 |
if( ed < dist ) {rv=e;dist=ed;} |
| 142 |
} |
| 143 |
} |
| 144 |
return rv; |
| 145 |
} |
| 146 |
|
| 147 |
void pick_object_xyz(Agraph_t *g, topview *t, float x, float y, float z) { |
| 148 |
const glCompPoint p = {.x = x, .y = y, .z = z}; |
| 149 |
void *const a = pick_object(g, p); |
| 150 |
if (!a) |
| 151 |
return; |
| 152 |
if(agobjkind(a)==AGNODE) |
| 153 |
{ |
| 154 |
select_node(g,a,1); |
| 155 |
ND_printLabel(a) = 1; |
| 156 |
|
| 157 |
cacheSelectedNodes(g,t); |
| 158 |
|
| 159 |
} |
| 160 |
if(agobjkind(a)==AGEDGE) |
| 161 |
{ |
| 162 |
select_edge(g,a,1); |
| 163 |
cacheSelectedEdges(g,t); |
| 164 |
} |
| 165 |
} |
| 166 |
void pick_objects_rect(Agraph_t* g) |
| 167 |
{ |
| 168 |
float x1; |
| 169 |
float y1; |
| 170 |
float x2; |
| 171 |
float y2; |
| 172 |
if(view->mouse.GLfinalPos.x > view->mouse.GLinitPos.x) |
| 173 |
{ |
| 174 |
x1=view->mouse.GLinitPos.x; |
| 175 |
x2=view->mouse.GLfinalPos.x; |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
x2=view->mouse.GLinitPos.x; |
| 180 |
x1=view->mouse.GLfinalPos.x; |
| 181 |
|
| 182 |
} |
| 183 |
if(view->mouse.GLfinalPos.y > view->mouse.GLinitPos.y) |
| 184 |
{ |
| 185 |
y1=view->mouse.GLinitPos.y; |
| 186 |
y2=view->mouse.GLfinalPos.y; |
| 187 |
} |
| 188 |
else |
| 189 |
{ |
| 190 |
y2=view->mouse.GLinitPos.y; |
| 191 |
y1=view->mouse.GLfinalPos.y; |
| 192 |
} |
| 193 |
pick_objects_in_rect(g,x1,y1,x2,y2); |
| 194 |
cacheSelectedNodes(g,view->Topview); |
| 195 |
cacheSelectedEdges(g,view->Topview); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
void deselect_all(Agraph_t* g) |
| 201 |
{ |
| 202 |
Agnode_t *v; |
| 203 |
Agedge_t *e; |
| 204 |
Agsym_t* nsel_attr = GN_selected(g); |
| 205 |
Agsym_t* esel_attr = GE_selected(g); |
| 206 |
if(!nsel_attr) |
| 207 |
nsel_attr = GN_selected(g) = agattr_text(g, AGNODE,"selected","0"); |
| 208 |
if(!esel_attr) |
| 209 |
esel_attr = GE_selected(g) = agattr_text(g, AGEDGE,"selected","0"); |
| 210 |
for (v = agfstnode(g); v; v = agnxtnode(g, v)) |
| 211 |
{ |
| 212 |
agxset(v,nsel_attr,"0"); |
| 213 |
ND_selected(v) = 0; |
| 214 |
ND_printLabel(v) = 0; |
| 215 |
|
| 216 |
for (e = agfstout(g, v); e; e = agnxtout(g, e)) |
| 217 |
{ |
| 218 |
agxset(e,esel_attr,"0"); |
| 219 |
ED_selected(e) = 0; |
| 220 |
} |
| 221 |
} |
| 222 |
cacheSelectedNodes(g,view->Topview); |
| 223 |
cacheSelectedEdges(g,view->Topview); |
| 224 |
} |
| 225 |
|
| 226 |
static int close_poly(glCompPoly_t *selPoly, glCompPoint pt) { |
| 227 |
/* int i=0; */ |
| 228 |
const double EPS = GetOGLDistance(3.0); |
| 229 |
if (LIST_SIZE(selPoly) < 2) |
| 230 |
return 0; |
| 231 |
if (LIST_FRONT(selPoly)->x - pt.x < EPS && |
| 232 |
LIST_FRONT(selPoly)->y - pt.y < EPS) |
| 233 |
return 1; |
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
static void select_polygon(Agraph_t *g, glCompPoly_t *selPoly) { |
| 238 |
Agnode_t *v; |
| 239 |
glCompPoint posN; |
| 240 |
|
| 241 |
for (v = agfstnode(g); v; v = agnxtnode(g, v)) |
| 242 |
{ |
| 243 |
posN = ND_A(v); |
| 244 |
if(point_in_polygon(selPoly,posN)) |
| 245 |
select_node(g,v,0); |
| 246 |
} |
| 247 |
cacheSelectedNodes(g,view->Topview); |
| 248 |
} |
| 249 |
|
| 250 |
void add_selpoly(Agraph_t *g, glCompPoly_t *selPoly, glCompPoint pt) { |
| 251 |
if(!close_poly(selPoly,pt)) |
| 252 |
{ |
| 253 |
LIST_APPEND(selPoly, ((glCompPoint){.x = pt.x, .y = pt.y})); |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
select_polygon (g,selPoly); |
| 258 |
LIST_FREE(selPoly); |
| 259 |
} |
| 260 |
} |
| 261 |
|