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:
@@ -2,7 +2,6 @@
|
||||
|
||||
#include "MenuOptions.h"
|
||||
#include "common/Widget.h"
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
|
||||
class ScreenSaver final : public Widget
|
||||
|
@@ -16,11 +16,7 @@ SplashScreen::SplashScreen(menu_options_t *options) : Widget(options->u8g2), m_o
|
||||
void SplashScreen::update(const uint64_t dt)
|
||||
{
|
||||
counter += dt;
|
||||
#ifndef ESP32
|
||||
if (counter > 3000)
|
||||
#else
|
||||
if (counter > 10)
|
||||
#endif
|
||||
if (counter >= 3000)
|
||||
{
|
||||
counter = 0;
|
||||
if (m_options && m_options->setScreen)
|
||||
|
@@ -1,5 +1,25 @@
|
||||
#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_log.h"
|
||||
#include "esp_mac.h"
|
||||
@@ -114,3 +134,4 @@ void persistence_deinit()
|
||||
|
||||
nvs_close(persistence_handle);
|
||||
}
|
||||
*/
|
12
src/main.cpp
12
src/main.cpp
@@ -21,6 +21,8 @@ constexpr unsigned int WINDOW_HEIGHT = (U8G2_SCREEN_HEIGHT * U8G2_SCREEN_FACTOR
|
||||
std::shared_ptr<Device> device;
|
||||
std::vector<std::shared_ptr<UIWidget>> widgets;
|
||||
|
||||
static uint64_t lastFrameTime = 0;
|
||||
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
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_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)
|
||||
{
|
||||
widget->Render();
|
||||
widget->Render(dt);
|
||||
}
|
||||
DebugOverlay::Render(context);
|
||||
|
||||
|
@@ -4,12 +4,15 @@
|
||||
#include <u8g2.h>
|
||||
|
||||
#include "MenuOptions.h"
|
||||
#include "common/InactivityTracker.h"
|
||||
#include "ui/ScreenSaver.h"
|
||||
#include "ui/SplashScreen.h"
|
||||
#include "ui/widgets/Button.h"
|
||||
#include "ui/widgets/D_Pad.h"
|
||||
|
||||
u8g2_t u8g2;
|
||||
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)
|
||||
{
|
||||
@@ -52,15 +55,11 @@ Device::Device(void *appstate) : UIWidget(appstate)
|
||||
m_children.push_back(std::make_shared<Button>(
|
||||
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,
|
||||
[]() {
|
||||
PushKey(SDLK_RETURN);
|
||||
}));
|
||||
[]() { PushKey(SDLK_RETURN); }));
|
||||
m_children.push_back(std::make_shared<Button>(
|
||||
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,
|
||||
[]() {
|
||||
PushKey(SDLK_BACKSPACE);
|
||||
}));
|
||||
[]() { PushKey(SDLK_BACKSPACE); }));
|
||||
m_children.push_back(std::make_shared<D_Pad>(
|
||||
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));
|
||||
@@ -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);
|
||||
u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
|
||||
|
||||
static char device_storage_name[] = "device_storage";
|
||||
m_persistence = {
|
||||
.name = device_storage_name,
|
||||
.save = savePersistence
|
||||
};
|
||||
m_persistence = {.save = savePersistence};
|
||||
|
||||
options = {
|
||||
.u8g2 = &u8g2,
|
||||
.setScreen = [this](const std::shared_ptr<Widget> &screen) {
|
||||
this->SetScreen(screen);
|
||||
},
|
||||
.pushScreen = [this](const std::shared_ptr<Widget> &screen) {
|
||||
this->PushScreen(screen);
|
||||
},
|
||||
.popScreen = [this]() {
|
||||
this->PopScreen();
|
||||
},
|
||||
.setScreen = [this](const std::shared_ptr<Widget> &screen) { this->SetScreen(screen); },
|
||||
.pushScreen = [this](const std::shared_ptr<Widget> &screen) { this->PushScreen(screen); },
|
||||
.popScreen = [this]() { this->PopScreen(); },
|
||||
.persistence = &m_persistence,
|
||||
};
|
||||
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)
|
||||
@@ -199,28 +192,30 @@ void Device::DrawBackground() const
|
||||
SDL_RenderFillRect(GetContext()->MainRenderer(), &rect);
|
||||
}
|
||||
|
||||
void Device::DrawScreen() const
|
||||
void Device::DrawScreen(const uint64_t dt) const
|
||||
{
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
|
||||
if (m_widget != nullptr)
|
||||
{
|
||||
m_widget->update(SDL_GetTicks());
|
||||
m_widget->update(dt);
|
||||
m_widget->render();
|
||||
}
|
||||
|
||||
RenderU8G2();
|
||||
}
|
||||
|
||||
void Device::Render() const
|
||||
void Device::Render(const uint64_t dt) const
|
||||
{
|
||||
DrawBackground();
|
||||
DrawScreen();
|
||||
DrawScreen(dt);
|
||||
|
||||
for (const auto &child : m_children)
|
||||
{
|
||||
child->Render();
|
||||
child->Render(dt);
|
||||
}
|
||||
|
||||
m_inactivityTracker->update(dt);
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
// SDL_Log("ReleaseTap: x=%f, y=%f, button=%d", event->x, event->y, event->button);
|
||||
m_inactivityTracker->reset();
|
||||
|
||||
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
|
||||
{
|
||||
m_inactivityTracker->reset();
|
||||
|
||||
if (m_widget != nullptr)
|
||||
{
|
||||
m_widget->onButtonClicked(button);
|
||||
|
@@ -15,7 +15,7 @@ class Device final : public UIWidget
|
||||
public:
|
||||
explicit Device(void *appstate);
|
||||
|
||||
void Render() const override;
|
||||
void Render(uint64_t dt) const override;
|
||||
|
||||
void HandleTap(const SDL_MouseButtonEvent *event) const;
|
||||
|
||||
@@ -32,7 +32,7 @@ public:
|
||||
private:
|
||||
void DrawBackground() const;
|
||||
|
||||
void DrawScreen() const;
|
||||
void DrawScreen(uint64_t dt) const;
|
||||
|
||||
void RenderU8G2() const;
|
||||
|
||||
|
@@ -9,7 +9,7 @@ public:
|
||||
|
||||
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;
|
||||
|
||||
|
@@ -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 =
|
||||
ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "button_normal.png");
|
||||
|
@@ -11,7 +11,7 @@ class Button final : public UIWidget
|
||||
public:
|
||||
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;
|
||||
|
||||
|
@@ -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 =
|
||||
ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "d-pad_normal.png");
|
||||
|
@@ -13,7 +13,7 @@ public:
|
||||
|
||||
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;
|
||||
|
||||
|
Reference in New Issue
Block a user