PolyUIComboBox.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "PolyUIComboBox.h"
  20. #include "PolyInputEvent.h"
  21. #include "PolyLabel.h"
  22. #include "PolyCoreServices.h"
  23. #include "PolyConfig.h"
  24. using namespace Polycode;
  25. UIComboBoxItem::UIComboBoxItem(String label, void *data) {
  26. this->label = label;
  27. this->data = data;
  28. }
  29. UIComboBoxItem::~UIComboBoxItem() {
  30. }
  31. UIComboBox::UIComboBox(UIGlobalMenu *globalMenu, Number comboWidth) : UIElement() {
  32. this->globalMenu = globalMenu;
  33. selectedIndex = -1;
  34. Config *conf = CoreServices::getInstance()->getConfig();
  35. String fontName = conf->getStringValue("Polycode", "uiComboBoxFont");
  36. int fontSize = conf->getNumericValue("Polycode", "uiComboBoxFontSize");
  37. String dropDownImageFile = conf->getStringValue("Polycode", "uiComboBoxDropdownImage");
  38. String bgImage = conf->getStringValue("Polycode", "uiComboBoxBgImage");
  39. Number st = conf->getNumericValue("Polycode", "uiComboBoxBgT");
  40. Number sr = conf->getNumericValue("Polycode", "uiComboBoxBgR");
  41. Number sb = conf->getNumericValue("Polycode", "uiComboBoxBgB");
  42. Number sl = conf->getNumericValue("Polycode", "uiComboBoxBgL");
  43. Number paddingX = conf->getNumericValue("Polycode", "uiComboBoxTextOffsetX");
  44. Number paddingY = conf->getNumericValue("Polycode", "uiComboBoxTextOffsetY");
  45. Number dropDownX = conf->getNumericValue("Polycode", "uiComboBoxDropX");
  46. Number dropDownY = conf->getNumericValue("Polycode", "uiComboBoxDropY");
  47. dropDownImage = new ScreenImage(dropDownImageFile);
  48. dropDownImage->setPosition(comboWidth - dropDownImage->getWidth() - dropDownX,dropDownY);
  49. this->comboHeight = conf->getNumericValue("Polycode", "uiComboBoxHeight");
  50. this->comboWidth = comboWidth;
  51. nextItemHeight = 0;
  52. bgBox = new UIBox(bgImage, st,sr,sb,sl, comboWidth, comboHeight);
  53. addChild(bgBox);
  54. addChild(dropDownImage);
  55. selectedLabel = new ScreenLabel("<None>", fontSize, fontName);
  56. selectedLabel->positionAtBaseline = false;
  57. selectedLabel->setPosition(paddingX, floor(((dropDownImage->getHeight()/2.0) - selectedLabel->getLabel()->getTextHeight()/2.0) + paddingY));
  58. addChild(selectedLabel);
  59. selectedLabel->color.setColorHex(strtol(conf->getStringValue("Polycode", "uiDefaultFontColor").c_str(), 0, 16));
  60. String selectorBgImage = conf->getStringValue("Polycode", "uiComboBoxSelectorBgImage");
  61. st = conf->getNumericValue("Polycode", "uiComboBoxSelectorBgT");
  62. sr = conf->getNumericValue("Polycode", "uiComboBoxSelectorBgR");
  63. sb = conf->getNumericValue("Polycode", "uiComboBoxSelectorBgB");
  64. sl = conf->getNumericValue("Polycode", "uiComboBoxSelectorBgL");
  65. dropDownMenu = NULL;
  66. selectedOffset = 0;
  67. dropDownImage->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  68. dropDownImage->processInputEvents = true;
  69. bgBox->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  70. bgBox->processInputEvents = true;
  71. this->width = comboWidth;
  72. this->height = comboHeight;
  73. }
  74. UIComboBox::~UIComboBox() {
  75. for(int c = 0; c < items.size(); c++)
  76. delete items[c];
  77. delete dropDownImage;
  78. delete bgBox;
  79. delete selectedLabel;
  80. }
  81. void UIComboBox::clearItems() {
  82. items.clear();
  83. }
  84. int UIComboBox::addComboItem(String itemName, void *data) {
  85. UIComboBoxItem *newItem = new UIComboBoxItem(itemName, data);
  86. items.push_back(newItem);
  87. return items.size()-1;
  88. }
  89. int UIComboBox::addComboItem(String itemName) {
  90. return addComboItem(itemName, NULL);
  91. }
  92. UIComboBoxItem *UIComboBox::getSelectedItem() {
  93. if(selectedIndex < items.size()) {
  94. return items[selectedIndex];
  95. } else {
  96. return NULL;
  97. }
  98. }
  99. void UIComboBox::toggleDropDown() {
  100. Vector2 screenPos = this->getScreenPosition();
  101. dropDownMenu = globalMenu->showMenu(screenPos.x, screenPos.y - height, width);
  102. for(int i=0; i < items.size(); i++) {
  103. dropDownMenu->addOption(items[i]->label, String::IntToString(i));
  104. }
  105. dropDownMenu->addEventListener(this, UIEvent::OK_EVENT);
  106. }
  107. int UIComboBox::getSelectedIndex() {
  108. return selectedIndex;
  109. }
  110. void UIComboBox::setSelectedIndex(unsigned int newIndex) {
  111. if(newIndex < items.size()) {
  112. selectedIndex = newIndex;
  113. selectedLabel->setText(items[selectedIndex]->label);
  114. dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
  115. }
  116. }
  117. unsigned int UIComboBox::getNumItems() {
  118. return items.size();
  119. }
  120. UIComboBoxItem *UIComboBox::getItemAtIndex(unsigned int index) {
  121. return items[index];
  122. }
  123. void UIComboBox::handleEvent(Event *event) {
  124. if(event->getDispatcher() == dropDownMenu && event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::OK_EVENT) {
  125. setSelectedIndex(atoi(dropDownMenu->getSelectedItem()->_id.c_str()));
  126. }
  127. if(event->getDispatcher() == dropDownImage || event->getDispatcher() == bgBox) {
  128. switch(event->getEventCode()) {
  129. case InputEvent::EVENT_MOUSEDOWN:
  130. toggleDropDown();
  131. break;
  132. }
  133. }
  134. }