[174] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[194] | 19 | #include <graph_displayer_canvas.h> |
---|
| 20 | #include <mapstorage.h> |
---|
| 21 | #include <nbtab.h> |
---|
[59] | 22 | #include <cmath> |
---|
[27] | 23 | |
---|
| 24 | |
---|
| 25 | bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event) |
---|
| 26 | { |
---|
| 27 | Gnome::Canvas::CanvasAA::on_expose_event(event); |
---|
| 28 | //usleep(10000); |
---|
| 29 | //rezoom(); |
---|
| 30 | return true; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | void GraphDisplayerCanvas::changeEditorialTool(int newtool) |
---|
| 34 | { |
---|
[34] | 35 | if(actual_tool!=newtool) |
---|
| 36 | { |
---|
[27] | 37 | |
---|
[34] | 38 | actual_handler.disconnect(); |
---|
[27] | 39 | |
---|
[34] | 40 | switch(actual_tool) |
---|
| 41 | { |
---|
| 42 | case CREATE_EDGE: |
---|
| 43 | { |
---|
| 44 | GdkEvent * generated=new GdkEvent(); |
---|
| 45 | generated->type=GDK_BUTTON_RELEASE; |
---|
| 46 | generated->button.button=3; |
---|
| 47 | createEdgeEventHandler(generated); |
---|
| 48 | break; |
---|
| 49 | } |
---|
[149] | 50 | case MAP_EDIT: |
---|
[35] | 51 | { |
---|
| 52 | break; |
---|
| 53 | } |
---|
[34] | 54 | default: |
---|
| 55 | break; |
---|
| 56 | } |
---|
[27] | 57 | |
---|
[34] | 58 | active_item=NULL; |
---|
| 59 | target_item=NULL; |
---|
| 60 | active_edge=INVALID; |
---|
| 61 | active_node=INVALID; |
---|
[33] | 62 | |
---|
[27] | 63 | |
---|
[34] | 64 | actual_tool=newtool; |
---|
| 65 | |
---|
| 66 | switch(newtool) |
---|
| 67 | { |
---|
| 68 | case MOVE: |
---|
| 69 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false); |
---|
| 70 | break; |
---|
[27] | 71 | |
---|
[34] | 72 | case CREATE_NODE: |
---|
| 73 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false); |
---|
| 74 | break; |
---|
[27] | 75 | |
---|
[34] | 76 | case CREATE_EDGE: |
---|
| 77 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false); |
---|
| 78 | break; |
---|
[27] | 79 | |
---|
[34] | 80 | case ERASER: |
---|
| 81 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false); |
---|
| 82 | break; |
---|
[32] | 83 | |
---|
[149] | 84 | case MAP_EDIT: |
---|
[34] | 85 | grab_focus(); |
---|
[149] | 86 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false); |
---|
[34] | 87 | break; |
---|
| 88 | |
---|
| 89 | default: |
---|
| 90 | break; |
---|
| 91 | } |
---|
[27] | 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
[30] | 95 | int GraphDisplayerCanvas::getActualTool() |
---|
[27] | 96 | { |
---|
| 97 | return actual_tool; |
---|
| 98 | } |
---|
| 99 | |
---|
[187] | 100 | bool GraphDisplayerCanvas::scrollEventHandler(GdkEvent* e) |
---|
| 101 | { |
---|
| 102 | bool handled=false; |
---|
| 103 | if(e->type==GDK_SCROLL) |
---|
| 104 | { |
---|
| 105 | |
---|
| 106 | //pointer shows this win point before zoom |
---|
| 107 | XY win_coord(((GdkEventScroll*)e)->x, ((GdkEventScroll*)e)->y); |
---|
| 108 | |
---|
| 109 | //the original scroll settings |
---|
| 110 | int scroll_offset_x, scroll_offset_y; |
---|
| 111 | get_scroll_offsets(scroll_offset_x, scroll_offset_y); |
---|
| 112 | |
---|
| 113 | //pointer shows this canvas point before zoom |
---|
| 114 | XY canvas_coord; |
---|
| 115 | window_to_world(win_coord.x, win_coord.y, canvas_coord.x, canvas_coord.y); |
---|
| 116 | |
---|
| 117 | if(((GdkEventScroll*)e)->direction) //IN |
---|
| 118 | { |
---|
| 119 | zoomIn(); |
---|
| 120 | } |
---|
| 121 | else |
---|
| 122 | { |
---|
| 123 | zoomOut(); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | //pointer shows this window point after zoom |
---|
| 127 | XY post_win_coord; |
---|
| 128 | world_to_window(canvas_coord.x, canvas_coord.y, post_win_coord.x, post_win_coord.y); |
---|
| 129 | |
---|
| 130 | //we have to add the difference between new and old window point to original scroll offset |
---|
| 131 | scroll_to(scroll_offset_x+(int)(post_win_coord.x-win_coord.x),scroll_offset_y+(int)(post_win_coord.y-win_coord.y)); |
---|
| 132 | |
---|
| 133 | //no other eventhandler is needed |
---|
| 134 | handled=true; |
---|
| 135 | } |
---|
| 136 | return handled; |
---|
| 137 | } |
---|
| 138 | |
---|
[30] | 139 | bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e) |
---|
[27] | 140 | { |
---|
[70] | 141 | static Gnome::Canvas::Text *coord_text = 0; |
---|
[27] | 142 | switch(e->type) |
---|
[160] | 143 | { |
---|
[27] | 144 | case GDK_BUTTON_PRESS: |
---|
| 145 | //we mark the location of the event to be able to calculate parameters of dragging |
---|
[31] | 146 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
[160] | 147 | |
---|
[31] | 148 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
[27] | 149 | active_node=INVALID; |
---|
[194] | 150 | for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i) |
---|
[27] | 151 | { |
---|
| 152 | if(nodesmap[i]==active_item) |
---|
| 153 | { |
---|
| 154 | active_node=i; |
---|
| 155 | } |
---|
| 156 | } |
---|
[148] | 157 | isbutton=e->button.button; |
---|
[27] | 158 | break; |
---|
| 159 | case GDK_BUTTON_RELEASE: |
---|
[70] | 160 | if (coord_text) |
---|
[160] | 161 | { |
---|
| 162 | delete coord_text; |
---|
| 163 | coord_text = 0; |
---|
| 164 | } |
---|
[27] | 165 | isbutton=0; |
---|
| 166 | active_item=NULL; |
---|
| 167 | active_node=INVALID; |
---|
| 168 | break; |
---|
| 169 | case GDK_MOTION_NOTIFY: |
---|
| 170 | //we only have to do sg. if the mouse button is pressed AND the click was on a node that was found in the set of nodes |
---|
| 171 | if(active_node!=INVALID) |
---|
[160] | 172 | { |
---|
[194] | 173 | (mytab.mapstorage)->modified = true; |
---|
[160] | 174 | |
---|
| 175 | //new coordinates will be the old values, |
---|
| 176 | //because the item will be moved to the |
---|
| 177 | //new coordinate therefore the new movement |
---|
| 178 | //has to be calculated from here |
---|
| 179 | |
---|
| 180 | double new_x, new_y; |
---|
| 181 | |
---|
| 182 | window_to_world (e->motion.x, e->motion.y, new_x, new_y); |
---|
| 183 | |
---|
| 184 | double dx=new_x-clicked_x; |
---|
| 185 | double dy=new_y-clicked_y; |
---|
| 186 | |
---|
| 187 | moveNode(dx, dy); |
---|
[70] | 188 | |
---|
[160] | 189 | clicked_x=new_x; |
---|
| 190 | clicked_y=new_y; |
---|
[27] | 191 | |
---|
[160] | 192 | // reposition the coordinates text |
---|
| 193 | std::ostringstream ostr; |
---|
| 194 | ostr << "(" << |
---|
[194] | 195 | (mytab.mapstorage)->coords[active_node].x << ", " << |
---|
| 196 | (mytab.mapstorage)->coords[active_node].y << ")"; |
---|
[160] | 197 | double radius = |
---|
| 198 | (nodesmap[active_node]->property_x2().get_value() - |
---|
| 199 | nodesmap[active_node]->property_x1().get_value()) / 2.0; |
---|
| 200 | if (coord_text) |
---|
| 201 | { |
---|
| 202 | coord_text->property_text().set_value(ostr.str()); |
---|
[194] | 203 | coord_text->property_x().set_value((mytab.mapstorage)->coords[active_node].x + |
---|
[160] | 204 | radius); |
---|
[194] | 205 | coord_text->property_y().set_value((mytab.mapstorage)->coords[active_node].y - |
---|
[160] | 206 | radius); |
---|
| 207 | } |
---|
| 208 | else |
---|
| 209 | { |
---|
| 210 | coord_text = new Gnome::Canvas::Text( |
---|
| 211 | displayed_graph, |
---|
[194] | 212 | (mytab.mapstorage)->coords[active_node].x + radius, |
---|
| 213 | (mytab.mapstorage)->coords[active_node].y - radius, |
---|
[160] | 214 | ostr.str()); |
---|
| 215 | coord_text->property_fill_color().set_value("black"); |
---|
| 216 | coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST); |
---|
| 217 | } |
---|
[31] | 218 | |
---|
| 219 | |
---|
[160] | 220 | } |
---|
| 221 | default: break; |
---|
| 222 | } |
---|
[27] | 223 | |
---|
[160] | 224 | return false; |
---|
[27] | 225 | } |
---|
| 226 | |
---|
[148] | 227 | XY GraphDisplayerCanvas::calcArrowPos(XY moved_node_1, XY moved_node_2, XY fix_node, XY old_arrow_pos, int move_code) |
---|
[98] | 228 | { |
---|
[148] | 229 | switch(move_code) |
---|
| 230 | { |
---|
| 231 | case 1: |
---|
| 232 | return XY((moved_node_2.x + fix_node.x) / 2.0, (moved_node_2.y + fix_node.y) / 2.0); |
---|
| 233 | break; |
---|
| 234 | case 2: |
---|
| 235 | return old_arrow_pos; |
---|
| 236 | break; |
---|
| 237 | case 3: |
---|
| 238 | { |
---|
| 239 | ////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 240 | /////////// keeps shape-with scalar multiplication - version 2. |
---|
| 241 | ////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
[98] | 242 | |
---|
[148] | 243 | //old vector from one to the other node - a |
---|
[150] | 244 | XY a_v(moved_node_1.x-fix_node.x,moved_node_1.y-fix_node.y); |
---|
[148] | 245 | //new vector from one to the other node - b |
---|
[150] | 246 | XY b_v(moved_node_2.x-fix_node.x,moved_node_2.y-fix_node.y); |
---|
[98] | 247 | |
---|
[148] | 248 | double absa=sqrt(a_v.normSquare()); |
---|
| 249 | double absb=sqrt(b_v.normSquare()); |
---|
[98] | 250 | |
---|
[148] | 251 | if ((absa == 0.0) || (absb == 0.0)) |
---|
| 252 | { |
---|
| 253 | return old_arrow_pos; |
---|
| 254 | } |
---|
| 255 | else |
---|
| 256 | { |
---|
| 257 | //old vector from one node to the breakpoint - c |
---|
[150] | 258 | XY c_v(old_arrow_pos.x-fix_node.x,old_arrow_pos.y-fix_node.y); |
---|
[148] | 259 | |
---|
| 260 | //unit vector with the same direction to a_v |
---|
[150] | 261 | XY a_v_u(a_v.x/absa,a_v.y/absa); |
---|
[148] | 262 | |
---|
| 263 | //normal vector of unit vector with the same direction to a_v |
---|
[150] | 264 | XY a_v_u_n(((-1)*a_v_u.y),a_v_u.x); |
---|
[148] | 265 | |
---|
| 266 | //unit vector with the same direction to b_v |
---|
[150] | 267 | XY b_v_u(b_v.x/absb,b_v.y/absb); |
---|
[148] | 268 | |
---|
| 269 | //normal vector of unit vector with the same direction to b_v |
---|
[150] | 270 | XY b_v_u_n(((-1)*b_v_u.y),b_v_u.x); |
---|
[148] | 271 | |
---|
| 272 | //vector c in a_v_u and a_v_u_n co-ordinate system |
---|
[150] | 273 | XY c_a(c_v*a_v_u,c_v*a_v_u_n); |
---|
[148] | 274 | |
---|
| 275 | //new vector from one node to the breakpoint - d - we have to calculate this one |
---|
[150] | 276 | XY d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n); |
---|
[148] | 277 | |
---|
| 278 | return XY(d_v.x+fix_node.x,d_v.y+fix_node.y); |
---|
| 279 | } |
---|
| 280 | break; |
---|
| 281 | } |
---|
| 282 | default: |
---|
| 283 | break; |
---|
[98] | 284 | } |
---|
[148] | 285 | } |
---|
[98] | 286 | |
---|
| 287 | |
---|
[30] | 288 | bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e) |
---|
[27] | 289 | { |
---|
| 290 | switch(e->type) |
---|
[63] | 291 | { |
---|
| 292 | //move the new node |
---|
| 293 | case GDK_MOTION_NOTIFY: |
---|
| 294 | { |
---|
| 295 | GdkEvent * generated=new GdkEvent(); |
---|
| 296 | generated->motion.x=e->motion.x; |
---|
| 297 | generated->motion.y=e->motion.y; |
---|
| 298 | generated->type=GDK_MOTION_NOTIFY; |
---|
| 299 | moveEventHandler(generated); |
---|
| 300 | break; |
---|
| 301 | } |
---|
[27] | 302 | |
---|
[63] | 303 | case GDK_BUTTON_RELEASE: |
---|
[194] | 304 | (mytab.mapstorage)->modified = true; |
---|
[53] | 305 | |
---|
[178] | 306 | is_drawn=true; |
---|
| 307 | |
---|
[27] | 308 | isbutton=1; |
---|
| 309 | |
---|
[194] | 310 | active_node=(mytab.mapstorage)->graph.addNode(); |
---|
[27] | 311 | |
---|
| 312 | //initiating values corresponding to new node in maps |
---|
| 313 | |
---|
| 314 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 315 | |
---|
[63] | 316 | // update coordinates |
---|
[194] | 317 | (mytab.mapstorage)->coords.set(active_node, XY(clicked_x, clicked_y)); |
---|
[63] | 318 | |
---|
| 319 | // update all other maps |
---|
| 320 | for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
---|
[194] | 321 | (mytab.mapstorage)->nodemap_storage.begin(); it != |
---|
| 322 | (mytab.mapstorage)->nodemap_storage.end(); ++it) |
---|
[63] | 323 | { |
---|
[64] | 324 | if ((it->first != "coordinates_x") && |
---|
| 325 | (it->first != "coordinates_y")) |
---|
[63] | 326 | { |
---|
| 327 | (*(it->second))[active_node] = |
---|
[194] | 328 | (mytab.mapstorage)->nodemap_default[it->first]; |
---|
[63] | 329 | } |
---|
| 330 | } |
---|
[64] | 331 | // increment the id map's default value |
---|
[194] | 332 | (mytab.mapstorage)->nodemap_default["label"] += 1.0; |
---|
[63] | 333 | |
---|
[53] | 334 | nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, |
---|
[63] | 335 | clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20); |
---|
[27] | 336 | active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]); |
---|
[63] | 337 | *(nodesmap[active_node]) << |
---|
| 338 | Gnome::Canvas::Properties::fill_color("blue"); |
---|
| 339 | *(nodesmap[active_node]) << |
---|
| 340 | Gnome::Canvas::Properties::outline_color("black"); |
---|
| 341 | active_item->raise_to_top(); |
---|
| 342 | |
---|
[27] | 343 | (nodesmap[active_node])->show(); |
---|
[28] | 344 | |
---|
[53] | 345 | nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph, |
---|
[63] | 346 | clicked_x+node_property_defaults[N_RADIUS]+5, |
---|
| 347 | clicked_y+node_property_defaults[N_RADIUS]+5, ""); |
---|
[28] | 348 | nodetextmap[active_node]->property_fill_color().set_value("darkblue"); |
---|
[63] | 349 | nodetextmap[active_node]->raise_to_top(); |
---|
[28] | 350 | |
---|
[94] | 351 | // mapwin.updateNode(active_node); |
---|
| 352 | propertyUpdate(active_node); |
---|
[28] | 353 | |
---|
[27] | 354 | isbutton=0; |
---|
[31] | 355 | target_item=NULL; |
---|
[27] | 356 | active_item=NULL; |
---|
| 357 | active_node=INVALID; |
---|
| 358 | break; |
---|
| 359 | default: |
---|
| 360 | break; |
---|
[63] | 361 | } |
---|
[27] | 362 | return false; |
---|
| 363 | } |
---|
| 364 | |
---|
[30] | 365 | bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e) |
---|
[27] | 366 | { |
---|
| 367 | switch(e->type) |
---|
[63] | 368 | { |
---|
[27] | 369 | case GDK_BUTTON_PRESS: |
---|
| 370 | //in edge creation right button has special meaning |
---|
| 371 | if(e->button.button!=3) |
---|
[63] | 372 | { |
---|
| 373 | //there is not yet selected node |
---|
| 374 | if(active_node==INVALID) |
---|
| 375 | { |
---|
| 376 | //we mark the location of the event to be able to calculate parameters of dragging |
---|
[31] | 377 | |
---|
[63] | 378 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
[31] | 379 | |
---|
[63] | 380 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
| 381 | active_node=INVALID; |
---|
[194] | 382 | for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i) |
---|
[63] | 383 | { |
---|
| 384 | if(nodesmap[i]==active_item) |
---|
| 385 | { |
---|
| 386 | active_node=i; |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | //the clicked item is really a node |
---|
| 390 | if(active_node!=INVALID) |
---|
| 391 | { |
---|
| 392 | *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); |
---|
| 393 | isbutton=1; |
---|
| 394 | } |
---|
| 395 | //clicked item was not a node. It could be e.g. edge. |
---|
| 396 | else |
---|
| 397 | { |
---|
| 398 | active_item=NULL; |
---|
| 399 | } |
---|
| 400 | } |
---|
| 401 | //we only have to do sg. if the mouse button |
---|
| 402 | // is pressed already once AND the click was |
---|
| 403 | // on a node that was found in the set of |
---|
| 404 | //nodes, and now we only search for the second |
---|
| 405 | //node |
---|
| 406 | else |
---|
| 407 | { |
---|
| 408 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 409 | target_item=(get_item_at(clicked_x, clicked_y)); |
---|
| 410 | Node target_node=INVALID; |
---|
[194] | 411 | for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i) |
---|
[63] | 412 | { |
---|
| 413 | if(nodesmap[i]==target_item) |
---|
| 414 | { |
---|
| 415 | target_node=i; |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | //the clicked item is a node, the edge can be drawn |
---|
| 419 | if(target_node!=INVALID) |
---|
| 420 | { |
---|
[194] | 421 | (mytab.mapstorage)->modified = true; |
---|
[151] | 422 | |
---|
| 423 | *(nodesmap[target_node]) << |
---|
| 424 | Gnome::Canvas::Properties::fill_color("red"); |
---|
| 425 | |
---|
| 426 | //creating new edge |
---|
[194] | 427 | active_edge=(mytab.mapstorage)->graph.addEdge(active_node, |
---|
[151] | 428 | target_node); |
---|
| 429 | |
---|
| 430 | // update maps |
---|
| 431 | for (std::map<std::string, |
---|
| 432 | Graph::EdgeMap<double>*>::const_iterator it = |
---|
[194] | 433 | (mytab.mapstorage)->edgemap_storage.begin(); it != |
---|
| 434 | (mytab.mapstorage)->edgemap_storage.end(); ++it) |
---|
[151] | 435 | { |
---|
| 436 | (*(it->second))[active_edge] = |
---|
[194] | 437 | (mytab.mapstorage)->edgemap_default[it->first]; |
---|
[151] | 438 | } |
---|
| 439 | // increment the id map's default value |
---|
[194] | 440 | (mytab.mapstorage)->edgemap_default["label"] += 1.0; |
---|
[151] | 441 | |
---|
[63] | 442 | if(target_node!=active_node) |
---|
| 443 | { |
---|
[113] | 444 | // set the coordinates of the arrow on the new edge |
---|
[194] | 445 | MapStorage& ms = *mytab.mapstorage; |
---|
[113] | 446 | ms.arrow_pos.set(active_edge, |
---|
| 447 | (ms.coords[ms.graph.source(active_edge)] + |
---|
| 448 | ms.coords[ms.graph.target(active_edge)])/ 2.0); |
---|
| 449 | |
---|
[63] | 450 | //drawing new edge |
---|
[98] | 451 | edgesmap[active_edge]=new BrokenEdge(displayed_graph, active_edge, |
---|
[63] | 452 | *this); |
---|
| 453 | } |
---|
| 454 | else |
---|
| 455 | { |
---|
[151] | 456 | // set the coordinates of the arrow on the new edge |
---|
[194] | 457 | MapStorage& ms = *mytab.mapstorage; |
---|
[151] | 458 | ms.arrow_pos.set(active_edge, |
---|
| 459 | (ms.coords[ms.graph.source(active_edge)] + |
---|
| 460 | XY(0.0, 80.0))); |
---|
| 461 | |
---|
| 462 | //drawing new edge |
---|
| 463 | edgesmap[active_edge]=new LoopEdge(displayed_graph, active_edge, |
---|
| 464 | *this); |
---|
[63] | 465 | } |
---|
[151] | 466 | |
---|
| 467 | //initializing edge-text as well, to empty string |
---|
[194] | 468 | XY text_pos=mytab.mapstorage->arrow_pos[active_edge]; |
---|
[151] | 469 | text_pos+=(XY(10,10)); |
---|
| 470 | |
---|
| 471 | edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, |
---|
| 472 | text_pos.x, text_pos.y, ""); |
---|
| 473 | edgetextmap[active_edge]->property_fill_color().set_value( |
---|
| 474 | "darkgreen"); |
---|
| 475 | edgetextmap[active_edge]->raise_to_top(); |
---|
| 476 | |
---|
| 477 | propertyUpdate(active_edge); |
---|
[63] | 478 | } |
---|
| 479 | //clicked item was not a node. it could be an e.g. edge. we do not |
---|
| 480 | //deal with it furthermore. |
---|
| 481 | else |
---|
| 482 | { |
---|
| 483 | target_item=NULL; |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | } |
---|
[27] | 487 | break; |
---|
| 488 | case GDK_BUTTON_RELEASE: |
---|
| 489 | isbutton=0; |
---|
| 490 | //we clear settings in two cases |
---|
| 491 | //1: the edge is ready (target_item has valid value) |
---|
| 492 | //2: the edge creation is cancelled with right button |
---|
| 493 | if((target_item)||(e->button.button==3)) |
---|
[63] | 494 | { |
---|
| 495 | if(active_item) |
---|
| 496 | { |
---|
[179] | 497 | propertyUpdate(active_node,N_COLOR); |
---|
[63] | 498 | active_item=NULL; |
---|
| 499 | } |
---|
| 500 | if(target_item) |
---|
| 501 | { |
---|
[194] | 502 | propertyUpdate((mytab.mapstorage)->graph.target(active_edge),N_COLOR); |
---|
[63] | 503 | target_item=NULL; |
---|
| 504 | } |
---|
| 505 | active_node=INVALID; |
---|
| 506 | active_edge=INVALID; |
---|
| 507 | } |
---|
[27] | 508 | break; |
---|
| 509 | default: |
---|
| 510 | break; |
---|
[63] | 511 | } |
---|
[27] | 512 | return false; |
---|
| 513 | } |
---|
| 514 | |
---|
[30] | 515 | bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e) |
---|
[27] | 516 | { |
---|
| 517 | switch(e->type) |
---|
| 518 | { |
---|
| 519 | case GDK_BUTTON_PRESS: |
---|
[43] | 520 | //finding the clicked items |
---|
[31] | 521 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 522 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
[27] | 523 | active_node=INVALID; |
---|
| 524 | active_edge=INVALID; |
---|
[43] | 525 | //was it a node? |
---|
[194] | 526 | for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i) |
---|
[27] | 527 | { |
---|
| 528 | if(nodesmap[i]==active_item) |
---|
| 529 | { |
---|
| 530 | active_node=i; |
---|
| 531 | } |
---|
| 532 | } |
---|
[43] | 533 | //or was it an edge? |
---|
[27] | 534 | if(active_node==INVALID) |
---|
| 535 | { |
---|
[194] | 536 | for (EdgeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i) |
---|
[27] | 537 | { |
---|
[153] | 538 | if(edgesmap[i]->getLine()==active_item) |
---|
[27] | 539 | { |
---|
| 540 | active_edge=i; |
---|
| 541 | } |
---|
| 542 | } |
---|
| 543 | } |
---|
[43] | 544 | |
---|
[129] | 545 | // return if the clicked object is neither an edge nor a node |
---|
| 546 | if (active_edge == INVALID) return false; |
---|
| 547 | |
---|
[43] | 548 | //recolor activated item |
---|
[31] | 549 | if(active_item) |
---|
| 550 | { |
---|
| 551 | *active_item << Gnome::Canvas::Properties::fill_color("red"); |
---|
| 552 | } |
---|
[27] | 553 | break; |
---|
| 554 | |
---|
| 555 | case GDK_BUTTON_RELEASE: |
---|
[31] | 556 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 557 | if(active_item) |
---|
[27] | 558 | { |
---|
[43] | 559 | //the cursor was not moved since pressing it |
---|
[31] | 560 | if( active_item == ( get_item_at (clicked_x, clicked_y) ) ) |
---|
[27] | 561 | { |
---|
[43] | 562 | //a node was found |
---|
[31] | 563 | if(active_node!=INVALID) |
---|
| 564 | { |
---|
[194] | 565 | (mytab.mapstorage)->modified = true; |
---|
[27] | 566 | |
---|
[31] | 567 | std::set<Graph::Edge> edges_to_delete; |
---|
[27] | 568 | |
---|
[194] | 569 | for(OutEdgeIt e((mytab.mapstorage)->graph,active_node);e!=INVALID;++e) |
---|
[31] | 570 | { |
---|
| 571 | edges_to_delete.insert(e); |
---|
| 572 | } |
---|
[69] | 573 | |
---|
[194] | 574 | for(InEdgeIt e((mytab.mapstorage)->graph,active_node);e!=INVALID;++e) |
---|
[31] | 575 | { |
---|
| 576 | edges_to_delete.insert(e); |
---|
| 577 | } |
---|
[69] | 578 | |
---|
[31] | 579 | //deleting collected edges |
---|
[69] | 580 | for(std::set<Graph::Edge>::iterator |
---|
| 581 | edge_set_it=edges_to_delete.begin(); |
---|
| 582 | edge_set_it!=edges_to_delete.end(); |
---|
| 583 | ++edge_set_it) |
---|
[31] | 584 | { |
---|
| 585 | deleteItem(*edge_set_it); |
---|
| 586 | } |
---|
| 587 | deleteItem(active_node); |
---|
| 588 | } |
---|
| 589 | //a simple edge was chosen |
---|
[129] | 590 | else if (active_edge != INVALID) |
---|
[27] | 591 | { |
---|
[31] | 592 | deleteItem(active_edge); |
---|
[27] | 593 | } |
---|
| 594 | } |
---|
[31] | 595 | //pointer was moved, deletion is cancelled |
---|
[27] | 596 | else |
---|
| 597 | { |
---|
[31] | 598 | if(active_node!=INVALID) |
---|
| 599 | { |
---|
| 600 | *active_item << Gnome::Canvas::Properties::fill_color("blue"); |
---|
| 601 | } |
---|
[129] | 602 | else if (active_edge != INVALID) |
---|
[31] | 603 | { |
---|
| 604 | *active_item << Gnome::Canvas::Properties::fill_color("green"); |
---|
| 605 | } |
---|
[27] | 606 | } |
---|
| 607 | } |
---|
| 608 | //reseting datas |
---|
| 609 | active_item=NULL; |
---|
| 610 | active_edge=INVALID; |
---|
| 611 | active_node=INVALID; |
---|
| 612 | break; |
---|
| 613 | |
---|
| 614 | case GDK_MOTION_NOTIFY: |
---|
| 615 | break; |
---|
| 616 | |
---|
| 617 | default: |
---|
| 618 | break; |
---|
| 619 | } |
---|
[31] | 620 | return false; |
---|
[27] | 621 | } |
---|
| 622 | |
---|
[149] | 623 | bool GraphDisplayerCanvas::mapEditEventHandler(GdkEvent* e) |
---|
[32] | 624 | { |
---|
[149] | 625 | if(actual_tool==MAP_EDIT) |
---|
[32] | 626 | { |
---|
[149] | 627 | switch(e->type) |
---|
| 628 | { |
---|
| 629 | case GDK_BUTTON_PRESS: |
---|
| 630 | { |
---|
| 631 | //for determine, whether it was an edge |
---|
| 632 | Edge clicked_edge=INVALID; |
---|
| 633 | //for determine, whether it was a node |
---|
| 634 | Node clicked_node=INVALID; |
---|
[43] | 635 | |
---|
[149] | 636 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
| 637 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
[48] | 638 | |
---|
[149] | 639 | //find the activated item between text of nodes |
---|
[194] | 640 | for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i) |
---|
[149] | 641 | { |
---|
| 642 | //at the same time only one can be active |
---|
| 643 | if(nodetextmap[i]==active_item) |
---|
| 644 | { |
---|
| 645 | clicked_node=i; |
---|
| 646 | } |
---|
| 647 | } |
---|
[65] | 648 | |
---|
[149] | 649 | //if there was not, search for it between nodes |
---|
| 650 | if(clicked_node==INVALID) |
---|
| 651 | { |
---|
[194] | 652 | for (NodeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i) |
---|
[149] | 653 | { |
---|
| 654 | //at the same time only one can be active |
---|
| 655 | if(nodesmap[i]==active_item) |
---|
| 656 | { |
---|
| 657 | clicked_node=i; |
---|
| 658 | } |
---|
| 659 | } |
---|
| 660 | } |
---|
[48] | 661 | |
---|
[149] | 662 | if(clicked_node==INVALID) |
---|
| 663 | { |
---|
| 664 | //find the activated item between texts |
---|
[194] | 665 | for (EdgeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i) |
---|
[149] | 666 | { |
---|
| 667 | //at the same time only one can be active |
---|
| 668 | if(edgetextmap[i]==active_item) |
---|
| 669 | { |
---|
| 670 | clicked_edge=i; |
---|
| 671 | } |
---|
| 672 | } |
---|
[48] | 673 | |
---|
[149] | 674 | //if it was not between texts, search for it between edges |
---|
| 675 | if(clicked_edge==INVALID) |
---|
| 676 | { |
---|
[194] | 677 | for (EdgeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i) |
---|
[149] | 678 | { |
---|
| 679 | //at the same time only one can be active |
---|
| 680 | if((edgesmap[i]->getLine())==active_item) |
---|
| 681 | { |
---|
| 682 | clicked_edge=i; |
---|
| 683 | } |
---|
| 684 | } |
---|
| 685 | } |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | //if it was really a node... |
---|
| 689 | if(clicked_node!=INVALID) |
---|
| 690 | { |
---|
| 691 | // the id map is not editable |
---|
| 692 | if (nodemap_to_edit == "label") return 0; |
---|
| 693 | |
---|
| 694 | //and there is activated map |
---|
| 695 | if(nodetextmap[clicked_node]->property_text().get_value()!="") |
---|
| 696 | { |
---|
| 697 | //activate the general variable for it |
---|
| 698 | active_node=clicked_node; |
---|
| 699 | |
---|
| 700 | //create a dialog |
---|
| 701 | Gtk::Dialog dialog("Edit value", true); |
---|
| 702 | dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); |
---|
| 703 | dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); |
---|
| 704 | Gtk::VBox* vbox = dialog.get_vbox(); |
---|
| 705 | Gtk::SpinButton spin(0.0, 4); |
---|
| 706 | spin.set_increments(1.0, 10.0); |
---|
| 707 | spin.set_range(-1000000.0, 1000000.0); |
---|
| 708 | spin.set_numeric(true); |
---|
| 709 | spin.set_value(atof(nodetextmap[active_node]->property_text().get_value().c_str())); |
---|
| 710 | vbox->add(spin); |
---|
| 711 | spin.show(); |
---|
| 712 | switch (dialog.run()) |
---|
| 713 | { |
---|
| 714 | case Gtk::RESPONSE_NONE: |
---|
| 715 | case Gtk::RESPONSE_CANCEL: |
---|
| 716 | break; |
---|
| 717 | case Gtk::RESPONSE_ACCEPT: |
---|
| 718 | double new_value = spin.get_value(); |
---|
[194] | 719 | (*(mytab.mapstorage)->nodemap_storage[nodemap_to_edit])[active_node] = |
---|
[149] | 720 | new_value; |
---|
| 721 | std::ostringstream ostr; |
---|
| 722 | ostr << new_value; |
---|
| 723 | nodetextmap[active_node]->property_text().set_value(ostr.str()); |
---|
| 724 | //mapwin.updateNode(active_node); |
---|
| 725 | //mapwin.updateNode(Node(INVALID)); |
---|
| 726 | propertyUpdate(Node(INVALID)); |
---|
| 727 | } |
---|
| 728 | } |
---|
| 729 | } |
---|
| 730 | else |
---|
| 731 | //if it was really an edge... |
---|
| 732 | if(clicked_edge!=INVALID) |
---|
| 733 | { |
---|
| 734 | // the id map is not editable |
---|
| 735 | if (edgemap_to_edit == "label") return 0; |
---|
| 736 | |
---|
| 737 | //and there is activated map |
---|
| 738 | if(edgetextmap[clicked_edge]->property_text().get_value()!="") |
---|
| 739 | { |
---|
| 740 | //activate the general variable for it |
---|
| 741 | active_edge=clicked_edge; |
---|
| 742 | |
---|
| 743 | //create a dialog |
---|
| 744 | Gtk::Dialog dialog("Edit value", true); |
---|
| 745 | dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); |
---|
| 746 | dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); |
---|
| 747 | Gtk::VBox* vbox = dialog.get_vbox(); |
---|
| 748 | Gtk::SpinButton spin(0.0, 4); |
---|
| 749 | spin.set_increments(1.0, 10.0); |
---|
| 750 | spin.set_range(-1000000.0, 1000000.0); |
---|
| 751 | spin.set_numeric(true); |
---|
| 752 | spin.set_value(atof(edgetextmap[active_edge]->property_text().get_value().c_str())); |
---|
| 753 | vbox->add(spin); |
---|
| 754 | spin.show(); |
---|
| 755 | switch (dialog.run()) |
---|
| 756 | { |
---|
| 757 | case Gtk::RESPONSE_NONE: |
---|
| 758 | case Gtk::RESPONSE_CANCEL: |
---|
| 759 | break; |
---|
| 760 | case Gtk::RESPONSE_ACCEPT: |
---|
| 761 | double new_value = spin.get_value(); |
---|
[194] | 762 | (*(mytab.mapstorage)->edgemap_storage[edgemap_to_edit])[active_edge] = |
---|
[149] | 763 | new_value; |
---|
| 764 | std::ostringstream ostr; |
---|
| 765 | ostr << new_value; |
---|
| 766 | edgetextmap[active_edge]->property_text().set_value( |
---|
| 767 | ostr.str()); |
---|
| 768 | //mapwin.updateEdge(active_edge); |
---|
| 769 | // mapwin.updateEdge(Edge(INVALID)); |
---|
| 770 | propertyUpdate(Edge(INVALID)); |
---|
| 771 | } |
---|
| 772 | } |
---|
| 773 | } |
---|
| 774 | break; |
---|
| 775 | } |
---|
| 776 | default: |
---|
| 777 | break; |
---|
| 778 | } |
---|
[32] | 779 | } |
---|
[35] | 780 | return false; |
---|
[32] | 781 | } |
---|
| 782 | |
---|
[62] | 783 | void GraphDisplayerCanvas::deleteItem(Node node_to_delete) |
---|
[27] | 784 | { |
---|
[28] | 785 | delete(nodetextmap[node_to_delete]); |
---|
[27] | 786 | delete(nodesmap[node_to_delete]); |
---|
[194] | 787 | (mytab.mapstorage)->graph.erase(node_to_delete); |
---|
[27] | 788 | } |
---|
| 789 | |
---|
[62] | 790 | void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete) |
---|
[27] | 791 | { |
---|
[28] | 792 | delete(edgetextmap[edge_to_delete]); |
---|
[27] | 793 | delete(edgesmap[edge_to_delete]); |
---|
[194] | 794 | (mytab.mapstorage)->graph.erase(edge_to_delete); |
---|
[27] | 795 | } |
---|
| 796 | |
---|
[150] | 797 | void GraphDisplayerCanvas::textReposition(XY new_place) |
---|
[27] | 798 | { |
---|
[150] | 799 | new_place+=(XY(10,10)); |
---|
[35] | 800 | edgetextmap[forming_edge]->property_x().set_value(new_place.x); |
---|
| 801 | edgetextmap[forming_edge]->property_y().set_value(new_place.y); |
---|
[27] | 802 | } |
---|
| 803 | |
---|
[147] | 804 | void GraphDisplayerCanvas::toggleEdgeActivity(EdgeBase* active_bre, bool on) |
---|
[27] | 805 | { |
---|
| 806 | if(on) |
---|
[147] | 807 | { |
---|
| 808 | if(forming_edge!=INVALID) |
---|
[27] | 809 | { |
---|
[147] | 810 | std::cerr << "ERROR!!!! Valid edge found!" << std::endl; |
---|
[27] | 811 | } |
---|
[147] | 812 | else |
---|
| 813 | { |
---|
[194] | 814 | for (EdgeIt i((mytab.mapstorage)->graph); i!=INVALID; ++i) |
---|
[147] | 815 | { |
---|
| 816 | if(edgesmap[i]==active_bre) |
---|
| 817 | { |
---|
| 818 | forming_edge=i; |
---|
| 819 | } |
---|
| 820 | } |
---|
| 821 | } |
---|
| 822 | } |
---|
[27] | 823 | else |
---|
| 824 | { |
---|
[160] | 825 | if(forming_edge!=INVALID) |
---|
| 826 | { |
---|
| 827 | forming_edge=INVALID; |
---|
| 828 | } |
---|
| 829 | else |
---|
| 830 | { |
---|
| 831 | std::cerr << "ERROR!!!! Invalid edge found!" << std::endl; |
---|
| 832 | } |
---|
[27] | 833 | } |
---|
[160] | 834 | } |
---|
| 835 | |
---|
| 836 | void GraphDisplayerCanvas::moveNode(double dx, double dy, Gnome::Canvas::Item * item, Node node) |
---|
| 837 | { |
---|
| 838 | Gnome::Canvas::Item * moved_item=item; |
---|
| 839 | Node moved_node=node; |
---|
| 840 | |
---|
| 841 | if(item==NULL && node==INVALID) |
---|
[147] | 842 | { |
---|
[160] | 843 | moved_item=active_item; |
---|
| 844 | moved_node=active_node; |
---|
[147] | 845 | } |
---|
[160] | 846 | else |
---|
| 847 | { |
---|
| 848 | isbutton=1; |
---|
| 849 | } |
---|
| 850 | |
---|
| 851 | //repositioning node and its text |
---|
| 852 | moved_item->move(dx, dy); |
---|
| 853 | nodetextmap[moved_node]->move(dx, dy); |
---|
| 854 | |
---|
| 855 | // the new coordinates of the centre of the node |
---|
[194] | 856 | double coord_x = dx + (mytab.mapstorage)->coords[moved_node].x; |
---|
| 857 | double coord_y = dy + (mytab.mapstorage)->coords[moved_node].y; |
---|
[160] | 858 | |
---|
| 859 | // write back the new coordinates to the coords map |
---|
[194] | 860 | (mytab.mapstorage)->coords.set(moved_node, XY(coord_x, coord_y)); |
---|
[160] | 861 | |
---|
| 862 | //all the edges connected to the moved point has to be redrawn |
---|
[194] | 863 | for(OutEdgeIt ei((mytab.mapstorage)->graph,moved_node);ei!=INVALID;++ei) |
---|
[160] | 864 | { |
---|
| 865 | XY arrow_pos; |
---|
| 866 | |
---|
[194] | 867 | if (mytab.mapstorage->graph.source(ei) == mytab.mapstorage->graph.target(ei)) |
---|
[160] | 868 | { |
---|
[194] | 869 | arrow_pos = mytab.mapstorage->arrow_pos[ei] + XY(dx, dy); |
---|
[160] | 870 | } |
---|
| 871 | else |
---|
| 872 | { |
---|
| 873 | XY moved_node_1(coord_x - dx, coord_y - dy); |
---|
| 874 | XY moved_node_2(coord_x, coord_y); |
---|
[194] | 875 | Node target = mytab.mapstorage->graph.target(ei); |
---|
| 876 | XY fix_node(mytab.mapstorage->coords[target].x, |
---|
| 877 | mytab.mapstorage->coords[target].y); |
---|
| 878 | XY old_arrow_pos(mytab.mapstorage->arrow_pos[ei]); |
---|
[160] | 879 | |
---|
| 880 | arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton); |
---|
| 881 | } |
---|
| 882 | |
---|
[194] | 883 | mytab.mapstorage->arrow_pos.set(ei, arrow_pos); |
---|
[160] | 884 | edgesmap[ei]->draw(); |
---|
| 885 | |
---|
| 886 | //reposition of edgetext |
---|
[194] | 887 | XY text_pos=mytab.mapstorage->arrow_pos[ei]; |
---|
[160] | 888 | text_pos+=(XY(10,10)); |
---|
| 889 | edgetextmap[ei]->property_x().set_value(text_pos.x); |
---|
| 890 | edgetextmap[ei]->property_y().set_value(text_pos.y); |
---|
| 891 | } |
---|
| 892 | |
---|
[194] | 893 | for(InEdgeIt ei((mytab.mapstorage)->graph,moved_node);ei!=INVALID;++ei) |
---|
[160] | 894 | { |
---|
[194] | 895 | if (mytab.mapstorage->graph.source(ei) != mytab.mapstorage->graph.target(ei)) |
---|
[160] | 896 | { |
---|
| 897 | XY moved_node_1(coord_x - dx, coord_y - dy); |
---|
| 898 | XY moved_node_2(coord_x, coord_y); |
---|
[194] | 899 | Node source = mytab.mapstorage->graph.source(ei); |
---|
| 900 | XY fix_node(mytab.mapstorage->coords[source].x, |
---|
| 901 | mytab.mapstorage->coords[source].y); |
---|
| 902 | XY old_arrow_pos(mytab.mapstorage->arrow_pos[ei]); |
---|
[160] | 903 | |
---|
| 904 | XY arrow_pos; |
---|
| 905 | arrow_pos = calcArrowPos(moved_node_1, moved_node_2, fix_node, old_arrow_pos, isbutton); |
---|
| 906 | |
---|
[194] | 907 | mytab.mapstorage->arrow_pos.set(ei, arrow_pos); |
---|
[160] | 908 | edgesmap[ei]->draw(); |
---|
| 909 | |
---|
| 910 | //reposition of edgetext |
---|
[194] | 911 | XY text_pos=mytab.mapstorage->arrow_pos[ei]; |
---|
[160] | 912 | text_pos+=(XY(10,10)); |
---|
| 913 | edgetextmap[ei]->property_x().set_value(text_pos.x); |
---|
| 914 | edgetextmap[ei]->property_y().set_value(text_pos.y); |
---|
| 915 | } |
---|
| 916 | } |
---|
[27] | 917 | } |
---|
[179] | 918 | |
---|
| 919 | Gdk::Color GraphDisplayerCanvas::rainbowColorCounter(double min, double max, double w) |
---|
| 920 | { |
---|
| 921 | Gdk::Color color; |
---|
| 922 | |
---|
| 923 | double pos=(w-min)/(max-min); |
---|
| 924 | int phase=0; |
---|
| 925 | |
---|
| 926 | //rainbow transitions contain 6 phase |
---|
| 927 | //in each phase only one color is changed |
---|
| 928 | //first we determine the phase, in which |
---|
| 929 | //the actual value belongs to |
---|
| 930 | for (int i=0;i<=5;i++) |
---|
| 931 | { |
---|
| 932 | if(((double)i/6<pos)&&(pos<=(double(i+1)/6))) |
---|
| 933 | { |
---|
| 934 | phase=i; |
---|
| 935 | } |
---|
| 936 | } |
---|
| 937 | if(phase<6) |
---|
| 938 | { |
---|
| 939 | //within its 1/6 long phase the relativ position |
---|
| 940 | //determines the power of the color changed in |
---|
| 941 | //that phase |
---|
| 942 | //we normalize that to one, to be able to give percentage |
---|
| 943 | //value for the function |
---|
| 944 | double rel_pos=(pos-(phase/6))*6; |
---|
| 945 | |
---|
| 946 | switch(phase) |
---|
| 947 | { |
---|
| 948 | case 0: |
---|
| 949 | color.set_rgb_p (1, 0, 1-rel_pos); |
---|
| 950 | break; |
---|
| 951 | case 1: |
---|
| 952 | color.set_rgb_p (1, rel_pos, 0); |
---|
| 953 | break; |
---|
| 954 | case 2: |
---|
| 955 | color.set_rgb_p (1-rel_pos, 1, 0); |
---|
| 956 | break; |
---|
| 957 | case 3: |
---|
| 958 | color.set_rgb_p (0, 1, rel_pos); |
---|
| 959 | break; |
---|
| 960 | case 4: |
---|
| 961 | color.set_rgb_p (0, 1-rel_pos, 1); |
---|
| 962 | break; |
---|
| 963 | case 5: |
---|
| 964 | color.set_rgb_p ((rel_pos/3), 0, 1); |
---|
| 965 | break; |
---|
| 966 | default: |
---|
| 967 | std::cout << "Wrong phase: " << phase << " " << pos << std::endl; |
---|
| 968 | } |
---|
| 969 | } |
---|
| 970 | else |
---|
| 971 | { |
---|
| 972 | std::cout << "Wrong phase: " << phase << " " << pos << std::endl; |
---|
| 973 | } |
---|
| 974 | return color; |
---|
| 975 | } |
---|