implement left/right with callback
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
14
src/main.cpp
14
src/main.cpp
@@ -8,7 +8,7 @@
|
||||
#include "debug/DebugOverlay.h"
|
||||
#include "hal/u8g2_hal_sdl.h"
|
||||
#include "model/AppContext.h"
|
||||
#include "PushButton.h"
|
||||
#include "common/Common.h"
|
||||
#include "ui/Device.h"
|
||||
#include "ui/UIWidget.h"
|
||||
#include "ui/widgets/Button.h"
|
||||
@@ -82,27 +82,27 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
return SDL_APP_SUCCESS;
|
||||
|
||||
case SDLK_UP:
|
||||
device->OnButtonClicked(BUTTON_UP);
|
||||
device->OnButtonClicked(ButtonType::UP);
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
device->OnButtonClicked(BUTTON_DOWN);
|
||||
device->OnButtonClicked(ButtonType::DOWN);
|
||||
break;
|
||||
|
||||
case SDLK_LEFT:
|
||||
device->OnButtonClicked(BUTTON_LEFT);
|
||||
device->OnButtonClicked(ButtonType::LEFT);
|
||||
break;
|
||||
|
||||
case SDLK_RIGHT:
|
||||
device->OnButtonClicked(BUTTON_RIGHT);
|
||||
device->OnButtonClicked(ButtonType::RIGHT);
|
||||
break;
|
||||
|
||||
case SDLK_RETURN:
|
||||
device->OnButtonClicked(BUTTON_SELECT);
|
||||
device->OnButtonClicked(ButtonType::SELECT);
|
||||
break;
|
||||
|
||||
case SDLK_BACKSPACE:
|
||||
device->OnButtonClicked(BUTTON_BACK);
|
||||
device->OnButtonClicked(ButtonType::BACK);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@@ -68,6 +68,12 @@ 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
|
||||
};
|
||||
|
||||
options = {
|
||||
.u8g2 = &u8g2,
|
||||
.setScreen = [this](const std::shared_ptr<Widget> &screen) {
|
||||
@@ -79,17 +85,18 @@ Device::Device(void *appstate) : UIWidget(appstate)
|
||||
.popScreen = [this]() {
|
||||
this->PopScreen();
|
||||
},
|
||||
.persistence = &m_persistence,
|
||||
};
|
||||
widget = std::make_shared<SplashScreen>(&options);
|
||||
m_widget = std::make_shared<SplashScreen>(&options);
|
||||
}
|
||||
|
||||
void Device::SetScreen(const std::shared_ptr<Widget> &screen)
|
||||
{
|
||||
if (screen != nullptr)
|
||||
{
|
||||
widget = screen;
|
||||
history.clear();
|
||||
history.emplace_back(widget);
|
||||
m_widget = screen;
|
||||
m_history.clear();
|
||||
m_history.emplace_back(m_widget);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,17 +104,17 @@ void Device::PushScreen(const std::shared_ptr<Widget> &screen)
|
||||
{
|
||||
if (screen != nullptr)
|
||||
{
|
||||
widget = screen;
|
||||
history.emplace_back(widget);
|
||||
m_widget = screen;
|
||||
m_history.emplace_back(m_widget);
|
||||
}
|
||||
}
|
||||
|
||||
void Device::PopScreen()
|
||||
{
|
||||
if (history.size() >= 2)
|
||||
if (m_history.size() >= 2)
|
||||
{
|
||||
history.pop_back();
|
||||
widget = history.back();
|
||||
m_history.pop_back();
|
||||
m_widget = m_history.back();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,6 +126,11 @@ void Device::PushKey(const SDL_Keycode key)
|
||||
SDL_PushEvent(&ev);
|
||||
}
|
||||
|
||||
void Device::savePersistence(const char *key, const char *value)
|
||||
{
|
||||
SDL_Log("Saving: %s = %s", key, value);
|
||||
}
|
||||
|
||||
void Device::RenderU8G2() const
|
||||
{
|
||||
SDL_Surface *u8g2_surface = nullptr;
|
||||
@@ -191,10 +203,10 @@ void Device::DrawScreen() const
|
||||
{
|
||||
u8g2_ClearBuffer(&u8g2);
|
||||
|
||||
if (widget != nullptr)
|
||||
if (m_widget != nullptr)
|
||||
{
|
||||
widget->update(SDL_GetTicks());
|
||||
widget->render();
|
||||
m_widget->update(SDL_GetTicks());
|
||||
m_widget->render();
|
||||
}
|
||||
|
||||
RenderU8G2();
|
||||
@@ -236,11 +248,11 @@ void Device::ReleaseTap(const SDL_MouseButtonEvent *event) const
|
||||
}
|
||||
|
||||
|
||||
void Device::OnButtonClicked(const uint8_t button) const
|
||||
void Device::OnButtonClicked(const ButtonType button) const
|
||||
{
|
||||
if (widget != nullptr)
|
||||
if (m_widget != nullptr)
|
||||
{
|
||||
widget->onButtonClicked(button);
|
||||
m_widget->onButtonClicked(button);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -5,12 +5,14 @@
|
||||
#include <vector>
|
||||
|
||||
#include "UIWidget.h"
|
||||
#include "persistence.h"
|
||||
#include "common/Common.h"
|
||||
#include "common/Widget.h"
|
||||
#include "model/AppContext.h"
|
||||
|
||||
class Device final : public UIWidget
|
||||
{
|
||||
public:
|
||||
public:
|
||||
explicit Device(void *appstate);
|
||||
|
||||
void Render() const override;
|
||||
@@ -19,7 +21,7 @@ class Device final : public UIWidget
|
||||
|
||||
void ReleaseTap(const SDL_MouseButtonEvent *event) const;
|
||||
|
||||
void OnButtonClicked(uint8_t button) const;
|
||||
void OnButtonClicked(ButtonType button) const;
|
||||
|
||||
[[nodiscard]] bool IsHit(int mouse_x, int mouse_y) const override;
|
||||
|
||||
@@ -27,7 +29,7 @@ class Device final : public UIWidget
|
||||
|
||||
void ReleaseTap(int mouse_x, int mouse_y) override;
|
||||
|
||||
private:
|
||||
private:
|
||||
void DrawBackground() const;
|
||||
|
||||
void DrawScreen() const;
|
||||
@@ -42,7 +44,10 @@ class Device final : public UIWidget
|
||||
|
||||
static void PushKey(SDL_Keycode key);
|
||||
|
||||
static void savePersistence(const char *key, const char *value);
|
||||
|
||||
std::vector<std::shared_ptr<UIWidget>> m_children{};
|
||||
std::shared_ptr<Widget> widget;
|
||||
std::vector<std::shared_ptr<Widget>> history;
|
||||
};
|
||||
std::shared_ptr<Widget> m_widget;
|
||||
std::vector<std::shared_ptr<Widget>> m_history;
|
||||
persistence_t m_persistence{};
|
||||
};
|
Reference in New Issue
Block a user