gizmo_scripting/
api_input.rs1use gizmo_core::input::Input;
7use mlua::prelude::*;
8
9pub fn register_input_api(lua: &Lua) -> Result<(), LuaError> {
11 let input_table = lua.create_table()?;
12
13 input_table.set("_keys", lua.create_table()?)?;
15 input_table.set("_just_keys", lua.create_table()?)?;
16 input_table.set("_mouse_x", 0.0f32)?;
17 input_table.set("_mouse_y", 0.0f32)?;
18 input_table.set("_mouse_dx", 0.0f32)?;
19 input_table.set("_mouse_dy", 0.0f32)?;
20 input_table.set("_mouse_left", false)?;
21 input_table.set("_mouse_right", false)?;
22 input_table.set("_mouse_middle", false)?;
23
24 lua.globals().set("input", input_table)?;
25
26 lua.load(
28 r#"
29 -- Tuş adından KeyCode'a eşleme tablosu
30 local key_map = {
31 w = 17, a = 4, s = 22, d = 7,
32 q = 20, e = 8, r = 21, f = 9,
33 z = 29, x = 27, c = 6, v = 25,
34 space = 44, lshift = 225, rshift = 229,
35 lctrl = 224, rctrl = 228,
36 tab = 43, escape = 41, enter = 40,
37 up = 82, down = 81, left = 80, right = 79,
38 ["1"] = 30, ["2"] = 31, ["3"] = 32, ["4"] = 33,
39 ["5"] = 34, ["6"] = 35, ["7"] = 36, ["8"] = 37,
40 ["9"] = 38, ["0"] = 39,
41 i = 12, j = 13, k = 14, l = 15,
42 b = 5, n = 17, m = 16,
43 }
44
45 function input.is_pressed(key_name)
46 local code = key_map[string.lower(key_name)]
47 if code and input._keys[code] then
48 return true
49 end
50 return false
51 end
52
53 function input.is_just_pressed(key_name)
54 local code = key_map[string.lower(key_name)]
55 if code and input._just_keys[code] then
56 return true
57 end
58 return false
59 end
60
61 function input.mouse_position()
62 return { x = input._mouse_x, y = input._mouse_y }
63 end
64
65 function input.mouse_delta()
66 return { x = input._mouse_dx, y = input._mouse_dy }
67 end
68
69 function input.is_mouse_pressed(button)
70 if button == "left" then return input._mouse_left
71 elseif button == "right" then return input._mouse_right
72 elseif button == "middle" then return input._mouse_middle
73 end
74 return false
75 end
76 "#,
77 )
78 .exec()?;
79
80 Ok(())
81}
82
83pub fn update_input_api(lua: &Lua, input: &Input) -> Result<(), LuaError> {
85 let input_table: LuaTable = lua.globals().get("input")?;
86
87 let keys = lua.create_table()?;
89 let just_keys = lua.create_table()?;
90
91 for code in 0..256u32 {
93 if input.is_key_pressed(code) {
94 keys.set(code, true)?;
95 }
96 if input.is_key_just_pressed(code) {
97 just_keys.set(code, true)?;
98 }
99 }
100
101 input_table.set("_keys", keys)?;
102 input_table.set("_just_keys", just_keys)?;
103
104 let (mx, my) = input.mouse_position();
105 input_table.set("_mouse_x", mx)?;
106 input_table.set("_mouse_y", my)?;
107
108 let (dx, dy) = input.mouse_delta();
109 input_table.set("_mouse_dx", dx)?;
110 input_table.set("_mouse_dy", dy)?;
111
112 input_table.set(
113 "_mouse_left",
114 input.is_mouse_button_pressed(gizmo_core::input::mouse::LEFT),
115 )?;
116 input_table.set(
117 "_mouse_right",
118 input.is_mouse_button_pressed(gizmo_core::input::mouse::RIGHT),
119 )?;
120 input_table.set(
121 "_mouse_middle",
122 input.is_mouse_button_pressed(gizmo_core::input::mouse::MIDDLE),
123 )?;
124
125 Ok(())
126}