Browse Source

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

pull/3/head
Emil Ernerfeldt 5 years ago
parent
commit
efe90c9326
  1. 7
      egui/src/containers/area.rs
  2. 19
      egui/src/containers/resize.rs
  3. 10
      egui/src/containers/window.rs
  4. 16
      egui/src/examples/app.rs
  5. 12
      egui/src/math.rs

7
egui/src/containers/area.rs

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

19
egui/src/containers/resize.rs

@ -72,18 +72,18 @@ impl Resize {
self
}
pub fn default_size(mut self, default_size: Vec2) -> Self {
self.default_size = default_size;
pub fn default_size(mut self, default_size: impl Into<Vec2>) -> Self {
self.default_size = default_size.into();
self
}
pub fn min_size(mut self, min_size: Vec2) -> Self {
self.min_size = min_size;
pub fn min_size(mut self, min_size: impl Into<Vec2>) -> Self {
self.min_size = min_size.into();
self
}
pub fn max_size(mut self, max_size: Vec2) -> Self {
self.max_size = max_size;
pub fn max_size(mut self, max_size: impl Into<Vec2>) -> Self {
self.max_size = max_size.into();
self
}
@ -108,7 +108,8 @@ impl Resize {
.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_height = false;
self.expand_width_to_fit_content = false;
@ -158,8 +159,8 @@ impl Resize {
}
/// Offset the position of the resize handle by this much
pub fn handle_offset(mut self, handle_offset: Vec2) -> Self {
self.handle_offset = handle_offset;
pub fn handle_offset(mut self, handle_offset: impl Into<Vec2>) -> Self {
self.handle_offset = handle_offset.into();
self
}

10
egui/src/containers/window.rs

@ -70,12 +70,12 @@ impl<'open> Window<'open> {
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
}
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
}
@ -84,17 +84,17 @@ impl<'open> Window<'open> {
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
}
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
}
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
}

16
egui/src/examples/app.rs

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

12
egui/src/math.rs

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

Loading…
Cancel
Save