PolyUICheckBox.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "PolyUICheckBox.h"
  20. #include "PolyInputEvent.h"
  21. #include "PolyLabel.h"
  22. #include "PolyCoreServices.h"
  23. #include "PolyConfig.h"
  24. #include "PolyRenderer.h"
  25. using namespace Polycode;
  26. UICheckBox::UICheckBox(String caption, bool checked) : UIElement() {
  27. Config *conf = CoreServices::getInstance()->getConfig();
  28. Number uiScale = conf->getNumericValue("Polycode", "uiScale");
  29. String fontName = conf->getStringValue("Polycode", "uiCheckBoxFont");
  30. int fontSize = conf->getNumericValue("Polycode", "uiCheckBoxFontSize");
  31. String checkImage = conf->getStringValue("Polycode", "uiCheckBoxCheckedImage");
  32. String uncheckImage = conf->getStringValue("Polycode", "uiCheckBoxUncheckedImage");
  33. Number checkboxTextOffsetX = conf->getNumericValue("Polycode", "uiCheckBoxLabelOffsetX");
  34. Number checkboxTextOffsetY = conf->getNumericValue("Polycode", "uiCheckBoxLabelOffsetY");
  35. this->checked = checked;
  36. buttonImageChecked = new UIImage(checkImage);
  37. buttonImageChecked->Resize(buttonImageChecked->getWidth() / uiScale, buttonImageChecked->getHeight() / uiScale);
  38. buttonImageChecked->visible = checked;
  39. buttonImageUnchecked = new UIImage(uncheckImage);
  40. buttonImageUnchecked->visible = !checked;
  41. buttonImageUnchecked->Resize(buttonImageUnchecked->getWidth() / uiScale, buttonImageUnchecked->getHeight() / uiScale);
  42. captionLabel = new UILabel(caption, fontSize, fontName, Label::ANTIALIAS_FULL);
  43. captionLabel->setBlendingMode(Renderer::BLEND_MODE_NORMAL);
  44. addChild(captionLabel);
  45. captionLabel->setPosition(buttonImageChecked->getWidth() + checkboxTextOffsetX, checkboxTextOffsetY);
  46. addChild(buttonImageUnchecked);
  47. addChild(buttonImageChecked);
  48. captionLabel->color.setColorHexFromString(conf->getStringValue("Polycode", "uiDefaultFontColor"));
  49. buttonImageUnchecked->addEventListener(this, InputEvent::EVENT_MOUSEOVER);
  50. buttonImageUnchecked->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
  51. buttonImageUnchecked->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  52. buttonImageUnchecked->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  53. buttonImageUnchecked->processInputEvents = true;
  54. captionLabel->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  55. captionLabel->processInputEvents = true;
  56. setHeight(buttonImageUnchecked->getHeight());
  57. setWidth(buttonImageUnchecked->getWidth() + captionLabel->getWidth() + checkboxTextOffsetX);
  58. }
  59. String UICheckBox::getCaptionLabel() {
  60. return captionLabel->getText();
  61. }
  62. UICheckBox::~UICheckBox() {
  63. if(!ownsChildren) {
  64. delete buttonImageChecked;
  65. delete buttonImageUnchecked;
  66. delete captionLabel;
  67. }
  68. }
  69. bool UICheckBox::isChecked() {
  70. return checked;
  71. }
  72. void UICheckBox::setChecked(bool val) {
  73. checked = val;
  74. buttonImageChecked->visible = checked;
  75. buttonImageUnchecked->visible = !checked;
  76. }
  77. void UICheckBox::changeCheck() {
  78. checked = !checked;
  79. buttonImageChecked->visible = checked;
  80. buttonImageUnchecked->visible = !checked;
  81. dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
  82. }
  83. void UICheckBox::handleEvent(Event *event) {
  84. if(event->getDispatcher() == buttonImageUnchecked || event->getDispatcher() == captionLabel) {
  85. switch(event->getEventCode()) {
  86. case InputEvent::EVENT_MOUSEDOWN:
  87. changeCheck();
  88. break;
  89. }
  90. }
  91. UIElement::handleEvent(event);
  92. }