Skip to content

Commit

Permalink
Move EventTypes inside LuaEvents to prevent global scope pollution
Browse files Browse the repository at this point in the history
  • Loading branch information
LBPHacker authored and jacob1 committed Nov 17, 2018
1 parent a8489ba commit 531229d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
14 changes: 7 additions & 7 deletions src/gui/game/GameController.cpp
Expand Up @@ -622,12 +622,12 @@ void GameController::CutRegion(ui::Point point1, ui::Point point2, bool includeP

bool GameController::MouseMove(int x, int y, int dx, int dy)
{
return commandInterface->HandleEvent(EventTypes::mousemove, new MouseMoveEvent(x, y, dx, dy));
return commandInterface->HandleEvent(LuaEvents::mousemove, new MouseMoveEvent(x, y, dx, dy));
}

bool GameController::MouseDown(int x, int y, unsigned button)
{
bool ret = commandInterface->HandleEvent(EventTypes::mousedown, new MouseDownEvent(x, y, button));
bool ret = commandInterface->HandleEvent(LuaEvents::mousedown, new MouseDownEvent(x, y, button));
if (ret && y<YRES && x<XRES && !gameView->GetPlacingSave() && !gameView->GetPlacingZoom())
{
ui::Point point = gameModel->AdjustZoomCoords(ui::Point(x, y));
Expand All @@ -649,7 +649,7 @@ bool GameController::MouseDown(int x, int y, unsigned button)

bool GameController::MouseUp(int x, int y, unsigned button, char type)
{
bool ret = commandInterface->HandleEvent(EventTypes::mouseup, new MouseUpEvent(x, y, button, type));
bool ret = commandInterface->HandleEvent(LuaEvents::mouseup, new MouseUpEvent(x, y, button, type));
if (type)
return ret;
if (ret && foundSignID != -1 && y<YRES && x<XRES && !gameView->GetPlacingSave())
Expand Down Expand Up @@ -707,17 +707,17 @@ bool GameController::MouseUp(int x, int y, unsigned button, char type)

bool GameController::MouseWheel(int x, int y, int d)
{
return commandInterface->HandleEvent(EventTypes::mousewheel, new MouseWheelEvent(x, y, d));
return commandInterface->HandleEvent(LuaEvents::mousewheel, new MouseWheelEvent(x, y, d));
}

bool GameController::TextInput(String text)
{
return commandInterface->HandleEvent(EventTypes::textinput, new TextInputEvent(text));
return commandInterface->HandleEvent(LuaEvents::textinput, new TextInputEvent(text));
}

bool GameController::KeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
bool ret = commandInterface->HandleEvent(EventTypes::keypress, new KeyEvent(key, scan, repeat, shift, ctrl, alt));
bool ret = commandInterface->HandleEvent(LuaEvents::keypress, new KeyEvent(key, scan, repeat, shift, ctrl, alt));
if (repeat)
return ret;
if (ret)
Expand Down Expand Up @@ -796,7 +796,7 @@ bool GameController::KeyPress(int key, int scan, bool repeat, bool shift, bool c

bool GameController::KeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
bool ret = commandInterface->HandleEvent(EventTypes::keyrelease, new KeyEvent(key, scan, repeat, shift, ctrl, alt));
bool ret = commandInterface->HandleEvent(LuaEvents::keyrelease, new KeyEvent(key, scan, repeat, shift, ctrl, alt));
if (repeat)
return ret;
if (ret)
Expand Down
2 changes: 1 addition & 1 deletion src/lua/CommandInterface.h
Expand Up @@ -26,7 +26,7 @@ class CommandInterface

virtual void OnTick() { }

virtual bool HandleEvent(EventTypes eventType, Event * event) { return true; }
virtual bool HandleEvent(LuaEvents::EventTypes eventType, Event * event) { return true; }

virtual int Command(String command);
virtual String FormatCommand(String command);
Expand Down
22 changes: 11 additions & 11 deletions src/lua/LuaEvents.h
Expand Up @@ -98,20 +98,20 @@ class TickEvent: public Event
int PushToStack(lua_State *l) override { return 0; }
};

enum EventTypes {
keypress,
keyrelease,
textinput,
mousedown,
mouseup,
mousemove,
mousewheel,
tick
};

class LuaEvents
{
public:
enum EventTypes {
keypress,
keyrelease,
textinput,
mousedown,
mouseup,
mousemove,
mousewheel,
tick
};

static int RegisterEventHook(lua_State* l, ByteString eventName);
static int UnregisterEventHook(lua_State* l, ByteString eventName);
static bool HandleEvent(LuaScriptInterface * luacon_ci, Event * event, ByteString eventName);
Expand Down
20 changes: 10 additions & 10 deletions src/lua/LuaScriptInterface.cpp
Expand Up @@ -3303,14 +3303,14 @@ void LuaScriptInterface::initEventAPI()
lua_getglobal(l, "event");
lua_setglobal(l, "evt");

lua_pushinteger(l, EventTypes::keypress); lua_setfield(l, -2, "keypress");
lua_pushinteger(l, EventTypes::keyrelease); lua_setfield(l, -2, "keyrelease");
lua_pushinteger(l, EventTypes::textinput); lua_setfield(l, -2, "textinput");
lua_pushinteger(l, EventTypes::mousedown); lua_setfield(l, -2, "mousedown");
lua_pushinteger(l, EventTypes::mouseup); lua_setfield(l, -2, "mouseup");
lua_pushinteger(l, EventTypes::mousemove); lua_setfield(l, -2, "mousemove");
lua_pushinteger(l, EventTypes::mousewheel); lua_setfield(l, -2, "mousewheel");
lua_pushinteger(l, EventTypes::tick); lua_setfield(l, -2, "tick");
lua_pushinteger(l, LuaEvents::keypress); lua_setfield(l, -2, "keypress");
lua_pushinteger(l, LuaEvents::keyrelease); lua_setfield(l, -2, "keyrelease");
lua_pushinteger(l, LuaEvents::textinput); lua_setfield(l, -2, "textinput");
lua_pushinteger(l, LuaEvents::mousedown); lua_setfield(l, -2, "mousedown");
lua_pushinteger(l, LuaEvents::mouseup); lua_setfield(l, -2, "mouseup");
lua_pushinteger(l, LuaEvents::mousemove); lua_setfield(l, -2, "mousemove");
lua_pushinteger(l, LuaEvents::mousewheel); lua_setfield(l, -2, "mousewheel");
lua_pushinteger(l, LuaEvents::tick); lua_setfield(l, -2, "tick");
}

int LuaScriptInterface::event_register(lua_State * l)
Expand All @@ -3335,7 +3335,7 @@ int LuaScriptInterface::event_getmodifiers(lua_State * l)
return 1;
}

bool LuaScriptInterface::HandleEvent(EventTypes eventType, Event * event)
bool LuaScriptInterface::HandleEvent(LuaEvents::EventTypes eventType, Event * event)
{
return LuaEvents::HandleEvent(this, event, ByteString::Build("tptevents-", eventType));
}
Expand All @@ -3349,7 +3349,7 @@ void LuaScriptInterface::OnTick()
lua_setfield(l, -2, "NUM_PARTS");
}
lua_pop(l, 1);
HandleEvent(EventTypes::tick, new TickEvent());
HandleEvent(LuaEvents::tick, new TickEvent());
}

int LuaScriptInterface::Command(String command)
Expand Down
2 changes: 1 addition & 1 deletion src/lua/LuaScriptInterface.h
Expand Up @@ -186,7 +186,7 @@ class LuaScriptInterface: public CommandInterface
LuaScriptInterface(GameController * c, GameModel * m);

virtual void OnTick();
virtual bool HandleEvent(EventTypes eventType, Event * event);
virtual bool HandleEvent(LuaEvents::EventTypes eventType, Event * event);

virtual void Init();
virtual void SetWindow(ui::Window * window);
Expand Down

0 comments on commit 531229d

Please sign in to comment.