bwidgets/examples/example_widget.hpp

56 lines
1.5 KiB
C++

#ifndef BWIDGET_EXAMPLES_EXAMPLE_WIDGET_
#define BWIDGET_EXAMPLES_EXAMPLE_WIDGET_
#include <SDL2/SDL_timer.h>
#include <basic_widgets/core/math.hpp>
#include <basic_widgets/core/renderer.hpp>
#include <basic_widgets/w/base/widget.hpp>
using bwidgets::core::rect_margin;
using bwidgets::core::Renderer;
using bwidgets::core::Size;
using bwidgets::widget::Widget;
class Example final : public Widget
{
private:
virtual void _handle_geometry_change(const SDL_Rect& vp) noexcept override
{
_widget_area = {2, 2, vp.w - 4, vp.h - 4};
}
virtual void _handle_rendering() override
{
auto now = SDL_GetTicks();
uint8_t r = 255 * (now % cycle_r / (float)cycle_r);
uint8_t g = 255 * (now % cycle_g / (float)cycle_g);
uint8_t b = 255 * (now % cycle_b / (float)cycle_b);
SDL_Color base_color {r, g, b, 255};
int border = 10;
for (auto i = 0; i < border; i+=3)
{
uint8_t alpha = 255 * i / border;
_renderer->draw_color({base_color.r, base_color.g, base_color.b, alpha})
->draw_rect(rect_margin(_widget_area, {i, i}));
}
_renderer->draw_color(base_color)
->draw_rect(nullptr)
->fill_rect(rect_margin(_widget_area, {border*2, border*2}));
}
public:
unsigned int cycle_r {3500};
unsigned int cycle_g {3500};
unsigned int cycle_b {3500};
virtual Size size() const noexcept
{
return {128, 64};
}
};
#endif