PolyUIComboBox.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, Number comboWidth, Number comboHeight) : ScreenEntity() {
  26. this->label = label;
  27. Config *conf = CoreServices::getInstance()->getConfig();
  28. String fontName = conf->getStringValue("Polycode", "uiComboBoxFont");
  29. int fontSize = conf->getNumericValue("Polycode", "uiComboBoxFontSize");
  30. Number paddingX = conf->getNumericValue("Polycode", "uiComboBoxTextOffsetX");
  31. Number paddingY = conf->getNumericValue("Polycode", "uiComboBoxTextOffsetY");
  32. itemLabel = new ScreenLabel(label, fontSize, fontName);
  33. itemLabel->setPosition(paddingX, floor(((comboHeight/2.0) - itemLabel->getHeight()/2.0) + paddingY));
  34. addChild(itemLabel);
  35. }
  36. UIComboBoxItem::~UIComboBoxItem() {
  37. }
  38. UIComboBox::UIComboBox(Number comboWidth) : ScreenEntity() {
  39. isDroppedDown = false;
  40. selectedIndex = -1;
  41. Config *conf = CoreServices::getInstance()->getConfig();
  42. String fontName = conf->getStringValue("Polycode", "uiComboBoxFont");
  43. int fontSize = conf->getNumericValue("Polycode", "uiComboBoxFontSize");
  44. String dropDownImageFile = conf->getStringValue("Polycode", "uiComboBoxDropdownImage");
  45. String bgImage = conf->getStringValue("Polycode", "uiComboBoxBgImage");
  46. Number st = conf->getNumericValue("Polycode", "uiComboBoxBgT");
  47. Number sr = conf->getNumericValue("Polycode", "uiComboBoxBgR");
  48. Number sb = conf->getNumericValue("Polycode", "uiComboBoxBgB");
  49. Number sl = conf->getNumericValue("Polycode", "uiComboBoxBgL");
  50. Number paddingX = conf->getNumericValue("Polycode", "uiComboBoxTextOffsetX");
  51. Number paddingY = conf->getNumericValue("Polycode", "uiComboBoxTextOffsetY");
  52. dropDownImage = new ScreenImage(dropDownImageFile);
  53. dropDownImage->setPosition(comboWidth - dropDownImage->getWidth(),0);
  54. this->comboHeight = dropDownImage->getHeight();
  55. this->comboWidth = comboWidth;
  56. nextItemHeight = 0;
  57. bgBox = new UIBox(bgImage, st,sr,sb,sl, comboWidth, comboHeight);
  58. addChild(bgBox);
  59. addChild(dropDownImage);
  60. selectedLabel = new ScreenLabel("<None>", fontSize, fontName);
  61. selectedLabel->setPosition(paddingX, floor(((dropDownImage->getHeight()/2.0) - selectedLabel->getHeight()/2.0) + paddingY));
  62. addChild(selectedLabel);
  63. String dropdownBgImage = conf->getStringValue("Polycode", "uiComboBoxItemsBgImage");
  64. st = conf->getNumericValue("Polycode", "uiComboBoxDropdownBgT");
  65. sr = conf->getNumericValue("Polycode", "uiComboBoxDropdownBgR");
  66. sb = conf->getNumericValue("Polycode", "uiComboBoxDropdownBgB");
  67. sl = conf->getNumericValue("Polycode", "uiComboBoxDropdownBgL");
  68. dropDownBox = new UIBox(dropdownBgImage, st,sr,sb,sl, comboWidth, comboHeight);
  69. dropDownBox->setPosition(0,comboHeight);
  70. addChild(dropDownBox);
  71. String selectorBgImage = conf->getStringValue("Polycode", "uiComboBoxSelectorBgImage");
  72. st = conf->getNumericValue("Polycode", "uiComboBoxSelectorBgT");
  73. sr = conf->getNumericValue("Polycode", "uiComboBoxSelectorBgR");
  74. sb = conf->getNumericValue("Polycode", "uiComboBoxSelectorBgB");
  75. sl = conf->getNumericValue("Polycode", "uiComboBoxSelectorBgL");
  76. dropDownBox->blockMouseInput = true;
  77. selectorBox = new UIBox(selectorBgImage, st,sr,sb,sl, comboWidth, comboHeight);
  78. dropDownBox->addChild(selectorBox);
  79. selectorBox->blockMouseInput = true;
  80. selectedOffset = 0;
  81. dropDownBox->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
  82. dropDownBox->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  83. dropDownImage->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  84. bgBox->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  85. isDroppedDown = false;
  86. updateVis();
  87. this->width = comboWidth;
  88. this->height = comboHeight;
  89. }
  90. UIComboBox::~UIComboBox() {
  91. }
  92. int UIComboBox::addComboItem(String itemName) {
  93. UIComboBoxItem *newItem = new UIComboBoxItem(itemName, comboWidth, comboHeight);
  94. items.push_back(newItem);
  95. dropDownBox->addChild(newItem);
  96. newItem->setPosition(0,nextItemHeight);
  97. nextItemHeight += comboHeight;
  98. dropDownBox->resizeBox(comboWidth, nextItemHeight);
  99. return items.size()-1;
  100. }
  101. void UIComboBox::toggleDropDown() {
  102. isDroppedDown = !isDroppedDown;
  103. updateVis();
  104. }
  105. void UIComboBox::updateVis() {
  106. dropDownBox->visible = isDroppedDown;
  107. dropDownBox->enabled = isDroppedDown;
  108. }
  109. int UIComboBox::getSelectedIndex() {
  110. return selectedIndex;
  111. }
  112. void UIComboBox::handleEvent(Event *event) {
  113. if(event->getDispatcher() == dropDownBox) {
  114. switch(event->getEventCode()) {
  115. case InputEvent::EVENT_MOUSEMOVE:
  116. {
  117. InputEvent *inputEvent = (InputEvent*) event;
  118. selectedOffset = floor(inputEvent->getMousePosition().y/comboHeight)- 1;
  119. if(selectedOffset >= 0 && selectedOffset < items.size())
  120. selectorBox->setPosition(0,selectedOffset*comboHeight);
  121. }
  122. break;
  123. case InputEvent::EVENT_MOUSEDOWN:
  124. {
  125. selectedIndex = selectedOffset;
  126. selectedLabel->setText(items[selectedIndex]->label);
  127. isDroppedDown = false;
  128. updateVis();
  129. }
  130. break;
  131. }
  132. }
  133. if(event->getDispatcher() == dropDownImage || event->getDispatcher() == bgBox) {
  134. switch(event->getEventCode()) {
  135. case InputEvent::EVENT_MOUSEDOWN:
  136. toggleDropDown();
  137. break;
  138. }
  139. }
  140. }