better global state structure?

This commit is contained in:
2025-12-19 21:54:48 -05:00
parent 30bc55c78e
commit bae17235c6
23 changed files with 335 additions and 230 deletions

View File

@@ -5,7 +5,7 @@ use std::{marker::Sized, sync::Arc, time::Instant};
use winit::{
event::{Ime, WindowEvent},
event_loop::{ActiveEventLoop, EventLoopProxy},
window::Window,
window::{Window, WindowAttributes},
};
mod app;
@@ -24,47 +24,7 @@ pub use sense::*;
pub type Proxy<Event> = EventLoopProxy<Event>;
pub struct DefaultUiRsc<State> {
pub ui: Ui,
pub ui_state: UiState,
pub events: EventManager<State>,
}
impl<State> DefaultUiRsc<State> {
pub fn new(window: impl Into<Arc<Window>>) -> Self {
Self {
ui: Ui::new(),
ui_state: UiState::new(window),
events: EventManager::default(),
}
}
}
impl<State: 'static> HasUi for DefaultUiRsc<State> {
fn ui_ref(&self) -> &Ui {
&self.ui
}
fn ui(&mut self) -> &mut Ui {
&mut self.ui
}
}
impl<State: 'static + HasRsc<Rsc = Self>> HasEvents for DefaultUiRsc<State> {
type State = State;
fn events(&mut self) -> &mut EventManager<State> {
&mut self.events
}
}
impl<State: 'static> HasUiState for DefaultUiRsc<State> {
fn ui_state(&mut self) -> &mut UiState {
&mut self.ui_state
}
}
pub struct UiState {
pub struct DefaultUiState {
pub renderer: UiRenderer,
pub input: Input,
pub focus: Option<WidgetRef<TextEdit>>,
@@ -74,7 +34,7 @@ pub struct UiState {
pub last_click: Instant,
}
impl UiState {
impl DefaultUiState {
pub fn new(window: impl Into<Arc<Window>>) -> Self {
let window = window.into();
Self {
@@ -89,39 +49,44 @@ impl UiState {
}
}
pub trait HasUiState: Sized + 'static + HasUi {
fn ui_state(&mut self) -> &mut UiState;
fn ui_with_ui_state(&mut self) -> (&mut Ui, &mut UiState) {
pub trait HasDefaultUiState: Sized + 'static + HasUi {
fn get(&self) -> &DefaultUiState;
fn get_mut(&mut self) -> &mut DefaultUiState;
fn ui_state(&self) -> &DefaultUiState {
HasDefaultUiState::get(self)
}
fn ui_state_mut(&mut self) -> &mut DefaultUiState {
HasDefaultUiState::get_mut(self)
}
fn ui_with_state(&mut self) -> (&mut Ui, &mut DefaultUiState) {
// as long as you're not doing anything actually unhinged this should always work safely
(unsafe { forget_mut(self.ui()) }, self.ui_state())
(
unsafe { forget_mut(self.ui_mut()) },
HasDefaultUiState::get_mut(self),
)
}
}
impl<R: HasRsc> HasUiState for R
where
R::Rsc: HasUiState,
{
fn ui_state(&mut self) -> &mut UiState {
self.rsc_mut().ui_state()
}
}
pub trait DefaultAppState: Sized + 'static + HasRsc + HasUi + HasUiState {
pub trait DefaultAppState: RunEvents + HasDefaultUiState {
type Event: 'static = ();
fn new(event_loop: &ActiveEventLoop, proxy: Proxy<Self::Event>) -> Self;
fn new(ui_state: DefaultUiState, proxy: Proxy<Self::Event>) -> Self;
#[allow(unused_variables)]
fn event(&mut self, event: Self::Event) {}
#[allow(unused_variables)]
fn exit(&mut self) {}
#[allow(unused_variables)]
fn window_event(&mut self, event: WindowEvent) {}
fn window_attributes() -> WindowAttributes {
Default::default()
}
}
impl<State: DefaultAppState> AppState for State {
type Event = State::Event;
fn new(event_loop: &ActiveEventLoop, proxy: EventLoopProxy<Self::Event>) -> Self {
Self::new(event_loop, proxy)
let window = event_loop.create_window(Self::window_attributes()).unwrap();
Self::new(DefaultUiState::new(window), proxy)
}
fn event(&mut self, event: Self::Event, _: &ActiveEventLoop) {
@@ -129,8 +94,8 @@ impl<State: DefaultAppState> AppState for State {
}
fn window_event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
let events = unsafe { forget_mut(self.events()) };
let ui_state = self.ui_state();
let events = unsafe { forget_mut(self.events_mut()) };
let ui_state = HasDefaultUiState::get_mut(self);
let input_changed = ui_state.input.event(&event);
let cursor_state = ui_state.cursor_state().clone();
let old = ui_state.focus;
@@ -141,7 +106,7 @@ impl<State: DefaultAppState> AppState for State {
let window_size = ui_state.window_size();
self.run_sensors(&cursor_state, window_size);
}
let (mut ui, mut ui_state) = self.ui_with_ui_state();
let (mut ui, mut ui_state) = self.ui_with_state();
if old != ui_state.focus
&& let Some(old) = old
{
@@ -208,7 +173,7 @@ impl<State: DefaultAppState> AppState for State {
_ => (),
}
self.window_event(event);
(ui, ui_state) = self.ui_with_ui_state();
(ui, ui_state) = self.ui_with_state();
if ui.needs_redraw() {
ui_state.renderer.window().request_redraw();
}