-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLimeReceiver.h
95 lines (77 loc) · 2.49 KB
/
LimeReceiver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
#include <irrlicht.h>
#include <sol/sol.hpp>
#include "LuaLime.h"
#include "Vector2D.h"
class ButtonCallbackPairClick {
public:
ButtonCallbackPairClick(irr::gui::IGUIButton* b, sol::function f) : button(b), callback(f) {
}
irr::gui::IGUIButton* button;
sol::function callback;
};
class ButtonCallbackPairHover {
public:
ButtonCallbackPairHover(irr::gui::IGUIButton* b, sol::function f) : button(b), hover(f) {
}
irr::gui::IGUIButton* button;
sol::function hover;
};
using namespace irr;
class LimeReceiver : public IEventReceiver
{
public:
std::vector<ButtonCallbackPairClick> buttonCallbackClick;
std::vector<ButtonCallbackPairHover> buttonCallbackHover;
void removeImg(irr::gui::IGUIButton* b);
struct SMouseState
{
core::position2di Position;
bool LeftButtonDown;
bool RightButtonDown;
bool MiddleButtonDown;
float WheelDelta;
SMouseState()
: LeftButtonDown(false), RightButtonDown(false), MiddleButtonDown(false), WheelDelta(0.0f)
{ }
} MouseState;
struct SControllerState
{
f32 Axis[SEvent::SJoystickEvent::NUMBER_OF_AXES];
u32 Buttons;
SControllerState()
{
std::fill(std::begin(Axis), std::end(Axis), 0.0f);
Buttons = 0;
}
bool isButtonPressed(u32 buttonIndex) const
{
return Buttons & (1 << buttonIndex);
}
} ControllerState;
LimeReceiver();
virtual bool OnEvent(const SEvent& event) override;
sol::table getMouseState() const;
sol::table getControllerState() const;
// Check if a key is currently pressed
bool isKeyDown(irr::EKEY_CODE keyCode) const;
irr::gui::IGUIButton* lastFocused = nullptr;
private:
std::array<bool, KEY_KEY_CODES_COUNT> keys;
std::array<bool, KEY_KEY_CODES_COUNT> keysRepeat;
SEvent::SJoystickEvent JoystickState;
template<typename... Args>
void callLuaFunction(const std::string& tableName, const std::string& functionName, Args&&... args)
{
sol::optional<sol::function> func = (*lua)[tableName][functionName];
if (func && (*lua)[tableName][functionName].get_type() == sol::type::function)
{
try {
func.value()(std::forward<Args>(args)...);
}
catch (const sol::error& e) {
std::cerr << "Error calling " << tableName << "." << functionName << ": " << e.what() << std::endl;
}
}
}
};