handle mouse events
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
if (DEFINED ENV{IDF_PATH})
|
||||
return()
|
||||
endif()
|
||||
return()
|
||||
endif ()
|
||||
|
||||
add_library(components INTERFACE)
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
if (DEFINED ENV{IDF_PATH})
|
||||
return()
|
||||
endif()
|
||||
return()
|
||||
endif ()
|
||||
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
project(ImGui)
|
||||
|
@@ -1,17 +1,17 @@
|
||||
if (DEFINED ENV{IDF_PATH})
|
||||
idf_component_register(SRCS
|
||||
src/common/PSMenu.cpp
|
||||
src/common/ScrollBar.cpp
|
||||
src/common/Widget.cpp
|
||||
src/data/MenuItem.cpp
|
||||
src/ui/LightMenu.cpp
|
||||
src/ui/MainMenu.cpp
|
||||
src/ui/SettingsMenu.cpp
|
||||
src/ui/SplashScreen.cpp
|
||||
INCLUDE_DIRS "include"
|
||||
)
|
||||
return()
|
||||
endif()
|
||||
idf_component_register(SRCS
|
||||
src/common/PSMenu.cpp
|
||||
src/common/ScrollBar.cpp
|
||||
src/common/Widget.cpp
|
||||
src/data/MenuItem.cpp
|
||||
src/ui/LightMenu.cpp
|
||||
src/ui/MainMenu.cpp
|
||||
src/ui/SettingsMenu.cpp
|
||||
src/ui/SplashScreen.cpp
|
||||
INCLUDE_DIRS "include"
|
||||
)
|
||||
return()
|
||||
endif ()
|
||||
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
project(justus)
|
||||
|
@@ -3,13 +3,14 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "u8g2.h"
|
||||
#include "common/Widget.h"
|
||||
#include "u8g2.h"
|
||||
|
||||
class Widget;
|
||||
|
||||
typedef struct {
|
||||
u8g2_t* u8g2;
|
||||
typedef struct
|
||||
{
|
||||
u8g2_t *u8g2;
|
||||
|
||||
std::function<void(std::shared_ptr<Widget>)> setScreen;
|
||||
std::function<void(std::shared_ptr<Widget>)> pushScreen;
|
||||
|
@@ -8,19 +8,20 @@
|
||||
|
||||
typedef std::function<void(uint8_t)> MenuCallback;
|
||||
|
||||
class PSMenu : public Widget {
|
||||
public:
|
||||
explicit PSMenu(menu_options_t* options);
|
||||
class PSMenu : public Widget
|
||||
{
|
||||
public:
|
||||
explicit PSMenu(menu_options_t *options);
|
||||
~PSMenu() override;
|
||||
|
||||
void render() override;
|
||||
void onButtonClicked(uint8_t button) override;
|
||||
|
||||
void addText(const std::string& text, const MenuCallback& callback);
|
||||
void addSwitch(const std::string& text, std::string& value, const MenuCallback& callback);
|
||||
void addNumber(const std::string& text, std::string& value, const MenuCallback& callback);
|
||||
void addText(const std::string &text, const MenuCallback &callback);
|
||||
void addSwitch(const std::string &text, std::string &value, const MenuCallback &callback);
|
||||
void addNumber(const std::string &text, std::string &value, const MenuCallback &callback);
|
||||
|
||||
private:
|
||||
private:
|
||||
void onPressedDown();
|
||||
void onPressedUp();
|
||||
void onPressedLeft();
|
||||
|
@@ -3,13 +3,14 @@
|
||||
#include "MenuOptions.h"
|
||||
#include "Widget.h"
|
||||
|
||||
class ScrollBar final : public Widget {
|
||||
public:
|
||||
class ScrollBar final : public Widget
|
||||
{
|
||||
public:
|
||||
ScrollBar(const menu_options_t *options, size_t x, size_t y, size_t width, size_t height);
|
||||
void render() override;
|
||||
void refresh(size_t value, size_t max, size_t min = 0);
|
||||
|
||||
private:
|
||||
private:
|
||||
size_t m_x;
|
||||
size_t m_y;
|
||||
size_t m_width;
|
||||
|
@@ -4,9 +4,10 @@
|
||||
|
||||
#include "u8g2.h"
|
||||
|
||||
class Widget {
|
||||
public:
|
||||
explicit Widget(u8g2_t* u8g2);
|
||||
class Widget
|
||||
{
|
||||
public:
|
||||
explicit Widget(u8g2_t *u8g2);
|
||||
|
||||
virtual ~Widget() = default;
|
||||
|
||||
@@ -16,6 +17,6 @@ public:
|
||||
|
||||
virtual void onButtonClicked(uint8_t button);
|
||||
|
||||
protected:
|
||||
u8g2_t* u8g2;
|
||||
protected:
|
||||
u8g2_t *u8g2;
|
||||
};
|
||||
|
@@ -4,14 +4,11 @@
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
class MenuItem {
|
||||
public:
|
||||
class MenuItem
|
||||
{
|
||||
public:
|
||||
MenuItem(uint8_t type, std::string text, std::function<void(uint8_t)> callback);
|
||||
MenuItem(
|
||||
uint8_t type,
|
||||
std::string text,
|
||||
std::string value,
|
||||
std::function<void(uint8_t)> callback);
|
||||
MenuItem(uint8_t type, std::string text, std::string value, std::function<void(uint8_t)> callback);
|
||||
[[nodiscard]] uint8_t getType() const;
|
||||
[[nodiscard]] const std::string &getText() const;
|
||||
[[nodiscard]] const std::string &getValue() const;
|
||||
@@ -19,7 +16,7 @@ public:
|
||||
void callback(uint8_t id) const;
|
||||
[[nodiscard]] bool hasCallback() const;
|
||||
|
||||
private:
|
||||
private:
|
||||
uint8_t m_type;
|
||||
std::string m_text;
|
||||
std::string m_value;
|
||||
|
@@ -4,6 +4,6 @@
|
||||
|
||||
class LightMenu : public PSMenu
|
||||
{
|
||||
public:
|
||||
explicit LightMenu(menu_options_t* options);
|
||||
public:
|
||||
explicit LightMenu(menu_options_t *options);
|
||||
};
|
||||
|
@@ -2,12 +2,13 @@
|
||||
|
||||
#include "common/PSMenu.h"
|
||||
|
||||
class MainMenu final : public PSMenu {
|
||||
public:
|
||||
explicit MainMenu(menu_options_t* options);
|
||||
class MainMenu final : public PSMenu
|
||||
{
|
||||
public:
|
||||
explicit MainMenu(menu_options_t *options);
|
||||
|
||||
private:
|
||||
private:
|
||||
void onSelect(uint8_t id) const;
|
||||
|
||||
menu_options_t* m_options;
|
||||
menu_options_t *m_options;
|
||||
};
|
||||
|
@@ -4,6 +4,6 @@
|
||||
|
||||
class SettingsMenu : public PSMenu
|
||||
{
|
||||
public:
|
||||
explicit SettingsMenu(menu_options_t* options) ;
|
||||
public:
|
||||
explicit SettingsMenu(menu_options_t *options);
|
||||
};
|
||||
|
@@ -3,12 +3,13 @@
|
||||
#include "MenuOptions.h"
|
||||
#include "common/Widget.h"
|
||||
|
||||
class SplashScreen final : public Widget {
|
||||
public:
|
||||
explicit SplashScreen(menu_options_t* options);
|
||||
class SplashScreen final : public Widget
|
||||
{
|
||||
public:
|
||||
explicit SplashScreen(menu_options_t *options);
|
||||
void update(uint64_t dt) override;
|
||||
void render() override;
|
||||
|
||||
private:
|
||||
menu_options_t* m_options;
|
||||
private:
|
||||
menu_options_t *m_options;
|
||||
};
|
||||
|
@@ -1,21 +1,23 @@
|
||||
#include "common/PSMenu.h"
|
||||
|
||||
#include "u8g2.h"
|
||||
#include "common/ScrollBar.h"
|
||||
#include "PushButton.h"
|
||||
#include "common/ScrollBar.h"
|
||||
#include "u8g2.h"
|
||||
|
||||
PSMenu::PSMenu(menu_options_t* options)
|
||||
: Widget(options->u8g2)
|
||||
, m_options(options) {
|
||||
PSMenu::PSMenu(menu_options_t *options) : Widget(options->u8g2), m_options(options)
|
||||
{
|
||||
m_options->onButtonClicked = [this](const uint8_t button) { onButtonClicked(button); };
|
||||
}
|
||||
|
||||
PSMenu::~PSMenu() {
|
||||
PSMenu::~PSMenu()
|
||||
{
|
||||
m_options->onButtonClicked = nullptr;
|
||||
}
|
||||
|
||||
void PSMenu::render() {
|
||||
if(m_selected_item < 0) {
|
||||
void PSMenu::render()
|
||||
{
|
||||
if (m_selected_item < 0)
|
||||
{
|
||||
onPressedDown();
|
||||
}
|
||||
|
||||
@@ -29,31 +31,24 @@ void PSMenu::render() {
|
||||
|
||||
int x = 8; // sure?
|
||||
auto widget = m_items.at(m_selected_item);
|
||||
renderWidget(
|
||||
widget.getType(), u8g2_font_helvB08_tr, x, u8g2->height / 2 + 3, widget.getText().c_str());
|
||||
renderWidget(widget.getType(), u8g2_font_helvB08_tr, x, u8g2->height / 2 + 3, widget.getText().c_str());
|
||||
|
||||
if(m_selected_item > 0) {
|
||||
if (m_selected_item > 0)
|
||||
{
|
||||
auto item = m_items.at(m_selected_item - 1);
|
||||
renderWidget(item.getType(), u8g2_font_haxrcorp4089_tr, x, 14, item.getText().c_str());
|
||||
}
|
||||
if(m_selected_item < m_items.size() - 1) {
|
||||
if (m_selected_item < m_items.size() - 1)
|
||||
{
|
||||
auto item = m_items.at(m_selected_item + 1);
|
||||
renderWidget(
|
||||
item.getType(),
|
||||
u8g2_font_haxrcorp4089_tr,
|
||||
x,
|
||||
u8g2->height - 10,
|
||||
item.getText().c_str());
|
||||
renderWidget(item.getType(), u8g2_font_haxrcorp4089_tr, x, u8g2->height - 10, item.getText().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void PSMenu::renderWidget(
|
||||
const uint8_t type,
|
||||
const uint8_t* font,
|
||||
const int x,
|
||||
const int y,
|
||||
const char* text) const {
|
||||
switch(type) {
|
||||
void PSMenu::renderWidget(const uint8_t type, const uint8_t *font, const int x, const int y, const char *text) const
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case 0: // text
|
||||
u8g2_SetFont(u8g2, font);
|
||||
u8g2_DrawStr(u8g2, x, y, text);
|
||||
@@ -64,8 +59,10 @@ void PSMenu::renderWidget(
|
||||
}
|
||||
}
|
||||
|
||||
void PSMenu::onButtonClicked(uint8_t button) {
|
||||
switch(button) {
|
||||
void PSMenu::onButtonClicked(uint8_t button)
|
||||
{
|
||||
switch (button)
|
||||
{
|
||||
case BUTTON_UP:
|
||||
onPressedUp();
|
||||
break;
|
||||
@@ -95,59 +92,77 @@ void PSMenu::onButtonClicked(uint8_t button) {
|
||||
}
|
||||
}
|
||||
|
||||
void PSMenu::onPressedDown() {
|
||||
if(m_selected_item == m_items.size() - 1) {
|
||||
void PSMenu::onPressedDown()
|
||||
{
|
||||
if (m_selected_item == m_items.size() - 1)
|
||||
{
|
||||
m_selected_item = 0;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
m_selected_item++;
|
||||
}
|
||||
}
|
||||
|
||||
void PSMenu::onPressedUp() {
|
||||
if(m_selected_item == 0) {
|
||||
void PSMenu::onPressedUp()
|
||||
{
|
||||
if (m_selected_item == 0)
|
||||
{
|
||||
m_selected_item = m_items.size() - 1;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
m_selected_item--;
|
||||
}
|
||||
}
|
||||
|
||||
void PSMenu::onPressedLeft() {
|
||||
void PSMenu::onPressedLeft()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
void PSMenu::onPressedRight() {
|
||||
void PSMenu::onPressedRight()
|
||||
{
|
||||
///
|
||||
}
|
||||
|
||||
void PSMenu::onPressedSelect() const {
|
||||
void PSMenu::onPressedSelect() const
|
||||
{
|
||||
m_items.at(m_selected_item).callback(m_selected_item);
|
||||
}
|
||||
|
||||
void PSMenu::onPressedBack() const {
|
||||
if(m_options && m_options->popScreen) {
|
||||
void PSMenu::onPressedBack() const
|
||||
{
|
||||
if (m_options && m_options->popScreen)
|
||||
{
|
||||
m_options->popScreen();
|
||||
}
|
||||
}
|
||||
|
||||
void PSMenu::addText(const std::string& text, const MenuCallback& callback) {
|
||||
void PSMenu::addText(const std::string &text, const MenuCallback &callback)
|
||||
{
|
||||
m_items.emplace_back(0, text, callback);
|
||||
}
|
||||
|
||||
void PSMenu::addSwitch(const std::string& text, std::string& value, const MenuCallback& callback) {
|
||||
void PSMenu::addSwitch(const std::string &text, std::string &value, const MenuCallback &callback)
|
||||
{
|
||||
m_items.emplace_back(1, text, value, callback);
|
||||
}
|
||||
|
||||
void PSMenu::addNumber(const std::string& text, std::string& value, const MenuCallback& callback) {
|
||||
void PSMenu::addNumber(const std::string &text, std::string &value, const MenuCallback &callback)
|
||||
{
|
||||
m_items.emplace_back(2, text, value, callback);
|
||||
}
|
||||
|
||||
void PSMenu::drawScrollBar() const {
|
||||
void PSMenu::drawScrollBar() const
|
||||
{
|
||||
ScrollBar scrollBar(m_options, u8g2->width - 3, 3, 1, u8g2->height - 6);
|
||||
scrollBar.refresh(m_selected_item, m_items.size());
|
||||
scrollBar.render();
|
||||
}
|
||||
|
||||
void PSMenu::drawSelectionBox() const {
|
||||
void PSMenu::drawSelectionBox() const
|
||||
{
|
||||
const auto displayHeight = u8g2->height;
|
||||
const auto displayWidth = u8g2->width;
|
||||
constexpr auto rightPadding = 8;
|
||||
|
@@ -1,29 +1,22 @@
|
||||
#include "common/ScrollBar.h"
|
||||
|
||||
ScrollBar::ScrollBar(
|
||||
const menu_options_t* options,
|
||||
const size_t x,
|
||||
const size_t y,
|
||||
const size_t width,
|
||||
const size_t height)
|
||||
: Widget(options->u8g2)
|
||||
, m_x(x)
|
||||
, m_y(y)
|
||||
, m_width(width)
|
||||
, m_height(height)
|
||||
, m_value(0)
|
||||
, m_max(0)
|
||||
, m_min(0)
|
||||
, m_thumbHeight(0)
|
||||
, m_thumbY(0) {
|
||||
ScrollBar::ScrollBar(const menu_options_t *options, const size_t x, const size_t y, const size_t width,
|
||||
const size_t height)
|
||||
: Widget(options->u8g2), m_x(x), m_y(y), m_width(width), m_height(height), m_value(0), m_max(0), m_min(0),
|
||||
m_thumbHeight(0), m_thumbY(0)
|
||||
{
|
||||
}
|
||||
|
||||
void ScrollBar::render() {
|
||||
if(1 == m_max) return;
|
||||
void ScrollBar::render()
|
||||
{
|
||||
if (1 == m_max)
|
||||
return;
|
||||
|
||||
// draw dotted line
|
||||
for(size_t y = m_y; y < m_y + m_height; y++) {
|
||||
if(y % 2 == 0) {
|
||||
for (size_t y = m_y; y < m_y + m_height; y++)
|
||||
{
|
||||
if (y % 2 == 0)
|
||||
{
|
||||
u8g2_DrawPixel(u8g2, m_x, y);
|
||||
}
|
||||
}
|
||||
@@ -32,7 +25,8 @@ void ScrollBar::render() {
|
||||
u8g2_DrawBox(u8g2, u8g2->width - 4, m_thumbY, 3, m_thumbHeight);
|
||||
}
|
||||
|
||||
void ScrollBar::refresh(const size_t value, const size_t max, const size_t min) {
|
||||
void ScrollBar::refresh(const size_t value, const size_t max, const size_t min)
|
||||
{
|
||||
m_value = value;
|
||||
m_max = max;
|
||||
m_min = min;
|
||||
|
@@ -1,14 +1,17 @@
|
||||
#include "common/Widget.h"
|
||||
|
||||
Widget::Widget(u8g2_t* u8g2)
|
||||
: u8g2(u8g2) {
|
||||
Widget::Widget(u8g2_t *u8g2) : u8g2(u8g2)
|
||||
{
|
||||
}
|
||||
|
||||
void Widget::update(uint64_t dt) {
|
||||
void Widget::update(uint64_t dt)
|
||||
{
|
||||
}
|
||||
|
||||
void Widget::render() {
|
||||
void Widget::render()
|
||||
{
|
||||
}
|
||||
|
||||
void Widget::onButtonClicked(uint8_t button) {
|
||||
void Widget::onButtonClicked(uint8_t button)
|
||||
{
|
||||
}
|
||||
|
@@ -1,23 +1,17 @@
|
||||
#include "data/MenuItem.h"
|
||||
|
||||
MenuItem::MenuItem(const uint8_t type, std::string text, std::function<void(uint8_t)> callback)
|
||||
: m_type(type)
|
||||
, m_text(std::move(text))
|
||||
, m_callback(std::move(callback)) {
|
||||
: m_type(type), m_text(std::move(text)), m_callback(std::move(callback))
|
||||
{
|
||||
}
|
||||
|
||||
MenuItem::MenuItem(
|
||||
const uint8_t type,
|
||||
std::string text,
|
||||
std::string value,
|
||||
std::function<void(uint8_t)> callback)
|
||||
: m_type(type)
|
||||
, m_text(std::move(text))
|
||||
, m_value(std::move(value))
|
||||
, m_callback(std::move(callback)) {
|
||||
MenuItem::MenuItem(const uint8_t type, std::string text, std::string value, std::function<void(uint8_t)> callback)
|
||||
: m_type(type), m_text(std::move(text)), m_value(std::move(value)), m_callback(std::move(callback))
|
||||
{
|
||||
}
|
||||
|
||||
uint8_t MenuItem::getType() const {
|
||||
uint8_t MenuItem::getType() const
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
|
@@ -5,7 +5,7 @@ void demoL(uint8_t id)
|
||||
//
|
||||
}
|
||||
|
||||
LightMenu::LightMenu(menu_options_t* options) : PSMenu(options)
|
||||
LightMenu::LightMenu(menu_options_t *options) : PSMenu(options)
|
||||
{
|
||||
addText("Tag/Nacht", nullptr);
|
||||
addText("LED Einstellungen", demoL);
|
||||
|
@@ -4,16 +4,17 @@
|
||||
#include "ui/LightMenu.h"
|
||||
#include "ui/SettingsMenu.h"
|
||||
|
||||
MainMenu::MainMenu(menu_options_t* options)
|
||||
: PSMenu(options)
|
||||
, m_options(options) {
|
||||
MainMenu::MainMenu(menu_options_t *options) : PSMenu(options), m_options(options)
|
||||
{
|
||||
addText("Lichtsteuerung", [this](const uint8_t button) { onSelect(button); });
|
||||
addText("Einstellungen", [this](const uint8_t button) { onSelect(button); });
|
||||
}
|
||||
|
||||
void MainMenu::onSelect(const uint8_t id) const {
|
||||
void MainMenu::onSelect(const uint8_t id) const
|
||||
{
|
||||
std::shared_ptr<Widget> widget;
|
||||
switch(id) {
|
||||
switch (id)
|
||||
{
|
||||
case 0:
|
||||
widget = std::make_shared<LightMenu>(m_options);
|
||||
break;
|
||||
@@ -21,7 +22,8 @@ void MainMenu::onSelect(const uint8_t id) const {
|
||||
widget = std::make_shared<SettingsMenu>(m_options);
|
||||
break;
|
||||
}
|
||||
if(m_options && m_options->pushScreen) {
|
||||
if (m_options && m_options->pushScreen)
|
||||
{
|
||||
m_options->pushScreen(widget);
|
||||
}
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@ void demo(uint8_t id)
|
||||
///
|
||||
}
|
||||
|
||||
SettingsMenu::SettingsMenu(menu_options_t* options) : PSMenu(options)
|
||||
SettingsMenu::SettingsMenu(menu_options_t *options) : PSMenu(options)
|
||||
{
|
||||
addText("OTA Einspielen", demo);
|
||||
}
|
||||
|
@@ -10,16 +10,18 @@
|
||||
|
||||
uint64_t counter = 0;
|
||||
|
||||
SplashScreen::SplashScreen(menu_options_t* options)
|
||||
: Widget(options->u8g2)
|
||||
, m_options(options) {
|
||||
SplashScreen::SplashScreen(menu_options_t *options) : Widget(options->u8g2), m_options(options)
|
||||
{
|
||||
}
|
||||
|
||||
void SplashScreen::update(const uint64_t dt) {
|
||||
void SplashScreen::update(const uint64_t dt)
|
||||
{
|
||||
counter += dt;
|
||||
if(counter > 200000) {
|
||||
if (counter > 200000)
|
||||
{
|
||||
counter = 0;
|
||||
if(m_options && m_options->setScreen) {
|
||||
if (m_options && m_options->setScreen)
|
||||
{
|
||||
m_options->setScreen(std::make_shared<MainMenu>(m_options));
|
||||
}
|
||||
}
|
||||
@@ -28,7 +30,8 @@ void SplashScreen::update(const uint64_t dt) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void SplashScreen::render() {
|
||||
void SplashScreen::render()
|
||||
{
|
||||
u8g2_SetFont(u8g2, u8g2_font_DigitalDisco_tr);
|
||||
u8g2_DrawStr(u8g2, 28, u8g2->height / 2 - 10, "HO Anlage");
|
||||
u8g2_DrawStr(u8g2, 30, u8g2->height / 2 + 5, "Axel Janz");
|
||||
|
@@ -1,10 +1,10 @@
|
||||
if (DEFINED ENV{IDF_PATH})
|
||||
idf_component_register(SRCS
|
||||
PushButton.cpp
|
||||
INCLUDE_DIRS "include"
|
||||
)
|
||||
return()
|
||||
endif()
|
||||
idf_component_register(SRCS
|
||||
PushButton.cpp
|
||||
INCLUDE_DIRS "include"
|
||||
)
|
||||
return()
|
||||
endif ()
|
||||
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
project(peter)
|
||||
|
Reference in New Issue
Block a user