| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- /*
- Copyright (C) 2012 by Ivan Safrin
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
- #include "PolyUIMenu.h"
- #include "PolyUIEvent.h"
- #include "PolyInputEvent.h"
- #include "PolyLabel.h"
- #include "PolyCoreServices.h"
- #include "PolyCore.h"
- #include "PolyConfig.h"
- #include "PolySceneLine.h"
- #include "PolyRenderer.h"
- using namespace Polycode;
- UIMenuItem::UIMenuItem(String label, String _id, void *data, Number comboWidth, Number comboHeight) : UIElement() {
- this->label = label;
- Config *conf = CoreServices::getInstance()->getConfig();
-
- String fontName = conf->getStringValue("Polycode", "uiMenuFont");
- int fontSize = conf->getNumericValue("Polycode", "uiMenuFontSize");
- Number paddingX = conf->getNumericValue("Polycode", "uiMenuTextOffsetX");
- Number paddingY = conf->getNumericValue("Polycode", "uiMenuTextOffsetY");
- itemLabel = new SceneLabel(label, fontSize, fontName);
- itemLabel->setBlendingMode(Renderer::BLEND_MODE_NORMAL);
- itemLabel->setPosition(paddingX, floor(((comboHeight/2.0) - itemLabel->getHeight()/2.0) + paddingY));
- addChild(itemLabel);
- itemLabel->color.setColorHexFromString(conf->getStringValue("Polycode", "uiDefaultFontColor"));
- this->_id = _id;
- this->data = data;
- }
- String UIMenuItem::getMenuItemID() {
- return _id;
- }
- UIMenuItem::UIMenuItem() : UIElement(), data(NULL), itemLabel(NULL) {
- }
- bool UIMenuItem::isSelectable()
- {
- return true;
- }
- UIMenuItem::~UIMenuItem() {
- if(itemLabel)
- delete itemLabel;
- }
- UIMenuDivider::UIMenuDivider(Number comboWidth, Number comboHeight) : UIMenuItem() {
- Config *conf = CoreServices::getInstance()->getConfig();
- Number paddingX = conf->getNumericValue("Polycode", "uiMenuSelectorPadding");
- line = new SceneLine(Vector3(paddingX, comboHeight/2.0, 0.0), Vector3(comboWidth-paddingX, comboHeight/2.0, 0.0));
-
- // line->setLineWidth(1.0);
- line->setColor(Color(0.0, 0.0, 0.0, 0.7));
- addChild(line);
- }
- UIMenuDivider::~UIMenuDivider() {
- delete line;
- }
- bool UIMenuDivider::isSelectable()
- {
- return false;
- }
- UIMenu::UIMenu(Number menuWidth) : UIElement() {
- Config *conf = CoreServices::getInstance()->getConfig();
-
- this->menuItemHeight = conf->getNumericValue("Polycode", "uiMenuItemHeight");
- this->menuWidth = menuWidth;
- nextItemHeight = 0;
-
- paddingX = conf->getNumericValue("Polycode", "uiMenuPaddingX");
- paddingY = conf->getNumericValue("Polycode", "uiMenuPaddingY");
-
- String dropdownBgImage = conf->getStringValue("Polycode", "uiMenuBgImage");
-
- Number st = conf->getNumericValue("Polycode", "uiMenuBgT");
- Number sr = conf->getNumericValue("Polycode", "uiMenuBgR");
- Number sb = conf->getNumericValue("Polycode", "uiMenuBgB");
- Number sl = conf->getNumericValue("Polycode", "uiMenuBgL");
-
- dropDownBox = new UIBox(dropdownBgImage, st,sr,sb,sl, menuWidth, menuItemHeight);
- dropDownBox->setPosition(0,0);
- addChild(dropDownBox);
-
- String selectorBgImage = conf->getStringValue("Polycode", "uiMenuSelectorBgImage");
-
- st = conf->getNumericValue("Polycode", "uiMenuSelectorBgT");
- sr = conf->getNumericValue("Polycode", "uiMenuSelectorBgR");
- sb = conf->getNumericValue("Polycode", "uiMenuSelectorBgB");
- sl = conf->getNumericValue("Polycode", "uiMenuSelectorBgL");
-
- dropDownBox->blockMouseInput = true;
-
- selectorPadding = conf->getNumericValue("Polycode", "uiMenuSelectorPadding");
-
- selectorBox = new UIBox(selectorBgImage, st,sr,sb,sl, menuWidth - (paddingX * 2.0), menuItemHeight + (selectorPadding * 2.0));
- dropDownBox->addChild(selectorBox);
- selectorBox->blockMouseInput = true;
-
- selectorBox->visible = false;
-
- selectedOffset = 0;
- dropDownBox->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
- dropDownBox->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
- dropDownBox->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
- dropDownBox->processInputEvents = true;
-
- CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
- CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEUP);
- CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_KEYDOWN);
- initialMouse = CoreServices::getInstance()->getCore()->getInput()->getMousePosition();
-
- setWidth(menuWidth);
- setHeight(menuItemHeight);
-
- // ugh, hackz
- ignoreMouse = true;
- }
- UIMenuItem *UIMenu::getSelectedItem() {
- if(selectedOffset < 0) {
- return items[0];
- }
- if(selectedOffset > items.size()-1) {
- return items[items.size()-1];
- }
-
- if(selectedOffset >= 0 && selectedOffset < items.size()) {
- return items[selectedOffset];
- } else {
- return NULL;
- }
- }
- void UIMenu::Update() {
- ignoreMouse = false;
- }
- void UIMenu::fitToScreenVertical() {
- // Make sure the entity doesn't go past the bottom of the screen.
- if(dropDownBox->getHeight() < CoreServices::getInstance()->getCore()->getYRes()) {
- // If the entity is as high as the screen, no point trying to fit it in vertically.
- Vector2 screenPos = this->getScreenPositionForMainCamera();
- Number exceedScreenBottom = screenPos.y + dropDownBox->getHeight() - CoreServices::getInstance()->getCore()->getYRes();
- if(exceedScreenBottom > 0) {
- this->setPosition(this->getPosition().x, this->getPosition().y - exceedScreenBottom);
- } else if(screenPos.y < 0) {
- this->setPosition(this->getPosition().x, 0);
- }
- }
- }
- void UIMenu::handleEvent(Event *event) {
- if(event->getDispatcher() == CoreServices::getInstance()->getCore()->getInput()) {
-
- InputEvent *inputEvent = (InputEvent*) event;
-
- if(event->getEventCode() == InputEvent::EVENT_KEYDOWN) {
- if(inputEvent->key == KEY_ESCAPE) {
- dispatchEvent(new UIEvent(), UIEvent::CANCEL_EVENT);
- }
- }
- if((event->getEventCode() == InputEvent::EVENT_MOUSEDOWN || (event->getEventCode() == InputEvent::EVENT_MOUSEUP && initialMouse != inputEvent->getMousePosition())) && !ignoreMouse) {
- if(selectorBox->visible) {
- dispatchEvent(new UIEvent(), UIEvent::OK_EVENT);
- } else if(!dropDownBox->hitTest(inputEvent->getMousePosition())) {
- dispatchEvent(new UIEvent(), UIEvent::CANCEL_EVENT);
- }
- }
- }
-
- if(event->getDispatcher() == dropDownBox) {
- switch(event->getEventCode()) {
- case InputEvent::EVENT_MOUSEOUT:
- {
- selectorBox->visible = false;
- }
- break;
- case InputEvent::EVENT_MOUSEMOVE:
- {
- CoreServices::getInstance()->getCore()->setCursor(Core::CURSOR_ARROW);
- InputEvent *inputEvent = (InputEvent*) event;
- selectedOffset = floor(((inputEvent->getMousePosition().y-selectorPadding)-paddingY)/menuItemHeight);
-
- if(selectedOffset >= 0 && selectedOffset < items.size() && items[selectedOffset]->isSelectable()) {
- selectorBox->visible = true;
- selectorBox->setPosition(paddingX,paddingY+(selectedOffset*menuItemHeight) - selectorPadding);
- } else {
- selectorBox->visible = false;
- }
- }
- break;
- }
- }
- }
- UIMenu::~UIMenu() {
- CoreServices::getInstance()->getCore()->getInput()->removeAllHandlersForListener(this);
- dropDownBox->ownsChildren = true;
- if(!ownsChildren) {
- delete dropDownBox;
- }
- }
- UIMenuItem *UIMenu::addOption(String label, String _id, void *data) {
- UIMenuItem *newItem = new UIMenuItem(label, _id, data, menuWidth, menuItemHeight);
- items.push_back(newItem);
- dropDownBox->addChild(newItem);
- newItem->setPosition(0,paddingY+nextItemHeight);
- nextItemHeight += menuItemHeight;
- dropDownBox->resizeBox(menuWidth, nextItemHeight + (paddingY * 2.0));
- return newItem;
- }
- UIMenuItem *UIMenu::addDivider()
- {
- Number newItemHeight = menuItemHeight;
- UIMenuItem *newItem = new UIMenuDivider(menuWidth, newItemHeight);
- items.push_back(newItem);
- dropDownBox->addChild(newItem);
- newItem->setPosition(0, paddingY+nextItemHeight);
- nextItemHeight += newItemHeight;
- dropDownBox->resizeBox(menuWidth, nextItemHeight + (paddingY*2.0));
- return newItem;
- }
- void UIMenu::Resize(Number width, Number height) {
- UIElement::Resize(width, height);
- }
- UIGlobalMenu::UIGlobalMenu() : Entity() {
- currentMenu = NULL;
- processInputEvents = true;
- willHideMenu = false;
- defaultMenuWidth = 100;
- }
- UIGlobalMenu::~UIGlobalMenu() {
-
- }
- void UIGlobalMenu::hideMenu() {
- removeChild(currentMenu);
- delete currentMenu;
- currentMenu = NULL;
- willHideMenu = false;
- }
- void UIGlobalMenu::Update() {
- if(willHideMenu) {
- hideMenu();
- }
- }
- void UIGlobalMenu::handleEvent(Event *event) {
- if(event->getDispatcher() == currentMenu && event->getEventType() == "UIEvent") {
- if(event->getEventCode() == UIEvent::OK_EVENT) {
- willHideMenu = true;
- }
- if(event->getEventCode() == UIEvent::CANCEL_EVENT) {
- willHideMenu = true;
- }
- }
- }
- UIMenu *UIGlobalMenu::showMenuAtMouse(Number width) {
- Vector2 pos = CoreServices::getInstance()->getCore()->getInput()->getMousePosition();
- return showMenu(pos.x, pos.y, width);
- }
- UIMenu *UIGlobalMenu::showMenu(Number x, Number y, Number width) {
- if(currentMenu) {
- hideMenu();
- }
- currentMenu = new UIMenu(width);
-
- currentMenu->addEventListener(this, UIEvent::OK_EVENT);
- currentMenu->addEventListener(this, UIEvent::CANCEL_EVENT);
-
- addChild(currentMenu);
- currentMenu->setPosition(x,y);
- return currentMenu;
-
- }
|