UI window wip
This commit is contained in:
parent
b9131a8d5c
commit
b8fb2d47e3
4 changed files with 199 additions and 14 deletions
|
|
@ -46,21 +46,20 @@ struct ui_example
|
|||
auto screen = element_factory.make_screen();
|
||||
screen->set_style(style);
|
||||
|
||||
std::shared_ptr<ui::button> button;
|
||||
auto window = element_factory.make_window("Settings");
|
||||
auto frame = element_factory.make_frame();
|
||||
{
|
||||
gfx::pixmap_rgba pm({32, 32});
|
||||
for (int x = 0; x < pm.width(); ++x)
|
||||
{
|
||||
for (int y = 0; y < pm.height(); ++y)
|
||||
{
|
||||
pm(x, y) = {(x * 255) / pm.width(), (y * 255) / pm.height(), 0, 255};
|
||||
}
|
||||
}
|
||||
auto tex = std::make_shared<gfx::texture_2d>(gfx::texture_2d::from_pixmap(pm));
|
||||
tex->nearest_filter();
|
||||
button = element_factory.make_button(tex);
|
||||
auto style = std::make_shared<ui::style>();
|
||||
style->border_width = 0;
|
||||
style->shadow_offset = {0, 0};
|
||||
frame->set_style(style);
|
||||
}
|
||||
screen->add_child(button, ui::screen::x_policy::center, ui::screen::y_policy::center);
|
||||
auto label = element_factory.make_label(to_be);
|
||||
label->set_overflow(ui::label::overflow_mode::ellipsis);
|
||||
label->set_multiline(ui::label::multiline_mode::minimize_lines);
|
||||
frame->set_child(label);
|
||||
window->set_child(frame);
|
||||
screen->add_child(window, ui::screen::x_policy::center, ui::screen::y_policy::center);
|
||||
|
||||
ui_controller.set_root(std::move(screen));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include <psemek/ui/button.hpp>
|
||||
#include <psemek/ui/label.hpp>
|
||||
#include <psemek/ui/frame.hpp>
|
||||
#include <psemek/ui/window.hpp>
|
||||
#include <psemek/ui/screen.hpp>
|
||||
#include <psemek/ui/grid_layout.hpp>
|
||||
|
||||
|
|
@ -23,6 +24,7 @@ namespace psemek::ui
|
|||
std::shared_ptr<button> make_button(std::shared_ptr<gfx::texture_2d> icon);
|
||||
std::shared_ptr<label> make_label(std::string text);
|
||||
std::shared_ptr<frame> make_frame();
|
||||
std::shared_ptr<window> make_window(std::string caption);
|
||||
std::shared_ptr<screen> make_screen();
|
||||
std::shared_ptr<grid_layout> make_grid_layout();
|
||||
|
||||
|
|
|
|||
15
libs/ui/include/psemek/ui/window.hpp
Normal file
15
libs/ui/include/psemek/ui/window.hpp
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/ui/element.hpp>
|
||||
|
||||
namespace psemek::ui
|
||||
{
|
||||
|
||||
struct window
|
||||
: element
|
||||
{
|
||||
virtual void set_caption(std::string caption) = 0;
|
||||
virtual std::shared_ptr<element> set_child(std::shared_ptr<element> c) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,10 @@
|
|||
#include <psemek/ui/default_element_factory.hpp>
|
||||
|
||||
#include <psemek/ui/box_shape.hpp>
|
||||
#include <psemek/util/memory_stream.hpp>
|
||||
|
||||
#include <psemek/ui/resources/cross_red_16x16_png.hpp>
|
||||
|
||||
|
||||
namespace psemek::ui
|
||||
{
|
||||
|
|
@ -169,10 +173,167 @@ namespace psemek::ui
|
|||
box_shape shape_;
|
||||
};
|
||||
|
||||
struct window_impl
|
||||
: window
|
||||
{
|
||||
window_impl(std::shared_ptr<gfx::texture_2d> close_icon)
|
||||
: caption_(std::make_shared<label>())
|
||||
, close_button_(std::make_shared<button_impl>(close_icon))
|
||||
{
|
||||
caption_->set_parent(this);
|
||||
close_button_->set_parent(this);
|
||||
|
||||
caption_->set_valign(label::valignment::center);
|
||||
caption_->set_overflow(label::overflow_mode::ellipsis);
|
||||
|
||||
auto close_button_style = std::make_shared<struct style>();
|
||||
close_button_style->shadow_offset = {0, 0};
|
||||
close_button_->set_style(close_button_style);
|
||||
|
||||
children_[0] = caption_.get();
|
||||
children_[1] = close_button_.get();
|
||||
children_[2] = nullptr;
|
||||
}
|
||||
|
||||
void setup_close()
|
||||
{
|
||||
auto weak_self = weak_from_this();
|
||||
close_button_->on_click([weak_self]{
|
||||
if (auto self = weak_self.lock())
|
||||
{
|
||||
if (auto p = dynamic_cast<container *>(self->parent()))
|
||||
p->remove_child(self.get());
|
||||
else
|
||||
throw std::runtime_error("Cannot remove window from non-container parent");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void set_caption(std::string caption) override
|
||||
{
|
||||
caption_->set_text(caption);
|
||||
}
|
||||
|
||||
std::shared_ptr<element> set_child(std::shared_ptr<element> c) override
|
||||
{
|
||||
if (child_) child_->set_parent(nullptr);
|
||||
std::swap(child_, c);
|
||||
if (child_) child_->set_parent(this);
|
||||
children_[2] = child_.get();
|
||||
return c;
|
||||
}
|
||||
|
||||
children_range children() const override
|
||||
{
|
||||
return children_range{children_};
|
||||
}
|
||||
|
||||
struct shape const & shape() const override
|
||||
{
|
||||
return shape_;
|
||||
}
|
||||
|
||||
void reshape(geom::box<float, 2> const & bbox) override
|
||||
{
|
||||
shape_.box = bbox;
|
||||
|
||||
auto lc = caption_->size_constraints();
|
||||
auto bc = close_button_->size_constraints();
|
||||
|
||||
auto st = merged_style();
|
||||
|
||||
float const header_height = std::max(lc[1].min + 2.f * (*st->border_width), bc[1].min);
|
||||
|
||||
{
|
||||
geom::box<float, 2> b;
|
||||
b[0].min = bbox[0].max - bc[0].min;
|
||||
b[0].max = bbox[0].max;
|
||||
b[1].min = bbox[1].min;
|
||||
b[1].max = bbox[1].min + header_height;
|
||||
close_button_->reshape(b);
|
||||
}
|
||||
|
||||
{
|
||||
geom::box<float, 2> b;
|
||||
b[0].min = bbox[0].min + (*st->border_width);
|
||||
b[0].max = bbox[0].max - bc[0].min;
|
||||
b[1].min = bbox[1].min + (*st->border_width);
|
||||
b[1].max = bbox[1].min + header_height - (*st->border_width);
|
||||
caption_->reshape(b);
|
||||
}
|
||||
|
||||
if (child_)
|
||||
{
|
||||
geom::box<float, 2> b;
|
||||
b[0].min = bbox[0].min + (*st->border_width);
|
||||
b[0].max = bbox[0].max - (*st->border_width);
|
||||
b[1].min = bbox[1].min + header_height;
|
||||
b[1].max = bbox[1].max - (*st->border_width);
|
||||
child_->reshape(b);
|
||||
}
|
||||
}
|
||||
|
||||
geom::box<float, 2> size_constraints() const override
|
||||
{
|
||||
geom::box<float, 2> r = element::size_constraints();
|
||||
if (child_) r = child_->size_constraints();
|
||||
|
||||
auto lc = caption_->size_constraints();
|
||||
auto bc = close_button_->size_constraints();
|
||||
|
||||
auto st = merged_style();
|
||||
|
||||
float const header_height = std::max(lc[1].min + 2.f * (*st->border_width), bc[1].min);
|
||||
|
||||
r[0].min = std::max(r[0].min + 2.f * (*st->border_width), (*st->border_width) + lc[0].min + bc[0].min);
|
||||
r[1] += (*st->border_width) * 1.f + header_height;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
void draw(painter & p) const override
|
||||
{
|
||||
auto st = merged_style();
|
||||
|
||||
auto lc = caption_->size_constraints();
|
||||
auto bc = close_button_->size_constraints();
|
||||
|
||||
auto const & bb = shape_.box;
|
||||
|
||||
if (*st->shadow_offset != geom::vector{0, 0})
|
||||
p.draw_rect(bb + geom::cast<float>(*st->shadow_offset), *st->shadow_color);
|
||||
p.draw_rect(bb, *st->border_color);
|
||||
|
||||
float const header_height = std::max(lc[1].min + 2.f * (*st->border_width), bc[1].min);
|
||||
p.draw_rect({{{bb[0].min + (*st->border_width), bb[0].max - bc[0].min}, {bb[1].min + (*st->border_width), bb[1].min + header_height - (*st->border_width)}}},
|
||||
*st->fg_color);
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<label> caption_;
|
||||
std::shared_ptr<button> close_button_;
|
||||
std::shared_ptr<element> child_;
|
||||
|
||||
box_shape shape_;
|
||||
|
||||
element * children_[3];
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
struct default_element_factory::impl
|
||||
{};
|
||||
{
|
||||
std::shared_ptr<gfx::texture_2d> cross_red_16x16;
|
||||
|
||||
impl();
|
||||
};
|
||||
|
||||
default_element_factory::impl::impl()
|
||||
{
|
||||
util::memory_istream is{resources::cross_red_16x16_png};
|
||||
cross_red_16x16 = std::make_shared<gfx::texture_2d>(gfx::texture_2d::from_pixmap(gfx::read_png(is)));
|
||||
cross_red_16x16->nearest_filter();
|
||||
}
|
||||
|
||||
default_element_factory::default_element_factory()
|
||||
: pimpl_{make_impl()}
|
||||
|
|
@ -202,6 +363,14 @@ namespace psemek::ui
|
|||
return std::make_shared<frame_impl>();
|
||||
}
|
||||
|
||||
std::shared_ptr<window> default_element_factory::make_window(std::string caption)
|
||||
{
|
||||
auto r = std::make_shared<window_impl>(impl().cross_red_16x16);
|
||||
r->setup_close();
|
||||
r->set_caption(std::move(caption));
|
||||
return r;
|
||||
}
|
||||
|
||||
std::shared_ptr<screen> default_element_factory::make_screen()
|
||||
{
|
||||
return std::make_shared<screen>();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue