Add simple ui::color_view element

This commit is contained in:
Nikita Lisitsa 2022-12-20 17:12:06 +03:00
parent 932e64a991
commit 5af0c66599
2 changed files with 67 additions and 0 deletions

View file

@ -0,0 +1,32 @@
#pragma once
#include <psemek/ui/element.hpp>
#include <psemek/ui/box_shape.hpp>
namespace psemek::ui
{
struct color_view
: ui::element
{
virtual gfx::color_rgba color() const { return color_; }
virtual void set_color(gfx::color_rgba value) { color_ = value; }
virtual bool square() const { return square_; }
virtual void set_square(bool value) { square_ = value; }
struct shape const & shape() const override { return shape_; }
void reshape(geom::box<float, 2> const & box) override { shape_.box = box; }
geom::interval<float> width_constraints(float height) const override;
geom::interval<float> height_constraints(float width) const override;
void draw(painter & p) const override;
private:
box_shape shape_;
gfx::color_rgba color_ = {255, 0, 0, 255};
bool square_ = true;
};
}

View file

@ -0,0 +1,35 @@
#include <psemek/ui/color_view.hpp>
#include <psemek/log/log.hpp>
namespace psemek::ui
{
geom::interval<float> color_view::width_constraints(float height) const
{
if (square_)
return {height, height};
return element::width_constraints(height);
}
geom::interval<float> color_view::height_constraints(float width) const
{
if (square_)
return {width, width};
return element::height_constraints(width);
}
void color_view::draw(painter & p) const
{
geom::box<float, 2> box = shape_.box;
if (square_)
{
if (box[0].length() > box[1].length())
box[0] = geom::expand(geom::interval<float>::singleton(box[0].center()), box[1].length() / 2.f);
else
box[1] = geom::expand(geom::interval<float>::singleton(box[1].center()), box[0].length() / 2.f);
}
p.draw_rect(box, color_);
}
}