Various small improvements
This commit is contained in:
parent
a56a869f48
commit
4efbb94e1b
8 changed files with 60 additions and 36 deletions
|
@ -37,10 +37,12 @@ impl Default for ExampleApp {
|
|||
impl ExampleApp {
|
||||
pub fn ui(&mut self, region: &mut Region) {
|
||||
region.foldable("About Emigui", |region| {
|
||||
region.add(label!("Emigui is an immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL."));
|
||||
region.add(label!(
|
||||
"Emigui is an experimental immediate mode GUI written in Rust."
|
||||
));
|
||||
});
|
||||
|
||||
region.foldable("Widget examples", |region| {
|
||||
region.foldable("Widgets", |region| {
|
||||
region.horizontal(Align::Min, |region| {
|
||||
region.add(label!("Text can have").text_color(srgba(110, 255, 110, 255)));
|
||||
region.add(label!("color").text_color(srgba(128, 140, 255, 255)));
|
||||
|
@ -134,7 +136,7 @@ impl ExampleApp {
|
|||
region.add_label("\
|
||||
For instance, foldable regions needs to store wether or not they are open. \
|
||||
If you fail to give them unique names then clicking one will open both. \
|
||||
To help you debug this, a error message is printed on screen:");
|
||||
To help you debug this, an error message is printed on screen:");
|
||||
|
||||
region.foldable("Foldable", |region| {
|
||||
region.add_label("Contents of first folddable region");
|
||||
|
|
|
@ -43,14 +43,14 @@ impl Id {
|
|||
Self(1)
|
||||
}
|
||||
|
||||
pub fn new<H: Hash>(source: &H) -> Id {
|
||||
pub fn new<H: Hash>(source: H) -> Id {
|
||||
use std::hash::Hasher;
|
||||
let mut hasher = DefaultHasher::new();
|
||||
source.hash(&mut hasher);
|
||||
Id(hasher.finish())
|
||||
}
|
||||
|
||||
pub fn with<H: Hash>(self, child: &H) -> Id {
|
||||
pub fn with<H: Hash>(self, child: H) -> Id {
|
||||
use std::hash::Hasher;
|
||||
let mut hasher = DefaultHasher::new();
|
||||
hasher.write_u64(self.0);
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
|
||||
#[derive(Clone, Copy, Default, Deserialize, Serialize)]
|
||||
pub struct Vec2 {
|
||||
pub x: f32,
|
||||
|
@ -80,7 +82,7 @@ impl PartialEq for Vec2 {
|
|||
}
|
||||
impl Eq for Vec2 {}
|
||||
|
||||
impl std::ops::Neg for Vec2 {
|
||||
impl Neg for Vec2 {
|
||||
type Output = Vec2;
|
||||
|
||||
fn neg(self) -> Vec2 {
|
||||
|
@ -88,7 +90,7 @@ impl std::ops::Neg for Vec2 {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::ops::AddAssign for Vec2 {
|
||||
impl AddAssign for Vec2 {
|
||||
fn add_assign(&mut self, rhs: Vec2) {
|
||||
*self = Vec2 {
|
||||
x: self.x + rhs.x,
|
||||
|
@ -97,7 +99,16 @@ impl std::ops::AddAssign for Vec2 {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add for Vec2 {
|
||||
impl SubAssign for Vec2 {
|
||||
fn sub_assign(&mut self, rhs: Vec2) {
|
||||
*self = Vec2 {
|
||||
x: self.x - rhs.x,
|
||||
y: self.y - rhs.y,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Vec2 {
|
||||
type Output = Vec2;
|
||||
fn add(self, rhs: Vec2) -> Vec2 {
|
||||
Vec2 {
|
||||
|
@ -107,7 +118,7 @@ impl std::ops::Add for Vec2 {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub for Vec2 {
|
||||
impl Sub for Vec2 {
|
||||
type Output = Vec2;
|
||||
fn sub(self, rhs: Vec2) -> Vec2 {
|
||||
Vec2 {
|
||||
|
@ -117,14 +128,14 @@ impl std::ops::Sub for Vec2 {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::ops::MulAssign<f32> for Vec2 {
|
||||
impl MulAssign<f32> for Vec2 {
|
||||
fn mul_assign(&mut self, rhs: f32) {
|
||||
self.x *= rhs;
|
||||
self.y *= rhs;
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Mul<f32> for Vec2 {
|
||||
impl Mul<f32> for Vec2 {
|
||||
type Output = Vec2;
|
||||
fn mul(self, factor: f32) -> Vec2 {
|
||||
Vec2 {
|
||||
|
@ -134,7 +145,7 @@ impl std::ops::Mul<f32> for Vec2 {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::ops::Mul<Vec2> for f32 {
|
||||
impl Mul<Vec2> for f32 {
|
||||
type Output = Vec2;
|
||||
fn mul(self, vec: Vec2) -> Vec2 {
|
||||
Vec2 {
|
||||
|
@ -144,7 +155,7 @@ impl std::ops::Mul<Vec2> for f32 {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::ops::Div<f32> for Vec2 {
|
||||
impl Div<f32> for Vec2 {
|
||||
type Output = Vec2;
|
||||
fn div(self, factor: f32) -> Vec2 {
|
||||
Vec2 {
|
||||
|
@ -223,7 +234,7 @@ impl PartialEq for Pos2 {
|
|||
}
|
||||
impl Eq for Pos2 {}
|
||||
|
||||
impl std::ops::AddAssign<Vec2> for Pos2 {
|
||||
impl AddAssign<Vec2> for Pos2 {
|
||||
fn add_assign(&mut self, rhs: Vec2) {
|
||||
*self = Pos2 {
|
||||
x: self.x + rhs.x,
|
||||
|
@ -232,7 +243,16 @@ impl std::ops::AddAssign<Vec2> for Pos2 {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add<Vec2> for Pos2 {
|
||||
impl SubAssign<Vec2> for Pos2 {
|
||||
fn sub_assign(&mut self, rhs: Vec2) {
|
||||
*self = Pos2 {
|
||||
x: self.x - rhs.x,
|
||||
y: self.y - rhs.y,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Vec2> for Pos2 {
|
||||
type Output = Pos2;
|
||||
fn add(self, rhs: Vec2) -> Pos2 {
|
||||
Pos2 {
|
||||
|
@ -242,17 +262,7 @@ impl std::ops::Add<Vec2> for Pos2 {
|
|||
}
|
||||
}
|
||||
|
||||
// impl std::ops::Add<Pos2> for Vec2 {
|
||||
// type Output = Pos2;
|
||||
// fn add(self, rhs: Pos2) -> Pos2 {
|
||||
// Pos2 {
|
||||
// x: self.x + rhs.x,
|
||||
// y: self.y + rhs.y,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
impl std::ops::Sub for Pos2 {
|
||||
impl Sub for Pos2 {
|
||||
type Output = Vec2;
|
||||
fn sub(self, rhs: Pos2) -> Vec2 {
|
||||
Vec2 {
|
||||
|
@ -262,7 +272,7 @@ impl std::ops::Sub for Pos2 {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub<Vec2> for Pos2 {
|
||||
impl Sub<Vec2> for Pos2 {
|
||||
type Output = Pos2;
|
||||
fn sub(self, rhs: Vec2) -> Pos2 {
|
||||
Pos2 {
|
||||
|
@ -323,14 +333,17 @@ impl Rect {
|
|||
}
|
||||
|
||||
/// Expand by this much in each direction
|
||||
#[must_use]
|
||||
pub fn expand(self, amnt: f32) -> Self {
|
||||
Rect::from_center_size(self.center(), self.size() + 2.0 * vec2(amnt, amnt))
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn translate(self, amnt: Vec2) -> Self {
|
||||
Rect::from_min_size(self.min() + amnt, self.size())
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn intersect(self, other: Rect) -> Self {
|
||||
Self {
|
||||
min: self.min.max(other.min),
|
||||
|
@ -423,8 +436,8 @@ impl std::fmt::Debug for Rect {
|
|||
|
||||
pub fn lerp<T>(min: T, max: T, t: f32) -> T
|
||||
where
|
||||
f32: std::ops::Mul<T, Output = T>,
|
||||
T: std::ops::Add<T, Output = T>,
|
||||
f32: Mul<T, Output = T>,
|
||||
T: Add<T, Output = T>,
|
||||
{
|
||||
(1.0 - t) * min + t * max
|
||||
}
|
||||
|
|
|
@ -23,9 +23,8 @@ pub struct Memory {
|
|||
/// The widget being interacted with (e.g. dragged, in case of a slider).
|
||||
pub(crate) active_id: Option<Id>,
|
||||
|
||||
/// Which foldable regions are open.
|
||||
// states of various types of widgets
|
||||
pub(crate) foldables: HashMap<Id, FoldableState>,
|
||||
|
||||
windows: HashMap<Id, WindowState>,
|
||||
|
||||
/// Top is last
|
||||
|
|
|
@ -319,7 +319,7 @@ impl Region {
|
|||
))
|
||||
}
|
||||
|
||||
/// Return a sub-region relative to the parent
|
||||
/// Return a sub-region relative to the cursor
|
||||
pub fn relative_region(&mut self, rect: Rect) -> Region {
|
||||
let region_pos = self.cursor + rect.min().to_vec2();
|
||||
let child_rect = Rect::from_min_size(region_pos, rect.size());
|
||||
|
|
|
@ -79,7 +79,7 @@ impl Style {
|
|||
/// Fill color of the interactive part of a component (button, slider grab, checkbox, ...)
|
||||
pub fn interact_fill_color(&self, interact: &InteractInfo) -> Option<Color> {
|
||||
if interact.active {
|
||||
Some(srgba(200, 200, 200, 255))
|
||||
Some(srgba(120, 120, 200, 255))
|
||||
} else if interact.hovered {
|
||||
Some(srgba(100, 100, 150, 255))
|
||||
} else {
|
||||
|
|
|
@ -92,8 +92,18 @@ fn main() {
|
|||
if region.add(Button::new("Quit")).clicked {
|
||||
quit = true;
|
||||
}
|
||||
example_app.ui(&mut region);
|
||||
emigui.ui(&mut region);
|
||||
|
||||
Window::new("Example APP")
|
||||
.default_pos(pos2(200.0, 100.0))
|
||||
.show(region.ctx(), |region| {
|
||||
example_app.ui(region);
|
||||
});
|
||||
|
||||
Window::new("Emigui settings")
|
||||
.default_pos(pos2(200.0, 500.0))
|
||||
.show(region.ctx(), |region| {
|
||||
emigui.ui(region);
|
||||
});
|
||||
|
||||
// TODO: Make it even simpler to show a window
|
||||
Window::new("Test window")
|
||||
|
|
|
@ -59,7 +59,7 @@ impl State {
|
|||
|
||||
region.set_align(Align::Min);
|
||||
region.add_label("WebGl painter info:");
|
||||
region.indent(Id::new(&"webgl region"), |region| {
|
||||
region.indent(Id::new("webgl region"), |region| {
|
||||
region.add_label(self.webgl_painter.debug_info());
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue