2
0

guiDecoyCtrl.cpp 7.1 KB

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