guiConsoleEditCtrl.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #include "console/consoleTypes.h"
  23. #include "console/console.h"
  24. #include "graphics/dgl.h"
  25. #include "gui/guiCanvas.h"
  26. #include "gui/guiConsoleEditCtrl.h"
  27. #include "memory/frameAllocator.h"
  28. GuiConsoleEditHistory::GuiConsoleEditHistory()
  29. {
  30. historyList = vector<string>();
  31. }
  32. const string& GuiConsoleEditHistory::getPrevHistory()
  33. {
  34. index = static_cast<U32>(getMax(static_cast<S32>(index - 1), 0));
  35. return historyList.size() == 0 ? workingText : historyList[index];
  36. }
  37. const string& GuiConsoleEditHistory::getNextHistory()
  38. {
  39. index = getMin(index + 1, historyList.size());
  40. return historyList.size() == index ? workingText : historyList[index];
  41. }
  42. IMPLEMENT_CONOBJECT(GuiConsoleEditCtrl);
  43. GuiConsoleEditCtrl::GuiConsoleEditCtrl()
  44. {
  45. mSinkAllKeyEvents = true;
  46. mSiblingScroller = NULL;
  47. mUseSiblingScroller = true;
  48. mReturnCausesTab = false;
  49. mHistory = GuiConsoleEditHistory();
  50. }
  51. void GuiConsoleEditCtrl::initPersistFields()
  52. {
  53. Parent::initPersistFields();
  54. addGroup("GuiConsoleEditCtrl");
  55. addField("useSiblingScroller", TypeBool, Offset(mUseSiblingScroller, GuiConsoleEditCtrl));
  56. endGroup("GuiConsoleEditCtrl");
  57. }
  58. bool GuiConsoleEditCtrl::onKeyDown(const GuiEvent& event)
  59. {
  60. setUpdate();
  61. if (event.keyCode == KEY_TAB)
  62. {
  63. // Get a buffer that can hold the completed text...
  64. FrameTemp<UTF8> tmpBuff(GuiTextEditCtrl::MAX_STRING_LENGTH);
  65. // And copy the text to be completed into it.
  66. StringBuffer strBuff(mTextBuffer.c_str());
  67. strBuff.getCopy8(tmpBuff, GuiTextEditCtrl::MAX_STRING_LENGTH);
  68. // perform the completion
  69. bool forward = event.modifier & SI_SHIFT;
  70. //mCursorPos = Con::tabComplete(tmpBuff, mCursorPos, GuiTextEditCtrl::MAX_STRING_LENGTH, forward);
  71. // place results in our buffer.
  72. mTextBuffer.assign(tmpBuff);
  73. return true;
  74. }
  75. else if ((event.keyCode == KEY_PAGE_UP) || (event.keyCode == KEY_PAGE_DOWN))
  76. {
  77. // See if there's some other widget that can scroll the console history.
  78. if (mUseSiblingScroller)
  79. {
  80. if (mSiblingScroller)
  81. {
  82. return mSiblingScroller->onKeyDown(event);
  83. }
  84. else
  85. {
  86. // Let's see if we can find it...
  87. SimGroup* pGroup = getGroup();
  88. if (pGroup)
  89. {
  90. // Find the first scroll control in the same group as us.
  91. for (SimSetIterator itr(pGroup); *itr; ++itr)
  92. {
  93. if ((mSiblingScroller = dynamic_cast<GuiScrollCtrl*>(*itr)))
  94. {
  95. return mSiblingScroller->onKeyDown(event);
  96. }
  97. }
  98. }
  99. // No luck... so don't try, next time.
  100. mUseSiblingScroller = false;
  101. }
  102. }
  103. }
  104. return Parent::onKeyDown(event);
  105. }
  106. bool GuiConsoleEditCtrl::handleEnterKey()
  107. {
  108. mHistory.updateHistory(mTextBuffer);
  109. return Parent::handleEnterKey();
  110. }
  111. bool GuiConsoleEditCtrl::handleArrowKey(GuiDirection direction)
  112. {
  113. if (!mTextWrap && (direction == GuiDirection::Up || direction == GuiDirection::Down))
  114. {
  115. string historyText = (direction == GuiDirection::Up ? mHistory.getPrevHistory() : mHistory.getNextHistory());
  116. setText(historyText);
  117. mSelector.setTextLength(historyText.length());
  118. mSelector.setCursorPosition(historyText.length());
  119. return true;
  120. }
  121. else
  122. {
  123. return Parent::handleArrowKey(direction);
  124. }
  125. }
  126. bool GuiConsoleEditCtrl::handleCharacterInput(const GuiEvent& event)
  127. {
  128. string before = mTextBuffer;
  129. bool result = Parent::handleCharacterInput(event);
  130. if (before != mTextBuffer)
  131. {
  132. mHistory.setWorkingText(mTextBuffer);
  133. }
  134. return result;
  135. }
  136. bool GuiConsoleEditCtrl::handleBackSpace()
  137. {
  138. string before = mTextBuffer;
  139. bool result = Parent::handleBackSpace();
  140. if (before != mTextBuffer)
  141. {
  142. mHistory.setWorkingText(mTextBuffer);
  143. }
  144. return result;
  145. }
  146. bool GuiConsoleEditCtrl::handleDelete()
  147. {
  148. string before = mTextBuffer;
  149. bool result = Parent::handleDelete();
  150. if (before != mTextBuffer)
  151. {
  152. mHistory.setWorkingText(mTextBuffer);
  153. }
  154. return result;
  155. }
  156. void GuiConsoleEditCtrl::onLoseFirstResponder()
  157. {
  158. Platform::disableKeyboardTranslation();
  159. if (isMethod("onLoseFirstResponder"))
  160. Con::executef(this, 2, "onLoseFirstResponder");
  161. mSelector.setFirstResponder(false);
  162. mTextOffsetY = 0;
  163. mScrollVelocity = 0;
  164. if (!mTextWrap && mTextBlockList.size() > 0)
  165. {
  166. mTextBlockList.front().resetScroll();
  167. }
  168. // Redraw the control:
  169. setUpdate();
  170. }