2
0

guiConsoleEditCtrl.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. IMPLEMENT_CONOBJECT(GuiConsoleEditCtrl);
  29. GuiConsoleEditCtrl::GuiConsoleEditCtrl()
  30. {
  31. mSinkAllKeyEvents = true;
  32. mSiblingScroller = NULL;
  33. mUseSiblingScroller = true;
  34. }
  35. void GuiConsoleEditCtrl::initPersistFields()
  36. {
  37. Parent::initPersistFields();
  38. addGroup("GuiConsoleEditCtrl");
  39. addField("useSiblingScroller", TypeBool, Offset(mUseSiblingScroller, GuiConsoleEditCtrl));
  40. endGroup("GuiConsoleEditCtrl");
  41. }
  42. bool GuiConsoleEditCtrl::onKeyDown(const GuiEvent &event)
  43. {
  44. setUpdate();
  45. if (event.keyCode == KEY_TAB)
  46. {
  47. // Get a buffer that can hold the completed text...
  48. FrameTemp<UTF8> tmpBuff(GuiTextCtrl::MAX_STRING_LENGTH);
  49. // And copy the text to be completed into it.
  50. mTextBuffer.getCopy8(tmpBuff, GuiTextCtrl::MAX_STRING_LENGTH);
  51. // perform the completion
  52. bool forward = event.modifier & SI_SHIFT;
  53. mCursorPos = Con::tabComplete(tmpBuff, mCursorPos, GuiTextCtrl::MAX_STRING_LENGTH, forward);
  54. // place results in our buffer.
  55. mTextBuffer.set(tmpBuff);
  56. return true;
  57. }
  58. else if ((event.keyCode == KEY_PAGE_UP) || (event.keyCode == KEY_PAGE_DOWN))
  59. {
  60. // See if there's some other widget that can scroll the console history.
  61. if (mUseSiblingScroller)
  62. {
  63. if (mSiblingScroller)
  64. {
  65. return mSiblingScroller->onKeyDown(event);
  66. }
  67. else
  68. {
  69. // Let's see if we can find it...
  70. SimGroup* pGroup = getGroup();
  71. if (pGroup)
  72. {
  73. // Find the first scroll control in the same group as us.
  74. for (SimSetIterator itr(pGroup); *itr; ++itr)
  75. {
  76. if ((mSiblingScroller = dynamic_cast<GuiScrollCtrl*>(*itr)))
  77. {
  78. return mSiblingScroller->onKeyDown(event);
  79. }
  80. }
  81. }
  82. // No luck... so don't try, next time.
  83. mUseSiblingScroller = false;
  84. }
  85. }
  86. }
  87. return Parent::onKeyDown(event);
  88. }