From 89cdeef9ad9c164e9f484cded3096bcbc72b7207 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Wed, 4 Apr 2012 18:52:34 +0100 Subject: [PATCH] CommandInterface, Mouse, Keyboard and Tick events, on screen log, print redirected to tpt.log --- src/cat/CommandInterface.cpp | 2 +- src/cat/CommandInterface.h | 9 +++++- src/cat/LuaScriptInterface.cpp | 47 +++++++++++++++++++++++++-- src/cat/LuaScriptInterface.h | 10 +++++- src/game/GameController.cpp | 31 ++++++++++++++++++ src/game/GameController.h | 8 +++++ src/game/GameModel.cpp | 21 ++++++++++++ src/game/GameModel.h | 5 +++ src/game/GameView.cpp | 58 ++++++++++++++++++++++++++++++++++ src/game/GameView.h | 14 ++++++++ 10 files changed, 200 insertions(+), 5 deletions(-) diff --git a/src/cat/CommandInterface.cpp b/src/cat/CommandInterface.cpp index f434f540d..4554b9f4f 100644 --- a/src/cat/CommandInterface.cpp +++ b/src/cat/CommandInterface.cpp @@ -33,7 +33,7 @@ std::string CommandInterface::FormatCommand(std::string command) void CommandInterface::Log(LogType type, std::string message) { - //Todo Put this info somewhere, an on-screen log output would be nice. + m->Log(message); } int CommandInterface::GetPropertyOffset(std::string key_, FormatType & format) diff --git a/src/cat/CommandInterface.h b/src/cat/CommandInterface.h index c77d83c51..8cabdab38 100644 --- a/src/cat/CommandInterface.h +++ b/src/cat/CommandInterface.h @@ -9,6 +9,7 @@ #define KITTY_H_ #include +#include //#include "game/GameModel.h" class GameModel; @@ -24,7 +25,13 @@ public: int GetParticleType(std::string type); void Log(LogType type, std::string message); //void AttachGameModel(GameModel * m); - virtual void Tick() {} + 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) {return true;} + virtual bool OnMouseWheel(int x, int y, int d) {return true;} + virtual bool OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) {return true;} + virtual bool OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) {return true;} + virtual void OnTick(float dt) {} virtual int Command(std::string command); virtual std::string FormatCommand(std::string command); std::string GetLastError(); diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index c7af15577..617577bd9 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -84,6 +84,12 @@ LuaScriptInterface::LuaScriptInterface(GameModel * m): l = lua_open(); luaL_openlibs(l); + + //Replace print function with our screen logging thingy + lua_pushcfunction(l, luatpt_log); + lua_setglobal(l, "print"); + + //Register all tpt functions luaL_register(l, "tpt", tptluaapi); tptProperties = lua_gettop(l); @@ -205,9 +211,46 @@ tpt.partsdata = nil"); } -void LuaScriptInterface::Tick() +bool LuaScriptInterface::OnMouseMove(int x, int y, int dx, int dy) { + luacon_mousex = x; + luacon_mousey = y; + return true; +} +bool LuaScriptInterface::OnMouseDown(int x, int y, unsigned button) +{ + luacon_mousedown = true; + luacon_mousebutton = button; + return luacon_mouseevent(x, y, button, LUACON_MDOWN); +} + +bool LuaScriptInterface::OnMouseUp(int x, int y, unsigned button) +{ + luacon_mousedown = false; + return luacon_mouseevent(x, y, button, LUACON_MUP); +} + +bool LuaScriptInterface::OnMouseWheel(int x, int y, int d) +{ + return true; +} + +bool LuaScriptInterface::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + return luacon_keyevent(key, /*TODO: sdl_mod*/0, LUACON_KDOWN); +} + +bool LuaScriptInterface::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + return luacon_keyevent(key, /*TODO: sdl_mod*/0, LUACON_KUP); +} + +void LuaScriptInterface::OnTick(float dt) +{ + if(luacon_mousedown) + luacon_mouseevent(luacon_mousex, luacon_mousey, luacon_mousebutton, LUACON_MPRESS); + luacon_step(luacon_mousex, luacon_mousey, luacon_selectedl, luacon_selectedr); } int LuaScriptInterface::Command(std::string command) @@ -753,7 +796,7 @@ int luacon_step(int mx, int my, int selectl, int selectr){ if (callret) { // failed, TODO: better error reporting - printf("%s\n",luacon_geterror()); + luacon_ci->Log(CommandInterface::LogError, luacon_geterror());//("%s\n",luacon_geterror()); } } } diff --git a/src/cat/LuaScriptInterface.h b/src/cat/LuaScriptInterface.h index 2fcfc33e6..b3ecd7a12 100644 --- a/src/cat/LuaScriptInterface.h +++ b/src/cat/LuaScriptInterface.h @@ -34,10 +34,18 @@ extern "C" #define LUACON_EL_MODIFIED_MENUS 0x4 class LuaScriptInterface: public CommandInterface { + int luacon_mousex, luacon_mousey, luacon_selectedl, luacon_selectedr, luacon_mousebutton; + bool luacon_mousedown; public: lua_State *l; LuaScriptInterface(GameModel * m); - virtual void Tick(); + virtual bool OnMouseMove(int x, int y, int dx, int dy); + virtual bool OnMouseDown(int x, int y, unsigned button); + virtual bool OnMouseUp(int x, int y, unsigned button); + virtual bool OnMouseWheel(int x, int y, int d); + virtual bool OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); + virtual bool OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); + virtual void OnTick(float dt); virtual int Command(std::string command); virtual std::string FormatCommand(std::string command); virtual ~LuaScriptInterface(); diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index 7b7ba20d3..b8f60f51a 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -303,8 +303,39 @@ void GameController::CopyRegion(ui::Point point1, ui::Point point2) gameModel->SetClipboard(saveData, saveSize); } +bool GameController::MouseMove(int x, int y, int dx, int dy) +{ + return commandInterface->OnMouseMove(x, y, dx, dy); +} + +bool GameController::MouseDown(int x, int y, unsigned button) +{ + return commandInterface->OnMouseDown(x, y, button); +} + +bool GameController::MouseUp(int x, int y, unsigned button) +{ + return commandInterface->OnMouseUp(x, y, button); +} + +bool GameController::MouseWheel(int x, int y, int d) +{ + return commandInterface->OnMouseWheel(x, y, d); +} + +bool GameController::KeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + return commandInterface->OnKeyPress(key, character, shift, ctrl, alt); +} + +bool GameController::KeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + return commandInterface->OnKeyRelease(key, character, shift, ctrl, alt); +} + void GameController::Update() { + commandInterface->OnTick(1.0f); gameModel->GetSimulation()->update_particles(); if(renderOptions && renderOptions->HasExited) { diff --git a/src/game/GameController.h b/src/game/GameController.h index cefca8eb9..3b5ff8874 100644 --- a/src/game/GameController.h +++ b/src/game/GameController.h @@ -47,6 +47,14 @@ public: GameController(); ~GameController(); GameView * GetView(); + + bool MouseMove(int x, int y, int dx, int dy); + bool MouseDown(int x, int y, unsigned button); + bool MouseUp(int x, int y, unsigned button); + bool MouseWheel(int x, int y, int d); + bool KeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); + bool KeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); + void SetZoomEnabled(bool zoomEnable); void SetZoomPosition(ui::Point position); void AdjustBrushSize(int direction); diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 2a9b69ea0..6a8f3d80e 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -457,6 +457,19 @@ void GameModel::SetStamp(Save * newStamp) notifyStampChanged(); } +void GameModel::Log(string message) +{ + consoleLog.push_front(message); + if(consoleLog.size()>100) + consoleLog.pop_back(); + notifyLogChanged(message); +} + +deque GameModel::GetLog() +{ + return consoleLog; +} + void GameModel::notifyColourSelectorColourChanged() { for(int i = 0; i < observers.size(); i++) @@ -576,3 +589,11 @@ void GameModel::notifyClipboardChanged() observers[i]->NotifyClipboardChanged(this); } } + +void GameModel::notifyLogChanged(string entry) +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyLogChanged(this, entry); + } +} diff --git a/src/game/GameModel.h b/src/game/GameModel.h index 7b953c98b..7dff6f249 100644 --- a/src/game/GameModel.h +++ b/src/game/GameModel.h @@ -2,6 +2,7 @@ #define GAMEMODEL_H #include +#include #include "search/Save.h" #include "simulation/Simulation.h" #include "interface/Colour.h" @@ -35,6 +36,7 @@ private: //unsigned char * clipboardData; Save * stamp; Save * clipboard; + deque consoleLog; vector observers; vector toolList; vector menuList; @@ -64,6 +66,7 @@ private: void notifyStampChanged(); void notifyColourSelectorColourChanged(); void notifyColourSelectorVisibilityChanged(); + void notifyLogChanged(string entry); public: GameModel(); ~GameModel(); @@ -110,6 +113,8 @@ public: void SetStamp(Save * newStamp); void AddStamp(unsigned char * saveData, int saveSize); void SetClipboard(unsigned char * saveData, int saveSize); + void Log(string message); + deque GetLog(); Save * GetClipboard(); Save * GetStamp(); }; diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index 4067d5a29..2ccf2e4b3 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -741,11 +741,55 @@ void GameView::OnTick(float dt) c->Update(); } +void GameView::DoMouseMove(int x, int y, int dx, int dy) +{ + if(c->MouseMove(x, y, dx, dy)) + Window::DoMouseMove(x, y, dx, dy); +} + +void GameView::DoMouseDown(int x, int y, unsigned button) +{ + if(c->MouseDown(x, y, button)) + Window::DoMouseDown(x, y, button); +} + +void GameView::DoMouseUp(int x, int y, unsigned button) +{ + if(c->MouseUp(x, y, button)) + Window::DoMouseUp(x, y, button); +} + +void GameView::DoMouseWheel(int x, int y, int d) +{ + if(c->MouseWheel(x, y, d)) + Window::DoMouseWheel(x, y, d); +} + +void GameView::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + if(c->KeyPress(key, character, shift, ctrl, alt)) + Window::DoKeyPress(key, character, shift, ctrl, alt); +} + +void GameView::DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + if(c->KeyRelease(key, character, shift, ctrl, alt)) + Window::DoKeyRelease(key, character, shift, ctrl, alt); +} + void GameView::NotifyZoomChanged(GameModel * sender) { zoomEnabled = sender->GetZoomEnabled(); } +void GameView::NotifyLogChanged(GameModel * sender, string entry) +{ + logEntries.push_front(entry); + lastLogEntry = 100.0f; + if(logEntries.size()>10) + logEntries.pop_back(); +} + void GameView::NotifyClipboardChanged(GameModel * sender) { if(clipboardThumb) @@ -853,5 +897,19 @@ void GameView::OnDraw() } } } + + int startX = 20; + int startY = YRES-20; + if(lastLogEntry>0.1 && logEntries.size()) + { + deque::iterator iter; + for(iter = logEntries.begin(); iter != logEntries.end(); iter++) + { + string message = (*iter); + startY -= 14; + g->fillrect(startX-3, startY-3, Graphics::textwidth((char*)message.c_str())+6, 14, 0, 0, 0, 100); + g->drawtext(startX, startY, message.c_str(), 255, 255, 255, 255); + } + } } } diff --git a/src/game/GameView.h b/src/game/GameView.h index 1be2d96ea..9658184e2 100644 --- a/src/game/GameView.h +++ b/src/game/GameView.h @@ -3,6 +3,8 @@ #include #include +#include +#include #include "GameController.h" #include "GameModel.h" #include "interface/Window.h" @@ -41,6 +43,8 @@ private: //UI Elements vector menuButtons; vector toolButtons; + deque logEntries; + float lastLogEntry; ui::Button * searchButton; ui::Button * reloadButton; ui::Button * saveSimulationButton; @@ -88,12 +92,22 @@ public: void NotifyColourSelectorColourChanged(GameModel * sender); void NotifyClipboardChanged(GameModel * sender); void NotifyStampChanged(GameModel * sender); + void NotifyLogChanged(GameModel * sender, string entry); virtual void OnMouseMove(int x, int y, int dx, int dy); virtual void OnMouseDown(int x, int y, unsigned button); virtual void OnMouseUp(int x, int y, unsigned button); virtual void OnMouseWheel(int x, int y, int d); virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); + + //Top-level handers, for Lua interface + 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 DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); + virtual void DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); + //virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) {} //virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) {} virtual void OnTick(float dt);