| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114 |
- /*
- Copyright (C) 2011 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 "PolyEntity.h"
- #include "PolyRenderer.h"
- #include "PolyCoreServices.h"
- #include "PolyInputEvent.h"
- using namespace Polycode;
- int Entity::defaultBlendingMode = Renderer::BLEND_MODE_NORMAL;
- Rotation::Rotation() {
- pitch = 0;
- yaw = 0;
- roll = 0;
- }
- Entity::Entity() : EventDispatcher() {
- initEntity();
- }
- Entity::Entity(Number width, Number height, Number depth) : EventDispatcher() {
- initEntity();
- bBox.x = width;
- bBox.y = height;
- bBox.z = depth;
- }
- void Entity::initEntity() {
- userData = NULL;
- scale.set(1,1,1);
- renderer = NULL;
- enabled = true;
- depthTest = true;
- visible = true;
- bBoxRadius = 0;
- color.setColor(1.0f,1.0f,1.0f,1.0f);
- parentEntity = NULL;
- matrixDirty = true;
- billboardMode = false;
- billboardRoll = false;
- billboardIgnoreScale = false;
- backfaceCulled = true;
- depthOnly = false;
- depthWrite = true;
- ignoreParentMatrix = false;
- alphaTest = false;
- blendingMode = Entity::defaultBlendingMode;
- lockMatrix = false;
- colorAffectsChildren = true;
- visibilityAffectsChildren = true;
- ownsChildren = false;
- enableScissor = false;
- processInputEvents = false;
- blockMouseInput = false;
- editorOnly = false;
- snapToPixels = false;
- tags = NULL;
- bBox.z = 0.001;
- mouseOver = false;
- yAdjust = 1.0;
- lastClickTicks = 0.0;
- }
- Entity *Entity::getEntityById(String id, bool recursive) const {
- for(int i=0;i<children.size();i++) {
- if(children[i]->id == id) {
- return children[i];
- } else {
- if(recursive) {
- Entity *ret = children[i]->getEntityById(id, recursive);
- if(ret) {
- return ret;
- }
- }
- }
- }
- return NULL;
- }
- Entity *Entity::Clone(bool deepClone, bool ignoreEditorOnly) const {
- Entity *newEntity = new Entity();
- applyClone(newEntity, deepClone, ignoreEditorOnly);
- return newEntity;
- }
- void Entity::applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) const {
- clone->ownsChildren = ownsChildren;
- clone->position = position;
- clone->rotation = rotation;
- clone->scale = scale;
- clone->color = color;
- clone->custEntityType = custEntityType;
- clone->billboardMode = billboardMode;
- clone->billboardRoll = billboardRoll;
- clone->alphaTest = alphaTest;
- clone->backfaceCulled = backfaceCulled;
- clone->depthWrite = depthWrite;
- clone->depthTest = depthTest;
- clone->blendingMode = blendingMode;
- clone->colorAffectsChildren = colorAffectsChildren;
- clone->visibilityAffectsChildren = visibilityAffectsChildren;
- clone->depthOnly = depthOnly;
- clone->setUserData(getUserData());
- clone->entityProps = entityProps;
- clone->bBox = bBox;
- clone->ignoreParentMatrix = ignoreParentMatrix;
- clone->enableScissor = enableScissor;
- clone->scissorBox = scissorBox;
- clone->editorOnly = editorOnly;
- clone->id = id;
- if(tags == NULL) {
- clone->tags = NULL;
- } else {
- clone->tags = new std::vector<String>();
- for(int i=0; i < tags->size(); i++) {
- clone->addTag((*tags)[i]);
- }
- }
- clone->setRenderer(renderer);
- if(deepClone) {
- for(int i=0; i < children.size(); i++) {
- if(children[i]->editorOnly && ignoreEditorOnly) {
- } else {
- Entity *childClone = children[i]->Clone(deepClone, ignoreEditorOnly);
- clone->addChild(childClone);
- }
- }
- }
- }
- void Entity::setOwnsChildrenRecursive(bool val) {
- ownsChildren = val;
- for(int i=0; i < children.size(); i++) {
- children[i]->setOwnsChildrenRecursive(val);
- }
- }
- std::vector<Entity*> Entity::getEntitiesByTag(String tag, bool recursive) const {
- std::vector<Entity*> retVector;
- for(int i=0;i<children.size();i++) {
- if(children[i]->hasTag(tag)) {
- retVector.push_back(children[i]);
- }
-
- if(recursive) {
- std::vector<Entity*> childVector = children[i]->getEntitiesByTag(tag, recursive);
- retVector.insert(retVector.end(), childVector.begin(), childVector.end());
- }
- }
-
- return retVector;
- }
- void Entity::setUserData(void *userData) {
- this->userData = userData;
- }
- void *Entity::getUserData() const {
- return userData;
- }
- Entity *Entity::getParentEntity() const {
- return parentEntity;
- }
- Color Entity::getCombinedColor() const {
- if(parentEntity) {
- if(parentEntity->colorAffectsChildren)
- return color * parentEntity->getCombinedColor();
- else
- return color;
- } else {
- return color;
- }
- }
- Matrix4 Entity::getLookAtMatrix(const Vector3 &loc, const Vector3 &upVector) {
- rebuildTransformMatrix();
- Vector3 D;
-
- D = loc - position;
-
- Vector3 back = D * -1;
- back.Normalize();
-
- Vector3 right = back.crossProduct(upVector) ;
- right.Normalize();
- right = right * -1;
-
- Vector3 up = back.crossProduct(right);
-
- Matrix4 newMatrix(right.x, right.y, right.z, 0,
- up.x, up.y, up.z, 0,
- back.x, back.y, back.z, 0,
- 0, 0 , 0, 1);
- return newMatrix;
- }
- void Entity::lookAt(const Vector3 &loc, const Vector3 &upVector) {
- Matrix4 newMatrix = getLookAtMatrix(loc, upVector);
- rotationQuat.createFromMatrix(newMatrix);
- matrixDirty = true;
- }
- void Entity::lookAtEntity(Entity *entity, const Vector3 &upVector) {
- if(entity->getParentEntity())
- lookAt(entity->getParentEntity()->getConcatenatedMatrix() * (entity->getPosition()), upVector);
- else
- lookAt(entity->getPosition(), upVector);
- }
- void Entity::removeChild(Entity *entityToRemove) {
- for(int i=0;i<children.size();i++) {
- if(children[i] == entityToRemove) {
- children.erase(children.begin()+i);
- return;
- }
- }
- }
- void Entity::moveChildUp(Entity *child) {
- for(int i=0; i < children.size(); i++) {
- if(children[i] == child && i < children.size()-1) {
- Entity *next = (Entity*)children[i+1];
- children[i+1] = child;
- children[i] = next;
- break;
- }
- }
- }
- void Entity::moveChildDown(Entity *child) {
- for(int i=0; i < children.size(); i++) {
- if(children[i] == child && i > 0) {
- Entity *prev = (Entity*)children[i-1];
- children[i-1] = child;
- children[i] = prev;
- break;
- }
- }
- }
- void Entity::moveChildTop(Entity *child) {
- for(int i=0; i < children.size(); i++) {
- if(children[i] == child && i < children.size()-1) {
- children.erase(children.begin()+i);
- children.push_back(child);
- break;
- }
- }
- }
- void Entity::moveChildBottom(Entity *child) {
- for(int i=0; i < children.size(); i++) {
- if(children[i] == child && i > 0) {
- children.erase(children.begin()+i);
- children.insert(children.begin(), child);
- break;
- }
- }
- }
- unsigned int Entity::getNumChildren() {
- return children.size();
- }
- Entity *Entity::getChildAtIndex(unsigned int index) {
- if(index < children.size()) {
- return children[index];
- }
- return NULL;
- }
- void Entity::addChild(Entity *newChild) {
- newChild->setRenderer(renderer);
- newChild->setParentEntity(this);
- newChild->setInverseY(getInverseY());
- children.push_back(newChild);
- }
- void Entity::setColor(Color color) {
- this->color.setColor(&color);
- }
- void Entity::setColorInt(int r, int g, int b, int a) {
- color.setColorRGBA(r,g, b, a);
- }
- void Entity::setColor(Number r, Number g, Number b, Number a) {
- color.setColor(r,g,b,a);
- }
- void Entity::recalculateBBox() {
-
- }
- void Entity::setBlendingMode(int newBlendingMode) {
- blendingMode = newBlendingMode;
- }
- Number Entity::getBBoxRadius() const {
- Number compRad;
- Number biggest = bBoxRadius;
- for(int i=0;i<children.size();i++) {
- compRad = children[i]->getCompoundBBoxRadius();
- if(compRad > biggest)
- biggest = compRad;
- }
- return biggest;
- }
- Number Entity::getCompoundBBoxRadius() const {
- Number compRad;
- Number biggest = bBoxRadius + position.distance(Vector3(0,0,0));
- for(int i=0;i<children.size();i++) {
- compRad = children[i]->getCompoundBBoxRadius();
- if(compRad > biggest)
- biggest = compRad;
- }
- return biggest;
- }
- void Entity::setBBoxRadius(Number rad) {
- bBoxRadius = rad;
- }
- Entity::~Entity() {
- if(ownsChildren) {
- for(int i=0; i < children.size(); i++) {
- delete children[i];
- }
- }
- if(tags) delete tags;
- }
- void Entity::setInverseY(bool val) {
- if(val) {
- yAdjust = -1.0;
- } else {
- yAdjust = 1.0;
- }
- for(int i=0; i < children.size(); i++) {
- children[i]->setInverseY(val);
- }
- }
- bool Entity::getInverseY() {
- return (yAdjust == -1.0);
- }
- Matrix4 Entity::buildPositionMatrix() {
- Matrix4 posMatrix;
-
- posMatrix.m[3][0] = position.x;
- posMatrix.m[3][1] = position.y * yAdjust;
- posMatrix.m[3][2] = position.z;
- if(snapToPixels) {
- posMatrix.m[3][0] = round(posMatrix.m[3][0]);
- posMatrix.m[3][1] = round(posMatrix.m[3][1]);
- posMatrix.m[3][2] = round(posMatrix.m[3][2]);
- }
- return posMatrix;
- }
- void Entity::rebuildTransformMatrix() {
- if(lockMatrix)
- return;
-
- if(billboardMode){
- transformMatrix.identity();
- } else {
- transformMatrix = rotationQuat.createMatrix();
- }
-
- Matrix4 scaleMatrix;
- scaleMatrix.m[0][0] *= scale.x;
- scaleMatrix.m[1][1] *= scale.y;
- scaleMatrix.m[2][2] *= scale.z;
-
- Matrix4 posMatrix = buildPositionMatrix();
- transformMatrix = scaleMatrix*transformMatrix*posMatrix;
- matrixDirty = false;
- }
- void Entity::doUpdates() {
- Update();
- for(int i=0; i < children.size(); i++) {
- children[i]->doUpdates();
- }
- }
- void Entity::updateEntityMatrix() {
- if(matrixDirty)
- rebuildTransformMatrix();
-
- for(int i=0; i < children.size(); i++) {
- children[i]->updateEntityMatrix();
- }
- }
- Vector3 Entity::getCompoundScale() const {
- if(parentEntity != NULL) {
- Vector3 parentScale = parentEntity->getCompoundScale();
- return Vector3(scale.x * parentScale.x, scale.y * parentScale.y, scale.z * parentScale.z);
-
- } else {
- return scale;
- }
- }
- Matrix4 Entity::getConcatenatedRollMatrix() const {
- Quaternion q;
- q.createFromAxisAngle(0.0f, 0.0f, 1.0f, rotation.z);
- Matrix4 transformMatrix = q.createMatrix();
-
- if(parentEntity != NULL)
- return transformMatrix * parentEntity->getConcatenatedRollMatrix();
- else
- return transformMatrix;
- }
- Vector2 Entity::getScreenPosition(const Matrix4 &projectionMatrix, const Matrix4 &cameraMatrix, const Polycode::Rectangle &viewport) {
- if(renderer){
- return renderer->Project(cameraMatrix, projectionMatrix, viewport, getConcatenatedMatrix().getPosition());
- } else {
- return Vector2();
- }
- }
- Vector2 Entity::getScreenPositionForMainCamera() {
- if(renderer) {
- return getScreenPosition(renderer->getProjectionMatrix(), renderer->getCameraMatrix(), renderer->getViewport());
- } else {
- return Vector2();
- }
- }
- void Entity::transformAndRender() {
- if(!renderer || !enabled)
- return;
- if(depthOnly) {
- renderer->drawToColorBuffer(false);
- }
-
- bool isScissorEnabled;
- Polycode::Rectangle oldScissorBox;
-
- if(enableScissor) {
- isScissorEnabled = renderer->isScissorEnabled();
- oldScissorBox = renderer->getScissorBox();
- renderer->enableScissor(true);
- Rectangle finalScissorBox = scissorBox;
- // make sure that our scissor box is constrained to the parent one if it exists
- if(isScissorEnabled) {
- finalScissorBox = finalScissorBox.Clipped(renderer->getScissorBox());
- }
- renderer->setScissorBox(finalScissorBox);
- }
-
- renderer->pushMatrix();
- if(ignoreParentMatrix && parentEntity) {
- renderer->multModelviewMatrix(parentEntity->getConcatenatedMatrix().Inverse());
- // renderer->setCurrentModelMatrix(parentEntity->getConcatenatedMatrix().Inverse());
- }
- renderer->multModelviewMatrix(transformMatrix);
- renderer->setCurrentModelMatrix(transformMatrix);
-
- renderer->setVertexColor(color.r,color.g,color.b,color.a);
-
- if(billboardMode) {
- if(billboardIgnoreScale) {
- renderer->billboardMatrix();
- } else {
- renderer->billboardMatrixWithScale(getCompoundScale());
- }
- if(billboardRoll) {
- renderer->multModelviewMatrix(getConcatenatedRollMatrix());
- }
- }
-
- if(!depthWrite)
- renderer->enableDepthWrite(false);
- else
- renderer->enableDepthWrite(true);
-
- if(!depthTest)
- renderer->enableDepthTest(false);
- else
- renderer->enableDepthTest(true);
-
- renderer->enableAlphaTest(alphaTest);
-
- renderer->pushVertexColor();
- renderer->multiplyVertexColor(color);
-
- renderer->setBlendingMode(blendingMode);
- renderer->enableBackfaceCulling(backfaceCulled);
-
- if(visible) {
- renderer->pushMatrix();
- renderer->translate3D(-anchorPoint.x * bBox.x * 0.5, -anchorPoint.y * bBox.y * 0.5 * yAdjust, -anchorPoint.z * bBox.z * 0.5);
- Render();
- renderer->popMatrix();
- }
-
- if(visible || (!visible && !visibilityAffectsChildren)) {
- renderChildren();
- }
-
- renderer->popMatrix();
- renderer->popVertexColor();
-
- if(!depthWrite)
- renderer->enableDepthWrite(true);
-
-
- if(depthOnly) {
- renderer->drawToColorBuffer(true);
- }
-
- if(enableScissor) {
- renderer->enableScissor(isScissorEnabled);
- renderer->setScissorBox(oldScissorBox);
- }
- }
- void Entity::setRenderer(Renderer *renderer) {
- this->renderer = renderer;
- for(int i=0;i<children.size();i++) {
- children[i]->setRenderer(renderer);
- }
- }
- void Entity::renderChildren() {
- for(int i=0;i<children.size();i++) {
- children[i]->transformAndRender();
- }
- }
- void Entity::dirtyMatrix(bool val) {
- matrixDirty = val;
- }
- void Entity::setRotationQuat(Number w, Number x, Number y, Number z) {
- rotationQuat.w = w;
- rotationQuat.x = x;
- rotationQuat.y = y;
- rotationQuat.z = z;
- rotation = rotationQuat.toEulerAngles();
- rotation = rotation * TODEGREES;
- matrixDirty = true;
- }
- void Entity::setRotationByQuaternion(const Quaternion &quaternion) {
- rotationQuat = quaternion;
- rotation = quaternion.toEulerAngles();
- rotation = rotation * TODEGREES;
- matrixDirty = true;
- }
- Quaternion Entity::getRotationQuat() const {
- return rotationQuat;
- }
- Vector3 Entity::getScale() const {
- return scale;
- }
- Vector3 Entity::getEulerRotation() const {
- return rotation;
- }
- Matrix4 Entity::getConcatenatedMatrixRelativeTo(Entity *relativeEntity) {
-
- if(matrixDirty)
- rebuildTransformMatrix();
- if(parentEntity != NULL && parentEntity != relativeEntity)
- return transformMatrix * parentEntity->getConcatenatedMatrixRelativeTo(relativeEntity);
- else
- return transformMatrix;
- }
- Matrix4 Entity::getAnchorAdjustedMatrix() {
- Matrix4 mat = getConcatenatedMatrix();
- Matrix4 adjust;
- adjust.setPosition(-anchorPoint.x * bBox.x * 0.5, -anchorPoint.y * bBox.y * 0.5 * yAdjust, -anchorPoint.z * bBox.z * 0.5);
- return adjust * mat;
- }
- Matrix4 Entity::getConcatenatedMatrix() {
- if(matrixDirty)
- rebuildTransformMatrix();
- if(parentEntity != NULL)
- return transformMatrix * parentEntity->getConcatenatedMatrix();
- else
- return transformMatrix;
- }
- const Matrix4& Entity::getTransformMatrix() const {
- return transformMatrix;
- }
- void Entity::Pitch(Number pitch) {
- rotation.x += pitch;
- rebuildRotation();
- matrixDirty = true;
- }
- void Entity::Yaw(Number yaw) {
- rotation.y += yaw;
- rebuildRotation();
- matrixDirty = true;
- }
- void Entity::Roll(Number roll) {
- rotation.z += roll;
- rebuildRotation();
- matrixDirty = true;
- }
- void Entity::setRoll(Number roll) {
- rotation.z = roll;
- rebuildRotation();
- matrixDirty = true;
- }
- void Entity::setPitch(Number pitch) {
- rotation.x = pitch;
- rebuildRotation();
- matrixDirty = true;
- }
- void Entity::setYaw(Number yaw) {
- rotation.y = yaw;
- rebuildRotation();
- matrixDirty = true;
- }
- void Entity::setRotationEuler(const Vector3 &rotation) {
- this->rotation = rotation;
- rebuildRotation();
- matrixDirty = true;
- }
- void Entity::rebuildRotation() {
- rotationQuat.fromAxes(rotation.x, rotation.y, rotation.z);
- }
- void Entity::setEntityProp(const String& propName, const String& propValue) {
- for(int i=0; i < entityProps.size(); i++) {
- if(entityProps[i].propName == propName) {
- entityProps[i].propValue = propValue;
- return;
- }
- }
- EntityProp entityProp;
- entityProp.propName = propName;
- entityProp.propValue = propValue;
- entityProps.push_back(entityProp);
- }
- String Entity::getEntityProp(const String& propName) {
- for(int i=0; i < entityProps.size(); i++) {
- if(entityProps[i].propName == propName) {
- return entityProps[i].propValue;
- }
- }
- return "null";
- }
- Vector3 Entity::getCombinedPosition() const {
- if(parentEntity != NULL)
- return (parentEntity->getCombinedPosition())+position;
- else
- return position;
- }
- void Entity::setParentEntity(Entity *entity) {
- parentEntity = entity;
- }
- void Entity::setWidth(Number width) {
- bBox.x = width;
- }
- void Entity::setHeight(Number height) {
- bBox.y = height;
- }
- void Entity::setDepth(Number depth) {
- bBox.z = depth;
- }
- Number Entity::getWidth() const {
- return bBox.x;
- }
- Number Entity::getHeight() const {
- return bBox.y;
- }
- Number Entity::getDepth() const {
- return bBox.z;
- }
- Number Entity::getPitch() const {
- return rotation.x;
- }
- Number Entity::getYaw() const {
- return rotation.y;
- }
- Number Entity::getRoll() const {
- return rotation.z;
- }
- void Entity::setTransformByMatrixPure(const Matrix4& matrix) {
- transformMatrix = matrix;
- }
- void Entity::setPosition(const Vector3 &posVec) {
- position = posVec;
- matrixDirty = true;
- }
- void Entity::setPositionX(Number x) {
- position.x = x;
- matrixDirty = true;
- }
- void Entity::setPositionY(Number y) {
- position.y = y;
- matrixDirty = true;
- }
- void Entity::setPositionZ(Number z) {
- position.z = z;
- matrixDirty = true;
- }
- void Entity::setScaleX(Number x) {
- scale.x = x;
- matrixDirty = true;
- }
- void Entity::setScaleY(Number y) {
- scale.y = y;
- matrixDirty = true;
- }
- void Entity::setScaleZ(Number z) {
- scale.z = z;
- matrixDirty = true;
- }
- void Entity::setScale(const Vector3 &v) {
- scale.x = v.x;
- scale.y = v.y;
- scale.z = v.z;
- matrixDirty = true;
- }
- void Entity::setPosition(Number x, Number y, Number z) {
- position.x = x;
- position.y = y;
- position.z = z;
- matrixDirty = true;
- }
- void Entity::Translate(const Vector3 &tVec) {
- position += tVec;
- matrixDirty = true;
- }
- void Entity::Translate(Number x, Number y, Number z) {
- position.x += x;
- position.y += y;
- position.z += z;
- matrixDirty = true;
- }
- void Entity::Scale(const Vector3 &scale) {
- this->scale = this->scale * scale;
- matrixDirty = true;
- }
- void Entity::Scale(Number x, Number y, Number z) {
- scale.x *= x;
- scale.y *= y;
- scale.z *= z;
- matrixDirty = true;
- }
- void Entity::setScale(Number x, Number y, Number z) {
- scale.x = x;
- scale.y = y;
- scale.z = z;
- matrixDirty = true;
- }
- Vector3 Entity::getPosition() const {
- return position;
- }
- Vector2 Entity::getPosition2D() const {
- return Vector2(position.x, position.y);
- }
- Number Entity::getCombinedPitch() const {
- if(parentEntity != NULL)
- return parentEntity->getCombinedPitch()+rotation.x;
- else
- return rotation.x;
- }
- Number Entity::getCombinedYaw() const {
- if(parentEntity != NULL)
- return parentEntity->getCombinedYaw()+rotation.y;
- else
- return rotation.y;
- }
- Number Entity::getCombinedRoll() const {
- if(parentEntity != NULL)
- return parentEntity->getCombinedRoll()+rotation.z;
- else
- return rotation.z;
- }
- unsigned int Entity::getNumTags() const {
- if(!tags) return 0;
- return tags->size();
- }
- String Entity::getTagAtIndex(unsigned int index) const {
- if(!tags) return "";
- if(index < tags->size())
- return (*tags)[index];
- return "";
- }
- bool Entity::hasTag(String tag) const {
- if(!tags) return false;
- for(int i=0; i < tags->size(); i++) {
- if((*tags)[i] == tag)
- return true;
- }
- return false;
- }
-
- void Entity::clearTags() {
- if(!tags) return;
- tags->clear();
- }
- void Entity::addTag(String tag) {
- if(!tags) tags = new std::vector<String>();
- if(!hasTag(tag)) {
- tags->push_back(tag);
- }
- }
- void Entity::setAnchorPoint(const Vector3 &anchorPoint) {
- this->anchorPoint = anchorPoint;
- }
- void Entity::setAnchorPoint(Number x, Number y, Number z) {
- anchorPoint.set(x,y,z);
- }
- Vector3 Entity::getAnchorPoint() const {
- return anchorPoint;
- }
- MouseEventResult Entity::onMouseDown(const Ray &ray, int mouseButton, int timestamp) {
- MouseEventResult ret;
- ret.hit = false;
- ret.blocked = false;
-
- if(processInputEvents && enabled) {
- if(ray.boxIntersect(bBox, getAnchorAdjustedMatrix())) {
- if(customHitDetection(ray)) {
- ret.hit = true;
-
- Vector3 localCoordinate = Vector3(ray.origin.x,ray.origin.y,0);
- Matrix4 inverse = getConcatenatedMatrix().Inverse();
- localCoordinate = inverse * localCoordinate;
-
- InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp);
- inputEvent->mouseButton = mouseButton;
- dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEDOWN);
-
- if(timestamp - lastClickTicks < 400) {
- InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp);
- inputEvent->mouseButton = mouseButton;
- dispatchEvent(inputEvent, InputEvent::EVENT_DOUBLECLICK);
- }
- lastClickTicks = timestamp;
-
- if(blockMouseInput) {
- ret.blocked = true;
- }
- }
- }
-
- for(int i=children.size()-1; i>=0; i--) {
- MouseEventResult childRes = children[i]->onMouseDown(ray, mouseButton, timestamp);
- if(childRes.hit)
- ret.hit = true;
-
- if(childRes.blocked) {
- ret.blocked = true;
- break;
- }
- }
- }
- return ret;
- }
- MouseEventResult Entity::onMouseUp(const Ray &ray, int mouseButton, int timestamp) {
- MouseEventResult ret;
- ret.hit = false;
- ret.blocked = false;
-
- if(processInputEvents && enabled) {
-
- Vector3 localCoordinate = Vector3(ray.origin.x,ray.origin.y,0);
- Matrix4 inverse = getConcatenatedMatrix().Inverse();
- localCoordinate = inverse * localCoordinate;
-
- InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp);
- inputEvent->mouseButton = mouseButton;
-
- if(ray.boxIntersect(bBox, getAnchorAdjustedMatrix())) {
- ret.hit = true;
-
- dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEUP);
- if(blockMouseInput) {
- ret.blocked = true;
- }
- } else {
- dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEUP_OUTSIDE);
- }
-
- for(int i=children.size()-1; i>=0; i--) {
- MouseEventResult childRes = children[i]->onMouseUp(ray, mouseButton, timestamp);
- if(childRes.hit)
- ret.hit = true;
-
- if(childRes.blocked) {
- ret.blocked = true;
- break;
- }
- }
- }
- return ret;}
- MouseEventResult Entity::onMouseMove(const Ray &ray, int timestamp) {
- MouseEventResult ret;
- ret.hit = false;
- ret.blocked = false;
-
- if(processInputEvents && enabled) {
-
- Vector3 localCoordinate = Vector3(ray.origin.x,ray.origin.y,0);
- Matrix4 inverse = getConcatenatedMatrix().Inverse();
- localCoordinate = inverse * localCoordinate;
-
- if(ray.boxIntersect(bBox, getAnchorAdjustedMatrix())) {
- //setColor(1.0, 0.0, 0.0, 1.0);
- ret.hit = true;
- dispatchEvent(new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp), InputEvent::EVENT_MOUSEMOVE);
-
- if(!mouseOver) {
- dispatchEvent(new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp), InputEvent::EVENT_MOUSEOVER);
- mouseOver = true;
- }
-
- if(blockMouseInput) {
- ret.blocked = true;
- }
- } else {
- if(mouseOver) {
- dispatchEvent(new InputEvent(Vector2(localCoordinate.x, localCoordinate.y), timestamp), InputEvent::EVENT_MOUSEOUT);
- mouseOver = false;
- }
- }
-
- for(int i=children.size()-1; i>=0; i--) {
- MouseEventResult childRes = children[i]->onMouseMove(ray, timestamp);
- if(childRes.hit)
- ret.hit = true;
-
- if(childRes.blocked) {
- ret.blocked = true;
- break;
- }
- }
- }
- return ret;
- }
- MouseEventResult Entity::onMouseWheelUp(const Ray &ray, int timestamp) {
- MouseEventResult ret;
- ret.hit = false;
- ret.blocked = false;
-
- if(processInputEvents && enabled) {
- if(ray.boxIntersect(bBox, getAnchorAdjustedMatrix())) {
- ret.hit = true;
-
- Vector3 localCoordinate = Vector3(ray.origin.x,ray.origin.y,0);
- Matrix4 inverse = getConcatenatedMatrix().Inverse();
- localCoordinate = inverse * localCoordinate;
-
- InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp);
- dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEWHEEL_UP);
-
- if(blockMouseInput) {
- ret.blocked = true;
- }
- }
-
- for(int i=children.size()-1; i>=0; i--) {
- MouseEventResult childRes = children[i]->onMouseWheelUp(ray, timestamp);
- if(childRes.hit)
- ret.hit = true;
-
- if(childRes.blocked) {
- ret.blocked = true;
- break;
- }
- }
- }
- return ret;
- }
- MouseEventResult Entity::onMouseWheelDown(const Ray &ray, int timestamp) {
- MouseEventResult ret;
- ret.hit = false;
- ret.blocked = false;
-
- if(processInputEvents && enabled) {
- if(ray.boxIntersect(bBox, getAnchorAdjustedMatrix())) {
- ret.hit = true;
-
- Vector3 localCoordinate = Vector3(ray.origin.x,ray.origin.y,0);
- Matrix4 inverse = getConcatenatedMatrix().Inverse();
- localCoordinate = inverse * localCoordinate;
-
- InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp);
- dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEWHEEL_DOWN);
-
- if(blockMouseInput) {
- ret.blocked = true;
- }
- }
-
- for(int i=children.size()-1; i>=0; i--) {
- MouseEventResult childRes = children[i]->onMouseWheelDown(ray, timestamp);
- if(childRes.hit)
- ret.hit = true;
-
- if(childRes.blocked) {
- ret.blocked = true;
- break;
- }
- }
- }
- return ret;
- }
|