diff --git a/package.json b/package.json index 5fac3cb..2d182a5 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,17 @@ "dummy" ], "resources": { - "media": [] + "media": [{ + "type": "font", + "name": "CLOCK_FONT_38", + "file": "clock_font_big.ttf", + "characterRange": "[0-9]" + }, { + "type": "font", + "name": "CLOCK_FONT_26", + "file": "clock_font_small.ttf", + "characterRange": "[0-9]" + }] } } } diff --git a/resources/clock_font_big.ttf b/resources/clock_font_big.ttf new file mode 100644 index 0000000..d40ff81 Binary files /dev/null and b/resources/clock_font_big.ttf differ diff --git a/resources/clock_font_small.ttf b/resources/clock_font_small.ttf new file mode 100644 index 0000000..4055d07 Binary files /dev/null and b/resources/clock_font_small.ttf differ diff --git a/src/c/clock_layer.c b/src/c/clock_layer.c new file mode 100644 index 0000000..cd9568f --- /dev/null +++ b/src/c/clock_layer.c @@ -0,0 +1,84 @@ +#include "clock_layer.h" + +#define CLOCK_COMMON_SPACE 2 +#define CLOCK_BETWEEN_HM_SPACE 8 + +static GFont s_clock_font_big; +static GFont s_clock_font_small; +static Layer *s_clock_layer; +static char s_clock_measure_buffer[4]; + +static void clock_layer_update_proc(Layer *layer, GContext *ctx) { + // Fill a whole layer with the black color. + GRect layer_bounds = layer_get_unobstructed_bounds(layer); + layer_bounds.origin = GPoint(0, 0); + graphics_context_set_fill_color(ctx, GColorBlack); + graphics_fill_rect(ctx, layer_bounds, 0, GCornerNone); + + // Get the current time. + time_t tmp = time(NULL); + struct tm *time = localtime(&tmp); + + // Drawing the measures. + graphics_context_set_text_color(ctx, GColorWhite); + + GSize big_text_size = graphics_text_layout_get_content_size( + "00", s_clock_font_big, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft); + GSize small_text_size = graphics_text_layout_get_content_size( + "00", s_clock_font_small, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft); + + int clock_w = + (big_text_size.w * 2) + small_text_size.w + CLOCK_COMMON_SPACE + CLOCK_BETWEEN_HM_SPACE; + int clock_w_pad = (layer_bounds.size.w - clock_w) / 2; + + strftime(s_clock_measure_buffer, sizeof(s_clock_measure_buffer), + clock_is_24h_style() ? "%H" : "%I", time); + graphics_draw_text(ctx, s_clock_measure_buffer, s_clock_font_big, + GRect(clock_w_pad, 1, big_text_size.w, big_text_size.h), + GTextOverflowModeFill, GTextAlignmentLeft, NULL); + + strftime(s_clock_measure_buffer, sizeof(s_clock_measure_buffer), "%M", time); + graphics_draw_text(ctx, s_clock_measure_buffer, s_clock_font_big, + GRect(clock_w_pad + big_text_size.w + CLOCK_BETWEEN_HM_SPACE, 1, + big_text_size.w, big_text_size.h), + GTextOverflowModeFill, GTextAlignmentLeft, NULL); + + strftime(s_clock_measure_buffer, sizeof(s_clock_measure_buffer), "%S", time); + graphics_draw_text( + ctx, s_clock_measure_buffer, s_clock_font_small, + GRect(clock_w_pad + (big_text_size.w * 2) + CLOCK_BETWEEN_HM_SPACE + CLOCK_COMMON_SPACE, + small_text_size.h - 12, small_text_size.w, small_text_size.h), + GTextOverflowModeFill, GTextAlignmentLeft, NULL); + + // Draw the colon between hours and minutes; + graphics_context_set_fill_color(ctx, GColorWhite); + + int colon_x = clock_w_pad + big_text_size.w - 2; + graphics_fill_rect(ctx, GRect(colon_x, (big_text_size.h / 4) + 3, 6, 6), 0, GCornerNone); + graphics_fill_rect(ctx, GRect(colon_x, (big_text_size.h / 4) + (big_text_size.h / 2) - 3, 6, 6), + 0, GCornerNone); +} + +void clock_layer_init(Window *window, GPoint position) { + s_clock_font_big = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_CLOCK_FONT_38)); + s_clock_font_small = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_CLOCK_FONT_26)); + + Layer *window_root_layer = window_get_root_layer(window); + GRect window_bounds = layer_get_unobstructed_bounds(window_root_layer); + + s_clock_layer = + layer_create(GRect(position.x, position.y, window_bounds.size.w - position.x, 40)); + layer_set_update_proc(s_clock_layer, clock_layer_update_proc); + + layer_add_child(window_root_layer, s_clock_layer); +} + +void clock_layer_update(void) { + layer_mark_dirty(s_clock_layer); +} + +void clock_layer_deinit(void) { + layer_destroy(s_clock_layer); + fonts_unload_custom_font(s_clock_font_big); + fonts_unload_custom_font(s_clock_font_small); +} diff --git a/src/c/clock_layer.h b/src/c/clock_layer.h new file mode 100644 index 0000000..315b9e0 --- /dev/null +++ b/src/c/clock_layer.h @@ -0,0 +1,10 @@ +#ifndef CLOCK_LAYER_H_ +#define CLOCK_LAYER_H_ + +#include + +void clock_layer_init(Window *window, GPoint position); +void clock_layer_update(void); +void clock_layer_deinit(void); + +#endif diff --git a/src/c/main_window.c b/src/c/main_window.c index a9d6572..e1d0b03 100644 --- a/src/c/main_window.c +++ b/src/c/main_window.c @@ -1,13 +1,20 @@ #include "main_window.h" +#include "clock_layer.h" static Window *s_main_window; -static void main_window_load(Window *window) { +static void main_window_tick(struct tm *time, TimeUnits units) { + clock_layer_update(); +} +static void main_window_load(Window *window) { + clock_layer_init(s_main_window, GPoint(0, 60)); + tick_timer_service_subscribe(SECOND_UNIT, main_window_tick); } static void main_window_unload(Window *window) { - + tick_timer_service_unsubscribe(); + clock_layer_deinit(); } void main_window_init(void) {