Use new ui crate version
This commit is contained in:
parent
d64029abcf
commit
856f24e65e
8 changed files with 49 additions and 70 deletions
|
@ -57,10 +57,10 @@ impl Context {
|
||||||
&mut self.sound_handler
|
&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
|
where
|
||||||
C: Send + Sync + 'static,
|
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 render_core = self.render_core.clone();
|
||||||
let consumer = world.resources.get_mut_unchecked::<C>();
|
let consumer = world.resources.get_mut_unchecked::<C>();
|
||||||
|
|
|
@ -21,11 +21,11 @@ impl TScene for GuiHandlerRenderer {
|
||||||
buffer_recorder: &mut CommandBufferRecorder<'_>,
|
buffer_recorder: &mut CommandBufferRecorder<'_>,
|
||||||
_images: &TargetMode<Vec<Arc<Image>>>,
|
_images: &TargetMode<Vec<Arc<Image>>>,
|
||||||
indices: &TargetMode<usize>,
|
indices: &TargetMode<usize>,
|
||||||
world: &World,
|
world: &mut World,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
world
|
world
|
||||||
.resources
|
.resources
|
||||||
.get::<Arc<GuiHandler>>()
|
.get_mut::<GuiHandler>()
|
||||||
.process(buffer_recorder, indices)
|
.process(buffer_recorder, indices)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,9 +34,9 @@ impl TScene for GuiHandlerRenderer {
|
||||||
window_width: f32,
|
window_width: f32,
|
||||||
window_height: f32,
|
window_height: f32,
|
||||||
images: &TargetMode<Vec<Arc<Image>>>,
|
images: &TargetMode<Vec<Arc<Image>>>,
|
||||||
world: &World,
|
world: &mut World,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
world.resources.get::<Arc<GuiHandler>>().resize(
|
world.resources.get_mut::<GuiHandler>().resize(
|
||||||
window_width as u32,
|
window_width as u32,
|
||||||
window_height as u32,
|
window_height as u32,
|
||||||
images,
|
images,
|
||||||
|
@ -249,7 +249,7 @@ impl Engine {
|
||||||
|
|
||||||
impl Engine {
|
impl Engine {
|
||||||
fn main_system<T: EventConsumer>(world: &mut World) -> Result<bool> {
|
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 input_map = world.resources.get_unchecked::<InputMap>();
|
||||||
let context = world.resources.get_unchecked::<Context>();
|
let context = world.resources.get_unchecked::<Context>();
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ impl Engine {
|
||||||
pub(crate) fn event<T: EventConsumer>(
|
pub(crate) fn event<T: EventConsumer>(
|
||||||
world: &mut World,
|
world: &mut World,
|
||||||
context: &Context,
|
context: &Context,
|
||||||
gui_handler: &GuiHandler,
|
gui_handler: &mut GuiHandler,
|
||||||
input: &InputMap,
|
input: &InputMap,
|
||||||
consumer: &mut T,
|
consumer: &mut T,
|
||||||
event: Event<'_>,
|
event: Event<'_>,
|
||||||
|
@ -78,7 +78,7 @@ impl Engine {
|
||||||
Self::button_up_event(world, consumer, controller, button)?;
|
Self::button_up_event(world, consumer, controller, button)?;
|
||||||
}
|
}
|
||||||
Event::ControllerAxis(controller) => {
|
Event::ControllerAxis(controller) => {
|
||||||
if !gui_handler.check_navigatable()? {
|
if !gui_handler.check_navigatable() {
|
||||||
Self::axis_event(world, consumer, &controller)?
|
Self::axis_event(world, consumer, &controller)?
|
||||||
} else {
|
} else {
|
||||||
gui_handler.update_selection(controller.direction())?;
|
gui_handler.update_selection(controller.direction())?;
|
||||||
|
@ -140,7 +140,7 @@ impl Engine {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn key_up_event<T: EventConsumer>(
|
fn key_up_event<T: EventConsumer>(
|
||||||
world: &mut World,
|
world: &mut World,
|
||||||
gui_handler: &GuiHandler,
|
gui_handler: &mut GuiHandler,
|
||||||
input: &InputMap,
|
input: &InputMap,
|
||||||
consumer: &mut T,
|
consumer: &mut T,
|
||||||
keycode: Keycode,
|
keycode: Keycode,
|
||||||
|
@ -160,7 +160,7 @@ impl Engine {
|
||||||
fn key_down_event<T: EventConsumer>(
|
fn key_down_event<T: EventConsumer>(
|
||||||
world: &mut World,
|
world: &mut World,
|
||||||
context: &Context,
|
context: &Context,
|
||||||
gui_handler: &GuiHandler,
|
gui_handler: &mut GuiHandler,
|
||||||
input: &InputMap,
|
input: &InputMap,
|
||||||
consumer: &mut T,
|
consumer: &mut T,
|
||||||
keycode: Keycode,
|
keycode: Keycode,
|
||||||
|
@ -188,9 +188,9 @@ impl Engine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Keycode::V => {
|
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()? {
|
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]
|
#[inline]
|
||||||
fn button_down_event<T: EventConsumer>(
|
fn button_down_event<T: EventConsumer>(
|
||||||
world: &mut World,
|
world: &mut World,
|
||||||
gui_handler: &GuiHandler,
|
gui_handler: &mut GuiHandler,
|
||||||
consumer: &mut T,
|
consumer: &mut T,
|
||||||
controller: &Controller,
|
controller: &Controller,
|
||||||
button: ControllerButton,
|
button: ControllerButton,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
if gui_handler.check_navigatable()? {
|
if gui_handler.check_navigatable() {
|
||||||
Self::check_button_down(world, gui_handler, consumer, controller, button)?;
|
Self::check_button_down(world, gui_handler, consumer, controller, button)?;
|
||||||
} else {
|
} else {
|
||||||
consumer.event(world, EngineEvent::ControllerButtonDown(controller, button))?;
|
consumer.event(world, EngineEvent::ControllerButtonDown(controller, button))?;
|
||||||
|
@ -249,7 +249,7 @@ impl Engine {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn check_button_down<T: EventConsumer>(
|
fn check_button_down<T: EventConsumer>(
|
||||||
world: &mut World,
|
world: &mut World,
|
||||||
gui_handler: &GuiHandler,
|
gui_handler: &mut GuiHandler,
|
||||||
consumer: &mut T,
|
consumer: &mut T,
|
||||||
controller: &Controller,
|
controller: &Controller,
|
||||||
button: ControllerButton,
|
button: ControllerButton,
|
||||||
|
@ -338,10 +338,10 @@ impl Engine {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn text_input(gui_handler: &GuiHandler, text: String) -> Result<()> {
|
fn text_input(gui_handler: &mut GuiHandler, text: String) -> Result<()> {
|
||||||
if let Some(writeable) = gui_handler.writeable()? {
|
if let Some(writeable) = gui_handler.writeable() {
|
||||||
for c in text.chars() {
|
for c in text.chars() {
|
||||||
writeable.add_letter(c)?;
|
writeable.add_letter(gui_handler, c)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -383,7 +383,7 @@ impl TScene for Scene {
|
||||||
buffer_recorder: &mut CommandBufferRecorder<'_>,
|
buffer_recorder: &mut CommandBufferRecorder<'_>,
|
||||||
images: &TargetMode<Vec<Arc<Image>>>,
|
images: &TargetMode<Vec<Arc<Image>>>,
|
||||||
indices: &TargetMode<usize>,
|
indices: &TargetMode<usize>,
|
||||||
world: &World,
|
world: &mut World,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
self.frame_time = {
|
self.frame_time = {
|
||||||
let now = world.now();
|
let now = world.now();
|
||||||
|
@ -479,7 +479,7 @@ impl TScene for Scene {
|
||||||
window_width: f32,
|
window_width: f32,
|
||||||
window_height: f32,
|
window_height: f32,
|
||||||
images: &TargetMode<Vec<Arc<Image>>>,
|
images: &TargetMode<Vec<Arc<Image>>>,
|
||||||
_world: &World,
|
_world: &mut World,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
self.screen_width = window_width;
|
self.screen_width = window_width;
|
||||||
self.screen_height = window_height;
|
self.screen_height = window_height;
|
||||||
|
|
|
@ -16,20 +16,22 @@ pub struct LoadingScreen<T: Send + Sync + 'static> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Send + Sync + 'static> LoadingScreen<T> {
|
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
|
where
|
||||||
R: Fn(T) -> Result<()> + Send + Sync + 'static,
|
R: Fn(T) -> Result<()> + Send + Sync + 'static,
|
||||||
T: Send + Sync + 'static,
|
T: Send + Sync + 'static,
|
||||||
L: FnOnce() -> T + Send + Sync + 'static,
|
L: FnOnce() -> T + Send + Sync + 'static,
|
||||||
G: TopLevelGui + TopGui + Send + Sync + 'static,
|
G: TopLevelGui + TopGui + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
gui.enable()?;
|
gui.enable(gui_handler)?;
|
||||||
|
|
||||||
let (sender, receiver) = channel();
|
let (sender, receiver) = channel();
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let _ = sender.send(loader());
|
let _ = sender.send(loader());
|
||||||
let _ = gui.disable();
|
|
||||||
|
// TODO: disable loading screen gui
|
||||||
|
// let _ = gui.disable();
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
|
|
@ -4,95 +4,81 @@ use assetpath::AssetPath;
|
||||||
use mlua::{FromLua, FromLuaMulti, Function, IntoLua, IntoLuaMulti, Lua, Table as MLuaTable};
|
use mlua::{FromLua, FromLuaMulti, Function, IntoLua, IntoLuaMulti, Lua, Table as MLuaTable};
|
||||||
|
|
||||||
pub struct Globals<'a> {
|
pub struct Globals<'a> {
|
||||||
table: MLuaTable<'a>,
|
table: MLuaTable,
|
||||||
lua: &'a Lua,
|
lua: &'a Lua,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Globals<'a> {
|
impl<'a> Globals<'a> {
|
||||||
pub fn set<K: IntoLua<'a>, V: IntoLua<'a>>(
|
pub fn set<K: IntoLua, V: IntoLua>(&self, key: K, value: V) -> Result<(), anyhow::Error> {
|
||||||
&self,
|
|
||||||
key: K,
|
|
||||||
value: V,
|
|
||||||
) -> Result<(), anyhow::Error> {
|
|
||||||
self.table.set(key, value)?;
|
self.table.set(key, value)?;
|
||||||
|
|
||||||
Ok(())
|
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)?)
|
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 {
|
Ok(Table {
|
||||||
table: self.lua.create_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)?;
|
self.set(name, table.table)?;
|
||||||
|
|
||||||
Ok(())
|
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 {
|
Ok(Table {
|
||||||
table: self.get(name)?,
|
table: self.get(name)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Table<'a> {
|
pub struct Table {
|
||||||
table: MLuaTable<'a>,
|
table: MLuaTable,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Table<'a> {
|
impl Table {
|
||||||
pub fn set<K: IntoLua<'a>, V: IntoLua<'a>>(
|
pub fn set<K: IntoLua, V: IntoLua>(&self, key: K, value: V) -> Result<(), anyhow::Error> {
|
||||||
&self,
|
|
||||||
key: K,
|
|
||||||
value: V,
|
|
||||||
) -> Result<(), anyhow::Error> {
|
|
||||||
self.table.set(key, value)?;
|
self.table.set(key, value)?;
|
||||||
|
|
||||||
Ok(())
|
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)?)
|
Ok(self.table.get(key)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct LuaFunction {
|
pub struct LuaFunction {
|
||||||
lua: Option<&'static Lua>,
|
lua: Lua,
|
||||||
function: ManuallyDrop<Function<'static>>,
|
function: ManuallyDrop<Function>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LuaFunction {
|
impl LuaFunction {
|
||||||
pub fn new(path: &AssetPath) -> Result<Self, anyhow::Error> {
|
pub fn new(path: &AssetPath) -> Result<Self, anyhow::Error> {
|
||||||
// into_static leaks the object
|
let lua = Lua::new();
|
||||||
let lua = Lua::new().into_static();
|
|
||||||
|
|
||||||
let function = ManuallyDrop::new(
|
let function = ManuallyDrop::new(
|
||||||
lua.load(fs::read_to_string(&path.full_path())?.as_bytes())
|
lua.load(fs::read_to_string(&path.full_path())?.as_bytes())
|
||||||
.eval()?,
|
.eval()?,
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self { lua, function })
|
||||||
lua: Some(lua),
|
|
||||||
function,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn globals<F, E>(&self, mut change_globals: F) -> Result<(), anyhow::Error>
|
pub fn globals<F, E>(&self, mut change_globals: F) -> Result<(), anyhow::Error>
|
||||||
where
|
where
|
||||||
F: FnMut(&Globals<'_>) -> Result<(), E>,
|
F: FnMut(&Globals) -> Result<(), E>,
|
||||||
E: std::error::Error + Send + Sync + 'static,
|
E: std::error::Error + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
let lua = self.lua.as_ref().unwrap();
|
|
||||||
|
|
||||||
let globals = Globals {
|
let globals = Globals {
|
||||||
table: lua.globals(),
|
table: self.lua.globals(),
|
||||||
lua,
|
lua: &self.lua,
|
||||||
};
|
};
|
||||||
|
|
||||||
change_globals(&globals)?;
|
change_globals(&globals)?;
|
||||||
|
@ -100,10 +86,7 @@ impl LuaFunction {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn execute<'a, A: IntoLuaMulti<'a>, R: FromLuaMulti<'a>>(
|
pub fn execute<A: IntoLuaMulti, R: FromLuaMulti>(&self, input: A) -> Result<R, anyhow::Error> {
|
||||||
&self,
|
|
||||||
input: A,
|
|
||||||
) -> Result<R, anyhow::Error> {
|
|
||||||
Ok(self.function.call(input)?)
|
Ok(self.function.call(input)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,11 +101,5 @@ impl Drop for LuaFunction {
|
||||||
unsafe {
|
unsafe {
|
||||||
ManuallyDrop::drop(&mut self.function);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ pub trait TScene: Send + Sync + 'static {
|
||||||
buffer_recorder: &mut CommandBufferRecorder<'_>,
|
buffer_recorder: &mut CommandBufferRecorder<'_>,
|
||||||
images: &TargetMode<Vec<Arc<Image>>>,
|
images: &TargetMode<Vec<Arc<Image>>>,
|
||||||
indices: &TargetMode<usize>,
|
indices: &TargetMode<usize>,
|
||||||
world: &World,
|
world: &mut World,
|
||||||
) -> Result<()>;
|
) -> Result<()>;
|
||||||
|
|
||||||
fn resize(
|
fn resize(
|
||||||
|
@ -25,7 +25,7 @@ pub trait TScene: Send + Sync + 'static {
|
||||||
window_width: f32,
|
window_width: f32,
|
||||||
window_height: f32,
|
window_height: f32,
|
||||||
images: &TargetMode<Vec<Arc<Image>>>,
|
images: &TargetMode<Vec<Arc<Image>>>,
|
||||||
world: &World,
|
world: &mut World,
|
||||||
) -> Result<()>;
|
) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -266,7 +266,7 @@ impl TScene for SkyBox {
|
||||||
buffer_recorder: &mut CommandBufferRecorder<'_>,
|
buffer_recorder: &mut CommandBufferRecorder<'_>,
|
||||||
_images: &TargetMode<Vec<Arc<Image>>>,
|
_images: &TargetMode<Vec<Arc<Image>>>,
|
||||||
indices: &TargetMode<usize>,
|
indices: &TargetMode<usize>,
|
||||||
_world: &World,
|
_world: &mut World,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
if !self.enabled {
|
if !self.enabled {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -298,7 +298,7 @@ impl TScene for SkyBox {
|
||||||
_window_width: f32,
|
_window_width: f32,
|
||||||
_window_height: f32,
|
_window_height: f32,
|
||||||
_images: &TargetMode<Vec<Arc<Image>>>,
|
_images: &TargetMode<Vec<Arc<Image>>>,
|
||||||
world: &World,
|
world: &mut World,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let sample_count = world
|
let sample_count = world
|
||||||
.resources
|
.resources
|
||||||
|
|
Loading…
Reference in a new issue