VEditorButton.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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()
  37. {
  38. docsURL;
  39. Parent::initPersistFields();
  40. addField( "IsDraggable", TypeBool, Offset( mIsDraggable, VEditorButton ) );
  41. }
  42. //-----------------------------------------------------------------------------
  43. void VEditorButton::onMouseDown( const GuiEvent &pEvent )
  44. {
  45. if ( !mActive )
  46. {
  47. return;
  48. }
  49. Parent::onMouseDown( pEvent );
  50. onMouseEvent( "onMouseDown", pEvent );
  51. }
  52. void VEditorButton::onMouseUp( const GuiEvent &pEvent )
  53. {
  54. if ( !mActive )
  55. {
  56. return;
  57. }
  58. Parent::onMouseUp( pEvent );
  59. if ( mIsDraggable && isMouseLocked() )
  60. {
  61. // Unlock.
  62. mouseUnlock();
  63. }
  64. onMouseEvent( "onMouseUp", pEvent );
  65. }
  66. void VEditorButton::onMouseDragged( const GuiEvent &pEvent )
  67. {
  68. if ( !mActive || !mIsDraggable )
  69. {
  70. return;
  71. }
  72. Parent::onMouseDragged( pEvent );
  73. if ( !isMouseLocked() )
  74. {
  75. GuiCanvas *canvas = getRoot();
  76. if ( canvas->getMouseLockedControl() )
  77. {
  78. GuiEvent event;
  79. canvas->getMouseLockedControl()->onMouseLeave( event );
  80. canvas->mouseUnlock( canvas->getMouseLockedControl() );
  81. }
  82. // Lock.
  83. mouseLock();
  84. }
  85. onMouseEvent( "onMouseDragged", pEvent );
  86. }
  87. void VEditorButton::onRightMouseDown( const GuiEvent &pEvent )
  88. {
  89. if ( !mActive )
  90. {
  91. return;
  92. }
  93. Parent::onRightMouseDown( pEvent );
  94. onMouseEvent( "onRightMouseDown", pEvent );
  95. }
  96. void VEditorButton::onRightMouseUp( const GuiEvent &pEvent )
  97. {
  98. if ( !mActive )
  99. {
  100. return;
  101. }
  102. Parent::onRightMouseUp( pEvent );
  103. onMouseEvent( "onRightMouseUp", pEvent );
  104. }
  105. void VEditorButton::onMouseEnter( const GuiEvent &pEvent )
  106. {
  107. if ( !mActive )
  108. {
  109. return;
  110. }
  111. Parent::onMouseEnter( pEvent );
  112. onMouseEvent( "onMouseEnter", pEvent );
  113. }
  114. void VEditorButton::onMouseLeave( const GuiEvent &pEvent )
  115. {
  116. if ( !mActive )
  117. {
  118. return;
  119. }
  120. Parent::onMouseLeave( pEvent );
  121. onMouseEvent( "onMouseLeave", pEvent );
  122. }
  123. void VEditorButton::onMouseEvent( const char *pEventName, const GuiEvent &pEvent )
  124. {
  125. // Argument Buffers.
  126. char argBuffer[3][32];
  127. // Format Event-Position Buffer.
  128. dSprintf( argBuffer[0], 32, "%d %d", pEvent.mousePoint.x, pEvent.mousePoint.y );
  129. // Format Event-Modifier Buffer.
  130. dSprintf( argBuffer[1], 32, "%d", pEvent.modifier );
  131. // Format Mouse-Click Count Buffer.
  132. dSprintf( argBuffer[2], 32, "%d", pEvent.mouseClickCount );
  133. // Call Scripts.
  134. Con::executef( this, pEventName, argBuffer[0], argBuffer[1], argBuffer[2] );
  135. }
  136. //-----------------------------------------------------------------------------
  137. void VEditorButton::onRender( Point2I offset, const RectI& updateRect )
  138. {
  139. // Fetch Texture.
  140. GFXTexHandle texture = getTextureForCurrentState();
  141. // Valid?
  142. if ( texture )
  143. {
  144. GFX->getDrawUtil()->clearBitmapModulation();
  145. GFX->getDrawUtil()->drawBitmapStretch( texture, RectI( offset, getExtent() ) );
  146. }
  147. else
  148. {
  149. if ( mProfile->mBorder != 0 )
  150. {
  151. RectI boundsRect( offset, getExtent() );
  152. if ( mDepressed || mStateOn || mHighlighted )
  153. {
  154. renderFilledBorder( boundsRect, mProfile->mBorderColorHL, mProfile->mFillColorHL );
  155. }
  156. else
  157. {
  158. renderFilledBorder( boundsRect, mProfile->mBorderColor, mProfile->mFillColor );
  159. }
  160. }
  161. }
  162. // Render Text.
  163. GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
  164. renderJustifiedText( offset + mProfile->mTextOffset, getExtent(), mButtonText );
  165. renderChildControls( offset, updateRect);
  166. }
  167. //-----------------------------------------------------------------------------
  168. //
  169. // Console Methods.
  170. //
  171. //-----------------------------------------------------------------------------
  172. DefineEngineMethod( VEditorButton, getState, bool, (), , "()" )
  173. {
  174. return object->getStateOn();
  175. }