guiDecoyCtrl.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #include "platform/platform.h"
  23. #include "gui/controls/guiDecoyCtrl.h"
  24. #include "gui/buttons/guiButtonBaseCtrl.h"
  25. #include "console/consoleTypes.h"
  26. #include "gfx/primBuilder.h"
  27. //-----------------------------------------------------------------------------
  28. // GuiDecoyCtrl
  29. //-----------------------------------------------------------------------------
  30. /*
  31. So far this control has been designed in mind solely for button controls. I'm pretty sure
  32. it can be used for other things, but to do anything more in depth; it has to be extended.
  33. Make sure you know a little about how guiCanvas hands out signals to gui controls before you tinker
  34. in this class.
  35. Been thinking about this class a little more. I tried pretty hard to protect this class into being
  36. guiControl like agnostic. But I ended up adding a check specifically for buttons in the
  37. onMouseUp function. Its been protected with a dynamic_cast and a NULL check; but in the end, the only way
  38. too solve the main problem, that GuiCanvas cannot process more than one mouse action for more than one
  39. gui control at a time, is for it to get a rewrite.
  40. */
  41. IMPLEMENT_CONOBJECT(GuiDecoyCtrl);
  42. ConsoleDocClass( GuiDecoyCtrl,
  43. "@brief Designed soley for buttons, primarily used in editor.\n\n"
  44. "Currently editor use only, no real application without extension.\n\n "
  45. "@internal");
  46. GuiDecoyCtrl::GuiDecoyCtrl() : mIsDecoy(true),
  47. mMouseOver(false),
  48. mDecoyReference(NULL)
  49. {
  50. }
  51. GuiDecoyCtrl::~GuiDecoyCtrl()
  52. {
  53. }
  54. void GuiDecoyCtrl::initPersistFields()
  55. {
  56. addField("isDecoy", TypeBool, Offset(mIsDecoy, GuiDecoyCtrl), "Sets this control to decoy mode");
  57. Parent::initPersistFields();
  58. }
  59. void GuiDecoyCtrl::onMouseUp(const GuiEvent &event)
  60. {
  61. mouseUnlock();
  62. setUpdate();
  63. //this code is pretty hacky. right now there is no way that guiCanvas will allow sending more than
  64. //one signal to one gui control at a time.
  65. if(mIsDecoy == true)
  66. {
  67. mVisible = false;
  68. GuiControl *parent = getParent();
  69. Point2I localPoint = parent->globalToLocalCoord(event.mousePoint);
  70. GuiControl *tempControl = parent->findHitControl(localPoint);
  71. //the decoy control has the responsibility of keeping track of the decoyed controls status
  72. if( mDecoyReference != NULL && tempControl == mDecoyReference)
  73. tempControl->onMouseUp(event);
  74. else if(mDecoyReference != NULL && tempControl != mDecoyReference)
  75. {
  76. //as explained earlier, this control was written in the mindset for buttons.
  77. //nothing bad should ever happen if not a button due to the checks in this function though.
  78. GuiButtonBaseCtrl *unCastCtrl = NULL;
  79. unCastCtrl = dynamic_cast<GuiButtonBaseCtrl *>( mDecoyReference );
  80. if(unCastCtrl != NULL)
  81. unCastCtrl->resetState();
  82. }
  83. mVisible = true;
  84. }
  85. }
  86. void GuiDecoyCtrl::onMouseDown(const GuiEvent &event)
  87. {
  88. if ( !mVisible || !mAwake )
  89. return;
  90. mouseLock();
  91. if(mIsDecoy == true)
  92. {
  93. mVisible = false;
  94. GuiControl *parent = getParent();
  95. Point2I localPoint = parent->globalToLocalCoord(event.mousePoint);
  96. GuiControl *tempControl = parent->findHitControl(localPoint);
  97. tempControl->onMouseDown(event);
  98. mVisible = true;
  99. }
  100. execConsoleCallback();
  101. setUpdate();
  102. }
  103. void GuiDecoyCtrl::onMouseMove(const GuiEvent &event)
  104. {
  105. //if this control is a dead end, make sure the event stops here
  106. if ( !mVisible || !mAwake )
  107. return;
  108. //pass the event to the parent
  109. GuiControl *parent = getParent();
  110. if ( parent )
  111. parent->onMouseMove( event );
  112. Point2I localPoint = parent->globalToLocalCoord(event.mousePoint);
  113. //also pretty hacky. since guiCanvas, *NOT* GuiControl, distributes the calls for onMouseEnter
  114. //and onMouseLeave, we simulate those calls here through a series of checks.
  115. if(mIsDecoy == true)
  116. {
  117. mVisible = false;
  118. GuiControl *parent = getParent();
  119. GuiControl *tempControl = parent->findHitControl(localPoint);
  120. //the decoy control has the responsibility of keeping track of the decoyed controls status
  121. if(mMouseOverDecoy == false && mDecoyReference != NULL)
  122. {
  123. tempControl->onMouseEnter(event);
  124. mMouseOverDecoy = true;
  125. }
  126. else if(tempControl != mDecoyReference && mDecoyReference != NULL)
  127. {
  128. mDecoyReference->onMouseLeave(event);
  129. mMouseOverDecoy = false;
  130. }
  131. mDecoyReference = tempControl;
  132. mVisible = true;
  133. }
  134. }
  135. void GuiDecoyCtrl::onMouseDragged(const GuiEvent &event)
  136. {
  137. }
  138. void GuiDecoyCtrl::onMouseEnter(const GuiEvent &event)
  139. {
  140. if ( !mVisible || !mAwake )
  141. return;
  142. setUpdate();
  143. Con::executef( this , "onMouseEnter" );
  144. mMouseOver = true;
  145. }
  146. void GuiDecoyCtrl::onMouseLeave(const GuiEvent &event)
  147. {
  148. if ( !mVisible || !mAwake )
  149. return;
  150. setUpdate();
  151. Con::executef( this , "onMouseLeave" );
  152. mMouseOver = false;
  153. }
  154. bool GuiDecoyCtrl::onMouseWheelUp( const GuiEvent &event )
  155. {
  156. //if this control is a dead end, make sure the event stops here
  157. if ( !mVisible || !mAwake )
  158. return true;
  159. //pass the event to the parent
  160. GuiControl *parent = getParent();
  161. if ( parent )
  162. return parent->onMouseWheelUp( event );
  163. else
  164. return false;
  165. }
  166. bool GuiDecoyCtrl::onMouseWheelDown( const GuiEvent &event )
  167. {
  168. //if this control is a dead end, make sure the event stops here
  169. if ( !mVisible || !mAwake )
  170. return true;
  171. //pass the event to the parent
  172. GuiControl *parent = getParent();
  173. if ( parent )
  174. return parent->onMouseWheelDown( event );
  175. else
  176. return false;
  177. }
  178. void GuiDecoyCtrl::onRightMouseDown(const GuiEvent &)
  179. {
  180. }
  181. void GuiDecoyCtrl::onRightMouseUp(const GuiEvent &)
  182. {
  183. }
  184. void GuiDecoyCtrl::onRightMouseDragged(const GuiEvent &)
  185. {
  186. }
  187. void GuiDecoyCtrl::onMiddleMouseDown(const GuiEvent &)
  188. {
  189. }
  190. void GuiDecoyCtrl::onMiddleMouseUp(const GuiEvent &)
  191. {
  192. }
  193. void GuiDecoyCtrl::onMiddleMouseDragged(const GuiEvent &)
  194. {
  195. }