guiFormCtrl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 "console/engineAPI.h"
  23. #include "platform/platform.h"
  24. #include "gui/containers/guiFormCtrl.h"
  25. #include "gui/core/guiDefaultControlRender.h"
  26. #include "gfx/gfxDrawUtil.h"
  27. #ifdef TORQUE_TOOLS
  28. IMPLEMENT_CONOBJECT(GuiFormCtrl);
  29. ConsoleDocClass( GuiFormCtrl,
  30. "@brief A generic form control.\n\n"
  31. "Currently editor use only.\n\n "
  32. "@internal"
  33. );
  34. IMPLEMENT_CALLBACK( GuiFormCtrl, onResize, void, (), (),
  35. "Called when the control is resized." );
  36. GuiFormCtrl::GuiFormCtrl()
  37. {
  38. setMinExtent(Point2I(200,100));
  39. mActive = true;
  40. mMouseOver = false;
  41. mDepressed = false;
  42. mCanMove = false;
  43. mCaption = "[none]";
  44. mUseSmallCaption = false;
  45. mContentLibrary = StringTable->insert("");
  46. mContent = StringTable->insert("");
  47. mCanSaveFieldDictionary = true;
  48. mIsContainer = true;
  49. // The attached menu bar
  50. mHasMenu = false;
  51. mMenuBar = NULL;
  52. }
  53. GuiFormCtrl::~GuiFormCtrl()
  54. {
  55. // If we still have a menu bar, delete it.
  56. if( mMenuBar )
  57. mMenuBar->deleteObject();
  58. }
  59. bool GuiFormCtrl::_setHasMenu( void *object, const char *index, const char *data )
  60. {
  61. GuiFormCtrl* ctrl = reinterpret_cast< GuiFormCtrl* >( object );
  62. ctrl->setHasMenu( dAtob( data ) );
  63. return false;
  64. }
  65. void GuiFormCtrl::initPersistFields()
  66. {
  67. addField("caption", TypeRealString, Offset(mCaption, GuiFormCtrl));
  68. addField("contentLibrary",TypeString, Offset(mContentLibrary, GuiFormCtrl));
  69. addField("content", TypeString, Offset(mContent, GuiFormCtrl));
  70. addField("movable", TypeBool, Offset(mCanMove, GuiFormCtrl));
  71. addProtectedField( "hasMenu", TypeBool, Offset(mHasMenu, GuiFormCtrl),
  72. &_setHasMenu, &defaultProtectedGetFn,
  73. "" );
  74. Parent::initPersistFields();
  75. }
  76. void GuiFormCtrl::setHasMenu( bool value )
  77. {
  78. if( mHasMenu == value )
  79. return;
  80. if( !value )
  81. {
  82. mMenuBar->deleteObject();
  83. mMenuBar = NULL;
  84. }
  85. else
  86. {
  87. if( !mMenuBar )
  88. {
  89. mMenuBar = new GuiMenuBar();
  90. mMenuBar->setField( "profile", "GuiFormMenuBarProfile" );
  91. mMenuBar->setField( "horizSizing", "right" );
  92. mMenuBar->setField( "vertSizing", "bottom" );
  93. mMenuBar->setField( "extent", "16 16" );
  94. mMenuBar->setField( "minExtent", "16 16" );
  95. mMenuBar->setField( "position", "0 0" );
  96. mMenuBar->setField( "class", "FormMenuBarClass "); // Give a generic class to the menu bar so that one set of functions may be used for all of them.
  97. mMenuBar->registerObject();
  98. mMenuBar->setProcessTicks(true); // Activate the processing of ticks to track if the mouse pointer has been hovering within the menu
  99. }
  100. addObject( mMenuBar ); // Add the menu bar to the form
  101. }
  102. mHasMenu = value;
  103. }
  104. bool GuiFormCtrl::onWake()
  105. {
  106. if ( !Parent::onWake() )
  107. return false;
  108. mFont = mProfile->mFont;
  109. AssertFatal(mFont, "GuiFormCtrl::onWake: invalid font in profile" );
  110. mProfile->constructBitmapArray();
  111. if(mProfile->mUseBitmapArray && mProfile->mBitmapArrayRects.size())
  112. {
  113. mThumbSize.set( mProfile->mBitmapArrayRects[0].extent.x, mProfile->mBitmapArrayRects[0].extent.y );
  114. mThumbSize.setMax( mProfile->mBitmapArrayRects[1].extent );
  115. if(mFont->getHeight() > mThumbSize.y)
  116. mThumbSize.y = mFont->getHeight();
  117. }
  118. else
  119. {
  120. mThumbSize.set(20, 20);
  121. }
  122. return true;
  123. }
  124. void GuiFormCtrl::addObject(SimObject *newObj )
  125. {
  126. if( ( mHasMenu && size() > 1) || (!mHasMenu && size() > 0 ) )
  127. {
  128. Con::warnf("GuiFormCtrl::addObject - Forms may only have one *direct* child - Placing on Parent!");
  129. GuiControl* parent = getParent();
  130. if ( parent )
  131. parent->addObject( newObj );
  132. return;
  133. }
  134. GuiControl *newCtrl = dynamic_cast<GuiControl*>( newObj );
  135. GuiFormCtrl*formCtrl = dynamic_cast<GuiFormCtrl*>( newObj );
  136. if( newCtrl && formCtrl )
  137. newCtrl->setCanSave( true );
  138. else if ( newCtrl )
  139. newCtrl->setCanSave( false );
  140. Parent::addObject( newObj );
  141. }
  142. void GuiFormCtrl::removeObject( SimObject* object )
  143. {
  144. if( object == mMenuBar )
  145. {
  146. mHasMenu = false;
  147. mMenuBar = NULL;
  148. }
  149. Parent::removeObject( object );
  150. }
  151. bool GuiFormCtrl::acceptsAsChild( SimObject* object ) const
  152. {
  153. return Parent::acceptsAsChild( object ) &&
  154. ( ( mHasMenu && size() == 1 ) || ( !mHasMenu && !size() ) ); // Only accept a single child.
  155. }
  156. void GuiFormCtrl::onSleep()
  157. {
  158. Parent::onSleep();
  159. mFont = NULL;
  160. }
  161. bool GuiFormCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  162. {
  163. if( !Parent::resize(newPosition, newExtent) )
  164. return false;
  165. if( !mAwake || !mProfile->mBitmapArrayRects.size() )
  166. return false;
  167. // Should the caption be modified because the title bar is too small?
  168. S32 textWidth = mProfile->mFont->getStrWidth(mCaption);
  169. S32 newTextArea = getWidth() - mThumbSize.x - mProfile->mBitmapArrayRects[4].extent.x;
  170. if(newTextArea < textWidth)
  171. {
  172. static char buf[256];
  173. mUseSmallCaption = true;
  174. mSmallCaption = StringTable->insert("");
  175. S32 strlen = dStrlen((const char*)mCaption);
  176. for(S32 i=strlen; i>=0; --i)
  177. {
  178. dStrcpy(buf, "");
  179. dStrncat(buf, (const char*)mCaption, i);
  180. dStrcat(buf, "...");
  181. textWidth = mProfile->mFont->getStrWidth(buf);
  182. if(textWidth < newTextArea)
  183. {
  184. mSmallCaption = StringTable->insert(buf, true);
  185. break;
  186. }
  187. }
  188. } else
  189. {
  190. mUseSmallCaption = false;
  191. }
  192. onResize_callback();
  193. return true;
  194. }
  195. void GuiFormCtrl::onRender(Point2I offset, const RectI &updateRect)
  196. {
  197. // Fill in the control's child area
  198. RectI boundsRect(offset, getExtent());
  199. boundsRect.point.y += mThumbSize.y;
  200. boundsRect.extent.y -= mThumbSize.y;
  201. // draw the border of the form if specified
  202. if (mProfile->mOpaque)
  203. GFX->getDrawUtil()->drawRectFill(boundsRect, mProfile->mFillColor);
  204. if (mProfile->mBorder)
  205. renderBorder(boundsRect, mProfile);
  206. // If we don't have a child, put some text in the child area
  207. if( empty() )
  208. {
  209. GFX->getDrawUtil()->setBitmapModulation(ColorI(0,0,0));
  210. renderJustifiedText(boundsRect.point, boundsRect.extent, "[none]");
  211. }
  212. S32 textWidth = 0;
  213. // Draw our little bar, too
  214. if (mProfile->mBitmapArrayRects.size() >= 5)
  215. {
  216. GFX->getDrawUtil()->clearBitmapModulation();
  217. S32 barStart = offset.x + textWidth;
  218. S32 barTop = mThumbSize.y / 2 + offset.y - mProfile->mBitmapArrayRects[3].extent.y / 2;
  219. Point2I barOffset(barStart, barTop);
  220. // Draw the start of the bar...
  221. GFX->getDrawUtil()->drawBitmapStretchSR(mProfile->mTextureObject ,RectI(barOffset, mProfile->mBitmapArrayRects[2].extent), mProfile->mBitmapArrayRects[2] );
  222. // Now draw the middle...
  223. barOffset.x += mProfile->mBitmapArrayRects[2].extent.x;
  224. S32 barMiddleSize = (getExtent().x - (barOffset.x - offset.x)) - mProfile->mBitmapArrayRects[4].extent.x + 1;
  225. if (barMiddleSize > 0)
  226. {
  227. // We have to do this inset to prevent nasty stretching artifacts
  228. RectI foo = mProfile->mBitmapArrayRects[3];
  229. foo.inset(1,0);
  230. GFX->getDrawUtil()->drawBitmapStretchSR(
  231. mProfile->mTextureObject,
  232. RectI(barOffset, Point2I(barMiddleSize, mProfile->mBitmapArrayRects[3].extent.y)),
  233. foo
  234. );
  235. }
  236. // And the end
  237. barOffset.x += barMiddleSize;
  238. GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mTextureObject, RectI(barOffset, mProfile->mBitmapArrayRects[4].extent),
  239. mProfile->mBitmapArrayRects[4]);
  240. GFX->getDrawUtil()->setBitmapModulation((mMouseOver ? mProfile->mFontColorHL : mProfile->mFontColor));
  241. renderJustifiedText(Point2I(mThumbSize.x, 0) + offset, Point2I(getWidth() - mThumbSize.x - mProfile->mBitmapArrayRects[4].extent.x, mThumbSize.y), (mUseSmallCaption ? mSmallCaption : mCaption) );
  242. }
  243. // Render the children
  244. renderChildControls(offset, updateRect);
  245. }
  246. void GuiFormCtrl::onMouseMove(const GuiEvent &event)
  247. {
  248. Point2I localMove = globalToLocalCoord(event.mousePoint);
  249. // If we're clicking in the header then resize
  250. mMouseOver = (localMove.y < mThumbSize.y);
  251. if(isMouseLocked())
  252. mDepressed = mMouseOver;
  253. }
  254. void GuiFormCtrl::onMouseEnter(const GuiEvent &event)
  255. {
  256. setUpdate();
  257. if(isMouseLocked())
  258. {
  259. mDepressed = true;
  260. mMouseOver = true;
  261. }
  262. else
  263. {
  264. mMouseOver = true;
  265. }
  266. }
  267. void GuiFormCtrl::onMouseLeave(const GuiEvent &event)
  268. {
  269. setUpdate();
  270. if(isMouseLocked())
  271. mDepressed = false;
  272. mMouseOver = false;
  273. }
  274. void GuiFormCtrl::onMouseDown(const GuiEvent &event)
  275. {
  276. Point2I localClick = globalToLocalCoord(event.mousePoint);
  277. // If we're clicking in the header then resize
  278. if(localClick.y < mThumbSize.y)
  279. {
  280. mouseLock();
  281. mDepressed = true;
  282. mMouseMovingWin = mCanMove;
  283. //update
  284. setUpdate();
  285. }
  286. mOrigBounds = getBounds();
  287. mMouseDownPosition = event.mousePoint;
  288. if (mMouseMovingWin )
  289. {
  290. mouseLock();
  291. }
  292. else
  293. {
  294. GuiControl *ctrl = findHitControl(localClick);
  295. if (ctrl && ctrl != this)
  296. ctrl->onMouseDown(event);
  297. }
  298. }
  299. void GuiFormCtrl::onMouseUp(const GuiEvent &event)
  300. {
  301. // Make sure we only get events we ought to be getting...
  302. if (! mActive)
  303. return;
  304. mouseUnlock();
  305. setUpdate();
  306. // If we're clicking in the header then resize
  307. //if(localClick.y < mThumbSize.y && mDepressed)
  308. // setCollapsed(!mCollapsed);
  309. }
  310. DefineEngineMethod( GuiFormCtrl, getMenuID, S32, (),,
  311. "Get the ID of this form's menu.\n\n"
  312. "@return The ID of the form menu\n" )
  313. {
  314. return object->getMenuBarID();
  315. }
  316. U32 GuiFormCtrl::getMenuBarID()
  317. {
  318. return mMenuBar ? mMenuBar->getId() : 0;
  319. }
  320. DefineEngineMethod( GuiFormCtrl, setCaption, void, ( const char* caption ),,
  321. "Sets the title of the form.\n\n"
  322. "@param caption Form caption\n" )
  323. {
  324. object->setCaption( caption );
  325. }
  326. #endif