| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- /*
- 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"
- using namespace Polycode;
- UIElement *UIElement::globalFocusedChild = NULL;
- UIImage::UIImage(String imagePath) : UIElement() {
- image = new SceneImage(imagePath);
- image->setAnchorPoint(-1.0, -1.0, 0.0);
- image->depthTest = false;
- image->depthWrite = false;
- addChild(image);
- setWidth(image->bBox.x);
- setHeight(image->bBox.y);
- }
- SceneImage *UIImage::getImage() {
- return image;
- }
- UIElement::UIElement() : Entity() {
- 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() {
- }
- 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);
- }
|