creator.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. mValue(0),
  39. mId(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. mTxtOffset(5),
  125. mRoot(0)
  126. {
  127. VECTOR_SET_ASSOCIATION(mNodeList);
  128. clear();
  129. }
  130. CreatorTree::~CreatorTree()
  131. {
  132. delete mRoot;
  133. }
  134. //------------------------------------------------------------------------------
  135. CreatorTree::Node * CreatorTree::createNode(const char * name, const char * value, bool group, Node * parent)
  136. {
  137. Node * node = new Node();
  138. node->mId = mCurId++;
  139. node->mName = name ? StringTable->insert(name) : 0;
  140. node->mValue = value ? StringTable->insert(value) : 0;
  141. node->mFlags.set(Node::Group, group);
  142. // add to the parent group
  143. if(parent)
  144. {
  145. node->mParent = parent;
  146. if(!addNode(parent, node))
  147. {
  148. delete node;
  149. return(0);
  150. }
  151. }
  152. return(node);
  153. }
  154. //------------------------------------------------------------------------------
  155. void CreatorTree::clear()
  156. {
  157. delete mRoot;
  158. mCurId = 0;
  159. mRoot = createNode(0, 0, true);
  160. mRoot->mFlags.set(Node::Root | Node::Expanded);
  161. mSize = Point2I(1,0);
  162. }
  163. //------------------------------------------------------------------------------
  164. bool CreatorTree::addNode(Node * parent, Node * node)
  165. {
  166. if(!parent->isGroup())
  167. return(false);
  168. //
  169. parent->mChildren.push_back(node);
  170. return(true);
  171. }
  172. //------------------------------------------------------------------------------
  173. CreatorTree::Node * CreatorTree::findNode(S32 id)
  174. {
  175. return(mRoot->find(id));
  176. }
  177. //------------------------------------------------------------------------------
  178. void CreatorTree::sort()
  179. {
  180. // groups then items by alpha
  181. }
  182. //------------------------------------------------------------------------------
  183. DefineConsoleMethod( CreatorTree, addGroup, S32, (S32 group, const char * name, const char * value), , "(string group, string name, string value)")
  184. {
  185. CreatorTree::Node * grp = object->findNode(group);
  186. if(!grp || !grp->isGroup())
  187. return(-1);
  188. // return same named group if found...
  189. for(U32 i = 0; i < grp->mChildren.size(); i++)
  190. if(!dStricmp(name, grp->mChildren[i]->mName))
  191. return(grp->mChildren[i]->mId);
  192. CreatorTree::Node * node = object->createNode(name, 0, true, grp);
  193. object->build();
  194. return(node ? node->getId() : -1);
  195. }
  196. DefineConsoleMethod( CreatorTree, addItem, S32, (S32 group, const char * name, const char * value), , "(Node group, string name, string value)")
  197. {
  198. CreatorTree::Node * grp = object->findNode(group);
  199. if(!grp || !grp->isGroup())
  200. return -1;
  201. CreatorTree::Node * node = object->createNode(name, value, false, grp);
  202. object->build();
  203. return(node ? node->getId() : -1);
  204. }
  205. //------------------------------------------------------------------------------
  206. DefineConsoleMethod( CreatorTree, fileNameMatch, bool, (const char * world, const char * type, const char * filename), , "(string world, string type, string filename)")
  207. {
  208. // argv[2] - world short
  209. // argv[3] - type short
  210. // argv[4] - filename
  211. // interior filenames
  212. // 0 - world short ('b', 'x', ...)
  213. // 1-> - type short ('towr', 'bunk', ...)
  214. U32 typeLen = dStrlen(type);
  215. if(dStrlen(filename) < (typeLen + 1))
  216. return(false);
  217. // world
  218. if(dToupper(filename[0]) != dToupper(world[0]))
  219. return(false);
  220. return(!dStrnicmp(filename+1, type, typeLen));
  221. }
  222. DefineConsoleMethod( CreatorTree, getSelected, S32, (), , "Return a handle to the currently selected item.")
  223. {
  224. return(object->getSelected());
  225. }
  226. DefineConsoleMethod( CreatorTree, isGroup, bool, (const char * group), , "(Group g)")
  227. {
  228. CreatorTree::Node * node = object->findNode(dAtoi(group));
  229. if(node && node->isGroup())
  230. return(true);
  231. return(false);
  232. }
  233. DefineConsoleMethod( CreatorTree, getName, const char*, (const char * item), , "(Node item)")
  234. {
  235. CreatorTree::Node * node = object->findNode(dAtoi(item));
  236. return(node ? node->mName : 0);
  237. }
  238. DefineConsoleMethod( CreatorTree, getValue, const char*, (S32 nodeValue), , "(Node n)")
  239. {
  240. CreatorTree::Node * node = object->findNode(nodeValue);
  241. return(node ? node->mValue : 0);
  242. }
  243. DefineConsoleMethod( CreatorTree, clear, void, (), , "Clear the tree.")
  244. {
  245. object->clear();
  246. }
  247. DefineConsoleMethod( CreatorTree, getParent, S32, (S32 nodeValue), , "(Node n)")
  248. {
  249. CreatorTree::Node * node = object->findNode(nodeValue);
  250. if(node && node->mParent)
  251. return(node->mParent->getId());
  252. return(-1);
  253. }
  254. //------------------------------------------------------------------------------
  255. void CreatorTree::buildNode(Node * node, U32 tab)
  256. {
  257. if(node->isExpanded())
  258. for(U32 i = 0; i < node->mChildren.size(); i++)
  259. {
  260. Node * child = node->mChildren[i];
  261. child->mTab = tab;
  262. child->select(false);
  263. mNodeList.push_back(child);
  264. // grab width
  265. if(bool(mProfile->mFont) && child->mName)
  266. {
  267. S32 width = (tab + 1) * mTabSize + mProfile->mFont->getStrWidth(child->mName) + mTxtOffset;
  268. if(width > mMaxWidth)
  269. mMaxWidth = width;
  270. }
  271. if(node->mChildren[i]->isGroup())
  272. buildNode(node->mChildren[i], tab+1);
  273. }
  274. }
  275. //------------------------------------------------------------------------------
  276. void CreatorTree::build()
  277. {
  278. mMaxWidth = 0;
  279. mNodeList.clear();
  280. buildNode(mRoot, 0);
  281. mCellSize.set( mMaxWidth + 1, 11 );
  282. setSize(Point2I(1, mNodeList.size()));
  283. }
  284. //------------------------------------------------------------------------------
  285. bool CreatorTree::onWake()
  286. {
  287. if(!Parent::onWake())
  288. return(false);
  289. mTabSize = 11;
  290. //
  291. build();
  292. mCellSize.set( mMaxWidth + 1, 11 );
  293. setSize(Point2I(1, mNodeList.size()));
  294. return true;
  295. }
  296. //------------------------------------------------------------------------------
  297. void CreatorTree::onMouseUp(const GuiEvent & event)
  298. {
  299. onAction();
  300. }
  301. void CreatorTree::onMouseDown(const GuiEvent & event)
  302. {
  303. Point2I pos = globalToLocalCoord(event.mousePoint);
  304. bool dblClick = event.mouseClickCount > 1;
  305. // determine cell
  306. Point2I cell(pos.x < 0 ? -1 : pos.x / mCellSize.x, pos.y < 0 ? -1 : pos.y / mCellSize.y);
  307. if(cell.x >= 0 && cell.x < mSize.x && cell.y >= 0 && cell.y < mSize.y)
  308. {
  309. Node * node = mNodeList[cell.y];
  310. S32 offset = mTabSize * node->mTab;
  311. if(node->isGroup() && node->mChildren.size() && pos.x >= offset && pos.x <= (offset + mTabSize))
  312. {
  313. node->expand(!node->isExpanded());
  314. build();
  315. dblClick = false;
  316. }
  317. if(pos.x >= offset)
  318. {
  319. if(dblClick)
  320. node->expand(!node->isExpanded());
  321. build();
  322. node->select(true);
  323. }
  324. }
  325. }
  326. //------------------------------------------------------------------------------
  327. void CreatorTree::onMouseDragged(const GuiEvent & event)
  328. {
  329. TORQUE_UNUSED(event);
  330. }
  331. //------------------------------------------------------------------------------
  332. void CreatorTree::onRenderCell(Point2I offset, Point2I cell, bool, bool)
  333. {
  334. Point2I cellOffset = offset;
  335. Node *node = mNodeList[cell.y];
  336. // Get our points
  337. Point2I boxStart( cellOffset.x + mTabSize * node->mTab, cellOffset.y );
  338. boxStart.x += 2;
  339. boxStart.y += 1;
  340. Point2I boxEnd = Point2I( boxStart );
  341. boxEnd.x += 8;
  342. boxEnd.y += 8;
  343. GFXDrawUtil *drawer = GFX->getDrawUtil();
  344. // Start drawing stuff
  345. if( node->isGroup() )
  346. {
  347. // If we need a box...
  348. drawer->drawRectFill( boxStart, boxEnd, mProfile->mFillColor ); // Box background
  349. drawer->drawRect( boxStart, boxEnd, mProfile->mFontColor ); // Border
  350. // Cross line
  351. drawer->drawLine( boxStart.x + 2, boxStart.y + 4, boxStart.x + 7, boxStart.y + 4, mProfile->mFontColor );
  352. if( !node->isExpanded() ) // If it's a [+] draw down line
  353. drawer->drawLine( boxStart.x + 4, boxStart.y + 2, boxStart.x + 4, boxStart.y + 7, mProfile->mFontColor );
  354. }
  355. else
  356. {
  357. // Draw horizontal line
  358. drawer->drawLine( boxStart.x + 4, boxStart.y + 4, boxStart.x + 9, boxStart.y + 4, mProfile->mFontColor );
  359. if( !node->isLast() ) // If it's a continuing one, draw a long down line
  360. drawer->drawLine( boxStart.x + 4, boxStart.y - 6, boxStart.x + 4, boxStart.y + 10, mProfile->mFontColor );
  361. else // Otherwise, just a small one
  362. drawer->drawLine( boxStart.x + 4, boxStart.y - 2, boxStart.x + 4, boxStart.y + 4, mProfile->mFontColor );
  363. }
  364. //draw in all the required continuation lines
  365. Node *parent = node->mParent;
  366. while( !parent->isRoot() )
  367. {
  368. if( !parent->isLast() )
  369. {
  370. drawer->drawLine( cellOffset.x + ( parent->mTab * mTabSize ) + 6,
  371. cellOffset.y - 2,
  372. cellOffset.x + ( parent->mTab * mTabSize ) + 6,
  373. cellOffset.y + 11,
  374. mProfile->mFontColor );
  375. }
  376. parent = parent->mParent;
  377. }
  378. ColorI fontColor = mProfile->mFontColor;
  379. if( node->isSelected() )
  380. fontColor = mProfile->mFontColorHL;
  381. else if( node->isGroup() && node->hasChildItem() )
  382. fontColor.set( 128, 0, 0 );
  383. else if( !node->isGroup() )
  384. fontColor.set( 0, 0, 128 );
  385. drawer->setBitmapModulation(fontColor); //node->isSelected() ? mProfile->mFontColorHL : mProfile->mFontColor);
  386. drawer->drawText( mProfile->mFont,
  387. Point2I( offset.x + mTxtOffset + mTabSize * ( node->mTab + 1 ), offset.y ),
  388. node->mName);
  389. }