feat(services): resources service
This commit is contained in:
parent
2778dc4eda
commit
9f6f83e296
5 changed files with 81 additions and 36 deletions
|
|
@ -1,26 +1,19 @@
|
||||||
#include "character_layer.h"
|
#include "character_layer.h"
|
||||||
|
#include "resources_service.h"
|
||||||
|
|
||||||
#define CHARACTER_H 80
|
#define CHARACTER_H 80
|
||||||
#define CHARACTER_STEP 4
|
#define CHARACTER_STEP 4
|
||||||
|
|
||||||
typedef struct CharacterLayerData {
|
typedef struct CharacterLayerData {
|
||||||
int ticks;
|
int8_t ticks;
|
||||||
GBitmap *even;
|
|
||||||
GBitmap *odd;
|
|
||||||
} CharacterLayerData;
|
} CharacterLayerData;
|
||||||
|
|
||||||
static Layer *s_character_layer;
|
static Layer *s_character_layer;
|
||||||
|
|
||||||
static void character_layer_update_proc(Layer *layer, GContext *ctx) {
|
static void character_layer_update_proc(Layer *layer, GContext *ctx) {
|
||||||
CharacterLayerData *layer_data = layer_get_data(s_character_layer);
|
|
||||||
|
|
||||||
// Get the character bitmap.
|
// Get the character bitmap.
|
||||||
GBitmap *bitmap;
|
CharacterLayerData *layer_data = layer_get_data(s_character_layer);
|
||||||
if (layer_data->ticks % 2 == 0) {
|
GBitmap *bitmap = resources_service_get_character(layer_data->ticks);
|
||||||
bitmap = layer_data->even;
|
|
||||||
} else {
|
|
||||||
bitmap = layer_data->odd;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the position of character bitmap and align it to bottom.
|
// Get the position of character bitmap and align it to bottom.
|
||||||
GRect frame = gbitmap_get_bounds(bitmap);
|
GRect frame = gbitmap_get_bounds(bitmap);
|
||||||
|
|
@ -50,8 +43,6 @@ void character_layer_init(Window *window) {
|
||||||
layer_create_with_data(GRect(0, window_bounds.size.h - CHARACTER_H, window_bounds.size.w, CHARACTER_H), sizeof(CharacterLayerData));
|
layer_create_with_data(GRect(0, window_bounds.size.h - CHARACTER_H, window_bounds.size.w, CHARACTER_H), sizeof(CharacterLayerData));
|
||||||
|
|
||||||
CharacterLayerData *layer_data = layer_get_data(s_character_layer);
|
CharacterLayerData *layer_data = layer_get_data(s_character_layer);
|
||||||
layer_data->even = gbitmap_create_with_resource(RESOURCE_ID_CHARACTER_EVEN);
|
|
||||||
layer_data->odd = gbitmap_create_with_resource(RESOURCE_ID_CHARACTER_ODD);
|
|
||||||
layer_data->ticks = 0;
|
layer_data->ticks = 0;
|
||||||
|
|
||||||
layer_set_update_proc(s_character_layer, character_layer_update_proc);
|
layer_set_update_proc(s_character_layer, character_layer_update_proc);
|
||||||
|
|
@ -67,8 +58,5 @@ void character_layer_tick(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void character_layer_deinit(void) {
|
void character_layer_deinit(void) {
|
||||||
CharacterLayerData *layer_data = layer_get_data(s_character_layer);
|
|
||||||
gbitmap_destroy(layer_data->even);
|
|
||||||
gbitmap_destroy(layer_data->odd);
|
|
||||||
layer_destroy(s_character_layer);
|
layer_destroy(s_character_layer);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,14 @@
|
||||||
#include "clock_layer.h"
|
#include "clock_layer.h"
|
||||||
|
|
||||||
|
#include "resources_service.h"
|
||||||
|
|
||||||
#define CLOCK_H 40
|
#define CLOCK_H 40
|
||||||
#define CLOCK_SPACE 2
|
#define CLOCK_SPACE 2
|
||||||
#define CLOCK_OFFSET 4
|
#define CLOCK_OFFSET 4
|
||||||
|
|
||||||
typedef struct ClockLayerData {
|
|
||||||
GFont font_big;
|
|
||||||
GFont font_small;
|
|
||||||
} ClockLayerData;
|
|
||||||
|
|
||||||
static Layer *s_clock_layer;
|
static Layer *s_clock_layer;
|
||||||
|
|
||||||
static void clock_layer_update_proc(Layer *layer, GContext *ctx) {
|
static void clock_layer_update_proc(Layer *layer, GContext *ctx) {
|
||||||
ClockLayerData *layer_data = layer_get_data(s_clock_layer);
|
|
||||||
|
|
||||||
// Fill a whole layer with the black color.
|
// Fill a whole layer with the black color.
|
||||||
GRect layer_bounds = layer_get_unobstructed_bounds(layer);
|
GRect layer_bounds = layer_get_unobstructed_bounds(layer);
|
||||||
layer_bounds.origin = GPoint(0, 0);
|
layer_bounds.origin = GPoint(0, 0);
|
||||||
|
|
@ -26,15 +21,19 @@ static void clock_layer_update_proc(Layer *layer, GContext *ctx) {
|
||||||
|
|
||||||
// Drawing the time and seconds.
|
// Drawing the time and seconds.
|
||||||
static char time_text[6], seconds_text[3] = "00\0";
|
static char time_text[6], seconds_text[3] = "00\0";
|
||||||
|
|
||||||
|
GFont font_small = resources_service_get_custom_font(CustomFontKonekoToro);
|
||||||
|
GFont font_big = resources_service_get_custom_font(CustomFontToro);
|
||||||
|
|
||||||
graphics_context_set_text_color(ctx, GColorWhite);
|
graphics_context_set_text_color(ctx, GColorWhite);
|
||||||
|
|
||||||
strftime(time_text, sizeof(time_text), clock_is_24h_style() ? "%H:%M" : "%I:%M", time);
|
strftime(time_text, sizeof(time_text), clock_is_24h_style() ? "%H:%M" : "%I:%M", time);
|
||||||
GSize time_text_size = graphics_text_layout_get_content_size(
|
GSize time_text_size = graphics_text_layout_get_content_size(
|
||||||
time_text, layer_data->font_big, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft);
|
time_text, font_big, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft);
|
||||||
|
|
||||||
seconds_text[0] = seconds_text[1] = '0';
|
seconds_text[0] = seconds_text[1] = '0';
|
||||||
GSize seconds_text_size = graphics_text_layout_get_content_size(
|
GSize seconds_text_size = graphics_text_layout_get_content_size(
|
||||||
seconds_text, layer_data->font_small, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft);
|
seconds_text, font_small, layer_bounds, GTextOverflowModeFill, GTextAlignmentLeft);
|
||||||
strftime(seconds_text, sizeof(seconds_text), "%S", time);
|
strftime(seconds_text, sizeof(seconds_text), "%S", time);
|
||||||
|
|
||||||
GRect timestamp_rect =
|
GRect timestamp_rect =
|
||||||
|
|
@ -44,12 +43,12 @@ static void clock_layer_update_proc(Layer *layer, GContext *ctx) {
|
||||||
|
|
||||||
GRect time_rect =
|
GRect time_rect =
|
||||||
GRect(timestamp_rect.origin.x, timestamp_rect.origin.y, time_text_size.w, time_text_size.h);
|
GRect(timestamp_rect.origin.x, timestamp_rect.origin.y, time_text_size.w, time_text_size.h);
|
||||||
graphics_draw_text(ctx, time_text, layer_data->font_big, time_rect, GTextOverflowModeFill,
|
graphics_draw_text(ctx, time_text, font_big, time_rect, GTextOverflowModeFill, GTextAlignmentLeft,
|
||||||
GTextAlignmentLeft, NULL);
|
NULL);
|
||||||
|
|
||||||
GRect seconds_rect = GRect(0, 0, seconds_text_size.w, seconds_text_size.h);
|
GRect seconds_rect = GRect(0, 0, seconds_text_size.w, seconds_text_size.h);
|
||||||
grect_align(&seconds_rect, ×tamp_rect, GAlignBottomRight, true);
|
grect_align(&seconds_rect, ×tamp_rect, GAlignBottomRight, true);
|
||||||
graphics_draw_text(ctx, seconds_text, layer_data->font_small, seconds_rect, GTextOverflowModeFill,
|
graphics_draw_text(ctx, seconds_text, font_small, seconds_rect, GTextOverflowModeFill,
|
||||||
GTextAlignmentLeft, NULL);
|
GTextAlignmentLeft, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,11 +57,7 @@ void clock_layer_init(Window *window, GPoint position) {
|
||||||
GRect window_bounds = layer_get_unobstructed_bounds(window_root_layer);
|
GRect window_bounds = layer_get_unobstructed_bounds(window_root_layer);
|
||||||
|
|
||||||
s_clock_layer =
|
s_clock_layer =
|
||||||
layer_create_with_data(GRect(position.x, position.y, window_bounds.size.w - position.x, CLOCK_H), sizeof(ClockLayerData));
|
layer_create(GRect(position.x, position.y, window_bounds.size.w - position.x, CLOCK_H));
|
||||||
|
|
||||||
ClockLayerData *layer_data = layer_get_data(s_clock_layer);
|
|
||||||
layer_data->font_big = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_TORO_38));
|
|
||||||
layer_data->font_small = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_KONEKO_TORO_28));
|
|
||||||
|
|
||||||
layer_set_update_proc(s_clock_layer, clock_layer_update_proc);
|
layer_set_update_proc(s_clock_layer, clock_layer_update_proc);
|
||||||
layer_add_child(window_root_layer, s_clock_layer);
|
layer_add_child(window_root_layer, s_clock_layer);
|
||||||
|
|
@ -73,8 +68,5 @@ void clock_layer_tick(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void clock_layer_deinit(void) {
|
void clock_layer_deinit(void) {
|
||||||
ClockLayerData *layer_data = layer_get_data(s_clock_layer);
|
|
||||||
fonts_unload_custom_font(layer_data->font_big);
|
|
||||||
fonts_unload_custom_font(layer_data->font_small);
|
|
||||||
layer_destroy(s_clock_layer);
|
layer_destroy(s_clock_layer);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,15 @@
|
||||||
#include <pebble.h>
|
#include <pebble.h>
|
||||||
#include "main_window.h"
|
#include "main_window.h"
|
||||||
|
#include "resources_service.h"
|
||||||
|
|
||||||
inline void init(void) {
|
inline void init(void) {
|
||||||
|
resources_service_init();
|
||||||
main_window_init();
|
main_window_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void deinit(void) {
|
inline void deinit(void) {
|
||||||
main_window_deinit();
|
main_window_deinit();
|
||||||
|
resources_service_deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
|
|
||||||
45
src/c/resources_service.c
Normal file
45
src/c/resources_service.c
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
#include "resources_service.h"
|
||||||
|
|
||||||
|
typedef struct ResourcesService {
|
||||||
|
GFont font_koneko_toro;
|
||||||
|
GFont font_toro;
|
||||||
|
GBitmap *character_even;
|
||||||
|
GBitmap *character_odd;
|
||||||
|
} ResourcesService;
|
||||||
|
|
||||||
|
static ResourcesService *s_resources_service;
|
||||||
|
|
||||||
|
void resources_service_init(void) {
|
||||||
|
s_resources_service = malloc(sizeof(ResourcesService));
|
||||||
|
s_resources_service->font_koneko_toro =
|
||||||
|
fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_KONEKO_TORO_28));
|
||||||
|
s_resources_service->font_toro =
|
||||||
|
fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_TORO_38));
|
||||||
|
s_resources_service->character_even = gbitmap_create_with_resource(RESOURCE_ID_CHARACTER_EVEN);
|
||||||
|
s_resources_service->character_odd = gbitmap_create_with_resource(RESOURCE_ID_CHARACTER_ODD);
|
||||||
|
}
|
||||||
|
|
||||||
|
void resources_service_deinit(void) {
|
||||||
|
fonts_unload_custom_font(s_resources_service->font_koneko_toro);
|
||||||
|
fonts_unload_custom_font(s_resources_service->font_toro);
|
||||||
|
gbitmap_destroy(s_resources_service->character_even);
|
||||||
|
gbitmap_destroy(s_resources_service->character_odd);
|
||||||
|
free(s_resources_service);
|
||||||
|
}
|
||||||
|
|
||||||
|
GBitmap *resources_service_get_character(int ticks) {
|
||||||
|
if (ticks % 2 == 0) {
|
||||||
|
return s_resources_service->character_even;
|
||||||
|
}
|
||||||
|
return s_resources_service->character_odd;
|
||||||
|
}
|
||||||
|
|
||||||
|
GFont resources_service_get_custom_font(CustomFont font) {
|
||||||
|
switch (font) {
|
||||||
|
case CustomFontKonekoToro:
|
||||||
|
return s_resources_service->font_koneko_toro;
|
||||||
|
case CustomFontToro:
|
||||||
|
return s_resources_service->font_toro;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
17
src/c/resources_service.h
Normal file
17
src/c/resources_service.h
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#ifndef RESOURCES_SERVICE_H_
|
||||||
|
#define RESOURCES_SERVICE_H_
|
||||||
|
|
||||||
|
#include <pebble.h>
|
||||||
|
|
||||||
|
typedef enum CustomFont {
|
||||||
|
CustomFontKonekoToro,
|
||||||
|
CustomFontToro
|
||||||
|
} CustomFont;
|
||||||
|
|
||||||
|
void resources_service_init(void);
|
||||||
|
void resources_service_deinit(void);
|
||||||
|
|
||||||
|
GBitmap *resources_service_get_character(int ticks);
|
||||||
|
GFont resources_service_get_custom_font(CustomFont font);
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Add table
Add a link
Reference in a new issue