Clone of Akilla's ac2d @ https://github.com/deregtd/AC2D

cEditBox.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #pragma once
  2. #include "cScrollBar.h"
  3. class cEditBox : public CWindow, private KeyboardEventsAbstractor< cEditBox >, private ResizeEventAbstractor< cEditBox >, private RenderEventAbstractor< cEditBox >
  4. {
  5. public:
  6. cEditBox()
  7. {
  8. m_sbScroll.SetPosition(GetWidth()-16, 0);
  9. m_sbScroll.SetSize(16, GetHeight());
  10. m_sbScroll.SetAnchorBottom(true);
  11. m_sbScroll.SetAnchorLeft(false);
  12. m_sbScroll.SetAnchorRight(true);
  13. m_sbScroll.SetValue(100);
  14. AddChild(m_sbScroll);
  15. SetActiveLine(0);
  16. SetMultiLine(false);
  17. SetReadOnly(true);
  18. SetFontSize(12);
  19. SetShowReverse(false);
  20. SetAddReverse(false);
  21. AddKeyboardEventHandler( *(KeyboardEventsAbstractor< cEditBox > *)this );
  22. AddResizeEventHandler( *(ResizeEventAbstractor< cEditBox > *)this );
  23. AddRenderEventHandler( *(RenderEventAbstractor< cEditBox > *)this );
  24. }
  25. private:
  26. bool RenderEventAbstractor< cEditBox >::OnRender( IWindow & Window, double TimeSlice )
  27. {
  28. float iLeft = GetAbsoluteLeft(),
  29. iRight = iLeft + GetWidth(),
  30. iTop = GetAbsoluteTop(),
  31. iBottom = iTop + GetHeight(),
  32. iWidth = GetWidth(),
  33. iHeight = GetHeight();
  34. glBindTexture(GL_TEXTURE_2D, 0);
  35. int iTpCount = (int) m_sbScroll.GetValue();
  36. if (m_bShowReverse)
  37. iTpCount = (int) m_vLines.size() - iTpCount;
  38. int iCount = 0;
  39. int iMaxLines = (int) (iHeight/13);
  40. if (!m_bMultiLine)
  41. {
  42. iMaxLines = 1;
  43. iTpCount = 0;
  44. }
  45. std::list<stChatLine>::iterator i = m_vLines.begin();
  46. for (int h=0; h<iTpCount; h++)
  47. i++;
  48. glListBase(0x01000000 + (m_dwFontSize << 8));
  49. for (; (i != m_vLines.end()) && (iCount < iMaxLines); i++, iCount++)
  50. {
  51. glColor4ub(cColor[(*i).Color][0],cColor[(*i).Color][1],cColor[(*i).Color][2], (BYTE) (1.0f/*m_fTrans*/*255));
  52. if (m_bShowReverse)
  53. glRasterPos2f(iLeft+1, iBottom-((m_dwFontSize+1)*(iCount))-5);
  54. else
  55. glRasterPos2f(iLeft+1, iTop+11+(float) (((m_dwFontSize+1)*iCount)));
  56. int iLen = (int) (*i).String.length();
  57. const char *Str = (*i).String.c_str();
  58. glCallLists(iLen, GL_UNSIGNED_BYTE, Str);
  59. }
  60. //border
  61. glBegin(GL_LINE_LOOP);
  62. glColor4f(0.5,0.5,0.5,1.0f/*m_fTrans*/);
  63. glVertex2f(iLeft,iTop);
  64. glVertex2f(iRight,iTop);
  65. glVertex2f(iRight,iBottom);
  66. glVertex2f(iLeft,iBottom);
  67. glEnd();
  68. return true;
  69. }
  70. bool ResizeEventAbstractor< cEditBox >::OnResize( IWindow & Window, float NewWidth, float NewHeight)
  71. {
  72. if (NewWidth < 32)
  73. return false;
  74. if (NewHeight < m_dwFontSize + 2)
  75. return false;
  76. return true;
  77. }
  78. bool ResizeEventAbstractor< cEditBox >::OnResized( IWindow & Window )
  79. {
  80. return false;
  81. }
  82. bool KeyboardEventsAbstractor< cEditBox >::OnKeyPress( IWindow & Window, unsigned long KeyCode )
  83. {
  84. if (m_bReadOnly)
  85. return true;
  86. if ((!m_vLines.size()) && (!m_bMultiLine))
  87. {
  88. AddLine("", eWhite);
  89. SetActiveLine(0);
  90. }
  91. if (KeyCode == 0x08) return true; //backspace
  92. if (KeyCode == 0x0A) return true; //linefeed
  93. if (KeyCode == 0x09) return true; //tab
  94. if (KeyCode == 0x1B) return true; //escape
  95. if (KeyCode == 0x0D) return true; //CR
  96. char bleh[2] = { 0, 0 };
  97. bleh[0] = (char) KeyCode;
  98. m_viActiveLine->String = m_viActiveLine->String + bleh;
  99. return true;
  100. }
  101. bool KeyboardEventsAbstractor< cEditBox >::OnKeyDown( IWindow & Window, unsigned long KeyCode )
  102. {
  103. if (m_bReadOnly)
  104. return true;
  105. if ((!m_vLines.size()) && (!m_bMultiLine))
  106. {
  107. AddLine("", eWhite);
  108. SetActiveLine(0);
  109. }
  110. char bleh[2] = { 0, 0 };
  111. bleh[0] = (char) KeyCode;
  112. if (KeyCode == VK_BACK)
  113. {
  114. if (m_viActiveLine->String.size())
  115. m_viActiveLine->String = m_viActiveLine->String.substr(0, m_viActiveLine->String.size()-1);
  116. }
  117. else if (KeyCode == VK_RETURN)
  118. {
  119. if (!m_bMultiLine)
  120. {
  121. m_vSubmitQueue.push_back(m_viActiveLine->String);
  122. m_viActiveLine->String = "";
  123. }
  124. }
  125. return true;
  126. }
  127. bool KeyboardEventsAbstractor< cEditBox >::OnKeyUp( IWindow &Window, unsigned long KeyCode )
  128. {
  129. return false;
  130. }
  131. public:
  132. bool NeedSubmit() const
  133. {
  134. if (!m_bMultiLine)
  135. return (m_vSubmitQueue.size() > 0);
  136. else
  137. return false;
  138. //TODO: Implement a multiline submit of some sort... Needs an event callback...
  139. }
  140. std::string GetSubmit()
  141. {
  142. if ((!m_bMultiLine) && (m_vSubmitQueue.size()))
  143. {
  144. std::string toret = m_vSubmitQueue.front();
  145. m_vSubmitQueue.pop_front();
  146. return toret;
  147. }
  148. return "";
  149. }
  150. /* void SetText(std::string Text, int iIndex)
  151. {
  152. if (iIndex < (int) m_vLines.size())
  153. m_vLines[iIndex] = Text;
  154. }
  155. std::string GetText(int iIndex)
  156. {
  157. return m_vLines[iIndex].String;
  158. }*/
  159. void SetMultiLine(bool ML)
  160. {
  161. m_bMultiLine = ML;
  162. if (m_bMultiLine)
  163. {
  164. m_sbScroll.SetVisible(true);
  165. }
  166. else
  167. {
  168. m_sbScroll.SetVisible(false);
  169. }
  170. }
  171. void SetReadOnly(bool RO)
  172. {
  173. m_bReadOnly = RO;
  174. }
  175. void SetFontSize(int FS)
  176. {
  177. m_dwFontSize = FS;
  178. }
  179. void SetShowReverse(bool SR)
  180. {
  181. m_bShowReverse = SR;
  182. }
  183. void SetAddReverse(bool AR)
  184. {
  185. m_bAddReverse = AR;
  186. }
  187. void AddLine(std::string String, int Color)
  188. {
  189. stChatLine CL = { String, (eColor) Color };
  190. if (m_bAddReverse)
  191. m_vLines.push_front(CL);
  192. else
  193. m_vLines.push_back(CL);
  194. bool ResetAfter = (m_sbScroll.GetValue() == m_sbScroll.GetMax());
  195. m_sbScroll.SetMin(1);
  196. m_sbScroll.SetMax((float) m_vLines.size());
  197. if (ResetAfter)
  198. m_sbScroll.SetValue(m_sbScroll.GetMax());
  199. }
  200. void SetActiveLine(int Line)
  201. {
  202. if (Line < (int) m_vLines.size())
  203. {
  204. m_iActiveLine = Line;
  205. m_viActiveLine = m_vLines.begin();
  206. for (int i=0; i<Line; i++)
  207. m_viActiveLine++;
  208. }
  209. }
  210. private:
  211. struct stChatLine {
  212. std::string String;
  213. eColor Color;
  214. };
  215. DWORD m_dwFontSize;
  216. bool m_bMultiLine;
  217. bool m_bReadOnly;
  218. int m_iActiveLine;
  219. bool m_bShowReverse;
  220. bool m_bAddReverse;
  221. std::list<std::string> m_vSubmitQueue;
  222. std::list<stChatLine> m_vLines;
  223. std::list<stChatLine>::iterator m_viActiveLine;
  224. cScrollBar m_sbScroll;
  225. };