Make ui::style::inner_margin a 2D vector

This commit is contained in:
Nikita Lisitsa 2022-03-09 19:25:56 +03:00
parent f139b1ca1f
commit 6412238e19
4 changed files with 20 additions and 19 deletions

View file

@ -40,7 +40,7 @@ namespace psemek::ui
std::optional<geom::vector<int, 2>> shadow_offset;
std::optional<gfx::color_rgba> shadow_color;
std::optional<int> inner_margin;
std::optional<geom::vector<int, 2>> inner_margin;
std::optional<int> outer_margin;
std::optional<gfx::color_rgba> text_color;

View file

@ -39,7 +39,7 @@ namespace psemek::ui
offset = geom::cast<float>(*st->action_offset);
element * c = label() ? (element *)label() : icon();
if (c) c->reshape(geom::shrink(bbox, 1.f * (*st->border_width + *st->inner_margin)) + offset);
if (c) c->reshape(geom::shrink(bbox, geom::vector<float, 2>{*st->border_width + (*st->inner_margin)[0], *st->border_width + (*st->inner_margin)[1]}) + offset);
}
void on_state_changed(state_t old) override
@ -98,9 +98,10 @@ namespace psemek::ui
if (auto st = merged_own_style())
{
float extra = 2.f * (*st->border_width + *st->inner_margin);
sc[0] += extra;
sc[1] += extra;
sc[0] += 2.f * (*st->border_width);
sc[1] += 2.f * (*st->border_width);
sc += 2.f * geom::cast<float>(*st->inner_margin);
}
}
@ -330,7 +331,7 @@ namespace psemek::ui
auto st = merged_own_style();
float const header_height = std::max(lc[1].min + 2.f * (*st->inner_margin) + 2.f * (*st->border_width), bc[1].min);
float const header_height = std::max(lc[1].min + 2.f * (*st->inner_margin)[1] + 2.f * (*st->border_width), bc[1].min);
{
geom::box<float, 2> b;
@ -343,10 +344,10 @@ namespace psemek::ui
{
geom::box<float, 2> b;
b[0].min = bbox[0].min + (*st->inner_margin) + (*st->border_width);
b[0].max = bbox[0].max - (*st->inner_margin) - bc[0].min;
b[1].min = bbox[1].min + (*st->inner_margin) + (*st->border_width);
b[1].max = bbox[1].min + header_height - (*st->inner_margin) - (*st->border_width);
b[0].min = bbox[0].min + (*st->inner_margin)[0] + (*st->border_width);
b[0].max = bbox[0].max - (*st->inner_margin)[0] - bc[0].min;
b[1].min = bbox[1].min + (*st->inner_margin)[1] + (*st->border_width);
b[1].max = bbox[1].min + header_height - (*st->inner_margin)[1] - (*st->border_width);
caption_->reshape(b);
}
@ -371,9 +372,9 @@ namespace psemek::ui
auto st = merged_own_style();
float const header_height = std::max(lc[1].min + 2.f * (*st->inner_margin) + 2.f * (*st->border_width), bc[1].min);
float const header_height = std::max(lc[1].min + 2.f * (*st->inner_margin)[1] + 2.f * (*st->border_width), bc[1].min);
r[0].min = std::max(r[0].min + 2.f * (*st->inner_margin) + 2.f * (*st->border_width), (*st->border_width) + lc[0].min + bc[0].min);
r[0].min = std::max(r[0].min + 2.f * (*st->inner_margin)[0] + 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;
@ -392,7 +393,7 @@ namespace psemek::ui
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->inner_margin) + 2.f * (*st->border_width), bc[1].min);
float const header_height = std::max(lc[1].min + 2.f * (*st->inner_margin)[1] + 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);
p.draw_rect({{{bb[0].min + (*st->border_width), bb[0].max - (*st->border_width)}, {bb[1].min + header_height, bb[1].max - (*st->border_width)}}},

View file

@ -18,14 +18,14 @@ namespace psemek::ui
auto st = merged_own_style();
geom::interval<float> x_range = geom::shrink<float>(box[0], *st->inner_margin);
geom::interval<float> x_range = geom::shrink<float>(box[0], (*st->inner_margin)[0]);
float y = box[1].min;
for (auto c : children())
{
float y_start = y;
y += *st->inner_margin;
y += (*st->inner_margin)[1];
if (c)
{
@ -34,7 +34,7 @@ namespace psemek::ui
y += sc[1].min;
}
y += *st->inner_margin;
y += (*st->inner_margin)[1];
child_boxes_.push_back({{box[0], {y_start, y}}});
@ -60,9 +60,9 @@ namespace psemek::ui
auto st = merged_own_style();
x_range += *st->inner_margin * 2.f;
x_range += (*st->inner_margin)[0] * 2.f;
y_sum += *st->inner_margin * 2.f * container_.size();
y_sum += (*st->inner_margin)[1] * 2.f * container_.size();
if (!container_.empty())
y_sum += *st->scale * y_extra() * static_cast<int>(container_.size() - 1);

View file

@ -103,7 +103,7 @@ namespace psemek::ui
s.shadow_offset = {1, 1};
s.shadow_color = {0, 0, 0, 255};
s.inner_margin = 5;
s.inner_margin = {5, 5};
s.outer_margin = 10;
s.text_color = {255, 255, 255, 255};