Files
cardboy/Firmware/sdk/library/include_public/SubSurface.hpp
2025-08-30 11:12:04 +02:00

76 lines
1.9 KiB
C++

//
// Created by Stepan Usatiuk on 27.07.2025.
//
#ifndef SUBSURFACE_HPP
#define SUBSURFACE_HPP
#include <memory>
#include <type_traits>
#include "Pixel.hpp"
#include "StandardEvents.hpp"
#include "Surface.hpp"
#include "Window.hpp"
#include "utils.hpp"
class SubSurface : public StandardEventHandler<SubSurface> {
public:
SubSurface(Surface* parent) : _parent(parent) {}
SubSurface(SubSurface* parent) : _parent(parent->_parent) {}
void draw_pixel_impl(unsigned x, unsigned y, const Pixel& pixel) {
if (x >= _x_size || y >= _y_size) {
assert(false);
}
_parent->draw_pixel(x + _x_offset, y + _y_offset, pixel);
}
unsigned get_x_offset() const { return _x_offset; }
unsigned get_y_offset() const { return _y_offset; }
unsigned get_width_impl() const { return _x_size; }
unsigned get_height_impl() const { return _y_size; }
void set_pos(unsigned x_offset, unsigned y_offset, unsigned x_size, unsigned y_size) {
_x_offset = x_offset;
_y_offset = y_offset;
_x_size = x_size;
_y_size = y_size;
this->handle(SurfaceResizeEvent(x_size, y_size));
}
template<typename T>
EventHandlingResult handle(const T& event) {
if (_window.get())
return _window->handle(event);
return EventHandlingResult::CONTINUE;
}
template<typename WindowType, typename... Args>
void set_window(Args&&... args) {
_window = std::make_unique<WindowType>(std::forward<Args>(args)...);
}
bool has_window() const { return _window != nullptr; }
Window* get_window() {
assert(has_window());
return _window.get();
}
private:
unsigned _x_offset = 0;
unsigned _y_offset = 0;
unsigned _x_size = 0;
unsigned _y_size = 0;
Surface* _parent;
std::unique_ptr<Window> _window;
};
#endif // SUBSURFACE_HPP