Added spawnCrystalItem
[ryzomcore.git] / ryzom / client / src / r2 / displayer_lua.cpp
blob05974dd91efd8329f5b54e915e87c66343ad1336
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "stdpch.h"
21 #include "displayer_lua.h"
22 #include "nel/gui/lua_ihm.h"
23 #include "../interface_v3/lua_ihm_ryzom.h"
24 #include "editor.h"
26 #ifdef DEBUG_NEW
27 #define new DEBUG_NEW
28 #endif
30 namespace R2
33 // *********************************************************************************************************
34 CDisplayerLua::CDisplayerLua()
36 _ToLua._Displayer = this;
39 // *********************************************************************************************************
40 bool CDisplayerLua::init(const CLuaObject &parameters)
42 //H_AUTO(R2_CDisplayerLua_init)
43 // parameters should be a function that create the lua displayer
44 CLuaStackChecker lsc(parameters.getLuaState());
45 _ToLua._LuaTable.release();
46 if (parameters.isString())
48 getEditor().getEnv()[parameters.toString()].push(); // get method from the R2 env
50 else
52 parameters.push();
54 CLuaState &ls = *parameters.getLuaState();
55 getEditor().getEnv().push(); // this is a method call
56 if (CLuaIHMRyzom::executeFunctionOnStack(ls, 1, 1))
58 _ToLua._LuaTable.pop(ls);
60 else
62 nlwarning("<CDisplayerLua::init> Error while calling displayer ctor (parameter should be a r2 method, or the *name* of a r2 method) : param is : ");
63 parameters.dump();
64 return false;
66 return CDisplayerBase::init(parameters);
69 // *********************************************************************************************************
70 CDisplayerLua::CToLua::CToLua():_Displayer(NULL)
74 // *********************************************************************************************************
75 CDisplayerLua* CDisplayerLua::CToLua::getEnclosing()
77 return _Displayer;
80 // *********************************************************************************************************
81 void CDisplayerLua::CToLua::executeHandler(const CLuaString &eventName, int numArgs)
83 CLuaState &ls = *getLua();
84 CLuaStackRestorer lsr(&ls, ls.getTop() - numArgs);
86 if (!_LuaTable.isValid()) return; // init failed
87 if (_LuaTable[ eventName.getStr().c_str() ].isNil()) return; // event not handled
88 static volatile bool dumpStackWanted = false;
89 if (dumpStackWanted) ls.dumpStack();
90 _LuaTable[ eventName.getStr().c_str() ].push();
91 if (dumpStackWanted) ls.dumpStack();
92 // put method before its args
93 ls.insert(- numArgs - 1);
94 if (dumpStackWanted) ls.dumpStack();
95 // this is a method call
96 _LuaTable.push();
97 if (dumpStackWanted) ls.dumpStack();
98 ls.insert(- numArgs - 1);
99 if (dumpStackWanted) ls.dumpStack();
100 // First arg always is the instance being displayed
101 if (getEnclosing())
102 getEnclosing()->getDisplayedInstance()->getLuaProjection().push();
103 ls.insert(- numArgs - 1);
104 if (dumpStackWanted) ls.dumpStack();
105 CLuaIHMRyzom::executeFunctionOnStack(*_LuaTable.getLuaState(), numArgs + 2, 0);
106 if (dumpStackWanted) ls.dumpStack();
110 // *********************************************************************************************************
111 void CDisplayerLua::CToLua::pushLuaAccess(CLuaState &ls)
113 if (_LuaTable.isValid())
115 nlassert(_LuaTable.getLuaState() == &ls);
116 _LuaTable.push();
118 else
120 ls.pushNil();
125 // *********************************************************************************************************
126 void CDisplayerLua::pushLuaAccess(CLuaState &ls)
128 //H_AUTO(R2_CDisplayerLua_pushLuaAccess)
129 _ToLua.pushLuaAccess(ls);
132 // *********************************************************************************************************
133 CLuaState *CDisplayerLua::CToLua::getLua()
135 if (!_LuaTable.isValid())
137 nlwarning("Warning: try to access to a corrupted table");
138 return 0;
140 return _LuaTable.getLuaState();
143 // *********************************************************************************************************
144 void CDisplayerLua::onActChanged()
146 //H_AUTO(R2_CDisplayerLua_onActChanged)
147 _ToLua.onActChanged();
150 // *********************************************************************************************************
151 void CDisplayerLua::onContinentChanged()
153 //H_AUTO(R2_CDisplayerLua_onContinentChanged)
154 _ToLua.onContinentChanged();
157 // *********************************************************************************************************
158 void CDisplayerLua::onPostCreate()
160 //H_AUTO(R2_CDisplayerLua_onPostCreate)
161 _ToLua.onPostCreate();
164 // *********************************************************************************************************
165 void CDisplayerLua::onCreate()
167 //H_AUTO(R2_CDisplayerLua_onCreate)
168 _ToLua.onCreate();
171 // *********************************************************************************************************
172 void CDisplayerLua::onErase()
174 //H_AUTO(R2_CDisplayerLua_onErase)
175 _ToLua.onErase();
178 // *********************************************************************************************************
179 void CDisplayerLua::onPreHrcMove()
181 //H_AUTO(R2_CDisplayerLua_onPreHrcMove)
182 _ToLua.onPreHrcMove();
185 // *********************************************************************************************************
186 void CDisplayerLua::onPostHrcMove()
188 //H_AUTO(R2_CDisplayerLua_onPostHrcMove)
189 _ToLua.onPostHrcMove();
192 // *********************************************************************************************************
193 void CDisplayerLua::onFocus(bool focused)
195 //H_AUTO(R2_CDisplayerLua_onFocus)
196 _ToLua.onFocus(focused);
199 // *********************************************************************************************************
200 void CDisplayerLua::onSelect(bool selected)
202 //H_AUTO(R2_CDisplayerLua_onSelect)
203 _ToLua.onSelect(selected);
206 // *********************************************************************************************************
207 void CDisplayerLua::onAttrModified(const std::string &attrName,sint32 index)
209 //H_AUTO(R2_CDisplayerLua_onAttrModified)
210 _ToLua.onAttrModified(attrName, index);
213 // *********************************************************************************************************
214 void CDisplayerLua::onTargetInstanceCreated(const std::string &refMakerAttr, sint32 refMakerAttrIndex)
216 //H_AUTO(R2_CDisplayerLua_onTargetInstanceCreated)
217 _ToLua.onTargetInstanceCreated(refMakerAttr, refMakerAttrIndex);
220 // *********************************************************************************************************
221 void CDisplayerLua::onTargetInstanceErased(const std::string &refMakerAttr, sint32 refMakerAttrIndex)
223 //H_AUTO(R2_CDisplayerLua_onTargetInstanceErased)
224 _ToLua.onTargetInstanceErased(refMakerAttr, refMakerAttrIndex);
227 // *********************************************************************************************************
228 void CDisplayerLua::onTargetInstanceEraseRequested(const std::string &refMakerAttr, sint32 refMakerAttrIndex)
230 //H_AUTO(R2_CDisplayerLua_onTargetInstanceEraseRequested)
231 _ToLua.onTargetInstanceEraseRequested(refMakerAttr, refMakerAttrIndex);
234 // *********************************************************************************************************
235 void CDisplayerLua::onTargetInstanceAttrModified(const std::string &refMakerAttr,sint32 refMakerAttrIndex,const std::string &targetAttrName,sint32 targetAttrIndex)
237 //H_AUTO(R2_CDisplayerLua_onTargetInstanceAttrModified)
238 _ToLua.onTargetInstanceAttrModified(refMakerAttr, refMakerAttrIndex, targetAttrName, targetAttrIndex);
241 // *********************************************************************************************************
242 void CDisplayerLua::onTargetInstancePreHrcMove(const std::string &refMakerAttr,sint32 refMakerAttrIndex)
244 //H_AUTO(R2_CDisplayerLua_onTargetInstancePreHrcMove)
245 _ToLua.onTargetInstancePreHrcMove(refMakerAttr, refMakerAttrIndex);
248 // *********************************************************************************************************
249 void CDisplayerLua::onTargetInstancePostHrcMove(const std::string &refMakerAttr,sint32 refMakerAttrIndex)
251 //H_AUTO(R2_CDisplayerLua_onTargetInstancePostHrcMove)
252 _ToLua.onTargetInstancePostHrcMove(refMakerAttr, refMakerAttrIndex);
258 } // R2