| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- /*
- 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 "PolyUIElement.h"
- #include "PolyRenderer.h"
- #include "PolyCoreServices.h"
- #include "PolyTexture.h"
- #include "PolyConfig.h"
- using namespace Polycode;
- UIElement *UIElement::globalFocusedChild = NULL;
- UIMultilineLabel::UIMultilineLabel(const String& text, int size, int spacing, const String& fontName, int amode) : UIElement() {
- labelSize = size;
- labelFontName = fontName;
- labelAAMode = amode;
- this->spacing = spacing;
- setText(text);
- }
- void UIMultilineLabel::setText(const String& text) {
- clearLabels();
-
- std::vector<String> lines = text.split("\n");
-
- Number lineSize = spacing;
- Number yPos = 0.0;
- for(int i=0; i < lines.size(); i++) {
- if(lines[i] == "") {
- yPos += lineSize + spacing;
- } else {
- UILabel *label = new UILabel(lines[i], labelSize, labelFontName, labelAAMode);
- lineSize = label->getHeight();
- addChild(label);
- label->setPositionY(yPos);
- yPos += label->getHeight() + spacing;
- addChild(label);
- }
- }
- }
- String UIMultilineLabel::getText() {
- String text;
- for(int i=0; i < labels.size(); i++) {
- if(i != 0) {
- text += "\n";
- }
- text += labels[i]->getText();
- }
- return text;
- }
- void UIMultilineLabel::clearLabels() {
- for(int i=0; i < labels.size(); i++) {
- removeChild(labels[i]);
- delete labels[i];
- }
- labels.clear();
- }
- UIMultilineLabel::~UIMultilineLabel() {
- if(!ownsChildren) {
- clearLabels();
- }
- }
- UILabel::UILabel(const String& text, int size, const String& fontName, int amode) : UIElement() {
- Config *conf = CoreServices::getInstance()->getConfig();
- label = new SceneLabel(text, size, fontName, amode);
- label->setBlendingMode(Renderer::BLEND_MODE_NORMAL);
- label->positionAtBaseline = true;
- label->setAnchorPoint(-1.0, -1.0, 0.0);
- label->snapToPixels = true;
-
- color.setColorHexFromString(conf->getStringValue("Polycode", "uiDefaultFontColor"));
- addChild(label);
- setLocalBoundingBox(label->getLocalBoundingBox());
- }
- void UILabel::setText(const String& text) {
- label->setText(text);
- setLocalBoundingBox(label->getLocalBoundingBox());
- }
- String UILabel::getText() {
- return label->getText();
- }
- UILabel::~UILabel() {
- if(!ownsChildren) {
- delete label;
- }
- }
- Label *UILabel::getLabel() {
- return label->getLabel();
- }
- UIRect::UIRect(String fileName, Number width, Number height) : UIElement() {
- texture = NULL;
- loadTexture(fileName);
- initRect(width, height);
- imageWidth = width;
- imageHeight = height;
- }
- UIRect::UIRect(String fileName) : UIElement() {
- texture = NULL;
- loadTexture(fileName);
- if(texture) {
- initRect(texture->getWidth(), texture->getHeight());
- imageWidth = texture->getWidth();
- imageHeight = texture->getHeight();
- } else {
- initRect(1, 1);
- imageWidth = 0;
- imageHeight = 0;
- }
- setWidth(imageWidth);
- setHeight(imageHeight);
- }
- UIRect::UIRect(Number width, Number height) : UIElement() {
- texture = NULL;
- initRect(width, height);
- imageWidth = 0;
- imageHeight = 0;
- }
- void UIRect::setImageCoordinates(Number x, Number y, Number width, Number height, Number imageScale) {
- Vertex *vertex;
- Number pixelSizeX = 1/imageWidth;
- Number pixelSizeY = 1/imageHeight;
- setWidth(width);
- setHeight(height);
- Number whalf = width/2.0f;
- Number hhalf = height/2.0f;
-
- Number xFloat = x * pixelSizeX * imageScale;
- Number yFloat = (y * pixelSizeY) * imageScale;
- Number wFloat = width * pixelSizeX * imageScale;
- Number hFloat = height * pixelSizeY * imageScale;
- vertex = rectMesh->getVertex(0);
- vertex->set(-whalf,-hhalf,0);
- vertex->setTexCoord(xFloat, (1.0-yFloat) - hFloat);
- vertex = rectMesh->getVertex(1);
- vertex->set(-whalf+width,-hhalf,0);
- vertex->setTexCoord(xFloat + wFloat, (1.0-yFloat) - hFloat);
- vertex = rectMesh->getVertex(2);
- vertex->set(-whalf+width,-hhalf+height,0);
- vertex->setTexCoord(xFloat + wFloat, 1.0-yFloat);
- vertex = rectMesh->getVertex(3);
- vertex->set(-whalf,-hhalf+height,0);
- vertex->setTexCoord(xFloat, 1.0-yFloat);
- rectMesh->arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
- rectMesh->arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
- rebuildTransformMatrix();
- matrixDirty = true;
- }
- Number UIRect::getImageWidth() const {
- return imageWidth;
- }
- Number UIRect::getImageHeight() const {
- return imageHeight;
- }
- void UIRect::initRect(Number width, Number height) {
- rectMesh = new Mesh(Mesh::QUAD_MESH);
- processInputEvents = true;
- setAnchorPoint(-1.0, -1.0, 0.0);
- setWidth(width);
- setHeight(height);
-
- Number whalf = width/2.0f;
- Number hhalf = height/2.0f;
-
- rectMesh->addVertex(-whalf,-hhalf,0,0,0);
- rectMesh->addVertex(-whalf+width,-hhalf,0, 1, 0);
- rectMesh->addVertex(-whalf+width,-hhalf+height,0, 1, 1);
- rectMesh->addVertex(-whalf,-hhalf+height,0,0,1);
- }
- UIRect::~UIRect() {
- delete rectMesh;
- }
- void UIRect::loadTexture(String fileName) {
- MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
- texture = materialManager->createTextureFromFile(fileName, materialManager->clampDefault, false);
- }
- void UIRect::setTexture(Texture *texture) {
- this->texture = texture;
- }
- Texture *UIRect::getTexture() {
- return texture;
- }
- void UIRect::Render() {
- renderer->clearShader();
- renderer->setTexture(texture);
- renderer->pushDataArrayForMesh(rectMesh, RenderDataArray::VERTEX_DATA_ARRAY);
- renderer->pushDataArrayForMesh(rectMesh, RenderDataArray::TEXCOORD_DATA_ARRAY);
- renderer->drawArrays(Mesh::QUAD_MESH);
- }
- void UIRect::Resize(Number width, Number height) {
- setWidth(width);
- setHeight(height);
- Number whalf = width/2.0f;
- Number hhalf = height/2.0f;
- Vertex *vertex;
- vertex = rectMesh->getVertex(0);
- vertex->set(-whalf,-hhalf,0);
- vertex = rectMesh->getVertex(1);
- vertex->set(-whalf+width,-hhalf,0);
- vertex = rectMesh->getVertex(2);
- vertex->set(-whalf+width,-hhalf+height,0);
- vertex = rectMesh->getVertex(3);
- vertex->set(-whalf,-hhalf+height,0);
- rectMesh->arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
- }
- UIImage::UIImage(String imagePath, int width, int height) : UIRect(imagePath, width, height) {
- setBlendingMode(Renderer::BLEND_MODE_NORMAL);
- }
- UIImage::UIImage(String imagePath) : UIRect(imagePath) {
- setBlendingMode(Renderer::BLEND_MODE_NORMAL);
- }
- UIElement::UIElement() : Entity() {
- snapToPixels = true;
- setAnchorPoint(-1.0, -1.0, 0.0);
- processInputEvents = true;
- depthTest = false;
- hasFocus = false;
- focusable = false;
- focusParent = NULL;
- hasDragLimits = false;
- dragged = false;
- depthTest = false;
- depthWrite = false;
- }
- UIElement::UIElement(Number width, Number height) : Entity() {
- setAnchorPoint(-1.0, -1.0, 0.0);
- processInputEvents = true;
- focusParent = NULL;
- hasFocus = false;
- hasDragLimits = false;
- dragged = false;
- depthTest = false;
- depthWrite = false;
- setWidth(width);
- setHeight(height);
- }
- void UIElement::addChild(Entity *child) {
- UIElement* uiChild = dynamic_cast<UIElement*>(child);
- if(uiChild) {
- addFocusChild(uiChild);
- }
- Entity::addChild(child);
- }
- void UIElement::setDragLimits(Rectangle rect) {
- dragLimits.x = rect.x;
- dragLimits.y = rect.y;
- dragLimits.w = rect.w;
- dragLimits.h = rect.h;
- hasDragLimits = true;
- }
- void UIElement::clearDragLimits() {
- hasDragLimits = false;
- }
- bool UIElement::isDragged() {
- return dragged;
- }
- void UIElement::startDrag(Number xOffset, Number yOffset) {
- dragged = true;
- dragOffsetX = xOffset;
- dragOffsetY = yOffset;
- }
- void UIElement::stopDrag() {
- dragged = false;
- }
- bool UIElement::isFocusable() {
- return focusable;
- }
- MouseEventResult UIElement::onMouseMove(const Ray &ray, int timestamp) {
- if(dragged) {
- Vector3 localCoordinate = Vector3(ray.origin.x, ray.origin.y,0);
- if(parentEntity) {
- Matrix4 inverse = parentEntity->getConcatenatedMatrix().Inverse();
- localCoordinate = inverse * localCoordinate;
- }
- setPosition(localCoordinate.x-dragOffsetX,-localCoordinate.y-dragOffsetY);
- if(hasDragLimits) {
- if(position.x < dragLimits.x)
- position.x = dragLimits.x;
- if(position.x > dragLimits.x + dragLimits.w)
- position.x = dragLimits.x + dragLimits.w;
- if(position.y < dragLimits.y)
- position.y = dragLimits.y;
- if(position.y > dragLimits.y + dragLimits.h)
- position.y = dragLimits.y + dragLimits.h;
- }
-
- }
- return Entity::onMouseMove(ray, timestamp);
- }
- UIElement::~UIElement() {
- if(UIElement::globalFocusedChild == this) {
- UIElement::globalFocusedChild = NULL;
- }
- }
- void UIElement::focusNextChild() {
- int j = 0;
- bool hasFocusedChild = false;
- if(UIElement::globalFocusedChild) {
- for(int i=0; i < focusChildren.size(); i++) {
- if(focusChildren[i] == UIElement::globalFocusedChild) {
- j = i;
- hasFocusedChild = true;
- }
- }
- }
- if(!hasFocusedChild)
- return;
- for(int i=0; i < focusChildren.size(); i++) {
- if(focusChildren[j]->isFocusable() && focusChildren[j] != UIElement::globalFocusedChild) {
- focusChild(focusChildren[j]);
- return;
- }
- j++;
- if(j == focusChildren.size())
- j = 0;
- }
- }
- void UIElement::focusSelf() {
- if(UIElement::globalFocusedChild) {
- UIElement::globalFocusedChild->onLoseFocus();
- UIElement::globalFocusedChild->hasFocus = false;
- }
- UIElement::globalFocusedChild = this;
-
- onGainFocus();
- hasFocus = true;
- }
- void UIElement::focusChild(UIElement *child) {
- if(UIElement::globalFocusedChild) {
- UIElement::globalFocusedChild->onLoseFocus();
- UIElement::globalFocusedChild->hasFocus = false;
- }
- UIElement::globalFocusedChild = child;
-
- if(child) {
- UIElement::globalFocusedChild->onGainFocus();
- UIElement::globalFocusedChild->hasFocus = true;
- }
- }
- void UIElement::addFocusChild(UIElement *element) {
- element->setFocusParent(element);
- focusChildren.push_back(element);
- }
- void UIElement::setFocusParent(UIElement *element) {
- focusParent = element;
- }
- void UIElement::Resize(Number width, Number height) {
- setWidth(width);
- setHeight(height);
- dirtyMatrix(true);
- }
|