implement ScreenSaver in simulator

set ruth to stub only, because of cross compile error

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-06-20 10:48:43 +02:00
parent d3dd96c93a
commit 1a912d31c4
11 changed files with 63 additions and 40 deletions

View File

@@ -2,7 +2,6 @@
#include "MenuOptions.h" #include "MenuOptions.h"
#include "common/Widget.h" #include "common/Widget.h"
#include <cstdlib>
#include <vector> #include <vector>
class ScreenSaver final : public Widget class ScreenSaver final : public Widget

View File

@@ -16,11 +16,7 @@ SplashScreen::SplashScreen(menu_options_t *options) : Widget(options->u8g2), m_o
void SplashScreen::update(const uint64_t dt) void SplashScreen::update(const uint64_t dt)
{ {
counter += dt; counter += dt;
#ifndef ESP32 if (counter >= 3000)
if (counter > 3000)
#else
if (counter > 10)
#endif
{ {
counter = 0; counter = 0;
if (m_options && m_options->setScreen) if (m_options && m_options->setScreen)

View File

@@ -1,5 +1,25 @@
#include "persistence.h" #include "persistence.h"
#include "stddef.h"
void *persistence_init(const char *namespace_name)
{
return NULL;
}
void persistence_save(persistence_value_t value_type, const char *key, const void *value)
{
}
void *persistence_load(persistence_value_t value_type, const char *key, void *out)
{
return NULL;
}
void persistence_deinit()
{
}
/*
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_mac.h" #include "esp_mac.h"
@@ -114,3 +134,4 @@ void persistence_deinit()
nvs_close(persistence_handle); nvs_close(persistence_handle);
} }
*/

View File

@@ -21,6 +21,8 @@ constexpr unsigned int WINDOW_HEIGHT = (U8G2_SCREEN_HEIGHT * U8G2_SCREEN_FACTOR
std::shared_ptr<Device> device; std::shared_ptr<Device> device;
std::vector<std::shared_ptr<UIWidget>> widgets; std::vector<std::shared_ptr<UIWidget>> widgets;
static uint64_t lastFrameTime = 0;
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{ {
if (SDL_Init(SDL_INIT_VIDEO) == false) if (SDL_Init(SDL_INIT_VIDEO) == false)
@@ -154,9 +156,17 @@ SDL_AppResult SDL_AppIterate(void *appstate)
SDL_SetRenderDrawColor(context->MainRenderer(), 0, 0, 0, 255); SDL_SetRenderDrawColor(context->MainRenderer(), 0, 0, 0, 255);
SDL_RenderClear(context->MainRenderer()); SDL_RenderClear(context->MainRenderer());
const uint64_t currentTime = SDL_GetTicks();
uint64_t dt = 0;
if (lastFrameTime > 0) {
dt = currentTime - lastFrameTime;
}
lastFrameTime = currentTime;
for (const auto &widget : widgets) for (const auto &widget : widgets)
{ {
widget->Render(); widget->Render(dt);
} }
DebugOverlay::Render(context); DebugOverlay::Render(context);

View File

@@ -4,12 +4,15 @@
#include <u8g2.h> #include <u8g2.h>
#include "MenuOptions.h" #include "MenuOptions.h"
#include "common/InactivityTracker.h"
#include "ui/ScreenSaver.h"
#include "ui/SplashScreen.h" #include "ui/SplashScreen.h"
#include "ui/widgets/Button.h" #include "ui/widgets/Button.h"
#include "ui/widgets/D_Pad.h" #include "ui/widgets/D_Pad.h"
u8g2_t u8g2; u8g2_t u8g2;
menu_options_t options; menu_options_t options;
std::unique_ptr<InactivityTracker> m_inactivityTracker;
static void set_pixel_rgba(const SDL_Surface *surface, const int x, const int y, const uint32_t pixel_color) static void set_pixel_rgba(const SDL_Surface *surface, const int x, const int y, const uint32_t pixel_color)
{ {
@@ -52,15 +55,11 @@ Device::Device(void *appstate) : UIWidget(appstate)
m_children.push_back(std::make_shared<Button>( m_children.push_back(std::make_shared<Button>(
GetContext(), U8G2_SCREEN_WIDTH * U8G2_SCREEN_FACTOR + 3 * U8G2_SCREEN_PADDING + DPAD_WIDTH, GetContext(), U8G2_SCREEN_WIDTH * U8G2_SCREEN_FACTOR + 3 * U8G2_SCREEN_PADDING + DPAD_WIDTH,
U8G2_SCREEN_HEIGHT * U8G2_SCREEN_FACTOR + U8G2_SCREEN_PADDING - BUTTON_WIDTH, BUTTON_WIDTH, U8G2_SCREEN_HEIGHT * U8G2_SCREEN_FACTOR + U8G2_SCREEN_PADDING - BUTTON_WIDTH, BUTTON_WIDTH,
[]() { []() { PushKey(SDLK_RETURN); }));
PushKey(SDLK_RETURN);
}));
m_children.push_back(std::make_shared<Button>( m_children.push_back(std::make_shared<Button>(
GetContext(), U8G2_SCREEN_WIDTH * U8G2_SCREEN_FACTOR + 4 * U8G2_SCREEN_PADDING + DPAD_WIDTH + BUTTON_WIDTH, GetContext(), U8G2_SCREEN_WIDTH * U8G2_SCREEN_FACTOR + 4 * U8G2_SCREEN_PADDING + DPAD_WIDTH + BUTTON_WIDTH,
U8G2_SCREEN_HEIGHT * U8G2_SCREEN_FACTOR + U8G2_SCREEN_PADDING - 2 * BUTTON_WIDTH, BUTTON_WIDTH, U8G2_SCREEN_HEIGHT * U8G2_SCREEN_FACTOR + U8G2_SCREEN_PADDING - 2 * BUTTON_WIDTH, BUTTON_WIDTH,
[]() { []() { PushKey(SDLK_BACKSPACE); }));
PushKey(SDLK_BACKSPACE);
}));
m_children.push_back(std::make_shared<D_Pad>( m_children.push_back(std::make_shared<D_Pad>(
GetContext(), U8G2_SCREEN_WIDTH * U8G2_SCREEN_FACTOR + 2 * U8G2_SCREEN_PADDING, GetContext(), U8G2_SCREEN_WIDTH * U8G2_SCREEN_FACTOR + 2 * U8G2_SCREEN_PADDING,
U8G2_SCREEN_HEIGHT * U8G2_SCREEN_FACTOR + U8G2_SCREEN_PADDING - DPAD_WIDTH, DPAD_WIDTH, dpad_callback)); U8G2_SCREEN_HEIGHT * U8G2_SCREEN_FACTOR + U8G2_SCREEN_PADDING - DPAD_WIDTH, DPAD_WIDTH, dpad_callback));
@@ -68,26 +67,20 @@ Device::Device(void *appstate) : UIWidget(appstate)
u8g2_Setup_sh1106_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_sdl_hw_spi, u8x8_gpio_and_delay_sdl); u8g2_Setup_sh1106_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_sdl_hw_spi, u8x8_gpio_and_delay_sdl);
u8x8_InitDisplay(u8g2_GetU8x8(&u8g2)); u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
static char device_storage_name[] = "device_storage"; m_persistence = {.save = savePersistence};
m_persistence = {
.name = device_storage_name,
.save = savePersistence
};
options = { options = {
.u8g2 = &u8g2, .u8g2 = &u8g2,
.setScreen = [this](const std::shared_ptr<Widget> &screen) { .setScreen = [this](const std::shared_ptr<Widget> &screen) { this->SetScreen(screen); },
this->SetScreen(screen); .pushScreen = [this](const std::shared_ptr<Widget> &screen) { this->PushScreen(screen); },
}, .popScreen = [this]() { this->PopScreen(); },
.pushScreen = [this](const std::shared_ptr<Widget> &screen) {
this->PushScreen(screen);
},
.popScreen = [this]() {
this->PopScreen();
},
.persistence = &m_persistence, .persistence = &m_persistence,
}; };
m_widget = std::make_shared<SplashScreen>(&options); m_widget = std::make_shared<SplashScreen>(&options);
m_inactivityTracker = std::make_unique<InactivityTracker>(60000, []() {
const auto screensaver = std::make_shared<ScreenSaver>(&options);
options.pushScreen(screensaver);
});
} }
void Device::SetScreen(const std::shared_ptr<Widget> &screen) void Device::SetScreen(const std::shared_ptr<Widget> &screen)
@@ -199,28 +192,30 @@ void Device::DrawBackground() const
SDL_RenderFillRect(GetContext()->MainRenderer(), &rect); SDL_RenderFillRect(GetContext()->MainRenderer(), &rect);
} }
void Device::DrawScreen() const void Device::DrawScreen(const uint64_t dt) const
{ {
u8g2_ClearBuffer(&u8g2); u8g2_ClearBuffer(&u8g2);
if (m_widget != nullptr) if (m_widget != nullptr)
{ {
m_widget->update(SDL_GetTicks()); m_widget->update(dt);
m_widget->render(); m_widget->render();
} }
RenderU8G2(); RenderU8G2();
} }
void Device::Render() const void Device::Render(const uint64_t dt) const
{ {
DrawBackground(); DrawBackground();
DrawScreen(); DrawScreen(dt);
for (const auto &child : m_children) for (const auto &child : m_children)
{ {
child->Render(); child->Render(dt);
} }
m_inactivityTracker->update(dt);
} }
void Device::HandleTap(const SDL_MouseButtonEvent *event) const void Device::HandleTap(const SDL_MouseButtonEvent *event) const
@@ -240,6 +235,7 @@ void Device::HandleTap(const SDL_MouseButtonEvent *event) const
void Device::ReleaseTap(const SDL_MouseButtonEvent *event) const void Device::ReleaseTap(const SDL_MouseButtonEvent *event) const
{ {
// SDL_Log("ReleaseTap: x=%f, y=%f, button=%d", event->x, event->y, event->button); // SDL_Log("ReleaseTap: x=%f, y=%f, button=%d", event->x, event->y, event->button);
m_inactivityTracker->reset();
for (const auto &child : m_children) for (const auto &child : m_children)
{ {
@@ -247,9 +243,10 @@ void Device::ReleaseTap(const SDL_MouseButtonEvent *event) const
} }
} }
void Device::OnButtonClicked(const ButtonType button) const void Device::OnButtonClicked(const ButtonType button) const
{ {
m_inactivityTracker->reset();
if (m_widget != nullptr) if (m_widget != nullptr)
{ {
m_widget->onButtonClicked(button); m_widget->onButtonClicked(button);

View File

@@ -15,7 +15,7 @@ class Device final : public UIWidget
public: public:
explicit Device(void *appstate); explicit Device(void *appstate);
void Render() const override; void Render(uint64_t dt) const override;
void HandleTap(const SDL_MouseButtonEvent *event) const; void HandleTap(const SDL_MouseButtonEvent *event) const;
@@ -32,7 +32,7 @@ public:
private: private:
void DrawBackground() const; void DrawBackground() const;
void DrawScreen() const; void DrawScreen(uint64_t dt) const;
void RenderU8G2() const; void RenderU8G2() const;

View File

@@ -9,7 +9,7 @@ public:
virtual ~UIWidget(); virtual ~UIWidget();
virtual void Render() const = 0; virtual void Render(uint64_t dt) const = 0;
[[nodiscard]] virtual bool IsHit(int mouse_x, int mouse_y) const = 0; [[nodiscard]] virtual bool IsHit(int mouse_x, int mouse_y) const = 0;

View File

@@ -9,7 +9,7 @@ Button::Button(void *appState, const float x, const float y, const float width,
{ {
} }
void Button::Render() const void Button::Render(const uint64_t dt) const
{ {
const auto button = const auto button =
ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "button_normal.png"); ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "button_normal.png");

View File

@@ -11,7 +11,7 @@ class Button final : public UIWidget
public: public:
Button(void *appState, float x, float y, float width, std::function<void()> callback); Button(void *appState, float x, float y, float width, std::function<void()> callback);
void Render() const override; void Render(uint64_t dt) const override;
[[nodiscard]] bool IsHit(int mouse_x, int mouse_y) const override; [[nodiscard]] bool IsHit(int mouse_x, int mouse_y) const override;

View File

@@ -8,7 +8,7 @@ D_Pad::D_Pad(void *appState, const float x, const float y, const float width, st
{ {
} }
void D_Pad::Render() const void D_Pad::Render(const uint64_t dt) const
{ {
const auto dPad = const auto dPad =
ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "d-pad_normal.png"); ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "d-pad_normal.png");

View File

@@ -13,7 +13,7 @@ public:
D_Pad(void *appState, float x, float y, float width, std::function<void(Direction)> callback); D_Pad(void *appState, float x, float y, float width, std::function<void(Direction)> callback);
void Render() const override; void Render(uint64_t dt) const override;
[[nodiscard]] bool IsHit(int mouse_x, int mouse_y) const override; [[nodiscard]] bool IsHit(int mouse_x, int mouse_y) const override;