VEditorButton.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "Verve/GUI/VEditorButton.h"
  24. #include "console/consoleTypes.h"
  25. #include "gfx/gfxDrawUtil.h"
  26. #include "gui/core/guiCanvas.h"
  27. #include "gui/core/guiDefaultControlRender.h"
  28. //-----------------------------------------------------------------------------
  29. IMPLEMENT_CONOBJECT( VEditorButton );
  30. //-----------------------------------------------------------------------------
  31. VEditorButton::VEditorButton( void ) :
  32. mIsDraggable( false )
  33. {
  34. // Void.
  35. }
  36. void VEditorButton::initPersistFields( void )
  37. {
  38. Parent::initPersistFields();
  39. addField( "IsDraggable", TypeBool, Offset( mIsDraggable, VEditorButton ) );
  40. }
  41. //-----------------------------------------------------------------------------
  42. void VEditorButton::onMouseDown( const GuiEvent &pEvent )
  43. {
  44. if ( !mActive )
  45. {
  46. return;
  47. }
  48. Parent::onMouseDown( pEvent );
  49. onMouseEvent( "onMouseDown", pEvent );
  50. }
  51. void VEditorButton::onMouseUp( const GuiEvent &pEvent )
  52. {
  53. if ( !mActive )
  54. {
  55. return;
  56. }
  57. Parent::onMouseUp( pEvent );
  58. if ( mIsDraggable && isMouseLocked() )
  59. {
  60. // Unlock.
  61. mouseUnlock();
  62. }
  63. onMouseEvent( "onMouseUp", pEvent );
  64. }
  65. void VEditorButton::onMouseDragged( const GuiEvent &pEvent )
  66. {
  67. if ( !mActive || !mIsDraggable )
  68. {
  69. return;
  70. }
  71. Parent::onMouseDragged( pEvent );
  72. if ( !isMouseLocked() )
  73. {
  74. GuiCanvas *canvas = getRoot();
  75. if ( canvas->getMouseLockedControl() )
  76. {
  77. GuiEvent event;
  78. canvas->getMouseLockedControl()->onMouseLeave( event );
  79. canvas->mouseUnlock( canvas->getMouseLockedControl() );
  80. }
  81. // Lock.
  82. mouseLock();
  83. }
  84. onMouseEvent( "onMouseDragged", pEvent );
  85. }
  86. void VEditorButton::onRightMouseDown( const GuiEvent &pEvent )
  87. {
  88. if ( !mActive )
  89. {
  90. return;
  91. }
  92. Parent::onRightMouseDown( pEvent );
  93. onMouseEvent( "onRightMouseDown", pEvent );
  94. }
  95. void VEditorButton::onRightMouseUp( const GuiEvent &pEvent )
  96. {
  97. if ( !mActive )
  98. {
  99. return;
  100. }
  101. Parent::onRightMouseUp( pEvent );
  102. onMouseEvent( "onRightMouseUp", pEvent );
  103. }
  104. void VEditorButton::onMouseEnter( const GuiEvent &pEvent )
  105. {
  106. if ( !mActive )
  107. {
  108. return;
  109. }
  110. Parent::onMouseEnter( pEvent );
  111. onMouseEvent( "onMouseEnter", pEvent );
  112. }
  113. void VEditorButton::onMouseLeave( const GuiEvent &pEvent )
  114. {
  115. if ( !mActive )
  116. {
  117. return;
  118. }
  119. Parent::onMouseLeave( pEvent );
  120. onMouseEvent( "onMouseLeave", pEvent );
  121. }
  122. void VEditorButton::onMouseEvent( const char *pEventName, const GuiEvent &pEvent )
  123. {
  124. // Argument Buffers.
  125. char argBuffer[3][32];
  126. // Format Event-Position Buffer.
  127. dSprintf( argBuffer[0], 32, "%d %d", pEvent.mousePoint.x, pEvent.mousePoint.y );
  128. // Format Event-Modifier Buffer.
  129. dSprintf( argBuffer[1], 32, "%d", pEvent.modifier );
  130. // Format Mouse-Click Count Buffer.
  131. dSprintf( argBuffer[2], 32, "%d", pEvent.mouseClickCount );
  132. // Call Scripts.
  133. Con::executef( this, pEventName, argBuffer[0], argBuffer[1], argBuffer[2] );
  134. }
  135. //-----------------------------------------------------------------------------
  136. void VEditorButton::onRender( Point2I offset, const RectI& updateRect )
  137. {
  138. // Fetch Texture.
  139. GFXTexHandle texture = getTextureForCurrentState();
  140. // Valid?
  141. if ( texture )
  142. {
  143. GFX->getDrawUtil()->clearBitmapModulation();
  144. GFX->getDrawUtil()->drawBitmapStretch( texture, RectI( offset, getExtent() ) );
  145. }
  146. else
  147. {
  148. if ( mProfile->mBorder != 0 )
  149. {
  150. RectI boundsRect( offset, getExtent() );
  151. if ( mDepressed || mStateOn || mHighlighted )
  152. {
  153. renderFilledBorder( boundsRect, mProfile->mBorderColorHL, mProfile->mFillColorHL );
  154. }
  155. else
  156. {
  157. renderFilledBorder( boundsRect, mProfile->mBorderColor, mProfile->mFillColor );
  158. }
  159. }
  160. }
  161. // Render Text.
  162. GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
  163. renderJustifiedText( offset + mProfile->mTextOffset, getExtent(), mButtonText );
  164. renderChildControls( offset, updateRect);
  165. }
  166. //-----------------------------------------------------------------------------
  167. //
  168. // Console Methods.
  169. //
  170. //-----------------------------------------------------------------------------
  171. DefineEngineMethod( VEditorButton, getState, bool, (), , "()" )
  172. {
  173. return object->getStateOn();
  174. }