Skip to content

Commit

Permalink
Change bounds to be relative
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthox committed Aug 12, 2024
1 parent fe78a78 commit c8bbb8c
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 66 deletions.
27 changes: 16 additions & 11 deletions src/bounds.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
use crate::{context::Context, render_util::Rect};
use crate::{
component_wise::ComponentWise,
context::Context,
render_util::{Point, Rect},
};
use nexus::imgui::Ui;

/// UI element bounds.
pub trait Bounds {
/// Calculates the bounding box of the element.
fn bounding_box(&self, ui: &Ui, ctx: &Context, pos: [f32; 2]) -> Rect;
/// Calculates the relative bounding box of the element.
fn bounds(&self, ui: &Ui, ctx: &Context) -> Rect;

/// Calculates the bounding box of the element with the given offset.
fn bounds_with_offset(&self, ui: &Ui, ctx: &Context, offset: Point) -> Rect {
let (start, end) = self.bounds(ui, ctx);
(offset.add(start), offset.add(end))
}

/// Calculates the combined bounding box of the elements.
fn combined_bounds<'a>(
iter: impl IntoIterator<Item = &'a Self>,
ui: &Ui,
ctx: &Context,
pos: [f32; 2],
) -> Rect
fn combined_bounds<'a>(iter: impl IntoIterator<Item = &'a Self>, ui: &Ui, ctx: &Context) -> Rect
where
Self: 'a,
{
iter.into_iter()
.map(|el| el.bounding_box(ui, ctx, pos))
.map(|el| el.bounds(ui, ctx))
.reduce(|a, b| {
let ([a1, a2], [a3, a4]) = a;
let ([b1, b2], [b3, b4]) = b;
([a1.min(b1), a2.min(b2)], [a3.max(b3), a4.max(b4)])
})
.unwrap_or((pos, pos))
.unwrap_or_default()
}
}
7 changes: 3 additions & 4 deletions src/elements/bar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Render for Bar {
if let Some(active) = active {
let alpha = ui.clone_style().alpha;

let (start, end) = self.bounding_box(ui, ctx, state.pos);
let (start, end) = self.bounds_with_offset(ui, ctx, state.pos);
let progress = self.process_value(self.progress_kind.calc(ctx, active, self.max));
let (offset_start, offset_end) =
self.direction.progress_rect_offset(self.size, progress);
Expand Down Expand Up @@ -110,9 +110,8 @@ impl Render for Bar {
}

impl Bounds for Bar {
fn bounding_box(&self, _ui: &Ui, _ctx: &Context, pos: [f32; 2]) -> Rect {
let offset = self.align.offset(self.size);
let start = pos.add(offset);
fn bounds(&self, _ui: &Ui, _ctx: &Context) -> Rect {
let start = self.align.offset(self.size);
(start, start.add(self.size))
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/elements/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ impl Common {
const COLOR: [f32; 4] = colors::WHITE;
const COLOR_DRAG: [f32; 4] = colors::YELLOW;

let (bound_min, bound_max) = bounds;
let window_pos = bound_min;
let window_size = bound_max.sub(window_pos);
let (min, max) = bounds;
let min = anchor.add(min);
let max = anchor.add(max);
let window_pos = min;
let window_size = max.sub(window_pos);
let _style = ui.push_style_var(StyleVar::WindowPadding([0.0, 0.0]));
Window::new("##reffect-edit")
.position(
Expand Down Expand Up @@ -131,7 +133,7 @@ impl Common {
let draw_list = ui.get_foreground_draw_list();

if hover {
draw_list.add_rect(bound_min, bound_max, color).build();
draw_list.add_rect(min, max, color).build();
}

let start = anchor.sub(ANCHOR_OFFSET);
Expand Down
9 changes: 4 additions & 5 deletions src/elements/direction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{align::Align, Icon};
use crate::render_util::Rect;
use crate::{component_wise::ComponentWise, render_util::Rect};
use serde::{Deserialize, Serialize};
use strum::{AsRefStr, EnumIter, VariantArray};

Expand Down Expand Up @@ -113,7 +113,7 @@ impl Direction {
let last = total.saturating_sub(1) as f32;
let offset_x = last * (width + pad);
let offset_y = last * (height + pad);
let (min, max) = match self {
let (first, last) = match self {
Self::Right => ([0.0, 0.0], [offset_x, 0.0]),
Self::Left => ([-offset_x, 0.0], [0.0, 0.0]),
Self::Up => ([0.0, -offset_y], [0.0, 0.0]),
Expand All @@ -127,8 +127,7 @@ impl Direction {
([0.0, start], [0.0, start + offset_y])
}
};
let (min, _) = Icon::bounds(min, size);
let (_, max) = Icon::bounds(max, size);
(min, max)
let (bounds_min, bounds_max) = Icon::bounds(size);
(first.add(bounds_min), last.add(bounds_max))
}
}
12 changes: 6 additions & 6 deletions src/elements/element/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ impl Render for ElementType {
}

impl Bounds for ElementType {
fn bounding_box(&self, ui: &Ui, ctx: &Context, pos: [f32; 2]) -> Rect {
fn bounds(&self, ui: &Ui, ctx: &Context) -> Rect {
match self {
Self::Group(group) => group.bounding_box(ui, ctx, pos),
Self::Icon(icon) => icon.bounding_box(ui, ctx, pos),
Self::IconList(list) => list.bounding_box(ui, ctx, pos),
Self::Text(text) => text.bounding_box(ui, ctx, pos),
Self::Bar(bar) => bar.bounding_box(ui, ctx, pos),
Self::Group(group) => group.bounds(ui, ctx),
Self::Icon(icon) => icon.bounds(ui, ctx),
Self::IconList(list) => list.bounds(ui, ctx),
Self::Text(text) => text.bounds(ui, ctx),
Self::Bar(bar) => bar.bounds(ui, ctx),
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/elements/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use super::{Animation, Common, RenderState};
use crate::{
action::ElementAction,
bounds::Bounds,
component_wise::ComponentWise,
context::{Context, EditState},
render_util::{
delete_confirm_modal, item_context_menu, style_disabled_if, tree_select_empty, Rect,
Expand Down Expand Up @@ -67,7 +66,7 @@ impl Element {

if ctx.edit.is_edited(self.common.id) {
let pos = self.common.pos(state);
let bounds = self.kind.bounding_box(ui, ctx, pos);
let bounds = self.kind.bounds(ui, ctx);
self.common.render_edit_indicators(ui, pos, bounds);
}
}
Expand Down Expand Up @@ -204,8 +203,7 @@ impl TreeNode for Element {
}

impl Bounds for Element {
fn bounding_box(&self, ui: &Ui, ctx: &Context, pos: [f32; 2]) -> Rect {
let pos = pos.add(self.common.pos);
self.kind.bounding_box(ui, ctx, pos)
fn bounds(&self, ui: &Ui, ctx: &Context) -> Rect {
self.kind.bounds_with_offset(ui, ctx, self.common.pos)
}
}
4 changes: 2 additions & 2 deletions src/elements/group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ impl Render for Group {
}

impl Bounds for Group {
fn bounding_box(&self, ui: &Ui, ctx: &Context, pos: [f32; 2]) -> Rect {
Bounds::combined_bounds(&self.members, ui, ctx, pos)
fn bounds(&self, ui: &Ui, ctx: &Context) -> Rect {
Bounds::combined_bounds(&self.members, ui, ctx)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/elements/icon/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl Render for IconElement {
}

impl Bounds for IconElement {
fn bounding_box(&self, _ui: &Ui, _ctx: &Context, pos: [f32; 2]) -> Rect {
Icon::bounds(pos, self.size)
fn bounds(&self, _ui: &Ui, _ctx: &Context) -> Rect {
Icon::bounds(self.size)
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/elements/icon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,9 @@ impl Icon {
[r, g, b, a * ui.clone_style().alpha]
}

pub fn rel_bounds(size: [f32; 2]) -> Rect {
let [half_x, half_y] = size.mul_scalar(0.5);
([-half_x, -half_y], [half_x, half_y])
}

pub fn bounds(pos: [f32; 2], size: [f32; 2]) -> Rect {
let half_size = size.mul_scalar(0.5);
let start = pos.sub(half_size);
let end = pos.add(half_size);
pub fn bounds(size: [f32; 2]) -> Rect {
let start = size.mul_scalar(-0.5);
let end = size.mul_scalar(0.5);
(start, end)
}

Expand All @@ -72,7 +66,9 @@ impl Icon {
let texture = self.source.get_texture();

if self.source.is_empty() || texture.is_some() {
let (start, end) = Self::bounds(state.pos, size);
let (start, end) = Self::bounds(size);
let start = state.pos.add(start);
let end = state.pos.add(end);
let color @ [_, _, _, alpha] = self.texture_color(ui);

// render icon
Expand Down
4 changes: 2 additions & 2 deletions src/elements/list/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl ListIcon {
.render(ui, ctx, state, self.trigger.active(), size)
}

pub fn bounds(&self, pos: [f32; 2], size: [f32; 2]) -> Rect {
Icon::bounds(pos, size)
pub fn bounds(&self, size: [f32; 2]) -> Rect {
Icon::bounds(size)
}

pub fn into_element(self, size: [f32; 2]) -> Element {
Expand Down
6 changes: 2 additions & 4 deletions src/elements/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use super::{Direction, RenderState};
use crate::{
bounds::Bounds,
colors,
component_wise::ComponentWise,
context::{Context, EditState},
render_util::{
collapsing_header_same_line_end, delete_confirm_modal, enum_combo, input_float_with_format,
Expand Down Expand Up @@ -72,11 +71,10 @@ impl Render for IconList {
}

impl Bounds for IconList {
fn bounding_box(&self, _ui: &Ui, _ctx: &Context, pos: [f32; 2]) -> Rect {
fn bounds(&self, _ui: &Ui, _ctx: &Context) -> Rect {
// calculate with all visible and at least 1 dummy
let len = self.icons.len().max(1);
let (bound_min, bound_max) = self.direction.icon_list_bounds(self.size, self.pad, len);
(pos.add(bound_min), pos.add(bound_max))
self.direction.icon_list_bounds(self.size, self.pad, len)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/elements/pack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Pack {

if ctx.edit.is_edited(self.common.id) {
let pos = anchor_pos.add(self.common.pos);
let bounds = Bounds::combined_bounds(self.elements.iter(), ui, ctx, pos);
let bounds = Bounds::combined_bounds(self.elements.iter(), ui, ctx);
self.common.render_edit_indicators(ui, pos, bounds)
}
}
Expand Down
25 changes: 15 additions & 10 deletions src/elements/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
context::{Context, ContextUpdate, EditState},
render_util::{
debug_optional, draw_text_bg, font_select, helper, helper_warn, input_text_multi_with_menu,
Font, Rect,
Font, FontToken, Rect,
},
traits::{Render, RenderDebug, RenderOptions},
tree::TreeLeaf,
Expand Down Expand Up @@ -99,9 +99,12 @@ impl Text {
result
}

fn calc_pos(&self, ui: &Ui, pos: [f32; 2], text: &str) -> [f32; 2] {
let offset = self.align.text_offset(ui, text, self.props.scale);
pos.add(offset)
fn calc_offset(&self, ui: &Ui, text: &str) -> [f32; 2] {
self.align.text_offset(ui, text, self.props.scale)
}

fn push_font(&self) -> Option<FontToken> {
self.loaded_font.and_then(|font| font.push())
}

pub fn load(&mut self) {
Expand All @@ -116,9 +119,10 @@ impl Render for Text {
self.update(ctx, state);

if let Some(text) = &self.text_memo {
let _font = self.loaded_font.and_then(|font| font.push());
let _font = self.push_font();
let font_scale = self.props.scale;
let pos = self.calc_pos(ui, state.pos, text);
let offset = self.calc_offset(ui, text);
let pos = state.pos.add(offset);
let [r, g, b, a] = self.props.color;
let alpha = a * ui.clone_style().alpha;
let color = [r, g, b, alpha];
Expand All @@ -132,13 +136,14 @@ impl Render for Text {
}

impl Bounds for Text {
fn bounding_box(&self, ui: &Ui, _ctx: &Context, pos: [f32; 2]) -> Rect {
fn bounds(&self, ui: &Ui, _ctx: &Context) -> Rect {
self.text_memo
.as_ref()
.map(|text| {
let pos = self.calc_pos(ui, pos, text);
let size = ui.calc_text_size(text);
(pos, pos.add(size.mul_scalar(self.props.scale)))
let _font = self.push_font();
let offset = self.calc_offset(ui, text);
let size = ui.calc_text_size(text).mul_scalar(self.props.scale);
(offset, offset.add(size))
})
.unwrap_or_default()
}
Expand Down

0 comments on commit c8bbb8c

Please sign in to comment.