Files
cardboy/Firmware/sdk/library/include_public/TextWindow.hpp

73 lines
2.0 KiB
C++

//
// Created by Stepan Usatiuk on 26.07.2025.
//
#ifndef TEXTWINDOW_HPP
#define TEXTWINDOW_HPP
#include <string>
#include "Fonts.hpp"
#include "Window.hpp"
#include "utils.hpp"
template<typename StringType>
struct TextUpdateEvent : public Event {
TextUpdateEvent(StringType text) : new_text(std::move(text)) {}
StringType new_text;
};
template<typename SurfaceType, typename StringType>
class TextWindow : public Window<SurfaceType>,
public EventHandler<TextUpdateEvent<StringType>>,
public EventQueue<TextWindow<SurfaceType, StringType>, TextUpdateEvent<StringType>> {
public:
using PixelType = typename SurfaceType::PixelType;
explicit TextWindow(SurfaceType* owner, EventLoop* loop, StringType text = "") :
Window<SurfaceType>(owner), EventQueue<TextWindow, TextUpdateEvent<StringType>>(loop, this),
_text(std::move(text)) {}
EventHandlingResult handle_v(SurfaceResizeEvent resize) override {
refresh();
return EventHandlingResult::DONE;
}
EventHandlingResult handle(TextUpdateEvent<StringType> event) {
_text = std::move(event.new_text);
refresh();
return EventHandlingResult::DONE;
}
void refresh() {
this->_owner->clear();
size_t _max_col = this->_owner->get_width() / 8;
size_t _max_row = this->_owner->get_height() / 16;
int col = 0, row = 0;
for (char c: _text) {
if (c == '\n' || col >= _max_col) {
row++;
col = 0;
if (c == '\n')
continue;
}
if (row >= _max_row) {
break;
}
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 16; y++) {
bool color = fonts_Terminess_Powerline[c][y] & (1 << (8 - x));
this->_owner->draw_pixel(col * 8 + x, row * 16 + y, PixelType(color));
}
}
col++;
}
}
private:
StringType _text;
};
#endif // TEXTWINDOW_HPP