guiMLTextCtrl.h 7.8 KB

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