diff --git a/hui-examples/examples/align_test.rs b/hui-examples/examples/align_test.rs index 03a8c9c..ad340de 100644 --- a/hui-examples/examples/align_test.rs +++ b/hui-examples/examples/align_test.rs @@ -10,7 +10,7 @@ use winit::{ use hui::{ element::{ container::Container, progress_bar::ProgressBar, fill_rect::FillRect, ElementList, UiElement - }, layout::{Alignment, UiDirection, Size}, rectangle::{Corners, Sides}, UiInstance + }, layout::{Alignment, Direction, Size}, rectangle::{Corners, Sides}, UiInstance }; use hui_glium::GliumUiRenderer; @@ -96,7 +96,7 @@ fn main() { gap: 5., padding: Sides::all(5.), background: vec4(0., 0., 0., 0.5).into(), - direction: UiDirection::Horizontal, + direction: Direction::Horizontal, children: { let mut x: Vec> = vec![]; for i in 0..10 { diff --git a/hui-examples/examples/mom_downloader.rs b/hui-examples/examples/mom_downloader.rs index 7685c40..d52e479 100644 --- a/hui-examples/examples/mom_downloader.rs +++ b/hui-examples/examples/mom_downloader.rs @@ -1,7 +1,7 @@ use std::time::Instant; use hui::{ size, - layout::{Alignment, UiDirection}, + layout::{Alignment, Direction}, element::{ container::Container, progress_bar::ProgressBar, @@ -46,7 +46,7 @@ ui_main!{ .with_corner_radius(0.125 * ProgressBar::DEFAULT_HEIGHT) .add_child(ui); Container::default() - .with_direction(UiDirection::Horizontal) + .with_direction(Direction::Horizontal) .with_align((Alignment::End, Alignment::Center)) .with_size(size!(100%, auto)) .with_children(|ui| { diff --git a/hui-examples/examples/rounded_rect.rs b/hui-examples/examples/rounded_rect.rs index 3d47137..ea85691 100644 --- a/hui-examples/examples/rounded_rect.rs +++ b/hui-examples/examples/rounded_rect.rs @@ -11,7 +11,7 @@ use hui::{ container::Container, text::Text, ElementList }, - layout::{Alignment, UiDirection, Size}, + layout::{Alignment, Direction, Size}, rectangle::{Corners, Sides}, UiInstance }; @@ -57,7 +57,7 @@ fn main() { children: ElementList(vec![ Box::new(Container { padding: Sides::all(20.), - direction: UiDirection::Horizontal, + direction: Direction::Horizontal, align: Alignment::Center.into(), size: (Size::Auto, Size::Auto).into(), background: vec4(0.1, 0.1, 0.1, 0.5).into(), @@ -83,7 +83,7 @@ fn main() { }), Box::new(Container { gap: 10., - direction: UiDirection::Horizontal, + direction: Direction::Horizontal, children: ElementList(vec![ Box::new(Container { size: (Size::Static(100.), Size::Static(100.)).into(), diff --git a/hui-examples/examples/ui_test_4_wrapping.rs b/hui-examples/examples/ui_test_4_wrapping.rs index 8fa5ecd..4061898 100644 --- a/hui-examples/examples/ui_test_4_wrapping.rs +++ b/hui-examples/examples/ui_test_4_wrapping.rs @@ -1,7 +1,7 @@ use std::time::Instant; use hui::{ color, size, - layout::{Alignment, UiDirection}, + layout::{Alignment, Direction}, element::{ container::Container, fill_rect::FillRect, @@ -22,7 +22,7 @@ ui_main!( let width_ratio = 0.5 + 0.5 * instant.elapsed().as_secs_f32().sin().powi(2); Container::default() .with_size(size!(width_ratio/, 100%)) - .with_direction(UiDirection::Horizontal) + .with_direction(Direction::Horizontal) .with_align(Alignment::Center) .with_padding(5.) .with_gap(10.) diff --git a/hui/src/element/builtin/container.rs b/hui/src/element/builtin/container.rs index 9d06800..e2b327f 100644 --- a/hui/src/element/builtin/container.rs +++ b/hui/src/element/builtin/container.rs @@ -6,7 +6,7 @@ use crate::{ background::BackgroundColor, draw::{RoundedCorners, ImageHandle, UiDrawCommand}, element::{ElementList, MeasureContext, ProcessContext, UiElement}, - layout::{Alignment, Alignment2d, LayoutInfo, Size, Size2d, UiDirection}, + layout::{Alignment, Alignment2d, LayoutInfo, Size, Size2d, Direction}, measure::{Hints, Response}, rectangle::{Corners, Sides} }; @@ -40,7 +40,7 @@ pub struct Container { pub size: Size2d, /// Layout direction (horizontal/vertical) - pub direction: UiDirection, + pub direction: Direction, //XXX: should we have separate gap value for primary and secondary (when wrapped, between lines of elements) axis? @@ -96,7 +96,7 @@ impl Default for Container { fn default() -> Self { Self { size: (Size::Auto, Size::Auto).into(), - direction: UiDirection::Vertical, + direction: Direction::Vertical, gap: 0., padding: Sides::all(0.), align: Alignment2d::default(), @@ -143,12 +143,12 @@ impl UiElement for Container { // In case wrapping is enabled, elements cannot exceed this size on the primary axis let max_line_pri = match self.direction { - UiDirection::Horizontal => match self.size.width { + Direction::Horizontal => match self.size.width { Size::Auto => ctx.layout.max_size.x, Size::Fraction(p) => ctx.layout.max_size.x * p, Size::Static(p) => p, }, - UiDirection::Vertical => match self.size.height { + Direction::Vertical => match self.size.height { Size::Auto => ctx.layout.max_size.y, Size::Fraction(p) => ctx.layout.max_size.y * p, Size::Static(p) => p, @@ -201,8 +201,8 @@ impl UiElement for Container { //Check the position of the side of element closest to the end on the primary axis let end_pos_pri = match self.direction { - UiDirection::Horizontal => line_size.x + measure.size.x + self.padding.left + self.padding.right, - UiDirection::Vertical => line_size.y + measure.size.y + self.padding.top + self.padding.bottom, + Direction::Horizontal => line_size.x + measure.size.x + self.padding.left + self.padding.right, + Direction::Vertical => line_size.y + measure.size.y + self.padding.top + self.padding.bottom, }; //Wrap the element if it exceeds container's size and is not the first element in the line @@ -223,11 +223,11 @@ impl UiElement for Container { //Update the total size accordingly match self.direction { - UiDirection::Horizontal => { + Direction::Horizontal => { total_size.x = total_size.x.max(line_size.x); total_size.y += line_size.y + self.gap; }, - UiDirection::Vertical => { + Direction::Vertical => { total_size.x += line_size.x + self.gap; total_size.y = total_size.y.max(line_size.y); } @@ -235,10 +235,10 @@ impl UiElement for Container { //Now, update line_sec_offset match self.direction { - UiDirection::Horizontal => { + Direction::Horizontal => { line_sec_offset.y += measure.size.y + self.gap; }, - UiDirection::Vertical => { + Direction::Vertical => { line_sec_offset.x += measure.size.x + self.gap; } }; @@ -253,12 +253,12 @@ impl UiElement for Container { //Sset the leftover gap in case this is the last element in the line match self.direction { - UiDirection::Horizontal => { + Direction::Horizontal => { line_size.x += measure.size.x + self.gap; line_size.y = line_size.y.max(measure.size.y); leftover_gap = vec2(self.gap, 0.); }, - UiDirection::Vertical => { + Direction::Vertical => { line_size.x = line_size.x.max(measure.size.x); line_size.y += measure.size.y + self.gap; leftover_gap = vec2(0., self.gap); @@ -273,11 +273,11 @@ impl UiElement for Container { //Update the total size according to the size of the last line match self.direction { - UiDirection::Horizontal => { + Direction::Horizontal => { total_size.x = total_size.x.max(line_size.x); total_size.y += line_size.y; }, - UiDirection::Vertical => { + Direction::Vertical => { total_size.x += line_size.x; total_size.y = total_size.y.max(line_size.y); } @@ -344,8 +344,8 @@ impl UiElement for Container { //convert alignment to pri/sec axis based //.0 = primary, .1 = secondary let pri_sec_align = match self.direction { - UiDirection::Horizontal => (self.align.horizontal, self.align.vertical), - UiDirection::Vertical => (self.align.vertical, self.align.horizontal), + Direction::Horizontal => (self.align.horizontal, self.align.vertical), + Direction::Vertical => (self.align.vertical, self.align.horizontal), }; //alignment (on sec. axis) @@ -371,16 +371,16 @@ impl UiElement for Container { //alignment on primary axis match (pri_sec_align.0, self.direction) { (Alignment::Begin, _) => (), - (Alignment::Center, UiDirection::Horizontal) => { + (Alignment::Center, Direction::Horizontal) => { local_position.x += (ctx.measure.size.x - cur_line.content_size.x) / 2. - self.padding.left; }, - (Alignment::Center, UiDirection::Vertical) => { + (Alignment::Center, Direction::Vertical) => { local_position.y += (ctx.measure.size.y - cur_line.content_size.y) / 2. - self.padding.top; }, - (Alignment::End, UiDirection::Horizontal) => { + (Alignment::End, Direction::Horizontal) => { local_position.x += ctx.measure.size.x - cur_line.content_size.x - self.padding.right - self.padding.left; }, - (Alignment::End, UiDirection::Vertical) => { + (Alignment::End, Direction::Vertical) => { local_position.y += ctx.measure.size.y - cur_line.content_size.y - self.padding.bottom - self.padding.top; } } @@ -416,26 +416,26 @@ impl UiElement for Container { let inner_content_size = ctx.measure.hints.inner_content_size.unwrap(); match (pri_sec_align.1, self.direction) { (Alignment::Begin, _) => (), - (Alignment::Center, UiDirection::Horizontal) => { + (Alignment::Center, Direction::Horizontal) => { //Align whole row el_layout.position.y += ((ctx.measure.size.y - self.padding.bottom - self.padding.top) - inner_content_size.y) / 2.; //Align within row el_layout.position.y += (cur_line.content_size.y - el_measure.size.y) / 2.; }, - (Alignment::Center, UiDirection::Vertical) => { + (Alignment::Center, Direction::Vertical) => { //Align whole row el_layout.position.x += ((ctx.measure.size.x - self.padding.left - self.padding.right) - inner_content_size.x) / 2.; //Align within row el_layout.position.x += (cur_line.content_size.x - el_measure.size.x) / 2.; }, //TODO update these two cases: - (Alignment::End, UiDirection::Horizontal) => { + (Alignment::End, Direction::Horizontal) => { //Align whole row el_layout.position.y += (ctx.measure.size.y - self.padding.bottom - self.padding.top) - inner_content_size.y; //Align within row el_layout.position.y += cur_line.content_size.y - el_measure.size.y; }, - (Alignment::End, UiDirection::Vertical) => { + (Alignment::End, Direction::Vertical) => { //Align whole row el_layout.position.x += (ctx.measure.size.x - self.padding.right - self.padding.left) - inner_content_size.x; //Align within row @@ -456,10 +456,10 @@ impl UiElement for Container { //layout match self.direction { - UiDirection::Horizontal => { + Direction::Horizontal => { local_position.x += el_measure.size.x + self.gap; }, - UiDirection::Vertical => { + Direction::Vertical => { local_position.y += el_measure.size.y + self.gap; } } @@ -467,12 +467,12 @@ impl UiElement for Container { //Move to the next line match self.direction { - UiDirection::Horizontal => { + Direction::Horizontal => { position.y += cur_line.content_size.y + self.gap; //position.x -= cur_line.content_size.x; // leftover_line_gap = vec2(0., self.gap); } - UiDirection::Vertical => { + Direction::Vertical => { position.x += cur_line.content_size.x + self.gap; //position.y -= cur_line.content_size.y; // leftover_line_gap = vec2(self.gap, 0.); diff --git a/hui/src/element/builtin/spacer.rs b/hui/src/element/builtin/spacer.rs index a9a6ae8..1a12b6a 100644 --- a/hui/src/element/builtin/spacer.rs +++ b/hui/src/element/builtin/spacer.rs @@ -4,7 +4,7 @@ use glam::vec2; use crate::{ element::{MeasureContext, ProcessContext, UiElement}, measure::Response, - layout::UiDirection + layout::Direction }; /// Adds spacing between elements in a layout\ @@ -25,8 +25,8 @@ impl UiElement for Spacer { fn measure(&self, ctx: MeasureContext) -> Response { Response { size: match ctx.layout.direction { - UiDirection::Horizontal => vec2(self.0, 0.), - UiDirection::Vertical => vec2(0., self.0), + Direction::Horizontal => vec2(self.0, 0.), + Direction::Vertical => vec2(0., self.0), }, ..Default::default() } diff --git a/hui/src/instance.rs b/hui/src/instance.rs index e8cefcb..62511cc 100644 --- a/hui/src/instance.rs +++ b/hui/src/instance.rs @@ -2,7 +2,7 @@ use glam::Vec2; use crate::{ draw::{ atlas::{TextureAtlasManager, TextureAtlasMeta}, TextureFormat, ImageHandle, UiDrawCall, UiDrawCommandList - }, element::{MeasureContext, ProcessContext, UiElement}, event::{EventQueue, UiEvent}, input::UiInputState, layout::{LayoutInfo, UiDirection}, state::StateRepo, text::{FontHandle, TextRenderer} + }, element::{MeasureContext, ProcessContext, UiElement}, event::{EventQueue, UiEvent}, input::UiInputState, layout::{LayoutInfo, Direction}, state::StateRepo, text::{FontHandle, TextRenderer} }; /// The main instance of the UI system. @@ -108,7 +108,7 @@ impl UiInstance { let layout = LayoutInfo { position: Vec2::ZERO, max_size, - direction: UiDirection::Vertical, + direction: Direction::Vertical, }; let measure = element.measure(MeasureContext { state: &self.stateful_state, diff --git a/hui/src/layout.rs b/hui/src/layout.rs index b878c82..2b4be77 100644 --- a/hui/src/layout.rs +++ b/hui/src/layout.rs @@ -132,7 +132,7 @@ impl From for Size2d { /// - `Vertical` - Children are laid out from top to bottom\ /// - `Horizontal` - Children are laid out from left to right #[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] -pub enum UiDirection { +pub enum Direction { /// Children are laid out from top to bottom #[default] Vertical, @@ -154,5 +154,5 @@ pub struct LayoutInfo { /// Current direction of the layout\ /// (Usually matches direction of the parent container) - pub direction: UiDirection, + pub direction: Direction, }