| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- /*
- 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 "PolyUIColorBox.h"
- #include "PolyInputEvent.h"
- #include "PolyLabel.h"
- #include "PolyCoreServices.h"
- #include "PolyConfig.h"
- using namespace Polycode;
- UIColorPicker::UIColorPicker() : UIWindow(L"Pick a color!", 300, 240) {
- closeOnEscape = true;
-
- // topPadding
- Config *conf = CoreServices::getInstance()->getConfig();
-
-
- String fontName = conf->getStringValue("Polycode", "uiDefaultFontName");
- int fontSize = conf->getNumericValue("Polycode", "uiDefaultFontSize");
- String mainBgImage = conf->getStringValue("Polycode", "uiColorPickerMainBg");
- String mainFrameImage = conf->getStringValue("Polycode", "uiColorPickerMainFrame");
- String hueFrameImage = conf->getStringValue("Polycode", "uiColorPickerHueFrame");
- String hueSelectorImage = conf->getStringValue("Polycode", "uiColorPickerHueSelector");
- String mainSelectorImage = conf->getStringValue("Polycode", "uiColorPickerMainSelector");
- mainBg = new ScreenImage(mainBgImage);
- mainBg->setPosition(padding, topPadding+padding);
- addChild(mainBg);
- mainFrame = new ScreenImage(mainFrameImage);
- mainFrame->setPosition(padding, topPadding+padding);
-
- alphaSlider = new UIHSlider(0, 1.0, mainFrame->getWidth());
- alphaSlider->setPosition(padding, mainFrame->getHeight() + mainFrame->getPosition().y + 13);
- addChild(alphaSlider);
- alphaSlider->addEventListener(this, UIEvent::CHANGE_EVENT);
-
- mainColorRect = new ScreenShape(ScreenShape::SHAPE_RECT, mainFrame->getWidth(), mainFrame->getHeight());
- mainColorRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
- mainColorRect->setPosition(padding+1, topPadding+padding+1);
- addChild(mainColorRect);
- addChild(mainFrame);
- hueFrame = new ScreenImage(hueFrameImage);
- hueFrame->setPosition(mainFrame->getPosition().x + mainFrame->getWidth()+10, topPadding+padding);
- addChild(hueFrame);
-
- hueSelector = new ScreenImage(hueSelectorImage);
- hueSelector->setPositionMode(ScreenEntity::POSITION_CENTER);
- hueSelector->setPosition(hueFrame->getPosition().x + (hueFrame->getWidth()/2.0), hueFrame->getPosition().y);
- addChild(hueSelector);
- hueSelector->setDragLimits(Polycode::Rectangle(hueSelector->getPosition().x,hueSelector->getPosition().y,0,hueFrame->getHeight()));
-
- mainSelector = new ScreenImage(mainSelectorImage);
- mainSelector->setPositionMode(ScreenEntity::POSITION_CENTER);
- mainSelector->setPosition(mainFrame->getPosition());
- addChild(mainSelector);
-
- mainColorRect->getMesh()->useVertexColors = true;
-
- mainColorRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
- mainColorRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
- mainColorRect->addEventListener(this, InputEvent::EVENT_MOUSEUP_OUTSIDE);
- mainColorRect->processInputEvents = true;
-
-
- hueFrame->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
- hueFrame->addEventListener(this, InputEvent::EVENT_MOUSEUP);
- hueFrame->addEventListener(this, InputEvent::EVENT_MOUSEUP_OUTSIDE);
- hueFrame->processInputEvents = true;
-
- ScreenLabel *label = new ScreenLabel(L"R:", fontSize, fontName);
- label->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 15, topPadding+padding + 3);
- addChild(label);
-
- rTextInput = new UITextInput(false, 40, 12);
- rTextInput->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 30, topPadding+padding);
- addChild(rTextInput);
- rTextInput->addEventListener(this, UIEvent::CHANGE_EVENT);
- label = new ScreenLabel(L"G:", fontSize, fontName);
- label->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 15, topPadding+padding + 33);
- addChild(label);
-
- gTextInput = new UITextInput(false, 40, 12);
- gTextInput->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 30, topPadding+padding + 30);
- addChild(gTextInput);
- gTextInput->addEventListener(this, UIEvent::CHANGE_EVENT);
-
- label = new ScreenLabel(L"B:", fontSize, fontName);
- label->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 15, topPadding+padding + 63);
- addChild(label);
-
- bTextInput = new UITextInput(false, 40, 12);
- bTextInput->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 30, topPadding+padding + 60);
- addChild(bTextInput);
- bTextInput->addEventListener(this, UIEvent::CHANGE_EVENT);
-
- label = new ScreenLabel(L"A:", fontSize, fontName);
- label->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 15, topPadding+padding + 93);
- addChild(label);
-
- aTextInput = new UITextInput(false, 40, 12);
- aTextInput->setPosition(hueFrame->getPosition().x+hueFrame->getWidth() + 30, topPadding+padding + 90);
- addChild(aTextInput);
- aTextInput->addEventListener(this, UIEvent::CHANGE_EVENT);
-
- setHue(0.0);
- setSaturationAndValue(0.0, 0.0);
-
- rTextInput->setNumberOnly(true);
- gTextInput->setNumberOnly(true);
- bTextInput->setNumberOnly(true);
- aTextInput->setNumberOnly(true);
-
- lastHueSelectorPosition = 0;
-
- mainSelector->setDragLimits(Polycode::Rectangle(mainColorRect->getPosition().x,mainColorRect->getPosition().y,mainColorRect->getWidth(), mainColorRect->getHeight()));
-
- colorAlpha = 1.0;
-
- visible = false;
- enabled = false;
-
- }
- void UIColorPicker::cancelColorListeners() {
- dispatchEvent(new Event(), Event::CANCEL_EVENT);
- }
- void UIColorPicker::onClose() {
- visible = false;
- enabled = false;
- }
- void UIColorPicker::rebuildFromTextInputs() {
- int newR = atoi(rTextInput->getText().c_str());
- int newG = atoi(gTextInput->getText().c_str());
- int newB = atoi(bTextInput->getText().c_str());
- int newA = atoi(aTextInput->getText().c_str());
-
-
- if(newR < 0)
- newR = 0;
- if(newR > 255)
- newR = 255;
- if(newG < 0)
- newG = 0;
- if(newG > 255)
- newG = 255;
- if(newB < 0)
- newB = 0;
- if(newB > 255)
- newB = 255;
- if(newA < 0)
- newA = 0;
- if(newA > 255)
- newA = 255;
-
- Color newColor;
- newColor.setColorRGBA(newR, newG, newB, newA);
- currentS = newColor.getSaturation();
- currentV = newColor.getValue();
- currentH = newColor.getHue();
- colorAlpha = newColor.a;
-
- updateSelectedColor(false);
- }
- void UIColorPicker::setPickerColor(Color newColor) {
- currentS = newColor.getSaturation();
- currentV = newColor.getValue();
- currentH = newColor.getHue();
- colorAlpha = newColor.a;
- updateSelectedColor();
- }
- void UIColorPicker::setSaturationAndValue(Number S, Number V) {
- currentS = S;
- currentV = V;
- updateSelectedColor();
- }
- void UIColorPicker::updateSelectedColor(bool updateTextFields) {
- selectedColor.setColorHSV(currentH, currentS, currentV);
- selectedColor.a = colorAlpha;
-
- int newR = floor(selectedColor.r*255.0);
- int newG = floor(selectedColor.g*255.0);
- int newB = floor(selectedColor.b*255.0);
- int newA = floor(selectedColor.a*255.0);
-
- mainColorRect->color.a = colorAlpha;
-
- if(updateTextFields) {
- rTextInput->setText(String::IntToString(newR));
- gTextInput->setText(String::IntToString(newG));
- bTextInput->setText(String::IntToString(newB));
- aTextInput->setText(String::IntToString(newA));
- }
-
- Color hueCol;
- hueCol.setColorHSV(currentH, 1.0, 1.0);
- hueCol.a = colorAlpha;
- mainColorRect->getMesh()->getPolygon(0)->getVertex(0)->vertexColor = Color(1.0,1.0,1.0,colorAlpha);
- mainColorRect->getMesh()->getPolygon(0)->getVertex(1)->vertexColor = hueCol;
- mainColorRect->getMesh()->getPolygon(0)->getVertex(2)->vertexColor = Color(0.0,0.0,0.0,colorAlpha);
- mainColorRect->getMesh()->getPolygon(0)->getVertex(3)->vertexColor = Color(0.0,0.0,0.0,colorAlpha);
- mainColorRect->getMesh()->arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;
-
-
- hueSelector->setPositionY(hueFrame->getPosition().y + hueFrame->getHeight() - ((currentH/360.0) * hueFrame->getHeight()));
- lastHueSelectorPosition = hueSelector->getPosition().y;
-
- mainSelector->setPosition(mainColorRect->getPosition().x + (currentS * mainColorRect->getWidth()), mainColorRect->getPosition().y + mainColorRect->getHeight() - (currentV * mainColorRect->getHeight()));
- lastMainSelectorPosition = mainSelector->getPosition2D();
-
- alphaSlider->setSliderValue(colorAlpha);
- dispatchEvent(new Event(), Event::CHANGE_EVENT);
- }
- Color UIColorPicker::getSelectedColor() {
- return selectedColor;
- }
- void UIColorPicker::handleEvent(Event *event) {
- if((event->getDispatcher() == rTextInput || event->getDispatcher() == gTextInput || event->getDispatcher() == bTextInput || event->getDispatcher() == aTextInput) && event->getEventType() == "UIEvent") {
- switch(event->getEventCode()) {
- case UIEvent::CHANGE_EVENT:
- {
- rebuildFromTextInputs();
- }
- break;
- }
- }
- if(event->getDispatcher() == alphaSlider) {
- switch(event->getEventCode()) {
- case UIEvent::CHANGE_EVENT:
- {
- colorAlpha = alphaSlider->getSliderValue();
- updateSelectedColor();
- }
- break;
- }
- }
- if(event->getDispatcher() == hueFrame) {
- switch(event->getEventCode()) {
- case InputEvent::EVENT_MOUSEDOWN:
- {
- InputEvent *inputEvent = (InputEvent*) event;
- hueSelector->setPositionY(inputEvent->getMousePosition().y);
- hueSelector->startDrag(inputEvent->mousePosition.x-hueSelector->getPosition().x,inputEvent->mousePosition.y-hueSelector->getPosition().y);
- Number newHue = 360.0 - (((inputEvent->getMousePosition().y-hueFrame->getPosition().y)/((hueFrame->getPosition().y+hueFrame->getHeight())-hueFrame->getPosition().y)) * 360.0f);
- setHue(newHue);
-
- }
- break;
- case InputEvent::EVENT_MOUSEUP:
- case InputEvent::EVENT_MOUSEUP_OUTSIDE:
- {
- hueSelector->stopDrag();
- }
- break;
- }
- }
- if(event->getDispatcher() == mainColorRect) {
- switch(event->getEventCode()) {
- case InputEvent::EVENT_MOUSEDOWN:
- {
- InputEvent *inputEvent = (InputEvent*) event;
- mainSelector->setPosition(inputEvent->getMousePosition().x, inputEvent->getMousePosition().y);
- mainSelector->startDrag(inputEvent->mousePosition.x-mainSelector->getPosition().x,inputEvent->mousePosition.y-mainSelector->getPosition().y);
-
- Number newV = 1.0 - ((inputEvent->getMousePosition().y-mainColorRect->getPosition().y)/((mainColorRect->getPosition().y+mainColorRect->getHeight())-mainColorRect->getPosition().y));
- Number newS = ((inputEvent->getMousePosition().x-mainColorRect->getPosition().x)/((mainColorRect->getPosition().x+mainColorRect->getWidth())-mainColorRect->getPosition().x));
-
- setSaturationAndValue(newS, newV);
- }
- break;
- case InputEvent::EVENT_MOUSEUP:
- case InputEvent::EVENT_MOUSEUP_OUTSIDE:
- {
- mainSelector->stopDrag();
- }
- break;
- }
- }
-
- UIWindow::handleEvent(event);
- }
- void UIColorPicker::setHue(Number hueNum) {
- if(hueNum < 0.0)
- hueNum = 0.0;
- if(hueNum >= 360.0)
- hueNum = 359.9999;
-
- currentH = hueNum;
- updateSelectedColor();
- }
- UIColorPicker::~UIColorPicker() {
- }
- void UIColorPicker::Update() {
- if(mainSelector->getPosition2D() != lastMainSelectorPosition) {
- lastMainSelectorPosition = mainSelector->getPosition2D();
-
- Number newPosX = lastMainSelectorPosition.x;
- Number newPosY = lastMainSelectorPosition.y;
-
- Number newV = 1.0 - ((newPosY-mainColorRect->getPosition().y)/((mainColorRect->getPosition().y+mainColorRect->getHeight())-mainColorRect->getPosition().y));
- Number newS = ((newPosX-mainColorRect->getPosition().x)/((mainColorRect->getPosition().x+mainColorRect->getWidth())-mainColorRect->getPosition().x));
-
- setSaturationAndValue(newS, newV);
- }
-
- if(hueSelector->getPosition().y != lastHueSelectorPosition) {
- lastHueSelectorPosition = hueSelector->getPosition().y;
-
- Number newPosY = lastHueSelectorPosition;
-
- if(newPosY < hueFrame->getPosition().y)
- newPosY = hueFrame->getPosition().y;
- if(newPosY > hueFrame->getHeight() + hueFrame->getPosition().y)
- newPosY = hueFrame->getHeight() + hueFrame->getPosition().y;
-
- hueSelector->setPositionY(newPosY);
-
- Number newHue = 360.0 - (((newPosY-hueFrame->getPosition().y)/((hueFrame->getPosition().y+hueFrame->getHeight())-hueFrame->getPosition().y)) * 360.0f);
- setHue(newHue);
- }
- UIWindow::Update();
- }
- UIColorBox::UIColorBox(UIColorPicker *colorPicker, Color initialColor, Number width, Number height) : UIElement() {
- Config *conf = CoreServices::getInstance()->getConfig();
- String frameImageFile = conf->getStringValue("Polycode", "uiColorBoxFrameImage");
- String bgImageFile = conf->getStringValue("Polycode", "uiColorBoxBgImage");
- Number frameInset = conf->getNumericValue("Polycode", "uiColorBoxFrameInset");
- Number st = conf->getNumericValue("Polycode", "uiColorBoxFrameImageT");
- Number sr = conf->getNumericValue("Polycode", "uiColorBoxFrameImageR");
- Number sb = conf->getNumericValue("Polycode", "uiColorBoxFrameImageB");
- Number sl = conf->getNumericValue("Polycode", "uiColorBoxFrameImageL");
- bgImage = new ScreenShape(ScreenShape::SHAPE_RECT, width-(frameInset*2), height-(frameInset*2));
- bgImage->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
- bgImage->loadTexture(bgImageFile);
- bgImage->setPosition(frameInset, frameInset);
- addChild(bgImage);
- bgImage->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
- bgImage->processInputEvents = true;
- colorShape = new ScreenShape(ScreenShape::SHAPE_RECT, width-(frameInset*2), height-(frameInset*2));
- colorShape->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
- colorShape->setPosition(frameInset, frameInset);
- addChild(colorShape);
-
- frameImage = new UIBox(frameImageFile, st,sr,sb,sl, width, height);
- addChild(frameImage);
-
- this->colorPicker = colorPicker;
- colorPicker->addEventListener(this, Event::CHANGE_EVENT);
- colorPicker->addEventListener(this, Event::CANCEL_EVENT);
-
- this->width = width;
- this->height = height;
-
- selectedColor = initialColor;
- colorShape->color = selectedColor;
-
- listeningToPicker = false;
- }
- Color UIColorBox::getSelectedColor() {
- return selectedColor;
- }
- UIColorBox::~UIColorBox() {
- colorPicker->removeAllHandlersForListener(this);
- }
- void UIColorBox::setBoxColor(Color newColor) {
- selectedColor = newColor;
- colorShape->color = selectedColor;
- if(listeningToPicker) {
- colorPicker->setPickerColor(newColor);
- }
- }
- void UIColorBox::showColorPicker() {
- colorPicker->visible = true;
- colorPicker->enabled = true;
- colorPicker->cancelColorListeners();
- colorPicker->setPickerColor(selectedColor);
- listeningToPicker = true;
- }
-
- void UIColorBox::handleEvent(Event *event) {
- if(event->getDispatcher() == colorPicker && event->getEventType() == "Event") {
- switch(event->getEventCode()) {
- case Event::CANCEL_EVENT:
- listeningToPicker = false;
- break;
- case Event::CHANGE_EVENT:
- if(listeningToPicker) {
- selectedColor = colorPicker->getSelectedColor();
- colorShape->color = selectedColor;
- dispatchEvent(new UIEvent(), UIEvent::CHANGE_EVENT);
- }
- break;
- }
- }
- if(event->getDispatcher() == bgImage) {
- switch(event->getEventCode()) {
- case InputEvent::EVENT_MOUSEDOWN:
- showColorPicker();
- break;
- }
- }
- }
|