forked from InfiniTimeOrg/InfiniTime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit for Dice.h and Dice.cpp
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "displayapp/screens/Dice.h" | ||
|
||
#include "displayapp/screens/Screen.h" | ||
#include "displayapp/screens/Symbols.h" | ||
|
||
#include <lvgl/lvgl.h> | ||
|
||
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(); | ||
} | ||
} | ||
|
||
Dice::Dice(DisplayApp* app, Controllers::DateTime& dateTime) : Screen(app), dateTime {dateTime} { | ||
|
||
srand(dateTime.Uptime().count() * dateTime.Seconds()); | ||
|
||
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); | ||
|
||
sidesCounter.Create(); | ||
lv_obj_align(sidesCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 49, 24); | ||
sidesCounter.SetValue(2); | ||
|
||
currentColorIndex = rand() % resultColorsLength; | ||
|
||
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); | ||
|
||
Roll(); | ||
|
||
btnRoll = lv_btn_create(lv_scr_act(), nullptr); | ||
btnRoll->user_data = this; | ||
lv_obj_set_event_cb(btnRoll, btnRollEventHandler); | ||
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); | ||
} | ||
|
||
Dice::~Dice() { | ||
lv_obj_clean(lv_scr_act()); | ||
} | ||
|
||
void Dice::Roll() { | ||
rollResult = ((rand() % sidesCounter.GetValue()) + 1); | ||
lv_label_set_text_fmt(resultLabel, "%d", rollResult); | ||
NextColor(); | ||
} | ||
|
||
void Dice::NextColor() { | ||
currentColorIndex = (currentColorIndex + 1) % resultColorsLength; | ||
lv_obj_set_style_local_text_color(resultLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, resultColors[currentColorIndex]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include "displayapp/screens/Screen.h" | ||
#include "displayapp/widgets/Counter.h" | ||
#include "components/datetime/DateTimeController.h" | ||
#include <lvgl/lvgl.h> | ||
|
||
namespace Pinetime::Applications::Screens { | ||
class Dice : public Screen { | ||
public: | ||
Dice(DisplayApp* app, Controllers::DateTime& dateTime); | ||
~Dice() override; | ||
void Roll(); | ||
void NextColor(); | ||
|
||
private: | ||
Controllers::DateTime& dateTime; | ||
|
||
lv_obj_t* btnRoll; | ||
lv_obj_t* btnRollLabel; | ||
lv_obj_t* resultLabel; | ||
|
||
lv_color_t resultColors[3] = {LV_COLOR_YELLOW, LV_COLOR_MAGENTA, LV_COLOR_AQUA}; | ||
uint8_t resultColorsLength = sizeof(resultColors) / sizeof(resultColors[0]); | ||
uint8_t currentColorIndex; | ||
|
||
uint8_t rollResult; | ||
|
||
Widgets::Counter sidesCounter = Widgets::Counter(2, 99, jetbrains_mono_42); | ||
}; | ||
} |