PolyUIMenu.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "PolyUIMenu.h"
  20. #include "PolyUIEvent.h"
  21. #include "PolyInputEvent.h"
  22. #include "PolyLabel.h"
  23. #include "PolyCoreServices.h"
  24. #include "PolyCore.h"
  25. #include "PolyConfig.h"
  26. #include "PolySceneLine.h"
  27. #include "PolyRenderer.h"
  28. using namespace Polycode;
  29. UIMenuItem::UIMenuItem(String label, String _id, void *data, Number comboWidth, Number comboHeight) : UIElement() {
  30. this->label = label;
  31. Config *conf = CoreServices::getInstance()->getConfig();
  32. String fontName = conf->getStringValue("Polycode", "uiMenuFont");
  33. int fontSize = conf->getNumericValue("Polycode", "uiMenuFontSize");
  34. Number paddingX = conf->getNumericValue("Polycode", "uiMenuTextOffsetX");
  35. Number paddingY = conf->getNumericValue("Polycode", "uiMenuTextOffsetY");
  36. itemLabel = new SceneLabel(label, fontSize, fontName);
  37. itemLabel->setBlendingMode(Renderer::BLEND_MODE_NORMAL);
  38. itemLabel->setPosition(paddingX, floor(((comboHeight/2.0) - itemLabel->getHeight()/2.0) + paddingY));
  39. addChild(itemLabel);
  40. itemLabel->color.setColorHexFromString(conf->getStringValue("Polycode", "uiDefaultFontColor"));
  41. this->_id = _id;
  42. this->data = data;
  43. }
  44. UIMenuItem::UIMenuItem() : UIElement(), data(NULL), itemLabel(NULL) {
  45. }
  46. bool UIMenuItem::isSelectable()
  47. {
  48. return true;
  49. }
  50. UIMenuItem::~UIMenuItem() {
  51. if(itemLabel)
  52. delete itemLabel;
  53. }
  54. UIMenuDivider::UIMenuDivider(Number comboWidth, Number comboHeight) : UIMenuItem() {
  55. Config *conf = CoreServices::getInstance()->getConfig();
  56. Number paddingX = conf->getNumericValue("Polycode", "uiMenuSelectorPadding");
  57. line = new SceneLine(Vector3(paddingX, comboHeight/2.0, 0.0), Vector3(comboWidth-paddingX, comboHeight/2.0, 0.0));
  58. // line->setLineWidth(1.0);
  59. line->setColor(Color(0.0, 0.0, 0.0, 0.7));
  60. addChild(line);
  61. }
  62. UIMenuDivider::~UIMenuDivider() {
  63. delete line;
  64. }
  65. bool UIMenuDivider::isSelectable()
  66. {
  67. return false;
  68. }
  69. UIMenu::UIMenu(Number menuWidth) : UIElement() {
  70. Config *conf = CoreServices::getInstance()->getConfig();
  71. this->menuItemHeight = conf->getNumericValue("Polycode", "uiMenuItemHeight");
  72. this->menuWidth = menuWidth;
  73. nextItemHeight = 0;
  74. paddingX = conf->getNumericValue("Polycode", "uiMenuPaddingX");
  75. paddingY = conf->getNumericValue("Polycode", "uiMenuPaddingY");
  76. String dropdownBgImage = conf->getStringValue("Polycode", "uiMenuBgImage");
  77. Number st = conf->getNumericValue("Polycode", "uiMenuBgT");
  78. Number sr = conf->getNumericValue("Polycode", "uiMenuBgR");
  79. Number sb = conf->getNumericValue("Polycode", "uiMenuBgB");
  80. Number sl = conf->getNumericValue("Polycode", "uiMenuBgL");
  81. dropDownBox = new UIBox(dropdownBgImage, st,sr,sb,sl, menuWidth, menuItemHeight);
  82. dropDownBox->setPosition(0,0);
  83. addChild(dropDownBox);
  84. String selectorBgImage = conf->getStringValue("Polycode", "uiMenuSelectorBgImage");
  85. st = conf->getNumericValue("Polycode", "uiMenuSelectorBgT");
  86. sr = conf->getNumericValue("Polycode", "uiMenuSelectorBgR");
  87. sb = conf->getNumericValue("Polycode", "uiMenuSelectorBgB");
  88. sl = conf->getNumericValue("Polycode", "uiMenuSelectorBgL");
  89. dropDownBox->blockMouseInput = true;
  90. selectorPadding = conf->getNumericValue("Polycode", "uiMenuSelectorPadding");
  91. selectorBox = new UIBox(selectorBgImage, st,sr,sb,sl, menuWidth - (paddingX * 2.0), menuItemHeight + (selectorPadding * 2.0));
  92. dropDownBox->addChild(selectorBox);
  93. selectorBox->blockMouseInput = true;
  94. selectorBox->visible = false;
  95. selectedOffset = 0;
  96. dropDownBox->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
  97. dropDownBox->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  98. dropDownBox->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
  99. dropDownBox->processInputEvents = true;
  100. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  101. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  102. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_KEYDOWN);
  103. initialMouse = CoreServices::getInstance()->getCore()->getInput()->getMousePosition();
  104. setWidth(menuWidth);
  105. setHeight(menuItemHeight);
  106. // ugh, hackz
  107. ignoreMouse = true;
  108. }
  109. UIMenuItem *UIMenu::getSelectedItem() {
  110. if(selectedOffset < 0) {
  111. return items[0];
  112. }
  113. if(selectedOffset > items.size()-1) {
  114. return items[items.size()-1];
  115. }
  116. if(selectedOffset >= 0 && selectedOffset < items.size()) {
  117. return items[selectedOffset];
  118. } else {
  119. return NULL;
  120. }
  121. }
  122. void UIMenu::Update() {
  123. ignoreMouse = false;
  124. }
  125. void UIMenu::fitToScreenVertical() {
  126. // Make sure the entity doesn't go past the bottom of the screen.
  127. if(dropDownBox->getHeight() < CoreServices::getInstance()->getCore()->getYRes()) {
  128. // If the entity is as high as the screen, no point trying to fit it in vertically.
  129. Vector2 screenPos = this->getScreenPositionForMainCamera();
  130. Number exceedScreenBottom = screenPos.y + dropDownBox->getHeight() - CoreServices::getInstance()->getCore()->getYRes();
  131. if(exceedScreenBottom > 0) {
  132. this->setPosition(this->getPosition().x, this->getPosition().y - exceedScreenBottom);
  133. } else if(screenPos.y < 0) {
  134. this->setPosition(this->getPosition().x, 0);
  135. }
  136. }
  137. }
  138. void UIMenu::handleEvent(Event *event) {
  139. if(event->getDispatcher() == CoreServices::getInstance()->getCore()->getInput()) {
  140. InputEvent *inputEvent = (InputEvent*) event;
  141. if(event->getEventCode() == InputEvent::EVENT_KEYDOWN) {
  142. if(inputEvent->key == KEY_ESCAPE) {
  143. dispatchEvent(new UIEvent(), UIEvent::CANCEL_EVENT);
  144. }
  145. }
  146. if((event->getEventCode() == InputEvent::EVENT_MOUSEDOWN || (event->getEventCode() == InputEvent::EVENT_MOUSEUP && initialMouse != inputEvent->getMousePosition())) && !ignoreMouse) {
  147. if(selectorBox->visible) {
  148. dispatchEvent(new UIEvent(), UIEvent::OK_EVENT);
  149. } else if(!dropDownBox->hitTest(inputEvent->getMousePosition())) {
  150. dispatchEvent(new UIEvent(), UIEvent::CANCEL_EVENT);
  151. }
  152. }
  153. }
  154. if(event->getDispatcher() == dropDownBox) {
  155. switch(event->getEventCode()) {
  156. case InputEvent::EVENT_MOUSEOUT:
  157. {
  158. selectorBox->visible = false;
  159. }
  160. break;
  161. case InputEvent::EVENT_MOUSEMOVE:
  162. {
  163. CoreServices::getInstance()->getCore()->setCursor(Core::CURSOR_ARROW);
  164. InputEvent *inputEvent = (InputEvent*) event;
  165. selectedOffset = floor(((inputEvent->getMousePosition().y-selectorPadding)-paddingY)/menuItemHeight);
  166. if(selectedOffset >= 0 && selectedOffset < items.size() && items[selectedOffset]->isSelectable()) {
  167. selectorBox->visible = true;
  168. selectorBox->setPosition(paddingX,paddingY+(selectedOffset*menuItemHeight) - selectorPadding);
  169. } else {
  170. selectorBox->visible = false;
  171. }
  172. }
  173. break;
  174. }
  175. }
  176. }
  177. UIMenu::~UIMenu() {
  178. CoreServices::getInstance()->getCore()->getInput()->removeAllHandlersForListener(this);
  179. dropDownBox->ownsChildren = true;
  180. if(!ownsChildren) {
  181. delete dropDownBox;
  182. }
  183. }
  184. UIMenuItem *UIMenu::addOption(String label, String _id, void *data) {
  185. UIMenuItem *newItem = new UIMenuItem(label, _id, data, menuWidth, menuItemHeight);
  186. items.push_back(newItem);
  187. dropDownBox->addChild(newItem);
  188. newItem->setPosition(0,paddingY+nextItemHeight);
  189. nextItemHeight += menuItemHeight;
  190. dropDownBox->resizeBox(menuWidth, nextItemHeight + (paddingY * 2.0));
  191. return newItem;
  192. }
  193. UIMenuItem *UIMenu::addDivider()
  194. {
  195. Number newItemHeight = menuItemHeight;
  196. UIMenuItem *newItem = new UIMenuDivider(menuWidth, newItemHeight);
  197. items.push_back(newItem);
  198. dropDownBox->addChild(newItem);
  199. newItem->setPosition(0, paddingY+nextItemHeight);
  200. nextItemHeight += newItemHeight;
  201. dropDownBox->resizeBox(menuWidth, nextItemHeight + (paddingY*2.0));
  202. return newItem;
  203. }
  204. void UIMenu::Resize(Number width, Number height) {
  205. UIElement::Resize(width, height);
  206. }
  207. UIGlobalMenu::UIGlobalMenu() : Entity() {
  208. currentMenu = NULL;
  209. processInputEvents = true;
  210. willHideMenu = false;
  211. defaultMenuWidth = 100;
  212. }
  213. UIGlobalMenu::~UIGlobalMenu() {
  214. }
  215. void UIGlobalMenu::hideMenu() {
  216. removeChild(currentMenu);
  217. delete currentMenu;
  218. currentMenu = NULL;
  219. willHideMenu = false;
  220. }
  221. void UIGlobalMenu::Update() {
  222. if(willHideMenu) {
  223. hideMenu();
  224. }
  225. }
  226. void UIGlobalMenu::handleEvent(Event *event) {
  227. if(event->getDispatcher() == currentMenu && event->getEventType() == "UIEvent") {
  228. if(event->getEventCode() == UIEvent::OK_EVENT) {
  229. willHideMenu = true;
  230. }
  231. if(event->getEventCode() == UIEvent::CANCEL_EVENT) {
  232. willHideMenu = true;
  233. }
  234. }
  235. }
  236. UIMenu *UIGlobalMenu::showMenuAtMouse(Number width) {
  237. Vector2 pos = CoreServices::getInstance()->getCore()->getInput()->getMousePosition();
  238. return showMenu(pos.x, pos.y, width);
  239. }
  240. UIMenu *UIGlobalMenu::showMenu(Number x, Number y, Number width) {
  241. if(currentMenu) {
  242. hideMenu();
  243. }
  244. currentMenu = new UIMenu(width);
  245. currentMenu->addEventListener(this, UIEvent::OK_EVENT);
  246. currentMenu->addEventListener(this, UIEvent::CANCEL_EVENT);
  247. addChild(currentMenu);
  248. currentMenu->setPosition(x,y);
  249. return currentMenu;
  250. }