PolyUIMenu.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. using namespace Polycode;
  27. UIMenuItem::UIMenuItem(String label, String _id, void *data, Number comboWidth, Number comboHeight) : UIElement() {
  28. this->label = label;
  29. Config *conf = CoreServices::getInstance()->getConfig();
  30. String fontName = conf->getStringValue("Polycode", "uiMenuFont");
  31. int fontSize = conf->getNumericValue("Polycode", "uiMenuFontSize");
  32. Number paddingX = conf->getNumericValue("Polycode", "uiMenuTextOffsetX");
  33. Number paddingY = conf->getNumericValue("Polycode", "uiMenuTextOffsetY");
  34. itemLabel = new ScreenLabel(label, fontSize, fontName);
  35. itemLabel->setPosition(paddingX, floor(((comboHeight/2.0) - itemLabel->getHeight()/2.0) + paddingY));
  36. addChild(itemLabel);
  37. this->_id = _id;
  38. this->data = data;
  39. }
  40. UIMenuItem::~UIMenuItem() {
  41. delete itemLabel;
  42. }
  43. UIMenu::UIMenu(Number menuWidth) : UIElement() {
  44. Config *conf = CoreServices::getInstance()->getConfig();
  45. this->menuItemHeight = conf->getNumericValue("Polycode", "uiMenuItemHeight");
  46. this->menuWidth = menuWidth;
  47. nextItemHeight = 0;
  48. paddingX = conf->getNumericValue("Polycode", "uiMenuPaddingX");
  49. paddingY = conf->getNumericValue("Polycode", "uiMenuPaddingY");
  50. String dropdownBgImage = conf->getStringValue("Polycode", "uiMenuBgImage");
  51. Number st = conf->getNumericValue("Polycode", "uiMenuBgT");
  52. Number sr = conf->getNumericValue("Polycode", "uiMenuBgR");
  53. Number sb = conf->getNumericValue("Polycode", "uiMenuBgB");
  54. Number sl = conf->getNumericValue("Polycode", "uiMenuBgL");
  55. dropDownBox = new UIBox(dropdownBgImage, st,sr,sb,sl, menuWidth, menuItemHeight);
  56. dropDownBox->setPosition(0,0);
  57. addChild(dropDownBox);
  58. String selectorBgImage = conf->getStringValue("Polycode", "uiMenuSelectorBgImage");
  59. st = conf->getNumericValue("Polycode", "uiMenuSelectorBgT");
  60. sr = conf->getNumericValue("Polycode", "uiMenuSelectorBgR");
  61. sb = conf->getNumericValue("Polycode", "uiMenuSelectorBgB");
  62. sl = conf->getNumericValue("Polycode", "uiMenuSelectorBgL");
  63. dropDownBox->blockMouseInput = true;
  64. selectorPadding = conf->getNumericValue("Polycode", "uiMenuSelectorPadding");
  65. selectorBox = new UIBox(selectorBgImage, st,sr,sb,sl, menuWidth - (paddingX * 2.0), menuItemHeight + (selectorPadding * 2.0));
  66. dropDownBox->addChild(selectorBox);
  67. selectorBox->blockMouseInput = true;
  68. selectorBox->visible = false;
  69. selectedOffset = 0;
  70. dropDownBox->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
  71. dropDownBox->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  72. dropDownBox->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
  73. dropDownBox->processInputEvents = true;
  74. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  75. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  76. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_KEYDOWN);
  77. initialMouse = CoreServices::getInstance()->getCore()->getInput()->getMousePosition();
  78. this->width = menuWidth;
  79. this->height = menuItemHeight;
  80. // ugh, hackz
  81. ignoreMouse = true;
  82. }
  83. UIMenuItem *UIMenu::getSelectedItem() {
  84. if(selectedOffset < 0) {
  85. return items[0];
  86. }
  87. if(selectedOffset > items.size()-1) {
  88. return items[items.size()-1];
  89. }
  90. if(selectedOffset >= 0 && selectedOffset < items.size()) {
  91. return items[selectedOffset];
  92. } else {
  93. return NULL;
  94. }
  95. }
  96. void UIMenu::Update() {
  97. ignoreMouse = false;
  98. }
  99. void UIMenu::handleEvent(Event *event) {
  100. if(event->getDispatcher() == CoreServices::getInstance()->getCore()->getInput()) {
  101. InputEvent *inputEvent = (InputEvent*) event;
  102. if(event->getEventCode() == InputEvent::EVENT_KEYDOWN) {
  103. if(inputEvent->key == KEY_ESCAPE) {
  104. dispatchEvent(new UIEvent(), UIEvent::CANCEL_EVENT);
  105. }
  106. }
  107. if((event->getEventCode() == InputEvent::EVENT_MOUSEDOWN || (event->getEventCode() == InputEvent::EVENT_MOUSEUP && initialMouse != inputEvent->getMousePosition())) && !ignoreMouse) {
  108. if(selectorBox->visible) {
  109. dispatchEvent(new UIEvent(), UIEvent::OK_EVENT);
  110. } else {
  111. dispatchEvent(new UIEvent(), UIEvent::CANCEL_EVENT);
  112. }
  113. }
  114. }
  115. if(event->getDispatcher() == dropDownBox) {
  116. switch(event->getEventCode()) {
  117. case InputEvent::EVENT_MOUSEOUT:
  118. {
  119. selectorBox->visible = false;
  120. }
  121. break;
  122. case InputEvent::EVENT_MOUSEMOVE:
  123. {
  124. InputEvent *inputEvent = (InputEvent*) event;
  125. selectedOffset = floor(((inputEvent->getMousePosition().y-selectorPadding)-paddingY)/menuItemHeight);
  126. if(selectedOffset >= 0 && selectedOffset < items.size()) {
  127. selectorBox->visible = true;
  128. selectorBox->setPosition(paddingX,paddingY+(selectedOffset*menuItemHeight) - selectorPadding);
  129. } else {
  130. selectorBox->visible = false;
  131. }
  132. }
  133. break;
  134. }
  135. }
  136. }
  137. UIMenu::~UIMenu() {
  138. CoreServices::getInstance()->getCore()->getInput()->removeAllHandlersForListener(this);
  139. for(int c = 0; c < items.size(); c++)
  140. delete items[c];
  141. if(!ownsChildren) {
  142. delete dropDownBox;
  143. delete selectorBox;
  144. }
  145. CoreServices::getInstance()->getCore()->getInput()->removeAllHandlersForListener(this);
  146. }
  147. UIMenuItem *UIMenu::addOption(String label, String _id, void *data) {
  148. UIMenuItem *newItem = new UIMenuItem(label, _id, data, menuWidth, menuItemHeight);
  149. items.push_back(newItem);
  150. dropDownBox->addChild(newItem);
  151. newItem->setPosition(0,paddingY+nextItemHeight);
  152. nextItemHeight += menuItemHeight;
  153. dropDownBox->resizeBox(menuWidth, nextItemHeight + (paddingY * 2.0));
  154. return newItem;
  155. }
  156. void UIMenu::Resize(Number width, Number height) {
  157. UIElement::Resize(width, height);
  158. }
  159. UIGlobalMenu::UIGlobalMenu() : ScreenEntity() {
  160. currentMenu = NULL;
  161. processInputEvents = true;
  162. willHideMenu = false;
  163. defaultMenuWidth = 100;
  164. }
  165. UIGlobalMenu::~UIGlobalMenu() {
  166. }
  167. void UIGlobalMenu::hideMenu() {
  168. removeChild(currentMenu);
  169. delete currentMenu;
  170. currentMenu = NULL;
  171. willHideMenu = false;
  172. }
  173. void UIGlobalMenu::Update() {
  174. if(willHideMenu) {
  175. hideMenu();
  176. }
  177. }
  178. void UIGlobalMenu::handleEvent(Event *event) {
  179. if(event->getDispatcher() == currentMenu && event->getEventType() == "UIEvent") {
  180. if(event->getEventCode() == UIEvent::OK_EVENT) {
  181. willHideMenu = true;
  182. }
  183. if(event->getEventCode() == UIEvent::CANCEL_EVENT) {
  184. willHideMenu = true;
  185. }
  186. }
  187. }
  188. UIMenu *UIGlobalMenu::showMenuAtMouse(Number width) {
  189. Vector2 pos = CoreServices::getInstance()->getCore()->getInput()->getMousePosition();
  190. return showMenu(pos.x, pos.y, width);
  191. }
  192. UIMenu *UIGlobalMenu::showMenu(Number x, Number y, Number width) {
  193. if(currentMenu) {
  194. hideMenu();
  195. }
  196. currentMenu = new UIMenu(width);
  197. currentMenu->addEventListener(this, UIEvent::OK_EVENT);
  198. currentMenu->addEventListener(this, UIEvent::CANCEL_EVENT);
  199. addChild(currentMenu);
  200. currentMenu->setPosition(x,y);
  201. return currentMenu;
  202. }