Skip to content

Commit

Permalink
A bit more tidying up
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Aug 8, 2024
1 parent 8fbfae3 commit b926dd5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
20 changes: 18 additions & 2 deletions core/common/src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::{any::Any, sync::Arc};
pub use mlua::prelude::*;

use crate::{
Callable, Callback, CallbackError, Color, Color32, FromVariant, Quat, ToVariant, ToVirtualPath, Variant, Vec2, Vec3,
Vec4,
Callable, Callback, CallbackError, Color, Color32, FromVariant, Quat, StringName, ToVariant, ToVirtualPath, Variant,
Vec2, Vec3, Vec4,
};

/// A Lua scripting engine.
Expand Down Expand Up @@ -196,6 +196,22 @@ impl<'lua> FromLua<'lua> for Variant {
}
}

/// Special handling for StringNames directly into IDs
impl<'lua> IntoLua<'lua> for StringName {
fn into_lua(self, _lua: &'lua Lua) -> LuaResult<LuaValue<'lua>> {
Ok(LuaValue::Integer(self.id().into()))
}
}

impl<'lua> FromLua<'lua> for StringName {
fn from_lua(value: LuaValue<'lua>, _lua: &'lua Lua) -> LuaResult<Self> {
match value {
LuaValue::Integer(value) => Ok(StringName::from_id(value.into())),
_ => Err(LuaError::UserDataTypeMismatch),
}
}
}

/// Wraps many [`Variant`]s to be passed as arguments to a Lua function.
///
/// This is necessary because Lua does not support variadic arguments.
Expand Down
12 changes: 11 additions & 1 deletion core/common/src/strings/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use crate::{Arena, Singleton};

crate::impl_arena_index!(StringId, "Identifies a string in a string pool.");
crate::impl_arena_index!(pub(crate) StringId, "Identifies a string in a string pool.");

/// A trait for objects that can be converted to a [`StringName`].
pub trait ToStringName {
Expand Down Expand Up @@ -34,6 +34,16 @@ impl StringName {
id: unsafe { intern_string(value) },
}
}

/// Creates a new string name from a string ID.
pub(crate) fn from_id(id: StringId) -> Self {
Self { id }
}

/// Returns the ID of the string.
pub(crate) fn id(&self) -> StringId {
self.id
}
}

/// Converts a string reference to a string name.
Expand Down
4 changes: 2 additions & 2 deletions core/input/src/keyboards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ pub trait KeyboardDevice {
}

/// A keyboard event.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum KeyboardEvent {
KeyDown(VirtualKey),
KeyUp(VirtualKey),
}

/// Possible key codes on a keyboard.
#[repr(u32)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum VirtualKey {
Escape,
F0,
Expand Down
2 changes: 1 addition & 1 deletion core/input/src/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub trait MouseDevice {
}

/// A mouse event.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum MouseEvent {
MouseMove { position: Vec2, delta: Vec2 },
MouseDown(MouseButton),
Expand Down

0 comments on commit b926dd5

Please sign in to comment.