feat(layers): character layer
This commit is contained in:
parent
685640a605
commit
db1f966e0a
8 changed files with 97 additions and 5 deletions
67
src/c/character_layer.c
Normal file
67
src/c/character_layer.c
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#include "character_layer.h"
|
||||
|
||||
#define CHARACTER_H 80
|
||||
#define CHARACTER_STEP 4
|
||||
|
||||
static Layer *s_character_layer;
|
||||
static GBitmap *s_character_even;
|
||||
static GBitmap *s_character_odd;
|
||||
static int s_character_ticks;
|
||||
|
||||
static void character_layer_update_proc(Layer *layer, GContext *ctx) {
|
||||
// Get the character bitmap.
|
||||
GBitmap *bitmap;
|
||||
if (s_character_ticks % 2 == 0) {
|
||||
bitmap = s_character_even;
|
||||
} else {
|
||||
bitmap = s_character_odd;
|
||||
}
|
||||
|
||||
// Get the position of character bitmap and align it to bottom.
|
||||
GRect frame = gbitmap_get_bounds(bitmap);
|
||||
GRect frame_inside = layer_get_unobstructed_bounds(layer);
|
||||
grect_align(&frame, &frame_inside, GAlignBottom, true);
|
||||
|
||||
// Move the character.
|
||||
switch (s_character_ticks) {
|
||||
case 1:
|
||||
frame.origin.x += CHARACTER_STEP; // to right
|
||||
break;
|
||||
case 3:
|
||||
frame.origin.x -= CHARACTER_STEP; // to left
|
||||
break;
|
||||
}
|
||||
|
||||
// Draw the character.
|
||||
graphics_context_set_compositing_mode(ctx, GCompOpSet);
|
||||
graphics_draw_bitmap_in_rect(ctx, bitmap, frame);
|
||||
}
|
||||
|
||||
void character_layer_init(Window *window) {
|
||||
s_character_ticks = 0;
|
||||
|
||||
s_character_even = gbitmap_create_with_resource(RESOURCE_ID_CHARACTER_EVEN);
|
||||
s_character_odd = gbitmap_create_with_resource(RESOURCE_ID_CHARACTER_ODD);
|
||||
|
||||
Layer *window_root_layer = window_get_root_layer(window);
|
||||
GRect window_bounds = layer_get_unobstructed_bounds(window_root_layer);
|
||||
|
||||
s_character_layer =
|
||||
layer_create(GRect(0, window_bounds.size.h - CHARACTER_H, window_bounds.size.w, CHARACTER_H));
|
||||
layer_set_update_proc(s_character_layer, character_layer_update_proc);
|
||||
|
||||
layer_add_child(window_root_layer, s_character_layer);
|
||||
}
|
||||
|
||||
void character_layer_tick(void) {
|
||||
if (++s_character_ticks == 4) {
|
||||
s_character_ticks = 0;
|
||||
}
|
||||
layer_mark_dirty(s_character_layer);
|
||||
}
|
||||
|
||||
void character_layer_deinit(void) {
|
||||
layer_destroy(s_character_layer);
|
||||
gbitmap_destroy(s_character_even);
|
||||
gbitmap_destroy(s_character_odd);
|
||||
}
|
||||
10
src/c/character_layer.h
Normal file
10
src/c/character_layer.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef CHARACTER_LAYER_H_
|
||||
#define CHARACTER_LAYER_H_
|
||||
|
||||
#include <pebble.h>
|
||||
|
||||
void character_layer_init(Window *window);
|
||||
void character_layer_tick(void);
|
||||
void character_layer_deinit(void);
|
||||
|
||||
#endif
|
||||
|
|
@ -69,11 +69,10 @@ void clock_layer_init(Window *window, GPoint position) {
|
|||
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) {
|
||||
void clock_layer_tick(void) {
|
||||
layer_mark_dirty(s_clock_layer);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
#include <pebble.h>
|
||||
|
||||
void clock_layer_init(Window *window, GPoint position);
|
||||
void clock_layer_update(void);
|
||||
void clock_layer_tick(void);
|
||||
void clock_layer_deinit(void);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,20 +1,24 @@
|
|||
#include "main_window.h"
|
||||
#include "character_layer.h"
|
||||
#include "clock_layer.h"
|
||||
|
||||
static Window *s_main_window;
|
||||
|
||||
static void main_window_tick(struct tm *time, TimeUnits units) {
|
||||
clock_layer_update();
|
||||
character_layer_tick();
|
||||
clock_layer_tick();
|
||||
}
|
||||
|
||||
static void main_window_load(Window *window) {
|
||||
clock_layer_init(s_main_window, GPoint(0, 60));
|
||||
character_layer_init(s_main_window);
|
||||
clock_layer_init(s_main_window, GPoint(0, 40));
|
||||
tick_timer_service_subscribe(SECOND_UNIT, main_window_tick);
|
||||
}
|
||||
|
||||
static void main_window_unload(Window *window) {
|
||||
tick_timer_service_unsubscribe();
|
||||
clock_layer_deinit();
|
||||
character_layer_deinit();
|
||||
}
|
||||
|
||||
void main_window_init(void) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue