shumate/vector/shumate-vector-symbol-container.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (C) 2021 James Westman <james@jwestman.net> | ||
| 3 | * | ||
| 4 | * This library is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU Lesser General Public | ||
| 6 | * License as published by the Free Software Foundation; either | ||
| 7 | * version 2.1 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This library is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | * Lesser General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU Lesser General Public | ||
| 15 | * License along with this library; if not, see <https://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include "shumate-vector-symbol-container-private.h" | ||
| 19 | #include "shumate-vector-symbol-private.h" | ||
| 20 | #include "shumate-vector-collision-private.h" | ||
| 21 | #include "shumate-symbol-event-private.h" | ||
| 22 | #include "shumate-profiling-private.h" | ||
| 23 | #include "shumate-inspector-settings-private.h" | ||
| 24 | |||
| 25 | typedef struct { | ||
| 26 | int layer_idx; | ||
| 27 | GPtrArray *symbols; | ||
| 28 | } LayerBucket; | ||
| 29 | |||
| 30 | struct _ShumateVectorSymbolContainer | ||
| 31 | { | ||
| 32 | ShumateLayer parent_instance; | ||
| 33 | |||
| 34 | ShumateMapSource *map_source; | ||
| 35 | |||
| 36 | GPtrArray *layer_buckets; | ||
| 37 | ShumateVectorCollision *collision; | ||
| 38 | |||
| 39 | int child_count, visible_count; | ||
| 40 | |||
| 41 | double last_rotation; | ||
| 42 | double last_zoom; | ||
| 43 | double last_center_x, last_center_y; | ||
| 44 | double last_width, last_height; | ||
| 45 | gboolean labels_changed : 1; | ||
| 46 | }; | ||
| 47 | |||
| 48 | ✗ | G_DEFINE_TYPE (ShumateVectorSymbolContainer, shumate_vector_symbol_container, SHUMATE_TYPE_LAYER) | |
| 49 | |||
| 50 | enum { | ||
| 51 | PROP_0, | ||
| 52 | PROP_MAP_SOURCE, | ||
| 53 | N_PROPS, | ||
| 54 | }; | ||
| 55 | |||
| 56 | static GParamSpec *obj_properties[N_PROPS] = { NULL, }; | ||
| 57 | |||
| 58 | enum | ||
| 59 | { | ||
| 60 | SYMBOL_CLICKED, | ||
| 61 | LAST_SIGNAL | ||
| 62 | }; | ||
| 63 | |||
| 64 | static guint signals[LAST_SIGNAL] = { 0, }; | ||
| 65 | |||
| 66 | |||
| 67 | typedef struct { | ||
| 68 | graphene_rect_t bounds; | ||
| 69 | |||
| 70 | // do not need to be freed because they're owned by the widget | ||
| 71 | ShumateVectorSymbol *symbol; | ||
| 72 | ShumateVectorSymbolInfo *symbol_info; | ||
| 73 | |||
| 74 | // These are coordinates [0, 1) within the tile | ||
| 75 | double x; | ||
| 76 | double y; | ||
| 77 | |||
| 78 | int tile_x; | ||
| 79 | int tile_y; | ||
| 80 | int zoom; | ||
| 81 | |||
| 82 | gboolean visible : 1; | ||
| 83 | } ChildInfo; | ||
| 84 | |||
| 85 | static void | ||
| 86 | ✗ | layer_bucket_free (LayerBucket *bucket) | |
| 87 | { | ||
| 88 | ✗ | g_clear_pointer (&bucket->symbols, g_ptr_array_unref); | |
| 89 | ✗ | g_free (bucket); | |
| 90 | ✗ | } | |
| 91 | |||
| 92 | static void | ||
| 93 | ✗ | add_symbol_to_layer_buckets (ShumateVectorSymbolContainer *self, | |
| 94 | ChildInfo *info) | ||
| 95 | { | ||
| 96 | ✗ | LayerBucket *bucket; | |
| 97 | |||
| 98 | ✗ | for (int i = 0; i < self->layer_buckets->len; i ++) | |
| 99 | { | ||
| 100 | ✗ | bucket = g_ptr_array_index (self->layer_buckets, i); | |
| 101 | |||
| 102 | ✗ | if (bucket->layer_idx == info->symbol_info->details->layer_idx) | |
| 103 | { | ||
| 104 | ✗ | g_ptr_array_add (bucket->symbols, info); | |
| 105 | ✗ | return; | |
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | ✗ | bucket = g_new0 (LayerBucket, 1); | |
| 110 | ✗ | bucket->layer_idx = info->symbol_info->details->layer_idx; | |
| 111 | ✗ | bucket->symbols = g_ptr_array_new_with_free_func (g_free); | |
| 112 | ✗ | g_ptr_array_add (self->layer_buckets, bucket); | |
| 113 | |||
| 114 | ✗ | g_ptr_array_add (bucket->symbols, info); | |
| 115 | } | ||
| 116 | |||
| 117 | static int | ||
| 118 | ✗ | child_info_compare (gconstpointer a, gconstpointer b) | |
| 119 | { | ||
| 120 | ✗ | const ChildInfo *child_a = *(ChildInfo **)a; | |
| 121 | ✗ | const ChildInfo *child_b = *(ChildInfo **)b; | |
| 122 | ✗ | return child_a->symbol_info->details->symbol_sort_key - child_b->symbol_info->details->symbol_sort_key; | |
| 123 | } | ||
| 124 | |||
| 125 | static int | ||
| 126 | ✗ | layer_bucket_compare (gconstpointer a, gconstpointer b) | |
| 127 | { | ||
| 128 | ✗ | const LayerBucket *bucket_a = *(LayerBucket **)a; | |
| 129 | ✗ | const LayerBucket *bucket_b = *(LayerBucket **)b; | |
| 130 | ✗ | return bucket_a->layer_idx - bucket_b->layer_idx; | |
| 131 | } | ||
| 132 | |||
| 133 | static void | ||
| 134 | ✗ | sort_layer_buckets (ShumateVectorSymbolContainer *self) | |
| 135 | { | ||
| 136 | ✗ | g_ptr_array_sort (self->layer_buckets, (GCompareFunc)layer_bucket_compare); | |
| 137 | |||
| 138 | ✗ | for (int i = 0; i < self->layer_buckets->len; i ++) | |
| 139 | { | ||
| 140 | ✗ | LayerBucket *bucket = self->layer_buckets->pdata[i]; | |
| 141 | ✗ | g_ptr_array_sort (bucket->symbols, (GCompareFunc)child_info_compare); | |
| 142 | } | ||
| 143 | ✗ | } | |
| 144 | |||
| 145 | ShumateVectorSymbolContainer * | ||
| 146 | ✗ | shumate_vector_symbol_container_new (ShumateMapSource *map_source, | |
| 147 | ShumateViewport *viewport) | ||
| 148 | { | ||
| 149 | ✗ | return g_object_new (SHUMATE_TYPE_VECTOR_SYMBOL_CONTAINER, | |
| 150 | "map-source", map_source, | ||
| 151 | "viewport", viewport, | ||
| 152 | NULL); | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | static void | ||
| 157 | ✗ | on_viewport_changed (ShumateVectorSymbolContainer *self, | |
| 158 | G_GNUC_UNUSED GParamSpec *pspec, | ||
| 159 | G_GNUC_UNUSED ShumateViewport *view) | ||
| 160 | { | ||
| 161 | ✗ | gtk_widget_queue_allocate (GTK_WIDGET (self)); | |
| 162 | ✗ | } | |
| 163 | |||
| 164 | |||
| 165 | static void | ||
| 166 | ✗ | shumate_vector_symbol_container_constructed (GObject *object) | |
| 167 | { | ||
| 168 | ✗ | ShumateVectorSymbolContainer *self = (ShumateVectorSymbolContainer *)object; | |
| 169 | ✗ | ShumateInspectorSettings *settings = shumate_inspector_settings_get_default (); | |
| 170 | ✗ | ShumateViewport *viewport; | |
| 171 | |||
| 172 | ✗ | G_OBJECT_CLASS (shumate_vector_symbol_container_parent_class)->constructed (object); | |
| 173 | |||
| 174 | ✗ | self->collision = shumate_vector_collision_new (); | |
| 175 | |||
| 176 | ✗ | viewport = shumate_layer_get_viewport (SHUMATE_LAYER (self)); | |
| 177 | ✗ | g_signal_connect_swapped (viewport, "changed", G_CALLBACK (on_viewport_changed), self); | |
| 178 | |||
| 179 | ✗ | g_signal_connect_object (settings, "notify::show-collision-boxes", G_CALLBACK (on_viewport_changed), self, G_CONNECT_SWAPPED); | |
| 180 | ✗ | } | |
| 181 | |||
| 182 | |||
| 183 | static void | ||
| 184 | ✗ | shumate_vector_symbol_container_finalize (GObject *object) | |
| 185 | { | ||
| 186 | ✗ | ShumateVectorSymbolContainer *self = (ShumateVectorSymbolContainer *)object; | |
| 187 | |||
| 188 | ✗ | g_clear_pointer (&self->layer_buckets, g_ptr_array_unref); | |
| 189 | ✗ | g_clear_pointer (&self->collision, shumate_vector_collision_free); | |
| 190 | |||
| 191 | ✗ | G_OBJECT_CLASS (shumate_vector_symbol_container_parent_class)->finalize (object); | |
| 192 | ✗ | } | |
| 193 | |||
| 194 | |||
| 195 | static void | ||
| 196 | ✗ | shumate_vector_symbol_container_dispose (GObject *object) | |
| 197 | { | ||
| 198 | ✗ | ShumateVectorSymbolContainer *self = (ShumateVectorSymbolContainer *)object; | |
| 199 | ✗ | ShumateViewport *viewport = shumate_layer_get_viewport (SHUMATE_LAYER (self)); | |
| 200 | ✗ | GtkWidget *child; | |
| 201 | |||
| 202 | ✗ | g_signal_handlers_disconnect_by_data (viewport, self); | |
| 203 | |||
| 204 | ✗ | while ((child = gtk_widget_get_first_child (GTK_WIDGET (object)))) | |
| 205 | ✗ | gtk_widget_unparent (child); | |
| 206 | |||
| 207 | ✗ | G_OBJECT_CLASS (shumate_vector_symbol_container_parent_class)->dispose (object); | |
| 208 | ✗ | } | |
| 209 | |||
| 210 | |||
| 211 | static void | ||
| 212 | ✗ | shumate_vector_symbol_container_get_property (GObject *object, | |
| 213 | guint property_id, | ||
| 214 | GValue *value, | ||
| 215 | GParamSpec *pspec) | ||
| 216 | { | ||
| 217 | ✗ | ShumateVectorSymbolContainer *self = (ShumateVectorSymbolContainer *)object; | |
| 218 | |||
| 219 | ✗ | switch (property_id) | |
| 220 | { | ||
| 221 | ✗ | case PROP_MAP_SOURCE: | |
| 222 | ✗ | g_value_set_object (value, self->map_source); | |
| 223 | ✗ | break; | |
| 224 | |||
| 225 | ✗ | default: | |
| 226 | ✗ | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
| 227 | } | ||
| 228 | ✗ | } | |
| 229 | |||
| 230 | |||
| 231 | static void | ||
| 232 | ✗ | shumate_vector_symbol_container_set_property (GObject *object, | |
| 233 | guint property_id, | ||
| 234 | const GValue *value, | ||
| 235 | GParamSpec *pspec) | ||
| 236 | { | ||
| 237 | ✗ | ShumateVectorSymbolContainer *self = SHUMATE_VECTOR_SYMBOL_CONTAINER (object); | |
| 238 | |||
| 239 | ✗ | switch (property_id) | |
| 240 | { | ||
| 241 | ✗ | case PROP_MAP_SOURCE: | |
| 242 | ✗ | shumate_vector_symbol_container_set_map_source (self, g_value_get_object (value)); | |
| 243 | ✗ | break; | |
| 244 | |||
| 245 | ✗ | default: | |
| 246 | ✗ | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
| 247 | } | ||
| 248 | ✗ | } | |
| 249 | |||
| 250 | static double | ||
| 251 | ✗ | get_effective_zoom_level (ShumateMapSource *map_source, ShumateViewport *viewport) | |
| 252 | { | ||
| 253 | ✗ | double zoom_level = shumate_viewport_get_zoom_level (viewport); | |
| 254 | ✗ | double our_tile_size = shumate_map_source_get_tile_size (map_source); | |
| 255 | ✗ | double reference_tile_size = shumate_map_source_get_tile_size (shumate_viewport_get_reference_map_source (viewport)); | |
| 256 | ✗ | return log2 (reference_tile_size / our_tile_size) + zoom_level; | |
| 257 | } | ||
| 258 | |||
| 259 | static void | ||
| 260 | ✗ | rotate_around_origin (double *x, | |
| 261 | double *y, | ||
| 262 | double angle) | ||
| 263 | { | ||
| 264 | /* Rotate (x, y) around (0, 0) */ | ||
| 265 | |||
| 266 | ✗ | if (angle == 0) | |
| 267 | return; | ||
| 268 | |||
| 269 | ✗ | double old_x = *x; | |
| 270 | ✗ | double old_y = *y; | |
| 271 | |||
| 272 | ✗ | *x = cosf (angle) * old_x - sinf (angle) * old_y; | |
| 273 | ✗ | *y = sinf (angle) * old_x + cosf (angle) * old_y; | |
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | static void | ||
| 278 | ✗ | rotate_around_center (double *x, | |
| 279 | double *y, | ||
| 280 | double width, | ||
| 281 | double height, | ||
| 282 | double angle) | ||
| 283 | { | ||
| 284 | /* Rotate (x, y) around (width / 2, height / 2) */ | ||
| 285 | |||
| 286 | ✗ | double center_x = width / 2.0; | |
| 287 | ✗ | double center_y = height / 2.0; | |
| 288 | |||
| 289 | ✗ | *x -= center_x; | |
| 290 | ✗ | *y -= center_y; | |
| 291 | ✗ | rotate_around_origin (x, y, angle); | |
| 292 | ✗ | *x += center_x; | |
| 293 | ✗ | *y += center_y; | |
| 294 | ✗ | } | |
| 295 | |||
| 296 | |||
| 297 | static void | ||
| 298 | ✗ | shumate_vector_symbol_container_size_allocate (GtkWidget *widget, | |
| 299 | int width, | ||
| 300 | int height, | ||
| 301 | int baseline) | ||
| 302 | { | ||
| 303 | ✗ | SHUMATE_PROFILE_START (); | |
| 304 | |||
| 305 | ✗ | ShumateVectorSymbolContainer *self = SHUMATE_VECTOR_SYMBOL_CONTAINER (widget); | |
| 306 | ✗ | GtkAllocation alloc; | |
| 307 | ✗ | double tile_size; | |
| 308 | ✗ | ShumateViewport *viewport; | |
| 309 | ✗ | double zoom_level; | |
| 310 | ✗ | double rotation; | |
| 311 | ✗ | double center_x, center_y; | |
| 312 | |||
| 313 | ✗ | gboolean recalc; | |
| 314 | |||
| 315 | ✗ | if (self->map_source == NULL) | |
| 316 | ✗ | return; | |
| 317 | |||
| 318 | ✗ | tile_size = shumate_map_source_get_tile_size (self->map_source); | |
| 319 | ✗ | viewport = shumate_layer_get_viewport (SHUMATE_LAYER (self)); | |
| 320 | ✗ | zoom_level = get_effective_zoom_level (self->map_source, viewport); | |
| 321 | ✗ | rotation = shumate_viewport_get_rotation (viewport); | |
| 322 | ✗ | center_x = shumate_map_source_get_x (self->map_source, zoom_level, shumate_location_get_longitude (SHUMATE_LOCATION (viewport))); | |
| 323 | ✗ | center_y = shumate_map_source_get_y (self->map_source, zoom_level, shumate_location_get_latitude (SHUMATE_LOCATION (viewport))); | |
| 324 | |||
| 325 | ✗ | recalc = self->labels_changed | |
| 326 | ✗ | || self->last_zoom != zoom_level | |
| 327 | ✗ | || self->last_rotation != rotation | |
| 328 | ✗ | || self->last_width != width | |
| 329 | ✗ | || self->last_height != height; | |
| 330 | |||
| 331 | ✗ | if (recalc) | |
| 332 | { | ||
| 333 | ✗ | shumate_vector_collision_clear (self->collision); | |
| 334 | ✗ | self->last_center_x = center_x; | |
| 335 | ✗ | self->last_center_y = center_y; | |
| 336 | ✗ | self->collision->delta_x = 0; | |
| 337 | ✗ | self->collision->delta_y = 0; | |
| 338 | ✗ | self->visible_count = 0; | |
| 339 | } | ||
| 340 | else | ||
| 341 | { | ||
| 342 | ✗ | self->collision->delta_x = center_x - self->last_center_x; | |
| 343 | ✗ | self->collision->delta_y = center_y - self->last_center_y; | |
| 344 | ✗ | rotate_around_origin (&self->collision->delta_x, &self->collision->delta_y, rotation); | |
| 345 | } | ||
| 346 | |||
| 347 | /* Higher layers have priority during placement, so iterate the | ||
| 348 | array from back to front */ | ||
| 349 | ✗ | for (int i = self->layer_buckets->len - 1; i >= 0; i --) | |
| 350 | { | ||
| 351 | ✗ | LayerBucket *bucket = g_ptr_array_index (self->layer_buckets, i); | |
| 352 | |||
| 353 | ✗ | for (int j = 0; j < bucket->symbols->len; j ++) | |
| 354 | { | ||
| 355 | ✗ | ChildInfo *child = g_ptr_array_index (bucket->symbols, j); | |
| 356 | ✗ | double tile_size_at_zoom = tile_size * pow (2, zoom_level - child->zoom); | |
| 357 | ✗ | double x = (child->tile_x + child->x) * tile_size_at_zoom - center_x + width/2.0; | |
| 358 | ✗ | double y = (child->tile_y + child->y) * tile_size_at_zoom - center_y + height/2.0; | |
| 359 | |||
| 360 | ✗ | rotate_around_center (&x, &y, width, height, rotation); | |
| 361 | |||
| 362 | ✗ | if (recalc) | |
| 363 | { | ||
| 364 | ✗ | gboolean now_visible = | |
| 365 | ✗ | shumate_vector_symbol_calculate_collision (child->symbol, | |
| 366 | self->collision, | ||
| 367 | x, | ||
| 368 | y, | ||
| 369 | tile_size_at_zoom, | ||
| 370 | rotation, | ||
| 371 | &child->bounds); | ||
| 372 | |||
| 373 | ✗ | if (now_visible != child->visible) | |
| 374 | { | ||
| 375 | ✗ | gtk_widget_set_child_visible (GTK_WIDGET (child->symbol), now_visible); | |
| 376 | ✗ | child->visible = now_visible; | |
| 377 | } | ||
| 378 | |||
| 379 | ✗ | if (now_visible) | |
| 380 | ✗ | self->visible_count ++; | |
| 381 | } | ||
| 382 | |||
| 383 | ✗ | if (!child->visible) | |
| 384 | ✗ | continue; | |
| 385 | |||
| 386 | ✗ | gtk_widget_measure (GTK_WIDGET (child->symbol), GTK_ORIENTATION_HORIZONTAL, -1, NULL, NULL, NULL, NULL); | |
| 387 | ✗ | gtk_widget_measure (GTK_WIDGET (child->symbol), GTK_ORIENTATION_VERTICAL, -1, NULL, NULL, NULL, NULL); | |
| 388 | |||
| 389 | ✗ | alloc.x = child->bounds.origin.x - self->collision->delta_x; | |
| 390 | ✗ | alloc.y = child->bounds.origin.y - self->collision->delta_y; | |
| 391 | ✗ | alloc.width = child->bounds.size.width; | |
| 392 | ✗ | alloc.height = child->bounds.size.height; | |
| 393 | |||
| 394 | ✗ | gtk_widget_size_allocate (GTK_WIDGET (child->symbol), &alloc, -1); | |
| 395 | ✗ | if (child->symbol_info->line != NULL) | |
| 396 | ✗ | gtk_widget_queue_draw (GTK_WIDGET (child->symbol)); | |
| 397 | } | ||
| 398 | } | ||
| 399 | |||
| 400 | ✗ | self->labels_changed = FALSE; | |
| 401 | ✗ | self->last_rotation = rotation; | |
| 402 | ✗ | self->last_zoom = zoom_level; | |
| 403 | ✗ | self->last_width = width; | |
| 404 | ✗ | self->last_height = height; | |
| 405 | } | ||
| 406 | |||
| 407 | |||
| 408 | static void | ||
| 409 | ✗ | shumate_vector_symbol_container_snapshot (GtkWidget *widget, | |
| 410 | GtkSnapshot *snapshot) | ||
| 411 | { | ||
| 412 | ✗ | SHUMATE_PROFILE_START (); | |
| 413 | |||
| 414 | ✗ | ShumateVectorSymbolContainer *self = SHUMATE_VECTOR_SYMBOL_CONTAINER (widget); | |
| 415 | ✗ | ShumateInspectorSettings *settings = shumate_inspector_settings_get_default (); | |
| 416 | |||
| 417 | ✗ | for (int i = 0; i < self->layer_buckets->len; i ++) | |
| 418 | { | ||
| 419 | ✗ | LayerBucket *bucket = g_ptr_array_index (self->layer_buckets, i); | |
| 420 | |||
| 421 | ✗ | for (int j = 0; j < bucket->symbols->len; j ++) | |
| 422 | { | ||
| 423 | ✗ | ChildInfo *child = g_ptr_array_index (bucket->symbols, j); | |
| 424 | ✗ | double correct_x = child->bounds.origin.x - self->collision->delta_x; | |
| 425 | ✗ | double correct_y = child->bounds.origin.y - self->collision->delta_y; | |
| 426 | |||
| 427 | ✗ | gtk_snapshot_save (snapshot); | |
| 428 | ✗ | gtk_snapshot_translate (snapshot, | |
| 429 | ✗ | &GRAPHENE_POINT_INIT ( | |
| 430 | correct_x - (int) correct_x, | ||
| 431 | correct_y - (int) correct_y | ||
| 432 | )); | ||
| 433 | ✗ | gtk_widget_snapshot_child (widget, GTK_WIDGET (child->symbol), snapshot); | |
| 434 | ✗ | gtk_snapshot_restore (snapshot); | |
| 435 | } | ||
| 436 | } | ||
| 437 | |||
| 438 | ✗ | if (shumate_inspector_settings_get_show_collision_boxes (settings)) | |
| 439 | { | ||
| 440 | ✗ | ShumateViewport *viewport = shumate_layer_get_viewport (SHUMATE_LAYER (self)); | |
| 441 | ✗ | double rotation = shumate_viewport_get_rotation (viewport); | |
| 442 | ✗ | double delta_x = -self->collision->delta_x; | |
| 443 | ✗ | double delta_y = -self->collision->delta_y; | |
| 444 | |||
| 445 | ✗ | rotate_around_origin (&delta_x, &delta_y, rotation); | |
| 446 | |||
| 447 | ✗ | gtk_snapshot_save (snapshot); | |
| 448 | ✗ | gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (delta_x, delta_y)); | |
| 449 | ✗ | shumate_vector_collision_visualize (self->collision, snapshot); | |
| 450 | ✗ | gtk_snapshot_restore (snapshot); | |
| 451 | } | ||
| 452 | ✗ | } | |
| 453 | |||
| 454 | |||
| 455 | static void | ||
| 456 | ✗ | shumate_vector_symbol_container_class_init (ShumateVectorSymbolContainerClass *klass) | |
| 457 | { | ||
| 458 | ✗ | GObjectClass *object_class = G_OBJECT_CLASS (klass); | |
| 459 | ✗ | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); | |
| 460 | |||
| 461 | ✗ | object_class->constructed = shumate_vector_symbol_container_constructed; | |
| 462 | ✗ | object_class->dispose = shumate_vector_symbol_container_dispose; | |
| 463 | ✗ | object_class->finalize = shumate_vector_symbol_container_finalize; | |
| 464 | ✗ | object_class->get_property = shumate_vector_symbol_container_get_property; | |
| 465 | ✗ | object_class->set_property = shumate_vector_symbol_container_set_property; | |
| 466 | ✗ | widget_class->size_allocate = shumate_vector_symbol_container_size_allocate; | |
| 467 | ✗ | widget_class->snapshot = shumate_vector_symbol_container_snapshot; | |
| 468 | |||
| 469 | /** | ||
| 470 | * ShumateVectorSymbolContainer:map-source: | ||
| 471 | * | ||
| 472 | * The source of the tiles this container displays. | ||
| 473 | */ | ||
| 474 | ✗ | obj_properties[PROP_MAP_SOURCE] = | |
| 475 | ✗ | g_param_spec_object ("map-source", NULL, NULL, | |
| 476 | SHUMATE_TYPE_MAP_SOURCE, | ||
| 477 | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); | ||
| 478 | |||
| 479 | ✗ | g_object_class_install_properties (object_class, N_PROPS, obj_properties); | |
| 480 | |||
| 481 | ✗ | signals[SYMBOL_CLICKED] = | |
| 482 | ✗ | g_signal_new ("symbol-clicked", | |
| 483 | G_OBJECT_CLASS_TYPE (object_class), | ||
| 484 | G_SIGNAL_RUN_LAST, | ||
| 485 | 0, NULL, NULL, | ||
| 486 | g_cclosure_marshal_VOID__OBJECT, | ||
| 487 | G_TYPE_NONE, | ||
| 488 | 1, | ||
| 489 | SHUMATE_TYPE_SYMBOL_EVENT); | ||
| 490 | ✗ | } | |
| 491 | |||
| 492 | static void | ||
| 493 | ✗ | shumate_vector_symbol_container_init (ShumateVectorSymbolContainer *self) | |
| 494 | { | ||
| 495 | ✗ | self->layer_buckets = g_ptr_array_new_with_free_func ((GDestroyNotify)layer_bucket_free); | |
| 496 | ✗ | } | |
| 497 | |||
| 498 | static void | ||
| 499 | ✗ | on_symbol_clicked (ShumateVectorSymbolContainer *self, | |
| 500 | ShumateSymbolEvent *event, | ||
| 501 | ShumateVectorSymbol *symbol) | ||
| 502 | { | ||
| 503 | ✗ | ShumateVectorSymbolInfo *symbol_info = shumate_vector_symbol_get_symbol_info (symbol); | |
| 504 | |||
| 505 | ✗ | double tile_size = shumate_map_source_get_tile_size (self->map_source); | |
| 506 | ✗ | double lat = shumate_map_source_get_latitude (self->map_source, | |
| 507 | ✗ | symbol_info->details->tile_zoom_level, | |
| 508 | ✗ | (symbol_info->details->tile_y + symbol_info->y) * tile_size); | |
| 509 | ✗ | double lon = shumate_map_source_get_longitude (self->map_source, | |
| 510 | ✗ | symbol_info->details->tile_zoom_level, | |
| 511 | ✗ | (symbol_info->details->tile_x + symbol_info->x) * tile_size); | |
| 512 | |||
| 513 | ✗ | shumate_symbol_event_set_lat_lon (event, lat, lon); | |
| 514 | |||
| 515 | ✗ | g_signal_emit (self, signals[SYMBOL_CLICKED], 0, event); | |
| 516 | ✗ | } | |
| 517 | |||
| 518 | |||
| 519 | void | ||
| 520 | ✗ | shumate_vector_symbol_container_add_symbols (ShumateVectorSymbolContainer *self, | |
| 521 | GPtrArray *symbol_infos, | ||
| 522 | int tile_x, | ||
| 523 | int tile_y, | ||
| 524 | int zoom) | ||
| 525 | { | ||
| 526 | ✗ | SHUMATE_PROFILE_START (); | |
| 527 | |||
| 528 | ✗ | g_return_if_fail (SHUMATE_IS_VECTOR_SYMBOL_CONTAINER (self)); | |
| 529 | |||
| 530 | ✗ | for (int i = 0; i < symbol_infos->len; i ++) | |
| 531 | { | ||
| 532 | ✗ | ChildInfo *info = g_new0 (ChildInfo, 1); | |
| 533 | ✗ | ShumateVectorSymbolInfo *symbol_info = symbol_infos->pdata[i]; | |
| 534 | ✗ | ShumateVectorSymbol *symbol = shumate_vector_symbol_new (symbol_info); | |
| 535 | |||
| 536 | ✗ | info->symbol = symbol; | |
| 537 | ✗ | info->symbol_info = symbol_info; | |
| 538 | ✗ | info->x = symbol_info->x; | |
| 539 | ✗ | info->y = symbol_info->y; | |
| 540 | ✗ | info->tile_x = tile_x; | |
| 541 | ✗ | info->tile_y = tile_y; | |
| 542 | ✗ | info->zoom = zoom; | |
| 543 | ✗ | info->visible = TRUE; | |
| 544 | |||
| 545 | ✗ | add_symbol_to_layer_buckets (self, info); | |
| 546 | ✗ | gtk_widget_set_parent (GTK_WIDGET (info->symbol), GTK_WIDGET (self)); | |
| 547 | ✗ | self->child_count ++; | |
| 548 | |||
| 549 | ✗ | g_signal_connect_object (info->symbol, | |
| 550 | "clicked", | ||
| 551 | G_CALLBACK (on_symbol_clicked), | ||
| 552 | self, | ||
| 553 | G_CONNECT_SWAPPED); | ||
| 554 | } | ||
| 555 | |||
| 556 | ✗ | sort_layer_buckets (self); | |
| 557 | ✗ | self->labels_changed = TRUE; | |
| 558 | } | ||
| 559 | |||
| 560 | |||
| 561 | void | ||
| 562 | ✗ | shumate_vector_symbol_container_remove_symbols (ShumateVectorSymbolContainer *self, | |
| 563 | int tile_x, | ||
| 564 | int tile_y, | ||
| 565 | int zoom) | ||
| 566 | { | ||
| 567 | ✗ | SHUMATE_PROFILE_START (); | |
| 568 | |||
| 569 | ✗ | g_return_if_fail (SHUMATE_IS_VECTOR_SYMBOL_CONTAINER (self)); | |
| 570 | |||
| 571 | ✗ | for (int i = 0; i < self->layer_buckets->len; i ++) | |
| 572 | { | ||
| 573 | ✗ | LayerBucket *bucket = g_ptr_array_index (self->layer_buckets, i); | |
| 574 | ✗ | int k = 0; | |
| 575 | |||
| 576 | ✗ | for (int j = 0; j < bucket->symbols->len; j ++) | |
| 577 | { | ||
| 578 | ✗ | ChildInfo *info = g_ptr_array_index (bucket->symbols, j); | |
| 579 | |||
| 580 | ✗ | if (info->tile_x == tile_x && info->tile_y == tile_y && info->zoom == zoom) | |
| 581 | { | ||
| 582 | ✗ | gtk_widget_unparent (GTK_WIDGET (info->symbol)); | |
| 583 | ✗ | self->child_count --; | |
| 584 | ✗ | g_clear_pointer (&g_ptr_array_index (bucket->symbols, j), g_free); | |
| 585 | } | ||
| 586 | else | ||
| 587 | { | ||
| 588 | ✗ | g_ptr_array_index (bucket->symbols, j) = NULL; | |
| 589 | ✗ | g_ptr_array_index (bucket->symbols, k) = info; | |
| 590 | ✗ | k ++; | |
| 591 | } | ||
| 592 | } | ||
| 593 | |||
| 594 | ✗ | g_ptr_array_set_size (bucket->symbols, k); | |
| 595 | } | ||
| 596 | |||
| 597 | ✗ | self->labels_changed = TRUE; | |
| 598 | } | ||
| 599 | |||
| 600 | |||
| 601 | ShumateMapSource * | ||
| 602 | ✗ | shumate_vector_symbol_container_get_map_source (ShumateVectorSymbolContainer *self) | |
| 603 | { | ||
| 604 | ✗ | g_return_val_if_fail (SHUMATE_IS_VECTOR_SYMBOL_CONTAINER (self), NULL); | |
| 605 | ✗ | return self->map_source; | |
| 606 | } | ||
| 607 | |||
| 608 | |||
| 609 | void | ||
| 610 | ✗ | shumate_vector_symbol_container_set_map_source (ShumateVectorSymbolContainer *self, | |
| 611 | ShumateMapSource *map_source) | ||
| 612 | { | ||
| 613 | ✗ | g_return_if_fail (SHUMATE_IS_VECTOR_SYMBOL_CONTAINER (self)); | |
| 614 | ✗ | g_return_if_fail (map_source == NULL || SHUMATE_IS_MAP_SOURCE (map_source)); | |
| 615 | |||
| 616 | ✗ | if (g_set_object (&self->map_source, map_source)) | |
| 617 | ✗ | gtk_widget_queue_allocate (GTK_WIDGET (self)); | |
| 618 | } | ||
| 619 | |||
| 620 | |||
| 621 | ShumateVectorCollision * | ||
| 622 | ✗ | shumate_vector_symbol_container_get_collision (ShumateVectorSymbolContainer *self) | |
| 623 | { | ||
| 624 | ✗ | g_return_val_if_fail (SHUMATE_IS_VECTOR_SYMBOL_CONTAINER (self), NULL); | |
| 625 | ✗ | return self->collision; | |
| 626 | } | ||
| 627 | |||
| 628 | |||
| 629 | char * | ||
| 630 | ✗ | shumate_vector_symbol_container_get_debug_text (ShumateVectorSymbolContainer *self) | |
| 631 | { | ||
| 632 | ✗ | return g_strdup_printf ("symbols: %d, %d visible\n", self->child_count, self->visible_count); | |
| 633 | } | ||
| 634 |