Skip to content

Commit c853681

Browse files
authored
Reduce duplication in Twos (InfiniTimeOrg#1274)
1 parent c9a5c3f commit c853681

File tree

2 files changed

+27
-51
lines changed

2 files changed

+27
-51
lines changed

src/displayapp/screens/Twos.cpp

+25-46
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,34 @@ using namespace Pinetime::Applications::Screens;
77

88
Twos::Twos(Pinetime::Applications::DisplayApp* app) : Screen(app) {
99

10-
// create styles to apply to different valued tiles
11-
lv_style_init(&style_cell1);
12-
lv_style_init(&style_cell2);
13-
lv_style_init(&style_cell3);
14-
lv_style_init(&style_cell4);
15-
lv_style_init(&style_cell5);
10+
struct colorPair {
11+
lv_color_t bg;
12+
lv_color_t fg;
13+
};
1614

17-
lv_style_set_border_color(&style_cell1, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
18-
lv_style_set_border_width(&style_cell1, LV_STATE_DEFAULT, 3);
19-
lv_style_set_bg_opa(&style_cell1, LV_STATE_DEFAULT, LV_OPA_COVER);
20-
lv_style_set_bg_color(&style_cell1, LV_STATE_DEFAULT, lv_color_hex(0xcdc0b4));
21-
lv_style_set_pad_top(&style_cell1, LV_STATE_DEFAULT, 29);
22-
lv_style_set_text_color(&style_cell1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
15+
static constexpr colorPair colors[nColors] = {
16+
{LV_COLOR_MAKE(0xcd, 0xc0, 0xb4), LV_COLOR_BLACK},
17+
{LV_COLOR_MAKE(0xef, 0xdf, 0xc6), LV_COLOR_BLACK},
18+
{LV_COLOR_MAKE(0xef, 0x92, 0x63), LV_COLOR_WHITE},
19+
{LV_COLOR_MAKE(0xf7, 0x61, 0x42), LV_COLOR_WHITE},
20+
{LV_COLOR_MAKE(0x00, 0x7d, 0xc5), LV_COLOR_WHITE},
21+
};
2322

24-
lv_style_set_border_color(&style_cell2, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
25-
lv_style_set_border_width(&style_cell2, LV_STATE_DEFAULT, 3);
26-
lv_style_set_bg_opa(&style_cell2, LV_STATE_DEFAULT, LV_OPA_COVER);
27-
lv_style_set_bg_color(&style_cell2, LV_STATE_DEFAULT, lv_color_hex(0xefdfc6));
28-
lv_style_set_pad_top(&style_cell2, LV_STATE_DEFAULT, 29);
29-
lv_style_set_text_color(&style_cell2, LV_STATE_DEFAULT, LV_COLOR_BLACK);
30-
31-
lv_style_set_border_color(&style_cell3, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
32-
lv_style_set_border_width(&style_cell3, LV_STATE_DEFAULT, 3);
33-
lv_style_set_bg_opa(&style_cell3, LV_STATE_DEFAULT, LV_OPA_COVER);
34-
lv_style_set_bg_color(&style_cell3, LV_STATE_DEFAULT, lv_color_hex(0xef9263));
35-
lv_style_set_pad_top(&style_cell3, LV_STATE_DEFAULT, 29);
23+
gridDisplay = lv_table_create(lv_scr_act(), nullptr);
3624

37-
lv_style_set_border_color(&style_cell4, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
38-
lv_style_set_border_width(&style_cell4, LV_STATE_DEFAULT, 3);
39-
lv_style_set_bg_opa(&style_cell4, LV_STATE_DEFAULT, LV_OPA_COVER);
40-
lv_style_set_bg_color(&style_cell4, LV_STATE_DEFAULT, lv_color_hex(0xf76142));
41-
lv_style_set_pad_top(&style_cell4, LV_STATE_DEFAULT, 29);
25+
for (size_t i = 0; i < nColors; i++) {
26+
lv_style_init(&cellStyles[i]);
4227

43-
lv_style_set_border_color(&style_cell5, LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
44-
lv_style_set_border_width(&style_cell5, LV_STATE_DEFAULT, 3);
45-
lv_style_set_bg_opa(&style_cell5, LV_STATE_DEFAULT, LV_OPA_COVER);
46-
lv_style_set_bg_color(&style_cell5, LV_STATE_DEFAULT, lv_color_hex(0x007dc5));
47-
lv_style_set_pad_top(&style_cell5, LV_STATE_DEFAULT, 29);
28+
lv_style_set_border_color(&cellStyles[i], LV_STATE_DEFAULT, lv_color_hex(0xbbada0));
29+
lv_style_set_border_width(&cellStyles[i], LV_STATE_DEFAULT, 3);
30+
lv_style_set_bg_opa(&cellStyles[i], LV_STATE_DEFAULT, LV_OPA_COVER);
31+
lv_style_set_bg_color(&cellStyles[i], LV_STATE_DEFAULT, colors[i].bg);
32+
lv_style_set_pad_top(&cellStyles[i], LV_STATE_DEFAULT, 29);
33+
lv_style_set_text_color(&cellStyles[i], LV_STATE_DEFAULT, colors[i].fg);
4834

49-
// format grid display
35+
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL1 + i, &cellStyles[i]);
36+
}
5037

51-
gridDisplay = lv_table_create(lv_scr_act(), nullptr);
52-
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL1, &style_cell1);
53-
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL2, &style_cell2);
54-
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL3, &style_cell3);
55-
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL4, &style_cell4);
56-
lv_obj_add_style(gridDisplay, LV_TABLE_PART_CELL4 + 1, &style_cell5);
5738
lv_table_set_col_cnt(gridDisplay, nCols);
5839
lv_table_set_row_cnt(gridDisplay, nRows);
5940
for (int col = 0; col < nCols; col++) {
@@ -83,11 +64,9 @@ Twos::Twos(Pinetime::Applications::DisplayApp* app) : Screen(app) {
8364
}
8465

8566
Twos::~Twos() {
86-
lv_style_reset(&style_cell1);
87-
lv_style_reset(&style_cell2);
88-
lv_style_reset(&style_cell3);
89-
lv_style_reset(&style_cell4);
90-
lv_style_reset(&style_cell5);
67+
for (lv_style_t cellStyle : cellStyles) {
68+
lv_style_reset(&cellStyle);
69+
}
9170
lv_obj_clean(lv_scr_act());
9271
}
9372

src/displayapp/screens/Twos.h

+2-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ namespace Pinetime {
1818
bool OnTouchEvent(TouchEvents event) override;
1919

2020
private:
21-
lv_style_t style_cell1;
22-
lv_style_t style_cell2;
23-
lv_style_t style_cell3;
24-
lv_style_t style_cell4;
25-
lv_style_t style_cell5;
21+
static constexpr int nColors = 5;
22+
lv_style_t cellStyles[nColors];
2623

2724
lv_obj_t* scoreText;
2825
lv_obj_t* gridDisplay;

0 commit comments

Comments
 (0)