Skip to content

Commit

Permalink
clean up labels in Dice.cpp and remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufmte committed Sep 18, 2022
1 parent ffb67d4 commit f0cc97c
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions src/displayapp/screens/Dice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,46 @@

using namespace Pinetime::Applications::Screens;

static void btnRollEventHandler(lv_obj_t* obj, lv_event_t event) {
auto* screen = static_cast<Dice*>(obj->user_data);
if (event == LV_EVENT_CLICKED) {
screen->Roll();
namespace {
lv_obj_t* MakeLabel(lv_font_t* font, lv_color_t color, lv_label_long_mode_t longMode, uint8_t width, lv_label_align_t labelAlignment, const char* text, lv_obj_t* reference, lv_align_t alignment, int8_t x, int8_t y) {
lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, font);
lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, color);
lv_label_set_long_mode(label, longMode);
if (width != 0) {
lv_obj_set_width(label, width);
}
lv_label_set_align(label, labelAlignment);
lv_label_set_text(label, text);
lv_obj_align(label, reference, alignment, x, y);
return label;
}

static void btnRollEventHandler(lv_obj_t* obj, lv_event_t event) {
auto* screen = static_cast<Dice*>(obj->user_data);
if (event == LV_EVENT_CLICKED) {
screen->Roll();
}
}
}

Dice::Dice(DisplayApp* app) : Screen(app) {
srand(xTaskGetTickCount() % (std::numeric_limits<unsigned int>::max()));

nCounter.Create();
lv_obj_align(nCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 24);
lv_obj_align(nCounter.GetObject(), lv_scr_act(), LV_ALIGN_IN_TOP_LEFT, 0, 24);
nCounter.SetValue(1);

lv_obj_t* dLabel = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(dLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
lv_obj_set_style_local_text_color(dLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
lv_label_set_text_static(dLabel, "d");
lv_obj_align(dLabel, lv_scr_act(), LV_ALIGN_IN_TOP_LEFT, 57, 80);
lv_obj_t* dLabel = MakeLabel(&jetbrains_mono_42, LV_COLOR_WHITE, LV_LABEL_LONG_EXPAND, 0, LV_LABEL_ALIGN_LEFT, "d", nCounter.GetObject(), LV_ALIGN_OUT_RIGHT_MID, 0, 0);

dCounter.Create();
lv_obj_align(dCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 83, 24);
lv_obj_align(dCounter.GetObject(), dLabel, LV_ALIGN_OUT_RIGHT_MID, 2, 0);
dCounter.SetValue(6);

currentColorIndex = rand() % resultColors.size();

resultTotalLabel = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(resultTotalLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
lv_obj_set_style_local_text_color(resultTotalLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, resultColors[currentColorIndex]);
lv_label_set_long_mode(resultTotalLabel, LV_LABEL_LONG_BREAK);
lv_obj_set_width(resultTotalLabel, 120);
lv_label_set_align(resultTotalLabel, LV_LABEL_ALIGN_CENTER);
lv_obj_align(resultTotalLabel, nullptr, LV_ALIGN_IN_TOP_RIGHT, 11, 25);

resultIndividualLabel = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(resultIndividualLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_bold_20);
lv_obj_set_style_local_text_color(resultIndividualLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, resultColors[currentColorIndex]);
lv_label_set_long_mode(resultIndividualLabel, LV_LABEL_LONG_BREAK);
lv_obj_set_width(resultIndividualLabel, 90);
lv_label_set_align(resultIndividualLabel, LV_LABEL_ALIGN_CENTER);
lv_obj_align(resultIndividualLabel, nullptr, LV_ALIGN_IN_TOP_RIGHT, -4, 75);
resultTotalLabel = MakeLabel(&jetbrains_mono_42, resultColors[currentColorIndex], LV_LABEL_LONG_BREAK, 120, LV_LABEL_ALIGN_CENTER, "", lv_scr_act(), LV_ALIGN_IN_TOP_RIGHT, 13, 30);
resultIndividualLabel = MakeLabel(&jetbrains_mono_bold_20, resultColors[currentColorIndex], LV_LABEL_LONG_BREAK, 90, LV_LABEL_ALIGN_CENTER, "", resultTotalLabel, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

Roll();

Expand All @@ -57,9 +56,7 @@ Dice::Dice(DisplayApp* app) : Screen(app) {
lv_obj_set_size(btnRoll, 240, 50);
lv_obj_align(btnRoll, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0);

btnRollLabel = lv_label_create(lv_scr_act(), nullptr);
lv_obj_align(btnRollLabel, btnRoll, LV_ALIGN_CENTER, 12, 0);
lv_label_set_text_static(btnRollLabel, Symbols::dice);
btnRollLabel = MakeLabel(&jetbrains_mono_bold_20, LV_COLOR_WHITE, LV_LABEL_LONG_EXPAND, 0, LV_LABEL_ALIGN_CENTER, Symbols::dice, btnRoll, LV_ALIGN_CENTER, 0, 0);
}

Dice::~Dice() {
Expand Down

0 comments on commit f0cc97c

Please sign in to comment.