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() : mIsDecoy(true),
  48. mMouseOver(false),
  49. mDecoyReference(NULL)
  50. {
  51. }
  52. GuiDecoyCtrl::~GuiDecoyCtrl()
  53. {
  54. }
  55. void GuiDecoyCtrl::initPersistFields()
  56. {
  57. addField("isDecoy", TypeBool, Offset(mIsDecoy, GuiDecoyCtrl), "Sets this control to decoy mode");
  58. Parent::initPersistFields();
  59. }
  60. void GuiDecoyCtrl::onMouseUp(const GuiEvent &event)
  61. {
  62. mouseUnlock();
  63. setUpdate();
  64. //this code is pretty hacky. right now there is no way that guiCanvas will allow sending more than
  65. //one signal to one gui control at a time.
  66. if(mIsDecoy == true)
  67. {
  68. mVisible = false;
  69. GuiControl *parent = getParent();
  70. Point2I localPoint = parent->globalToLocalCoord(event.mousePoint);
  71. GuiControl *tempControl = parent->findHitControl(localPoint);
  72. //the decoy control has the responsibility of keeping track of the decoyed controls status
  73. if( mDecoyReference != NULL && tempControl == mDecoyReference)
  74. tempControl->onMouseUp(event);
  75. else if(mDecoyReference != NULL && tempControl != mDecoyReference)
  76. {
  77. //as explained earlier, this control was written in the mindset for buttons.
  78. //nothing bad should ever happen if not a button due to the checks in this function though.
  79. GuiButtonBaseCtrl *unCastCtrl = NULL;
  80. unCastCtrl = dynamic_cast<GuiButtonBaseCtrl *>( mDecoyReference );
  81. if(unCastCtrl != NULL)
  82. unCastCtrl->resetState();
  83. }
  84. mVisible = true;
  85. }
  86. }
  87. void GuiDecoyCtrl::onMouseDown(const GuiEvent &event)
  88. {
  89. if ( !mVisible || !mAwake )
  90. return;
  91. mouseLock();
  92. if(mIsDecoy == true)
  93. {
  94. mVisible = false;
  95. GuiControl *parent = getParent();
  96. Point2I localPoint = parent->globalToLocalCoord(event.mousePoint);
  97. GuiControl *tempControl = parent->findHitControl(localPoint);
  98. tempControl->onMouseDown(event);
  99. mVisible = true;
  100. }
  101. execConsoleCallback();
  102. setUpdate();
  103. }
  104. void GuiDecoyCtrl::onMouseMove(const GuiEvent &event)
  105. {
  106. //if this control is a dead end, make sure the event stops here
  107. if ( !mVisible || !mAwake )
  108. return;
  109. //pass the event to the parent
  110. GuiControl *parent = getParent();
  111. if ( parent )
  112. parent->onMouseMove( event );
  113. Point2I localPoint = parent->globalToLocalCoord(event.mousePoint);
  114. //also pretty hacky. since guiCanvas, *NOT* GuiControl, distributes the calls for onMouseEnter
  115. //and onMouseLeave, we simulate those calls here through a series of checks.
  116. if(mIsDecoy == true)
  117. {
  118. mVisible = false;
  119. GuiControl *parent = getParent();
  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. }