mirror of
https://github.com/griffi-gh/hUI.git
synced 2024-11-21 14:48:42 -06:00
move stuff out
This commit is contained in:
parent
b348873632
commit
8729e8f345
|
@ -4,7 +4,7 @@ use crate::{
|
|||
background::BackgroundColor,
|
||||
draw::{ImageHandle, RoundedCorners, UiDrawCommand},
|
||||
element::{MeasureContext, ProcessContext, UiElement},
|
||||
layout::{Size, Size2d},
|
||||
layout::{compute_size, Size, Size2d},
|
||||
measure::Response,
|
||||
rectangle::Corners,
|
||||
};
|
||||
|
@ -57,31 +57,18 @@ impl UiElement for Image {
|
|||
|
||||
fn measure(&self, ctx: MeasureContext) -> Response {
|
||||
let dim = ctx.images.get_size(self.image).expect("invalid image handle");
|
||||
let pre_size = compute_size(ctx.layout, self.size, dim.as_vec2());
|
||||
Response {
|
||||
size: vec2(
|
||||
match self.size.width {
|
||||
Size::Auto => {
|
||||
match self.size.height {
|
||||
Size::Auto => dim.x as f32,
|
||||
Size::Fraction(f) => ((f * ctx.layout.max_size.y) / dim.y as f32) * dim.x as f32,
|
||||
Size::Static(pixels) => (pixels / dim.y as f32) * dim.x as f32,
|
||||
}
|
||||
},
|
||||
Size::Fraction(percentage) => ctx.layout.max_size.x * percentage,
|
||||
Size::Static(pixels) => pixels,
|
||||
size: compute_size(ctx.layout, self.size, vec2(
|
||||
match self.size.height {
|
||||
Size::Auto => dim.x as f32,
|
||||
_ => (pre_size.y / dim.y as f32) * dim.x as f32,
|
||||
},
|
||||
match self.size.height {
|
||||
Size::Auto => {
|
||||
match self.size.width {
|
||||
Size::Auto => dim.y as f32,
|
||||
Size::Fraction(f) => ((f * ctx.layout.max_size.x) / dim.x as f32) * dim.y as f32,
|
||||
Size::Static(pixels) => (pixels / dim.x as f32) * dim.y as f32,
|
||||
}
|
||||
},
|
||||
Size::Fraction(percentage) => ctx.layout.max_size.y * percentage,
|
||||
Size::Static(pixels) => pixels,
|
||||
Size::Auto => dim.x as f32,
|
||||
_ => (pre_size.y / dim.y as f32) * dim.x as f32,
|
||||
},
|
||||
),
|
||||
)),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::{
|
|||
background::BackgroundColor,
|
||||
draw::{RoundedCorners, UiDrawCommand},
|
||||
element::{MeasureContext, ProcessContext, UiElement},
|
||||
layout::{Size, Size2d},
|
||||
layout::{compute_size, Size, Size2d},
|
||||
measure::Response,
|
||||
rectangle::Corners
|
||||
};
|
||||
|
@ -55,18 +55,10 @@ impl UiElement for ProgressBar {
|
|||
|
||||
fn measure(&self, ctx: MeasureContext) -> Response {
|
||||
Response {
|
||||
size: vec2(
|
||||
match self.size.width {
|
||||
Size::Auto => ctx.layout.max_size.x.max(300.),
|
||||
Size::Fraction(p) => ctx.layout.max_size.x * p,
|
||||
Size::Static(p) => p,
|
||||
},
|
||||
match self.size.height {
|
||||
Size::Auto => Self::DEFAULT_HEIGHT,
|
||||
Size::Fraction(p) => ctx.layout.max_size.y * p,
|
||||
Size::Static(p) => p,
|
||||
}
|
||||
),
|
||||
size: compute_size(ctx.layout, self.size, vec2(
|
||||
ctx.layout.max_size.x.max(300.),
|
||||
Self::DEFAULT_HEIGHT,
|
||||
)),
|
||||
hints: Default::default(),
|
||||
user_data: None,
|
||||
..Default::default()
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! element layout, alignment and sizing
|
||||
|
||||
use glam::Vec2;
|
||||
use glam::{vec2, Vec2};
|
||||
|
||||
/// Alignment along a single axis
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, PartialOrd, Ord)]
|
||||
|
@ -160,3 +160,19 @@ pub struct LayoutInfo {
|
|||
/// (Usually matches direction of the parent container)
|
||||
pub direction: Direction,
|
||||
}
|
||||
|
||||
/// Helper function to calculate the size of an element based on its layout and size information\
|
||||
/// Used to help reduce code duplication in the `measure` method of UI elements
|
||||
pub fn compute_size(layout: &LayoutInfo, size: Size2d, comfy_size: Vec2) -> Vec2 {
|
||||
let width = match size.width {
|
||||
Size::Auto => comfy_size.x,
|
||||
Size::Fraction(fraction) => layout.max_size.x * fraction,
|
||||
Size::Static(size) => size,
|
||||
};
|
||||
let height = match size.height {
|
||||
Size::Auto => comfy_size.y,
|
||||
Size::Fraction(fraction) => layout.max_size.y * fraction,
|
||||
Size::Static(size) => size,
|
||||
};
|
||||
vec2(width, height)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue