Parent Directory | Revision Log
Prepared 64 bit id support
1 | harbaum | 1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. | ||
3 | * | ||
4 | * This file is part of OSM2Go. | ||
5 | * | ||
6 | * OSM2Go is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation, either version 3 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * OSM2Go is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with OSM2Go. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | ||
19 | |||
20 | #include "appdata.h" | ||
21 | |||
22 | #include <gdk/gdkkeysyms.h> | ||
23 | |||
24 | |||
25 | #undef DESTROY_WAIT_FOR_GTK | ||
26 | |||
27 | static void map_statusbar(map_t *map, map_item_t *map_item) { | ||
28 | char *item_str = NULL; | ||
29 | item_id_t id = ID_ILLEGAL; | ||
30 | tag_t *tag = NULL; | ||
31 | char *str = NULL; | ||
32 | |||
33 | harbaum | 154 | switch(map_item->object.type) { |
34 | case NODE: | ||
35 | harbaum | 1 | item_str = "Node"; |
36 | harbaum | 154 | id = map_item->object.node->id; |
37 | tag = map_item->object.node->tag; | ||
38 | harbaum | 1 | break; |
39 | |||
40 | harbaum | 154 | case WAY: |
41 | harbaum | 1 | item_str = "Way"; |
42 | harbaum | 154 | id = map_item->object.way->id; |
43 | tag = map_item->object.way->tag; | ||
44 | harbaum | 1 | break; |
45 | |||
46 | harbaum | 154 | case RELATION: |
47 | harbaum | 153 | item_str = "Relation"; |
48 | harbaum | 154 | id = map_item->object.relation->id; |
49 | tag = map_item->object.relation->tag; | ||
50 | harbaum | 153 | break; |
51 | |||
52 | harbaum | 1 | default: |
53 | break; | ||
54 | } | ||
55 | |||
56 | gboolean collision = FALSE; | ||
57 | tag_t *tags = tag; | ||
58 | |||
59 | if(id == ID_ILLEGAL) | ||
60 | str = g_strdup_printf(_("Unknown item")); | ||
61 | else { | ||
62 | harbaum | 161 | str = g_strdup_printf("%s #" ITEM_ID_FORMAT, item_str, id); |
63 | harbaum | 1 | |
64 | /* add some tags ... */ | ||
65 | achadwick | 28 | /* |
66 | * XXX Should we just try to present only the name or the ref (or the | ||
67 | * alt_name, old_name, whatever) here? Hurling a load of tags in the | ||
68 | * user's face in some unpredictable, uninformative order isn't very | ||
69 | * friendly. | ||
70 | * | ||
71 | * Actually, a tag_short_desc() function would be useful in dialogs | ||
72 | * nd user messages too. | ||
73 | */ | ||
74 | harbaum | 1 | while(tag) { |
75 | if(!collision && info_tag_key_collision(tags, tag)) | ||
76 | collision = TRUE; | ||
77 | |||
78 | /* we don't have much space, so ignore created_by tag */ | ||
79 | if(!osm_is_creator_tag(tag)) { | ||
80 | char *old = str; | ||
81 | str = g_strdup_printf("%s, %s=%s", old, tag->key, tag->value); | ||
82 | g_free(old); | ||
83 | } | ||
84 | tag = tag->next; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | statusbar_set(map->appdata, str, collision); | ||
89 | g_free(str); | ||
90 | } | ||
91 | |||
92 | void map_outside_error(appdata_t *appdata) { | ||
93 | errorf(GTK_WIDGET(appdata->window), | ||
94 | _("Items must not be placed outside the working area!")); | ||
95 | } | ||
96 | |||
97 | void map_item_chain_destroy(map_item_chain_t **chainP) { | ||
98 | if(!*chainP) { | ||
99 | printf("nothing to destroy!\n"); | ||
100 | return; | ||
101 | } | ||
102 | |||
103 | #ifdef DESTROY_WAIT_FOR_GTK | ||
104 | map_item_chain_t *chain = *chainP; | ||
105 | while(chain) { | ||
106 | map_item_chain_t *next = chain->next; | ||
107 | canvas_item_destroy(chain->map_item->item); | ||
108 | chain = next; | ||
109 | } | ||
110 | |||
111 | /* wait until gtks event handling has actually destroyed this item */ | ||
112 | printf("waiting for item destruction "); | ||
113 | while(gtk_events_pending() || *chainP) { | ||
114 | putchar('.'); | ||
115 | gtk_main_iteration(); | ||
116 | } | ||
117 | printf(" ok\n"); | ||
118 | |||
119 | /* the callback routine connected to this item should have been */ | ||
120 | /* called by now and it has set the chain to NULL */ | ||
121 | |||
122 | #else | ||
123 | map_item_chain_t *chain = *chainP; | ||
124 | while(chain) { | ||
125 | map_item_chain_t *next = chain->next; | ||
126 | canvas_item_destroy(chain->map_item->item); | ||
127 | |||
128 | g_free(chain); | ||
129 | chain = next; | ||
130 | } | ||
131 | *chainP = NULL; | ||
132 | #endif | ||
133 | } | ||
134 | |||
135 | static void map_node_select(appdata_t *appdata, node_t *node) { | ||
136 | map_t *map = appdata->map; | ||
137 | map_item_t *map_item = &map->selected; | ||
138 | |||
139 | g_assert(!map->highlight); | ||
140 | |||
141 | harbaum | 154 | map_item->object.type = NODE; |
142 | map_item->object.node = node; | ||
143 | harbaum | 1 | map_item->highlight = FALSE; |
144 | |||
145 | /* node may not have any visible representation at all */ | ||
146 | if(node->map_item_chain) | ||
147 | map_item->item = node->map_item_chain->map_item->item; | ||
148 | else | ||
149 | map_item->item = NULL; | ||
150 | |||
151 | map_statusbar(map, map_item); | ||
152 | icon_bar_map_item_selected(appdata, map_item, TRUE); | ||
153 | |||
154 | /* highlight node */ | ||
155 | harbaum | 154 | gint x = map_item->object.node->lpos.x, y = map_item->object.node->lpos.y; |
156 | harbaum | 1 | |
157 | /* create a copy of this map item and mark it as being a highlight */ | ||
158 | map_item_t *new_map_item = g_new0(map_item_t, 1); | ||
159 | memcpy(new_map_item, map_item, sizeof(map_item_t)); | ||
160 | new_map_item->highlight = TRUE; | ||
161 | |||
162 | float radius = map->style->highlight.width + map->style->node.radius; | ||
163 | if(!node->ways) radius += map->style->node.border_radius; | ||
164 | harbaum | 14 | if(node->icon_buf && map->style->icon.enable && |
165 | !appdata->settings->no_icons) { | ||
166 | harbaum | 154 | gint w = gdk_pixbuf_get_width(map_item->object.node->icon_buf); |
167 | gint h = gdk_pixbuf_get_height(map_item->object.node->icon_buf); | ||
168 | harbaum | 1 | /* icons are technically square, so a radius slightly bigger */ |
169 | /* than sqrt(2)*MAX(w,h) should fit nicely */ | ||
170 | radius = 0.75 * map->style->icon.scale * ((w>h)?w:h); | ||
171 | } | ||
172 | |||
173 | map_hl_circle_new(map, CANVAS_GROUP_NODES_HL, new_map_item, | ||
174 | x, y, radius, map->style->highlight.color); | ||
175 | harbaum | 103 | |
176 | harbaum | 1 | if(!map_item->item) { |
177 | /* and draw a fake node */ | ||
178 | new_map_item = g_new0(map_item_t, 1); | ||
179 | memcpy(new_map_item, map_item, sizeof(map_item_t)); | ||
180 | new_map_item->highlight = TRUE; | ||
181 | harbaum | 151 | map_hl_circle_new(map, CANVAS_GROUP_NODES_IHL, new_map_item, |
182 | harbaum | 1 | x, y, map->style->node.radius, |
183 | map->style->highlight.node_color); | ||
184 | } | ||
185 | } | ||
186 | |||
187 | void map_way_select(appdata_t *appdata, way_t *way) { | ||
188 | map_t *map = appdata->map; | ||
189 | map_item_t *map_item = &map->selected; | ||
190 | |||
191 | g_assert(!map->highlight); | ||
192 | |||
193 | harbaum | 154 | map_item->object.type = WAY; |
194 | map_item->object.way = way; | ||
195 | harbaum | 1 | map_item->highlight = FALSE; |
196 | map_item->item = way->map_item_chain->map_item->item; | ||
197 | |||
198 | map_statusbar(map, map_item); | ||
199 | icon_bar_map_item_selected(appdata, map_item, TRUE); | ||
200 | gtk_widget_set_sensitive(appdata->menu_item_map_hide_sel, TRUE); | ||
201 | |||
202 | harbaum | 154 | gint arrow_width = (map_item->object.way->draw.flags & OSM_DRAW_FLAG_BG)? |
203 | map->style->highlight.width + map_item->object.way->draw.bg.width/2: | ||
204 | map->style->highlight.width + map_item->object.way->draw.width/2; | ||
205 | harbaum | 1 | |
206 | harbaum | 154 | node_chain_t *node_chain = map_item->object.way->node_chain; |
207 | harbaum | 1 | node_t *last = NULL; |
208 | while(node_chain) { | ||
209 | map_item_t item; | ||
210 | harbaum | 154 | item.object.type = NODE; |
211 | item.object.node = node_chain->node; | ||
212 | harbaum | 1 | |
213 | /* draw an arrow between every two nodes */ | ||
214 | if(last) { | ||
215 | /* create a new map item for every arrow */ | ||
216 | map_item_t *new_map_item = g_new0(map_item_t, 1); | ||
217 | harbaum | 154 | new_map_item->object.type = WAY; |
218 | new_map_item->object.way = way; | ||
219 | harbaum | 1 | new_map_item->highlight = TRUE; |
220 | |||
221 | struct { float x, y;} center, diff; | ||
222 | center.x = (last->lpos.x + node_chain->node->lpos.x)/2; | ||
223 | center.y = (last->lpos.y + node_chain->node->lpos.y)/2; | ||
224 | diff.x = node_chain->node->lpos.x - last->lpos.x; | ||
225 | diff.y = node_chain->node->lpos.y - last->lpos.y; | ||
226 | |||
227 | harbaum | 15 | /* only draw arrow if there's sufficient space */ |
228 | /* TODO: what if there's not enough space anywhere? */ | ||
229 | harbaum | 1 | float len = sqrt(pow(diff.x, 2)+pow(diff.y, 2)); |
230 | if(len > map->style->highlight.arrow_limit*arrow_width) { | ||
231 | len /= arrow_width; | ||
232 | diff.x = diff.x / len; | ||
233 | diff.y = diff.y / len; | ||
234 | |||
235 | canvas_points_t *points = canvas_points_new(4); | ||
236 | points->coords[2*0+0] = points->coords[2*3+0] = center.x + diff.x; | ||
237 | points->coords[2*0+1] = points->coords[2*3+1] = center.y + diff.y; | ||
238 | points->coords[2*1+0] = center.x + diff.y - diff.x; | ||
239 | points->coords[2*1+1] = center.y - diff.x - diff.y; | ||
240 | points->coords[2*2+0] = center.x - diff.y - diff.x; | ||
241 | points->coords[2*2+1] = center.y + diff.x - diff.y; | ||
242 | |||
243 | harbaum | 105 | map_hl_polygon_new(map, CANVAS_GROUP_WAYS_DIR, new_map_item, |
244 | harbaum | 1 | points, map->style->highlight.arrow_color); |
245 | |||
246 | canvas_points_free(points); | ||
247 | } | ||
248 | } | ||
249 | |||
250 | if(!map_hl_item_is_highlighted(map, &item)) { | ||
251 | |||
252 | /* create a new map item for every node */ | ||
253 | map_item_t *new_map_item = g_new0(map_item_t, 1); | ||
254 | harbaum | 154 | new_map_item->object.type = NODE; |
255 | new_map_item->object.node = node_chain->node; | ||
256 | harbaum | 1 | new_map_item->highlight = TRUE; |
257 | |||
258 | gint x = node_chain->node->lpos.x; | ||
259 | gint y = node_chain->node->lpos.y; | ||
260 | |||
261 | harbaum | 151 | map_hl_circle_new(map, CANVAS_GROUP_NODES_IHL, new_map_item, |
262 | harbaum | 1 | x, y, map->style->node.radius, |
263 | map->style->highlight.node_color); | ||
264 | } | ||
265 | |||
266 | last = node_chain->node; | ||
267 | node_chain = node_chain->next; | ||
268 | } | ||
269 | |||
270 | /* a way needs at least 2 points to be drawn */ | ||
271 | guint nodes = osm_way_number_of_nodes(way); | ||
272 | if(nodes > 1) { | ||
273 | |||
274 | /* allocate space for nodes */ | ||
275 | canvas_points_t *points = canvas_points_new(nodes); | ||
276 | |||
277 | int node = 0; | ||
278 | harbaum | 154 | node_chain = map_item->object.way->node_chain; |
279 | harbaum | 1 | while(node_chain) { |
280 | canvas_point_set_pos(points, node++, &node_chain->node->lpos); | ||
281 | node_chain = node_chain->next; | ||
282 | } | ||
283 | |||
284 | /* create a copy of this map item and mark it as being a highlight */ | ||
285 | map_item_t *new_map_item = g_new0(map_item_t, 1); | ||
286 | memcpy(new_map_item, map_item, sizeof(map_item_t)); | ||
287 | new_map_item->highlight = TRUE; | ||
288 | |||
289 | map_hl_polyline_new(map, CANVAS_GROUP_WAYS_HL, new_map_item, points, | ||
290 | harbaum | 154 | (map_item->object.way->draw.flags & OSM_DRAW_FLAG_BG)? |
291 | 2*map->style->highlight.width + map_item->object.way->draw.bg.width: | ||
292 | 2*map->style->highlight.width + map_item->object.way->draw.width, | ||
293 | harbaum | 1 | map->style->highlight.color); |
294 | |||
295 | canvas_points_free(points); | ||
296 | } | ||
297 | } | ||
298 | |||
299 | harbaum | 153 | void map_relation_select(appdata_t *appdata, relation_t *relation) { |
300 | map_t *map = appdata->map; | ||
301 | |||
302 | harbaum | 161 | printf("highlighting relation "ITEM_ID_FORMAT"\n", relation->id); |
303 | harbaum | 153 | |
304 | g_assert(!map->highlight); | ||
305 | map_highlight_t **hl = &map->highlight; | ||
306 | |||
307 | map_item_t *map_item = &map->selected; | ||
308 | harbaum | 154 | map_item->object.type = RELATION; |
309 | map_item->object.relation = relation; | ||
310 | harbaum | 153 | map_item->highlight = FALSE; |
311 | map_item->item = NULL; | ||
312 | |||
313 | map_statusbar(map, map_item); | ||
314 | icon_bar_map_item_selected(appdata, map_item, TRUE); | ||
315 | |||
316 | /* process all members */ | ||
317 | member_t *member = relation->member; | ||
318 | while(member) { | ||
319 | canvas_item_t *item = NULL; | ||
320 | |||
321 | harbaum | 155 | switch(member->object.type) { |
322 | harbaum | 153 | |
323 | case NODE: { | ||
324 | harbaum | 155 | node_t *node = member->object.node; |
325 | harbaum | 161 | printf(" -> node "ITEM_ID_FORMAT"\n", node->id); |
326 | harbaum | 153 | |
327 | item = canvas_circle_new(map->canvas, CANVAS_GROUP_NODES_HL, | ||
328 | node->lpos.x, node->lpos.y, | ||
329 | map->style->highlight.width + map->style->node.radius, | ||
330 | 0, map->style->highlight.color, NO_COLOR); | ||
331 | } break; | ||
332 | |||
333 | case WAY: { | ||
334 | harbaum | 155 | way_t *way = member->object.way; |
335 | harbaum | 153 | /* a way needs at least 2 points to be drawn */ |
336 | guint nodes = osm_way_number_of_nodes(way); | ||
337 | if(nodes > 1) { | ||
338 | |||
339 | /* allocate space for nodes */ | ||
340 | canvas_points_t *points = canvas_points_new(nodes); | ||
341 | |||
342 | int node = 0; | ||
343 | node_chain_t *node_chain = way->node_chain; | ||
344 | while(node_chain) { | ||
345 | canvas_point_set_pos(points, node++, &node_chain->node->lpos); | ||
346 | node_chain = node_chain->next; | ||
347 | } | ||
348 | |||
349 | if(way->draw.flags & OSM_DRAW_FLAG_AREA) | ||
350 | item = canvas_polygon_new(map->canvas, CANVAS_GROUP_WAYS_HL, points, 0, 0, | ||
351 | map->style->highlight.color); | ||
352 | else | ||
353 | item = canvas_polyline_new(map->canvas, CANVAS_GROUP_WAYS_HL, points, | ||
354 | (way->draw.flags & OSM_DRAW_FLAG_BG)? | ||
355 | 2*map->style->highlight.width + way->draw.bg.width: | ||
356 | 2*map->style->highlight.width + way->draw.width, | ||
357 | map->style->highlight.color); | ||
358 | |||
359 | canvas_points_free(points); | ||
360 | } } break; | ||
361 | |||
362 | default: | ||
363 | break; | ||
364 | } | ||
365 | |||
366 | /* attach item to item chain */ | ||
367 | if(item) { | ||
368 | *hl = g_new0(map_highlight_t, 1); | ||
369 | (*hl)->item = item; | ||
370 | hl = &(*hl)->next; | ||
371 | } | ||
372 | |||
373 | member = member->next; | ||
374 | } | ||
375 | } | ||
376 | |||
377 | harbaum | 154 | static void map_object_select(appdata_t *appdata, object_t *object) { |
378 | switch(object->type) { | ||
379 | case NODE: | ||
380 | map_node_select(appdata, object->node); | ||
381 | harbaum | 1 | break; |
382 | harbaum | 154 | case WAY: |
383 | map_way_select(appdata, object->way); | ||
384 | harbaum | 1 | break; |
385 | harbaum | 154 | case RELATION: |
386 | map_relation_select(appdata, object->relation); | ||
387 | break; | ||
388 | harbaum | 1 | default: |
389 | harbaum | 154 | g_assert((object->type == NODE)||(object->type == RELATION)|| |
390 | (object->type == WAY)); | ||
391 | harbaum | 1 | break; |
392 | } | ||
393 | } | ||
394 | |||
395 | void map_item_deselect(appdata_t *appdata) { | ||
396 | |||
397 | /* save tags for "last" function in info dialog */ | ||
398 | harbaum | 154 | if(appdata->map->selected.object.type == NODE) { |
399 | harbaum | 1 | if(appdata->map->last_node_tags) |
400 | osm_tags_free(appdata->map->last_node_tags); | ||
401 | |||
402 | appdata->map->last_node_tags = | ||
403 | harbaum | 154 | osm_tags_copy(appdata->map->selected.object.node->tag, FALSE); |
404 | } else if(appdata->map->selected.object.type == WAY) { | ||
405 | harbaum | 1 | if(appdata->map->last_way_tags) |
406 | osm_tags_free(appdata->map->last_way_tags); | ||
407 | |||
408 | appdata->map->last_way_tags = | ||
409 | harbaum | 154 | osm_tags_copy(appdata->map->selected.object.way->tag, FALSE); |
410 | harbaum | 1 | } |
411 | |||
412 | /* remove statusbar message */ | ||
413 | statusbar_set(appdata, NULL, FALSE); | ||
414 | |||
415 | /* disable/enable icons in icon bar */ | ||
416 | icon_bar_map_item_selected(appdata, NULL, FALSE); | ||
417 | gtk_widget_set_sensitive(appdata->menu_item_map_hide_sel, FALSE); | ||
418 | |||
419 | /* remove highlight */ | ||
420 | map_hl_remove(appdata); | ||
421 | |||
422 | /* forget about selection */ | ||
423 | harbaum | 154 | appdata->map->selected.object.type = ILLEGAL; |
424 | harbaum | 1 | } |
425 | |||
426 | /* called whenever a map item is to be destroyed */ | ||
427 | static gint map_item_destroy_event(GtkWidget *widget, gpointer data) { | ||
428 | map_item_t *map_item = (map_item_t*)data; | ||
429 | |||
430 | // printf("destroying map_item @ %p\n", map_item); | ||
431 | |||
432 | #ifdef DESTROY_WAIT_FOR_GTK | ||
433 | /* remove item from nodes/ways map_item_chain */ | ||
434 | map_item_chain_t **chain = NULL; | ||
435 | harbaum | 154 | if(map_item->object.type == NODE) |
436 | chain = &map_item->object.node->map_item_chain; | ||
437 | else if(map_item->object.type == WAY) | ||
438 | chain = &map_item->object.way->map_item_chain; | ||
439 | harbaum | 1 | |
440 | /* there must be a chain with content, otherwise things are broken */ | ||
441 | g_assert(chain); | ||
442 | g_assert(*chain); | ||
443 | |||
444 | /* search current map_item, ... */ | ||
445 | while(*chain && (*chain)->map_item != map_item) | ||
446 | chain = &(*chain)->next; | ||
447 | |||
448 | g_assert(*chain); | ||
449 | |||
450 | /* ... remove it from chain and free it */ | ||
451 | map_item_chain_t *tmp = *chain; | ||
452 | *chain = (*chain)->next; | ||
453 | |||
454 | g_free(tmp); | ||
455 | #endif | ||
456 | |||
457 | g_free(map_item); | ||
458 | return FALSE; | ||
459 | } | ||
460 | |||
461 | static canvas_item_t *map_node_new(map_t *map, node_t *node, gint radius, | ||
462 | gint width, canvas_color_t fill, canvas_color_t border) { | ||
463 | |||
464 | map_item_t *map_item = g_new0(map_item_t, 1); | ||
465 | harbaum | 154 | map_item->object.type = NODE; |
466 | map_item->object.node = node; | ||
467 | harbaum | 1 | |
468 | harbaum | 14 | if(!node->icon_buf || !map->style->icon.enable || |
469 | harbaum | 64 | map->appdata->settings->no_icons) |
470 | harbaum | 103 | map_item->item = canvas_circle_new(map->canvas, CANVAS_GROUP_NODES, |
471 | harbaum | 1 | node->lpos.x, node->lpos.y, radius, width, fill, border); |
472 | else | ||
473 | harbaum | 103 | map_item->item = canvas_image_new(map->canvas, CANVAS_GROUP_NODES, |
474 | harbaum | 1 | node->icon_buf, |
475 | node->lpos.x - map->style->icon.scale/2 * | ||
476 | gdk_pixbuf_get_width(node->icon_buf), | ||
477 | node->lpos.y - map->style->icon.scale/2 * | ||
478 | gdk_pixbuf_get_height(node->icon_buf), | ||
479 | map->style->icon.scale,map->style->icon.scale); | ||
480 | |||
481 | canvas_item_set_zoom_max(map_item->item, node->zoom_max); | ||
482 | |||
483 | /* attach map_item to nodes map_item_chain */ | ||
484 | map_item_chain_t **chain = &node->map_item_chain; | ||
485 | while(*chain) chain = &(*chain)->next; | ||
486 | *chain = g_new0(map_item_chain_t, 1); | ||
487 | (*chain)->map_item = map_item; | ||
488 | |||
489 | canvas_item_set_user_data(map_item->item, map_item); | ||
490 | |||
491 | canvas_item_destroy_connect(map_item->item, | ||
492 | G_CALLBACK(map_item_destroy_event), map_item); | ||
493 | |||
494 | return map_item->item; | ||
495 | } | ||
496 | |||
497 | /* in the rare case that a way consists of only one node, it is */ | ||
498 | /* drawn as a circle. This e.g. happens when drawing a new way */ | ||
499 | static canvas_item_t *map_way_single_new(map_t *map, way_t *way, gint radius, | ||
500 | gint width, canvas_color_t fill, canvas_color_t border) { | ||
501 | |||
502 | map_item_t *map_item = g_new0(map_item_t, 1); | ||
503 | harbaum | 154 | map_item->object.type = WAY; |
504 | map_item->object.way = way; | ||
505 | harbaum | 103 | map_item->item = canvas_circle_new(map->canvas, CANVAS_GROUP_WAYS, |
506 | harbaum | 1 | way->node_chain->node->lpos.x, way->node_chain->node->lpos.y, |
507 | radius, width, fill, border); | ||
508 | |||
509 | // TODO: decide: do we need canvas_item_set_zoom_max() here too? | ||
510 | |||
511 | /* attach map_item to nodes map_item_chain */ | ||
512 | map_item_chain_t **chain = &way->map_item_chain; | ||
513 | while(*chain) chain = &(*chain)->next; | ||
514 | *chain = g_new0(map_item_chain_t, 1); | ||
515 | (*chain)->map_item = map_item; | ||
516 | |||
517 | canvas_item_set_user_data(map_item->item, map_item); | ||
518 | |||
519 | canvas_item_destroy_connect(map_item->item, | ||
520 | G_CALLBACK(map_item_destroy_event), map_item); | ||
521 | |||
522 | return map_item->item; | ||
523 | } | ||
524 | |||
525 | static canvas_item_t *map_way_new(map_t *map, canvas_group_t group, | ||
526 | way_t *way, canvas_points_t *points, gint width, | ||
527 | canvas_color_t color, canvas_color_t fill_color) { | ||
528 | map_item_t *map_item = g_new0(map_item_t, 1); | ||
529 | harbaum | 154 | map_item->object.type = WAY; |
530 | map_item->object.way = way; | ||
531 | harbaum | 1 | |
532 | if(way->draw.flags & OSM_DRAW_FLAG_AREA) { | ||
533 | harbaum | 22 | if(map->style->area.color & 0xff) |
534 | harbaum | 103 | map_item->item = canvas_polygon_new(map->canvas, group, points, |
535 | harbaum | 1 | width, color, fill_color); |
536 | else | ||
537 | harbaum | 103 | map_item->item = canvas_polyline_new(map->canvas, group, points, |
538 | harbaum | 1 | width, color); |
539 | } else { | ||
540 | harbaum | 103 | map_item->item = canvas_polyline_new(map->canvas, group, points, width, color); |
541 | harbaum | 1 | } |
542 | |||
543 | canvas_item_set_zoom_max(map_item->item, way->draw.zoom_max); | ||
544 | |||
545 | harbaum | 108 | /* a ways outline itself is never dashed */ |
546 | achadwick | 13 | if (group != CANVAS_GROUP_WAYS_OL) |
547 | if (way->draw.dashed) | ||
548 | achadwick | 46 | canvas_item_set_dashed(map_item->item, width, way->draw.dash_length); |
549 | achadwick | 13 | |
550 | harbaum | 1 | /* attach map_item to ways map_item_chain */ |
551 | map_item_chain_t **chain = &way->map_item_chain; | ||
552 | while(*chain) chain = &(*chain)->next; | ||
553 | *chain = g_new0(map_item_chain_t, 1); | ||
554 | (*chain)->map_item = map_item; | ||
555 | |||
556 | canvas_item_set_user_data(map_item->item, map_item); | ||
557 | |||
558 | canvas_item_destroy_connect(map_item->item, | ||
559 | G_CALLBACK(map_item_destroy_event), map_item); | ||
560 | |||
561 | return map_item->item; | ||
562 | } | ||
563 | |||
564 | void map_show_node(map_t *map, node_t *node) { | ||
565 | map_node_new(map, node, map->style->node.radius, 0, | ||
566 | harbaum | 22 | map->style->node.color, 0); |
567 | harbaum | 1 | } |
568 | |||
569 | void map_way_draw(map_t *map, way_t *way) { | ||
570 | |||
571 | /* don't draw a way that's not there anymore */ | ||
572 | if(way->flags & (OSM_FLAG_DELETED | OSM_FLAG_HIDDEN)) | ||
573 | return; | ||
574 | |||
575 | /* allocate space for nodes */ | ||
576 | /* a way needs at least 2 points to be drawn */ | ||
577 | guint nodes = osm_way_number_of_nodes(way); | ||
578 | if(nodes == 1) { | ||
579 | /* draw a single dot where this single node is */ | ||
580 | map_way_single_new(map, way, map->style->node.radius, 0, | ||
581 | harbaum | 22 | map->style->node.color, 0); |
582 | harbaum | 1 | } else { |
583 | canvas_points_t *points = canvas_points_new(nodes); | ||
584 | |||
585 | int node = 0; | ||
586 | node_chain_t *node_chain = way->node_chain; | ||
587 | while(node_chain) { | ||
588 | canvas_point_set_pos(points, node++, &node_chain->node->lpos); | ||
589 | node_chain = node_chain->next; | ||
590 | } | ||
591 | |||
592 | /* draw way */ | ||
593 | if(way->draw.flags & OSM_DRAW_FLAG_AREA) { | ||
594 | map_way_new(map, CANVAS_GROUP_POLYGONS, way, points, | ||
595 | way->draw.width, way->draw.color, way->draw.area.color); | ||
596 | } else { | ||
597 | |||
598 | harbaum | 108 | if(way->draw.flags & OSM_DRAW_FLAG_BG) { |
599 | map_way_new(map, CANVAS_GROUP_WAYS_INT, way, points, | ||
600 | way->draw.width, way->draw.color, NO_COLOR); | ||
601 | |||
602 | harbaum | 1 | map_way_new(map, CANVAS_GROUP_WAYS_OL, way, points, |
603 | way->draw.bg.width, way->draw.bg.color, NO_COLOR); | ||
604 | harbaum | 108 | |
605 | } else | ||
606 | map_way_new(map, CANVAS_GROUP_WAYS, way, points, | ||
607 | way->draw.width, way->draw.color, NO_COLOR); | ||
608 | harbaum | 1 | } |
609 | canvas_points_free(points); | ||
610 | } | ||
611 | } | ||
612 | |||
613 | void map_node_draw(map_t *map, node_t *node) { | ||
614 | /* don't draw a node that's not there anymore */ | ||
615 | if(node->flags & OSM_FLAG_DELETED) | ||
616 | return; | ||
617 | |||
618 | if(!node->ways) | ||
619 | map_node_new(map, node, | ||
620 | map->style->node.radius, | ||
621 | map->style->node.border_radius, | ||
622 | harbaum | 22 | map->style->node.fill_color, |
623 | map->style->node.color); | ||
624 | harbaum | 1 | |
625 | else if(map->style->node.show_untagged || osm_node_has_tag(node)) | ||
626 | map_node_new(map, node, | ||
627 | map->style->node.radius, 0, | ||
628 | harbaum | 22 | map->style->node.color, 0); |
629 | harbaum | 1 | } |
630 | |||
631 | static void map_item_draw(map_t *map, map_item_t *map_item) { | ||
632 | harbaum | 154 | switch(map_item->object.type) { |
633 | case NODE: | ||
634 | map_node_draw(map, map_item->object.node); | ||
635 | harbaum | 1 | break; |
636 | harbaum | 154 | case WAY: |
637 | map_way_draw(map, map_item->object.way); | ||
638 | harbaum | 1 | break; |
639 | default: | ||
640 | harbaum | 154 | g_assert((map_item->object.type == NODE) || |
641 | (map_item->object.type == WAY)); | ||
642 | harbaum | 1 | } |
643 | } | ||
644 | |||
645 | static void map_item_remove(map_t *map, map_item_t *map_item) { | ||
646 | map_item_chain_t **chainP = NULL; | ||
647 | |||
648 | harbaum | 154 | switch(map_item->object.type) { |
649 | case NODE: | ||
650 | chainP = &map_item->object.node->map_item_chain; | ||
651 | harbaum | 1 | break; |
652 | harbaum | 154 | case WAY: |
653 | chainP = &map_item->object.way->map_item_chain; | ||
654 | harbaum | 1 | break; |
655 | default: | ||
656 | harbaum | 154 | g_assert((map_item->object.type == NODE) || |
657 | (map_item->object.type == WAY)); | ||
658 | harbaum | 1 | } |
659 | |||
660 | map_item_chain_destroy(chainP); | ||
661 | } | ||
662 | |||
663 | static void map_item_init(style_t *style, map_item_t *map_item) { | ||
664 | harbaum | 154 | switch (map_item->object.type){ |
665 | case WAY: | ||
666 | josm_elemstyles_colorize_way(style, map_item->object.way); | ||
667 | harbaum | 1 | break; |
668 | harbaum | 154 | case NODE: |
669 | josm_elemstyles_colorize_node(style, map_item->object.node); | ||
670 | harbaum | 1 | break; |
671 | default: | ||
672 | harbaum | 154 | g_assert((map_item->object.type == NODE) || |
673 | (map_item->object.type == WAY)); | ||
674 | harbaum | 1 | } |
675 | } | ||
676 | |||
677 | void map_item_redraw(appdata_t *appdata, map_item_t *map_item) { | ||
678 | map_item_t item = *map_item; | ||
679 | |||
680 | /* check if the item to be redrawn is the selected one */ | ||
681 | gboolean is_selected = FALSE; | ||
682 | harbaum | 154 | if(map_item->object.ptr == appdata->map->selected.object.ptr) { |
683 | harbaum | 1 | map_item_deselect(appdata); |
684 | is_selected = TRUE; | ||
685 | } | ||
686 | |||
687 | map_item_remove(appdata->map, &item); | ||
688 | map_item_init(appdata->map->style, &item); | ||
689 | map_item_draw(appdata->map, &item); | ||
690 | |||
691 | /* restore selection if there was one */ | ||
692 | if(is_selected) | ||
693 | harbaum | 154 | map_object_select(appdata, &item.object); |
694 | harbaum | 1 | } |
695 | |||
696 | static void map_frisket_rectangle(canvas_points_t *points, | ||
697 | gint x0, gint x1, gint y0, gint y1) { | ||
698 | points->coords[2*0+0] = points->coords[2*3+0] = points->coords[2*4+0] = x0; | ||
699 | points->coords[2*1+0] = points->coords[2*2+0] = x1; | ||
700 | points->coords[2*0+1] = points->coords[2*1+1] = points->coords[2*4+1] = y0; | ||
701 | points->coords[2*2+1] = points->coords[2*3+1] = y1; | ||
702 | } | ||
703 | |||
704 | /* Draw the frisket area which masks off areas it'd be unsafe to edit, | ||
705 | * plus its inner edge marker line */ | ||
706 | void map_frisket_draw(map_t *map, bounds_t *bounds) { | ||
707 | canvas_points_t *points = canvas_points_new(5); | ||
708 | |||
709 | /* don't draw frisket at all if it's completely transparent */ | ||
710 | harbaum | 22 | if(map->style->frisket.color & 0xff) { |
711 | elemstyle_color_t color = map->style->frisket.color; | ||
712 | harbaum | 1 | |
713 | float mult = map->style->frisket.mult; | ||
714 | |||
715 | /* top rectangle */ | ||
716 | map_frisket_rectangle(points, mult*bounds->min.x, mult*bounds->max.x, | ||
717 | mult*bounds->min.y, bounds->min.y); | ||
718 | harbaum | 103 | canvas_polygon_new(map->canvas, CANVAS_GROUP_FRISKET, points, |
719 | 1, NO_COLOR, color); | ||
720 | harbaum | 1 | |
721 | /* bottom rectangle */ | ||
722 | map_frisket_rectangle(points, mult*bounds->min.x, mult*bounds->max.x, | ||
723 | bounds->max.y, mult*bounds->max.y); | ||
724 | harbaum | 103 | canvas_polygon_new(map->canvas, CANVAS_GROUP_FRISKET, points, |
725 | 1, NO_COLOR, color); | ||
726 | harbaum | 1 | |
727 | /* left rectangle */ | ||
728 | map_frisket_rectangle(points, mult*bounds->min.x, bounds->min.x, | ||
729 | mult*bounds->min.y, mult*bounds->max.y); | ||
730 | harbaum | 103 | canvas_polygon_new(map->canvas, CANVAS_GROUP_FRISKET, points, |
731 | 1, NO_COLOR, color); | ||
732 | harbaum | 1 | |
733 | /* right rectangle */ | ||
734 | map_frisket_rectangle(points, bounds->max.x, mult*bounds->max.x, | ||
735 | mult*bounds->min.y, mult*bounds->max.y); | ||
736 | harbaum | 103 | canvas_polygon_new(map->canvas, CANVAS_GROUP_FRISKET, points, |
737 | 1, NO_COLOR, color); | ||
738 | harbaum | 1 | |
739 | } | ||
740 | |||
741 | if(map->style->frisket.border.present) { | ||
742 | // Edge marker line | ||
743 | gint ew2 = map->style->frisket.border.width/2; | ||
744 | map_frisket_rectangle(points, | ||
745 | bounds->min.x-ew2, bounds->max.x+ew2, | ||
746 | bounds->min.y-ew2, bounds->max.y+ew2); | ||
747 | |||
748 | harbaum | 103 | canvas_polyline_new(map->canvas, CANVAS_GROUP_FRISKET, points, |
749 | harbaum | 1 | map->style->frisket.border.width, |
750 | map->style->frisket.border.color); | ||
751 | |||
752 | } | ||
753 | canvas_points_free(points); | ||
754 | } | ||
755 | |||
756 | static void map_draw(map_t *map, osm_t *osm) { | ||
757 | g_assert(map->canvas); | ||
758 | |||
759 | printf("drawing ways ...\n"); | ||
760 | way_t *way = osm->way; | ||
761 | while(way) { | ||
762 | map_way_draw(map, way); | ||
763 | way = way->next; | ||
764 | } | ||
765 | |||
766 | printf("drawing single nodes ...\n"); | ||
767 | node_t *node = osm->node; | ||
768 | while(node) { | ||
769 | map_node_draw(map, node); | ||
770 | node = node->next; | ||
771 | } | ||
772 | |||
773 | printf("drawing frisket...\n"); | ||
774 | map_frisket_draw(map, osm->bounds); | ||
775 | } | ||
776 | |||
777 | void map_state_free(map_state_t *state) { | ||
778 | if(!state) return; | ||
779 | |||
780 | /* free state of noone else references it */ | ||
781 | if(state->refcount > 1) | ||
782 | state->refcount--; | ||
783 | else | ||
784 | g_free(state); | ||
785 | } | ||
786 | |||
787 | void map_free_map_item_chains(appdata_t *appdata) { | ||
788 | if(!appdata->osm) return; | ||
789 | |||
790 | #ifndef DESTROY_WAIT_FOR_GTK | ||
791 | printf(" DESTROY_WAIT_FOR_GTK not set, removing all chains now\n"); | ||
792 | |||
793 | /* free all map_item_chains */ | ||
794 | node_t *node = appdata->osm->node; | ||
795 | while(node) { | ||
796 | map_item_chain_t *chain = node->map_item_chain; | ||
797 | while(chain) { | ||
798 | map_item_chain_t *next = chain->next; | ||
799 | g_free(chain); | ||
800 | chain = next; | ||
801 | } | ||
802 | node->map_item_chain = NULL; | ||
803 | node = node->next; | ||
804 | } | ||
805 | |||
806 | way_t *way = appdata->osm->way; | ||
807 | while(way) { | ||
808 | map_item_chain_t *chain = way->map_item_chain; | ||
809 | while(chain) { | ||
810 | map_item_chain_t *next = chain->next; | ||
811 | g_free(chain); | ||
812 | chain = next; | ||
813 | } | ||
814 | way->map_item_chain = NULL; | ||
815 | way = way->next; | ||
816 | } | ||
817 | #endif | ||
818 | } | ||
819 | |||
820 | static gint map_destroy_event(GtkWidget *widget, gpointer data) { | ||
821 | appdata_t *appdata = (appdata_t*)data; | ||
822 | map_t *map = appdata->map; | ||
823 | |||
824 | printf("destroying entire map\n"); | ||
825 | |||
826 | map_free_map_item_chains(appdata); | ||
827 | |||
828 | /* free buffered tags */ | ||
829 | if(map->last_node_tags) osm_tags_free(map->last_node_tags); | ||
830 | if(map->last_way_tags) osm_tags_free(map->last_way_tags); | ||
831 | |||
832 | map_state_free(map->state); | ||
833 | |||
834 | if(map->style) | ||
835 | style_free(map->style); | ||
836 | |||
837 | /* destroy existing highlight */ | ||
838 | if(map->highlight) { | ||
839 | printf("removing highlight\n"); | ||
840 | |||
841 | map_highlight_t *hl = map->highlight; | ||
842 | while(hl) { | ||
843 | map_highlight_t *next = hl->next; | ||
844 | g_free(hl); | ||
845 | hl = next; | ||
846 | } | ||
847 | } | ||
848 | |||
849 | g_free(map); | ||
850 | |||
851 | appdata->map = NULL; | ||
852 | |||
853 | return FALSE; | ||
854 | } | ||
855 | |||
856 | /* get the item at position x, y */ | ||
857 | map_item_t *map_item_at(map_t *map, gint x, gint y) { | ||
858 | printf("map check at %d/%d\n", x, y); | ||
859 | |||
860 | canvas_window2world(map->canvas, x, y, &x, &y); | ||
861 | |||
862 | printf("world check at %d/%d\n", x, y); | ||
863 | |||
864 | canvas_item_t *item = canvas_get_item_at(map->canvas, x, y); | ||
865 | |||
866 | if(!item) { | ||
867 | printf(" there's no item\n"); | ||
868 | return NULL; | ||
869 | } | ||
870 | |||
871 | printf(" there's an item (%p)\n", item); | ||
872 | |||
873 | map_item_t *map_item = canvas_item_get_user_data(item); | ||
874 | |||
875 | if(!map_item) { | ||
876 | printf(" item has no user data!\n"); | ||
877 | return NULL; | ||
878 | } | ||
879 | |||
880 | harbaum | 15 | if(map_item->highlight) |
881 | harbaum | 1 | printf(" item is highlight\n"); |
882 | |||
883 | harbaum | 154 | switch(map_item->object.type) { |
884 | case NODE: | ||
885 | harbaum | 161 | printf(" item is node #"ITEM_ID_FORMAT"\n", map_item->object.node->id); |
886 | harbaum | 1 | break; |
887 | harbaum | 154 | case WAY: |
888 | harbaum | 161 | printf(" item is way #"ITEM_ID_FORMAT"\n", map_item->object.way->id); |
889 | harbaum | 1 | break; |
890 | default: | ||
891 | printf(" unknown item\n"); | ||
892 | break; | ||
893 | } | ||
894 | |||
895 | return map_item; | ||
896 | } | ||
897 | |||
898 | /* get the real item (no highlight) at x, y */ | ||
899 | harbaum | 15 | map_item_t *map_real_item_at(map_t *map, gint x, gint y) { |
900 | harbaum | 1 | map_item_t *map_item = map_item_at(map, x, y); |
901 | |||
902 | /* no item or already a real one */ | ||
903 | if(!map_item || !map_item->highlight) return map_item; | ||
904 | |||
905 | /* get the item (parent) this item is the highlight of */ | ||
906 | map_item_t *parent = NULL; | ||
907 | harbaum | 154 | switch(map_item->object.type) { |
908 | harbaum | 1 | |
909 | harbaum | 154 | case NODE: |
910 | if(map_item->object.node->map_item_chain) | ||
911 | parent = map_item->object.node->map_item_chain->map_item; | ||
912 | harbaum | 1 | |
913 | if(parent) | ||
914 | harbaum | 161 | printf(" using parent item node #" ITEM_ID_FORMAT "\n", |
915 | parent->object.node->id); | ||
916 | harbaum | 1 | break; |
917 | |||
918 | harbaum | 154 | case WAY: |
919 | if(map_item->object.way->map_item_chain) | ||
920 | parent = map_item->object.way->map_item_chain->map_item; | ||
921 | harbaum | 1 | |
922 | if(parent) | ||
923 | harbaum | 161 | printf(" using parent item way #" ITEM_ID_FORMAT "\n", |
924 | parent->object.way->id); | ||
925 | harbaum | 1 | break; |
926 | |||
927 | default: | ||
928 | harbaum | 154 | g_assert((map_item->object.type == NODE) || |
929 | (map_item->object.type == WAY)); | ||
930 | harbaum | 1 | break; |
931 | } | ||
932 | |||
933 | if(parent) | ||
934 | map_item = parent; | ||
935 | else | ||
936 | printf(" no parent, working on highlight itself\n"); | ||
937 | |||
938 | return map_item; | ||
939 | } | ||
940 | |||
941 | /* Limitations on the amount by which we can scroll. Keeps part of the | ||
942 | * map visible at all times */ | ||
943 | harbaum | 103 | static void map_limit_scroll(map_t *map, canvas_unit_t unit, |
944 | gint *sx, gint *sy) { | ||
945 | harbaum | 1 | |
946 | harbaum | 103 | /* get scale factor for pixel->meter conversion. set to 1 if */ |
947 | /* given coordinates are already in meters */ | ||
948 | gdouble scale = (unit == CANVAS_UNIT_METER)?1.0:canvas_get_zoom(map->canvas); | ||
949 | harbaum | 1 | |
950 | harbaum | 103 | /* convert pixels to meters if necessary */ |
951 | gdouble sx_cu = *sx / scale; | ||
952 | gdouble sy_cu = *sy / scale; | ||
953 | |||
954 | /* get size of visible area in canvas units (meters) */ | ||
955 | gint aw_cu = canvas_get_viewport_width(map->canvas, CANVAS_UNIT_METER); | ||
956 | gint ah_cu = canvas_get_viewport_height(map->canvas, CANVAS_UNIT_METER); | ||
957 | |||
958 | // Data rect minimum and maximum | ||
959 | gint min_x, min_y, max_x, max_y; | ||
960 | min_x = map->appdata->osm->bounds->min.x; | ||
961 | min_y = map->appdata->osm->bounds->min.y; | ||
962 | max_x = map->appdata->osm->bounds->max.x; | ||
963 | max_y = map->appdata->osm->bounds->max.y; | ||
964 | |||
965 | // limit stops - prevent scrolling beyond these | ||
966 | gint min_sy_cu = 0.95*(min_y - ah_cu); | ||
967 | gint min_sx_cu = 0.95*(min_x - aw_cu); | ||
968 | gint max_sy_cu = 0.95*(max_y); | ||
969 | gint max_sx_cu = 0.95*(max_x); | ||
970 | if (sy_cu < min_sy_cu) { *sy = min_sy_cu * scale; } | ||
971 | if (sx_cu < min_sx_cu) { *sx = min_sx_cu * scale; } | ||
972 | if (sy_cu > max_sy_cu) { *sy = max_sy_cu * scale; } | ||
973 | if (sx_cu > max_sx_cu) { *sx = max_sx_cu * scale; } | ||
974 | harbaum | 1 | } |
975 | |||
976 | |||
977 | /* Limit a proposed zoom factor to sane ranges. | ||
978 | * Specifically the map is allowed to be no smaller than the viewport. */ | ||
979 | static gboolean map_limit_zoom(map_t *map, gdouble *zoom) { | ||
980 | // Data rect minimum and maximum | ||
981 | gint min_x, min_y, max_x, max_y; | ||
982 | min_x = map->appdata->osm->bounds->min.x; | ||
983 | min_y = map->appdata->osm->bounds->min.y; | ||
984 | max_x = map->appdata->osm->bounds->max.x; | ||
985 | max_y = map->appdata->osm->bounds->max.y; | ||
986 | |||
987 | harbaum | 103 | /* get size of visible area in pixels and convert to meters of intended */ |
988 | /* zoom by deviding by zoom (which is basically pix/m) */ | ||
989 | gint aw_cu = | ||
990 | canvas_get_viewport_width(map->canvas, CANVAS_UNIT_PIXEL) / *zoom; | ||
991 | gint ah_cu = | ||
992 | canvas_get_viewport_height(map->canvas, CANVAS_UNIT_PIXEL) / *zoom; | ||
993 | harbaum | 1 | |
994 | gdouble oldzoom = *zoom; | ||
995 | if (ah_cu < aw_cu) { | ||
996 | gint lim_h = ah_cu*0.95; | ||
997 | if (max_y-min_y < lim_h) { | ||
998 | gdouble corr = ((gdouble)max_y-min_y) / (gdouble)lim_h; | ||
999 | *zoom /= corr; | ||
1000 | } | ||
1001 | } | ||
1002 | else { | ||
1003 | gint lim_w = aw_cu*0.95; | ||
1004 | if (max_x-min_x < lim_w) { | ||
1005 | gdouble corr = ((gdouble)max_x-min_x) / (gdouble)lim_w; | ||
1006 | *zoom /= corr; | ||
1007 | } | ||
1008 | } | ||
1009 | if (*zoom != oldzoom) { | ||
1010 | printf("Can't zoom further out\n"); | ||
1011 | return 1; | ||
1012 | } | ||
1013 | return 0; | ||
1014 | } | ||
1015 | |||
1016 | |||
1017 | achadwick | 12 | /* |
1018 | * Scroll the map to a point if that point is currently offscreen. | ||
1019 | */ | ||
1020 | void map_scroll_to_if_offscreen(map_t *map, lpos_t *lpos) { | ||
1021 | |||
1022 | // Ignore anything outside the working area | ||
1023 | if (!(map && map->appdata && map->appdata->osm)) { | ||
1024 | return; | ||
1025 | } | ||
1026 | gint min_x, min_y, max_x, max_y; | ||
1027 | min_x = map->appdata->osm->bounds->min.x; | ||
1028 | min_y = map->appdata->osm->bounds->min.y; | ||
1029 | max_x = map->appdata->osm->bounds->max.x; | ||
1030 | max_y = map->appdata->osm->bounds->max.y; | ||
1031 | if ( (lpos->x > max_x) || (lpos->x < min_x) | ||
1032 | || (lpos->y > max_y) || (lpos->y < min_y)) { | ||
1033 | harbaum | 14 | printf("cannot scroll to (%d, %d): outside the working area\n", |
1034 | lpos->x, lpos->y); | ||
1035 | achadwick | 12 | return; |
1036 | } | ||
1037 | |||
1038 | // Viewport dimensions in canvas space | ||
1039 | |||
1040 | harbaum | 103 | /* get size of visible area in canvas units (meters) */ |
1041 | gdouble pix_per_meter = canvas_get_zoom(map->canvas); | ||
1042 | gdouble aw = canvas_get_viewport_width(map->canvas, CANVAS_UNIT_METER); | ||
1043 | gdouble ah = canvas_get_viewport_height(map->canvas, CANVAS_UNIT_METER); | ||
1044 | |||
1045 | achadwick | 12 | // Is the point still onscreen? |
1046 | gboolean vert_recentre_needed = FALSE; | ||
1047 | gboolean horiz_recentre_needed = FALSE; | ||
1048 | gint sx, sy; | ||
1049 | harbaum | 103 | canvas_scroll_get(map->canvas, CANVAS_UNIT_PIXEL, &sx, &sy); |
1050 | gint viewport_left = (sx/pix_per_meter); | ||
1051 | gint viewport_right = (sx/pix_per_meter)+aw; | ||
1052 | gint viewport_top = (sy/pix_per_meter); | ||
1053 | gint viewport_bottom = (sy/pix_per_meter)+ah; | ||
1054 | achadwick | 12 | if (lpos->x > viewport_right) { |
1055 | printf("** off right edge (%d > %d)\n", lpos->x, viewport_right); | ||
1056 | horiz_recentre_needed = TRUE; | ||
1057 | } | ||
1058 | if (lpos->x < viewport_left) { | ||
1059 | printf("** off left edge (%d < %d)\n", lpos->x, viewport_left); | ||
1060 | horiz_recentre_needed = TRUE; | ||
1061 | } | ||
1062 | if (lpos->y > viewport_bottom) { | ||
1063 | printf("** off bottom edge (%d > %d)\n", lpos->y, viewport_bottom); | ||
1064 | vert_recentre_needed = TRUE; | ||
1065 | } | ||
1066 | if (lpos->y < viewport_top) { | ||
1067 | printf("** off top edge (%d < %d)\n", lpos->y, viewport_top); | ||
1068 | vert_recentre_needed = TRUE; | ||
1069 | } | ||
1070 | |||
1071 | if (horiz_recentre_needed || vert_recentre_needed) { | ||
1072 | gint new_sx, new_sy; | ||
1073 | harbaum | 78 | |
1074 | achadwick | 12 | // Just centre both at once |
1075 | harbaum | 103 | new_sx = pix_per_meter * (lpos->x - (aw/2)); |
1076 | new_sy = pix_per_meter * (lpos->y - (ah/2)); | ||
1077 | harbaum | 78 | |
1078 | harbaum | 103 | map_limit_scroll(map, CANVAS_UNIT_PIXEL, &new_sx, &new_sy); |
1079 | canvas_scroll_to(map->canvas, CANVAS_UNIT_PIXEL, new_sx, new_sy); | ||
1080 | achadwick | 12 | } |
1081 | } | ||
1082 | |||
1083 | harbaum | 1 | /* Deselects the current way or node if its zoom_max |
1084 | * means that it's not going to render at the current map zoom. */ | ||
1085 | void map_deselect_if_zoom_below_zoom_max(map_t *map) { | ||
1086 | harbaum | 154 | if (map->selected.object.type == WAY) { |
1087 | harbaum | 1 | printf("will deselect way if zoomed below %f\n", |
1088 | harbaum | 154 | map->selected.object.way->draw.zoom_max); |
1089 | if (map->state->zoom < map->selected.object.way->draw.zoom_max) { | ||
1090 | harbaum | 1 | printf(" deselecting way!\n"); |
1091 | map_item_deselect(map->appdata); | ||
1092 | } | ||
1093 | } | ||
1094 | harbaum | 154 | else if (map->selected.object.type == NODE) { |
1095 | harbaum | 1 | printf("will deselect node if zoomed below %f\n", |
1096 | harbaum | 154 | map->selected.object.node->zoom_max); |
1097 | if (map->state->zoom < map->selected.object.node->zoom_max) { | ||
1098 | harbaum | 1 | printf(" deselecting node!\n"); |
1099 | map_item_deselect(map->appdata); | ||
1100 | } | ||
1101 | } | ||
1102 | } | ||
1103 | |||
1104 | void map_set_zoom(map_t *map, double zoom, | ||
1105 | gboolean update_scroll_offsets) { | ||
1106 | gboolean at_zoom_limit = 0; | ||
1107 | at_zoom_limit = map_limit_zoom(map, &zoom); | ||
1108 | harbaum | 78 | |
1109 | harbaum | 1 | map->state->zoom = zoom; |
1110 | canvas_set_zoom(map->canvas, map->state->zoom); | ||
1111 | |||
1112 | map_deselect_if_zoom_below_zoom_max(map); | ||
1113 | |||
1114 | harbaum | 103 | if(update_scroll_offsets) { |
1115 | harbaum | 1 | if (!at_zoom_limit) { |
1116 | /* zooming affects the scroll offsets */ | ||
1117 | gint sx, sy; | ||
1118 | harbaum | 103 | canvas_scroll_get(map->canvas, CANVAS_UNIT_PIXEL, &sx, &sy); |
1119 | map_limit_scroll(map, CANVAS_UNIT_PIXEL, &sx, &sy); | ||
1120 | |||
1121 | // keep the map visible | ||
1122 | canvas_scroll_to(map->canvas, CANVAS_UNIT_PIXEL, sx, sy); | ||
1123 | harbaum | 1 | } |
1124 | harbaum | 103 | |
1125 | canvas_scroll_get(map->canvas, CANVAS_UNIT_METER, | ||
1126 | &map->state->scroll_offset.x, | ||
1127 | &map->state->scroll_offset.y); | ||
1128 | harbaum | 1 | } |
1129 | } | ||
1130 | |||
1131 | static gboolean map_scroll_event(GtkWidget *widget, GdkEventScroll *event, | ||
1132 | gpointer data) { | ||
1133 | appdata_t *appdata = (appdata_t*)data; | ||
1134 | |||
1135 | if(!appdata->osm) return FALSE; | ||
1136 | |||
1137 | if(event->type == GDK_SCROLL && appdata->map && appdata->map->state) { | ||
1138 | if(event->direction) | ||
1139 | map_set_zoom(appdata->map, | ||
1140 | appdata->map->state->zoom / ZOOM_FACTOR_WHEEL, TRUE); | ||
1141 | else | ||
1142 | map_set_zoom(appdata->map, | ||
1143 | appdata->map->state->zoom * ZOOM_FACTOR_WHEEL, TRUE); | ||
1144 | } | ||
1145 | |||
1146 | return TRUE; | ||
1147 | } | ||
1148 | |||
1149 | static gboolean distance_above(map_t *map, gint x, gint y, gint limit) { | ||
1150 | gint sx, sy; | ||
1151 | |||
1152 | /* add offsets generated by mouse within map and map scrolling */ | ||
1153 | sx = (x-map->pen_down.at.x); | ||
1154 | sy = (y-map->pen_down.at.y); | ||
1155 | |||
1156 | return(sx*sx + sy*sy > limit*limit); | ||
1157 | } | ||
1158 | |||
1159 | /* scroll with respect to two screen positions */ | ||
1160 | static void map_do_scroll(map_t *map, gint x, gint y) { | ||
1161 | gint sx, sy; | ||
1162 | |||
1163 | harbaum | 103 | canvas_scroll_get(map->canvas, CANVAS_UNIT_PIXEL, &sx, &sy); |
1164 | harbaum | 1 | sx -= x-map->pen_down.at.x; |
1165 | sy -= y-map->pen_down.at.y; | ||
1166 | harbaum | 103 | map_limit_scroll(map, CANVAS_UNIT_PIXEL, &sx, &sy); |
1167 | canvas_scroll_to(map->canvas, CANVAS_UNIT_PIXEL, sx, sy); | ||
1168 | |||
1169 | canvas_scroll_get(map->canvas, CANVAS_UNIT_METER, | ||
1170 | &map->state->scroll_offset.x, | ||
1171 | &map->state->scroll_offset.y); | ||
1172 | harbaum | 1 | } |
1173 | |||
1174 | |||
1175 | /* scroll a certain step */ | ||
1176 | static void map_do_scroll_step(map_t *map, gint x, gint y) { | ||
1177 | gint sx, sy; | ||
1178 | harbaum | 103 | canvas_scroll_get(map->canvas, CANVAS_UNIT_PIXEL, &sx, &sy); |
1179 | harbaum | 1 | sx += x; |
1180 | sy += y; | ||
1181 | harbaum | 103 | map_limit_scroll(map, CANVAS_UNIT_PIXEL, &sx, &sy); |
1182 | canvas_scroll_to(map->canvas, CANVAS_UNIT_PIXEL, sx, sy); | ||
1183 | |||
1184 | canvas_scroll_get(map->canvas, CANVAS_UNIT_METER, | ||
1185 | &map->state->scroll_offset.x, | ||
1186 | &map->state->scroll_offset.y); | ||
1187 | harbaum | 1 | } |
1188 | |||
1189 | gboolean map_item_is_selected_node(map_t *map, map_item_t *map_item) { | ||
1190 | printf("check if item is a selected node\n"); | ||
1191 | |||
1192 | if(!map_item) { | ||
1193 | printf(" no item requested\n"); | ||
1194 | return FALSE; | ||
1195 | } | ||
1196 | |||
1197 | harbaum | 154 | if(map->selected.object.type == ILLEGAL) { |
1198 | harbaum | 1 | printf(" nothing is selected\n"); |
1199 | return FALSE; | ||
1200 | } | ||
1201 | |||
1202 | /* clicked the highlight directly */ | ||
1203 | harbaum | 154 | if(map_item->object.type != NODE) { |
1204 | harbaum | 1 | printf(" didn't click node\n"); |
1205 | return FALSE; | ||
1206 | } | ||
1207 | |||
1208 | harbaum | 154 | if(map->selected.object.type == NODE) { |
1209 | harbaum | 1 | printf(" selected item is a node\n"); |
1210 | |||
1211 | harbaum | 154 | if(map_item->object.node == map->selected.object.node) { |
1212 | harbaum | 1 | printf(" requested item is a selected node\n"); |
1213 | return TRUE; | ||
1214 | } | ||
1215 | printf(" but it's not the requested one\n"); | ||
1216 | return FALSE; | ||
1217 | |||
1218 | harbaum | 154 | } else if(map->selected.object.type == WAY) { |
1219 | harbaum | 1 | printf(" selected item is a way\n"); |
1220 | |||
1221 | harbaum | 154 | node_chain_t *node_chain = map->selected.object.way->node_chain; |
1222 | harbaum | 1 | while(node_chain) { |
1223 | harbaum | 154 | if(node_chain->node == map_item->object.node) { |
1224 | harbaum | 1 | printf(" requested item is part of selected way\n"); |
1225 | return TRUE; | ||
1226 | } | ||
1227 | node_chain = node_chain->next; | ||
1228 | } | ||
1229 | printf(" but it doesn't include the requested node\n"); | ||
1230 | return FALSE; | ||
1231 | |||
1232 | } else { | ||
1233 | printf(" selected item is unknown\n"); | ||
1234 | return FALSE; | ||
1235 | } | ||
1236 | |||
1237 | g_assert(0); | ||
1238 | return TRUE; | ||
1239 | } | ||
1240 | |||
1241 | /* return true if the item given is the currenly selected way */ | ||
1242 | /* also return false if nothing is selected or the selection is no way */ | ||
1243 | gboolean map_item_is_selected_way(map_t *map, map_item_t *map_item) { | ||
1244 | printf("check if item is the selected way\n"); | ||
1245 | |||
1246 | if(!map_item) { | ||
1247 | printf(" no item requested\n"); | ||
1248 | return FALSE; | ||
1249 | } | ||
1250 | |||
1251 | harbaum | 154 | if(map->selected.object.type == ILLEGAL) { |
1252 | harbaum | 1 | printf(" nothing is selected\n"); |
1253 | return FALSE; | ||
1254 | } | ||
1255 | |||
1256 | /* clicked the highlight directly */ | ||
1257 | harbaum | 154 | if(map_item->object.type != WAY) { |
1258 | harbaum | 1 | printf(" didn't click way\n"); |
1259 | return FALSE; | ||
1260 | } | ||
1261 | |||
1262 | harbaum | 154 | if(map->selected.object.type == WAY) { |
1263 | harbaum | 1 | printf(" selected item is a way\n"); |
1264 | |||
1265 | harbaum | 154 | if(map_item->object.way == map->selected.object.way) { |
1266 | harbaum | 1 | printf(" requested item is a selected way\n"); |
1267 | return TRUE; | ||
1268 | } | ||
1269 | printf(" but it's not the requested one\n"); | ||
1270 | return FALSE; | ||
1271 | } | ||
1272 | |||
1273 | printf(" selected item is not a way\n"); | ||
1274 | return FALSE; | ||
1275 | } | ||
1276 | |||
1277 | |||
1278 | void map_highlight_refresh(appdata_t *appdata) { | ||
1279 | map_t *map = appdata->map; | ||
1280 | harbaum | 154 | object_t old = map->selected.object; |
1281 | harbaum | 1 | |
1282 | printf("type to refresh is %d\n", old.type); | ||
1283 | harbaum | 154 | if(old.type == ILLEGAL) |
1284 | harbaum | 1 | return; |
1285 | |||
1286 | map_item_deselect(appdata); | ||
1287 | harbaum | 154 | map_object_select(appdata, &old); |
1288 | harbaum | 1 | } |
1289 | |||
1290 | void map_way_delete(appdata_t *appdata, way_t *way) { | ||
1291 | harbaum | 161 | printf("deleting way #" ITEM_ID_FORMAT " from map and osm\n", way->id); |
1292 | harbaum | 1 | |
1293 | /* remove it visually from the screen */ | ||
1294 | map_item_chain_destroy(&way->map_item_chain); | ||
1295 | |||
1296 | /* also remove the visible representation of all nodes (nodes with tags) */ | ||
1297 | node_chain_t *chain = way->node_chain; | ||
1298 | while(chain) { | ||
1299 | if(chain->node->map_item_chain) | ||
1300 | map_item_chain_destroy(&chain->node->map_item_chain); | ||
1301 | |||
1302 | chain = chain->next; | ||
1303 | } | ||
1304 | |||
1305 | /* and mark it "deleted" in the database */ | ||
1306 | osm_way_remove_from_relation(appdata->osm, way); | ||
1307 | osm_way_delete(appdata->osm, &appdata->icon, way, FALSE); | ||
1308 | } | ||
1309 | |||
1310 | static void map_handle_click(appdata_t *appdata, map_t *map) { | ||
1311 | |||
1312 | /* problem: on_item may be the highlight itself! So store it! */ | ||
1313 | map_item_t map_item; | ||
1314 | if(map->pen_down.on_item) map_item = *map->pen_down.on_item; | ||
1315 | harbaum | 154 | else map_item.object.type = ILLEGAL; |
1316 | harbaum | 1 | |
1317 | /* if we aready have something selected, then de-select it */ | ||
1318 | map_item_deselect(appdata); | ||
1319 | |||
1320 | /* select the clicked item (if there was one) */ | ||
1321 | harbaum | 154 | if(map_item.object.type != ILLEGAL) { |
1322 | switch(map_item.object.type) { | ||
1323 | case NODE: | ||
1324 | map_node_select(appdata, map_item.object.node); | ||
1325 | harbaum | 1 | break; |
1326 | |||
1327 | harbaum | 154 | case WAY: |
1328 | map_way_select(appdata, map_item.object.way); | ||
1329 | harbaum | 1 | break; |
1330 | |||
1331 | default: | ||
1332 | harbaum | 154 | g_assert((map_item.object.type == NODE) || |
1333 | (map_item.object.type == WAY)); | ||
1334 | harbaum | 1 | break; |
1335 | } | ||
1336 | } | ||
1337 | } | ||
1338 | |||
1339 | static void map_touchnode_update(appdata_t *appdata, gint x, gint y) { | ||
1340 | map_t *map = appdata->map; | ||
1341 | |||
1342 | map_hl_touchnode_clear(map); | ||
1343 | |||
1344 | node_t *cur_node = NULL; | ||
1345 | |||
1346 | /* the "current node" which is the one we are working on and which */ | ||
1347 | /* should not be highlighted depends on the action */ | ||
1348 | switch(map->action.type) { | ||
1349 | |||
1350 | /* in idle mode the dragged node is not highlighted */ | ||
1351 | case MAP_ACTION_IDLE: | ||
1352 | g_assert(map->pen_down.on_item); | ||
1353 | harbaum | 154 | g_assert(map->pen_down.on_item->object.type == NODE); |
1354 | cur_node = map->pen_down.on_item->object.node; | ||
1355 | harbaum | 1 | break; |
1356 | |||
1357 | default: | ||
1358 | break; | ||
1359 | } | ||
1360 | |||
1361 | |||
1362 | /* check if we are close to one of the other nodes */ | ||
1363 | canvas_window2world(appdata->map->canvas, x, y, &x, &y); | ||
1364 | node_t *node = appdata->osm->node; | ||
1365 | while(!map->touchnode && node) { | ||
1366 | /* don't highlight the dragged node itself and don't highlight */ | ||
1367 | /* deleted ones */ | ||
1368 | if((node != cur_node) && (!(node->flags & OSM_FLAG_DELETED))) { | ||
1369 | gint nx = x - node->lpos.x; | ||
1370 | gint ny = y - node->lpos.y; | ||
1371 | |||
1372 | if((nx < map->style->node.radius) && (ny < map->style->node.radius) && | ||
1373 | (nx*nx + ny*ny < map->style->node.radius * map->style->node.radius)) | ||
1374 | map_hl_touchnode_draw(map, node); | ||
1375 | } | ||
1376 | node = node->next; | ||
1377 | } | ||
1378 | |||
1379 | /* during way creation also nodes of the new way */ | ||
1380 | /* need to be searched */ | ||
1381 | if(!map->touchnode && map->action.way) { | ||
1382 | node_chain_t *chain = map->action.way->node_chain; | ||
1383 | while(!map->touchnode && chain && chain->next) { | ||
1384 | gint nx = x - chain->node->lpos.x; | ||
1385 | gint ny = y - chain->node->lpos.y; | ||
1386 | |||
1387 | if((nx < map->style->node.radius) && (ny < map->style->node.radius) && | ||
1388 | (nx*nx + ny*ny < map->style->node.radius * map->style->node.radius)) | ||
1389 | map_hl_touchnode_draw(map, chain->node); | ||
1390 | |||
1391 | chain = chain->next; | ||
1392 | } | ||
1393 | } | ||
1394 | } | ||
1395 | |||
1396 | static void map_button_press(map_t *map, gint x, gint y) { | ||
1397 | |||
1398 | printf("left button pressed\n"); | ||
1399 | map->pen_down.is = TRUE; | ||
1400 | |||
1401 | /* save press position */ | ||
1402 | map->pen_down.at.x = x; | ||
1403 | map->pen_down.at.y = y; | ||
1404 | map->pen_down.drag = FALSE; // don't assume drag yet | ||
1405 | |||
1406 | /* determine wether this press was on an item */ | ||
1407 | map->pen_down.on_item = map_real_item_at(map, x, y); | ||
1408 | |||
1409 | /* check if the clicked item is a highlighted node as the user */ | ||
1410 | /* might want to drag that */ | ||
1411 | map->pen_down.on_selected_node = FALSE; | ||
1412 | if(map->pen_down.on_item) | ||
1413 | map->pen_down.on_selected_node = | ||
1414 | map_item_is_selected_node(map, map->pen_down.on_item); | ||
1415 | |||
1416 | /* button press */ | ||
1417 | switch(map->action.type) { | ||
1418 | |||
1419 | case MAP_ACTION_WAY_NODE_ADD: | ||
1420 | map_edit_way_node_add_highlight(map, map->pen_down.on_item, x, y); | ||
1421 | break; | ||
1422 | |||
1423 | case MAP_ACTION_WAY_CUT: | ||
1424 | map_edit_way_cut_highlight(map, map->pen_down.on_item, x, y); | ||
1425 | break; | ||
1426 | |||
1427 | case MAP_ACTION_NODE_ADD: | ||
1428 | map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius); | ||
1429 | break; | ||
1430 | |||
1431 | case MAP_ACTION_WAY_ADD: | ||
1432 | map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius); | ||
1433 | map_touchnode_update(map->appdata, x, y); | ||
1434 | break; | ||
1435 | |||
1436 | default: | ||
1437 | break; | ||
1438 | } | ||
1439 | } | ||
1440 | |||
1441 | /* move the background image (wms data) during wms adjustment */ | ||
1442 | static void map_bg_adjust(map_t *map, gint x, gint y) { | ||
1443 | g_assert(map->appdata); | ||
1444 | g_assert(map->appdata->osm); | ||
1445 | g_assert(map->appdata->osm->bounds); | ||
1446 | |||
1447 | x += map->appdata->osm->bounds->min.x + map->bg.offset.x - | ||
1448 | map->pen_down.at.x; | ||
1449 | y += map->appdata->osm->bounds->min.y + map->bg.offset.y - | ||
1450 | map->pen_down.at.y; | ||
1451 | |||
1452 | canvas_image_move(map->bg.item, x, y, map->bg.scale.x, map->bg.scale.y); | ||
1453 | } | ||
1454 | |||
1455 | static void map_button_release(map_t *map, gint x, gint y) { | ||
1456 | map->pen_down.is = FALSE; | ||
1457 | |||
1458 | /* before button release is handled */ | ||
1459 | switch(map->action.type) { | ||
1460 | case MAP_ACTION_BG_ADJUST: | ||
1461 | map_bg_adjust(map, x, y); | ||
1462 | map->bg.offset.x += x - map->pen_down.at.x; | ||
1463 | map->bg.offset.y += y - map->pen_down.at.y; | ||
1464 | break; | ||
1465 | |||
1466 | case MAP_ACTION_IDLE: | ||
1467 | /* check if distance to press is above drag limit */ | ||
1468 | if(!map->pen_down.drag) | ||
1469 | map->pen_down.drag = distance_above(map, x, y, MAP_DRAG_LIMIT); | ||
1470 | |||
1471 | if(!map->pen_down.drag) { | ||
1472 | printf("left button released after click\n"); | ||
1473 | |||
1474 | map_item_t old_sel = map->selected; | ||
1475 | map_handle_click(map->appdata, map); | ||
1476 | |||
1477 | harbaum | 154 | if((old_sel.object.type != ILLEGAL) && |
1478 | (old_sel.object.type == map->selected.object.type) && | ||
1479 | (old_sel.object.ptr == map->selected.object.ptr)) { | ||
1480 | harbaum | 1 | printf("re-selected same item of type %d, " |
1481 | harbaum | 154 | "pushing it to the bottom\n", old_sel.object.type); |
1482 | harbaum | 1 | |
1483 | if(!map->selected.item) { | ||
1484 | printf(" item has no visible representation to push\n"); | ||
1485 | } else { | ||
1486 | canvas_item_to_bottom(map->selected.item); | ||
1487 | |||
1488 | /* update clicked item, to correctly handle the click */ | ||
1489 | map->pen_down.on_item = | ||
1490 | map_real_item_at(map, map->pen_down.at.x, map->pen_down.at.y); | ||
1491 | |||
1492 | map_handle_click(map->appdata, map); | ||
1493 | } | ||
1494 | } | ||
1495 | } else { | ||
1496 | printf("left button released after drag\n"); | ||
1497 | |||
1498 | /* just scroll if we didn't drag an selected item */ | ||
1499 | if(!map->pen_down.on_selected_node) | ||
1500 | map_do_scroll(map, x, y); | ||
1501 | else { | ||
1502 | printf("released after dragging node\n"); | ||
1503 | map_hl_cursor_clear(map); | ||
1504 | |||
1505 | /* now actually move the node */ | ||
1506 | map_edit_node_move(map->appdata, map->pen_down.on_item, x, y); | ||
1507 | } | ||
1508 | } | ||
1509 | break; | ||
1510 | |||
1511 | case MAP_ACTION_NODE_ADD: | ||
1512 | printf("released after NODE ADD\n"); | ||
1513 | map_hl_cursor_clear(map); | ||
1514 | |||
1515 | /* convert mouse position to canvas (world) position */ | ||
1516 | canvas_window2world(map->canvas, x, y, &x, &y); | ||
1517 | |||
1518 | node_t *node = NULL; | ||
1519 | if(!osm_position_within_bounds(map->appdata->osm, x, y)) | ||
1520 | map_outside_error(map->appdata); | ||
1521 | else { | ||
1522 | node = osm_node_new(map->appdata->osm, x, y); | ||
1523 | osm_node_attach(map->appdata->osm, node); | ||
1524 | map_node_draw(map, node); | ||
1525 | } | ||
1526 | map_action_set(map->appdata, MAP_ACTION_IDLE); | ||
1527 | |||
1528 | map_item_deselect(map->appdata); | ||
1529 | |||
1530 | if(node) { | ||
1531 | map_node_select(map->appdata, node); | ||
1532 | |||
1533 | /* let the user specify some tags for the new node */ | ||
1534 | info_dialog(GTK_WIDGET(map->appdata->window), map->appdata, NULL); | ||
1535 | } | ||
1536 | break; | ||
1537 | |||
1538 | case MAP_ACTION_WAY_ADD: | ||
1539 | printf("released after WAY ADD\n"); | ||
1540 | map_hl_cursor_clear(map); | ||
1541 | |||
1542 | map_edit_way_add_segment(map, x, y); | ||
1543 | break; | ||
1544 | |||
1545 | case MAP_ACTION_WAY_NODE_ADD: | ||
1546 | printf("released after WAY NODE ADD\n"); | ||
1547 | map_hl_cursor_clear(map); | ||
1548 | |||
1549 | map_edit_way_node_add(map, x, y); | ||
1550 | break; | ||
1551 | |||
1552 | case MAP_ACTION_WAY_CUT: | ||
1553 | printf("released after WAY CUT\n"); | ||
1554 | map_hl_cursor_clear(map); | ||
1555 | |||
1556 | map_edit_way_cut(map, x, y); | ||
1557 | break; | ||
1558 | |||
1559 | |||
1560 | default: | ||
1561 | break; | ||
1562 | } | ||
1563 | } | ||
1564 | |||
1565 | static gboolean map_button_event(GtkWidget *widget, GdkEventButton *event, | ||
1566 | gpointer data) { | ||
1567 | appdata_t *appdata = (appdata_t*)data; | ||
1568 | map_t *map = appdata->map; | ||
1569 | |||
1570 | if(!appdata->osm) return FALSE; | ||
1571 | |||
1572 | if(event->button == 1) { | ||
1573 | gint x = event->x, y = event->y; | ||
1574 | |||
1575 | if(event->type == GDK_BUTTON_PRESS) | ||
1576 | map_button_press(map, x, y); | ||
1577 | |||
1578 | if(event->type == GDK_BUTTON_RELEASE) | ||
1579 | map_button_release(map, x, y); | ||
1580 | } | ||
1581 | |||
1582 | return FALSE; /* forward to further processing */ | ||
1583 | } | ||
1584 | |||
1585 | static gboolean map_motion_notify_event(GtkWidget *widget, | ||
1586 | GdkEventMotion *event, gpointer data) { | ||
1587 | appdata_t *appdata = (appdata_t*)data; | ||
1588 | map_t *map = appdata->map; | ||
1589 | gint x, y; | ||
1590 | GdkModifierType state; | ||
1591 | |||
1592 | if(!appdata->osm) return FALSE; | ||
1593 | |||
1594 | #ifdef USE_HILDON | ||
1595 | /* reduce update frequency on hildon to keep screen update fluid */ | ||
1596 | static guint32 last_time = 0; | ||
1597 | |||
1598 | harbaum | 24 | if(event->time - last_time < 250) return FALSE; |
1599 | harbaum | 1 | last_time = event->time; |
1600 | #endif | ||
1601 | |||
1602 | if(!map->pen_down.is) | ||
1603 | return FALSE; | ||
1604 | |||
1605 | harbaum | 78 | #ifndef USE_GOOCANVAS |
1606 | harbaum | 1 | /* handle hints, hints are handled by goocanvas directly */ |
1607 | if(event->is_hint) | ||
1608 | gdk_window_get_pointer(event->window, &x, &y, &state); | ||
1609 | else | ||
1610 | #endif | ||
1611 | { | ||
1612 | x = event->x; | ||
1613 | y = event->y; | ||
1614 | state = event->state; | ||
1615 | } | ||
1616 | |||
1617 | /* check if distance to press is above drag limit */ | ||
1618 | if(!map->pen_down.drag) | ||
1619 | map->pen_down.drag = distance_above(map, x, y, MAP_DRAG_LIMIT); | ||
1620 | |||
1621 | /* drag */ | ||
1622 | switch(map->action.type) { | ||
1623 | case MAP_ACTION_BG_ADJUST: | ||
1624 | map_bg_adjust(map, x, y); | ||
1625 | break; | ||
1626 | |||
1627 | case MAP_ACTION_IDLE: | ||
1628 | if(map->pen_down.drag) { | ||
1629 | /* just scroll if we didn't drag an selected item */ | ||
1630 | if(!map->pen_down.on_selected_node) | ||
1631 | map_do_scroll(map, x, y); | ||
1632 | else { | ||
1633 | map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius); | ||
1634 | map_touchnode_update(appdata, x, y); | ||
1635 | } | ||
1636 | } | ||
1637 | break; | ||
1638 | |||
1639 | case MAP_ACTION_NODE_ADD: | ||
1640 | map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius); | ||
1641 | break; | ||
1642 | harbaum | 15 | |
1643 | harbaum | 1 | case MAP_ACTION_WAY_ADD: |
1644 | map_hl_cursor_draw(map, x, y, FALSE, map->style->node.radius); | ||
1645 | map_touchnode_update(appdata, x, y); | ||
1646 | break; | ||
1647 | |||
1648 | case MAP_ACTION_WAY_NODE_ADD: | ||
1649 | map_hl_cursor_clear(map); | ||
1650 | harbaum | 105 | map_item_t *item = map_item_at(map, x, y); |
1651 | harbaum | 1 | if(item) map_edit_way_node_add_highlight(map, item, x, y); |
1652 | break; | ||
1653 | |||
1654 | case MAP_ACTION_WAY_CUT: | ||
1655 | map_hl_cursor_clear(map); | ||
1656 | harbaum | 105 | item = map_item_at(map, x, y); |
1657 | harbaum | 1 | if(item) map_edit_way_cut_highlight(map, item, x, y); |
1658 | break; | ||
1659 | |||
1660 | default: | ||
1661 | break; | ||
1662 | } | ||
1663 | |||
1664 | |||
1665 | return FALSE; /* forward to further processing */ | ||
1666 | } | ||
1667 | |||
1668 | gboolean map_key_press_event(appdata_t *appdata, GdkEventKey *event) { | ||
1669 | |||
1670 | if(!appdata->osm) return FALSE; | ||
1671 | |||
1672 | /* map needs to be there to handle buttons */ | ||
1673 | if(!appdata->map->canvas) | ||
1674 | return FALSE; | ||
1675 | |||
1676 | if(event->type == GDK_KEY_PRESS) { | ||
1677 | gdouble zoom = 0; | ||
1678 | switch(event->keyval) { | ||
1679 | |||
1680 | case GDK_Left: | ||
1681 | map_do_scroll_step(appdata->map, -50, 0); | ||
1682 | break; | ||
1683 | |||
1684 | case GDK_Right: | ||
1685 | map_do_scroll_step(appdata->map, +50, 0); | ||
1686 | break; | ||
1687 | |||
1688 | case GDK_Up: | ||
1689 | map_do_scroll_step(appdata->map, 0, -50); | ||
1690 | break; | ||
1691 | |||
1692 | case GDK_Down: | ||
1693 | map_do_scroll_step(appdata->map, 0, +50); | ||
1694 | break; | ||
1695 | |||
1696 | case GDK_Return: // same as HILDON_HARDKEY_SELECT | ||
1697 | /* if the ok button is enabled, call its function */ | ||
1698 | if(GTK_WIDGET_FLAGS(appdata->iconbar->ok) & GTK_SENSITIVE) | ||
1699 | map_action_ok(appdata); | ||
1700 | /* otherwise if info is enabled call that */ | ||
1701 | else if(GTK_WIDGET_FLAGS(appdata->iconbar->info) & GTK_SENSITIVE) | ||
1702 | info_dialog(GTK_WIDGET(appdata->window), appdata, NULL); | ||
1703 | break; | ||
1704 | |||
1705 | case GDK_Escape: // same as HILDON_HARDKEY_ESC | ||
1706 | /* if the cancel button is enabled, call its function */ | ||
1707 | if(GTK_WIDGET_FLAGS(appdata->iconbar->cancel) & GTK_SENSITIVE) | ||
1708 | map_action_cancel(appdata); | ||
1709 | break; | ||
1710 | |||
1711 | #ifdef USE_HILDON | ||
1712 | case HILDON_HARDKEY_INCREASE: | ||
1713 | #else | ||
1714 | case '+': | ||
1715 | #endif | ||
1716 | zoom = appdata->map->state->zoom; | ||
1717 | zoom *= ZOOM_FACTOR_BUTTON; | ||
1718 | map_set_zoom(appdata->map, zoom, TRUE); | ||
1719 | printf("zoom is now %f (1:%d)\n", zoom, (int)zoom_to_scaledn(zoom)); | ||
1720 | return TRUE; | ||
1721 | break; | ||
1722 | |||
1723 | #ifdef USE_HILDON | ||
1724 | case HILDON_HARDKEY_DECREASE: | ||
1725 | #else | ||
1726 | case '-': | ||
1727 | #endif | ||
1728 | zoom = appdata->map->state->zoom; | ||
1729 | zoom /= ZOOM_FACTOR_BUTTON; | ||
1730 | map_set_zoom(appdata->map, zoom, TRUE); | ||
1731 | printf("zoom is now %f (1:%d)\n", zoom, (int)zoom_to_scaledn(zoom)); | ||
1732 | return TRUE; | ||
1733 | break; | ||
1734 | |||
1735 | default: | ||
1736 | printf("key event %d\n", event->keyval); | ||
1737 | break; | ||
1738 | } | ||
1739 | } | ||
1740 | return FALSE; | ||
1741 | } | ||
1742 | |||
1743 | GtkWidget *map_new(appdata_t *appdata) { | ||
1744 | map_t *map = appdata->map = g_new0(map_t, 1); | ||
1745 | |||
1746 | map->style = style_load(appdata, appdata->settings->style); | ||
1747 | harbaum | 22 | if(!map->style) { |
1748 | errorf(NULL, _("Unable to load valid style, terminating.")); | ||
1749 | g_free(map); | ||
1750 | return NULL; | ||
1751 | } | ||
1752 | harbaum | 1 | |
1753 | if(appdata->project && appdata->project->map_state) { | ||
1754 | printf("Using projects map state\n"); | ||
1755 | map->state = appdata->project->map_state; | ||
1756 | } else { | ||
1757 | printf("Creating new map state\n"); | ||
1758 | map->state = g_new0(map_state_t, 1); | ||
1759 | map->state->zoom = 0.25; | ||
1760 | } | ||
1761 | |||
1762 | map->state->refcount++; | ||
1763 | |||
1764 | map->pen_down.at.x = -1; | ||
1765 | map->pen_down.at.y = -1; | ||
1766 | map->appdata = appdata; | ||
1767 | map->action.type = MAP_ACTION_IDLE; | ||
1768 | |||
1769 | harbaum | 103 | map->canvas = canvas_new(map->style->background.color); |
1770 | canvas_set_antialias(map->canvas, !appdata->settings->no_antialias); | ||
1771 | harbaum | 1 | |
1772 | harbaum | 103 | GtkWidget *canvas_widget = canvas_get_widget(map->canvas); |
1773 | harbaum | 1 | |
1774 | harbaum | 103 | gtk_widget_set_events(canvas_widget, |
1775 | harbaum | 1 | GDK_BUTTON_PRESS_MASK |
1776 | | GDK_BUTTON_RELEASE_MASK | ||
1777 | | GDK_SCROLL_MASK | ||
1778 | | GDK_POINTER_MOTION_MASK | ||
1779 | | GDK_POINTER_MOTION_HINT_MASK); | ||
1780 | |||
1781 | harbaum | 103 | gtk_signal_connect(GTK_OBJECT(canvas_widget), |
1782 | harbaum | 1 | "button_press_event", G_CALLBACK(map_button_event), appdata); |
1783 | harbaum | 103 | gtk_signal_connect(GTK_OBJECT(canvas_widget), |
1784 | harbaum | 1 | "button_release_event", G_CALLBACK(map_button_event), appdata); |
1785 | harbaum | 103 | gtk_signal_connect(GTK_OBJECT(canvas_widget), |
1786 | harbaum | 1 | "motion_notify_event", G_CALLBACK(map_motion_notify_event), appdata); |
1787 | harbaum | 103 | gtk_signal_connect(GTK_OBJECT(canvas_widget), |
1788 | harbaum | 1 | "scroll_event", G_CALLBACK(map_scroll_event), appdata); |
1789 | |||
1790 | harbaum | 103 | gtk_signal_connect(GTK_OBJECT(canvas_widget), |
1791 | harbaum | 1 | "destroy", G_CALLBACK(map_destroy_event), appdata); |
1792 | |||
1793 | harbaum | 103 | return canvas_widget; |
1794 | harbaum | 1 | } |
1795 | |||
1796 | void map_init(appdata_t *appdata) { | ||
1797 | map_t *map = appdata->map; | ||
1798 | |||
1799 | /* set initial zoom */ | ||
1800 | map_set_zoom(map, map->state->zoom, FALSE); | ||
1801 | josm_elemstyles_colorize_world(map->style, appdata->osm); | ||
1802 | |||
1803 | map_draw(map, appdata->osm); | ||
1804 | |||
1805 | float mult = appdata->map->style->frisket.mult; | ||
1806 | canvas_set_bounds(map->canvas, | ||
1807 | mult*appdata->osm->bounds->min.x, | ||
1808 | mult*appdata->osm->bounds->min.y, | ||
1809 | mult*appdata->osm->bounds->max.x, | ||
1810 | mult*appdata->osm->bounds->max.y); | ||
1811 | |||
1812 | harbaum | 103 | printf("restore scroll position %d/%d\n", |
1813 | harbaum | 1 | map->state->scroll_offset.x, map->state->scroll_offset.y); |
1814 | |||
1815 | harbaum | 103 | map_limit_scroll(map, CANVAS_UNIT_METER, |
1816 | &map->state->scroll_offset.x, &map->state->scroll_offset.y); | ||
1817 | canvas_scroll_to(map->canvas, CANVAS_UNIT_METER, | ||
1818 | map->state->scroll_offset.x, map->state->scroll_offset.y); | ||
1819 | harbaum | 1 | } |
1820 | |||
1821 | |||
1822 | harbaum | 103 | void map_clear(appdata_t *appdata, gint group_mask) { |
1823 | harbaum | 1 | map_t *map = appdata->map; |
1824 | |||
1825 | printf("freeing map contents\n"); | ||
1826 | |||
1827 | map_free_map_item_chains(appdata); | ||
1828 | |||
1829 | /* remove a possibly existing highlight */ | ||
1830 | map_item_deselect(appdata); | ||
1831 | |||
1832 | harbaum | 103 | canvas_erase(map->canvas, group_mask); |
1833 | harbaum | 1 | } |
1834 | |||
1835 | void map_paint(appdata_t *appdata) { | ||
1836 | map_t *map = appdata->map; | ||
1837 | |||
1838 | harbaum | 78 | /* user may have changed antialias settings */ |
1839 | harbaum | 103 | canvas_set_antialias(map->canvas, !appdata->settings->no_antialias); |
1840 | harbaum | 24 | |
1841 | harbaum | 1 | josm_elemstyles_colorize_world(map->style, appdata->osm); |
1842 | map_draw(map, appdata->osm); | ||
1843 | } | ||
1844 | |||
1845 | /* called from several icons like e.g. "node_add" */ | ||
1846 | void map_action_set(appdata_t *appdata, map_action_t action) { | ||
1847 | printf("map action set to %d\n", action); | ||
1848 | |||
1849 | appdata->map->action.type = action; | ||
1850 | |||
1851 | /* enable/disable ok/cancel buttons */ | ||
1852 | // MAP_ACTION_IDLE=0, NODE_ADD, BG_ADJUST, WAY_ADD, WAY_NODE_ADD, WAY_CUT | ||
1853 | const gboolean ok_state[] = { FALSE, FALSE, TRUE, FALSE, FALSE, FALSE }; | ||
1854 | const gboolean cancel_state[] = { FALSE, TRUE, TRUE, TRUE, TRUE, TRUE }; | ||
1855 | |||
1856 | g_assert(MAP_ACTION_NUM == sizeof(ok_state)/sizeof(gboolean)); | ||
1857 | g_assert(action < sizeof(ok_state)/sizeof(gboolean)); | ||
1858 | |||
1859 | icon_bar_map_cancel_ok(appdata, cancel_state[action], ok_state[action]); | ||
1860 | |||
1861 | switch(action) { | ||
1862 | case MAP_ACTION_BG_ADJUST: | ||
1863 | /* an existing selection only causes confusion ... */ | ||
1864 | map_item_deselect(appdata); | ||
1865 | break; | ||
1866 | |||
1867 | case MAP_ACTION_WAY_ADD: | ||
1868 | printf("starting new way\n"); | ||
1869 | |||
1870 | /* remember if there was a way selected */ | ||
1871 | way_t *way_sel = NULL; | ||
1872 | harbaum | 154 | if(appdata->map->selected.object.type == WAY) |
1873 | way_sel = appdata->map->selected.object.way; | ||
1874 | harbaum | 1 | |
1875 | map_item_deselect(appdata); | ||
1876 | map_edit_way_add_begin(appdata->map, way_sel); | ||
1877 | break; | ||
1878 | |||
1879 | case MAP_ACTION_NODE_ADD: | ||
1880 | map_item_deselect(appdata); | ||
1881 | break; | ||
1882 | |||
1883 | default: | ||
1884 | break; | ||
1885 | } | ||
1886 | |||
1887 | icon_bar_map_action_idle(appdata, action == MAP_ACTION_IDLE); | ||
1888 | gtk_widget_set_sensitive(appdata->menu_item_wms_adjust, | ||
1889 | action == MAP_ACTION_IDLE); | ||
1890 | |||
1891 | const char *str_state[] = { | ||
1892 | NULL, | ||
1893 | _("Place a node"), | ||
1894 | _("Adjust background image position"), | ||
1895 | _("Place first node of new way"), | ||
1896 | _("Place node on selected way"), | ||
1897 | _("Select segment to cut way"), | ||
1898 | }; | ||
1899 | |||
1900 | g_assert(MAP_ACTION_NUM == sizeof(str_state)/sizeof(char*)); | ||
1901 | |||
1902 | statusbar_set(appdata, str_state[action], FALSE); | ||
1903 | } | ||
1904 | |||
1905 | |||
1906 | void map_action_cancel(appdata_t *appdata) { | ||
1907 | map_t *map = appdata->map; | ||
1908 | |||
1909 | switch(map->action.type) { | ||
1910 | case MAP_ACTION_WAY_ADD: | ||
1911 | map_edit_way_add_cancel(map); | ||
1912 | break; | ||
1913 | |||
1914 | case MAP_ACTION_BG_ADJUST: | ||
1915 | /* undo all changes to bg_offset */ | ||
1916 | map->bg.offset.x = appdata->project->wms_offset.x; | ||
1917 | map->bg.offset.y = appdata->project->wms_offset.y; | ||
1918 | |||
1919 | gint x = appdata->osm->bounds->min.x + map->bg.offset.x; | ||
1920 | gint y = appdata->osm->bounds->min.y + map->bg.offset.y; | ||
1921 | canvas_image_move(map->bg.item, x, y, map->bg.scale.x, map->bg.scale.y); | ||
1922 | break; | ||
1923 | |||
1924 | default: | ||
1925 | break; | ||
1926 | } | ||
1927 | |||
1928 | map_action_set(appdata, MAP_ACTION_IDLE); | ||
1929 | } | ||
1930 | |||
1931 | void map_action_ok(appdata_t *appdata) { | ||
1932 | map_t *map = appdata->map; | ||
1933 | |||
1934 | /* reset action now as this erases the statusbar and some */ | ||
1935 | /* of the actions may set it */ | ||
1936 | map_action_t type = map->action.type; | ||
1937 | map_action_set(appdata, MAP_ACTION_IDLE); | ||
1938 | |||
1939 | switch(type) { | ||
1940 | case MAP_ACTION_WAY_ADD: | ||
1941 | map_edit_way_add_ok(map); | ||
1942 | break; | ||
1943 | |||
1944 | case MAP_ACTION_BG_ADJUST: | ||
1945 | /* save changes to bg_offset in project */ | ||
1946 | appdata->project->wms_offset.x = map->bg.offset.x; | ||
1947 | appdata->project->wms_offset.y = map->bg.offset.y; | ||
1948 | appdata->project->dirty = TRUE; | ||
1949 | break; | ||
1950 | |||
1951 | default: | ||
1952 | break; | ||
1953 | } | ||
1954 | } | ||
1955 | |||
1956 | /* called from icon "trash" */ | ||
1957 | void map_delete_selected(appdata_t *appdata) { | ||
1958 | map_t *map = appdata->map; | ||
1959 | |||
1960 | if(!yes_no_f(GTK_WIDGET(appdata->window), | ||
1961 | appdata, MISC_AGAIN_ID_DELETE, MISC_AGAIN_FLAG_DONT_SAVE_NO, | ||
1962 | _("Delete selected object?"), | ||
1963 | _("Do you really want to delete the selected object?"))) | ||
1964 | return; | ||
1965 | |||
1966 | /* work on local copy since de-selecting destroys the selection */ | ||
1967 | map_item_t item = map->selected; | ||
1968 | |||
1969 | /* deleting the selected item de-selects it ... */ | ||
1970 | map_item_deselect(appdata); | ||
1971 | |||
1972 | harbaum | 155 | undo_remember_delete(appdata, &item.object); |
1973 | harbaum | 54 | |
1974 | harbaum | 154 | switch(item.object.type) { |
1975 | case NODE: | ||
1976 | harbaum | 161 | printf("request to delete node #" ITEM_ID_FORMAT "\n", |
1977 | item.object.node->id); | ||
1978 | harbaum | 1 | |
1979 | /* check if this node is part of a way with two nodes only. */ | ||
1980 | /* we cannot delete this as this would also delete the way */ | ||
1981 | harbaum | 154 | way_chain_t *way_chain = osm_node_to_way(appdata->osm, item.object.node); |
1982 | harbaum | 1 | if(way_chain) { |
1983 | gboolean short_way = FALSE; | ||
1984 | |||
1985 | /* free the chain of ways */ | ||
1986 | while(way_chain && !short_way) { | ||
1987 | way_chain_t *next = way_chain->next; | ||
1988 | |||
1989 | if(osm_way_number_of_nodes(way_chain->way) <= 2) | ||
1990 | short_way = TRUE; | ||
1991 | |||
1992 | g_free(way_chain); | ||
1993 | way_chain = next; | ||
1994 | } | ||
1995 | |||
1996 | if(short_way) { | ||
1997 | if(!yes_no_f(GTK_WIDGET(appdata->window), NULL, 0, 0, | ||
1998 | _("Delete node in short way(s)?"), | ||
1999 | _("Deleting this node will also delete one or more ways " | ||
2000 | "since they'll contain only one node afterwards. " | ||
2001 | "Do you really want this?"))) | ||
2002 | return; | ||
2003 | } | ||
2004 | } | ||
2005 | |||
2006 | /* remove it visually from the screen */ | ||
2007 | harbaum | 154 | map_item_chain_destroy(&item.object.node->map_item_chain); |
2008 | harbaum | 1 | |
2009 | /* and mark it "deleted" in the database */ | ||
2010 | harbaum | 154 | osm_node_remove_from_relation(appdata->osm, item.object.node); |
2011 | harbaum | 1 | way_chain_t *chain = osm_node_delete(appdata->osm, |
2012 | harbaum | 154 | &appdata->icon, item.object.node, FALSE, TRUE); |
2013 | harbaum | 1 | |
2014 | /* redraw all affected ways */ | ||
2015 | while(chain) { | ||
2016 | way_chain_t *next = chain->next; | ||
2017 | |||
2018 | if(osm_way_number_of_nodes(chain->way) == 1) { | ||
2019 | /* this way now only contains one node and thus isn't a valid */ | ||
2020 | /* way anymore. So it'll also get deleted (which in turn may */ | ||
2021 | /* cause other nodes to be deleted as well) */ | ||
2022 | map_way_delete(appdata, chain->way); | ||
2023 | } else { | ||
2024 | map_item_t item; | ||
2025 | harbaum | 154 | item.object.type = WAY; |
2026 | item.object.way = chain->way; | ||
2027 | harbaum | 1 | map_item_redraw(appdata, &item); |
2028 | } | ||
2029 | |||
2030 | g_free(chain); | ||
2031 | |||
2032 | chain = next; | ||
2033 | } | ||
2034 | |||
2035 | break; | ||
2036 | |||
2037 | harbaum | 154 | case WAY: |
2038 | harbaum | 161 | printf("request to delete way #" ITEM_ID_FORMAT "\n", item.object.way->id); |
2039 | harbaum | 154 | map_way_delete(appdata, item.object.way); |
2040 | harbaum | 1 | break; |
2041 | |||
2042 | default: | ||
2043 | harbaum | 154 | g_assert((item.object.type == NODE) || |
2044 | (item.object.type == WAY)); | ||
2045 | harbaum | 1 | break; |
2046 | } | ||
2047 | } | ||
2048 | |||
2049 | /* ----------------------- track related stuff ----------------------- */ | ||
2050 | |||
2051 | harbaum | 156 | static gboolean track_pos2lpos(bounds_t *bounds, pos_t *pos, lpos_t *lpos) { |
2052 | pos2lpos(bounds, pos, lpos); | ||
2053 | |||
2054 | /* check if point is within bounds */ | ||
2055 | return ((lpos->x >= bounds->min.x) && (lpos->x <= bounds->max.x) && | ||
2056 | (lpos->y >= bounds->min.y) && (lpos->y <= bounds->max.y)); | ||
2057 | } | ||
2058 | |||
2059 | harbaum | 1 | void map_track_draw_seg(map_t *map, track_seg_t *seg) { |
2060 | harbaum | 156 | bounds_t *bounds = map->appdata->osm->bounds; |
2061 | |||
2062 | harbaum | 1 | /* a track_seg needs at least 2 points to be drawn */ |
2063 | guint pnum = track_seg_points(seg); | ||
2064 | printf("seg of length %d\n", pnum); | ||
2065 | |||
2066 | harbaum | 156 | if(!pnum) |
2067 | return; | ||
2068 | harbaum | 1 | |
2069 | harbaum | 156 | /* nothing should have been drawn by now ... */ |
2070 | g_assert(!seg->item_chain); | ||
2071 | |||
2072 | track_item_chain_t **itemP = &seg->item_chain; | ||
2073 | track_point_t *track_point = seg->track_point; | ||
2074 | while(track_point) { | ||
2075 | lpos_t lpos; | ||
2076 | harbaum | 1 | |
2077 | harbaum | 156 | /* skip all points not on screen */ |
2078 | track_point_t *last = NULL; | ||
2079 | while(track_point && !track_pos2lpos(bounds, &track_point->pos, &lpos)) { | ||
2080 | last = track_point; | ||
2081 | track_point = track_point->next; | ||
2082 | } | ||
2083 | |||
2084 | int visible = 0; | ||
2085 | |||
2086 | /* count nodes that _are_ on screen */ | ||
2087 | track_point_t *tmp = track_point; | ||
2088 | while(tmp && track_pos2lpos(bounds, &tmp->pos, &lpos)) { | ||
2089 | tmp = tmp->next; | ||
2090 | visible++; | ||
2091 | } | ||
2092 | |||
2093 | /* actually start drawing with the last position that was offscreen */ | ||
2094 | /* so the track nicely enters the viewing area */ | ||
2095 | if(last) { | ||
2096 | track_point = last; | ||
2097 | visible++; | ||
2098 | } | ||
2099 | |||
2100 | /* also use last one that's offscreen to nicely leave the visible area */ | ||
2101 | if(tmp && tmp->next) | ||
2102 | visible++; | ||
2103 | |||
2104 | harbaum | 1 | /* allocate space for nodes */ |
2105 | harbaum | 156 | canvas_points_t *points = canvas_points_new(visible); |
2106 | harbaum | 1 | |
2107 | harbaum | 156 | printf("visible are %d\n", visible); |
2108 | int point; | ||
2109 | for(point=0;point<visible;point++) { | ||
2110 | track_pos2lpos(bounds, &track_point->pos, &lpos); | ||
2111 | |||
2112 | points->coords[2*point+0] = lpos.x; | ||
2113 | points->coords[2*point+1] = lpos.y; | ||
2114 | harbaum | 1 | track_point = track_point->next; |
2115 | } | ||
2116 | |||
2117 | harbaum | 156 | *itemP = g_new0(track_item_chain_t, 1); |
2118 | (*itemP)->item = canvas_polyline_new(map->canvas, CANVAS_GROUP_TRACK, | ||
2119 | points, map->style->track.width, map->style->track.color); | ||
2120 | itemP = &(*itemP)->next; | ||
2121 | harbaum | 1 | |
2122 | canvas_points_free(points); | ||
2123 | } | ||
2124 | } | ||
2125 | |||
2126 | harbaum | 156 | /* update the last visible fragment of this segment since a */ |
2127 | /* gps position may have been added */ | ||
2128 | harbaum | 1 | void map_track_update_seg(map_t *map, track_seg_t *seg) { |
2129 | harbaum | 156 | bounds_t *bounds = map->appdata->osm->bounds; |
2130 | |||
2131 | printf("-- APPENDING TO TRACK --\n"); | ||
2132 | |||
2133 | harbaum | 1 | /* a track_seg needs at least 2 points to be drawn */ |
2134 | guint pnum = track_seg_points(seg); | ||
2135 | printf("seg of length %d\n", pnum); | ||
2136 | |||
2137 | harbaum | 156 | /* there are two cases: either the second last point was on screen */ |
2138 | /* or it wasn't. We'll have to start a new screen item if the latter */ | ||
2139 | /* is the case */ | ||
2140 | |||
2141 | /* search last point */ | ||
2142 | track_point_t *begin = seg->track_point, *second_last = seg->track_point; | ||
2143 | lpos_t lpos; | ||
2144 | while(second_last && second_last->next && second_last->next->next) { | ||
2145 | if(!track_pos2lpos(bounds, &second_last->pos, &lpos)) | ||
2146 | begin = second_last; | ||
2147 | |||
2148 | second_last = second_last->next; | ||
2149 | } | ||
2150 | track_point_t *last = second_last->next; | ||
2151 | |||
2152 | /* since we are updating an existing track, it sure has at least two */ | ||
2153 | /* points, second_last must be valid and its "next" (last) also */ | ||
2154 | g_assert(second_last); | ||
2155 | g_assert(last); | ||
2156 | |||
2157 | /* check if the last and second_last points are visible */ | ||
2158 | gboolean last_is_visible = | ||
2159 | track_pos2lpos(bounds, &last->pos, &lpos); | ||
2160 | gboolean second_last_is_visible = | ||
2161 | track_pos2lpos(bounds, &second_last->pos, &lpos); | ||
2162 | |||
2163 | /* if both are invisible, then nothing has changed on screen */ | ||
2164 | if(!last_is_visible && !second_last_is_visible) { | ||
2165 | printf("second_last and last entry are invisible -> doing nothing\n"); | ||
2166 | return; | ||
2167 | } | ||
2168 | |||
2169 | /* search last element in item chain */ | ||
2170 | track_item_chain_t *item = seg->item_chain; | ||
2171 | while(item && item->next) | ||
2172 | item = item->next; | ||
2173 | |||
2174 | if(second_last_is_visible) { | ||
2175 | /* there must be something already on the screen and there must */ | ||
2176 | /* be visible nodes in the chain */ | ||
2177 | g_assert(item); | ||
2178 | g_assert(begin); | ||
2179 | |||
2180 | printf("second_last is visible -> append\n"); | ||
2181 | harbaum | 1 | |
2182 | harbaum | 156 | /* count points to be placed */ |
2183 | int npoints = 0; | ||
2184 | track_point_t *tmp = begin; | ||
2185 | while(tmp) { | ||
2186 | tmp = tmp->next; | ||
2187 | npoints++; | ||
2188 | } | ||
2189 | |||
2190 | printf("updating last segment to %d points\n", npoints); | ||
2191 | |||
2192 | canvas_points_t *points = canvas_points_new(npoints); | ||
2193 | |||
2194 | gint point = 0; | ||
2195 | while(begin) { | ||
2196 | track_pos2lpos(bounds, &begin->pos, &lpos); | ||
2197 | canvas_point_set_pos(points, point++, &lpos); | ||
2198 | begin = begin->next; | ||
2199 | } | ||
2200 | harbaum | 1 | |
2201 | harbaum | 156 | canvas_item_set_points(item->item, points); |
2202 | canvas_points_free(points); | ||
2203 | |||
2204 | } else { | ||
2205 | printf("second last is invisible -> start new screen segment\n"); | ||
2206 | |||
2207 | /* the search for the "begin" ends with the second_last item */ | ||
2208 | /* verify the last one also */ | ||
2209 | if(begin->next && !track_pos2lpos(bounds, &begin->next->pos, &lpos)) | ||
2210 | begin = begin->next; | ||
2211 | |||
2212 | item->next = g_new0(track_item_chain_t, 1); | ||
2213 | item = item->next; | ||
2214 | |||
2215 | /* count points to be placed */ | ||
2216 | int npoints = 0; | ||
2217 | track_point_t *tmp = begin; | ||
2218 | while(tmp) { | ||
2219 | tmp = tmp->next; | ||
2220 | npoints++; | ||
2221 | harbaum | 1 | } |
2222 | harbaum | 156 | |
2223 | printf("attaching new segment with %d points\n", npoints); | ||
2224 | |||
2225 | canvas_points_t *points = canvas_points_new(npoints); | ||
2226 | |||
2227 | gint point = 0; | ||
2228 | while(begin) { | ||
2229 | track_pos2lpos(bounds, &begin->pos, &lpos); | ||
2230 | canvas_point_set_pos(points, point++, &lpos); | ||
2231 | begin = begin->next; | ||
2232 | } | ||
2233 | harbaum | 1 | |
2234 | harbaum | 156 | item->item = canvas_polyline_new(map->canvas, CANVAS_GROUP_TRACK, |
2235 | points, map->style->track.width, map->style->track.color); | ||
2236 | |||
2237 | harbaum | 1 | canvas_points_free(points); |
2238 | } | ||
2239 | harbaum | 156 | |
2240 | harbaum | 1 | } |
2241 | |||
2242 | void map_track_draw(map_t *map, track_t *track) { | ||
2243 | track_seg_t *seg = track->track_seg; | ||
2244 | |||
2245 | /* draw all segments */ | ||
2246 | while(seg) { | ||
2247 | map_track_draw_seg(map, seg); | ||
2248 | seg = seg->next; | ||
2249 | } | ||
2250 | } | ||
2251 | |||
2252 | void map_track_remove(appdata_t *appdata) { | ||
2253 | track_t *track = appdata->track.track; | ||
2254 | |||
2255 | printf("removing track\n"); | ||
2256 | |||
2257 | g_assert(track); | ||
2258 | |||
2259 | /* remove all segments */ | ||
2260 | track_seg_t *seg = track->track_seg; | ||
2261 | while(seg) { | ||
2262 | harbaum | 156 | track_item_chain_t *item = seg->item_chain; |
2263 | while(item) { | ||
2264 | track_item_chain_t *next = item->next; | ||
2265 | canvas_item_destroy(item->item); | ||
2266 | item = next; | ||
2267 | harbaum | 1 | } |
2268 | harbaum | 156 | |
2269 | seg->item_chain = NULL; | ||
2270 | harbaum | 1 | seg = seg->next; |
2271 | } | ||
2272 | } | ||
2273 | |||
2274 | harbaum | 156 | void map_track_pos(appdata_t *appdata, pos_t *pos) { |
2275 | harbaum | 1 | if(appdata->track.gps_item) { |
2276 | canvas_item_destroy(appdata->track.gps_item); | ||
2277 | appdata->track.gps_item = NULL; | ||
2278 | } | ||
2279 | |||
2280 | harbaum | 156 | if(pos) { |
2281 | lpos_t lpos; | ||
2282 | pos2lpos(appdata->osm->bounds, pos, &lpos); | ||
2283 | |||
2284 | harbaum | 103 | appdata->track.gps_item = |
2285 | canvas_circle_new(appdata->map->canvas, CANVAS_GROUP_GPS, | ||
2286 | harbaum | 156 | lpos.x, lpos.y, appdata->map->style->track.width/2.0, 0, |
2287 | harbaum | 22 | appdata->map->style->track.gps_color, NO_COLOR); |
2288 | harbaum | 156 | } |
2289 | harbaum | 1 | } |
2290 | |||
2291 | /* ------------------- map background ------------------ */ | ||
2292 | |||
2293 | void map_remove_bg_image(map_t *map) { | ||
2294 | if(!map) return; | ||
2295 | |||
2296 | if(map->bg.item) { | ||
2297 | canvas_item_destroy(map->bg.item); | ||
2298 | map->bg.item = NULL; | ||
2299 | } | ||
2300 | } | ||
2301 | |||
2302 | static gint map_bg_item_destroy_event(GtkWidget *widget, gpointer data) { | ||
2303 | map_t *map = (map_t*)data; | ||
2304 | |||
2305 | /* destroying background item */ | ||
2306 | |||
2307 | map->bg.item = NULL; | ||
2308 | if(map->bg.pix) { | ||
2309 | printf("destroying background item\n"); | ||
2310 | gdk_pixbuf_unref(map->bg.pix); | ||
2311 | map->bg.pix = NULL; | ||
2312 | } | ||
2313 | return FALSE; | ||
2314 | } | ||
2315 | |||
2316 | void map_set_bg_image(map_t *map, char *filename) { | ||
2317 | bounds_t *bounds = map->appdata->osm->bounds; | ||
2318 | |||
2319 | map_remove_bg_image(map); | ||
2320 | |||
2321 | map->bg.pix = gdk_pixbuf_new_from_file(filename, NULL); | ||
2322 | |||
2323 | /* calculate required scale factor */ | ||
2324 | map->bg.scale.x = (float)(bounds->max.x - bounds->min.x)/ | ||
2325 | (float)gdk_pixbuf_get_width(map->bg.pix); | ||
2326 | map->bg.scale.y = (float)(bounds->max.y - bounds->min.y)/ | ||
2327 | (float)gdk_pixbuf_get_height(map->bg.pix); | ||
2328 | |||
2329 | harbaum | 103 | map->bg.item = canvas_image_new(map->canvas, CANVAS_GROUP_BG, map->bg.pix, |
2330 | harbaum | 1 | bounds->min.x, bounds->min.y, map->bg.scale.x, map->bg.scale.y); |
2331 | |||
2332 | canvas_item_destroy_connect(map->bg.item, | ||
2333 | G_CALLBACK(map_bg_item_destroy_event), map); | ||
2334 | } | ||
2335 | |||
2336 | |||
2337 | /* -------- hide and show objects (for performance reasons) ------- */ | ||
2338 | |||
2339 | void map_hide_selected(appdata_t *appdata) { | ||
2340 | map_t *map = appdata->map; | ||
2341 | if(!map) return; | ||
2342 | |||
2343 | harbaum | 154 | if(map->selected.object.type != WAY) { |
2344 | harbaum | 1 | printf("selected item is not a way\n"); |
2345 | return; | ||
2346 | } | ||
2347 | |||
2348 | harbaum | 154 | way_t *way = map->selected.object.way; |
2349 | harbaum | 161 | printf("hiding way #" ITEM_ID_FORMAT "\n", way->id); |
2350 | harbaum | 1 | |
2351 | map_item_deselect(appdata); | ||
2352 | way->flags |= OSM_FLAG_HIDDEN; | ||
2353 | map_item_chain_destroy(&way->map_item_chain); | ||
2354 | |||
2355 | gtk_widget_set_sensitive(appdata->menu_item_map_show_all, TRUE); | ||
2356 | } | ||
2357 | |||
2358 | void map_show_all(appdata_t *appdata) { | ||
2359 | map_t *map = appdata->map; | ||
2360 | if(!map) return; | ||
2361 | |||
2362 | way_t *way = appdata->osm->way; | ||
2363 | while(way) { | ||
2364 | if(way->flags & OSM_FLAG_HIDDEN) { | ||
2365 | way->flags &= ~OSM_FLAG_HIDDEN; | ||
2366 | map_way_draw(map, way); | ||
2367 | } | ||
2368 | |||
2369 | way = way->next; | ||
2370 | } | ||
2371 | |||
2372 | gtk_widget_set_sensitive(appdata->menu_item_map_show_all, FALSE); | ||
2373 | } | ||
2374 | harbaum | 153 | |
2375 | achadwick | 13 | // vim:et:ts=8:sw=2:sts=2:ai |