Rename UiDirection to Direction

This commit is contained in:
griffi-gh 2024-03-07 02:06:14 +01:00
parent 481fff7dee
commit 226fac9e8b
8 changed files with 45 additions and 45 deletions

View file

@ -10,7 +10,7 @@ use winit::{
use hui::{ use hui::{
element::{ element::{
container::Container, progress_bar::ProgressBar, fill_rect::FillRect, ElementList, UiElement 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; use hui_glium::GliumUiRenderer;
@ -96,7 +96,7 @@ fn main() {
gap: 5., gap: 5.,
padding: Sides::all(5.), padding: Sides::all(5.),
background: vec4(0., 0., 0., 0.5).into(), background: vec4(0., 0., 0., 0.5).into(),
direction: UiDirection::Horizontal, direction: Direction::Horizontal,
children: { children: {
let mut x: Vec<Box<dyn UiElement>> = vec![]; let mut x: Vec<Box<dyn UiElement>> = vec![];
for i in 0..10 { for i in 0..10 {

View file

@ -1,7 +1,7 @@
use std::time::Instant; use std::time::Instant;
use hui::{ use hui::{
size, size,
layout::{Alignment, UiDirection}, layout::{Alignment, Direction},
element::{ element::{
container::Container, container::Container,
progress_bar::ProgressBar, progress_bar::ProgressBar,
@ -46,7 +46,7 @@ ui_main!{
.with_corner_radius(0.125 * ProgressBar::DEFAULT_HEIGHT) .with_corner_radius(0.125 * ProgressBar::DEFAULT_HEIGHT)
.add_child(ui); .add_child(ui);
Container::default() Container::default()
.with_direction(UiDirection::Horizontal) .with_direction(Direction::Horizontal)
.with_align((Alignment::End, Alignment::Center)) .with_align((Alignment::End, Alignment::Center))
.with_size(size!(100%, auto)) .with_size(size!(100%, auto))
.with_children(|ui| { .with_children(|ui| {

View file

@ -11,7 +11,7 @@ use hui::{
container::Container, container::Container,
text::Text, ElementList text::Text, ElementList
}, },
layout::{Alignment, UiDirection, Size}, layout::{Alignment, Direction, Size},
rectangle::{Corners, Sides}, rectangle::{Corners, Sides},
UiInstance UiInstance
}; };
@ -57,7 +57,7 @@ fn main() {
children: ElementList(vec![ children: ElementList(vec![
Box::new(Container { Box::new(Container {
padding: Sides::all(20.), padding: Sides::all(20.),
direction: UiDirection::Horizontal, direction: Direction::Horizontal,
align: Alignment::Center.into(), align: Alignment::Center.into(),
size: (Size::Auto, Size::Auto).into(), size: (Size::Auto, Size::Auto).into(),
background: vec4(0.1, 0.1, 0.1, 0.5).into(), background: vec4(0.1, 0.1, 0.1, 0.5).into(),
@ -83,7 +83,7 @@ fn main() {
}), }),
Box::new(Container { Box::new(Container {
gap: 10., gap: 10.,
direction: UiDirection::Horizontal, direction: Direction::Horizontal,
children: ElementList(vec![ children: ElementList(vec![
Box::new(Container { Box::new(Container {
size: (Size::Static(100.), Size::Static(100.)).into(), size: (Size::Static(100.), Size::Static(100.)).into(),

View file

@ -1,7 +1,7 @@
use std::time::Instant; use std::time::Instant;
use hui::{ use hui::{
color, size, color, size,
layout::{Alignment, UiDirection}, layout::{Alignment, Direction},
element::{ element::{
container::Container, container::Container,
fill_rect::FillRect, fill_rect::FillRect,
@ -22,7 +22,7 @@ ui_main!(
let width_ratio = 0.5 + 0.5 * instant.elapsed().as_secs_f32().sin().powi(2); let width_ratio = 0.5 + 0.5 * instant.elapsed().as_secs_f32().sin().powi(2);
Container::default() Container::default()
.with_size(size!(width_ratio/, 100%)) .with_size(size!(width_ratio/, 100%))
.with_direction(UiDirection::Horizontal) .with_direction(Direction::Horizontal)
.with_align(Alignment::Center) .with_align(Alignment::Center)
.with_padding(5.) .with_padding(5.)
.with_gap(10.) .with_gap(10.)

View file

@ -6,7 +6,7 @@ use crate::{
background::BackgroundColor, background::BackgroundColor,
draw::{RoundedCorners, ImageHandle, UiDrawCommand}, draw::{RoundedCorners, ImageHandle, UiDrawCommand},
element::{ElementList, MeasureContext, ProcessContext, UiElement}, element::{ElementList, MeasureContext, ProcessContext, UiElement},
layout::{Alignment, Alignment2d, LayoutInfo, Size, Size2d, UiDirection}, layout::{Alignment, Alignment2d, LayoutInfo, Size, Size2d, Direction},
measure::{Hints, Response}, measure::{Hints, Response},
rectangle::{Corners, Sides} rectangle::{Corners, Sides}
}; };
@ -40,7 +40,7 @@ pub struct Container {
pub size: Size2d, pub size: Size2d,
/// Layout direction (horizontal/vertical) /// 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? //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 { fn default() -> Self {
Self { Self {
size: (Size::Auto, Size::Auto).into(), size: (Size::Auto, Size::Auto).into(),
direction: UiDirection::Vertical, direction: Direction::Vertical,
gap: 0., gap: 0.,
padding: Sides::all(0.), padding: Sides::all(0.),
align: Alignment2d::default(), 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 // In case wrapping is enabled, elements cannot exceed this size on the primary axis
let max_line_pri = match self.direction { 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::Auto => ctx.layout.max_size.x,
Size::Fraction(p) => ctx.layout.max_size.x * p, Size::Fraction(p) => ctx.layout.max_size.x * p,
Size::Static(p) => 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::Auto => ctx.layout.max_size.y,
Size::Fraction(p) => ctx.layout.max_size.y * p, Size::Fraction(p) => ctx.layout.max_size.y * p,
Size::Static(p) => 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 //Check the position of the side of element closest to the end on the primary axis
let end_pos_pri = match self.direction { let end_pos_pri = match self.direction {
UiDirection::Horizontal => line_size.x + measure.size.x + self.padding.left + self.padding.right, Direction::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::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 //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 //Update the total size accordingly
match self.direction { match self.direction {
UiDirection::Horizontal => { Direction::Horizontal => {
total_size.x = total_size.x.max(line_size.x); total_size.x = total_size.x.max(line_size.x);
total_size.y += line_size.y + self.gap; total_size.y += line_size.y + self.gap;
}, },
UiDirection::Vertical => { Direction::Vertical => {
total_size.x += line_size.x + self.gap; total_size.x += line_size.x + self.gap;
total_size.y = total_size.y.max(line_size.y); total_size.y = total_size.y.max(line_size.y);
} }
@ -235,10 +235,10 @@ impl UiElement for Container {
//Now, update line_sec_offset //Now, update line_sec_offset
match self.direction { match self.direction {
UiDirection::Horizontal => { Direction::Horizontal => {
line_sec_offset.y += measure.size.y + self.gap; line_sec_offset.y += measure.size.y + self.gap;
}, },
UiDirection::Vertical => { Direction::Vertical => {
line_sec_offset.x += measure.size.x + self.gap; 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 //Sset the leftover gap in case this is the last element in the line
match self.direction { match self.direction {
UiDirection::Horizontal => { Direction::Horizontal => {
line_size.x += measure.size.x + self.gap; line_size.x += measure.size.x + self.gap;
line_size.y = line_size.y.max(measure.size.y); line_size.y = line_size.y.max(measure.size.y);
leftover_gap = vec2(self.gap, 0.); leftover_gap = vec2(self.gap, 0.);
}, },
UiDirection::Vertical => { Direction::Vertical => {
line_size.x = line_size.x.max(measure.size.x); line_size.x = line_size.x.max(measure.size.x);
line_size.y += measure.size.y + self.gap; line_size.y += measure.size.y + self.gap;
leftover_gap = vec2(0., 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 //Update the total size according to the size of the last line
match self.direction { match self.direction {
UiDirection::Horizontal => { Direction::Horizontal => {
total_size.x = total_size.x.max(line_size.x); total_size.x = total_size.x.max(line_size.x);
total_size.y += line_size.y; total_size.y += line_size.y;
}, },
UiDirection::Vertical => { Direction::Vertical => {
total_size.x += line_size.x; total_size.x += line_size.x;
total_size.y = total_size.y.max(line_size.y); 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 //convert alignment to pri/sec axis based
//.0 = primary, .1 = secondary //.0 = primary, .1 = secondary
let pri_sec_align = match self.direction { let pri_sec_align = match self.direction {
UiDirection::Horizontal => (self.align.horizontal, self.align.vertical), Direction::Horizontal => (self.align.horizontal, self.align.vertical),
UiDirection::Vertical => (self.align.vertical, self.align.horizontal), Direction::Vertical => (self.align.vertical, self.align.horizontal),
}; };
//alignment (on sec. axis) //alignment (on sec. axis)
@ -371,16 +371,16 @@ impl UiElement for Container {
//alignment on primary axis //alignment on primary axis
match (pri_sec_align.0, self.direction) { match (pri_sec_align.0, self.direction) {
(Alignment::Begin, _) => (), (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; 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; 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; 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; 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(); let inner_content_size = ctx.measure.hints.inner_content_size.unwrap();
match (pri_sec_align.1, self.direction) { match (pri_sec_align.1, self.direction) {
(Alignment::Begin, _) => (), (Alignment::Begin, _) => (),
(Alignment::Center, UiDirection::Horizontal) => { (Alignment::Center, Direction::Horizontal) => {
//Align whole row //Align whole row
el_layout.position.y += ((ctx.measure.size.y - self.padding.bottom - self.padding.top) - inner_content_size.y) / 2.; el_layout.position.y += ((ctx.measure.size.y - self.padding.bottom - self.padding.top) - inner_content_size.y) / 2.;
//Align within row //Align within row
el_layout.position.y += (cur_line.content_size.y - el_measure.size.y) / 2.; 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 //Align whole row
el_layout.position.x += ((ctx.measure.size.x - self.padding.left - self.padding.right) - inner_content_size.x) / 2.; el_layout.position.x += ((ctx.measure.size.x - self.padding.left - self.padding.right) - inner_content_size.x) / 2.;
//Align within row //Align within row
el_layout.position.x += (cur_line.content_size.x - el_measure.size.x) / 2.; el_layout.position.x += (cur_line.content_size.x - el_measure.size.x) / 2.;
}, },
//TODO update these two cases: //TODO update these two cases:
(Alignment::End, UiDirection::Horizontal) => { (Alignment::End, Direction::Horizontal) => {
//Align whole row //Align whole row
el_layout.position.y += (ctx.measure.size.y - self.padding.bottom - self.padding.top) - inner_content_size.y; el_layout.position.y += (ctx.measure.size.y - self.padding.bottom - self.padding.top) - inner_content_size.y;
//Align within row //Align within row
el_layout.position.y += cur_line.content_size.y - el_measure.size.y; el_layout.position.y += cur_line.content_size.y - el_measure.size.y;
}, },
(Alignment::End, UiDirection::Vertical) => { (Alignment::End, Direction::Vertical) => {
//Align whole row //Align whole row
el_layout.position.x += (ctx.measure.size.x - self.padding.right - self.padding.left) - inner_content_size.x; el_layout.position.x += (ctx.measure.size.x - self.padding.right - self.padding.left) - inner_content_size.x;
//Align within row //Align within row
@ -456,10 +456,10 @@ impl UiElement for Container {
//layout //layout
match self.direction { match self.direction {
UiDirection::Horizontal => { Direction::Horizontal => {
local_position.x += el_measure.size.x + self.gap; local_position.x += el_measure.size.x + self.gap;
}, },
UiDirection::Vertical => { Direction::Vertical => {
local_position.y += el_measure.size.y + self.gap; local_position.y += el_measure.size.y + self.gap;
} }
} }
@ -467,12 +467,12 @@ impl UiElement for Container {
//Move to the next line //Move to the next line
match self.direction { match self.direction {
UiDirection::Horizontal => { Direction::Horizontal => {
position.y += cur_line.content_size.y + self.gap; position.y += cur_line.content_size.y + self.gap;
//position.x -= cur_line.content_size.x; //position.x -= cur_line.content_size.x;
// leftover_line_gap = vec2(0., self.gap); // leftover_line_gap = vec2(0., self.gap);
} }
UiDirection::Vertical => { Direction::Vertical => {
position.x += cur_line.content_size.x + self.gap; position.x += cur_line.content_size.x + self.gap;
//position.y -= cur_line.content_size.y; //position.y -= cur_line.content_size.y;
// leftover_line_gap = vec2(self.gap, 0.); // leftover_line_gap = vec2(self.gap, 0.);

View file

@ -4,7 +4,7 @@ use glam::vec2;
use crate::{ use crate::{
element::{MeasureContext, ProcessContext, UiElement}, element::{MeasureContext, ProcessContext, UiElement},
measure::Response, measure::Response,
layout::UiDirection layout::Direction
}; };
/// Adds spacing between elements in a layout\ /// Adds spacing between elements in a layout\
@ -25,8 +25,8 @@ impl UiElement for Spacer {
fn measure(&self, ctx: MeasureContext) -> Response { fn measure(&self, ctx: MeasureContext) -> Response {
Response { Response {
size: match ctx.layout.direction { size: match ctx.layout.direction {
UiDirection::Horizontal => vec2(self.0, 0.), Direction::Horizontal => vec2(self.0, 0.),
UiDirection::Vertical => vec2(0., self.0), Direction::Vertical => vec2(0., self.0),
}, },
..Default::default() ..Default::default()
} }

View file

@ -2,7 +2,7 @@ use glam::Vec2;
use crate::{ use crate::{
draw::{ draw::{
atlas::{TextureAtlasManager, TextureAtlasMeta}, TextureFormat, ImageHandle, UiDrawCall, UiDrawCommandList 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. /// The main instance of the UI system.
@ -108,7 +108,7 @@ impl UiInstance {
let layout = LayoutInfo { let layout = LayoutInfo {
position: Vec2::ZERO, position: Vec2::ZERO,
max_size, max_size,
direction: UiDirection::Vertical, direction: Direction::Vertical,
}; };
let measure = element.measure(MeasureContext { let measure = element.measure(MeasureContext {
state: &self.stateful_state, state: &self.stateful_state,

View file

@ -132,7 +132,7 @@ impl From<Size> for Size2d {
/// - `Vertical` - Children are laid out from top to bottom\ /// - `Vertical` - Children are laid out from top to bottom\
/// - `Horizontal` - Children are laid out from left to right /// - `Horizontal` - Children are laid out from left to right
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum UiDirection { pub enum Direction {
/// Children are laid out from top to bottom /// Children are laid out from top to bottom
#[default] #[default]
Vertical, Vertical,
@ -154,5 +154,5 @@ pub struct LayoutInfo {
/// Current direction of the layout\ /// Current direction of the layout\
/// (Usually matches direction of the parent container) /// (Usually matches direction of the parent container)
pub direction: UiDirection, pub direction: Direction,
} }