mirror of
https://github.com/usatiuk/cardboy.git
synced 2025-10-28 23:27:49 +01:00
71 lines
2.0 KiB
C++
71 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 PixelType, typename StringType>
|
|
class TextWindow : public Window<SurfaceType, PixelType>,
|
|
public EventHandler<TextUpdateEvent<StringType>>,
|
|
public EventQueue<TextWindow<SurfaceType, PixelType, StringType>, TextUpdateEvent<StringType>> {
|
|
public:
|
|
explicit TextWindow(SurfaceType* owner, EventLoop* loop, StringType text = "") :
|
|
Window<SurfaceType, PixelType>(owner), EventQueue<TextWindow, TextUpdateEvent<StringType>>(loop, this),
|
|
_text(std::move(text)) {}
|
|
|
|
EventHandlingResult handle(SurfaceResizeEvent resize) override {
|
|
refresh();
|
|
return EventHandlingResult::DONE;
|
|
}
|
|
|
|
EventHandlingResult handle(TextUpdateEvent<StringType> event) {
|
|
_text = std::move(event.new_text);
|
|
refresh();
|
|
return EventHandlingResult::DONE;
|
|
}
|
|
|
|
void refresh() override {
|
|
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
|