2
0

guiMLTextCtrl.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 _GUIMLTEXTCTRL_H_
  23. #define _GUIMLTEXTCTRL_H_
  24. #ifndef _GUICONTROL_H_
  25. #include "gui/core/guiControl.h"
  26. #endif
  27. #ifndef _STRINGBUFFER_H_
  28. #include "core/stringBuffer.h"
  29. #endif
  30. #ifndef SOUND_ASSET_H
  31. #include "T3D/assets/SoundAsset.h"
  32. #endif
  33. class GFont;
  34. class SFXTrack;
  35. GFX_DeclareTextureProfile(GFXMLTextureProfile);
  36. class GuiMLTextCtrl : public GuiControl
  37. {
  38. typedef GuiControl Parent;
  39. //-------------------------------------- Public interfaces...
  40. public:
  41. enum Justification
  42. {
  43. LeftJustify,
  44. RightJustify,
  45. CenterJustify,
  46. };
  47. struct Font {
  48. char *faceName;
  49. U32 faceNameLen;
  50. U32 size;
  51. Resource<GFont> fontRes;
  52. Font *next;
  53. };
  54. struct Bitmap {
  55. char bitmapName[1024];
  56. U32 bitmapNameLen;
  57. GFXTexHandle bitmapObject;
  58. Bitmap *next;
  59. };
  60. struct URL
  61. {
  62. bool mouseDown;
  63. U32 textStart;
  64. U32 len;
  65. bool noUnderline;
  66. };
  67. struct Style
  68. {
  69. ColorI color;
  70. ColorI shadowColor;
  71. ColorI linkColor;
  72. ColorI linkColorHL;
  73. Point2I shadowOffset;
  74. Font *font;
  75. bool used;
  76. Style *next;
  77. };
  78. struct Atom
  79. {
  80. U32 textStart;
  81. U32 len;
  82. U32 xStart;
  83. U32 yStart;
  84. U32 width;
  85. U32 baseLine;
  86. U32 descent;
  87. Style *style;
  88. bool isClipped;
  89. U32 pauseTime;
  90. URL *url;
  91. Atom *next;
  92. };
  93. struct Line {
  94. U32 y;
  95. U32 height;
  96. U32 divStyle;
  97. U32 textStart;
  98. U32 len;
  99. Atom *atomList;
  100. Line *next;
  101. };
  102. struct BitmapRef : public RectI
  103. {
  104. BitmapRef *nextBlocker;
  105. U32 textStart;
  106. U32 len;
  107. Bitmap *bitmap;
  108. BitmapRef *next;
  109. };
  110. struct LineTag {
  111. U32 id;
  112. S32 y;
  113. LineTag *next;
  114. };
  115. GuiMLTextCtrl();
  116. ~GuiMLTextCtrl();
  117. DECLARE_CALLBACK( void, onURL, (const char* url));
  118. DECLARE_CALLBACK( void, onResize, ( S32 width, S32 maxY ));
  119. // Text retrieval functions
  120. U32 getNumChars() const;
  121. U32 getText(char* pBuffer, const U32 bufferSize) const;
  122. U32 getWrappedText(char* pBuffer, const U32 bufferSize) const;
  123. const char* getTextContent();
  124. void insertChars(const char* inputChars,
  125. const U32 numInputChars,
  126. const U32 position);
  127. // Text substitution functions
  128. void setText(const char* textBuffer, const U32 numChars);
  129. void addText(const char* textBuffer, const U32 numChars, bool reformat);
  130. void setAlpha(F32 alpha) { mAlpha = alpha;}
  131. bool setCursorPosition(const S32);
  132. void ensureCursorOnScreen();
  133. // Scroll functions
  134. void scrollToTag( U32 id );
  135. void scrollToTop();
  136. void scrollToBottom();
  137. virtual void reflow();
  138. DECLARE_CONOBJECT(GuiMLTextCtrl);
  139. DECLARE_CATEGORY( "Gui Text" );
  140. DECLARE_DESCRIPTION( "A control that displays multiple lines of text." );
  141. static void initPersistFields();
  142. void setScriptValue(const char *value) override;
  143. const char *getScriptValue() override;
  144. static char *stripControlChars(const char *inString);
  145. //-------------------------------------- Protected Structures and constants
  146. protected:
  147. bool mIsEditCtrl;
  148. U32 *mTabStops;
  149. U32 mTabStopCount;
  150. U32 mCurTabStop;
  151. F32 mAlpha;
  152. DataChunker mViewChunker;
  153. DataChunker mResourceChunker;
  154. Line *mLineList;
  155. Bitmap *mBitmapList;
  156. BitmapRef *mBitmapRefList;
  157. Font *mFontList;
  158. LineTag *mTagList;
  159. bool mDirty;
  160. Style *mCurStyle;
  161. U32 mCurLMargin;
  162. U32 mCurRMargin;
  163. U32 mCurJustify;
  164. U32 mCurDiv;
  165. U32 mCurY;
  166. U32 mCurClipX;
  167. Atom *mLineAtoms;
  168. Atom **mLineAtomPtr;
  169. Atom *mEmitAtoms;
  170. Atom **mEmitAtomPtr;
  171. BitmapRef mSentinel;
  172. Line **mLineInsert;
  173. BitmapRef *mBlockList;
  174. U32 mScanPos;
  175. U32 mCurX;
  176. U32 mMaxY;
  177. URL *mCurURL;
  178. URL *mHitURL;
  179. void freeLineBuffers();
  180. void freeResources();
  181. Bitmap *allocBitmap(const char *bitmapName, U32 bitmapNameLen);
  182. Font *allocFont(const char *faceName, U32 faceNameLen, U32 size);
  183. LineTag *allocLineTag(U32 id);
  184. void emitNewLine(U32 textStart);
  185. Atom *buildTextAtom(U32 start, U32 len, U32 left, U32 right, URL *url);
  186. void emitTextToken(U32 textStart, U32 len);
  187. void emitBitmapToken(Bitmap *bmp, U32 textStart, bool bitmapBreak);
  188. void processEmitAtoms();
  189. Atom *splitAtomListEmit(Atom *list, U32 width);
  190. void drawAtomText(bool sel, U32 start, U32 end, Atom *atom, Line *line, Point2I offset);
  191. Atom *findHitAtom(const Point2I localCoords);
  192. Style *allocStyle(Style *style);
  193. static const U32 csmTextBufferGrowthSize;
  194. //-------------------------------------- Data...
  195. protected:
  196. // Cursor position should always be <= mCurrTextSize
  197. U32 mCursorPosition;
  198. // Actual text data. The line buffer is rebuilt from the linear text
  199. // given a specific width. TextBuffer is /not/ \0 terminated
  200. StringBuffer mTextBuffer;
  201. U32 mLineStart;
  202. S32 mMaxBufferSize;
  203. StringTableEntry mInitialText;
  204. // Selection information
  205. bool mSelectionActive;
  206. U32 mSelectionStart;
  207. U32 mSelectionEnd;
  208. U32 mVertMoveAnchor;
  209. bool mVertMoveAnchorValid;
  210. S32 mSelectionAnchor;
  211. Point2I mSelectionAnchorDropped;
  212. // Font resource
  213. Resource<GFont> mFont;
  214. // Console settable parameters
  215. S32 mLineSpacingPixels;
  216. bool mAllowColorChars;
  217. bool mUseURLMouseCursor;
  218. // Too many chars sound:
  219. DECLARE_SOUNDASSET(GuiMLTextCtrl, DeniedSound);
  220. DECLARE_ASSET_SETGET(GuiMLTextCtrl, DeniedSound);
  221. // Typeout over time
  222. bool mUseTypeOverTime;
  223. U32 mTypeOverTimeStartMS;
  224. F32 mTypeOverTimeSpeedMS;
  225. U32 mTypeOverTimeIndex;
  226. U32 mTypeOverTimeTextLen;
  227. SFXTrack *mTypeoutSound;
  228. S32 mTypeoutSoundRate;
  229. //-------------------------------------- Protected interface
  230. protected:
  231. // Inserting and deleting character blocks...
  232. void deleteChars(const U32 rangeStart,
  233. const U32 rangeEnd);
  234. void copyToClipboard(const U32 rangeStart,
  235. const U32 rangeEnd);
  236. // Selection maintainence
  237. bool isSelectionActive() const;
  238. void clearSelection();
  239. // Pixel -> text position mappings
  240. S32 getTextPosition(const Point2I& localPosition);
  241. // Gui control overrides
  242. bool onWake() override;
  243. void onSleep() override;
  244. void onPreRender() override;
  245. void onRender(Point2I offset, const RectI &updateRect) override;
  246. void getCursorPositionAndColor(Point2I &cursorTop, Point2I &cursorBottom, ColorI &color);
  247. void inspectPostApply() override;
  248. void parentResized(const RectI& oldParentRect, const RectI& newParentRect) override;
  249. bool onKeyDown(const GuiEvent& event) override;
  250. void onMouseDown(const GuiEvent&) override;
  251. void onMouseDragged(const GuiEvent&) override;
  252. void onMouseUp(const GuiEvent&) override;
  253. void getCursor(GuiCursor *&cursor, bool &showCursor, const GuiEvent &lastGuiEvent) override;
  254. public:
  255. // Gui control overrides
  256. bool onAdd() override;
  257. void setSelectionStart( U32 start ) { clearSelection(); mSelectionStart = start; };
  258. void setSelectionEnd( U32 end ) { mSelectionEnd = end;};
  259. void setSelectionActive(bool active) { mSelectionActive = active; };
  260. S32 getCursorPosition() { return( mCursorPosition ); }
  261. bool resize(const Point2I &newPosition, const Point2I &newExtent) override;
  262. bool isTypingOut();
  263. };
  264. #endif // _H_GUIMLTEXTCTRL_