Instantly share code, notes, and snippets.
Created
May 19, 2013 01:10
-
Star
1
(1)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save martijnthe/5606284 to your computer and use it in GitHub Desktop.
MenuLayer code fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BUFFER_SIZE 25 | |
static const uint8_t s_music_launcher_icon_pixels[] = { | |
0xff, 0xff, 0x1f, 0x00, 0xff, 0xff, 0x01, 0x00, 0xff, 0x3f, 0x00, 0x00, 0xff, 0x03, 0x00, 0x00, /* bytes 0 - 16 */ | |
0x7f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x18, 0x00, 0x7f, 0x00, 0x1f, 0x00, /* bytes 16 - 32 */ | |
0x7f, 0xf0, 0x1f, 0x00, 0x7f, 0xfc, 0x1f, 0x00, 0x7f, 0xfc, 0x1f, 0x00, 0x7f, 0xfc, 0x1f, 0x00, /* bytes 32 - 48 */ | |
0x7f, 0xfc, 0x1f, 0x00, 0x7f, 0xfc, 0x1f, 0x00, 0x7f, 0xfc, 0x1f, 0x00, 0x7f, 0xfc, 0x1f, 0x00, /* bytes 48 - 64 */ | |
0x7f, 0xfc, 0x1f, 0x00, 0x7f, 0xfc, 0x1f, 0x00, 0x7f, 0xfc, 0x00, 0x00, 0x7f, 0x7c, 0x00, 0x00, /* bytes 64 - 80 */ | |
0x03, 0x3c, 0x00, 0x00, 0x01, 0x3c, 0x00, 0x00, 0x00, 0x3c, 0x80, 0x00, 0x00, 0x3c, 0xc0, 0x00, /* bytes 80 - 96 */ | |
0x00, 0x7e, 0xe0, 0x00, 0x00, 0xff, 0xff, 0x00, 0x81, 0xff, 0xff, 0x00, | |
}; | |
static const GBitmap s_music_launcher_icon_bitmap = { | |
.addr = (void*) &s_music_launcher_icon_pixels, | |
.row_size_bytes = 4, | |
.info_flags = 0x1000, | |
.bounds = { | |
.origin = { .x = 0, .y = 0 }, | |
.size = { .w = 24, .h = 27 }, | |
}, | |
}; | |
typedef struct { | |
Window window; | |
MenuLayer menu_layer; | |
GBitmap icon; | |
Window detail_window; | |
TextLayer detail_text; | |
char detail_text_buffer[50]; | |
} AppData; | |
static uint16_t get_num_sections_callback(struct MenuLayer *menu_layer, AppData *data) { | |
(void)data; | |
(void)menu_layer; | |
return 4; | |
} | |
static uint16_t get_num_rows_callback(struct MenuLayer *menu_layer, uint16_t section_index, AppData *data) { | |
(void)data; | |
(void)menu_layer; | |
switch (section_index) { | |
default: | |
case 0: return 2; | |
case 1: return 3; | |
case 2: return 4; | |
case 3: return 5; | |
} | |
} | |
static int16_t get_cell_height_callback(struct MenuLayer *menu_layer, MenuIndex *cell_index, AppData *data) { | |
(void)data; | |
(void)menu_layer; | |
(void)cell_index; | |
// Variable row heights demo: | |
switch (cell_index->row % 3) { | |
default: | |
case 0: | |
return 44; | |
case 1: | |
return 64; | |
case 2: | |
return 84; | |
} | |
} | |
static int16_t get_header_height_callback(struct MenuLayer *menu_layer, uint16_t section_index, AppData *data) { | |
(void)data; | |
(void)menu_layer; | |
(void)section_index; | |
return MENU_CELL_BASIC_HEADER_HEIGHT; | |
} | |
static void draw_row_callback(GContext* ctx, Layer *cell_layer, MenuIndex *cell_index, AppData *data) { | |
char title[BUFFER_SIZE]; | |
char subtitle[BUFFER_SIZE]; | |
switch (cell_index->row % 2) { | |
case 0: | |
sniprintf(title, BUFFER_SIZE, "Title %i/%i ", cell_index->section, cell_index->row); | |
sniprintf(subtitle, BUFFER_SIZE, "Subtitle %i/%i", cell_index->section, cell_index->row); | |
menu_cell_basic_draw(ctx, cell_layer, title, subtitle, (GBitmap*)&s_music_launcher_icon_bitmap); | |
break; | |
default: | |
case 1: | |
sniprintf(title, BUFFER_SIZE, "Only Title %i/%i", cell_index->section, cell_index->row); | |
menu_cell_title_draw(ctx, cell_layer, title); | |
break; | |
} | |
(void)data; | |
} | |
static void draw_header_callback(GContext* ctx, Layer *cell_layer, uint16_t section_index, AppData *data) { | |
char title[BUFFER_SIZE]; | |
sniprintf(title, BUFFER_SIZE, "Section Header (%i)", section_index); | |
menu_cell_basic_header_draw(ctx, cell_layer, title); | |
(void)data; | |
} | |
static void detail_window_load(Window *window) { | |
AppData *data = window_get_user_data(window); | |
TextLayer *t = &data->detail_text; | |
text_layer_init(t, window->layer.bounds); | |
text_layer_set_text(t, data->detail_text_buffer); | |
layer_add_child(&window->layer, &t->layer); | |
} | |
static void push_detail_window(AppData *data, MenuIndex *index, bool is_long_click) { | |
sniprintf(data->detail_text_buffer, 50, "SELECTION:\n\nSection %i, Row %i\nLong click: %c", index->section, index->row, is_long_click ? 'Y' : 'N'); | |
Window *detail_window = &data->detail_window; | |
window_init(detail_window, WINDOW_NAME("Demo Menu Detail")); | |
window_set_user_data(detail_window, data); | |
window_set_window_handlers(detail_window, (WindowHandlers) { | |
.load = detail_window_load, | |
}); | |
const bool animated = true; | |
window_stack_push(detail_window, animated); | |
} | |
static void select_callback(MenuLayer *menu_layer, MenuIndex *cell_index, AppData *data) { | |
push_detail_window(data, cell_index, false); | |
(void)menu_layer; | |
} | |
static void select_long_callback(MenuLayer *menu_layer, MenuIndex *cell_index, AppData *data) { | |
push_detail_window(data, cell_index, true); | |
(void)menu_layer; | |
} | |
static void window_load(Window *window) { | |
AppData *data = window_get_user_data(window); | |
MenuLayer *menu_layer = &data->menu_layer; | |
menu_layer_init(menu_layer, window->layer.bounds); | |
menu_layer_set_callbacks(menu_layer, data, (MenuLayerCallbacks) { | |
.get_num_sections = (MenuLayerGetNumberOfSectionsCallback) get_num_sections_callback, | |
.get_num_rows = (MenuLayerGetNumberOfRowsInSectionsCallback) get_num_rows_callback, | |
.get_cell_height = (MenuLayerGetCellHeightCallback) get_cell_height_callback, | |
.get_header_height = (MenuLayerGetHeaderHeightCallback) get_header_height_callback, | |
.draw_row = (MenuLayerDrawRowCallback) draw_row_callback, | |
.draw_header = (MenuLayerDrawHeaderCallback) draw_header_callback, | |
.select_click = (MenuLayerSelectCallback) select_callback, | |
.select_long_click = (MenuLayerSelectCallback) select_long_callback, | |
}); | |
menu_layer_set_click_config_onto_window(menu_layer, window); | |
layer_add_child(&window->layer, menu_layer_get_layer(menu_layer)); | |
} | |
static void push_window(AppData *data) { | |
Window *window = &data->window; | |
window_init(window, WINDOW_NAME("Demo Menu")); | |
window_set_user_data(window, data); | |
window_set_window_handlers(window, (WindowHandlers) { | |
.load = window_load, | |
}); | |
const bool animated = true; | |
window_stack_push(window, animated); | |
} | |
//////////////////// | |
// App boilerplate | |
static void app_init(AppContextRef app_ctx) { | |
AppData *data = (AppData*) malloc(sizeof(AppData)); | |
if (data == NULL) { | |
PBL_CROAK("Out of memory"); | |
} | |
app_set_user_data(app_ctx, data); | |
push_window(data); | |
} | |
static void app_deinit(AppContextRef app_ctx) { | |
AppData *data = app_get_user_data(app_ctx); | |
if (data) { | |
free(data); | |
} | |
} | |
static void app_main(void *params) { | |
static PebbleAppHandlers s_handlers = { | |
.init_handler = app_init, | |
.deinit_handler = app_deinit, | |
}; | |
app_event_loop(params, &s_handlers); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment