Reduce duplication in Twos (#1274)

This commit is contained in:
Riku Isokoski 2022-09-11 21:57:49 +03:00 committed by GitHub
parent c9a5c3fa5c
commit c853681d75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 54 deletions

View File

@ -7,53 +7,34 @@ using namespace Pinetime::Applications::Screens;
Twos::Twos(Pinetime::Applications::DisplayApp* app) : Screen(app) { Twos::Twos(Pinetime::Applications::DisplayApp* app) : Screen(app) {
// create styles to apply to different valued tiles struct colorPair {
lv_style_init(&style_cell1); lv_color_t bg;
lv_style_init(&style_cell2); lv_color_t fg;
lv_style_init(&style_cell3); };
lv_style_init(&style_cell4);
lv_style_init(&style_cell5);
lv_style_set_border_color(&style_cell1, LV_STATE_DEFAULT, lv_color_hex(0xbbada0)); static constexpr colorPair colors[nColors] = {
lv_style_set_border_width(&style_cell1, LV_STATE_DEFAULT, 3); {LV_COLOR_MAKE(0xcd, 0xc0, 0xb4), LV_COLOR_BLACK},
lv_style_set_bg_opa(&style_cell1, LV_STATE_DEFAULT, LV_OPA_COVER); {LV_COLOR_MAKE(0xef, 0xdf, 0xc6), LV_COLOR_BLACK},
lv_style_set_bg_color(&style_cell1, LV_STATE_DEFAULT, lv_color_hex(0xcdc0b4)); {LV_COLOR_MAKE(0xef, 0x92, 0x63), LV_COLOR_WHITE},
lv_style_set_pad_top(&style_cell1, LV_STATE_DEFAULT, 29); {LV_COLOR_MAKE(0xf7, 0x61, 0x42), LV_COLOR_WHITE},
lv_style_set_text_color(&style_cell1, LV_STATE_DEFAULT, LV_COLOR_BLACK); {LV_COLOR_MAKE(0x00, 0x7d, 0xc5), LV_COLOR_WHITE},
};
lv_style_set_border_color(&style_cell2, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
lv_style_set_border_width(&style_cell2, LV_STATE_DEFAULT, 3);
lv_style_set_bg_opa(&style_cell2, LV_STATE_DEFAULT, LV_OPA_COVER);
lv_style_set_bg_color(&style_cell2, LV_STATE_DEFAULT, lv_color_hex(0xefdfc6));
lv_style_set_pad_top(&style_cell2, LV_STATE_DEFAULT, 29);
lv_style_set_text_color(&style_cell2, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_style_set_border_color(&style_cell3, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
lv_style_set_border_width(&style_cell3, LV_STATE_DEFAULT, 3);
lv_style_set_bg_opa(&style_cell3, LV_STATE_DEFAULT, LV_OPA_COVER);
lv_style_set_bg_color(&style_cell3, LV_STATE_DEFAULT, lv_color_hex(0xef9263));
lv_style_set_pad_top(&style_cell3, LV_STATE_DEFAULT, 29);
lv_style_set_border_color(&style_cell4, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
lv_style_set_border_width(&style_cell4, LV_STATE_DEFAULT, 3);
lv_style_set_bg_opa(&style_cell4, LV_STATE_DEFAULT, LV_OPA_COVER);
lv_style_set_bg_color(&style_cell4, LV_STATE_DEFAULT, lv_color_hex(0xf76142));
lv_style_set_pad_top(&style_cell4, LV_STATE_DEFAULT, 29);
lv_style_set_border_color(&style_cell5, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
lv_style_set_border_width(&style_cell5, LV_STATE_DEFAULT, 3);
lv_style_set_bg_opa(&style_cell5, LV_STATE_DEFAULT, LV_OPA_COVER);
lv_style_set_bg_color(&style_cell5, LV_STATE_DEFAULT, lv_color_hex(0x007dc5));
lv_style_set_pad_top(&style_cell5, LV_STATE_DEFAULT, 29);
// format grid display
gridDisplay = lv_table_create(lv_scr_act(), nullptr); gridDisplay = lv_table_create(lv_scr_act(), nullptr);
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL1, &style_cell1);
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL2, &style_cell2); for (size_t i = 0; i < nColors; i++) {
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL3, &style_cell3); lv_style_init(&cellStyles[i]);
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL4, &style_cell4);
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL4 + 1, &style_cell5); lv_style_set_border_color(&cellStyles[i], LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
lv_style_set_border_width(&cellStyles[i], LV_STATE_DEFAULT, 3);
lv_style_set_bg_opa(&cellStyles[i], LV_STATE_DEFAULT, LV_OPA_COVER);
lv_style_set_bg_color(&cellStyles[i], LV_STATE_DEFAULT, colors[i].bg);
lv_style_set_pad_top(&cellStyles[i], LV_STATE_DEFAULT, 29);
lv_style_set_text_color(&cellStyles[i], LV_STATE_DEFAULT, colors[i].fg);
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL1 + i, &cellStyles[i]);
}
lv_table_set_col_cnt(gridDisplay, nCols); lv_table_set_col_cnt(gridDisplay, nCols);
lv_table_set_row_cnt(gridDisplay, nRows); lv_table_set_row_cnt(gridDisplay, nRows);
for (int col = 0; col < nCols; col++) { for (int col = 0; col < nCols; col++) {
@ -83,11 +64,9 @@ Twos::Twos(Pinetime::Applications::DisplayApp* app) : Screen(app) {
} }
Twos::~Twos() { Twos::~Twos() {
lv_style_reset(&style_cell1); for (lv_style_t cellStyle : cellStyles) {
lv_style_reset(&style_cell2); lv_style_reset(&cellStyle);
lv_style_reset(&style_cell3); }
lv_style_reset(&style_cell4);
lv_style_reset(&style_cell5);
lv_obj_clean(lv_scr_act()); lv_obj_clean(lv_scr_act());
} }

View File

@ -18,11 +18,8 @@ namespace Pinetime {
bool OnTouchEvent(TouchEvents event) override; bool OnTouchEvent(TouchEvents event) override;
private: private:
lv_style_t style_cell1; static constexpr int nColors = 5;
lv_style_t style_cell2; lv_style_t cellStyles[nColors];
lv_style_t style_cell3;
lv_style_t style_cell4;
lv_style_t style_cell5;
lv_obj_t* scoreText; lv_obj_t* scoreText;
lv_obj_t* gridDisplay; lv_obj_t* gridDisplay;