Skip to content

Commit

Permalink
Fix list bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthox committed Jul 9, 2024
1 parent 2c30c6c commit 769f91f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
47 changes: 39 additions & 8 deletions src/elements/direction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::Align;
use crate::component_wise::ComponentWise;
use super::{Align, Icon};
use crate::{component_wise::ComponentWise, render_util::Rect};
use serde::{Deserialize, Serialize};
use strum::{AsRefStr, EnumIter, VariantArray};

Expand Down Expand Up @@ -61,28 +61,59 @@ impl Direction {
}
}

pub fn list_start_offset(&self, size: [f32; 2], pad: f32, total: usize) -> [f32; 2] {
let [width, height] = size;
let last = total.saturating_sub(1) as f32;
match self {
Self::Right | Self::Left | Self::Up | Self::Down => [0.0, 0.0],
Self::Horizontal => [-0.5 * last * (width + pad), 0.0],
Self::Vertical => [0.0, -0.5 * last * (height + pad)],
}
}

pub fn list_item_offset(
&self,
size: [f32; 2],
pad: f32,
element: usize,
total: usize,
) -> [f32; 2] {
// TODO: adjust center point of first element?
let [width, height] = size;
let i = element as f32;
let offset_x = i * (width + pad);
let offset_y = i * (height + pad);
let additional = total.saturating_sub(1) as f32;
let half = 0.5 * additional;
let half_pad = 0.5 * additional * pad;
let last = total.saturating_sub(1) as f32;
match self {
Self::Right => [offset_x, 0.0],
Self::Left => [-offset_x, 0.0],
Self::Up => [0.0, -offset_y],
Self::Down => [0.0, offset_y],
Self::Horizontal => [offset_x - half * width - half_pad, 0.0],
Self::Vertical => [0.0, offset_y - half * height - half_pad],
Self::Horizontal => [offset_x - 0.5 * last * (width + pad), 0.0],
Self::Vertical => [0.0, offset_y - 0.5 * last * (height + pad)],
}
}

pub fn icon_list_bounds(&self, size: [f32; 2], pad: f32, total: usize) -> Rect {
let [width, height] = size;
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 {
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]),
Self::Down => ([0.0, 0.0], [0.0, offset_y]),
Self::Horizontal => {
let start = -0.5 * last * (width + pad);
([start, 0.0], [start + offset_x, 0.0])
}
Self::Vertical => {
let start = -0.5 * last * (height + pad);
([0.0, start], [0.0, start + offset_y])
}
};
let (min, _) = Icon::bounds(min, size);
let (_, max) = Icon::bounds(max, size);
(min, max)
}
}
10 changes: 8 additions & 2 deletions src/elements/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ impl Icon {
self.buff.is_active_or_edit(ctx, state)
}

pub fn bounds(&self, pos: [f32; 2], size: [f32; 2]) -> Rect {
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);
Expand All @@ -50,7 +56,7 @@ impl Icon {
pub fn render(&mut self, ui: &Ui, ctx: &Context, state: &RenderState, size: [f32; 2]) {
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(state.pos, size);
let color @ [_, _, _, alpha] = self.texture_color(ui);

// render icon
Expand Down
2 changes: 1 addition & 1 deletion src/elements/icon_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Render for IconElement {

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

Expand Down
17 changes: 4 additions & 13 deletions src/elements/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,10 @@ impl Render for IconList {

impl Bounds for IconList {
fn bounding_box(&self, _ui: &Ui, _ctx: &Context, pos: [f32; 2]) -> Rect {
// we calculate the bounds with all icons visible for now
// FIXME: fix & move to direction
let len = self.icons.len();
if len > 0 {
let first = self.direction.list_item_offset(self.size, self.pad, 0, len);
let last = self
.direction
.list_item_offset(self.size, self.pad, len - 1, len);
let offset = self.size.mul_scalar(0.5);
(pos.add(offset).add(first), pos.sub(offset).add(last))
} else {
Rect::default()
}
// 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))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/elements/list_icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl ListIcon {
}

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

Expand Down

0 comments on commit 769f91f

Please sign in to comment.