Moved all the files from Screen. Components now uses shared ptr objects of Box. TextBox inherits from Box.

This commit is contained in:
Yohan Boujon 2023-11-28 23:31:17 +01:00
parent f2153f53af
commit a16ee2b01f
8 changed files with 150 additions and 114 deletions

View file

@ -0,0 +1,39 @@
#ifndef _HEADER_DISPLAY_BOX
#define _HEADER_DISPLAY_BOX
#include <U8g2lib.h>
namespace Display
{
// Global Constants
constexpr uint8_t FONT_SIZE = 8;
// Enums
enum class StyleWidth
{
UNDEFINED,
LEFT,
CENTERED,
RIGHT
};
enum class StyleHeight
{
UNDEFINED,
TOP,
CENTERED,
BOTTOM
};
class Box
{
public:
Box() {};
virtual void Display(size_t size, size_t position, u8g2_uint_t offsetY) {};
virtual void Update(String str) {};
virtual StyleHeight getStyleHeight() { return StyleHeight::UNDEFINED; };
// Only vertical/height padding
virtual u8g2_uint_t getPadding() { return 0; };
};
}
#endif //_HEADER_DISPLAY_BOX

View file

@ -0,0 +1,57 @@
#include "Components.hpp"
using namespace Display;
Components::Components()
{
}
void Components::Add(std::shared_ptr<Box> box)
{
// When Added, boxes should be reordered by style.
_boxes.push_back(box);
}
void Components::Add(std::vector<std::shared_ptr<Box>> boxes)
{
for (auto box : boxes)
{
_boxes.push_back(box);
}
}
void Components::Update(size_t index, String text)
{
_boxes[index]->Update(text);
}
void Components::Display()
{
size_t i(0);
u8g2_uint_t verticalPadding(0);
for (auto it = _boxes.begin(); it != _boxes.end(); it++)
{
const auto size_boxes = GetSize((*it)->getStyleHeight());
(*it)->Display(size_boxes, i, verticalPadding);
// Index and verticalPadding only incrementing for the same style. (eg : it and it+1 as the same style.)
if (it + 1 != _boxes.end() && ((*(it+1))->getStyleHeight() == (*it)->getStyleHeight()))
{
verticalPadding += (*it)->getPadding();
i++;
}
else
{
verticalPadding = 0;
i = 0;
}
}
}
size_t Components::GetSize(StyleHeight sh)
{
size_t returnSize(0);
// returnSize is equal to FONT_SIZE + vertical padding * boxes with style sh
for(auto& box : _boxes)
returnSize += (box->getStyleHeight() == sh ? (FONT_SIZE+box->getPadding()) : 0);
return returnSize;
}

View file

@ -0,0 +1,24 @@
#ifndef _HEADER_DISPLAY_COMPONENTS
#define _HEADER_DISPLAY_COMPONENTS
#include <vector>
#include <memory>
#include "Box.hpp"
namespace Display
{
class Components
{
public:
Components();
void Add(std::shared_ptr<Box> box);
void Add(std::vector<std::shared_ptr<Box>> boxes);
void Update(size_t index, String text);
void Display();
private:
size_t GetSize(StyleHeight sh);
// Boxes
std::vector<std::shared_ptr<Box>> _boxes;
};
};
#endif //_HEADER_DISPLAY_COMPONENTS

View file

@ -1,63 +1,8 @@
#include "ScreenComponents.hpp"
#include "Screen.hpp"
#include <U8g2lib.h>
#include "../Screen.hpp"
#include "TextBox.hpp"
using namespace Display;
Components::Components()
{
}
void Components::Add(TextBox box)
{
// When Added, boxes should be reordered by style.
_boxes.push_back(box);
}
void Components::Add(std::vector<TextBox> boxes)
{
for (auto box : boxes)
{
_boxes.push_back(box);
}
}
void Components::Update(size_t index, String text)
{
_boxes[index].Update(text);
}
void Components::Display()
{
size_t i(0);
u8g2_uint_t verticalPadding(0);
for (auto it = _boxes.begin(); it != _boxes.end(); it++)
{
const auto size_boxes = GetSize(it->getStyleHeight());
it->Display(size_boxes, i, verticalPadding);
// Index and verticalPadding only incrementing for the same style. (eg : it and it+1 as the same style.)
if (it + 1 != _boxes.end() && ((it + 1)->getStyleHeight() == it->getStyleHeight()))
{
verticalPadding += it->getPadding();
i++;
}
else
{
verticalPadding = 0;
i = 0;
}
}
}
size_t Components::GetSize(StyleHeight sh)
{
size_t returnSize(0);
// returnSize is equal to FONT_SIZE + vertical padding * boxes with style sh
for(auto& box : _boxes)
returnSize += (box.getStyleHeight() == sh ? (FONT_SIZE+box.getPadding()) : 0);
return returnSize;
}
TextBox::TextBox()
{
}
@ -89,6 +34,8 @@ void TextBox::Calculate()
case StyleWidth::RIGHT:
_x = width - _textWidth;
break;
default:
_x=0;
}
}
@ -108,6 +55,8 @@ void TextBox::Display(size_t size, size_t position, u8g2_uint_t offsetY)
case StyleHeight::BOTTOM:
Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + x, centeredOffset + (position + 1) * FONT_SIZE + offsetY, _style, _textWidth, _paddingHeight, _paddingWidth, _text.c_str());
break;
default:
break;
}
}

View file

@ -1,28 +1,11 @@
#ifndef _HEADER_SCREEN_COMPONENTS
#define _HEADER_SCREEN_COMPONENTS
#include <WString.h>
#include <vector>
#include <U8g2lib.h>
#ifndef _HEADER_DISPLAY_TEXTBOX
#define _HEADER_DISPLAY_TEXTBOX
#include "Box.hpp"
namespace Display
{
// Enums
enum class StyleWidth
{
LEFT,
CENTERED,
RIGHT
};
enum class StyleHeight
{
TOP,
CENTERED,
BOTTOM
};
// TextBox which inherits from Box (Single Component)
class TextBox
class TextBox : public Box
{
public:
TextBox();
@ -38,11 +21,10 @@ namespace Display
* @param takeWholeLine if true, the button takes the whole line
*/
TextBox(String str, StyleWidth sw, StyleHeight sh, u8g2_uint_t style, u8g2_uint_t w_padding=0, u8g2_uint_t h_padding=0, bool takeWholeLine=false);
void Display(size_t size, size_t position, u8g2_uint_t offsetY);
void Update(String str);
StyleHeight getStyleHeight();
// Only vertical/height padding
u8g2_uint_t getPadding();
void Display(size_t size, size_t position, u8g2_uint_t offsetY) override;
void Update(String str) override;
StyleHeight getStyleHeight() override;
u8g2_uint_t getPadding() override;
private:
void Calculate();
String _text;
@ -56,21 +38,6 @@ namespace Display
uint16_t _textWidth;
bool _takeWholeLine;
};
}
// Components which contains every type
class Components
{
public:
Components();
void Add(TextBox box);
void Add(std::vector<TextBox> boxes);
void Update(size_t index, String text);
void Display();
private:
size_t GetSize(StyleHeight sh);
// Boxes
std::vector<TextBox> _boxes;
};
};
#endif //_HEADER_SCREEN_COMPONENTS
#endif //_HEADER_DISPLAY_TEXTBOX

View file

@ -1,7 +1,6 @@
#include "Screen.hpp"
#include <string.h>
#include <vector>
#include <algorithm>
#include <memory>
using namespace Display;
@ -15,7 +14,8 @@ Screen::Screen()
}
Screen::~Screen()
{}
{
}
void Screen::Setup(uint8_t *font)
{
@ -24,12 +24,13 @@ void Screen::Setup(uint8_t *font)
headerSetup = TextBox("Clover Setup", StyleWidth::LEFT, StyleHeight::TOP, U8G2_BTN_INV, 2, 5, true);
// Static Components
connectingWindow.Add({headerSetup, TextBox("connect", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0)});
connectedWindow.Add({headerSetup,
TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0,0,2),
TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0,0,2),
TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0,0,2)});
loopWindow.Add(TextBox("Hello, Plant!", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW1));
connectingWindow.Add({std::make_shared<TextBox>(headerSetup),
std::make_shared<TextBox>(TextBox("connect", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0))});
connectedWindow.Add({std::make_shared<TextBox>(headerSetup),
std::make_shared<TextBox>(TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)),
std::make_shared<TextBox>(TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)),
std::make_shared<TextBox>(TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2))});
loopWindow.Add(std::make_shared<TextBox>(TextBox("Hello, Plant!", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW1)));
}
void Screen::connecting(uint8_t state)

View file

@ -3,12 +3,11 @@
#include <U8g2lib.h>
#include <Wire.h>
#include "ScreenComponents.hpp"
#include "Components/Components.hpp"
#include "Components/TextBox.hpp"
namespace Display
{
constexpr uint8_t FONT_SIZE=8;
class Screen
{
public:

View file

@ -1,5 +1,5 @@
#include "ServerHandler.hpp"
#include "Screen.hpp"
#include "../Display/Screen.hpp"
ServerHandler::ServerHandler() : server(80), display_time(0) {
}