Use new ui crate version

This commit is contained in:
Michael Hübner 2025-03-04 15:00:59 +01:00
parent d64029abcf
commit 856f24e65e
8 changed files with 49 additions and 70 deletions

View file

@ -57,10 +57,10 @@ impl Context {
&mut self.sound_handler
}
pub fn next_frame<C, F>(&mut self, world: &mut World, mut f: F) -> Result<bool>
pub fn next_frame<'a, C, F>(&mut self, world: &mut World, mut f: F) -> Result<bool>
where
C: Send + Sync + 'static,
F: FnMut(&mut World, &mut C, Event<'_>) -> Result<()> + Send + Sync + 'static,
F: FnMut(&mut World, &mut C, Event<'_>) -> Result<()> + Send + Sync + 'a,
{
let render_core = self.render_core.clone();
let consumer = world.resources.get_mut_unchecked::<C>();

View file

@ -21,11 +21,11 @@ impl TScene for GuiHandlerRenderer {
buffer_recorder: &mut CommandBufferRecorder<'_>,
_images: &TargetMode<Vec<Arc<Image>>>,
indices: &TargetMode<usize>,
world: &World,
world: &mut World,
) -> Result<()> {
world
.resources
.get::<Arc<GuiHandler>>()
.get_mut::<GuiHandler>()
.process(buffer_recorder, indices)
}
@ -34,9 +34,9 @@ impl TScene for GuiHandlerRenderer {
window_width: f32,
window_height: f32,
images: &TargetMode<Vec<Arc<Image>>>,
world: &World,
world: &mut World,
) -> Result<()> {
world.resources.get::<Arc<GuiHandler>>().resize(
world.resources.get_mut::<GuiHandler>().resize(
window_width as u32,
window_height as u32,
images,
@ -249,7 +249,7 @@ impl Engine {
impl Engine {
fn main_system<T: EventConsumer>(world: &mut World) -> Result<bool> {
let gui_handler = world.resources.get_unchecked::<Arc<GuiHandler>>();
let gui_handler = world.resources.get_mut_unchecked::<GuiHandler>();
let input_map = world.resources.get_unchecked::<InputMap>();
let context = world.resources.get_unchecked::<Context>();

View file

@ -39,7 +39,7 @@ impl Engine {
pub(crate) fn event<T: EventConsumer>(
world: &mut World,
context: &Context,
gui_handler: &GuiHandler,
gui_handler: &mut GuiHandler,
input: &InputMap,
consumer: &mut T,
event: Event<'_>,
@ -78,7 +78,7 @@ impl Engine {
Self::button_up_event(world, consumer, controller, button)?;
}
Event::ControllerAxis(controller) => {
if !gui_handler.check_navigatable()? {
if !gui_handler.check_navigatable() {
Self::axis_event(world, consumer, &controller)?
} else {
gui_handler.update_selection(controller.direction())?;
@ -140,7 +140,7 @@ impl Engine {
#[inline]
fn key_up_event<T: EventConsumer>(
world: &mut World,
gui_handler: &GuiHandler,
gui_handler: &mut GuiHandler,
input: &InputMap,
consumer: &mut T,
keycode: Keycode,
@ -160,7 +160,7 @@ impl Engine {
fn key_down_event<T: EventConsumer>(
world: &mut World,
context: &Context,
gui_handler: &GuiHandler,
gui_handler: &mut GuiHandler,
input: &InputMap,
consumer: &mut T,
keycode: Keycode,
@ -188,9 +188,9 @@ impl Engine {
}
}
Keycode::V => {
if let Some(writeable) = gui_handler.writeable()? {
if let Some(writeable) = gui_handler.writeable() {
if let Some(content) = context.window_config().clipboard_content()? {
writeable.set_text(content)?;
writeable.set_text(gui_handler, content)?;
}
}
}
@ -218,12 +218,12 @@ impl Engine {
#[inline]
fn button_down_event<T: EventConsumer>(
world: &mut World,
gui_handler: &GuiHandler,
gui_handler: &mut GuiHandler,
consumer: &mut T,
controller: &Controller,
button: ControllerButton,
) -> Result<()> {
if gui_handler.check_navigatable()? {
if gui_handler.check_navigatable() {
Self::check_button_down(world, gui_handler, consumer, controller, button)?;
} else {
consumer.event(world, EngineEvent::ControllerButtonDown(controller, button))?;
@ -249,7 +249,7 @@ impl Engine {
#[inline]
fn check_button_down<T: EventConsumer>(
world: &mut World,
gui_handler: &GuiHandler,
gui_handler: &mut GuiHandler,
consumer: &mut T,
controller: &Controller,
button: ControllerButton,
@ -338,10 +338,10 @@ impl Engine {
}
#[inline]
fn text_input(gui_handler: &GuiHandler, text: String) -> Result<()> {
if let Some(writeable) = gui_handler.writeable()? {
fn text_input(gui_handler: &mut GuiHandler, text: String) -> Result<()> {
if let Some(writeable) = gui_handler.writeable() {
for c in text.chars() {
writeable.add_letter(c)?;
writeable.add_letter(gui_handler, c)?;
}
}

View file

@ -383,7 +383,7 @@ impl TScene for Scene {
buffer_recorder: &mut CommandBufferRecorder<'_>,
images: &TargetMode<Vec<Arc<Image>>>,
indices: &TargetMode<usize>,
world: &World,
world: &mut World,
) -> anyhow::Result<()> {
self.frame_time = {
let now = world.now();
@ -479,7 +479,7 @@ impl TScene for Scene {
window_width: f32,
window_height: f32,
images: &TargetMode<Vec<Arc<Image>>>,
_world: &World,
_world: &mut World,
) -> anyhow::Result<()> {
self.screen_width = window_width;
self.screen_height = window_height;

View file

@ -16,20 +16,22 @@ pub struct LoadingScreen<T: Send + Sync + 'static> {
}
impl<T: Send + Sync + 'static> LoadingScreen<T> {
pub fn load<R, L, G>(gui: Arc<G>, loader: L) -> Result<Self>
pub fn load<R, L, G>(gui_handler: &mut GuiHandler, gui: Arc<G>, loader: L) -> Result<Self>
where
R: Fn(T) -> Result<()> + Send + Sync + 'static,
T: Send + Sync + 'static,
L: FnOnce() -> T + Send + Sync + 'static,
G: TopLevelGui + TopGui + Send + Sync + 'static,
{
gui.enable()?;
gui.enable(gui_handler)?;
let (sender, receiver) = channel();
thread::spawn(move || {
let _ = sender.send(loader());
let _ = gui.disable();
// TODO: disable loading screen gui
// let _ = gui.disable();
});
Ok(Self {

View file

@ -4,95 +4,81 @@ use assetpath::AssetPath;
use mlua::{FromLua, FromLuaMulti, Function, IntoLua, IntoLuaMulti, Lua, Table as MLuaTable};
pub struct Globals<'a> {
table: MLuaTable<'a>,
table: MLuaTable,
lua: &'a Lua,
}
impl<'a> Globals<'a> {
pub fn set<K: IntoLua<'a>, V: IntoLua<'a>>(
&self,
key: K,
value: V,
) -> Result<(), anyhow::Error> {
pub fn set<K: IntoLua, V: IntoLua>(&self, key: K, value: V) -> Result<(), anyhow::Error> {
self.table.set(key, value)?;
Ok(())
}
pub fn get<K: IntoLua<'a>, V: FromLua<'a>>(&self, key: K) -> Result<V, anyhow::Error> {
pub fn get<K: IntoLua, V: FromLua>(&self, key: K) -> Result<V, anyhow::Error> {
Ok(self.table.get(key)?)
}
pub fn create_table(&self) -> Result<Table<'_>, anyhow::Error> {
pub fn create_table(&self) -> Result<Table, anyhow::Error> {
Ok(Table {
table: self.lua.create_table()?,
})
}
pub fn set_table(&self, name: &str, table: Table<'_>) -> Result<(), anyhow::Error> {
pub fn set_table(&self, name: &str, table: Table) -> Result<(), anyhow::Error> {
self.set(name, table.table)?;
Ok(())
}
pub fn get_table(&self, name: &str) -> Result<Table<'_>, anyhow::Error> {
pub fn get_table(&self, name: &str) -> Result<Table, anyhow::Error> {
Ok(Table {
table: self.get(name)?,
})
}
}
pub struct Table<'a> {
table: MLuaTable<'a>,
pub struct Table {
table: MLuaTable,
}
impl<'a> Table<'a> {
pub fn set<K: IntoLua<'a>, V: IntoLua<'a>>(
&self,
key: K,
value: V,
) -> Result<(), anyhow::Error> {
impl Table {
pub fn set<K: IntoLua, V: IntoLua>(&self, key: K, value: V) -> Result<(), anyhow::Error> {
self.table.set(key, value)?;
Ok(())
}
pub fn get<K: IntoLua<'a>, V: FromLua<'a>>(&self, key: K) -> Result<V, anyhow::Error> {
pub fn get<K: IntoLua, V: FromLua>(&self, key: K) -> Result<V, anyhow::Error> {
Ok(self.table.get(key)?)
}
}
pub struct LuaFunction {
lua: Option<&'static Lua>,
function: ManuallyDrop<Function<'static>>,
lua: Lua,
function: ManuallyDrop<Function>,
}
impl LuaFunction {
pub fn new(path: &AssetPath) -> Result<Self, anyhow::Error> {
// into_static leaks the object
let lua = Lua::new().into_static();
let lua = Lua::new();
let function = ManuallyDrop::new(
lua.load(fs::read_to_string(&path.full_path())?.as_bytes())
.eval()?,
);
Ok(Self {
lua: Some(lua),
function,
})
Ok(Self { lua, function })
}
pub fn globals<F, E>(&self, mut change_globals: F) -> Result<(), anyhow::Error>
where
F: FnMut(&Globals<'_>) -> Result<(), E>,
F: FnMut(&Globals) -> Result<(), E>,
E: std::error::Error + Send + Sync + 'static,
{
let lua = self.lua.as_ref().unwrap();
let globals = Globals {
table: lua.globals(),
lua,
table: self.lua.globals(),
lua: &self.lua,
};
change_globals(&globals)?;
@ -100,10 +86,7 @@ impl LuaFunction {
Ok(())
}
pub fn execute<'a, A: IntoLuaMulti<'a>, R: FromLuaMulti<'a>>(
&self,
input: A,
) -> Result<R, anyhow::Error> {
pub fn execute<A: IntoLuaMulti, R: FromLuaMulti>(&self, input: A) -> Result<R, anyhow::Error> {
Ok(self.function.call(input)?)
}
}
@ -118,11 +101,5 @@ impl Drop for LuaFunction {
unsafe {
ManuallyDrop::drop(&mut self.function);
}
if let Some(lua) = self.lua.take() {
// we need to drop it, due to leakage from static cast
let l = unsafe { Lua::from_static(lua) };
drop(l);
}
}
}

View file

@ -17,7 +17,7 @@ pub trait TScene: Send + Sync + 'static {
buffer_recorder: &mut CommandBufferRecorder<'_>,
images: &TargetMode<Vec<Arc<Image>>>,
indices: &TargetMode<usize>,
world: &World,
world: &mut World,
) -> Result<()>;
fn resize(
@ -25,7 +25,7 @@ pub trait TScene: Send + Sync + 'static {
window_width: f32,
window_height: f32,
images: &TargetMode<Vec<Arc<Image>>>,
world: &World,
world: &mut World,
) -> Result<()>;
}

View file

@ -266,7 +266,7 @@ impl TScene for SkyBox {
buffer_recorder: &mut CommandBufferRecorder<'_>,
_images: &TargetMode<Vec<Arc<Image>>>,
indices: &TargetMode<usize>,
_world: &World,
_world: &mut World,
) -> Result<()> {
if !self.enabled {
return Ok(());
@ -298,7 +298,7 @@ impl TScene for SkyBox {
_window_width: f32,
_window_height: f32,
_images: &TargetMode<Vec<Arc<Image>>>,
world: &World,
world: &mut World,
) -> Result<()> {
let sample_count = world
.resources