layouts: margins!

This commit is contained in:
hippoz 2022-04-01 07:08:21 +03:00
parent f8a91f3a83
commit eec776ba46
Signed by: hippoz
GPG key ID: 7C52899193467641
5 changed files with 28 additions and 10 deletions

View file

@ -6,14 +6,17 @@
namespace Raven {
void Layout::run_on(Widget *target) {
Point point { target->get_current_geometry().get_x(), target->get_current_geometry().get_y() };
void Layout::run() {
if (!m_target)
return;
Point point { m_target->get_current_geometry().get_x() + m_margin, m_target->get_current_geometry().get_y() + m_margin };
double row_largest_height = 0.0;
auto children = target->get_children();
auto children = m_target->get_children();
for (auto& c : children) {
if (c->get_control_type() == ControlWidgetType::NewRow && row_largest_height) {
point.add(0, row_largest_height);
point.add(0, row_largest_height + m_margin);
row_largest_height = 0.0;
point.set_x(0);
}
@ -22,7 +25,7 @@ void Layout::run_on(Widget *target) {
}
auto new_geometry = Box{ point.get_x(), point.get_y(), c->get_current_geometry().get_width(), c->get_current_geometry().get_height() };
c->set_current_geometry(std::move(new_geometry));
point.add(c->get_current_geometry().get_width(), 0);
point.add(c->get_current_geometry().get_width() + m_margin, 0);
}
}

View file

@ -5,10 +5,19 @@
namespace Raven {
class Layout {
private:
double m_margin { 0.0 };
Widget *m_target { nullptr };
public:
Layout() {}
void run_on(Widget *target);
void run();
double get_margin() { return m_margin; }
void set_margin(double margin) { m_margin = margin; run(); }
void bind_to(Widget *target) { m_target = target; }
Widget *get_target() { return m_target; }
};
}

View file

@ -8,9 +8,13 @@
namespace Raven {
void Widget::do_layout() {
if (m_layout) {
m_layout->run_on(this);
}
if (m_layout)
m_layout->run();
}
void Widget::set_layout(Layout *layout) {
m_layout = layout;
m_layout->bind_to(this);
}
void Widget::set_window(Window *window) {

View file

@ -83,7 +83,7 @@ public:
bool get_accepts_events() { return m_accepts_events; }
void set_accepts_events(bool accepts_events) { m_accepts_events = accepts_events; }
void set_layout(Layout *layout) { m_layout = layout; }
void set_layout(Layout *layout);
Layout *get_layout() { return m_layout; }
WidgetType get_type() { return m_type; }

View file

@ -23,6 +23,8 @@ int main() {
impostor_button.set_current_geometry(Raven::Box(0, 0, 100, 30));
label.set_current_geometry(Raven::Box(0, 0, 100, 20));
main_layout.set_margin(6.0);
main_widget.set_layout(&main_layout);
window.set_main_widget(&main_widget);