Files
cardboy/Firmware/sdk/library/include_public/SubSurface.hpp
2025-07-31 16:12:35 +02:00

59 lines
1.5 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"
template<typename SurfaceParent>
class SubSurface : public Surface<SubSurface<SurfaceParent>, typename SurfaceParent::PixelType> {
public:
using PixelType = typename SurfaceParent::PixelType;
SubSurface(SurfaceParent* parent) : _parent(parent) {}
SubSurface(SubSurface<SurfaceParent>* parent) : _parent(parent->_parent) {}
void draw_pixel_impl(unsigned x, unsigned y, const PixelType& 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));
}
private:
unsigned _x_offset = 0;
unsigned _y_offset = 0;
unsigned _x_size = 0;
unsigned _y_size = 0;
SurfaceParent* _parent;
};
#endif // SUBSURFACE_HPP