Skip to content

Commit

Permalink
implement rolling multiple dice in Dice
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufmte committed Sep 15, 2022
1 parent 6d23a14 commit 52e4af5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
50 changes: 40 additions & 10 deletions src/displayapp/screens/Dice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,37 @@ static void btnRollEventHandler(lv_obj_t* obj, lv_event_t event) {
Dice::Dice(DisplayApp* app, System::SystemTask& systemTask) : Screen(app), systemTask {systemTask} {
srand(xTaskGetTickCount() % (std::numeric_limits<unsigned int>::max()));

nCounter.Create();
lv_obj_align(nCounter.GetObject(), nullptr, 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, 14, 80);
lv_obj_align(dLabel, lv_scr_act(), LV_ALIGN_IN_TOP_LEFT, 57, 80);

sidesCounter.Create();
lv_obj_align(sidesCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 49, 24);
sidesCounter.SetValue(2);
dCounter.Create();
lv_obj_align(dCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 83, 24);
dCounter.SetValue(2);

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

resultLabel = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(resultLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76);
lv_obj_set_style_local_text_color(resultLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, resultColors[currentColorIndex]);
lv_obj_align(resultLabel, nullptr, LV_ALIGN_IN_TOP_RIGHT, -110, 60);
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);

Roll();

Expand All @@ -52,11 +67,26 @@ Dice::~Dice() {
}

void Dice::Roll() {
lv_label_set_text_fmt(resultLabel, "%d", ((rand() % sidesCounter.GetValue()) + 1));
uint8_t resultIndividual;
uint16_t resultTotal = 0;

lv_label_set_text(resultIndividualLabel, "");

for (uint8_t i = 0; i < nCounter.GetValue(); i++) {
resultIndividual = ((rand() % dCounter.GetValue()) + 1);
resultTotal += resultIndividual;
lv_label_ins_text(resultIndividualLabel, LV_LABEL_POS_LAST, std::to_string(resultIndividual).c_str());
if (i < (nCounter.GetValue() - 1)) {
lv_label_ins_text(resultIndividualLabel, LV_LABEL_POS_LAST, "+");
}
}

lv_label_set_text_fmt(resultTotalLabel, "%d", resultTotal);
NextColor();
}

void Dice::NextColor() {
currentColorIndex = (currentColorIndex + 1) % resultColors.size();
lv_obj_set_style_local_text_color(resultLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, resultColors[currentColorIndex]);
lv_obj_set_style_local_text_color(resultTotalLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, resultColors[currentColorIndex]);
lv_obj_set_style_local_text_color(resultIndividualLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, resultColors[currentColorIndex]);
}
6 changes: 4 additions & 2 deletions src/displayapp/screens/Dice.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ namespace Pinetime::Applications::Screens {

lv_obj_t* btnRoll;
lv_obj_t* btnRollLabel;
lv_obj_t* resultLabel;
lv_obj_t* resultTotalLabel;
lv_obj_t* resultIndividualLabel;

std::array<lv_color_t, 3> resultColors = {LV_COLOR_YELLOW, LV_COLOR_MAGENTA, LV_COLOR_AQUA};
uint8_t currentColorIndex;
void NextColor();

Widgets::Counter sidesCounter = Widgets::Counter(2, 99, jetbrains_mono_42);
Widgets::Counter nCounter = Widgets::Counter(1, 9, jetbrains_mono_42);
Widgets::Counter dCounter = Widgets::Counter(2, 99, jetbrains_mono_42);
};
}

0 comments on commit 52e4af5

Please sign in to comment.