Merge branch '138-toggle-free-look-with-hotkey' into main/gingo-test
[ryzomcore.git] / ryzom / client / src / r2 / instance.h
blobe8ee67d3d4fe6d1ce87d3a17c2fd559ec7fc1d52
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 #ifndef R2_INSTANCE_H
21 #define R2_INSTANCE_H
23 #include "nel/misc/smart_ptr.h"
25 #include "nel/gui/lua_object.h"
27 #include "game_share/object.h"
28 #include "displayer_base.h"
29 #include "nel/gui/interface_element.h"
30 #include "lua_event_forwarder.h"
32 class CEntityCL;
33 namespace NLGUI
35 class CLuaState;
38 namespace R2
40 class CDisplayerVisual;
41 class CInstance;
43 // unique identifier of an instance in the current module
44 typedef std::string TInstanceId;
47 // callback for instance tree traversal
48 struct IInstanceVisitor
50 virtual void visit(CInstance &inst) = 0;
53 void setDisplayedInstance(CDisplayerBase *displayer, CInstance *displayedInstance);
56 /** An instance in the editor (should be created from a CEditor object)
57 * An instance allows to attach displayers to an objects in the scenario,
59 * \TODO : find a better name that 'CInstance', something like 'CClientObject', 'CClientView' or 'CEditorObject' ...
62 class CInstance : public NLMISC::CRefCount, public CReflectable
64 public:
65 typedef NLMISC::CSmartPtr<CInstance> TSmartPtr;
66 typedef NLMISC::CRefPtr<CInstance> TRefPtr;
67 // shortcut to get the id of this instance
68 TInstanceId getId() const;
69 // shortcut to get the "Name" field in utf8 format (or returns an empty string if not present)
70 std::string getName() const;
71 // get the display name for an entity
72 ucstring getDisplayName();
73 // recursively visit the tree of instances rooted at this instance
74 void visit(IInstanceVisitor &visitor);
75 // get class name for that instance
76 std::string getClassName() const;
77 // get the index of the class of this object
78 sint getClassIndex()const { return _ClassIndex; }
79 // get class definition in lua for that instance (nil if not found)
80 CLuaObject &getClass() const;
81 // Get the lua projection for the CObjectInstance that goes with that object
82 CLuaObject &getLuaProjection();
83 // Advanced : get the counterpart CObjectTable object (which is wrapped by the object
84 // returned by 'getLuaProjection').
85 const CObjectTable *getObjectTable() const { return _ObjectTable; }
86 // TMP TMP TMP for lag compensation (should never write in object properties directly...)
87 CObjectTable *getObjectTable() { return const_cast<CObjectTable *>(_ObjectTable); }
88 // get id in the palette
89 std::string getPaletteId();
90 // Get this instance parent
91 CInstance *getParent() const;
92 // Test if that instance is son of another one
93 bool isSonOf(CInstance *other) const;
94 // Get the parent act
95 CInstance *getParentAct() const;
96 /** See if this instance is of the wanted class (or of a derived one)
97 * \TODO nico : duplicated in lua code, see if worth merging
99 bool isKindOf(const std::string &className) const;
100 // if this object is inside a group, get the group leader, returns the object else, or NULL on error
101 const CInstance *getParentGroupLeader() const;
102 // get parent group if any, or return this object else
103 CInstance *getParentGroup();
104 // get possible parent group
106 // if this object is a logic entity, return its selected sequence, elsedefault to 0
107 sint getSelectedSequence() const;
108 // resturn selected activity sequence for this entity. If a group entity or a group, return the current sequence for the group
109 CObject *getGroupSelectedSequence() const;
111 // get the instance id for the position object into this one
112 std::string getPosInstanceId() const;
114 ////////////////
115 // DISPLAYERS //
116 ////////////////
118 void setDisplayerVisual(CDisplayerVisual *displayer);
119 CDisplayerVisual *getDisplayerVisual() const;
120 void setDisplayerUI(CDisplayerBase *displayer);
121 CDisplayerBase *getDisplayerUI() const { return _DisplayerUI; }
122 void setDisplayerProperties(CDisplayerBase *displayer);
123 CDisplayerBase *getDisplayerProperties() const { return _DisplayerProperties; }
125 ////////////
126 // EVENTS //
127 ////////////
128 void onPreActChanged();
129 void onActChanged();
130 void onContinentChanged();
131 // onCreate is called at creation, and before sons are created
132 void onCreate();
133 // onPostCreate after creation of sons is done
134 void onPostCreate();
135 void onErase();
136 void onPreHrcMove(); // instance is about to move in the hierarchy of object
137 void onPostHrcMove(); // instance has moved in the hierarchy of objects
138 void onFocus(bool focused);
139 void onSelect(bool selected);
140 /** An attribute inside this object has been modified
141 * \param attrName Name of the attribute inside this object, as given by its class definition. If the attribute
142 * is an array, then an additionnal parameter gives the index of the element being modified in the array (or -1 if the whole array is set)
143 * \param attrIndex Index in the element in its array, or -1 if attrName doesn't refer to an array, or if the whole array is modified
145 void onAttrModified(const std::string &attrName, sint32 attrIndex = -1);
146 //virtual void onTableModified(const std::string &tableName, const std::string &keyInTable, sint32 indexInTable);
148 void onPreRender();
149 void onPostRender();
151 /////////////////////////////////
152 // REFERENCED INSTANCES EVENTS //
153 /////////////////////////////////
155 /** A targeted instance has been created
156 * nico : I expect this event to be the result of an undo (will be implemented later, server side)
157 * may also happen if net msg telling that an instance is created arrives after its targeter is created
159 * \param refMaker Name of the attribute that reference the instance (not the instance id)
160 * \param refMakerAttrIndex If the reference maker is in an array, then refMakerAttr gives the name of the array, and refMakerAttrIndex
161 * give its indexin the array
163 void onTargetInstanceCreated(const std::string &refMakerAttr, sint32 refMakerAttrIndex);
164 // a targeted instance has been removed
165 void onTargetInstanceErased(const std::string &refMakerAttr, sint32 refMakerAttrIndex);
166 /** A targeted instance deletion request is about to be sent.
167 * This allow reference makers to delete themselves
168 * Unlike other notifications, this is not forwarded to the displayers
169 * This is because the client that delete a reference target is responsible to
170 * remove the reference target too (by reacting to that message in lua code)
172 void onTargetInstanceEraseRequested(const std::string &refMakerAttr, sint32 refMakerAttrIndex);
173 // an attribute of a targeted instance has been modified
174 void onTargetInstanceAttrModified(const std::string &refMakerAttr, sint32 refMakerAttrIndex,
175 const std::string &targetAttrName, sint32 targetAttrIndex);
176 // Target instance is about to move in the hierarchy of object
177 void onTargetInstancePreHrcMove(const std::string &refMakerAttr, sint32 refMakerAttrIndex);
178 // Target instance has moved in the hierarchy of objects
179 void onTargetInstancePostHrcMove(const std::string &refMakerAttr, sint32 refMakerAttrIndex);
182 /////////////
183 // HELPERS //
184 /////////////
186 // If this entity is displayed using a CEntityCL instance, get it
187 CEntityCL *getEntity() const;
188 // If this entity is displayed using a CEntityCL instance, return its sheet (Entity may be NULL if more then 254 entities are displayed)
189 std::string getSheet() const;
191 ////////////////
192 // PROPERTIES //
193 ////////////////
194 // nb : usually, when they are modified, displayers should be notified of it
196 // Is the object selectable ?
197 bool getSelectable() const { return _Selectable; }
198 void setSelectable(bool selectable);
199 // test select flag, including ancestors
200 bool getSelectableFromRoot() const;
201 void dummySetSelectableFromRoot(bool selectable ); // for export (this prop is read only)
203 //Is object a ghost object ?
204 bool getGhost() const;
205 void setGhost(bool ghost);
207 //////////////////////////////////////////////////////////////////
208 // EXPORTED PROPERTIES (a.k.a native properties in the lua doc) //
209 //////////////////////////////////////////////////////////////////
211 REFLECT_EXPORT_START(R2::CInstance, CReflectable)
212 REFLECT_BOOL("Selectable", getSelectable, setSelectable);
213 REFLECT_BOOL("SelectableFromRoot", getSelectableFromRoot, dummySetSelectableFromRoot);
214 REFLECT_BOOL("Ghost", getGhost, setGhost);
215 REFLECT_EXPORT_END
218 // is this object outnumbering the max number of visible entities (displayed in scene as an exclamation mark)
219 bool maxVisibleEntityExceeded() const;
221 private:
222 CDisplayerBase::TSmartPtr _DisplayerVisual;
223 CDisplayerBase::TSmartPtr _DisplayerUI;
224 CDisplayerBase::TSmartPtr _DisplayerProperties;
225 class CToLua : public CLuaEventForwarder
227 public:
228 CLuaObject _LuaProjection; // projection in lua of this instance properties & methods
229 mutable CLuaObject _Class; // shortcut to class definition of this instance
230 virtual CLuaState *getLua();
231 virtual void executeHandler(const CLuaString &eventName, int numArgs);
233 CToLua _ToLua;
234 const CObjectTable *_ObjectTable; // the real datas for that object
235 mutable TInstanceId _Id; // cache for this object instance id
236 bool _Selectable;
237 sint _ClassIndex;
239 // Caching of current parent pointer
240 mutable bool _LastParentOk;
241 mutable CInstance *_LastParent;
243 bool _RegisteredInDispNameList;
245 private:
246 friend class CEditor;
247 // For editor : Create this object from the CObjectTable it materialize in the editor
248 CInstance(const CObjectTable *objectTable, CLuaState &ls);
249 // copy not supported
250 CInstance(const CInstance &/* other */):NLMISC::CRefCount() { nlassert(0); }
251 CInstance &operator = (const CInstance &/* other */) { nlassert(0); return *this; }
253 void executeHandler(const CLuaString &name, int numArgs);
255 void refreshDisplayNameHandle();
256 public:
257 ~CInstance();
260 } // R2
262 #endif