creator.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 "console/engineAPI.h"
  24. #include "gui/worldEditor/creator.h"
  25. #include "gfx/gfxDrawUtil.h"
  26. IMPLEMENT_CONOBJECT(CreatorTree);
  27. ConsoleDocClass( CreatorTree,
  28. "@brief Creator tree from old editor. Not used in current editor.\n\n"
  29. "@internal"
  30. );
  31. //------------------------------------------------------------------------------
  32. // Class CreatorTree::Node
  33. //------------------------------------------------------------------------------
  34. CreatorTree::Node::Node() :
  35. mFlags(0),
  36. mParent(0),
  37. mName(0),
  38. mId(0),
  39. mValue(0),
  40. mTab(0)
  41. {
  42. VECTOR_SET_ASSOCIATION(mChildren);
  43. }
  44. CreatorTree::Node::~Node()
  45. {
  46. for(U32 i = 0; i < mChildren.size(); i++)
  47. delete mChildren[i];
  48. }
  49. //------------------------------------------------------------------------------
  50. void CreatorTree::Node::expand(bool exp)
  51. {
  52. if(exp)
  53. {
  54. if(mParent)
  55. mParent->expand(exp);
  56. mFlags.set(Node::Expanded);
  57. }
  58. else if(!isRoot())
  59. {
  60. if(isGroup())
  61. for(U32 i = 0; i < mChildren.size(); i++)
  62. mChildren[i]->expand(exp);
  63. mFlags.clear(Selected);
  64. mFlags.clear(Expanded);
  65. }
  66. }
  67. //------------------------------------------------------------------------------
  68. CreatorTree::Node * CreatorTree::Node::find(S32 id)
  69. {
  70. if(mId == id)
  71. return(this);
  72. if(!isGroup())
  73. return(0);
  74. for(U32 i = 0; i < mChildren.size(); i++)
  75. {
  76. Node * node = mChildren[i]->find(id);
  77. if(node)
  78. return(node);
  79. }
  80. return(0);
  81. }
  82. //------------------------------------------------------------------------------
  83. bool CreatorTree::Node::isFirst()
  84. {
  85. AssertFatal(!isRoot(), "CreatorTree::Node::isFirst - cannot call on root node");
  86. return(this == mParent->mChildren[0]);
  87. }
  88. bool CreatorTree::Node::isLast()
  89. {
  90. AssertFatal(!isRoot(), "CreatorTree::Node::isLast - cannot call on root node");
  91. return(this == mParent->mChildren[mParent->mChildren.size()-1]);
  92. }
  93. bool CreatorTree::Node::hasChildItem()
  94. {
  95. for(U32 i = 0; i < mChildren.size(); i++)
  96. {
  97. if(mChildren[i]->isGroup() && mChildren[i]->hasChildItem())
  98. return(true);
  99. if(!mChildren[i]->isGroup())
  100. return(true);
  101. }
  102. return(false);
  103. }
  104. S32 CreatorTree::Node::getSelected()
  105. {
  106. for(U32 i = 0; i < mChildren.size(); i++)
  107. {
  108. if(mChildren[i]->isSelected())
  109. return(mChildren[i]->mId);
  110. else if(mChildren[i]->isGroup())
  111. {
  112. S32 ret = mChildren[i]->getSelected();
  113. if(ret != -1)
  114. return(ret);
  115. }
  116. }
  117. return(-1);
  118. }
  119. //------------------------------------------------------------------------------
  120. // Class CreatorTree
  121. //------------------------------------------------------------------------------
  122. CreatorTree::CreatorTree() :
  123. mCurId(0),
  124. mRoot(0),
  125. mTxtOffset(5),
  126. mTabSize(11),
  127. mMaxWidth(0)
  128. {
  129. VECTOR_SET_ASSOCIATION(mNodeList);
  130. clear();
  131. }
  132. CreatorTree::~CreatorTree()
  133. {
  134. delete mRoot;
  135. }
  136. //------------------------------------------------------------------------------
  137. CreatorTree::Node * CreatorTree::createNode(const char * name, const char * value, bool group, Node * parent)
  138. {
  139. Node * node = new Node();
  140. node->mId = mCurId++;
  141. node->mName = name ? StringTable->insert(name) : 0;
  142. node->mValue = value ? StringTable->insert(value) : 0;
  143. node->mFlags.set(Node::Group, group);
  144. // add to the parent group
  145. if(parent)
  146. {
  147. node->mParent = parent;
  148. if(!addNode(parent, node))
  149. {
  150. delete node;
  151. return(0);
  152. }
  153. }
  154. return(node);
  155. }
  156. //------------------------------------------------------------------------------
  157. void CreatorTree::clear()
  158. {
  159. delete mRoot;
  160. mCurId = 0;
  161. mRoot = createNode(0, 0, true);
  162. mRoot->mFlags.set(Node::Root | Node::Expanded);
  163. mSize = Point2I(1,0);
  164. }
  165. //------------------------------------------------------------------------------
  166. bool CreatorTree::addNode(Node * parent, Node * node)
  167. {
  168. if(!parent->isGroup())
  169. return(false);
  170. //
  171. parent->mChildren.push_back(node);
  172. return(true);
  173. }
  174. //------------------------------------------------------------------------------
  175. CreatorTree::Node * CreatorTree::findNode(S32 id)
  176. {
  177. return(mRoot->find(id));
  178. }
  179. //------------------------------------------------------------------------------
  180. void CreatorTree::sort()
  181. {
  182. // groups then items by alpha
  183. }
  184. //------------------------------------------------------------------------------
  185. DefineEngineMethod( CreatorTree, addGroup, S32, (S32 group, const char * name, const char * value), , "(string group, string name, string value)")
  186. {
  187. CreatorTree::Node * grp = object->findNode(group);
  188. if(!grp || !grp->isGroup())
  189. return(-1);
  190. // return same named group if found...
  191. for(U32 i = 0; i < grp->mChildren.size(); i++)
  192. if(!dStricmp(name, grp->mChildren[i]->mName))
  193. return(grp->mChildren[i]->mId);
  194. CreatorTree::Node * node = object->createNode(name, 0, true, grp);
  195. object->build();
  196. return(node ? node->getId() : -1);
  197. }
  198. DefineEngineMethod( CreatorTree, addItem, S32, (S32 group, const char * name, const char * value), , "(Node group, string name, string value)")
  199. {
  200. CreatorTree::Node * grp = object->findNode(group);
  201. if(!grp || !grp->isGroup())
  202. return -1;
  203. CreatorTree::Node * node = object->createNode(name, value, false, grp);
  204. object->build();
  205. return(node ? node->getId() : -1);
  206. }
  207. //------------------------------------------------------------------------------
  208. DefineEngineMethod( CreatorTree, fileNameMatch, bool, (const char * world, const char * type, const char * filename), , "(string world, string type, string filename)")
  209. {
  210. // argv[2] - world short
  211. // argv[3] - type short
  212. // argv[4] - filename
  213. // interior filenames
  214. // 0 - world short ('b', 'x', ...)
  215. // 1-> - type short ('towr', 'bunk', ...)
  216. U32 typeLen = dStrlen(type);
  217. if(dStrlen(filename) < (typeLen + 1))
  218. return(false);
  219. // world
  220. if(dToupper(filename[0]) != dToupper(world[0]))
  221. return(false);
  222. return(!dStrnicmp(filename+1, type, typeLen));
  223. }
  224. DefineEngineMethod( CreatorTree, getSelected, S32, (), , "Return a handle to the currently selected item.")
  225. {
  226. return(object->getSelected());
  227. }
  228. DefineEngineMethod( CreatorTree, isGroup, bool, (const char * group), , "(Group g)")
  229. {
  230. CreatorTree::Node * node = object->findNode(dAtoi(group));
  231. if(node && node->isGroup())
  232. return(true);
  233. return(false);
  234. }
  235. DefineEngineMethod( CreatorTree, getName, const char*, (const char * item), , "(Node item)")
  236. {
  237. CreatorTree::Node * node = object->findNode(dAtoi(item));
  238. return(node ? node->mName : 0);
  239. }
  240. DefineEngineMethod( CreatorTree, getValue, const char*, (S32 nodeValue), , "(Node n)")
  241. {
  242. CreatorTree::Node * node = object->findNode(nodeValue);
  243. return(node ? node->mValue : 0);
  244. }
  245. DefineEngineMethod( CreatorTree, clear, void, (), , "Clear the tree.")
  246. {
  247. object->clear();
  248. }
  249. DefineEngineMethod( CreatorTree, getParent, S32, (S32 nodeValue), , "(Node n)")
  250. {
  251. CreatorTree::Node * node = object->findNode(nodeValue);
  252. if(node && node->mParent)
  253. return(node->mParent->getId());
  254. return(-1);
  255. }
  256. //------------------------------------------------------------------------------
  257. void CreatorTree::buildNode(Node * node, U32 tab)
  258. {
  259. if(node->isExpanded())
  260. for(U32 i = 0; i < node->mChildren.size(); i++)
  261. {
  262. Node * child = node->mChildren[i];
  263. child->mTab = tab;
  264. child->select(false);
  265. mNodeList.push_back(child);
  266. // grab width
  267. if(bool(mProfile->mFont) && child->mName)
  268. {
  269. S32 width = (tab + 1) * mTabSize + mProfile->mFont->getStrWidth(child->mName) + mTxtOffset;
  270. if(width > mMaxWidth)
  271. mMaxWidth = width;
  272. }
  273. if(node->mChildren[i]->isGroup())
  274. buildNode(node->mChildren[i], tab+1);
  275. }
  276. }
  277. //------------------------------------------------------------------------------
  278. void CreatorTree::build()
  279. {
  280. mMaxWidth = 0;
  281. mNodeList.clear();
  282. buildNode(mRoot, 0);
  283. mCellSize.set( mMaxWidth + 1, 11 );
  284. setSize(Point2I(1, mNodeList.size()));
  285. }
  286. //------------------------------------------------------------------------------
  287. bool CreatorTree::onWake()
  288. {
  289. if(!Parent::onWake())
  290. return(false);
  291. mTabSize = 11;
  292. //
  293. build();
  294. mCellSize.set( mMaxWidth + 1, 11 );
  295. setSize(Point2I(1, mNodeList.size()));
  296. return true;
  297. }
  298. //------------------------------------------------------------------------------
  299. void CreatorTree::onMouseUp(const GuiEvent & event)
  300. {
  301. onAction();
  302. }
  303. void CreatorTree::onMouseDown(const GuiEvent & event)
  304. {
  305. Point2I pos = globalToLocalCoord(event.mousePoint);
  306. bool dblClick = event.mouseClickCount > 1;
  307. // determine cell
  308. Point2I cell(pos.x < 0 ? -1 : pos.x / mCellSize.x, pos.y < 0 ? -1 : pos.y / mCellSize.y);
  309. if(cell.x >= 0 && cell.x < mSize.x && cell.y >= 0 && cell.y < mSize.y)
  310. {
  311. Node * node = mNodeList[cell.y];
  312. S32 offset = mTabSize * node->mTab;
  313. if(node->isGroup() && node->mChildren.size() && pos.x >= offset && pos.x <= (offset + mTabSize))
  314. {
  315. node->expand(!node->isExpanded());
  316. build();
  317. dblClick = false;
  318. }
  319. if(pos.x >= offset)
  320. {
  321. if(dblClick)
  322. node->expand(!node->isExpanded());
  323. build();
  324. node->select(true);
  325. }
  326. }
  327. }
  328. //------------------------------------------------------------------------------
  329. void CreatorTree::onMouseDragged(const GuiEvent & event)
  330. {
  331. TORQUE_UNUSED(event);
  332. }
  333. //------------------------------------------------------------------------------
  334. void CreatorTree::onRenderCell(Point2I offset, Point2I cell, bool, bool)
  335. {
  336. Point2I cellOffset = offset;
  337. Node *node = mNodeList[cell.y];
  338. // Get our points
  339. Point2I boxStart( cellOffset.x + mTabSize * node->mTab, cellOffset.y );
  340. boxStart.x += 2;
  341. boxStart.y += 1;
  342. Point2I boxEnd = Point2I( boxStart );
  343. boxEnd.x += 8;
  344. boxEnd.y += 8;
  345. GFXDrawUtil *drawer = GFX->getDrawUtil();
  346. // Start drawing stuff
  347. if( node->isGroup() )
  348. {
  349. // If we need a box...
  350. drawer->drawRectFill( boxStart, boxEnd, mProfile->mFillColor ); // Box background
  351. drawer->drawRect( boxStart, boxEnd, mProfile->mFontColor ); // Border
  352. // Cross line
  353. drawer->drawLine( boxStart.x + 2, boxStart.y + 4, boxStart.x + 7, boxStart.y + 4, mProfile->mFontColor );
  354. if( !node->isExpanded() ) // If it's a [+] draw down line
  355. drawer->drawLine( boxStart.x + 4, boxStart.y + 2, boxStart.x + 4, boxStart.y + 7, mProfile->mFontColor );
  356. }
  357. else
  358. {
  359. // Draw horizontal line
  360. drawer->drawLine( boxStart.x + 4, boxStart.y + 4, boxStart.x + 9, boxStart.y + 4, mProfile->mFontColor );
  361. if( !node->isLast() ) // If it's a continuing one, draw a long down line
  362. drawer->drawLine( boxStart.x + 4, boxStart.y - 6, boxStart.x + 4, boxStart.y + 10, mProfile->mFontColor );
  363. else // Otherwise, just a small one
  364. drawer->drawLine( boxStart.x + 4, boxStart.y - 2, boxStart.x + 4, boxStart.y + 4, mProfile->mFontColor );
  365. }
  366. //draw in all the required continuation lines
  367. Node *parent = node->mParent;
  368. while( !parent->isRoot() )
  369. {
  370. if( !parent->isLast() )
  371. {
  372. drawer->drawLine( cellOffset.x + ( parent->mTab * mTabSize ) + 6,
  373. cellOffset.y - 2,
  374. cellOffset.x + ( parent->mTab * mTabSize ) + 6,
  375. cellOffset.y + 11,
  376. mProfile->mFontColor );
  377. }
  378. parent = parent->mParent;
  379. }
  380. ColorI fontColor = mProfile->mFontColor;
  381. if( node->isSelected() )
  382. fontColor = mProfile->mFontColorHL;
  383. else if( node->isGroup() && node->hasChildItem() )
  384. fontColor.set( 128, 0, 0 );
  385. else if( !node->isGroup() )
  386. fontColor.set( 0, 0, 128 );
  387. drawer->setBitmapModulation(fontColor); //node->isSelected() ? mProfile->mFontColorHL : mProfile->mFontColor);
  388. drawer->drawText( mProfile->mFont,
  389. Point2I( offset.x + mTxtOffset + mTabSize * ( node->mTab + 1 ), offset.y ),
  390. node->mName);
  391. }