PolyUIWindow.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * PolyUIWindow.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 7/30/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "PolyUIWindow.h"
  10. using namespace Polycode;
  11. UIWindow::UIWindow(String windowName, Number width, Number height) : ScreenEntity() {
  12. closeOnEscape = false;
  13. snapToPixels = true;
  14. Config *conf = CoreServices::getInstance()->getConfig();
  15. String fontName = conf->getStringValue("Polycode", "uiWindowTitleFont");
  16. int fontSize = conf->getNumericValue("Polycode", "uiWindowTitleFontSize");
  17. Number st = conf->getNumericValue("Polycode", "uiWindowSkinT");
  18. Number sr = conf->getNumericValue("Polycode", "uiWindowSkinR");
  19. Number sb = conf->getNumericValue("Polycode", "uiWindowSkinB");
  20. Number sl = conf->getNumericValue("Polycode", "uiWindowSkinL");
  21. topPadding = st;
  22. padding = conf->getNumericValue("Polycode", "uiWindowSkinPadding");
  23. windowRect = new UIBox(conf->getStringValue("Polycode", "uiWindowSkin"),
  24. st,sr,sb,sl,
  25. width, height);
  26. addChild(windowRect);
  27. titlebarRect = new ScreenShape(ScreenShape::SHAPE_RECT, width, st);
  28. titlebarRect->setColor(0,0,0,0);
  29. titlebarRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  30. addChild(titlebarRect);
  31. ScreenLabel *titleLabel = new ScreenLabel(fontName, windowName, fontSize, Label::ANTIALIAS_FULL);
  32. titleLabel->setPosition(conf->getNumericValue("Polycode", "uiWindowTitleX"),conf->getNumericValue("Polycode", "uiWindowTitleY"));
  33. addChild(titleLabel);
  34. closeBtn = new UIImageButton(conf->getStringValue("Polycode", "uiWindowCloseIcon"));
  35. addChild(closeBtn);
  36. closeBtn->setPosition(width-closeBtn->getWidth()-conf->getNumericValue("Polycode", "uiCloseIconX"), conf->getNumericValue("Polycode", "uiCloseIconY"));
  37. titlebarRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  38. titlebarRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  39. closeBtn->addEventListener(this, UIEvent::CLICK_EVENT);
  40. this->width = width;
  41. this->height = height;
  42. this->hitwidth = width;
  43. this->hitheight = height;
  44. focusable = true;
  45. blockMouseInput = true;
  46. }
  47. void UIWindow::setWindowSize(Number w, Number h) {
  48. // windowRect->setScale(w/windowRect->getWidth(), h/windowRect->getHeight());
  49. // shadowRect->setScale(w/shadowRect->getWidth(), h/shadowRect->getHeight());
  50. // titlebarRect->setScale((w-4)/titlebarRect->getWidth(), 1.0f);
  51. // closeBtn->setPosition(w-closeBtn->getWidth()-2, 4);
  52. }
  53. UIWindow::~UIWindow() {
  54. }
  55. void UIWindow::onKeyDown(TAUKey key, wchar_t charCode) {
  56. if(key == TAUK_TAB) {
  57. if(hasFocus) {
  58. focusNextChild();
  59. }
  60. }
  61. if(key == TAUK_ESCAPE && closeOnEscape) {
  62. onClose();
  63. dispatchEvent(new UIEvent(), UIEvent::CLOSE_EVENT);
  64. }
  65. }
  66. void UIWindow::onLoseFocus() {
  67. if(focusedChild) {
  68. focusedChild->hasFocus = false;
  69. focusedChild->onLoseFocus();
  70. }
  71. }
  72. void UIWindow::onMouseDown(Number x, Number y) {
  73. if(hasFocus)
  74. return;
  75. hasFocus = true;
  76. dispatchEvent(new ScreenEvent(), ScreenEvent::ENTITY_MOVE_TOP);
  77. for(int i=0; i < children.size(); i++) {
  78. if(((ScreenEntity*)children[i])->isFocusable()) {
  79. focusChild(((ScreenEntity*)children[i]));
  80. return;
  81. }
  82. }
  83. }
  84. void UIWindow::showWindow() {
  85. // if(!visible) {
  86. visible = true;
  87. windowTween = new Tween(&color.a, Tween::EASE_IN_QUAD, 0.0f, 1.0f, 0.01f);
  88. // }
  89. }
  90. void UIWindow::hideWindow() {
  91. // if(visible) {
  92. windowTween = new Tween(&color.a, Tween::EASE_IN_QUAD, 1.0f, 0.0f, 0.01f);
  93. windowTween->addEventListener(this, Event::COMPLETE_EVENT);
  94. // }
  95. }
  96. void UIWindow::handleEvent(Event *event) {
  97. if(event->getDispatcher() == titlebarRect) {
  98. InputEvent *inputEvent = (InputEvent*)event;
  99. switch(event->getEventCode()) {
  100. case InputEvent::EVENT_MOUSEUP:
  101. stopDrag();
  102. break;
  103. case InputEvent::EVENT_MOUSEDOWN:
  104. startDrag(inputEvent->mousePosition.x,inputEvent->mousePosition.y);
  105. break;
  106. }
  107. }
  108. if(event->getDispatcher() == closeBtn) {
  109. onClose();
  110. dispatchEvent(new UIEvent(), UIEvent::CLOSE_EVENT);
  111. }
  112. if(event->getDispatcher() == windowTween) {
  113. visible = false;
  114. windowTween->removeEventListener(this, Event::COMPLETE_EVENT);
  115. }
  116. }