guiFormCtrl.cpp 11 KB

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