messageVector.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MESSAGEVECTOR_H_
  23. #define _MESSAGEVECTOR_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. #ifndef _SIMBASE_H_
  31. #include "console/simBase.h"
  32. #endif
  33. /// Store a list of chat messages.
  34. ///
  35. /// This is responsible for managing messages which appear in the chat HUD.
  36. ///
  37. /// @see GuiMessageVectorCtrl for more details on how this is used.
  38. class MessageVector : public SimObject
  39. {
  40. typedef SimObject Parent;
  41. //-------------------------------------- Public interface...
  42. public:
  43. MessageVector();
  44. ~MessageVector();
  45. public:
  46. struct MessageLine {
  47. char* message;
  48. S32 messageTag;
  49. };
  50. // Spectator registration...
  51. public:
  52. enum MessageCode {
  53. LineInserted = 0,
  54. LineDeleted = 1,
  55. VectorDeletion = 2
  56. };
  57. typedef void (*SpectatorCallback)(void * spectatorKey,
  58. const MessageCode code,
  59. const U32 argument);
  60. void registerSpectator(SpectatorCallback, void * spectatorKey);
  61. void unregisterSpectator(void *spectatorKey);
  62. // Query functions
  63. public:
  64. U32 getNumLines() const;
  65. const MessageLine& getLine(const U32 line) const;
  66. // Mutators
  67. public:
  68. void pushBackLine(const char*, const S32);
  69. void popBackLine();
  70. void pushFrontLine(const char*, const S32);
  71. void popFrontLine();
  72. void clear();
  73. virtual void insertLine(const U32 position, const char*, const S32);
  74. virtual void deleteLine(const U32);
  75. bool dump( const char* filename, const char* header = NULL );
  76. //-------------------------------------- Internal interface
  77. protected:
  78. bool onAdd();
  79. void onRemove();
  80. private:
  81. struct SpectatorRef {
  82. SpectatorCallback callback;
  83. void * key;
  84. };
  85. Vector<MessageLine> mMessageLines;
  86. Vector<SpectatorRef> mSpectators;
  87. void spectatorMessage(MessageCode, const U32 arg);
  88. public:
  89. DECLARE_CONOBJECT(MessageVector);
  90. static void initPersistFields();
  91. };
  92. //--------------------------------------------------------------------------
  93. inline U32 MessageVector::getNumLines() const
  94. {
  95. return mMessageLines.size();
  96. }
  97. //--------------------------------------------------------------------------
  98. inline const MessageVector::MessageLine& MessageVector::getLine(const U32 line) const
  99. {
  100. AssertFatal(line < mMessageLines.size(), "MessageVector::getLine: out of bounds line index");
  101. return mMessageLines[line];
  102. }
  103. #endif // _H_GUICHATVECTOR_