Add frame element
This commit is contained in:
parent
79e0009422
commit
619c4edbe2
4 changed files with 128 additions and 0 deletions
|
|
@ -3,6 +3,7 @@
|
|||
#include <psemek/ui/style.hpp>
|
||||
#include <psemek/ui/button.hpp>
|
||||
#include <psemek/ui/label.hpp>
|
||||
#include <psemek/ui/frame.hpp>
|
||||
#include <psemek/ui/screen.hpp>
|
||||
|
||||
#include <psemek/util/pimpl.hpp>
|
||||
|
|
@ -20,6 +21,7 @@ namespace psemek::ui
|
|||
|
||||
std::shared_ptr<button> make_button(std::string text);
|
||||
std::shared_ptr<label> make_label(std::string text);
|
||||
std::shared_ptr<frame> make_frame();
|
||||
std::shared_ptr<screen> make_screen();
|
||||
|
||||
private:
|
||||
|
|
|
|||
30
libs/ui/include/psemek/ui/frame.hpp
Normal file
30
libs/ui/include/psemek/ui/frame.hpp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/ui/element.hpp>
|
||||
#include <psemek/ui/box_shape.hpp>
|
||||
|
||||
namespace psemek::ui
|
||||
{
|
||||
|
||||
struct frame
|
||||
: element
|
||||
{
|
||||
children_range children() const override { return children_; }
|
||||
|
||||
virtual std::shared_ptr<element> set_child(std::shared_ptr<element> c);
|
||||
|
||||
virtual void set_min_size(std::optional<geom::vector<float, 2>> const & size);
|
||||
virtual void set_max_size(std::optional<geom::vector<float, 2>> const & size);
|
||||
virtual void set_fixed_size(geom::vector<float, 2> const & size);
|
||||
|
||||
geom::box<float, 2> size_constraints() const override;
|
||||
|
||||
private:
|
||||
std::optional<geom::vector<float, 2>> min_size_;
|
||||
std::optional<geom::vector<float, 2>> max_size_;
|
||||
|
||||
std::shared_ptr<element> child_;
|
||||
element * children_[1]{nullptr};
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -72,6 +72,38 @@ namespace psemek::ui
|
|||
box_shape shape_;
|
||||
};
|
||||
|
||||
struct frame_impl
|
||||
: frame
|
||||
{
|
||||
|
||||
struct shape const & shape() const override { return shape_; }
|
||||
|
||||
void reshape(geom::box<float, 2> const & bbox) override
|
||||
{
|
||||
shape_.box = bbox;
|
||||
auto st = style();
|
||||
if (!st) return;
|
||||
for (auto c : children())
|
||||
if (c) c->reshape(geom::shrink(bbox, 1.f * (st->border_width + st->outer_margin)));
|
||||
}
|
||||
|
||||
void draw(painter & p) const override
|
||||
{
|
||||
auto st = style();
|
||||
if (!st) return;
|
||||
|
||||
if (st->shadow_offset != geom::vector{0, 0})
|
||||
p.draw_rect(shape_.box + geom::cast<float>(st->shadow_offset), st->shadow_color);
|
||||
|
||||
if (st->border_width > 0)
|
||||
p.draw_rect(shape_.box, st->border_color);
|
||||
p.draw_rect(geom::shrink(shape_.box, 1.f * st->border_width), st->bg_color);
|
||||
}
|
||||
|
||||
private:
|
||||
box_shape shape_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
struct default_element_factory::impl
|
||||
|
|
@ -124,6 +156,11 @@ namespace psemek::ui
|
|||
return r;
|
||||
}
|
||||
|
||||
std::shared_ptr<frame> default_element_factory::make_frame()
|
||||
{
|
||||
return impl().create<frame_impl>();
|
||||
}
|
||||
|
||||
std::shared_ptr<screen> default_element_factory::make_screen()
|
||||
{
|
||||
return impl().create<screen>();
|
||||
|
|
|
|||
59
libs/ui/source/frame.cpp
Normal file
59
libs/ui/source/frame.cpp
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#include <psemek/ui/frame.hpp>
|
||||
|
||||
namespace psemek::ui
|
||||
{
|
||||
|
||||
std::shared_ptr<element> frame::set_child(std::shared_ptr<element> c)
|
||||
{
|
||||
auto old = std::move(child_);
|
||||
if (old) old->set_parent(nullptr);
|
||||
|
||||
child_ = std::move(c);
|
||||
if (child_) child_->set_parent(this);
|
||||
children_[0] = child_.get();
|
||||
|
||||
return old;
|
||||
}
|
||||
|
||||
void frame::set_min_size(std::optional<geom::vector<float, 2>> const & size)
|
||||
{
|
||||
min_size_ = size;
|
||||
post_reshape();
|
||||
}
|
||||
|
||||
void frame::set_max_size(std::optional<geom::vector<float, 2>> const & size)
|
||||
{
|
||||
max_size_ = size;
|
||||
post_reshape();
|
||||
}
|
||||
|
||||
void frame::set_fixed_size(geom::vector<float, 2> const & size)
|
||||
{
|
||||
min_size_ = size;
|
||||
max_size_ = size;
|
||||
post_reshape();
|
||||
}
|
||||
|
||||
geom::box<float, 2> frame::size_constraints() const
|
||||
{
|
||||
geom::box<float, 2> r = element::size_constraints();
|
||||
|
||||
if (child_)
|
||||
r = child_->size_constraints();
|
||||
|
||||
if (min_size_)
|
||||
{
|
||||
r[0].min = (*min_size_)[0];
|
||||
r[1].min = (*min_size_)[1];
|
||||
}
|
||||
|
||||
if (max_size_)
|
||||
{
|
||||
r[0].max = (*max_size_)[0];
|
||||
r[1].max = (*max_size_)[1];
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue