1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30:03 -04:00

Remove 'Web' from GuiInput

This commit is contained in:
Emil Ernerfeldt
2020-05-21 12:04:14 +02:00
parent 555d7aed20
commit ae6080405c
8 changed files with 63 additions and 66 deletions

View File

@@ -10,6 +10,8 @@ use crate::{color::*, containers::*, examples::FractalClock, widgets::*, *};
#[derive(Default, Deserialize, Serialize)]
#[serde(default)]
pub struct ExampleApp {
previous_web_location_hash: String,
open_windows: OpenWindows,
// TODO: group the following together as ExampleWindows
example_window: ExampleWindow,
@@ -17,7 +19,20 @@ pub struct ExampleApp {
}
impl ExampleApp {
pub fn ui(&mut self, ui: &mut Ui) {
/// `web_location_hash`: for web demo only. e.g. "#fragmet".
pub fn ui(&mut self, ui: &mut Ui, web_location_hash: &str) {
if self.previous_web_location_hash != web_location_hash {
// #fragment end of URL:
if web_location_hash == "#clock" {
self.open_windows = OpenWindows {
fractal_clock: true,
..OpenWindows::none()
};
}
self.previous_web_location_hash = web_location_hash.to_owned();
}
show_menu_bar(ui, &mut self.open_windows);
self.windows(ui.ctx());
}
@@ -31,24 +46,9 @@ impl ExampleApp {
open_windows,
example_window,
fractal_clock,
..
} = self;
if ctx.previus_input().web != ctx.input().web {
let location_hash = ctx
.input()
.web
.as_ref()
.map(|web| web.location_hash.as_str());
// #fragment end of URL:
if location_hash == Some("#clock") {
*open_windows = OpenWindows {
fractal_clock: true,
..OpenWindows::none()
};
}
}
Window::new("Examples")
.open(&mut open_windows.examples)
.default_pos(pos2(32.0, 100.0))

View File

@@ -31,9 +31,6 @@ pub struct RawInput {
/// In-order events received this frame
pub events: Vec<Event>,
/// Web-only input
pub web: Option<Web>,
}
/// What emigui maintains
@@ -61,9 +58,6 @@ pub struct GuiInput {
/// In-order events received this frame
pub events: Vec<Event>,
/// Web-only input
pub web: Option<Web>,
}
/// What emigui maintains
@@ -91,14 +85,6 @@ pub struct MouseInput {
pub velocity: Vec2,
}
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Deserialize)]
#[serde(default)]
pub struct Web {
pub location: String,
/// i.e. "#fragment"
pub location_hash: String,
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Event {
@@ -149,27 +135,32 @@ impl GuiInput {
dt,
seconds_since_midnight: new.seconds_since_midnight,
events: new.events.clone(),
web: new.web.clone(),
}
}
}
impl MouseInput {
pub fn from_last_and_new(last: &RawInput, new: &RawInput) -> MouseInput {
pub fn from_last_and_new(last: &GuiInput, new: &RawInput) -> MouseInput {
let delta = new
.mouse_pos
.and_then(|new| last.mouse_pos.map(|last| new - last))
.and_then(|new| last.mouse.pos.map(|last| new - last))
.unwrap_or_default();
let dt = (new.time - last.time) as f32;
let mut velocity = delta / dt;
if !velocity.is_finite() {
velocity = Vec2::zero();
}
let pressed = !last.mouse.down && new.mouse_down;
let mut press_origin = last.mouse.press_origin;
if pressed {
press_origin = new.mouse_pos;
}
MouseInput {
down: new.mouse_down && new.mouse_pos.is_some(),
pressed: !last.mouse_down && new.mouse_down,
released: last.mouse_down && !new.mouse_down,
pressed,
released: last.mouse.down && !new.mouse_down,
pos: new.mouse_pos,
press_origin,
delta,
velocity,
}
@@ -188,9 +179,6 @@ impl RawInput {
ui.add(label!("pixels_per_point: {:?}", self.pixels_per_point));
ui.add(label!("time: {:.3} s", self.time));
ui.add(label!("events: {:?}", self.events));
if let Some(web) = &self.web {
web.ui(ui);
}
}
}
@@ -207,9 +195,6 @@ impl GuiInput {
ui.add(label!("pixels_per_point: {}", self.pixels_per_point));
ui.add(label!("time: {:.3} s", self.time));
ui.add(label!("events: {:?}", self.events));
if let Some(web) = &self.web {
web.ui(ui);
}
}
}
@@ -228,11 +213,3 @@ impl MouseInput {
));
}
}
impl Web {
pub fn ui(&self, ui: &mut crate::Ui) {
use crate::label;
ui.add(label!("location: '{}'", self.location));
ui.add(label!("location_hash: '{}'", self.location_hash));
}
}