Merge branch '138-toggle-free-look-with-hotkey' into main/gingo-test
[ryzomcore.git] / ryzom / client / src / actions.h
blob7e03452b9fc33e488e384a9b3bfdb1a28258279a
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2019 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2012 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 // Copyright (C) 2015-2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #ifndef CL_ACTIONS_H
24 #define CL_ACTIONS_H
27 /////////////
28 // INCLUDE //
29 /////////////
31 // client
32 #include "events_listener.h"
34 ///////////
35 // CLASS //
36 class CCombo;
37 class CAction;
38 class CActionsManager;
40 /**
41 * The goal of CCombo is to gather together Inputs that will validate an Action.
42 * For now, CCombo is composed of keyboard inputs or mouse inputs
43 * (not Keyboard and mouse inputs in the same CCombo).
45 * \author Guillaume PUZIN
46 * \author Nevrax France
47 * \date 2001
49 * \todo Load/Save.
50 * \todo Check if there is memory leak.
51 * \todo Check comments.
53 class CCombo
55 public:
57 // The key
58 NLMISC::TKey Key;
60 // The CTRL - SHIFT - ALT state
61 NLMISC::TKeyButton KeyButtons;
63 public:
65 /// Init the combo
66 void init (NLMISC::TKey key, NLMISC::TKeyButton keyButtons);
68 /// Get the combo in human readable form
69 std::string toString() const;
71 // For maps
72 bool operator<(const CCombo &other) const
74 if (Key < other.Key)
75 return true;
76 if (Key > other.Key)
77 return false;
78 return KeyButtons < other.KeyButtons;
83 /**
84 * The Goal of CAction is to know the state of an Action.
86 * \author Guillaume PUZIN
87 * \author Nevrax France
88 * \date 2001
90 * \todo Check comments.
92 class CAction
94 public:
96 // The name of an action
97 class CName
99 public:
100 std::string Name;
101 std::string Argu;
103 CName()
107 CName(const char *name)
109 Name = name;
112 CName(const char *name, const char *argu)
114 Name = name;
115 Argu = argu;
119 // For the maps
120 bool operator<(const CName &other) const
122 if (Name < other.Name)
123 return true;
124 if (Name > other.Name)
125 return false;
126 return Argu < other.Argu;
129 // For the maps
130 bool operator== (const CName &other) const
132 return (Name == other.Name) && (Argu == other.Argu);
136 // The icon of an action
137 class CIcon
139 public:
141 // The background color
142 NLMISC::CRGBA Color;
144 // The bitmaps (1 over 0)
145 std::string Bitmaps[2];
147 // The text (6 letters, only A-Z 0-9)
148 std::string Text;
151 /// Constructor
152 CAction();
154 // Execute the action
155 void runAction ();
157 // True if the action has begun (key down..)
158 bool Valide;
160 // Repetition for this action ?
161 bool Repeat;
163 // Do we have to run an AH on key up ?
164 bool KeyUp;
166 // Do we have to run an AH on key down ?
167 bool KeyDown;
169 // The name of the action
170 CName Name;
174 * The base action is a class used to generate actions
176 class CBaseAction
178 public:
180 // Init default values
181 CBaseAction ();
183 // The parameter descriptor of an action
184 class CParameter
186 public:
187 enum TType
189 Hidden=0,
190 Constant, // The parameter is a list of const string
191 User, // The parameter is a user string
192 UserName, // The parameter is a user "name" string
193 } Type;
195 // Init default values
196 CParameter ();
198 // The parameter name (optional)
199 std::string Name;
201 // The parameter localized name (optional)
202 std::string LocalizedName;
204 // Default value
205 std::string DefaultValue;
207 // The parameter constant value is Type == Constant
208 class CValue
210 public:
211 // The value
212 std::string Value;
214 // contexts in which this value is possible
215 std::string Contexts;
217 // The localized value
218 std::string LocalizedValue;
220 std::vector<CValue> Values;
223 // The action parameter descritors
224 std::vector<CParameter> Parameters;
226 // Repetition for this action ?
227 bool Repeat;
229 // Do we have to run an AH on key up ?
230 bool KeyUp;
232 // Do we have to run an AH on key down ?
233 bool KeyDown;
235 // Do we have to wait for an answer from the server before continuing execution in macros ?
236 bool WaitForServer;
238 // Is this action can be macroized?
239 bool Macroisable;
241 // The name of the action handler used by the action
242 std::string Name;
244 // The localized name
245 std::string LocalizedName;
247 // Contexts in which this action exists
248 std::string Contexts;
250 /// Get an action localized text
251 std::string getActionLocalizedText (const CAction::CName &name) const;
253 // see if there's at least one set of parameters for which this action is usable in current context
254 bool isUsableInCurrentContext() const;
256 // The action icon
257 // CAction::CIcon Icon;
261 * Category of action
263 class CCategory
265 public:
266 // The category name
267 std::string Name;
269 // The category localized name
270 std::string LocalizedName;
272 // The set of base action for this category
273 std::vector<CBaseAction> BaseActions;
275 // Is this whole category can be macroized?
276 bool Macroisable;
278 public:
279 CCategory()
281 Macroisable= true;
285 // HashMapTraits for NLMISC::TKey
286 struct CTKeyHashMapTraits
288 enum { bucket_size = 4, min_buckets = 8, };
289 CTKeyHashMapTraits() { }
290 size_t operator() (NLMISC::TKey key) const
292 return (size_t)key;
294 bool operator() (NLMISC::TKey key1, NLMISC::TKey key2) const
296 return key1 < key2;
301 * The aims of CActionsManager ...
303 * \author Guillaume PUZIN
304 * \author Nevrax France
305 * \date 2001
307 * \todo DelCombo(...);
308 * \todo Load/Save.
309 * \todo Check if there is memory leak.
310 * \todo Check comments.
312 class CActionsManager
314 public:
316 /// Typedef to use the map easily.
317 typedef std::map<CAction::CName, CAction> TActionsMap;
318 typedef std::set<CAction::CName> TActionsForceDisplaySet;
319 typedef std::map<CAction::CName, CCombo> TActionComboMap;
320 typedef std::map<CCombo, CAction::CName> TComboActionMap;
321 typedef CHashMultiMap<NLMISC::TKey, CAction::CName, CTKeyHashMapTraits> TKeyActionMap;
323 // Category locator
324 class CCategoryLocator
326 public:
327 uint CategoryId;
328 uint BaseActionId;
331 typedef CHashMultiMap<std::string, CCategoryLocator> TActionBaseActionMap;
333 /// Constructor
334 CActionsManager();
336 /// Add a new Action in the context.
337 bool addAction(const CAction::CName &name);
339 /// Return an action from its name
340 CAction* getAction(const CAction::CName &name);
342 /// Clear actions
343 void clear ();
345 /// Enable / disable combos
346 void enable (bool enable);
348 /// Remove a combo
349 void removeCombo (const CCombo &combo);
351 // Remove all the combos
352 void removeAllCombos();
354 /// Add a combo in an Action.
355 bool addCombo(const CAction::CName &name, const CCombo &combo, bool createAction = true);
357 /// Get the base action and category of an action
358 const CCategoryLocator *getActionLocator (const CAction::CName &name) const;
360 /// Get the base action of an action
361 const CBaseAction *getBaseAction (const CAction::CName &name) const;
363 // remove an action from the action manager
364 void removeBaseAction(const CAction::CName &name);
366 /// Get the category of an action
367 const CCategory *getCategory (const CAction::CName &name) const;
369 /// Return if the Action is valide.
370 bool valide(const CAction::CName &name) const;
371 void validate(const CAction::CName &name);
372 void unvalidate(const CAction::CName &name);
374 // Return true if the action is present in current (global) context
375 bool isActionPresentInContext(const CAction::CName &name) const;
377 /// A key has been pushed. Return true if an action handler is associated with this key.
378 bool keyPushed (const NLMISC::CEventKeyDown &keyDown);
380 /// A key has been released
381 void keyReleased (const NLMISC::CEventKeyUp &keyUp);
383 /// Get the category array
384 const std::vector<CCategory> &getCategories () const;
386 /// Reserve space in the category array
387 void reserveCategories (uint space);
389 /// Add a category
390 void addCategory (const CCategory &category);
392 /// Remove a category
393 void removeCategory (const std::string &catName);
395 /// Get combo / action map
396 const TComboActionMap &getComboActionMap () const;
398 /// Get action / combo map
399 const TActionComboMap &getActionComboMap () const;
401 /// Release All keys, without running any AH
402 void releaseAllKeyNoRunning();
404 /// true if a combo is already associated to an action
405 bool isComboAssociated(const CCombo &combo) const {return _ComboAction.find(combo)!=_ComboAction.end();}
407 /// true if an action is already associated to a combo
408 bool isActionAssociated(const CAction::CName &name) const {return _ActionCombo.find(name)!=_ActionCombo.end();}
410 /// Force the action to be always displayed in the "Keys" interface, even if the key is unbound. default is false
411 void forceDisplayForAction(const CAction::CName &name, bool state);
413 /// see forceDisplayForAction
414 bool isActionDisplayForced(const CAction::CName &name) const;
416 /// see forceDisplayForAction
417 const TActionsForceDisplaySet &getActionsForceDisplaySet() const {return _ActionForceDisplay;}
419 /// Get an action localized text
420 std::string getActionLocalizedText (const CAction::CName &name) const;
422 //@}
424 private:
426 /// Update key buttons
427 void updateKeyButton (NLMISC::TKeyButton);
429 /// Map with all Actions in the context. the string = Action name.
430 TActionsMap _Actions;
431 TActionsForceDisplaySet _ActionForceDisplay;
432 TActionComboMap _ActionCombo;
433 TComboActionMap _ComboAction;
434 TKeyActionMap _KeyAction;
435 TKeyActionMap _WatchedActions;
436 TActionBaseActionMap _ActionCategory;
437 std::vector<CCategory> _Categories;
439 bool _Enabled;
441 };// CActionsManager //
444 * Action context
446 class CActionsContext
448 public:
449 CActionsContext();
451 // Add an action manager.
452 bool addActionsManager (CActionsManager *actionManager, const std::string &category="");
454 // Get an action manager. Returns NULL if not found.
455 CActionsManager *getActionsManager (const std::string &category="") const;
457 void setContext(const std::string &context) { _Context = context; }
458 bool matchContext(const std::string &contexts) const;
460 void removeAllCombos();
462 private:
463 typedef std::map<std::string, CActionsManager *> TActionsManagerMap;
464 TActionsManagerMap _ActionsManagers;
465 std::string _Context;
468 extern CActionsContext ActionsContext;
470 #endif // CL_ACTIONS_H
472 /* End of actions.h */