[window] allow specifying pos/size with [f32; 2]

This commit is contained in:
Emil Ernerfeldt 2020-05-30 11:48:33 +02:00
parent 506dd11f73
commit efe90c9326
5 changed files with 39 additions and 25 deletions

View file

@ -85,12 +85,13 @@ impl Area {
self self
} }
pub fn default_pos(mut self, default_pos: Pos2) -> Self { pub fn default_pos(mut self, default_pos: impl Into<Pos2>) -> Self {
self.default_pos = Some(default_pos); self.default_pos = Some(default_pos.into());
self self
} }
pub fn fixed_pos(mut self, fixed_pos: Pos2) -> Self { pub fn fixed_pos(mut self, fixed_pos: impl Into<Pos2>) -> Self {
let fixed_pos = fixed_pos.into();
self.default_pos = Some(fixed_pos); self.default_pos = Some(fixed_pos);
self.fixed_pos = Some(fixed_pos); self.fixed_pos = Some(fixed_pos);
self.movable = false; self.movable = false;

View file

@ -72,18 +72,18 @@ impl Resize {
self self
} }
pub fn default_size(mut self, default_size: Vec2) -> Self { pub fn default_size(mut self, default_size: impl Into<Vec2>) -> Self {
self.default_size = default_size; self.default_size = default_size.into();
self self
} }
pub fn min_size(mut self, min_size: Vec2) -> Self { pub fn min_size(mut self, min_size: impl Into<Vec2>) -> Self {
self.min_size = min_size; self.min_size = min_size.into();
self self
} }
pub fn max_size(mut self, max_size: Vec2) -> Self { pub fn max_size(mut self, max_size: impl Into<Vec2>) -> Self {
self.max_size = max_size; self.max_size = max_size.into();
self self
} }
@ -108,7 +108,8 @@ impl Resize {
.auto_expand_height(true) .auto_expand_height(true)
} }
pub fn fixed_size(mut self, size: Vec2) -> Self { pub fn fixed_size(mut self, size: impl Into<Vec2>) -> Self {
let size = size.into();
self.auto_shrink_width = false; self.auto_shrink_width = false;
self.auto_shrink_height = false; self.auto_shrink_height = false;
self.expand_width_to_fit_content = false; self.expand_width_to_fit_content = false;
@ -158,8 +159,8 @@ impl Resize {
} }
/// Offset the position of the resize handle by this much /// Offset the position of the resize handle by this much
pub fn handle_offset(mut self, handle_offset: Vec2) -> Self { pub fn handle_offset(mut self, handle_offset: impl Into<Vec2>) -> Self {
self.handle_offset = handle_offset; self.handle_offset = handle_offset.into();
self self
} }

View file

@ -70,12 +70,12 @@ impl<'open> Window<'open> {
self self
} }
pub fn default_pos(mut self, default_pos: Pos2) -> Self { pub fn default_pos(mut self, default_pos: impl Into<Pos2>) -> Self {
self.area = self.area.default_pos(default_pos); self.area = self.area.default_pos(default_pos);
self self
} }
pub fn default_size(mut self, default_size: Vec2) -> Self { pub fn default_size(mut self, default_size: impl Into<Vec2>) -> Self {
self.resize = self.resize.default_size(default_size); self.resize = self.resize.default_size(default_size);
self self
} }
@ -84,17 +84,17 @@ impl<'open> Window<'open> {
self.default_pos(rect.min).default_size(rect.size()) self.default_pos(rect.min).default_size(rect.size())
} }
pub fn min_size(mut self, min_size: Vec2) -> Self { pub fn min_size(mut self, min_size: impl Into<Vec2>) -> Self {
self.resize = self.resize.min_size(min_size); self.resize = self.resize.min_size(min_size);
self self
} }
pub fn max_size(mut self, max_size: Vec2) -> Self { pub fn max_size(mut self, max_size: impl Into<Vec2>) -> Self {
self.resize = self.resize.max_size(max_size); self.resize = self.resize.max_size(max_size);
self self
} }
pub fn fixed_size(mut self, size: Vec2) -> Self { pub fn fixed_size(mut self, size: impl Into<Vec2>) -> Self {
self.resize = self.resize.fixed_size(size); self.resize = self.resize.fixed_size(size);
self self
} }

View file

@ -50,31 +50,31 @@ impl ExampleApp {
Window::new("Examples") Window::new("Examples")
.open(&mut open_windows.examples) .open(&mut open_windows.examples)
.default_pos(pos2(32.0, 100.0)) .default_pos([32.0, 100.0])
.default_size(vec2(430.0, 600.0)) .default_size([430.0, 600.0])
.show(ctx, |ui| { .show(ctx, |ui| {
example_window.ui(ui); example_window.ui(ui);
}); });
Window::new("Settings") Window::new("Settings")
.open(&mut open_windows.settings) .open(&mut open_windows.settings)
.default_pos(pos2(500.0, 100.0)) .default_pos([500.0, 100.0])
.default_size(vec2(350.0, 400.0)) .default_size([350.0, 400.0])
.show(ctx, |ui| { .show(ctx, |ui| {
ctx.settings_ui(ui); ctx.settings_ui(ui);
}); });
Window::new("Inspection") Window::new("Inspection")
.open(&mut open_windows.inspection) .open(&mut open_windows.inspection)
.default_pos(pos2(500.0, 400.0)) .default_pos([500.0, 400.0])
.default_size(vec2(400.0, 300.0)) .default_size([400.0, 300.0])
.show(ctx, |ui| { .show(ctx, |ui| {
ctx.inspection_ui(ui); ctx.inspection_ui(ui);
}); });
Window::new("Memory") Window::new("Memory")
.open(&mut open_windows.memory) .open(&mut open_windows.memory)
.default_pos(pos2(700.0, 350.0)) .default_pos([700.0, 350.0])
.auto_sized() .auto_sized()
.show(ctx, |ui| { .show(ctx, |ui| {
ctx.memory_ui(ui); ctx.memory_ui(ui);
@ -493,7 +493,7 @@ impl Default for LayoutExample {
impl LayoutExample { impl LayoutExample {
pub fn ui(&mut self, ui: &mut Ui) { pub fn ui(&mut self, ui: &mut Ui) {
Resize::default() Resize::default()
.default_size(vec2(200.0, 200.0)) .default_size([200.0, 200.0])
.show(ui, |ui| self.content_ui(ui)); .show(ui, |ui| self.content_ui(ui));
} }

View file

@ -12,6 +12,12 @@ pub fn vec2(x: f32, y: f32) -> Vec2 {
Vec2 { x, y } Vec2 { x, y }
} }
impl From<[f32; 2]> for Vec2 {
fn from(v: [f32; 2]) -> Self {
Self { x: v[0], y: v[1] }
}
}
impl Vec2 { impl Vec2 {
pub fn zero() -> Self { pub fn zero() -> Self {
Self { x: 0.0, y: 0.0 } Self { x: 0.0, y: 0.0 }
@ -223,6 +229,12 @@ pub fn pos2(x: f32, y: f32) -> Pos2 {
Pos2 { x, y } Pos2 { x, y }
} }
impl From<[f32; 2]> for Pos2 {
fn from(v: [f32; 2]) -> Self {
Self { x: v[0], y: v[1] }
}
}
impl Pos2 { impl Pos2 {
pub fn to_vec2(self) -> Vec2 { pub fn to_vec2(self) -> Vec2 {
Vec2 { Vec2 {