|
| 1 | +/* |
| 2 | +* Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <pebble.h> |
| 18 | +#include "../../../image_manager/image_manager.h" |
| 19 | + |
| 20 | +#include "map.h" |
| 21 | + |
| 22 | +static void prv_image_updated(int image_id, ImageStatus status, void *context); |
| 23 | +static void prv_layer_update(Layer *layer, GContext *ctx); |
| 24 | + |
| 25 | +typedef struct { |
| 26 | + ConversationEntry* entry; |
| 27 | + GBitmap* bitmap; |
| 28 | +} MapWidgetData; |
| 29 | + |
| 30 | +static inline int prv_get_image_id(MapWidgetData *data) { |
| 31 | + return conversation_entry_get_widget(data->entry)->widget.map.image_id; |
| 32 | +} |
| 33 | + |
| 34 | +MapWidget* map_widget_create(GRect rect, ConversationEntry* entry) { |
| 35 | + int image_id = conversation_entry_get_widget(entry)->widget.map.image_id; |
| 36 | + GSize image_size = image_manager_get_size(image_id); |
| 37 | + Layer *layer = layer_create_with_data(GRect(rect.origin.x, rect.origin.y, rect.size.w, image_size.h + 2), sizeof(MapWidgetData)); |
| 38 | + MapWidgetData* data = layer_get_data(layer); |
| 39 | + data->entry = entry; |
| 40 | + data->bitmap = NULL; |
| 41 | + image_manager_register_callback(image_id, prv_image_updated, layer); |
| 42 | + layer_set_update_proc(layer, prv_layer_update); |
| 43 | + return layer; |
| 44 | +} |
| 45 | + |
| 46 | +ConversationEntry* map_widget_get_entry(MapWidget* layer) { |
| 47 | + MapWidgetData* data = layer_get_data(layer); |
| 48 | + return data->entry; |
| 49 | +} |
| 50 | + |
| 51 | +void map_widget_destroy(MapWidget* layer) { |
| 52 | + MapWidgetData* data = layer_get_data(layer); |
| 53 | + if (data->bitmap) { |
| 54 | + gbitmap_destroy(data->bitmap); |
| 55 | + } |
| 56 | + int image_id = prv_get_image_id(data); |
| 57 | + image_manager_unregister_callback(image_id); |
| 58 | + layer_destroy(layer); |
| 59 | +} |
| 60 | + |
| 61 | +void map_widget_update(MapWidget* layer) { |
| 62 | + // nothing to do here. |
| 63 | +} |
| 64 | + |
| 65 | +static void prv_image_updated(int image_id, ImageStatus status, void *context) { |
| 66 | + Layer *layer = context; |
| 67 | + MapWidgetData* data = layer_get_data(layer); |
| 68 | + if (status == ImageStatusCompleted) { |
| 69 | + if (data->bitmap) { |
| 70 | + gbitmap_destroy(data->bitmap); |
| 71 | + } |
| 72 | + data->bitmap = image_manager_get_image(image_id); |
| 73 | + layer_mark_dirty(layer); |
| 74 | + } else if (status == ImageStatusDestroyed) { |
| 75 | + data->bitmap = NULL; |
| 76 | + layer_mark_dirty(layer); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +static void prv_layer_update(Layer *layer, GContext *ctx) { |
| 81 | + MapWidgetData *data = layer_get_data(layer); |
| 82 | + GRect bounds = layer_get_bounds(layer); |
| 83 | + // bounds = grect_inset(bounds, (GEdgeInsets){.right = 5}); |
| 84 | + GSize image_size = image_manager_get_size(prv_get_image_id(data)); |
| 85 | + // GRect border_rect = GRect((bounds.size.w - image_size.w) / 2, 0, image_size.w + 2, image_size.h + 2); |
| 86 | + // GRect image_rect = grect_inset(border_rect, GEdgeInsets(1)); |
| 87 | + GRect image_rect = grect_inset(bounds, GEdgeInsets(1, 0)); |
| 88 | + graphics_context_set_stroke_color(ctx, GColorBlack); |
| 89 | + graphics_draw_line(ctx, GPoint(0, 0), GPoint(bounds.size.w, 0)); |
| 90 | + graphics_draw_line(ctx, GPoint(0, bounds.size.h - 1), GPoint(bounds.size.w, bounds.size.h - 1)); |
| 91 | + if (data->bitmap) { |
| 92 | + graphics_draw_bitmap_in_rect(ctx, data->bitmap, image_rect); |
| 93 | + } else { |
| 94 | + graphics_context_set_fill_color(ctx, COLOR_FALLBACK(GColorLightGray, GColorWhite)); |
| 95 | + graphics_fill_rect(ctx, image_rect, 0, GCornerNone); |
| 96 | + } |
| 97 | +} |
0 commit comments