From 5af0c665990a53916c2833e2872ef74b58d89bef Mon Sep 17 00:00:00 2001 From: lisyarus Date: Tue, 20 Dec 2022 17:12:06 +0300 Subject: [PATCH] Add simple ui::color_view element --- libs/ui/include/psemek/ui/color_view.hpp | 32 ++++++++++++++++++++++ libs/ui/source/color_view.cpp | 35 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 libs/ui/include/psemek/ui/color_view.hpp create mode 100644 libs/ui/source/color_view.cpp diff --git a/libs/ui/include/psemek/ui/color_view.hpp b/libs/ui/include/psemek/ui/color_view.hpp new file mode 100644 index 00000000..37399cd1 --- /dev/null +++ b/libs/ui/include/psemek/ui/color_view.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include + +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 const & box) override { shape_.box = box; } + + geom::interval width_constraints(float height) const override; + geom::interval 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; + }; + +} diff --git a/libs/ui/source/color_view.cpp b/libs/ui/source/color_view.cpp new file mode 100644 index 00000000..6d9111b8 --- /dev/null +++ b/libs/ui/source/color_view.cpp @@ -0,0 +1,35 @@ +#include +#include + +namespace psemek::ui +{ + + geom::interval color_view::width_constraints(float height) const + { + if (square_) + return {height, height}; + return element::width_constraints(height); + } + + geom::interval 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 box = shape_.box; + if (square_) + { + if (box[0].length() > box[1].length()) + box[0] = geom::expand(geom::interval::singleton(box[0].center()), box[1].length() / 2.f); + else + box[1] = geom::expand(geom::interval::singleton(box[1].center()), box[0].length() / 2.f); + } + + p.draw_rect(box, color_); + } + +}