Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6281 for code/branches


Ignore:
Timestamp:
Dec 9, 2009, 12:43:12 PM (15 years ago)
Author:
rgrieder
Message:

Added LuaFunctor that can execute arbitrary lua code.
Also added LuaState::createLuaFunctor(std::string) and implemented that for the keybindings menu.

Location:
code/branches/presentation2
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2/data/gui/scripts/KeyBindMenu.lua

    r6268 r6281  
    8686   
    8787    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind)
    88 
    8988end
    9089
    9190function P.keybind()
     91    local funct = luaState:createLuaFunctor("InfoPopup:close()")
     92    orxonox.KeyBinderManager:getInstance():registerKeybindCallback(funct)
    9293    orxonox.KeyBinderManager:getInstance():keybind(commandList[commandNr])
    9394end
  • code/branches/presentation2/src/libraries/core/LuaState.cc

    r6190 r6281  
    263263        }
    264264    }
     265
     266
     267    LuaFunctor::LuaFunctor(const std::string& code, LuaState* luaState)
     268    {
     269        this->code_ = code;
     270        this->lua_ = luaState;
     271    }
     272
     273    void LuaFunctor::operator()(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
     274    {
     275        lua_->doString(this->code_);
     276    }
    265277}
  • code/branches/presentation2/src/libraries/core/LuaState.h

    r6150 r6281  
    4040
    4141#include "util/ScopeGuard.h"
     42#include "core/Functor.h"
    4243#include "ToluaInterface.h"
    4344
    44 // tolua_begin
    45 namespace orxonox
    46 {
     45namespace orxonox // tolua_export
     46{ // tolua_export
     47    class Functor; // tolua_export
     48
     49    //! Functor subclass that simply executes code with 0 arguments.
     50    class _CoreExport LuaFunctor : public Functor
     51    {
     52        public:
     53            LuaFunctor(const std::string& code, LuaState* luaState);
     54            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);
     55            void evaluateParam(unsigned int index, MultiType& param) const {}
     56
     57        private:
     58            std::string code_;
     59            LuaState*   lua_;
     60    };
     61
     62
    4763    /**
    4864    @brief
    4965        Representation of an interface to lua
    5066    */
    51     class _CoreExport LuaState
    52     {
    53 // tolua_end
     67    class _CoreExport LuaState // tolua_export
     68    { // tolua_export
    5469    public:
    5570        LuaState();
     
    7489        void setDefaultResourceInfo(const shared_ptr<ResourceInfo>& sourceFileInfo) { this->sourceFileInfo_ = sourceFileInfo; }
    7590        const shared_ptr<ResourceInfo>& getDefaultResourceInfo() { return this->sourceFileInfo_; }
     91
     92        Functor* createLuaFunctor(const std::string& code) { return new LuaFunctor(code, this); } // tolua_export
    7693
    7794        static bool addToluaInterface(int (*function)(lua_State*), const std::string& name);
  • code/branches/presentation2/src/libraries/core/input/KeyBinderManager.cc

    r6266 r6281  
    2929#include "KeyBinderManager.h"
    3030
     31#include <CEGUIWindow.h>
     32
    3133#include "util/Debug.h"
    3234#include "util/Exception.h"
     
    3739#include "InputManager.h"
    3840#include "KeyDetector.h"
    39 
    40 #include <CEGUIWindow.h>
    4141
    4242namespace orxonox
     
    4949        , bBinding_(false)
    5050    {
    51         this->callbackFunction_ = createFunctor(&KeyBinderManager::callback, this);
    52 
    5351        RegisterObject(KeyBinderManager);
    5452        this->setConfigValues();
     
    6967        for (std::map<std::string, KeyBinder*>::const_iterator it = this->binders_.begin(); it != this->binders_.end(); ++it)
    7068            delete it->second;
    71         delete this->callbackFunction_;
    7269    }
    7370
     
    152149        {
    153150            COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
    154             KeyDetector::getInstance().setCallback(callbackFunction_);
     151            KeyDetector::getInstance().setCallback(shared_ptr<Functor>(createFunctor(&KeyBinderManager::keybindKeyPressed, this)));
    155152            InputManager::getInstance().enterState("detector");
    156153            this->command_ = command;
     
    162159
    163160    // Gets called by the KeyDetector (registered with a Functor)
    164     void KeyBinderManager::callback(const std::string& keyName)
     161    void KeyBinderManager::keybindKeyPressed(const std::string& keyName)
    165162    {
    166163        if (this->bBinding_)
     
    169166            this->currentBinder_->setBinding(command_, keyName, bTemporary_);
    170167            InputManager::getInstance().leaveState("detector");
     168            // inform whatever was calling the command
     169            if (this->callbackFunction_)
     170                (*this->callbackFunction_)();
    171171            this->bBinding_ = false;
    172172        }
  • code/branches/presentation2/src/libraries/core/input/KeyBinderManager.h

    r6280 r6281  
    3434#include <map>
    3535#include <string>
     36#include <boost/shared_ptr.hpp>
     37#include <CEGUIForwardRefs.h>
     38
    3639#include "util/Singleton.h"
    3740#include "core/OrxonoxClass.h"
    38 
    39 #include <CEGUIForwardRefs.h>
    4041
    4142namespace orxonox //tolua_export
     
    99100        inline void tkeybind(const std::string& command)
    100101            { this->keybindInternal(command, true); }
     102        inline void registerKeybindCallback(Functor* function) { this->callbackFunction_.reset(function); } // tolua_export
    101103
    102104    private:
    103105        KeyBinderManager(const KeyBinderManager&);
    104106        void keybindInternal(const std::string& command, bool bTemporary);
    105         void callback(const std::string& keyName);
     107        void keybindKeyPressed(const std::string& keyName);
    106108        void defaultFilenameChanged();
    107109
     
    113115
    114116        // keybind command related
    115         Functor* callbackFunction_;                  //! Function to be called when key was pressed after "keybind" command
     117        shared_ptr<Functor> callbackFunction_;       //! Function to be called when key was pressed after "keybind" command
    116118        bool bBinding_;                              //! Tells whether a key binding process is active
    117119        bool bTemporary_;                            //! Stores tkeybind/keybind value
  • code/branches/presentation2/src/libraries/core/input/KeyDetector.h

    r5929 r6281  
    3232#include "InputPrereqs.h"
    3333
     34#include <boost/shared_ptr.hpp>
    3435#include "util/Singleton.h"
    3536#include "KeyBinder.h"
     
    4546        ~KeyDetector();
    4647
    47         void setCallback(Functor* function) { this->callbackFunction_ = function; }
     48        void setCallback(const shared_ptr<Functor>& function) { this->callbackFunction_ = function; }
    4849
    4950    private:
     
    5455        void assignCommands();
    5556
    56         Functor* callbackFunction_;
     57        shared_ptr<Functor> callbackFunction_;
    5758        InputState* inputState_;
    5859        static std::string callbackCommand_s;
Note: See TracChangeset for help on using the changeset viewer.