/bullet-2.78/Extras/glui/glui_treepanel.cpp

https://github.com/RudyAramayo/iOSOpenGLBulletPhysics · C++ · 388 lines · 281 code · 77 blank · 30 comment · 114 complexity · ad2d143b7494ec807a9a61448c213281 MD5 · raw file

  1. #include "GL/glui.h"
  2. /****************************** GLUI_TreePanel::GLUI_TreePanel() *********/
  3. GLUI_TreePanel::GLUI_TreePanel(GLUI_Node *parent, const char *name,
  4. bool open, int inset)
  5. {
  6. common_init();
  7. set_name( name );
  8. user_id = -1;
  9. if ( !open ) {
  10. is_open = false;
  11. h = GLUI_DEFAULT_CONTROL_HEIGHT + 7;
  12. }
  13. parent->add_control( this );
  14. }
  15. /****************************** GLUI_TreePanel::set_color() *********/
  16. void GLUI_TreePanel::set_color(float r, float g, float b)
  17. {
  18. red = r;
  19. green = g;
  20. blue = b;
  21. redraw();
  22. }
  23. /************************ GLUI_TreePanel::set_level_color() *********/
  24. void GLUI_TreePanel::set_level_color(float r, float g, float b)
  25. {
  26. lred = r;
  27. lgreen = g;
  28. lblue = b;
  29. redraw();
  30. }
  31. /****************************** GLUI_TreePanel::ab() *********/
  32. /* Adds branch to curr_root */
  33. GLUI_Tree *GLUI_TreePanel::ab(const char *name, GLUI_Tree *root)
  34. {
  35. GLUI_Tree *temp;
  36. if (root != NULL) {
  37. resetToRoot(root);
  38. }
  39. temp = new GLUI_Tree(curr_root, name);
  40. initNode(temp);
  41. formatNode(temp);
  42. curr_root = temp;
  43. curr_branch = NULL; /* Currently at leaf */
  44. if (temp->dynamicCastGLUI_Tree())
  45. ((GLUI_Tree *)temp)->set_current(true);
  46. //refresh();
  47. // glui->deactivate_current_control();
  48. //glui->activate_control( temp, GLUI_ACTIVATE_TAB );
  49. return temp;
  50. }
  51. /****************************** GLUI_TreePanel::fb() *********/
  52. /* Goes up one level, resets curr_root and curr_branch to parents*/
  53. void GLUI_TreePanel::fb(GLUI_Tree *branch)
  54. {
  55. if (((GLUI_Panel *)branch) == ((GLUI_Panel *)this))
  56. return;
  57. if (((GLUI_Panel *)curr_branch) == ((GLUI_Panel *)this)) {
  58. resetToRoot();
  59. return;
  60. }
  61. if (((GLUI_Panel *)curr_root) == ((GLUI_Panel *)this)) {
  62. resetToRoot();
  63. return;
  64. }
  65. if (branch != NULL) {
  66. if ( branch->dynamicCastGLUI_Tree() )
  67. ((GLUI_Tree *)branch)->set_current(false);
  68. curr_branch = (GLUI_Tree *)branch->next();
  69. curr_root = (GLUI_Panel *)branch->parent();
  70. if (curr_branch == NULL && (curr_root->collapsed_node).first_child() != NULL)
  71. curr_branch = (GLUI_Tree *)(curr_root->collapsed_node).first_child();
  72. if ( curr_root->dynamicCastGLUI_Tree() )
  73. ((GLUI_Tree *)curr_root)->set_current(true);
  74. } else {
  75. if (curr_root != NULL) { /* up one parent */
  76. if (curr_root->dynamicCastGLUI_Tree())
  77. ((GLUI_Tree *)curr_root)->set_current(false);
  78. curr_branch = (GLUI_Tree *) curr_root->next();
  79. curr_root = (GLUI_Panel *) curr_root->parent();
  80. if (curr_branch == NULL && (curr_root->collapsed_node).first_child() != NULL)
  81. curr_branch = (GLUI_Tree *)(curr_root->collapsed_node).first_child();
  82. if (curr_root->dynamicCastGLUI_Tree())
  83. ((GLUI_Tree *)curr_root)->set_current(true);
  84. }
  85. }
  86. //refresh();
  87. }
  88. /****************************** GLUI_TreePanel::refresh() *********/
  89. void GLUI_TreePanel::refresh()
  90. {
  91. glui->deactivate_current_control();
  92. glui->activate_control( curr_root, GLUI_ACTIVATE_TAB );
  93. redraw();
  94. }
  95. /****************************** GLUI_TreePanel::initNode() *********/
  96. void GLUI_TreePanel::initNode(GLUI_Tree *temp)
  97. {
  98. if (temp == NULL)
  99. return;
  100. int level = temp->get_level();
  101. int child_number = 1;
  102. GLUI_Tree *ptree = temp->parent()->dynamicCastGLUI_Tree();
  103. if (ptree) {
  104. level = ptree->get_level() + 1;
  105. GLUI_Tree *prevTree = temp->prev()->dynamicCastGLUI_Tree();
  106. if (prevTree) {
  107. child_number = prevTree->get_child_number() + 1;
  108. }
  109. } else if (temp->dynamicCastGLUI_Tree() &&
  110. temp->parent()->dynamicCastGLUI_TreePanel()) {
  111. child_number = ++root_children;
  112. }
  113. temp->set_id(uniqueID()); // -1 if unset
  114. temp->set_level(level);
  115. temp->set_child_number(child_number);
  116. }
  117. /****************************** GLUI_TreePanel::formatNode() *********/
  118. void GLUI_TreePanel::formatNode(GLUI_Tree *temp)
  119. {
  120. if (temp == NULL)
  121. return;
  122. int level = temp->get_level();
  123. int child_number = temp->get_child_number();
  124. GLUI_String level_name="";
  125. GLUI_String full_name="";
  126. temp->level_name == "";
  127. if (format & GLUI_TREEPANEL_DISPLAY_HIERARCHY) {
  128. if (format & GLUI_TREEPANEL_HIERARCHY_LEVEL_ONLY) {
  129. glui_format_str(level_name, "%d", level);
  130. }
  131. if (format & GLUI_TREEPANEL_HIERARCHY_NUMERICDOT) {
  132. if ( temp->parent()->dynamicCastGLUI_Tree() )
  133. glui_format_str(level_name, "%s.%d",
  134. ((GLUI_Tree *)(temp->parent()))->level_name.c_str(),
  135. child_number);
  136. else
  137. glui_format_str(level_name, "%d", child_number);
  138. }
  139. }
  140. temp->set_level_color(lred, lgreen, lblue);
  141. temp->set_format(format);
  142. temp->level_name = level_name;
  143. if (format & GLUI_TREEPANEL_ALTERNATE_COLOR) {
  144. switch (level%8) {
  145. case (7): temp->set_color(.5,.5,.5); break;
  146. case (6): temp->set_color(.3,.5,.5); break;
  147. case (5): temp->set_color(.5,.3,.5); break;
  148. case (4): temp->set_color(.3,.3,.5); break;
  149. case (3): temp->set_color(.5,.5,.3); break;
  150. case (2): temp->set_color(.3,.5,.3); break;
  151. case (1): temp->set_color(.5,.3,.3); break;
  152. default: temp->set_color(.3,.3,.3);
  153. }
  154. } else {
  155. temp->set_color(red,green,blue);
  156. }
  157. if (format & GLUI_TREEPANEL_DISABLE_BAR) {
  158. temp->disable_bar();
  159. } else {
  160. if (format & GLUI_TREEPANEL_DISABLE_DEEPEST_BAR) {
  161. temp->disable_bar();
  162. if ( curr_root->dynamicCastGLUI_Tree() )
  163. ((GLUI_Tree *)curr_root)->enable_bar();
  164. } else
  165. if (format & GLUI_TREEPANEL_CONNECT_CHILDREN_ONLY) {
  166. temp->disable_bar();
  167. if (temp->prev() && temp->prev()->dynamicCastGLUI_Tree() )
  168. {
  169. ((GLUI_Tree *)temp->prev())->enable_bar();
  170. }
  171. }
  172. }
  173. }
  174. /****************************** GLUI_TreePanel::update_all() *********/
  175. void GLUI_TreePanel::update_all()
  176. {
  177. printf("GLUI_TreePanel::update_all() doesn't work yet. - JVK\n");
  178. return;
  179. GLUI_Panel *saved_root = curr_root;
  180. GLUI_Tree *saved_branch = curr_branch;
  181. root_children = 0;
  182. resetToRoot(this);
  183. if (curr_branch && curr_branch->dynamicCastGLUI_Tree())
  184. formatNode((GLUI_Tree *)curr_branch);
  185. next();
  186. while (curr_root && curr_branch != this->first_child()) {
  187. if (curr_branch && curr_branch->dynamicCastGLUI_Tree()) {
  188. formatNode((GLUI_Tree *)curr_branch);
  189. }
  190. next();
  191. }
  192. curr_root = saved_root;
  193. curr_branch = saved_branch;
  194. }
  195. /****************************** GLUI_TreePanel::expand_all() *********/
  196. void GLUI_TreePanel::expand_all()
  197. {
  198. GLUI_Panel *saved_root = curr_root;
  199. GLUI_Tree *saved_branch = curr_branch;
  200. resetToRoot(this);
  201. if (curr_root->dynamicCastGLUI_Tree())
  202. ((GLUI_Tree*)curr_root)->open();
  203. next();
  204. while (curr_root != NULL && curr_branch != this->first_child()) {
  205. if (curr_root->dynamicCastGLUI_Tree())
  206. ((GLUI_Tree*)curr_root)->open();
  207. next();
  208. }
  209. curr_root = saved_root;
  210. curr_branch = saved_branch;
  211. }
  212. /****************************** GLUI_TreePanel::collapse_all() *********/
  213. void GLUI_TreePanel::collapse_all()
  214. {
  215. GLUI_Panel *saved_root = curr_root;
  216. GLUI_Tree *saved_branch = curr_branch;
  217. resetToRoot(this);
  218. next();
  219. while (curr_root != NULL && curr_branch != this->first_child()) {
  220. if (curr_root->dynamicCastGLUI_Tree() &&
  221. curr_branch == NULL) { /* we want to close everything leaf-first */
  222. ((GLUI_Tree*)curr_root)->close();
  223. /* Rather than simply next(), we need to manually move the
  224. curr_root because this node has been moved to the
  225. collapsed_node list */
  226. curr_branch = (GLUI_Tree *)curr_root->next();
  227. curr_root = (GLUI_Panel *)curr_root->parent();
  228. } else
  229. next();
  230. }
  231. curr_root = saved_root;
  232. curr_branch = saved_branch;
  233. }
  234. /****************************** GLUI_TreePanel::db() *********/
  235. /* Deletes the curr_root */
  236. void GLUI_TreePanel::db(GLUI_Tree *root)
  237. {
  238. GLUI_Tree *temp_branch;
  239. GLUI_Panel *temp_root;
  240. if (((GLUI_Control *)root) == ((GLUI_Control *)this))
  241. return;
  242. if (root != NULL) {
  243. curr_root = (GLUI_Tree *)root;
  244. curr_branch = NULL;
  245. }
  246. if (curr_root == NULL || ((GLUI_Panel *)curr_root) == ((GLUI_Panel *)this)) {
  247. resetToRoot();
  248. return;
  249. }
  250. temp_branch = (GLUI_Tree *)curr_root->next(); /* Next branch, if any */
  251. temp_root = (GLUI_Panel *)curr_root->parent(); /* new root */
  252. curr_root->unlink();
  253. delete curr_root;
  254. curr_branch = (GLUI_Tree *) temp_branch;
  255. curr_root = (GLUI_Panel *) temp_root;
  256. if (curr_root->dynamicCastGLUI_Tree())
  257. ((GLUI_Tree *)curr_root)->open();
  258. if ((format & GLUI_TREEPANEL_DISABLE_DEEPEST_BAR) == GLUI_TREEPANEL_DISABLE_DEEPEST_BAR) {
  259. if (curr_root->dynamicCastGLUI_Tree() && ((GLUI_Tree *)curr_root->next()) == NULL)
  260. ((GLUI_Tree *)curr_root)->disable_bar();
  261. }
  262. //refresh();
  263. }
  264. /****************************** GLUI_TreePanel::descendBranch() *********/
  265. /* Finds the very last branch of curr_root, resets vars */
  266. void GLUI_TreePanel::descendBranch(GLUI_Panel *root) {
  267. if (root)
  268. resetToRoot(root);
  269. else
  270. resetToRoot(curr_root);
  271. if (curr_branch != NULL && curr_branch != ((GLUI_Panel *)this)) {
  272. if (curr_root->dynamicCastGLUI_Tree())
  273. ((GLUI_Tree *)curr_root)->set_current(false);
  274. descendBranch(curr_branch);
  275. }
  276. }
  277. /****************************** GLUI_TreePanel::next() *********/
  278. void GLUI_TreePanel::next()
  279. {
  280. if (curr_root == NULL)
  281. resetToRoot(this);
  282. if (curr_branch == NULL && (curr_root->collapsed_node).first_child() != NULL)
  283. curr_branch = (GLUI_Tree *)(curr_root->collapsed_node).first_child();
  284. if (curr_branch != NULL && curr_branch != ((GLUI_Panel *)this)) { /* Descend into branch */
  285. if (curr_root->dynamicCastGLUI_Tree())
  286. ((GLUI_Tree *)curr_root)->set_current(false);
  287. resetToRoot(curr_branch);
  288. } else if (curr_branch == NULL) {
  289. fb(NULL); /* Backup and move on */
  290. }
  291. }
  292. /****************************** GLUI_TreePanel::resetToRoot() *********/
  293. /* Resets curr_root and curr branch to TreePanel and lastChild */
  294. void GLUI_TreePanel::resetToRoot(GLUI_Panel *new_root)
  295. {
  296. GLUI_Panel *root = this;
  297. if (new_root != NULL)
  298. root = new_root;
  299. curr_root = root;
  300. if (curr_root->dynamicCastGLUI_Tree())
  301. ((GLUI_Tree *)curr_root)->set_current(true);
  302. curr_branch = (GLUI_Tree *)root->first_child();
  303. /* since Trees are collapsable, we need to check the collapsed nodes
  304. in case the curr_root is collapsed */
  305. if (curr_branch == NULL && (root->collapsed_node).first_child() != NULL) {
  306. curr_branch = (GLUI_Tree *)(root->collapsed_node).first_child();
  307. }
  308. while (curr_branch && curr_branch->dynamicCastGLUI_Tree()) {
  309. curr_branch=(GLUI_Tree *)curr_branch->next();
  310. }
  311. }