Fixed connect_signal for mouse, made not an object
use std::fmt::{self, Display, Formatter};
use std::default::Default;
-use std::rc::Rc;
-use rlua::{self, Table, Lua, UserData, ToLua, Value};
-use super::object::{Object, Objectable};
-use super::class::{self, Class, ClassBuilder};
+use rlua::{self, Table, Lua, UserData, ToLua};
+use super::signal;
#[derive(Clone, Debug)]
pub struct MouseState {
@@ -13,8 +11,6 @@ pub struct MouseState {
dummy: i32
}
-pub struct Mouse<'lua>(Table<'lua>);
-
impl Default for MouseState {
fn default() -> Self {
MouseState {
@@ -23,38 +19,36 @@ impl Default for MouseState {
}
}
-impl <'lua> Mouse<'lua> {
- fn new(lua: &Lua) -> rlua::Result<Object> {
- // TODO FIXME
- let class = class::button_class(lua)?;
- Ok(Mouse::allocate(lua, class)?.build())
- }
-}
-
impl Display for MouseState {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Mouse: {:p}", self)
}
}
-impl <'lua> ToLua<'lua> for Mouse<'lua> {
- fn to_lua(self, lua: &'lua Lua) -> rlua::Result<Value<'lua>> {
- self.0.to_lua(lua)
- }
-}
-
impl UserData for MouseState {}
-pub fn init(lua: &Lua) -> rlua::Result<Class> {
- method_setup(lua, Class::builder(lua, Some(Rc::new(Mouse::new)), None, None)?)?
- .save_class("mouse")?
- .build()
+pub fn init(lua: &Lua) -> rlua::Result<()> {
+ let mouse_table = lua.create_table();
+ state_setup(lua, &mouse_table)?;
+ meta_setup(lua, &mouse_table)?;
+ method_setup(lua, &mouse_table)?;
+ let globals = lua.globals();
+ globals.set("mouse", mouse_table)
}
-fn method_setup<'lua>(lua: &'lua Lua, builder: ClassBuilder<'lua>) -> rlua::Result<ClassBuilder<'lua>> {
- // TODO Do properly
- use super::dummy;
- builder.method("connect_signal".into(), lua.create_function(dummy))
+fn state_setup(lua: &Lua, mouse_table: &Table) -> rlua::Result<()> {
+ mouse_table.set("__data", MouseState::default().to_lua(lua)?)
}
-impl_objectable!(Mouse, MouseState);
+fn meta_setup(lua: &Lua, mouse_table: &Table) -> rlua::Result<()> {
+ let meta_table = lua.create_table();
+ mouse_table.set("__tostring", lua.create_function(|_, val: Table| {
+ Ok(format!("{}", val.get::<_, MouseState>("__data")?))
+ }))?;
+ mouse_table.set_metatable(Some(meta_table));
+ Ok(())
+}
+
+fn method_setup(lua: &Lua, mouse_table: &Table) -> rlua::Result<()> {
+ mouse_table.set("connect_signal", lua.create_function(signal::global_connect_signal))
+}
