Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 19, 2009, 12:19:11 AM (15 years ago)
Author:
rgrieder
Message:

Cleaned out the lua script files for the GUI.
Also replaced "require" function to support resources.
Fixed a problem with the return value of doFile, includeFile and require being discarded because the tolua binding is for a C++ function returning void.

Location:
code/branches/resource2/src/core
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/resource2/src/core/GUIManager.cc

    r5658 r5661  
    2727 *
    2828 */
    29 
    30 /**
    31 @file
    32 @brief
    33     Implementation of the GUIManager class.
    34 */
    3529
    3630#include "GUIManager.h"
     
    6054#include "Clock.h"
    6155#include "LuaState.h"
     56#include "Resource.h"
    6257
    6358namespace orxonox
     
    115110
    116111        // setup scripting
    117         scriptModule_.reset(new LuaScriptModule());
    118         luaState_ = scriptModule_->getLuaState();
     112        luaState_.reset(new LuaState());
     113        scriptModule_.reset(new LuaScriptModule(luaState_->getInternalLuaState()));
    119114
    120115        // Create our own logger to specify the filepath
     
    129124        guiSystem_.reset(new System(guiRenderer_.get(), resourceProvider_, 0, scriptModule_.get()));
    130125
    131         // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place
    132         LuaState::openToluaInterfaces(this->luaState_);
    133 
    134         // initialise the basic lua code
    135         this->loadLuaCode();
    136     }
    137 
    138     /**
    139     @brief
    140         Destructor of the GUIManager
    141 
     126        // Initialise the basic lua code
     127        rootFileInfo_ = Resource::getInfo("InitialiseGUI.lua", "GUI");
     128        this->luaState_->doFile("InitialiseGUI.lua", "GUI", false);
     129    }
     130
     131    /**
     132    @brief
    142133        Basically shuts down CEGUI (member smart pointers) but first unloads our Tolua modules.
    143134    */
    144135    GUIManager::~GUIManager()
    145136    {
    146         // destroy our own tolua interfaces
    147         LuaState::closeToluaInterfaces(this->luaState_);
    148     }
    149 
    150     /**
    151     @brief
    152         Calls main Lua script
    153     @todo
    154         This function calls the main Lua script for our GUI.
    155 
    156         Additionally we set the datapath variable in Lua. This is needed so Lua can access the data used for the GUI.
    157     */
    158     void GUIManager::loadLuaCode()
    159     {
    160         // set datapath for GUI data
    161         lua_pushfstring(this->scriptModule_->getLuaState(), Core::getDataPathString().c_str());
    162         lua_setglobal(this->scriptModule_->getLuaState(), "datapath");
    163         // call main Lua script
    164         this->scriptModule_->executeScriptFile("loadGUI_3.lua", "GUI");
    165137    }
    166138
     
    209181    void GUIManager::executeCode(const std::string& str)
    210182    {
    211         try
    212         {
    213             this->scriptModule_->executeString(str);
    214         }
    215         catch (const CEGUI::Exception& ex)
    216         {
    217             COUT(2) << "CEGUI Error: \"" << ex.getMessage() << "\" while executing code \"" << str << "\"" << std::endl;
    218         }
    219         catch (...)
    220         {
    221             COUT(2) << "Couldn't execute GUI related Lua code due to unknown reasons." << std::endl;
    222         }
     183        this->luaState_->doString(str, rootFileInfo_);
    223184    }
    224185
     
    234195    void GUIManager::showGUI(const std::string& name)
    235196    {
    236         this->executeCode(std::string("showGUI(\"") + name + "\")");
     197        this->luaState_->doString("showGUI(\"" + name + "\")", rootFileInfo_);
    237198    }
    238199
  • code/branches/resource2/src/core/GUIManager.h

    r5651 r5661  
    2828 */
    2929
    30 /**
    31 @file
    32 @brief
    33     Declaration of the GUIManager class.
    34 */
    35 
    3630#ifndef _GUIManager_H__
    3731#define _GUIManager_H__
     
    4337#include <CEGUIForwardRefs.h>
    4438#include <boost/scoped_ptr.hpp>
     39#include <boost/shared_ptr.hpp>
    4540
    4641#include "util/OgreForwardRefs.h"
     
    8075        GUIManager(const GUIManager& instance); //!< private and undefined copy c'tor (this is a singleton class)
    8176
    82         void loadLuaCode();
    83 
    8477        // keyHandler functions
    8578        void keyPressed (const KeyEvent& evt);
     
    9386
    9487        scoped_ptr<CEGUI::OgreCEGUIRenderer> guiRenderer_;  //!< CEGUI's interface to the Ogre Engine
     88        scoped_ptr<LuaState>                 luaState_;     //!< LuaState, access point to the Lua engine
    9589        scoped_ptr<CEGUI::LuaScriptModule>   scriptModule_; //!< CEGUI's script module to use Lua
    9690        scoped_ptr<CEGUI::System>            guiSystem_;    //!< CEGUI's main system
     91        shared_ptr<ResourceInfo>             rootFileInfo_; //!< Resource information about the root script
    9792        Ogre::RenderWindow*      renderWindow_;     //!< Ogre's render window to give CEGUI access to it
    9893        CEGUI::ResourceProvider* resourceProvider_; //!< CEGUI's resource provider
    9994        CEGUI::Logger*           ceguiLogger_;      //!< CEGUI's logger to be able to log CEGUI errors in our log
    100         lua_State*               luaState_;         //!< Lua state, access point to the Lua engine
    10195
    10296        static GUIManager*       singletonPtr_s;    //!< Singleton reference to GUIManager
  • code/branches/resource2/src/core/LuaState.cc

    r5660 r5661  
    147147            sourceFileInfo_ = sourceFileInfo;
    148148
    149         //if (!bIsRunning_)
    150         //{
    151         //    bIsRunning_ = true;
    152 
    153149        int error = 0;
    154150#if LUA_VERSION_NUM != 501
     
    163159        // execute the chunk
    164160        if (error == 0)
    165             error = lua_pcall(luaState_, 0, 0, 0);
     161            error = lua_pcall(luaState_, 0, 1, 0);
    166162        if (error != 0)
    167163        {
     
    170166                origin = " originating from " + sourceFileInfo_->filename;
    171167            COUT(2) << "Error in Lua-script" << origin << ": " << lua_tostring(luaState_, -1) << std::endl;
    172         }
    173 
    174         //    bIsRunning_ = false;
    175         //}
    176         //else
    177         //{
    178         //    COUT(2) << "Warning: LuaState do function called while running!" << std::endl;
    179         //}
     168            // return value is nil
     169            lua_pushnil(luaState_);
     170        }
     171        // push return value because it will get lost since the return value of this function is void
     172        lua_setglobal(luaState_, "LuaStateReturnValue");
    180173
    181174        // Load the old info again
     
    191184    {
    192185        OutputHandler::getOutStream().setOutputLevel(level) << message << std::endl;
     186    }
     187
     188    bool LuaState::fileExists(const std::string& filename, const std::string& resourceGroup, bool bSearchOtherPaths)
     189    {
     190        shared_ptr<ResourceInfo> info =  this->getFileInfo(filename, resourceGroup, bSearchOtherPaths);
     191        if (info == NULL)
     192            return false;
     193        else
     194            return true;
    193195    }
    194196
  • code/branches/resource2/src/core/LuaState.h

    r5655 r5661  
    5959        void doString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo = shared_ptr<ResourceInfo>());
    6060
    61         void includeFile(const std::string& filename, const std::string& resourceGroup = "Genreal", bool bSearchOtherPaths = true); // tolua_export
     61        void includeFile(const std::string& filename, const std::string& resourceGroup = "General", bool bSearchOtherPaths = true); // tolua_export
    6262        void includeString(const std::string& code, shared_ptr<ResourceInfo> sourceFileInfo = shared_ptr<ResourceInfo>());
    6363
    6464        void luaPrint(const std::string& str); // tolua_export
    6565        void luaLog(unsigned int level, const std::string& message); // tolua_export
     66        bool fileExists(const std::string& filename, const std::string& resourceGroup = "General", bool bSearchOtherPaths = true); // tolua_export
    6667
    6768        const std::stringstream& getOutput() const { return output_; }
Note: See TracChangeset for help on using the changeset viewer.