2020-04-21 12:47:17 +00:00
|
|
|
use std::collections::HashMap;
|
2020-04-17 13:29:48 +00:00
|
|
|
|
|
|
|
use crate::{window::WindowState, *};
|
|
|
|
|
2020-04-21 12:47:17 +00:00
|
|
|
// TODO: move together with foldable code into own file
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct FoldableState {
|
|
|
|
pub open: bool,
|
|
|
|
pub toggle_time: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FoldableState {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
open: false,
|
|
|
|
toggle_time: -std::f64::INFINITY,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 18:52:17 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
|
|
pub struct ScrollState {
|
|
|
|
/// Positive offset means scrolling down/right
|
|
|
|
pub offset: Vec2,
|
|
|
|
}
|
2020-04-21 12:47:17 +00:00
|
|
|
|
2020-04-17 13:29:48 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct Memory {
|
|
|
|
/// The widget being interacted with (e.g. dragged, in case of a slider).
|
|
|
|
pub(crate) active_id: Option<Id>,
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
// states of various types of widgets
|
2020-04-21 12:47:17 +00:00
|
|
|
pub(crate) foldables: HashMap<Id, FoldableState>,
|
2020-04-21 18:52:17 +00:00
|
|
|
pub(crate) scroll_areas: HashMap<Id, ScrollState>,
|
2020-04-17 13:29:48 +00:00
|
|
|
windows: HashMap<Id, WindowState>,
|
|
|
|
|
|
|
|
/// Top is last
|
2020-04-17 21:22:28 +00:00
|
|
|
pub window_order: Vec<Id>,
|
2020-04-17 13:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Memory {
|
2020-04-19 21:34:34 +00:00
|
|
|
pub fn get_window(&mut self, id: Id) -> Option<WindowState> {
|
|
|
|
self.windows.get(&id).cloned()
|
|
|
|
}
|
|
|
|
|
2020-04-17 13:29:48 +00:00
|
|
|
pub fn set_window_state(&mut self, id: Id, state: WindowState) {
|
2020-04-19 21:34:34 +00:00
|
|
|
let did_insert = self.windows.insert(id, state).is_none();
|
|
|
|
if did_insert {
|
|
|
|
self.window_order.push(id);
|
|
|
|
}
|
2020-04-17 13:29:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 18:46:30 +00:00
|
|
|
/// TODO: call once at the start of the frame for the current mouse pos
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn layer_at(&self, pos: Pos2) -> Layer {
|
2020-04-17 13:29:48 +00:00
|
|
|
for window_id in self.window_order.iter().rev() {
|
|
|
|
if let Some(state) = self.windows.get(window_id) {
|
2020-04-21 18:46:30 +00:00
|
|
|
if state.outer_rect.contains(pos) {
|
2020-04-17 13:29:48 +00:00
|
|
|
return Layer::Window(*window_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Layer::Background
|
|
|
|
}
|
|
|
|
|
2020-04-17 21:22:28 +00:00
|
|
|
pub fn move_window_to_top(&mut self, id: Id) {
|
|
|
|
if self.window_order.last() == Some(&id) {
|
|
|
|
return; // common case early-out
|
|
|
|
}
|
|
|
|
if let Some(index) = self.window_order.iter().position(|x| *x == id) {
|
|
|
|
self.window_order.remove(index);
|
|
|
|
}
|
|
|
|
self.window_order.push(id);
|
2020-04-17 13:29:48 +00:00
|
|
|
}
|
|
|
|
}
|