root/OpenSceneGraph/trunk/src/osgWidget/Lua.cpp @ 13041

Revision 13041, 3.7 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1// -*-c++-*- osgWidget - Code by: Jeremy Moles (cubicool) 2007-2008
2
3#include <osgDB/FileUtils>
4#include <osgWidget/Lua>
5#include <osgWidget/Box>
6#include <osgWidget/WindowManager>
7
8// If you want to build with LUA, include it--otherwise, typedef some of the data types
9// so that we don't pollute our code too much with conditional includes.
10#ifdef OSGWIDGET_USELUA
11extern "C" {
12#include <lua.h>
13#include <lualib.h>
14#include <lauxlib.h>
15}
16#endif
17
18namespace osgWidget {
19
20// This namespace will include all of our Lua library functions. Otherwise, it'll be
21// an empty namespace. This is 100% for code clarity, as this namespace is internal and
22// not visible to the outside C/C++ world.
23namespace lua {
24#ifdef OSGWIDGET_USELUA
25
26// Strings representing our global REGISTRY values.
27const char* G_WM = "osgWidget_G_WindowManager";
28
29WindowManager* getWindowManager(lua_State* L) {
30    lua_pushstring(L, G_WM);
31    lua_gettable(L, LUA_REGISTRYINDEX);
32
33    return reinterpret_cast<WindowManager*>(lua_touserdata(L, -1));
34}
35
36int newWindow(lua_State* L) {
37    osg::ref_ptr<Window> w = new Box("testLUA", Box::HORIZONTAL);
38
39    lua_pushstring(L, w->getName().c_str());
40
41    return 1;
42}
43
44int newWidget(lua_State* L) {
45    osg::ref_ptr<Widget> w = new Widget("testLUA", 0.0f, 0.0f);
46
47    lua_pushstring(L, w->getName().c_str());
48
49    return 1;
50}
51
52int getWindow(lua_State* L) {
53    WindowManager* wm = getWindowManager(L);
54
55    lua_pushlightuserdata(L, wm);
56
57    return 1;
58}
59
60#endif
61}
62
63// A helper function for all those cases where we need to inform the user that there isn't
64// a LUA engine available.
65bool noLuaFail(const std::string& err) {
66    warn() << err << "; Lua not compiled in library." << std::endl;
67
68    return false;
69}
70
71// Our "private", internal data.
72struct LuaEngineData {
73#ifdef OSGWIDGET_USELUA
74    LuaEngineData():
75    lua(0) {
76    }
77
78    lua_State* lua;
79#endif
80};
81
82LuaEngine::LuaEngine(WindowManager* wm):
83_wm(wm) {
84#ifdef OSGWIDGET_USELUA
85    _data = new LuaEngineData();
86
87#else
88    _data = 0;
89#endif
90}
91
92bool LuaEngine::initialize() {
93#ifdef OSGWIDGET_USELUA
94    _data->lua = lua_open();
95
96    luaL_openlibs(_data->lua);
97
98    static const struct luaL_reg library[] = {
99        {"newWindow", lua::newWindow},
100        {"newWidget", lua::newWidget},
101        {"getWindow", lua::getWindow},
102        {0, 0}
103    };
104
105    luaL_openlib(_data->lua, "osgwidget", library, 0);
106
107    // An alternative to using the Registry here would be to pass the WindowManager
108    // as a userdata "closure" (pvalue). Please see the following doc on more info:
109    // http://www.lua.org/pil/27.3.3.html
110    lua_pushstring(_data->lua, lua::G_WM);
111    lua_pushlightuserdata(_data->lua, _wm);
112    lua_settable(_data->lua, LUA_REGISTRYINDEX);
113
114    return true;
115
116#else
117    return noLuaFail("Can't initialize the LuaEngine");
118#endif
119}
120
121bool LuaEngine::close() {
122#ifdef OSGWIDGET_USELUA
123    lua_close(_data->lua);
124
125    delete _data;
126
127    return true;
128
129#else
130    return noLuaFail("Can't close the LuaEngine");
131#endif
132}
133
134bool LuaEngine::eval(const std::string& code) {
135#ifdef OSGWIDGET_USELUA
136    if(luaL_dostring(_data->lua, code.c_str())) {
137        warn() << "LuaEngine::eval - " << lua_tostring(_data->lua, -1) << std::endl;
138
139        return false;
140    }
141
142    return true;
143
144#else
145    return noLuaFail("Can't evaluate code in LuaEngine");
146#endif
147}
148
149bool LuaEngine::runFile(const std::string& filePath) {
150#ifdef OSGWIDGET_USELUA
151    if(!osgDB::fileExists(filePath)) {
152        warn() << "Couldn't find file \"" << filePath << "\" for LuaEngine." << std::endl;
153
154        return false;
155    }
156
157    if(luaL_dofile(_data->lua, filePath.c_str())) {
158        warn() << "LuaEngine::runFile - " << lua_tostring(_data->lua, -1) << std::endl;
159
160        return false;
161    }
162
163    return true;
164
165#else
166    return noLuaFail("Can't run file in LuaEngine");
167#endif
168}
169
170}
Note: See TracBrowser for help on using the browser.