app work?
This commit is contained in:
@@ -7,8 +7,7 @@ pub struct Aligned {
|
|||||||
|
|
||||||
impl Widget for Aligned {
|
impl Widget for Aligned {
|
||||||
fn draw(&mut self, painter: &mut Painter) {
|
fn draw(&mut self, painter: &mut Painter) {
|
||||||
let region =
|
let region = UiRegion::from_ui_size_align(painter.region_size(&self.inner), self.align);
|
||||||
UiRegion::from_ui_size_align(painter.size(&self.inner).to_uivec2(), self.align);
|
|
||||||
painter.widget_within(&self.inner, region);
|
painter.widget_within(&self.inner, region);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,13 @@ impl Widget for Sized {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size {
|
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size {
|
||||||
|
let rel = ctx.size.rel;
|
||||||
|
if let Some(x) = self.x {
|
||||||
|
ctx.size.axis_mut(Axis::X).set(x.apply_rest(rel.x));
|
||||||
|
}
|
||||||
|
if let Some(y) = self.y {
|
||||||
|
ctx.size.axis_mut(Axis::Y).set(y.apply_rest(rel.y));
|
||||||
|
}
|
||||||
Size {
|
Size {
|
||||||
x: self.x.unwrap_or_else(|| ctx.size(&self.inner).x),
|
x: self.x.unwrap_or_else(|| ctx.size(&self.inner).x),
|
||||||
y: self.y.unwrap_or_else(|| ctx.size(&self.inner).y),
|
y: self.y.unwrap_or_else(|| ctx.size(&self.inner).y),
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ impl<'a> TextEditCtx<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn select(&mut self, pos: Vec2, size: Vec2) {
|
pub fn select(&mut self, pos: Vec2, size: Vec2) {
|
||||||
let pos = pos - self.text.region().top_left.to_size(size);
|
let pos = pos - self.text.region().top_left.to_abs(size);
|
||||||
self.text.cursor = self.text.buf.hit(pos.x, pos.y);
|
self.text.cursor = self.text.buf.hit(pos.x, pos.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::ops::Not;
|
use std::ops::Not;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{UiNum, UiVec2, Vec2, vec2},
|
layout::{UiNum, UiScalar, UiVec2, Vec2, vec2},
|
||||||
util::impl_op,
|
util::impl_op,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -143,17 +143,8 @@ impl Size {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_uivec2(self) -> UiVec2 {
|
pub fn to_uivec2(self, rel_len: Vec2) -> UiVec2 {
|
||||||
UiVec2 {
|
UiVec2::from_scalars(self.x.apply_rest(rel_len.x), self.y.apply_rest(rel_len.y))
|
||||||
rel: Vec2 {
|
|
||||||
x: self.x.total_rel(),
|
|
||||||
y: self.y.total_rel(),
|
|
||||||
},
|
|
||||||
abs: Vec2 {
|
|
||||||
x: self.x.abs,
|
|
||||||
y: self.y.abs,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_axis(axis: Axis, aligned: Len, ortho: Len) -> Self {
|
pub fn from_axis(axis: Axis, aligned: Len, ortho: Len) -> Self {
|
||||||
@@ -184,13 +175,17 @@ impl Len {
|
|||||||
rest: 0.0,
|
rest: 0.0,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn total_rel(&self) -> f32 {
|
pub fn apply_rest(&self, rel_len: f32) -> UiScalar {
|
||||||
if self.rest > 0.0 {
|
UiScalar {
|
||||||
self.rel.max(1.0)
|
rel: if self.rest > 0.0 {
|
||||||
} else {
|
self.rel.max(1.0)
|
||||||
self.rel
|
} else {
|
||||||
|
self.rel
|
||||||
|
} * rel_len,
|
||||||
|
abs: if self.rest > 0.0 { 0.0 } else { self.abs },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn abs(abs: impl UiNum) -> Self {
|
pub fn abs(abs: impl UiNum) -> Self {
|
||||||
Self {
|
Self {
|
||||||
abs: abs.to_f32(),
|
abs: abs.to_f32(),
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
layout::{
|
layout::{
|
||||||
Layers, Modules, Size, TextAttrs, TextBuffer, TextData, TextTexture, TextureHandle,
|
Layers, Modules, Size, TextAttrs, TextBuffer, TextData, TextTexture, TextureHandle,
|
||||||
Textures, UiRegion, Vec2, WidgetId, Widgets,
|
Textures, UiRegion, UiVec2, Vec2, WidgetId, Widgets,
|
||||||
},
|
},
|
||||||
render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst},
|
render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst},
|
||||||
util::{HashMap, HashSet, Id, TrackedArena},
|
util::{HashMap, HashSet, Id, TrackedArena},
|
||||||
@@ -91,12 +91,12 @@ impl<'a> PainterCtx<'a> {
|
|||||||
text: self.text,
|
text: self.text,
|
||||||
textures: self.textures,
|
textures: self.textures,
|
||||||
widgets: self.widgets,
|
widgets: self.widgets,
|
||||||
region: UiRegion::full(),
|
size: UiVec2::FULL_SIZE,
|
||||||
screen_size: self.screen_size,
|
screen_size: self.screen_size,
|
||||||
px_dependent: self.px_dependent,
|
px_dependent: self.px_dependent,
|
||||||
id,
|
id,
|
||||||
};
|
};
|
||||||
let desired = ctx.size_inner(id, active.region);
|
let desired = ctx.size_inner(id, active.region.size());
|
||||||
if size != desired {
|
if size != desired {
|
||||||
self.redraw(rid);
|
self.redraw(rid);
|
||||||
if self.draw_started.contains(&id) {
|
if self.draw_started.contains(&id) {
|
||||||
@@ -190,7 +190,7 @@ impl<'a> PainterCtx<'a> {
|
|||||||
screen_size: painter.ctx.screen_size,
|
screen_size: painter.ctx.screen_size,
|
||||||
px_dependent: painter.ctx.px_dependent,
|
px_dependent: painter.ctx.px_dependent,
|
||||||
id: painter.id,
|
id: painter.id,
|
||||||
region: UiRegion::full(),
|
size: UiVec2::FULL_SIZE,
|
||||||
}
|
}
|
||||||
.size_raw(id);
|
.size_raw(id);
|
||||||
|
|
||||||
@@ -352,6 +352,10 @@ impl<'a, 'c> Painter<'a, 'c> {
|
|||||||
self.size_ctx().size(id)
|
self.size_ctx().size(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn region_size<W>(&mut self, id: &WidgetId<W>) -> UiVec2 {
|
||||||
|
self.size_ctx().size(id).to_uivec2(self.region.size().rel)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn size_ctx(&mut self) -> SizeCtx<'_> {
|
pub fn size_ctx(&mut self) -> SizeCtx<'_> {
|
||||||
SizeCtx {
|
SizeCtx {
|
||||||
text: self.ctx.text,
|
text: self.ctx.text,
|
||||||
@@ -361,13 +365,13 @@ impl<'a, 'c> Painter<'a, 'c> {
|
|||||||
screen_size: self.ctx.screen_size,
|
screen_size: self.ctx.screen_size,
|
||||||
px_dependent: self.ctx.px_dependent,
|
px_dependent: self.ctx.px_dependent,
|
||||||
id: self.id,
|
id: self.id,
|
||||||
region: self.region,
|
size: self.region.size(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn px_size(&mut self) -> Vec2 {
|
pub fn px_size(&mut self) -> Vec2 {
|
||||||
self.ctx.px_dependent.insert(self.id);
|
self.ctx.px_dependent.insert(self.id);
|
||||||
self.region.in_size(self.ctx.screen_size)
|
self.region.size().to_abs(self.ctx.screen_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn text_data(&mut self) -> &mut TextData {
|
pub fn text_data(&mut self) -> &mut TextData {
|
||||||
@@ -389,38 +393,39 @@ pub struct SizeCtx<'a> {
|
|||||||
widgets: &'a Widgets,
|
widgets: &'a Widgets,
|
||||||
px_dependent: &'a mut HashSet<Id>,
|
px_dependent: &'a mut HashSet<Id>,
|
||||||
checked: &'a mut HashMap<Id, Size>,
|
checked: &'a mut HashMap<Id, Size>,
|
||||||
region: UiRegion,
|
/// TODO: should this be pub? rn used for sized
|
||||||
|
pub size: UiVec2,
|
||||||
screen_size: Vec2,
|
screen_size: Vec2,
|
||||||
id: Id,
|
id: Id,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SizeCtx<'_> {
|
impl SizeCtx<'_> {
|
||||||
fn size_inner(&mut self, id: Id, region: UiRegion) -> Size {
|
fn size_inner(&mut self, id: Id, size: UiVec2) -> Size {
|
||||||
let self_region = self.region;
|
let self_size = self.size;
|
||||||
self.region = region;
|
self.size = size;
|
||||||
let size = self.widgets.get_dyn_dynamic(id).desired_size(self);
|
let size = self.widgets.get_dyn_dynamic(id).desired_size(self);
|
||||||
self.region = self_region;
|
self.size = self_size;
|
||||||
self.checked.insert(id, size);
|
self.checked.insert(id, size);
|
||||||
size
|
size
|
||||||
}
|
}
|
||||||
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Size {
|
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Size {
|
||||||
// TODO: determine if this is useful
|
// TODO: determine if this is useful
|
||||||
if let Some(&size) = self.checked.get(&id.id) {
|
// if let Some(&size) = self.checked.get(&id.id) {
|
||||||
return size;
|
// return size;
|
||||||
}
|
// }
|
||||||
self.size_inner(id.id, self.region)
|
self.size_inner(id.id, self.size)
|
||||||
}
|
}
|
||||||
fn size_raw(&mut self, id: Id) -> Size {
|
fn size_raw(&mut self, id: Id) -> Size {
|
||||||
self.size_inner(id, self.region)
|
self.size_inner(id, self.size)
|
||||||
}
|
|
||||||
pub fn size_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion) -> Size {
|
|
||||||
self.size_inner(id.id, region.within(&self.region))
|
|
||||||
}
|
}
|
||||||
pub fn px_size(&mut self) -> Vec2 {
|
pub fn px_size(&mut self) -> Vec2 {
|
||||||
self.px_dependent.insert(self.id);
|
self.px_dependent.insert(self.id);
|
||||||
self.region.in_size(self.screen_size)
|
self.size.to_abs(self.screen_size)
|
||||||
}
|
}
|
||||||
pub fn draw_text(&mut self, buffer: &mut TextBuffer, attrs: &TextAttrs) -> TextTexture {
|
pub fn draw_text(&mut self, buffer: &mut TextBuffer, attrs: &TextAttrs) -> TextTexture {
|
||||||
self.text.draw(buffer, attrs, self.textures)
|
self.text.draw(buffer, attrs, self.textures)
|
||||||
}
|
}
|
||||||
|
pub fn label(&self) -> &String {
|
||||||
|
self.widgets.label(&self.id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,11 +100,11 @@ impl UiVec2 {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_size(&self, size: Vec2) -> Vec2 {
|
pub fn to_abs(&self, rel: Vec2) -> Vec2 {
|
||||||
self.rel * size + self.abs
|
self.rel * rel + self.abs
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const MAX_SIZE: Self = Self::rel(Vec2::ONE);
|
pub const FULL_SIZE: Self = Self::rel(Vec2::ONE);
|
||||||
|
|
||||||
pub const fn from_axis(axis: Axis, aligned: UiScalar, ortho: UiScalar) -> Self {
|
pub const fn from_axis(axis: Axis, aligned: UiScalar, ortho: UiScalar) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -112,6 +112,13 @@ impl UiVec2 {
|
|||||||
abs: Vec2::from_axis(axis, aligned.abs, ortho.abs),
|
abs: Vec2::from_axis(axis, aligned.abs, ortho.abs),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const fn from_scalars(x: UiScalar, y: UiScalar) -> Self {
|
||||||
|
Self {
|
||||||
|
rel: Vec2 { x: x.rel, y: y.rel },
|
||||||
|
abs: Vec2 { x: x.abs, y: y.abs },
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for UiVec2 {
|
impl Display for UiVec2 {
|
||||||
@@ -276,10 +283,6 @@ impl UiRegion {
|
|||||||
Align::Center.pos().within(self)
|
Align::Center.pos().within(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn in_size(&self, size: Vec2) -> Vec2 {
|
|
||||||
self.bot_right.to_size(size) - self.top_left.to_size(size)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn size(&self) -> UiVec2 {
|
pub fn size(&self) -> UiVec2 {
|
||||||
self.bot_right - self.top_left
|
self.bot_right - self.top_left
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user