guiDecoyCtrl.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. docsURL;
  59. addField("isDecoy", TypeBool, Offset(mIsDecoy, GuiDecoyCtrl), "Sets this control to decoy mode");
  60. Parent::initPersistFields();
  61. }
  62. void GuiDecoyCtrl::onMouseUp(const GuiEvent &event)
  63. {
  64. mouseUnlock();
  65. setUpdate();
  66. //this code is pretty hacky. right now there is no way that guiCanvas will allow sending more than
  67. //one signal to one gui control at a time.
  68. if(mIsDecoy == true)
  69. {
  70. mVisible = false;
  71. GuiControl *parent = getParent();
  72. Point2I localPoint = parent->globalToLocalCoord(event.mousePoint);
  73. GuiControl *tempControl = parent->findHitControl(localPoint);
  74. //the decoy control has the responsibility of keeping track of the decoyed controls status
  75. if( mDecoyReference != NULL && tempControl == mDecoyReference)
  76. tempControl->onMouseUp(event);
  77. else if(mDecoyReference != NULL && tempControl != mDecoyReference)
  78. {
  79. //as explained earlier, this control was written in the mindset for buttons.
  80. //nothing bad should ever happen if not a button due to the checks in this function though.
  81. GuiButtonBaseCtrl *unCastCtrl = NULL;
  82. unCastCtrl = dynamic_cast<GuiButtonBaseCtrl *>( mDecoyReference );
  83. if(unCastCtrl != NULL)
  84. unCastCtrl->resetState();
  85. }
  86. mVisible = true;
  87. }
  88. }
  89. void GuiDecoyCtrl::onMouseDown(const GuiEvent &event)
  90. {
  91. if ( !mVisible || !mAwake )
  92. return;
  93. mouseLock();
  94. if(mIsDecoy == true)
  95. {
  96. mVisible = false;
  97. GuiControl *parent = getParent();
  98. Point2I localPoint = parent->globalToLocalCoord(event.mousePoint);
  99. GuiControl *tempControl = parent->findHitControl(localPoint);
  100. tempControl->onMouseDown(event);
  101. mVisible = true;
  102. }
  103. execConsoleCallback();
  104. setUpdate();
  105. }
  106. void GuiDecoyCtrl::onMouseMove(const GuiEvent &event)
  107. {
  108. //if this control is a dead end, make sure the event stops here
  109. if ( !mVisible || !mAwake )
  110. return;
  111. //pass the event to the parent
  112. GuiControl *parent = getParent();
  113. if ( parent )
  114. parent->onMouseMove( event );
  115. Point2I localPoint = parent->globalToLocalCoord(event.mousePoint);
  116. //also pretty hacky. since guiCanvas, *NOT* GuiControl, distributes the calls for onMouseEnter
  117. //and onMouseLeave, we simulate those calls here through a series of checks.
  118. if(mIsDecoy == true)
  119. {
  120. mVisible = false;
  121. GuiControl *tempControl = parent->findHitControl(localPoint);
  122. //the decoy control has the responsibility of keeping track of the decoyed controls status
  123. if(mMouseOverDecoy == false && mDecoyReference != NULL &&
  124. !mDecoyReference->isDeleted() && !mDecoyReference->isRemoved())
  125. {
  126. tempControl->onMouseEnter(event);
  127. mMouseOverDecoy = true;
  128. }
  129. else if(tempControl != mDecoyReference && mDecoyReference != NULL &&
  130. !mDecoyReference->isDeleted() && !mDecoyReference->isRemoved())
  131. {
  132. mDecoyReference->onMouseLeave(event);
  133. mMouseOverDecoy = false;
  134. }
  135. mDecoyReference = tempControl;
  136. mVisible = true;
  137. }
  138. }
  139. void GuiDecoyCtrl::onMouseDragged(const GuiEvent &event)
  140. {
  141. }
  142. void GuiDecoyCtrl::onMouseEnter(const GuiEvent &event)
  143. {
  144. if ( !mVisible || !mAwake )
  145. return;
  146. setUpdate();
  147. Con::executef( this , "onMouseEnter" );
  148. mMouseOver = true;
  149. }
  150. void GuiDecoyCtrl::onMouseLeave(const GuiEvent &event)
  151. {
  152. if ( !mVisible || !mAwake )
  153. return;
  154. setUpdate();
  155. Con::executef( this , "onMouseLeave" );
  156. mMouseOver = false;
  157. }
  158. bool GuiDecoyCtrl::onMouseWheelUp( const GuiEvent &event )
  159. {
  160. //if this control is a dead end, make sure the event stops here
  161. if ( !mVisible || !mAwake )
  162. return true;
  163. //pass the event to the parent
  164. GuiControl *parent = getParent();
  165. if ( parent )
  166. return parent->onMouseWheelUp( event );
  167. else
  168. return false;
  169. }
  170. bool GuiDecoyCtrl::onMouseWheelDown( const GuiEvent &event )
  171. {
  172. //if this control is a dead end, make sure the event stops here
  173. if ( !mVisible || !mAwake )
  174. return true;
  175. //pass the event to the parent
  176. GuiControl *parent = getParent();
  177. if ( parent )
  178. return parent->onMouseWheelDown( event );
  179. else
  180. return false;
  181. }
  182. void GuiDecoyCtrl::onRightMouseDown(const GuiEvent &)
  183. {
  184. }
  185. void GuiDecoyCtrl::onRightMouseUp(const GuiEvent &)
  186. {
  187. }
  188. void GuiDecoyCtrl::onRightMouseDragged(const GuiEvent &)
  189. {
  190. }
  191. void GuiDecoyCtrl::onMiddleMouseDown(const GuiEvent &)
  192. {
  193. }
  194. void GuiDecoyCtrl::onMiddleMouseUp(const GuiEvent &)
  195. {
  196. }
  197. void GuiDecoyCtrl::onMiddleMouseDragged(const GuiEvent &)
  198. {
  199. }