[Orxonox-commit 1563] r6281 - in code/branches/presentation2: data/gui/scripts src/libraries/core src/libraries/core/input

rgrieder at orxonox.net rgrieder at orxonox.net
Wed Dec 9 12:43:12 CET 2009


Author: rgrieder
Date: 2009-12-09 12:43:12 +0100 (Wed, 09 Dec 2009)
New Revision: 6281

Modified:
   code/branches/presentation2/data/gui/scripts/KeyBindMenu.lua
   code/branches/presentation2/src/libraries/core/LuaState.cc
   code/branches/presentation2/src/libraries/core/LuaState.h
   code/branches/presentation2/src/libraries/core/input/KeyBinderManager.cc
   code/branches/presentation2/src/libraries/core/input/KeyBinderManager.h
   code/branches/presentation2/src/libraries/core/input/KeyDetector.h
Log:
Added LuaFunctor that can execute arbitrary lua code.
Also added LuaState::createLuaFunctor(std::string) and implemented that for the keybindings menu.

Modified: code/branches/presentation2/data/gui/scripts/KeyBindMenu.lua
===================================================================
--- code/branches/presentation2/data/gui/scripts/KeyBindMenu.lua	2009-12-09 10:06:48 UTC (rev 6280)
+++ code/branches/presentation2/data/gui/scripts/KeyBindMenu.lua	2009-12-09 11:43:12 UTC (rev 6281)
@@ -85,10 +85,11 @@
     commandNr = tonumber(string.match(name, "%d+"))
     
     openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind)
-
 end
 
 function P.keybind()
+    local funct = luaState:createLuaFunctor("InfoPopup:close()")
+    orxonox.KeyBinderManager:getInstance():registerKeybindCallback(funct)
     orxonox.KeyBinderManager:getInstance():keybind(commandList[commandNr])
 end
 

Modified: code/branches/presentation2/src/libraries/core/LuaState.cc
===================================================================
--- code/branches/presentation2/src/libraries/core/LuaState.cc	2009-12-09 10:06:48 UTC (rev 6280)
+++ code/branches/presentation2/src/libraries/core/LuaState.cc	2009-12-09 11:43:12 UTC (rev 6281)
@@ -262,4 +262,16 @@
             lua_setglobal(state, it->first.c_str());
         }
     }
+
+
+    LuaFunctor::LuaFunctor(const std::string& code, LuaState* luaState)
+    {
+        this->code_ = code;
+        this->lua_ = luaState;
+    }
+
+    void LuaFunctor::operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
+    {
+        lua_->doString(this->code_);
+    }
 }

Modified: code/branches/presentation2/src/libraries/core/LuaState.h
===================================================================
--- code/branches/presentation2/src/libraries/core/LuaState.h	2009-12-09 10:06:48 UTC (rev 6280)
+++ code/branches/presentation2/src/libraries/core/LuaState.h	2009-12-09 11:43:12 UTC (rev 6281)
@@ -39,18 +39,33 @@
 #include <boost/shared_ptr.hpp>
 
 #include "util/ScopeGuard.h"
+#include "core/Functor.h"
 #include "ToluaInterface.h"
 
-// tolua_begin
-namespace orxonox
-{
+namespace orxonox // tolua_export
+{ // tolua_export
+    class Functor; // tolua_export
+
+    //! Functor subclass that simply executes code with 0 arguments.
+    class _CoreExport LuaFunctor : public Functor
+    {
+        public:
+            LuaFunctor(const std::string& code, LuaState* luaState);
+            void operator()(const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null);
+            void evaluateParam(unsigned int index, MultiType& param) const {}
+
+        private:
+            std::string code_;
+            LuaState*   lua_;
+    };
+
+
     /**
     @brief
         Representation of an interface to lua
     */
-    class _CoreExport LuaState
-    {
-// tolua_end
+    class _CoreExport LuaState // tolua_export
+    { // tolua_export
     public:
         LuaState();
         ~LuaState();
@@ -74,6 +89,8 @@
         void setDefaultResourceInfo(const shared_ptr<ResourceInfo>& sourceFileInfo) { this->sourceFileInfo_ = sourceFileInfo; }
         const shared_ptr<ResourceInfo>& getDefaultResourceInfo() { return this->sourceFileInfo_; }
 
+        Functor* createLuaFunctor(const std::string& code) { return new LuaFunctor(code, this); } // tolua_export
+
         static bool addToluaInterface(int (*function)(lua_State*), const std::string& name);
         static bool removeToluaInterface(const std::string& name);
         static void openToluaInterfaces(lua_State* state);

Modified: code/branches/presentation2/src/libraries/core/input/KeyBinderManager.cc
===================================================================
--- code/branches/presentation2/src/libraries/core/input/KeyBinderManager.cc	2009-12-09 10:06:48 UTC (rev 6280)
+++ code/branches/presentation2/src/libraries/core/input/KeyBinderManager.cc	2009-12-09 11:43:12 UTC (rev 6281)
@@ -28,6 +28,8 @@
 
 #include "KeyBinderManager.h"
 
+#include <CEGUIWindow.h>
+
 #include "util/Debug.h"
 #include "util/Exception.h"
 #include "core/ConfigValueIncludes.h"
@@ -37,8 +39,6 @@
 #include "InputManager.h"
 #include "KeyDetector.h"
 
-#include <CEGUIWindow.h>
-
 namespace orxonox
 {
     ManageScopedSingleton(KeyBinderManager, ScopeID::Graphics, false);
@@ -48,8 +48,6 @@
         , bDefaultFileLoaded_(true)
         , bBinding_(false)
     {
-        this->callbackFunction_ = createFunctor(&KeyBinderManager::callback, this);
-
         RegisterObject(KeyBinderManager);
         this->setConfigValues();
 
@@ -68,7 +66,6 @@
         // Delete all remaining KeyBinders
         for (std::map<std::string, KeyBinder*>::const_iterator it = this->binders_.begin(); it != this->binders_.end(); ++it)
             delete it->second;
-        delete this->callbackFunction_;
     }
 
     void KeyBinderManager::setConfigValues()
@@ -151,7 +148,7 @@
         if (!this->bBinding_)
         {
             COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
-            KeyDetector::getInstance().setCallback(callbackFunction_);
+            KeyDetector::getInstance().setCallback(shared_ptr<Functor>(createFunctor(&KeyBinderManager::keybindKeyPressed, this)));
             InputManager::getInstance().enterState("detector");
             this->command_ = command;
             this->bTemporary_ = bTemporary;
@@ -161,13 +158,16 @@
     }
 
     // Gets called by the KeyDetector (registered with a Functor)
-    void KeyBinderManager::callback(const std::string& keyName)
+    void KeyBinderManager::keybindKeyPressed(const std::string& keyName)
     {
         if (this->bBinding_)
         {
             COUT(0) << "Binding string \"" << command_ << "\" on key '" << keyName << "'" << std::endl;
             this->currentBinder_->setBinding(command_, keyName, bTemporary_);
             InputManager::getInstance().leaveState("detector");
+            // inform whatever was calling the command
+            if (this->callbackFunction_)
+                (*this->callbackFunction_)();
             this->bBinding_ = false;
         }
         // else: A key was probably pressed within the same tick, ignore it.

Modified: code/branches/presentation2/src/libraries/core/input/KeyBinderManager.h
===================================================================
--- code/branches/presentation2/src/libraries/core/input/KeyBinderManager.h	2009-12-09 10:06:48 UTC (rev 6280)
+++ code/branches/presentation2/src/libraries/core/input/KeyBinderManager.h	2009-12-09 11:43:12 UTC (rev 6281)
@@ -33,11 +33,12 @@
 
 #include <map>
 #include <string>
+#include <boost/shared_ptr.hpp>
+#include <CEGUIForwardRefs.h>
+
 #include "util/Singleton.h"
 #include "core/OrxonoxClass.h"
 
-#include <CEGUIForwardRefs.h>
-
 namespace orxonox //tolua_export
 { //tolua_export
     /**
@@ -98,11 +99,12 @@
         //! Bind 'command' to any key pressed after this call (use with care!), but temporarily (no file save)
         inline void tkeybind(const std::string& command)
             { this->keybindInternal(command, true); }
+        inline void registerKeybindCallback(Functor* function) { this->callbackFunction_.reset(function); } // tolua_export
 
     private:
         KeyBinderManager(const KeyBinderManager&);
         void keybindInternal(const std::string& command, bool bTemporary);
-        void callback(const std::string& keyName);
+        void keybindKeyPressed(const std::string& keyName);
         void defaultFilenameChanged();
 
         // KeyBinder management
@@ -112,7 +114,7 @@
         std::string defaultFilename_;                //! Name of the file with the default key bindings
 
         // keybind command related
-        Functor* callbackFunction_;                  //! Function to be called when key was pressed after "keybind" command
+        shared_ptr<Functor> callbackFunction_;       //! Function to be called when key was pressed after "keybind" command
         bool bBinding_;                              //! Tells whether a key binding process is active
         bool bTemporary_;                            //! Stores tkeybind/keybind value
         std::string command_;                        //! Stores the command received by (t)keybind

Modified: code/branches/presentation2/src/libraries/core/input/KeyDetector.h
===================================================================
--- code/branches/presentation2/src/libraries/core/input/KeyDetector.h	2009-12-09 10:06:48 UTC (rev 6280)
+++ code/branches/presentation2/src/libraries/core/input/KeyDetector.h	2009-12-09 11:43:12 UTC (rev 6281)
@@ -31,6 +31,7 @@
 
 #include "InputPrereqs.h"
 
+#include <boost/shared_ptr.hpp>
 #include "util/Singleton.h"
 #include "KeyBinder.h"
 
@@ -44,7 +45,7 @@
         KeyDetector();
         ~KeyDetector();
 
-        void setCallback(Functor* function) { this->callbackFunction_ = function; }
+        void setCallback(const shared_ptr<Functor>& function) { this->callbackFunction_ = function; }
 
     private:
         KeyDetector(const KeyDetector&);
@@ -53,7 +54,7 @@
         void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList);
         void assignCommands();
 
-        Functor* callbackFunction_;
+        shared_ptr<Functor> callbackFunction_;
         InputState* inputState_;
         static std::string callbackCommand_s;
         static KeyDetector* singletonPtr_s;




More information about the Orxonox-commit mailing list