Skip to content

Commit

Permalink
Add tick units
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthox committed Jul 20, 2024
1 parent ff029ce commit 97ffd07
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 21 deletions.
75 changes: 55 additions & 20 deletions src/elements/bar.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Align, Direction, Progress, RenderState};
use super::{Align, Direction, Progress, RenderState, Unit};
use crate::{
action::Action,
bounds::Bounds,
Expand Down Expand Up @@ -37,8 +37,9 @@ pub struct Bar {
pub border_size: f32,
pub border_color: [f32; 4],

pub tick_color: [f32; 4],
pub tick_size: f32,
pub tick_color: [f32; 4],
pub tick_unit: Unit,
pub ticks: Vec<f32>,
}

Expand Down Expand Up @@ -73,14 +74,19 @@ impl Render for Bar {
}

let end_offset = self.direction.tick_end_offset(self.size);
let max = active.max();
for tick in &self.ticks {
let offset = self.direction.progress_value_offset(self.size, *tick);
let start = start.add(offset);
let end = start.add(end_offset);
draw_list
.add_line(start, end, self.tick_color)
.thickness(self.tick_size)
.build();
if let Some(tick_progress) = self.tick_unit.calc_progress(*tick, max) {
let offset = self
.direction
.progress_value_offset(self.size, tick_progress);
let start = start.add(offset);
let end = start.add(end_offset);
draw_list
.add_line(start, end, self.tick_color)
.thickness(self.tick_size)
.build();
}
}
}
}
Expand Down Expand Up @@ -167,27 +173,55 @@ impl RenderOptions for Bar {
.alpha_bar(true)
.build(ui);

if let Some(prev) = enum_combo(ui, "Tick unit", &mut self.tick_unit, ComboBoxFlags::empty())
{
let new = self.tick_unit;
for tick in &mut self.ticks {
match (prev, new) {
(Unit::Percent, Unit::Absolute) => *tick *= 100.0,
(Unit::Absolute, Unit::Percent) => *tick /= 100.0,
_ => {}
}
}
}

let mut action = Action::new();
for (i, tick) in self.ticks.iter_mut().enumerate() {
action.input_with_buttons(ui, i, || {
let mut value = 100.0 * *tick;
if Slider::new(format!("##tick{i}"), 0.0, 100.0)
.flags(SliderFlags::ALWAYS_CLAMP)
.display_format("%.1f")
.build(ui, &mut value)
{
*tick = value / 100.0;
let _id = ui.push_id(i as i32);
action.input_with_buttons(ui, i, || match self.tick_unit {
Unit::Absolute => {
input_float_with_format(
"##tick",
tick,
0.0,
0.0,
"%.1f",
InputTextFlags::empty(),
);
}
Unit::Percent => {
let mut value = 100.0 * *tick;
if Slider::new("##tick", 0.0, 100.0)
.flags(SliderFlags::ALWAYS_CLAMP)
.display_format("%.1f")
.build(ui, &mut value)
{
*tick = value / 100.0;
}
}
});
ui.same_line();
ui.text(format!("Tick {}", i + 1));
if i == 0 {
if self.tick_unit == Unit::Percent && i == 0 {
helper_slider(ui);
}
}
action.perform(&mut self.ticks);
if ui.button("Add Tick") {
self.ticks.push(0.5);
self.ticks.push(match self.tick_unit {
Unit::Percent => 0.5,
Unit::Absolute => 1.0,
});
}
}
}
Expand All @@ -206,8 +240,9 @@ impl Default for Bar {
border: true,
border_size: 1.0,
border_color: colors::BLACK,
tick_color: colors::BLACK,
tick_size: 1.0,
tick_color: colors::BLACK,
tick_unit: Unit::default(),
ticks: Vec::new(),
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/elements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ mod render_state;
mod screen_anchor;
mod text;
mod text_decoration;
mod unit;

pub use self::{
align::*, align_horizontal::*, animation::*, bar::*, common::*, direction::*, dnd::*,
element::*, element_type::*, group::*, icon::*, icon_element::*, icon_source::*, layout::*,
list::*, list_icon::*, pack::*, progress::*, render_state::*, screen_anchor::*, text::*,
text_decoration::*,
text_decoration::*, unit::*,
};
39 changes: 39 additions & 0 deletions src/elements/unit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use serde::{Deserialize, Serialize};
use strum::{AsRefStr, EnumIter, VariantArray};

#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
AsRefStr,
EnumIter,
VariantArray,
Serialize,
Deserialize,
)]
pub enum Unit {
#[default]
Percent,
Absolute,
}

impl Unit {
pub fn calc_progress(&self, value: f32, max: u32) -> Option<f32> {
let progress = match self {
Self::Percent => value,
Self::Absolute => {
if max == 0 {
return None;
}
value / max as f32
}
};
(0.0 < progress && progress < 1.0).then_some(progress)
}
}

0 comments on commit 97ffd07

Please sign in to comment.