Skip to content

Commit

Permalink
add new events api, replaces legacy event functions
Browse files Browse the repository at this point in the history
event.register(event.mousedown, function(...) print(...) end)
event.unregister(event.mousedown, somefunc)

mouseclick event split into mousedown, mouseup, mousemove, mousewheel
keypress event split into keypress, keyrelease, textinput. key* events only contain keycode and scancode, don't attempt to represent a letter (was very broken at this before). Also have helpful shift/ctrl/alt flags passed in. textinput just represents inserted text, can probably even handle foreign characters.
register_step replaced with event.tick event

All legacy register_* and unregister_ functions are removed. There is a compatibility lua script, might embed it later. tpt.set_shortcuts / tpt.test also removed. event.getmodifiers added, just a misc function to get the currently held modifiers

Lots of code duplication to handle each event is removed, it's not handled in a more generic way. Although the Event class / child classes could use some work.
  • Loading branch information
jacob1 committed Nov 16, 2018
1 parent fa0809d commit a8489ba
Show file tree
Hide file tree
Showing 13 changed files with 490 additions and 552 deletions.
33 changes: 12 additions & 21 deletions src/gui/game/GameController.cpp
Expand Up @@ -37,6 +37,7 @@
#else
#include "lua/TPTScriptInterface.h"
#endif
#include "lua/LuaEvents.h"

using namespace std;

Expand Down Expand Up @@ -157,11 +158,6 @@ GameController::GameController():
commandInterface = new TPTScriptInterface(this, gameModel);
#endif

ActiveToolChanged(0, gameModel->GetActiveTool(0));
ActiveToolChanged(1, gameModel->GetActiveTool(1));
ActiveToolChanged(2, gameModel->GetActiveTool(2));
ActiveToolChanged(3, gameModel->GetActiveTool(3));

Client::Ref().AddListener(this);

debugInfo.push_back(new DebugParts(0x1, gameModel->GetSimulation()));
Expand Down Expand Up @@ -626,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->OnMouseMove(x, y, dx, dy);
return commandInterface->HandleEvent(EventTypes::mousemove, new MouseMoveEvent(x, y, dx, dy));
}

bool GameController::MouseDown(int x, int y, unsigned button)
{
bool ret = commandInterface->OnMouseDown(x, y, button);
bool ret = commandInterface->HandleEvent(EventTypes::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 @@ -653,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->OnMouseUp(x, y, button, type);
bool ret = commandInterface->HandleEvent(EventTypes::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 @@ -711,12 +707,17 @@ bool GameController::MouseUp(int x, int y, unsigned button, char type)

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

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

bool GameController::KeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
bool ret = commandInterface->OnKeyPress(key, scan, repeat, shift, ctrl, alt);
bool ret = commandInterface->HandleEvent(EventTypes::keypress, new KeyEvent(key, scan, repeat, shift, ctrl, alt));
if (repeat)
return ret;
if (ret)
Expand Down Expand Up @@ -795,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->OnKeyRelease(key, scan, repeat, shift, ctrl, alt);
bool ret = commandInterface->HandleEvent(EventTypes::keyrelease, new KeyEvent(key, scan, repeat, shift, ctrl, alt));
if (repeat)
return ret;
if (ret)
Expand Down Expand Up @@ -832,11 +833,6 @@ bool GameController::KeyRelease(int key, int scan, bool repeat, bool shift, bool
return ret;
}

bool GameController::MouseTick()
{
return commandInterface->OnMouseTick();
}

void GameController::Tick()
{
if(firstTick)
Expand Down Expand Up @@ -1150,11 +1146,6 @@ void GameController::RebuildFavoritesMenu()
gameModel->BuildFavoritesMenu();
}

void GameController::ActiveToolChanged(int toolSelection, Tool *tool)
{
commandInterface->OnActiveToolChanged(toolSelection, tool);
}

Tool * GameController::GetActiveTool(int selection)
{
return gameModel->GetActiveTool(selection);
Expand Down
3 changes: 1 addition & 2 deletions src/gui/game/GameController.h
Expand Up @@ -64,9 +64,9 @@ class GameController: public ClientListener
bool MouseDown(int x, int y, unsigned button);
bool MouseUp(int x, int y, unsigned button, char type);
bool MouseWheel(int x, int y, int d);
bool TextInput(String text);
bool KeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
bool KeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
bool MouseTick();
void Tick();
void Exit();

Expand Down Expand Up @@ -113,7 +113,6 @@ class GameController: public ClientListener
void SetLastTool(Tool * tool);
int GetReplaceModeFlags();
void SetReplaceModeFlags(int flags);
void ActiveToolChanged(int toolSelection, Tool *tool);
void SetActiveColourPreset(int preset);
void SetColour(ui::Colour colour);
void SetToolStrength(float value);
Expand Down
24 changes: 7 additions & 17 deletions src/gui/game/GameView.cpp
Expand Up @@ -709,8 +709,7 @@ void GameView::NotifyActiveToolsChanged(GameModel * sender)
toolButtons[i]->SetSelectionState(-1);
}
}
//need to do this for all tools every time just in case it wasn't caught if you weren't in the menu a tool was changed to
c->ActiveToolChanged(0, sender->GetActiveTool(0));

if (sender->GetRenderer()->findingElement)
{
Tool *active = sender->GetActiveTool(0);
Expand All @@ -719,9 +718,6 @@ void GameView::NotifyActiveToolsChanged(GameModel * sender)
else
ren->findingElement = sender->GetActiveTool(0)->GetToolID()%256;
}
c->ActiveToolChanged(1, sender->GetActiveTool(1));
c->ActiveToolChanged(2, sender->GetActiveTool(2));
c->ActiveToolChanged(3, sender->GetActiveTool(3));
}

void GameView::NotifyLastToolChanged(GameModel * sender)
Expand Down Expand Up @@ -1841,6 +1837,12 @@ void GameView::DoMouseWheel(int x, int y, int d)
Window::DoMouseWheel(x, y, d);
}

void GameView::DoTextInput(String text)
{
if (c->TextInput(text))
Window::DoTextInput(text);
}

void GameView::DoKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt)
{
if (c->KeyPress(key, scan, repeat, shift, ctrl, alt))
Expand All @@ -1853,18 +1855,6 @@ void GameView::DoKeyRelease(int key, int scan, bool repeat, bool shift, bool ctr
Window::DoKeyRelease(key, scan, repeat, shift, ctrl, alt);
}

void GameView::DoTick(float dt)
{
//mouse events trigger every frame when mouse is held down, needs to happen here (before things are drawn) so it can clear the point queue if false is returned from a lua mouse event
if (!c->MouseTick())
{
isMouseDown = false;
selectMode = SelectNone;
drawMode = DrawPoints;
}
Window::DoTick(dt);
}

void GameView::DoDraw()
{
Window::DoDraw();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/game/GameView.h
Expand Up @@ -198,12 +198,12 @@ class GameView: public ui::Window
virtual void OnBlur();

//Top-level handlers, for Lua interface
virtual void DoTick(float dt);
virtual void DoDraw();
virtual void DoMouseMove(int x, int y, int dx, int dy);
virtual void DoMouseDown(int x, int y, unsigned button);
virtual void DoMouseUp(int x, int y, unsigned button);
virtual void DoMouseWheel(int x, int y, int d);
virtual void DoTextInput(String text);
virtual void DoKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
virtual void DoKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);

Expand Down
19 changes: 9 additions & 10 deletions src/lua/CommandInterface.h
Expand Up @@ -2,13 +2,16 @@
#define COMMANDINTERFACE_H_

#include "common/String.h"
#include "lua/LuaEvents.h"
#include "gui/interface/Engine.h"
//#include "game/GameModel.h"

class Event;
class GameModel;
class GameController;
class Tool;
class CommandInterface {

class CommandInterface
{
protected:
String lastError;
GameModel * m;
Expand All @@ -20,15 +23,11 @@ class CommandInterface {
int GetPropertyOffset(ByteString key, FormatType & format);
void Log(LogType type, String message);
//void AttachGameModel(GameModel * m);
virtual bool OnActiveToolChanged(int toolSelection, Tool * tool) {return true;}
virtual bool OnMouseMove(int x, int y, int dx, int dy) {return true;}
virtual bool OnMouseDown(int x, int y, unsigned button) {return true;}
virtual bool OnMouseUp(int x, int y, unsigned button, char type) {return true;}
virtual bool OnMouseWheel(int x, int y, int d) {return true;}
virtual bool OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) {return true;}
virtual bool OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) {return true;}
virtual bool OnMouseTick() { return true; }

virtual void OnTick() { }

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

virtual int Command(String command);
virtual String FormatCommand(String command);
String GetLastError();
Expand Down

0 comments on commit a8489ba

Please sign in to comment.