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 "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. {
  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 *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. }