static void calib_touch_up(calib_screen_t* s, point_t p) { (void)p; if (s->sample_count[s->ref_pt_idx] < NUM_SAMPLES_PER_POINT) return; if (++s->ref_pt_idx < NUM_CALIB_POINTS) { s->sample_idx = 0; widget_invalidate(s->widget); } else { int i,j; s->calib_complete = 1; point_t avg_sample[NUM_CALIB_POINTS]; for (i = 0; i < NUM_CALIB_POINTS; ++i) { avg_sample[i].x = 0; avg_sample[i].y = 0; for (j = 0; j < (int)s->sample_count[i]; ++j) { avg_sample[i].x += s->sampled_pts[i][j].x; avg_sample[i].y += s->sampled_pts[i][j].y; } avg_sample[i].x /= s->sample_count[i]; avg_sample[i].y /= s->sample_count[i]; } touch_set_calib(ref_pts, avg_sample); widget_hide(s->lbl_instructions); widget_show(s->recal_button); widget_show(s->complete_button); } }
static void place_quantity_widgets(home_screen_t* s) { int i; rect_t rect = widget_get_rect(s->stage_widget); sensor_info_t* active_sensors[NUM_SENSORS]; int num_active_sensors = 0; for (i = 0; i < NUM_SENSORS; ++i) { if (s->sensors[i].enabled) { widget_show(s->sensors[i].quantity_widget); active_sensors[num_active_sensors++] = &s->sensors[i]; } else widget_hide(s->sensors[i].quantity_widget); } if (num_active_sensors == 0) { widget_show(s->sensors[SENSOR_1].quantity_widget); active_sensors[num_active_sensors++] = &s->sensors[SENSOR_1]; } for (i = 0; i < num_active_sensors; ++i) { rect_t wrect = widget_get_rect(active_sensors[i]->quantity_widget); int spacing = (rect.height - (num_active_sensors * wrect.height)) / (num_active_sensors + 1); wrect.y = (spacing * (i + 1)) + (wrect.height * i); widget_set_rect(active_sensors[i]->quantity_widget, wrect); } }
static void listbox_layout(widget_t* w) { int i; listbox_t* l = widget_get_instance_data(w); int visible_items = num_visible_items(w); for (i = 0; i < widget_num_children(w); ++i) { int visible_index = i - l->pos; widget_t* child = widget_get_child(w, i); if (visible_index >= 0 && visible_index < visible_items) { rect_t child_rect = widget_get_rect(child); child_rect.y = (visible_index * l->item_height) + ((l->item_height - child_rect.height) / 2); widget_set_rect(child, child_rect); widget_show(child); } else { widget_hide(child); } } widget_enable(l->up_button, (l->pos > 0)); widget_enable(l->dn_button, (l->pos < (widget_num_children(w) - visible_items))); }
void gui_loop() { SDL_Event event; GuiWidget *root_widget = gui_root_widget(); if (root_widget == NULL) { printf("ERROR: no found root widget\n"); return; } widget_show(root_widget); root_widget = gui_main_widget(); widget_show(root_widget); while (gui->running) { timer_exec(); if (gui_update() == 0) SDL_Delay(10); if (SDL_PollEvent(&event)) { GuiWidget *cur_widget = (GuiWidget*)stack_peek(gui->win_stack); switch (event.type) { case SDL_VIDEORESIZE: break; case SDL_MOUSEBUTTONDOWN: printf("mouse (%d, %d)\n", event.button.x, event.button.y); break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_SPACE) { gui->running = 0; break; } if (cur_widget == NULL) continue; if (event.key.keysym.sym == SDLK_ESCAPE) widget_close(cur_widget); else { widget_process_event(cur_widget, (void *)&event); } break; case SDL_QUIT: gui->running = 0; break; } } } SDL_Quit(); }
static void restart_calib(button_event_t* event) { if (event->id == EVT_BUTTON_CLICK) { widget_t* screen_widget = widget_get_parent(event->widget); calib_screen_t* s = widget_get_instance_data(screen_widget); s->calib_complete = false; s->sample_idx = 0; s->ref_pt_idx = 0; memset(s->sample_count, 0, sizeof(s->sample_count)); widget_show(s->lbl_instructions); widget_hide(s->recal_button); widget_hide(s->complete_button); widget_invalidate(screen_widget); } }
void textentry_screen_show(textentry_format_t format, text_handler_t text_handler, void* user_data) { int i; textentry_screen_t* screen = calloc(1, sizeof(textentry_screen_t)); screen->text_handler = text_handler; screen->user_data = user_data; switch (format) { case TXT_FMT_IP: screen->btn_layout = btn_layout_numeric; screen->num_rows = NUM_ROWS_NUMERIC; break; default: case TXT_FMT_ANY: screen->btn_layout = btn_layout_all; screen->num_rows = NUM_ROWS_ALL; break; } screen->widget = widget_create(NULL, &textentry_screen_widget_class, screen, display_rect); rect_t rect = { .x = 7, .y = 5, .width = 48, .height = 48, }; widget_t* back_btn = button_create(screen->widget, rect, img_cancel, WHITE, BLACK, back_button_clicked); button_set_up_bg_color(back_btn, BLACK); button_set_up_icon_color(back_btn, WHITE); button_set_down_bg_color(back_btn, BLACK); button_set_down_icon_color(back_btn, LIGHT_GRAY); button_set_disabled_bg_color(back_btn, BLACK); button_set_disabled_icon_color(back_btn, DARK_GRAY); rect.x = 7; rect.y = 65; for (i = 0; i < NUM_VISIBLE_ROWS; ++i) { int j; for (j = 0; j < NUM_BUTTONS_PER_ROW; ++j) { widget_t* b = button_create(screen->widget, rect, img_circle, WHITE, BLACK, char_button_clicked); button_set_font(b, font_opensans_regular_22); button_set_up_bg_color(b, BLACK); button_set_up_icon_color(b, WHITE); button_set_down_bg_color(b, BLACK); button_set_down_icon_color(b, LIGHT_GRAY); button_set_disabled_bg_color(b, BLACK); button_set_disabled_icon_color(b, DARK_GRAY); screen->buttons[i][j] = b; rect.x += 52; } rect.y += 58; rect.x = 7; } rect.x = 268; rect.y = 5; widget_t* b = button_create(screen->widget, rect, img_check, WHITE, BLACK, ok_button_clicked); button_set_up_bg_color(b, BLACK); button_set_up_icon_color(b, WHITE); button_set_down_bg_color(b, BLACK); button_set_down_icon_color(b, LIGHT_GRAY); button_set_disabled_bg_color(b, BLACK); button_set_disabled_icon_color(b, DARK_GRAY); rect.y = 65; b = button_create(screen->widget, rect, img_backspace, WHITE, BLACK, backspace_button_clicked); button_set_up_bg_color(b, BLACK); button_set_up_icon_color(b, WHITE); button_set_down_bg_color(b, BLACK); button_set_down_icon_color(b, LIGHT_GRAY); button_set_disabled_bg_color(b, BLACK); button_set_disabled_icon_color(b, DARK_GRAY); rect.y += 58; b = button_create(screen->widget, rect, img_up, WHITE, BLACK, up_button_clicked); button_set_up_bg_color(b, BLACK); button_set_up_icon_color(b, WHITE); button_set_down_bg_color(b, BLACK); button_set_down_icon_color(b, LIGHT_GRAY); button_set_disabled_bg_color(b, BLACK); button_set_disabled_icon_color(b, DARK_GRAY); rect.y += 58; b = button_create(screen->widget, rect, img_down, WHITE, BLACK, down_button_clicked); button_set_up_bg_color(b, BLACK); button_set_up_icon_color(b, WHITE); button_set_down_bg_color(b, BLACK); button_set_down_icon_color(b, LIGHT_GRAY); button_set_disabled_bg_color(b, BLACK); button_set_disabled_icon_color(b, DARK_GRAY); update_input_buttons(screen); rect.x = 60; rect.y = 8; rect.width = 200; screen->text_label = label_create(screen->widget, rect, "", font_opensans_regular_22, WHITE, 2); widget_set_background(screen->text_label, LIGHT_GRAY); gui_push_screen(screen->widget); } static void update_input_buttons(textentry_screen_t* screen) { int i; for (i = 0; i < NUM_VISIBLE_ROWS; ++i) { btn_row_t* row = &screen->btn_layout[screen->row_idx + i]; int j; for (j = 0; j < NUM_BUTTONS_PER_ROW; ++j) { widget_t* btn = screen->buttons[i][j]; const char* btn_text = (*row)[j]; if (btn_text == NULL) widget_hide(btn); else { if (*btn_text == ' ') button_set_icon(btn, img_space); else button_set_icon(btn, img_circle); button_set_text(btn, btn_text); widget_show(btn); } } } }
void text_line_show(struct text_line* t) { widget_show(WIDGET_POINTER(t)); }
void spinbox_show(struct spinbox* b) { widget_show(WIDGET_POINTER(b)); }