PolyUITreeContainer.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. Copyright (C) 2012 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyUITreeContainer.h"
  20. #include "PolyConfig.h"
  21. #include "PolyInputEvent.h"
  22. #include "PolyLabel.h"
  23. #include "PolyCoreServices.h"
  24. #include "PolyCore.h"
  25. using namespace Polycode;
  26. UITreeContainer::UITreeContainer(String icon, String text, Number treeWidth, Number treeHeight) : UIElement() {
  27. Config *conf = CoreServices::getInstance()->getConfig();
  28. Number st = conf->getNumericValue("Polycode", "uiTreeContainerSkinT");
  29. Number sr = conf->getNumericValue("Polycode", "uiTreeContainerSkinR");
  30. Number sb = conf->getNumericValue("Polycode", "uiTreeContainerSkinB");
  31. Number sl = conf->getNumericValue("Polycode", "uiTreeContainerSkinL");
  32. Number padding = conf->getNumericValue("Polycode", "uiTreeContainerSkinPadding");
  33. // Number scrollBarOffset = conf->getNumericValue("Polycode", "uiTreeContainerScrollBarOffset");
  34. bgBox = new UIBox(conf->getStringValue("Polycode", "uiTreeContainerSkin"),
  35. st,sr,sb,sl,
  36. treeWidth, treeHeight);
  37. addChild(bgBox);
  38. bgBox->blockMouseInput = true;
  39. blockMouseInput = true;
  40. //bgBox->setPosition(-padding, -padding);
  41. scrollChild = new ScreenEntity();
  42. scrollChild->processInputEvents = true;
  43. rootNode = new UITree(icon, text, treeWidth,0);
  44. rootNode->addEventListener(this, UITreeEvent::NEED_REFRESH_EVENT);
  45. scrollChild->addChild(rootNode);
  46. mainContainer = new UIScrollContainer(scrollChild, false, true, treeWidth-conf->getNumericValue("Polycode", "uiScrollDefaultSize"), treeHeight);
  47. addChild(mainContainer);
  48. width = treeWidth;
  49. height = treeHeight;
  50. setHitbox(width, height);
  51. Resize(width, height);
  52. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_KEYDOWN);
  53. this->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  54. focusable = true;
  55. keyNavigable = true;
  56. }
  57. void UITreeContainer::Resize(Number width, Number height) {
  58. mainContainer->Resize(width,height);
  59. bgBox->resizeBox(width, height);
  60. mainContainer->setPositionY(0);
  61. rootNode->Resize(width);
  62. // width = x;
  63. // height = y;
  64. setHitbox(width, height);
  65. }
  66. void UITreeContainer::handleEvent(Event *event) {
  67. if(event->getDispatcher() == rootNode) {
  68. if(event->getEventCode() == UITreeEvent::NEED_REFRESH_EVENT) {
  69. mainContainer->setContentSize(rootNode->getWidth(), rootNode->getHeight());
  70. }
  71. }
  72. if (!hasFocus && event->getDispatcher() == this) {
  73. if (event->getEventCode() == InputEvent::EVENT_MOUSEDOWN) {
  74. if (parentEntity && isFocusable())
  75. ((ScreenEntity*)parentEntity)->focusChild(this);
  76. }
  77. }
  78. }
  79. UITree *UITreeContainer::getRootNode() {
  80. return rootNode;
  81. }
  82. UITreeContainer::~UITreeContainer() {
  83. if(!ownsChildren) {
  84. delete bgBox;
  85. delete scrollChild;
  86. delete rootNode;
  87. delete mainContainer;
  88. }
  89. }
  90. void UITreeContainer::onGainFocus() {
  91. if (!getRootNode()->getSelectedNode())
  92. getRootNode()->setSelected();
  93. }
  94. // RECURSIVE HELPER FUNCTIONS
  95. //
  96. // used in KEY_UP tree nav
  97. UITree *UITreeContainer::findLastOpenNode(UITree *tree) {
  98. if (!tree->hasTreeChildren() || tree->isCollapsed())
  99. return tree;
  100. UITree *t = tree->getTreeChild(tree->getNumTreeChildren()-1);
  101. if (t->isCollapsed() || !t->hasTreeChildren())
  102. return t;
  103. else
  104. return findLastOpenNode(t);
  105. }
  106. // used in KEY_DOWN tree nav
  107. UITree *UITreeContainer::findNextParentSibling(UITree *parent) {
  108. UITree *sibling = parent->getNextSibling();
  109. if (sibling)
  110. return sibling;
  111. else {
  112. if (parent->getParent())
  113. return findNextParentSibling(parent->getParent());
  114. else
  115. return NULL;
  116. }
  117. }
  118. //
  119. // END RECURSIVE HELPER FUNCTIONS
  120. void UITreeContainer::onKeyDown(PolyKEY key, wchar_t charCode) {
  121. if (hasFocus) {
  122. // KEYBOARD NAV STUFF
  123. // TODO: add ability to jump to prev/next sibling via holding down some button
  124. //
  125. if (keyNavigable) {
  126. enum { NONE, UP, DOWN } scrollDir = NONE;
  127. UITree *currentSelection = getRootNode()->getSelectedNode();
  128. if (!currentSelection)
  129. return;
  130. UITree *parent = currentSelection->getParent();
  131. // - select parent if currently selected node is first in the tree
  132. // - else, select the last opened node in a sibling above
  133. if (key == KEY_UP) {
  134. if (parent) {
  135. for (int i=0; i < parent->getNumTreeChildren(); i++) {
  136. if (parent->getTreeChild(i) == currentSelection) {
  137. if (i == 0)
  138. parent->setSelected();
  139. else
  140. findLastOpenNode((parent->getTreeChild(i-1)))->setSelected();
  141. scrollDir = UP;
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. // - select first child of currently selected if expanded and has children
  148. // - or select next sibling
  149. // - else, select the next sibling of some ancestor node
  150. if (key == KEY_DOWN) {
  151. if (currentSelection == getRootNode())
  152. parent = getRootNode();
  153. if (currentSelection->hasTreeChildren() && !currentSelection->isCollapsed()) {
  154. currentSelection->getTreeChild(0)->setSelected();
  155. scrollDir = DOWN;
  156. }
  157. else {
  158. for (int i=0; i < parent->getNumTreeChildren(); i++) {
  159. if (parent->getTreeChild(i) == currentSelection) {
  160. if (i == parent->getNumTreeChildren()-1) {
  161. UITree *psib = findNextParentSibling(parent);
  162. if (psib)
  163. psib->setSelected();
  164. } else {
  165. parent->getTreeChild(i+1)->setSelected();
  166. }
  167. scrollDir = DOWN;
  168. break;
  169. }
  170. }
  171. }
  172. }
  173. if (key == KEY_LEFT) {
  174. if (currentSelection->hasTreeChildren() && !currentSelection->isCollapsed())
  175. currentSelection->toggleCollapsed();
  176. else if (parent) {
  177. parent->setSelected();
  178. scrollDir = UP;
  179. }
  180. }
  181. if (key == KEY_RIGHT) {
  182. if (currentSelection->hasTreeChildren()) {
  183. if (currentSelection->isCollapsed())
  184. currentSelection->toggleCollapsed();
  185. else {
  186. currentSelection->getTreeChild(0)->setSelected();
  187. scrollDir = DOWN;
  188. }
  189. }
  190. }
  191. if (scrollDir != NONE)
  192. scrollToNode(getRootNode()->getSelectedNode(), (scrollDir == UP) ? true : false, false);
  193. }
  194. //
  195. // END KEYBOARD NAV STUFF
  196. }
  197. }
  198. void UITreeContainer::scrollToNode(UITree *node, bool showAtTop, bool select) {
  199. Number nodeY = node->getScreenPosition().y - getRootNode()->getScreenPosition().y;
  200. Number contentHeight = mainContainer->getContentSize().y;
  201. Number scrollHeight = contentHeight - mainContainer->getHeight();
  202. Number viewTop = (contentHeight - mainContainer->getHeight()) * mainContainer->getVScrollBar()->getScrollValue();
  203. Number viewBottom = viewTop + mainContainer->getHeight();
  204. if (select)
  205. node->setSelected();
  206. if (nodeY < viewTop || nodeY+node->getCellHeight() > viewBottom) {
  207. if (showAtTop)
  208. mainContainer->scrollVertical((nodeY-viewTop) / scrollHeight);
  209. else
  210. mainContainer->scrollVertical((nodeY+node->getCellHeight()-viewBottom) / scrollHeight);
  211. }
  212. }