guiDecoyCtrl.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. {
  124. tempControl->onMouseEnter(event);
  125. mMouseOverDecoy = true;
  126. }
  127. else if(tempControl != mDecoyReference && mDecoyReference != NULL)
  128. {
  129. mDecoyReference->onMouseLeave(event);
  130. mMouseOverDecoy = false;
  131. }
  132. mDecoyReference = tempControl;
  133. mVisible = true;
  134. }
  135. }
  136. void GuiDecoyCtrl::onMouseDragged(const GuiEvent &event)
  137. {
  138. }
  139. void GuiDecoyCtrl::onMouseEnter(const GuiEvent &event)
  140. {
  141. if ( !mVisible || !mAwake )
  142. return;
  143. setUpdate();
  144. Con::executef( this , "onMouseEnter" );
  145. mMouseOver = true;
  146. }
  147. void GuiDecoyCtrl::onMouseLeave(const GuiEvent &event)
  148. {
  149. if ( !mVisible || !mAwake )
  150. return;
  151. setUpdate();
  152. Con::executef( this , "onMouseLeave" );
  153. mMouseOver = false;
  154. }
  155. bool GuiDecoyCtrl::onMouseWheelUp( const GuiEvent &event )
  156. {
  157. //if this control is a dead end, make sure the event stops here
  158. if ( !mVisible || !mAwake )
  159. return true;
  160. //pass the event to the parent
  161. GuiControl *parent = getParent();
  162. if ( parent )
  163. return parent->onMouseWheelUp( event );
  164. else
  165. return false;
  166. }
  167. bool GuiDecoyCtrl::onMouseWheelDown( const GuiEvent &event )
  168. {
  169. //if this control is a dead end, make sure the event stops here
  170. if ( !mVisible || !mAwake )
  171. return true;
  172. //pass the event to the parent
  173. GuiControl *parent = getParent();
  174. if ( parent )
  175. return parent->onMouseWheelDown( event );
  176. else
  177. return false;
  178. }
  179. void GuiDecoyCtrl::onRightMouseDown(const GuiEvent &)
  180. {
  181. }
  182. void GuiDecoyCtrl::onRightMouseUp(const GuiEvent &)
  183. {
  184. }
  185. void GuiDecoyCtrl::onRightMouseDragged(const GuiEvent &)
  186. {
  187. }
  188. void GuiDecoyCtrl::onMiddleMouseDown(const GuiEvent &)
  189. {
  190. }
  191. void GuiDecoyCtrl::onMiddleMouseUp(const GuiEvent &)
  192. {
  193. }
  194. void GuiDecoyCtrl::onMiddleMouseDragged(const GuiEvent &)
  195. {
  196. }