Break into separate text layers
This commit is contained in:
98
src/c/main.c
98
src/c/main.c
@@ -10,10 +10,14 @@ static Window *s_result_window;
|
|||||||
static MenuLayer *s_main_menu_layer;
|
static MenuLayer *s_main_menu_layer;
|
||||||
static MenuLayer *s_cidr_menu_layer;
|
static MenuLayer *s_cidr_menu_layer;
|
||||||
static MenuLayer *s_mask_menu_layer;
|
static MenuLayer *s_mask_menu_layer;
|
||||||
static TextLayer *s_result_text_layer;
|
static TextLayer *s_cidr_text_layer;
|
||||||
|
static TextLayer *s_mask_text_layer;
|
||||||
|
static TextLayer *s_usable_hosts_text_layer;
|
||||||
static TextLayer *s_bitwidth_text_layer;
|
static TextLayer *s_bitwidth_text_layer;
|
||||||
|
|
||||||
static char s_result_text[192];
|
static char s_cidr_text[24];
|
||||||
|
static char s_mask_text[32];
|
||||||
|
static char s_usable_hosts_text[40];
|
||||||
static char s_bitwidth_text[36];
|
static char s_bitwidth_text[36];
|
||||||
static uint8_t s_current_prefix = 24;
|
static uint8_t s_current_prefix = 24;
|
||||||
static bool s_up_boundary_haptic_sent = false;
|
static bool s_up_boundary_haptic_sent = false;
|
||||||
@@ -63,7 +67,7 @@ static uint64_t prefix_to_usable_hosts(int prefix) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void layout_result_text_layer(void) {
|
static void layout_result_text_layer(void) {
|
||||||
if (!s_result_text_layer || !s_bitwidth_text_layer) {
|
if (!s_cidr_text_layer || !s_mask_text_layer || !s_usable_hosts_text_layer || !s_bitwidth_text_layer) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,27 +77,29 @@ static void layout_result_text_layer(void) {
|
|||||||
const int16_t width = bounds.size.w - (2 * side_margin);
|
const int16_t width = bounds.size.w - (2 * side_margin);
|
||||||
const int16_t gap = 2;
|
const int16_t gap = 2;
|
||||||
|
|
||||||
GSize main_size = text_layer_get_content_size(s_result_text_layer);
|
GSize cidr_size = text_layer_get_content_size(s_cidr_text_layer);
|
||||||
|
GSize mask_size = text_layer_get_content_size(s_mask_text_layer);
|
||||||
|
GSize usable_size = text_layer_get_content_size(s_usable_hosts_text_layer);
|
||||||
GSize bit_size = text_layer_get_content_size(s_bitwidth_text_layer);
|
GSize bit_size = text_layer_get_content_size(s_bitwidth_text_layer);
|
||||||
|
|
||||||
int16_t main_h = main_size.h;
|
int16_t cidr_h = cidr_size.h > 0 ? cidr_size.h : 1;
|
||||||
int16_t bit_h = bit_size.h;
|
int16_t mask_h = mask_size.h > 0 ? mask_size.h : 1;
|
||||||
|
int16_t usable_h = usable_size.h > 0 ? usable_size.h : 1;
|
||||||
|
int16_t bit_h = bit_size.h > 0 ? bit_size.h : 1;
|
||||||
|
|
||||||
if (main_h < 1) {
|
int16_t total_h = cidr_h + gap + mask_h + gap + usable_h + gap + bit_h;
|
||||||
main_h = 1;
|
|
||||||
}
|
|
||||||
if (bit_h < 1) {
|
|
||||||
bit_h = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t total_h = main_h + gap + bit_h;
|
|
||||||
if (total_h > bounds.size.h - 10) {
|
if (total_h > bounds.size.h - 10) {
|
||||||
total_h = bounds.size.h - 10;
|
total_h = bounds.size.h - 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t start_y = (bounds.size.h - total_h) / 2;
|
int16_t y = (bounds.size.h - total_h) / 2;
|
||||||
layer_set_frame(text_layer_get_layer(s_result_text_layer), GRect(side_margin, start_y, width, main_h));
|
layer_set_frame(text_layer_get_layer(s_cidr_text_layer), GRect(side_margin, y, width, cidr_h));
|
||||||
layer_set_frame(text_layer_get_layer(s_bitwidth_text_layer), GRect(side_margin, start_y + main_h + gap, width, bit_h));
|
y += cidr_h + gap;
|
||||||
|
layer_set_frame(text_layer_get_layer(s_mask_text_layer), GRect(side_margin, y, width, mask_h));
|
||||||
|
y += mask_h + gap;
|
||||||
|
layer_set_frame(text_layer_get_layer(s_usable_hosts_text_layer), GRect(side_margin, y, width, usable_h));
|
||||||
|
y += usable_h + gap;
|
||||||
|
layer_set_frame(text_layer_get_layer(s_bitwidth_text_layer), GRect(side_margin, y, width, bit_h));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void update_result_text(void) {
|
static void update_result_text(void) {
|
||||||
@@ -104,13 +110,9 @@ static void update_result_text(void) {
|
|||||||
|
|
||||||
mask_to_dotted(mask, mask_text, sizeof(mask_text));
|
mask_to_dotted(mask, mask_text, sizeof(mask_text));
|
||||||
|
|
||||||
snprintf(
|
snprintf(s_cidr_text, sizeof(s_cidr_text), "CIDR: /%d", s_current_prefix);
|
||||||
s_result_text,
|
snprintf(s_mask_text, sizeof(s_mask_text), "Mask: %s", mask_text);
|
||||||
sizeof(s_result_text),
|
snprintf(s_usable_hosts_text, sizeof(s_usable_hosts_text), "Usable Hosts: %llu", usable_hosts);
|
||||||
"CIDR: /%d\nMask: %s\nUsable Hosts: %llu",
|
|
||||||
s_current_prefix,
|
|
||||||
mask_text,
|
|
||||||
usable_hosts);
|
|
||||||
|
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
for (int i = 0; i < 32; i++) {
|
for (int i = 0; i < 32; i++) {
|
||||||
@@ -121,8 +123,10 @@ static void update_result_text(void) {
|
|||||||
}
|
}
|
||||||
s_bitwidth_text[pos] = '\0';
|
s_bitwidth_text[pos] = '\0';
|
||||||
|
|
||||||
if (s_result_text_layer && s_bitwidth_text_layer) {
|
if (s_cidr_text_layer && s_mask_text_layer && s_usable_hosts_text_layer && s_bitwidth_text_layer) {
|
||||||
text_layer_set_text(s_result_text_layer, s_result_text);
|
text_layer_set_text(s_cidr_text_layer, s_cidr_text);
|
||||||
|
text_layer_set_text(s_mask_text_layer, s_mask_text);
|
||||||
|
text_layer_set_text(s_usable_hosts_text_layer, s_usable_hosts_text);
|
||||||
text_layer_set_text(s_bitwidth_text_layer, s_bitwidth_text);
|
text_layer_set_text(s_bitwidth_text_layer, s_bitwidth_text);
|
||||||
layout_result_text_layer();
|
layout_result_text_layer();
|
||||||
}
|
}
|
||||||
@@ -331,12 +335,26 @@ static void mask_window_unload(Window *window) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void result_window_load(Window *window) {
|
static void result_window_load(Window *window) {
|
||||||
s_result_text_layer = text_layer_create(GRect(0, 0, PBL_DISPLAY_WIDTH, 24));
|
s_cidr_text_layer = text_layer_create(GRect(0, 0, PBL_DISPLAY_WIDTH, 24));
|
||||||
text_layer_set_background_color(s_result_text_layer, GColorClear);
|
text_layer_set_background_color(s_cidr_text_layer, GColorClear);
|
||||||
text_layer_set_text_color(s_result_text_layer, GColorWhite);
|
text_layer_set_text_color(s_cidr_text_layer, GColorWhite);
|
||||||
text_layer_set_font(s_result_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD));
|
text_layer_set_font(s_cidr_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD));
|
||||||
text_layer_set_overflow_mode(s_result_text_layer, GTextOverflowModeWordWrap);
|
text_layer_set_overflow_mode(s_cidr_text_layer, GTextOverflowModeTrailingEllipsis);
|
||||||
text_layer_set_text(s_result_text_layer, s_result_text);
|
text_layer_set_text(s_cidr_text_layer, s_cidr_text);
|
||||||
|
|
||||||
|
s_mask_text_layer = text_layer_create(GRect(0, 0, PBL_DISPLAY_WIDTH, 24));
|
||||||
|
text_layer_set_background_color(s_mask_text_layer, GColorClear);
|
||||||
|
text_layer_set_text_color(s_mask_text_layer, GColorWhite);
|
||||||
|
text_layer_set_font(s_mask_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD));
|
||||||
|
text_layer_set_overflow_mode(s_mask_text_layer, GTextOverflowModeTrailingEllipsis);
|
||||||
|
text_layer_set_text(s_mask_text_layer, s_mask_text);
|
||||||
|
|
||||||
|
s_usable_hosts_text_layer = text_layer_create(GRect(0, 0, PBL_DISPLAY_WIDTH, 24));
|
||||||
|
text_layer_set_background_color(s_usable_hosts_text_layer, GColorClear);
|
||||||
|
text_layer_set_text_color(s_usable_hosts_text_layer, GColorWhite);
|
||||||
|
text_layer_set_font(s_usable_hosts_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD));
|
||||||
|
text_layer_set_overflow_mode(s_usable_hosts_text_layer, GTextOverflowModeTrailingEllipsis);
|
||||||
|
text_layer_set_text(s_usable_hosts_text_layer, s_usable_hosts_text);
|
||||||
|
|
||||||
s_bitwidth_text_layer = text_layer_create(GRect(0, 0, PBL_DISPLAY_WIDTH, 14));
|
s_bitwidth_text_layer = text_layer_create(GRect(0, 0, PBL_DISPLAY_WIDTH, 14));
|
||||||
text_layer_set_background_color(s_bitwidth_text_layer, GColorClear);
|
text_layer_set_background_color(s_bitwidth_text_layer, GColorClear);
|
||||||
@@ -345,21 +363,29 @@ static void result_window_load(Window *window) {
|
|||||||
text_layer_set_text(s_bitwidth_text_layer, s_bitwidth_text);
|
text_layer_set_text(s_bitwidth_text_layer, s_bitwidth_text);
|
||||||
|
|
||||||
#if PBL_ROUND
|
#if PBL_ROUND
|
||||||
text_layer_set_text_alignment(s_result_text_layer, GTextAlignmentCenter);
|
text_layer_set_text_alignment(s_cidr_text_layer, GTextAlignmentCenter);
|
||||||
|
text_layer_set_text_alignment(s_mask_text_layer, GTextAlignmentCenter);
|
||||||
|
text_layer_set_text_alignment(s_usable_hosts_text_layer, GTextAlignmentCenter);
|
||||||
text_layer_set_text_alignment(s_bitwidth_text_layer, GTextAlignmentCenter);
|
text_layer_set_text_alignment(s_bitwidth_text_layer, GTextAlignmentCenter);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Layer *window_layer = window_get_root_layer(window);
|
Layer *window_layer = window_get_root_layer(window);
|
||||||
layer_add_child(window_layer, text_layer_get_layer(s_result_text_layer));
|
layer_add_child(window_layer, text_layer_get_layer(s_cidr_text_layer));
|
||||||
|
layer_add_child(window_layer, text_layer_get_layer(s_mask_text_layer));
|
||||||
|
layer_add_child(window_layer, text_layer_get_layer(s_usable_hosts_text_layer));
|
||||||
layer_add_child(window_layer, text_layer_get_layer(s_bitwidth_text_layer));
|
layer_add_child(window_layer, text_layer_get_layer(s_bitwidth_text_layer));
|
||||||
update_result_text();
|
update_result_text();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void result_window_unload(Window *window) {
|
static void result_window_unload(Window *window) {
|
||||||
(void)window;
|
(void)window;
|
||||||
text_layer_destroy(s_result_text_layer);
|
text_layer_destroy(s_cidr_text_layer);
|
||||||
|
text_layer_destroy(s_mask_text_layer);
|
||||||
|
text_layer_destroy(s_usable_hosts_text_layer);
|
||||||
text_layer_destroy(s_bitwidth_text_layer);
|
text_layer_destroy(s_bitwidth_text_layer);
|
||||||
s_result_text_layer = NULL;
|
s_cidr_text_layer = NULL;
|
||||||
|
s_mask_text_layer = NULL;
|
||||||
|
s_usable_hosts_text_layer = NULL;
|
||||||
s_bitwidth_text_layer = NULL;
|
s_bitwidth_text_layer = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user