PolyEntity.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyEntity.h"
  20. #include "PolyRenderer.h"
  21. #include "PolyCoreServices.h"
  22. #include "PolyInputEvent.h"
  23. using namespace Polycode;
  24. int Entity::defaultBlendingMode = Renderer::BLEND_MODE_NORMAL;
  25. Rotation::Rotation() {
  26. pitch = 0;
  27. yaw = 0;
  28. roll = 0;
  29. }
  30. Entity::Entity() : EventDispatcher() {
  31. initEntity();
  32. }
  33. Entity::Entity(Number width, Number height, Number depth) : EventDispatcher() {
  34. initEntity();
  35. bBox.x = width;
  36. bBox.y = height;
  37. bBox.z = depth;
  38. }
  39. void Entity::initEntity() {
  40. userData = NULL;
  41. scale.set(1,1,1);
  42. renderer = NULL;
  43. enabled = true;
  44. depthTest = true;
  45. visible = true;
  46. color.setColor(1.0f,1.0f,1.0f,1.0f);
  47. parentEntity = NULL;
  48. matrixDirty = true;
  49. billboardMode = false;
  50. billboardRoll = false;
  51. billboardIgnoreScale = false;
  52. depthOnly = false;
  53. depthWrite = true;
  54. ignoreParentMatrix = false;
  55. blendingMode = Entity::defaultBlendingMode;
  56. lockMatrix = false;
  57. colorAffectsChildren = true;
  58. visibilityAffectsChildren = true;
  59. ownsChildren = false;
  60. enableScissor = false;
  61. processInputEvents = false;
  62. blockMouseInput = false;
  63. editorOnly = false;
  64. snapToPixels = false;
  65. tags = NULL;
  66. bBox.z = 0.001;
  67. mouseOver = false;
  68. yAdjust = 1.0;
  69. lastClickTicks = 0.0;
  70. rendererVis = true;
  71. layerID = 0;
  72. }
  73. Entity *Entity::getEntityById(String id, bool recursive) const {
  74. for(int i=0;i<children.size();i++) {
  75. if(children[i]->id == id) {
  76. return children[i];
  77. } else {
  78. if(recursive) {
  79. Entity *ret = children[i]->getEntityById(id, recursive);
  80. if(ret) {
  81. return ret;
  82. }
  83. }
  84. }
  85. }
  86. return NULL;
  87. }
  88. Entity *Entity::Clone(bool deepClone, bool ignoreEditorOnly) const {
  89. Entity *newEntity = new Entity();
  90. applyClone(newEntity, deepClone, ignoreEditorOnly);
  91. return newEntity;
  92. }
  93. void Entity::applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) const {
  94. clone->ownsChildren = ownsChildren;
  95. clone->setPosition(position);
  96. clone->setRotationByQuaternion(rotationQuat);
  97. clone->setScale(scale);
  98. clone->color = color;
  99. clone->billboardMode = billboardMode;
  100. clone->billboardRoll = billboardRoll;
  101. clone->depthWrite = depthWrite;
  102. clone->depthTest = depthTest;
  103. clone->blendingMode = blendingMode;
  104. clone->colorAffectsChildren = colorAffectsChildren;
  105. clone->visibilityAffectsChildren = visibilityAffectsChildren;
  106. clone->depthOnly = depthOnly;
  107. clone->setUserData(getUserData());
  108. clone->entityProps = entityProps;
  109. clone->bBox = bBox;
  110. clone->ignoreParentMatrix = ignoreParentMatrix;
  111. clone->enableScissor = enableScissor;
  112. clone->scissorBox = scissorBox;
  113. clone->editorOnly = editorOnly;
  114. clone->snapToPixels = snapToPixels;
  115. clone->setAnchorPoint(anchorPoint);
  116. clone->layerID = layerID;
  117. clone->id = id;
  118. if(tags == NULL) {
  119. clone->tags = NULL;
  120. } else {
  121. clone->tags = new std::vector<String>();
  122. for(int i=0; i < tags->size(); i++) {
  123. clone->addTag((*tags)[i]);
  124. }
  125. }
  126. clone->setRenderer(renderer);
  127. if(deepClone) {
  128. for(int i=0; i < children.size(); i++) {
  129. if(children[i]->editorOnly && ignoreEditorOnly) {
  130. } else {
  131. Entity *childClone = children[i]->Clone(deepClone, ignoreEditorOnly);
  132. clone->addChild(childClone);
  133. }
  134. }
  135. }
  136. }
  137. void Entity::setOwnsChildrenRecursive(bool val) {
  138. ownsChildren = val;
  139. for(int i=0; i < children.size(); i++) {
  140. children[i]->setOwnsChildrenRecursive(val);
  141. }
  142. }
  143. std::vector<Entity*> Entity::getEntitiesByLayerID(unsigned char layerID, bool recursive) const {
  144. std::vector<Entity*> retVector;
  145. for(int i=0;i<children.size();i++) {
  146. if(children[i]->layerID == layerID) {
  147. retVector.push_back(children[i]);
  148. }
  149. if(recursive) {
  150. std::vector<Entity*> childVector = children[i]->getEntitiesByLayerID(layerID, recursive);
  151. retVector.insert(retVector.end(), childVector.begin(), childVector.end());
  152. }
  153. }
  154. return retVector;
  155. }
  156. std::vector<Entity*> Entity::getEntitiesByTag(String tag, bool recursive) const {
  157. std::vector<Entity*> retVector;
  158. for(int i=0;i<children.size();i++) {
  159. if(children[i]->hasTag(tag)) {
  160. retVector.push_back(children[i]);
  161. }
  162. if(recursive) {
  163. std::vector<Entity*> childVector = children[i]->getEntitiesByTag(tag, recursive);
  164. retVector.insert(retVector.end(), childVector.begin(), childVector.end());
  165. }
  166. }
  167. return retVector;
  168. }
  169. void Entity::setUserData(void *userData) {
  170. this->userData = userData;
  171. }
  172. void *Entity::getUserData() const {
  173. return userData;
  174. }
  175. Entity *Entity::getParentEntity() const {
  176. return parentEntity;
  177. }
  178. Color Entity::getCombinedColor() const {
  179. if(parentEntity) {
  180. if(parentEntity->colorAffectsChildren)
  181. return color * parentEntity->getCombinedColor();
  182. else
  183. return color;
  184. } else {
  185. return color;
  186. }
  187. }
  188. Matrix4 Entity::getLookAtMatrix(const Vector3 &loc, const Vector3 &upVector) {
  189. rebuildTransformMatrix();
  190. Vector3 D;
  191. D = loc - position;
  192. Vector3 back = D * -1;
  193. back.Normalize();
  194. Vector3 right = back.crossProduct(upVector) ;
  195. right.Normalize();
  196. right = right * -1;
  197. Vector3 up = back.crossProduct(right);
  198. Matrix4 newMatrix(right.x, right.y, right.z, 0,
  199. up.x, up.y, up.z, 0,
  200. back.x, back.y, back.z, 0,
  201. 0, 0 , 0, 1);
  202. return newMatrix;
  203. }
  204. void Entity::lookAt(const Vector3 &loc, const Vector3 &upVector) {
  205. Matrix4 newMatrix = getLookAtMatrix(loc, upVector);
  206. rotationQuat.createFromMatrix(newMatrix);
  207. matrixDirty = true;
  208. }
  209. void Entity::lookAtEntity(Entity *entity, const Vector3 &upVector) {
  210. if(entity->getParentEntity())
  211. lookAt(entity->getParentEntity()->getConcatenatedMatrix() * (entity->getPosition()), upVector);
  212. else
  213. lookAt(entity->getPosition(), upVector);
  214. }
  215. void Entity::removeChild(Entity *entityToRemove) {
  216. for(int i=0;i<children.size();i++) {
  217. if(children[i] == entityToRemove) {
  218. entityToRemove->setParentEntity(NULL);
  219. children.erase(children.begin()+i);
  220. return;
  221. }
  222. }
  223. }
  224. void Entity::moveChildUp(Entity *child) {
  225. for(int i=0; i < children.size(); i++) {
  226. if(children[i] == child && i < children.size()-1) {
  227. Entity *next = (Entity*)children[i+1];
  228. children[i+1] = child;
  229. children[i] = next;
  230. break;
  231. }
  232. }
  233. }
  234. void Entity::moveChildDown(Entity *child) {
  235. for(int i=0; i < children.size(); i++) {
  236. if(children[i] == child && i > 0) {
  237. Entity *prev = (Entity*)children[i-1];
  238. children[i-1] = child;
  239. children[i] = prev;
  240. break;
  241. }
  242. }
  243. }
  244. void Entity::moveChildTop(Entity *child) {
  245. for(int i=0; i < children.size(); i++) {
  246. if(children[i] == child && i < children.size()-1) {
  247. children.erase(children.begin()+i);
  248. children.push_back(child);
  249. break;
  250. }
  251. }
  252. }
  253. void Entity::moveChildBottom(Entity *child) {
  254. for(int i=0; i < children.size(); i++) {
  255. if(children[i] == child && i > 0) {
  256. children.erase(children.begin()+i);
  257. children.insert(children.begin(), child);
  258. break;
  259. }
  260. }
  261. }
  262. unsigned int Entity::getNumChildren() {
  263. return children.size();
  264. }
  265. Entity *Entity::getChildAtIndex(unsigned int index) {
  266. if(index < children.size()) {
  267. return children[index];
  268. }
  269. return NULL;
  270. }
  271. void Entity::addChild(Entity *newChild) {
  272. newChild->setRenderer(renderer);
  273. newChild->setParentEntity(this);
  274. newChild->setInverseY(getInverseY());
  275. children.push_back(newChild);
  276. }
  277. void Entity::setColor(Color color) {
  278. this->color.setColor(&color);
  279. }
  280. void Entity::setColorInt(int r, int g, int b, int a) {
  281. color.setColorRGBA(r,g, b, a);
  282. }
  283. void Entity::setColor(Number r, Number g, Number b, Number a) {
  284. color.setColor(r,g,b,a);
  285. }
  286. void Entity::setBlendingMode(int newBlendingMode) {
  287. blendingMode = newBlendingMode;
  288. }
  289. Entity::~Entity() {
  290. if(ownsChildren) {
  291. for(int i=0; i < children.size(); i++) {
  292. delete children[i];
  293. }
  294. }
  295. if(tags) delete tags;
  296. }
  297. void Entity::setInverseY(bool val) {
  298. if(val) {
  299. yAdjust = -1.0;
  300. } else {
  301. yAdjust = 1.0;
  302. }
  303. for(int i=0; i < children.size(); i++) {
  304. children[i]->setInverseY(val);
  305. }
  306. matrixDirty = true;
  307. }
  308. bool Entity::getInverseY() {
  309. return (yAdjust == -1.0);
  310. }
  311. Matrix4 Entity::buildPositionMatrix() {
  312. Matrix4 posMatrix;
  313. posMatrix.m[3][0] = position.x;
  314. posMatrix.m[3][1] = position.y * yAdjust;
  315. posMatrix.m[3][2] = position.z;
  316. if(snapToPixels) {
  317. posMatrix.m[3][0] = round(posMatrix.m[3][0]);
  318. posMatrix.m[3][1] = round(posMatrix.m[3][1]);
  319. posMatrix.m[3][2] = round(posMatrix.m[3][2]);
  320. }
  321. return posMatrix;
  322. }
  323. void Entity::rebuildTransformMatrix() {
  324. if(lockMatrix)
  325. return;
  326. if(billboardMode){
  327. transformMatrix.identity();
  328. } else {
  329. transformMatrix = rotationQuat.createMatrix();
  330. }
  331. Matrix4 scaleMatrix;
  332. scaleMatrix.m[0][0] *= scale.x;
  333. scaleMatrix.m[1][1] *= scale.y;
  334. scaleMatrix.m[2][2] *= scale.z;
  335. Matrix4 posMatrix = buildPositionMatrix();
  336. transformMatrix = scaleMatrix*transformMatrix*posMatrix;
  337. matrixDirty = false;
  338. }
  339. void Entity::doUpdates() {
  340. if (enabled) {
  341. Update();
  342. for(int i=0; i < children.size(); i++) {
  343. children[i]->doUpdates();
  344. }
  345. }
  346. }
  347. void Entity::updateEntityMatrix() {
  348. if(matrixDirty) {
  349. rebuildTransformMatrix();
  350. recalculateAABBAllChildren();
  351. }
  352. for(int i=0; i < children.size(); i++) {
  353. children[i]->updateEntityMatrix();
  354. }
  355. }
  356. Vector3 Entity::getCompoundScale() const {
  357. if(parentEntity != NULL) {
  358. Vector3 parentScale = parentEntity->getCompoundScale();
  359. return Vector3(scale.x * parentScale.x, scale.y * parentScale.y, scale.z * parentScale.z);
  360. } else {
  361. return scale;
  362. }
  363. }
  364. Matrix4 Entity::getConcatenatedRollMatrix() const {
  365. Quaternion q;
  366. q.createFromAxisAngle(0.0f, 0.0f, 1.0f, rotation.z);
  367. Matrix4 transformMatrix = q.createMatrix();
  368. if(parentEntity != NULL)
  369. return transformMatrix * parentEntity->getConcatenatedRollMatrix();
  370. else
  371. return transformMatrix;
  372. }
  373. Vector2 Entity::getScreenPosition(const Matrix4 &projectionMatrix, const Matrix4 &cameraMatrix, const Polycode::Rectangle &viewport) {
  374. if(renderer){
  375. return renderer->Project(cameraMatrix, projectionMatrix, viewport, getConcatenatedMatrix().getPosition());
  376. } else {
  377. return Vector2();
  378. }
  379. }
  380. Vector2 Entity::getScreenPositionForMainCamera() {
  381. if(renderer) {
  382. return getScreenPosition(renderer->getProjectionMatrix(), renderer->getCameraMatrix(), renderer->getViewport());
  383. } else {
  384. return Vector2();
  385. }
  386. }
  387. void Entity::transformAndRender() {
  388. if(!renderer || !enabled)
  389. return;
  390. if(matrixDirty) {
  391. rebuildTransformMatrix();
  392. }
  393. if(depthOnly) {
  394. renderer->drawToColorBuffer(false);
  395. }
  396. bool isScissorEnabled;
  397. Polycode::Rectangle oldScissorBox;
  398. if(enableScissor) {
  399. isScissorEnabled = renderer->isScissorEnabled();
  400. oldScissorBox = renderer->getScissorBox();
  401. renderer->enableScissor(true);
  402. Rectangle finalScissorBox = scissorBox;
  403. // make sure that our scissor box is constrained to the parent one if it exists
  404. if(isScissorEnabled) {
  405. finalScissorBox = finalScissorBox.Clipped(renderer->getScissorBox());
  406. }
  407. renderer->setScissorBox(finalScissorBox);
  408. }
  409. renderer->pushMatrix();
  410. if(ignoreParentMatrix && parentEntity) {
  411. renderer->multModelviewMatrix(parentEntity->getConcatenatedMatrix().Inverse());
  412. }
  413. renderer->multModelviewMatrix(transformMatrix);
  414. renderer->setVertexColor(color.r,color.g,color.b,color.a);
  415. if(billboardMode) {
  416. if(billboardIgnoreScale) {
  417. renderer->billboardMatrix();
  418. } else {
  419. renderer->billboardMatrixWithScale(getCompoundScale());
  420. }
  421. if(billboardRoll) {
  422. renderer->multModelviewMatrix(getConcatenatedRollMatrix());
  423. }
  424. }
  425. if(!depthWrite)
  426. renderer->enableDepthWrite(false);
  427. else
  428. renderer->enableDepthWrite(true);
  429. if(!depthTest)
  430. renderer->enableDepthTest(false);
  431. else
  432. renderer->enableDepthTest(true);
  433. renderer->pushVertexColor();
  434. renderer->multiplyVertexColor(color);
  435. renderer->setBlendingMode(blendingMode);
  436. if(visible && rendererVis) {
  437. renderer->pushMatrix();
  438. renderer->translate3D(-anchorPoint.x * bBox.x * 0.5, -anchorPoint.y * bBox.y * 0.5 * yAdjust, -anchorPoint.z * bBox.z * 0.5);
  439. Render();
  440. renderer->popMatrix();
  441. }
  442. if(!colorAffectsChildren) {
  443. renderer->popVertexColor();
  444. if(visible || (!visible && !visibilityAffectsChildren)) {
  445. renderChildren();
  446. }
  447. } else {
  448. if(visible || (!visible && !visibilityAffectsChildren)) {
  449. renderChildren();
  450. }
  451. renderer->popVertexColor();
  452. }
  453. renderer->popMatrix();
  454. if(!depthWrite)
  455. renderer->enableDepthWrite(true);
  456. if(depthOnly) {
  457. renderer->drawToColorBuffer(true);
  458. }
  459. if(enableScissor) {
  460. renderer->enableScissor(isScissorEnabled);
  461. renderer->setScissorBox(oldScissorBox);
  462. }
  463. }
  464. void Entity::setRenderer(Renderer *renderer) {
  465. this->renderer = renderer;
  466. for(int i=0;i<children.size();i++) {
  467. children[i]->setRenderer(renderer);
  468. }
  469. }
  470. void Entity::renderChildren() {
  471. for(int i=0;i<children.size();i++) {
  472. children[i]->transformAndRender();
  473. }
  474. }
  475. void Entity::dirtyMatrix(bool val) {
  476. matrixDirty = val;
  477. }
  478. void Entity::recalculateAABBAllChildren() {
  479. recalculateAABB();
  480. for(int i=0; i< children.size(); i++) {
  481. children[i]->recalculateAABBAllChildren();
  482. }
  483. }
  484. void Entity::recalculateAABB() {
  485. aabb.min = Vector3();
  486. aabb.max = Vector3();
  487. Vector3 bBoxCoords[8] = {
  488. Vector3(-bBox.x * 0.5, -bBox.y * 0.5, bBox.z * 0.5),
  489. Vector3(bBox.x * 0.5, -bBox.y * 0.5, bBox.z * 0.5),
  490. Vector3(-bBox.x * 0.5, -bBox.y * 0.5, -bBox.z * 0.5),
  491. Vector3(bBox.x * 0.5, -bBox.y * 0.5, -bBox.z * 0.5),
  492. Vector3(-bBox.x * 0.5, bBox.y * 0.5, bBox.z * 0.5),
  493. Vector3(bBox.x * 0.5, bBox.y * 0.5, bBox.z * 0.5),
  494. Vector3(-bBox.x * 0.5, bBox.y * 0.5, -bBox.z * 0.5),
  495. Vector3(bBox.x * 0.5, bBox.y * 0.5, -bBox.z * 0.5)
  496. };
  497. Matrix4 fullMatrix = getAnchorAdjustedMatrix();
  498. if(ignoreParentMatrix) {
  499. if(matrixDirty) {
  500. rebuildTransformMatrix();
  501. }
  502. fullMatrix = transformMatrix;
  503. }
  504. for(int i=0; i < 8; i++) {
  505. bBoxCoords[i] = fullMatrix * bBoxCoords[i];
  506. if(i ==0 ) {
  507. aabb.min = bBoxCoords[i];
  508. aabb.max = bBoxCoords[i];
  509. } else {
  510. if(bBoxCoords[i].x < aabb.min.x) {
  511. aabb.min.x = bBoxCoords[i].x;
  512. }
  513. if(bBoxCoords[i].y < aabb.min.y) {
  514. aabb.min.y = bBoxCoords[i].y;
  515. }
  516. if(bBoxCoords[i].z < aabb.min.z) {
  517. aabb.min.z = bBoxCoords[i].z;
  518. }
  519. if(bBoxCoords[i].x > aabb.max.x) {
  520. aabb.max.x = bBoxCoords[i].x;
  521. }
  522. if(bBoxCoords[i].y > aabb.max.y) {
  523. aabb.max.y = bBoxCoords[i].y;
  524. }
  525. if(bBoxCoords[i].z > aabb.max.z) {
  526. aabb.max.z = bBoxCoords[i].z;
  527. }
  528. }
  529. }
  530. }
  531. AABB Entity::getWorldAABB() {
  532. return aabb;
  533. }
  534. Vector3 Entity::getLocalBoundingBox() {
  535. return bBox;
  536. }
  537. void Entity::setLocalBoundingBox(const Vector3 box) {
  538. bBox = box;
  539. recalculateAABB();
  540. matrixDirty = true;
  541. }
  542. void Entity::setLocalBoundingBox(Number x, Number y, Number z) {
  543. bBox.set(x, y, z);
  544. recalculateAABB();
  545. matrixDirty = true;
  546. }
  547. void Entity::setLocalBoundingBoxX(Number x) {
  548. bBox.x = x;
  549. recalculateAABB();
  550. matrixDirty = true;
  551. }
  552. void Entity::setLocalBoundingBoxY(Number y) {
  553. bBox.y = y;
  554. recalculateAABB();
  555. matrixDirty = true;
  556. }
  557. void Entity::setLocalBoundingBoxZ(Number z) {
  558. bBox.z = z;
  559. recalculateAABB();
  560. matrixDirty = true;
  561. }
  562. void Entity::setRotationQuat(Number w, Number x, Number y, Number z) {
  563. rotationQuat.w = w;
  564. rotationQuat.x = x;
  565. rotationQuat.y = y;
  566. rotationQuat.z = z;
  567. rotation = rotationQuat.toEulerAngles();
  568. rotation = rotation * TODEGREES;
  569. matrixDirty = true;
  570. }
  571. void Entity::setRotationByQuaternion(const Quaternion &quaternion) {
  572. rotationQuat = quaternion;
  573. rotation = quaternion.toEulerAngles();
  574. rotation = rotation * TODEGREES;
  575. matrixDirty = true;
  576. }
  577. Quaternion Entity::getRotationQuat() const {
  578. return rotationQuat;
  579. }
  580. Vector3 Entity::getScale() const {
  581. return scale;
  582. }
  583. Vector3 Entity::getEulerRotation() const {
  584. return rotation;
  585. }
  586. Matrix4 Entity::getConcatenatedMatrixRelativeTo(Entity *relativeEntity) {
  587. if(matrixDirty) {
  588. rebuildTransformMatrix();
  589. }
  590. if(parentEntity != NULL && parentEntity != relativeEntity)
  591. return transformMatrix * parentEntity->getConcatenatedMatrixRelativeTo(relativeEntity);
  592. else
  593. return transformMatrix;
  594. }
  595. Matrix4 Entity::getAnchorAdjustedMatrix() {
  596. Matrix4 mat = getConcatenatedMatrix();
  597. Matrix4 adjust;
  598. adjust.setPosition(-anchorPoint.x * bBox.x * 0.5, -anchorPoint.y * bBox.y * 0.5 * yAdjust, -anchorPoint.z * bBox.z * 0.5);
  599. return adjust * mat;
  600. }
  601. Matrix4 Entity::getConcatenatedMatrix() {
  602. if(matrixDirty) {
  603. rebuildTransformMatrix();
  604. }
  605. if(parentEntity != NULL && !ignoreParentMatrix)
  606. return transformMatrix * parentEntity->getConcatenatedMatrix();
  607. else
  608. return transformMatrix;
  609. }
  610. const Matrix4& Entity::getTransformMatrix() const {
  611. return transformMatrix;
  612. }
  613. void Entity::Pitch(Number pitch) {
  614. rotation.x += pitch;
  615. rebuildRotation();
  616. matrixDirty = true;
  617. }
  618. void Entity::Yaw(Number yaw) {
  619. rotation.y += yaw;
  620. rebuildRotation();
  621. matrixDirty = true;
  622. }
  623. void Entity::Roll(Number roll) {
  624. rotation.z += roll;
  625. rebuildRotation();
  626. matrixDirty = true;
  627. }
  628. void Entity::setRoll(Number roll) {
  629. rotation.z = roll;
  630. rebuildRotation();
  631. matrixDirty = true;
  632. }
  633. void Entity::setPitch(Number pitch) {
  634. rotation.x = pitch;
  635. rebuildRotation();
  636. matrixDirty = true;
  637. }
  638. void Entity::setYaw(Number yaw) {
  639. rotation.y = yaw;
  640. rebuildRotation();
  641. matrixDirty = true;
  642. }
  643. void Entity::setRotationEuler(const Vector3 &rotation) {
  644. this->rotation = rotation;
  645. rebuildRotation();
  646. matrixDirty = true;
  647. }
  648. void Entity::rebuildRotation() {
  649. rotationQuat.fromAxes(rotation.x, rotation.y, rotation.z);
  650. }
  651. void Entity::setEntityProp(const String& propName, const String& propValue) {
  652. for(int i=0; i < entityProps.size(); i++) {
  653. if(entityProps[i].propName == propName) {
  654. entityProps[i].propValue = propValue;
  655. return;
  656. }
  657. }
  658. EntityProp entityProp;
  659. entityProp.propName = propName;
  660. entityProp.propValue = propValue;
  661. entityProps.push_back(entityProp);
  662. }
  663. String Entity::getEntityProp(const String& propName) {
  664. for(int i=0; i < entityProps.size(); i++) {
  665. if(entityProps[i].propName == propName) {
  666. return entityProps[i].propValue;
  667. }
  668. }
  669. return "null";
  670. }
  671. Vector3 Entity::getCombinedPosition() const {
  672. if(parentEntity != NULL)
  673. return (parentEntity->getCombinedPosition())+position;
  674. else
  675. return position;
  676. }
  677. void Entity::setParentEntity(Entity *entity) {
  678. parentEntity = entity;
  679. }
  680. void Entity::setWidth(Number width) {
  681. setLocalBoundingBoxX(width);
  682. }
  683. void Entity::setHeight(Number height) {
  684. setLocalBoundingBoxY(height);
  685. }
  686. void Entity::setDepth(Number depth) {
  687. setLocalBoundingBoxZ(depth);
  688. }
  689. Number Entity::getWidth() const {
  690. return bBox.x;
  691. }
  692. Number Entity::getHeight() const {
  693. return bBox.y;
  694. }
  695. Number Entity::getDepth() const {
  696. return bBox.z;
  697. }
  698. Number Entity::getPitch() const {
  699. return rotation.x;
  700. }
  701. Number Entity::getYaw() const {
  702. return rotation.y;
  703. }
  704. Number Entity::getRoll() const {
  705. return rotation.z;
  706. }
  707. void Entity::setTransformByMatrixPure(const Matrix4& matrix) {
  708. transformMatrix = matrix;
  709. }
  710. void Entity::setPosition(const Vector3 &posVec) {
  711. position = posVec;
  712. matrixDirty = true;
  713. }
  714. void Entity::setPositionX(Number x) {
  715. position.x = x;
  716. matrixDirty = true;
  717. }
  718. void Entity::setPositionY(Number y) {
  719. position.y = y;
  720. matrixDirty = true;
  721. }
  722. void Entity::setPositionZ(Number z) {
  723. position.z = z;
  724. matrixDirty = true;
  725. }
  726. void Entity::setScaleX(Number x) {
  727. scale.x = x;
  728. matrixDirty = true;
  729. }
  730. void Entity::setScaleY(Number y) {
  731. scale.y = y;
  732. matrixDirty = true;
  733. }
  734. void Entity::setScaleZ(Number z) {
  735. scale.z = z;
  736. matrixDirty = true;
  737. }
  738. void Entity::setScale(const Vector3 &v) {
  739. scale.x = v.x;
  740. scale.y = v.y;
  741. scale.z = v.z;
  742. matrixDirty = true;
  743. }
  744. void Entity::setPosition(Number x, Number y, Number z) {
  745. position.x = x;
  746. position.y = y;
  747. position.z = z;
  748. matrixDirty = true;
  749. }
  750. void Entity::Translate(const Vector3 &tVec) {
  751. position += tVec;
  752. matrixDirty = true;
  753. }
  754. void Entity::Translate(Number x, Number y, Number z) {
  755. position.x += x;
  756. position.y += y;
  757. position.z += z;
  758. matrixDirty = true;
  759. }
  760. void Entity::Scale(const Vector3 &scale) {
  761. this->scale = this->scale * scale;
  762. matrixDirty = true;
  763. }
  764. void Entity::Scale(Number x, Number y, Number z) {
  765. scale.x *= x;
  766. scale.y *= y;
  767. scale.z *= z;
  768. matrixDirty = true;
  769. }
  770. void Entity::setScale(Number x, Number y, Number z) {
  771. scale.x = x;
  772. scale.y = y;
  773. scale.z = z;
  774. matrixDirty = true;
  775. }
  776. Vector3 Entity::getPosition() const {
  777. return position;
  778. }
  779. Vector2 Entity::getPosition2D() const {
  780. return Vector2(position.x, position.y);
  781. }
  782. Number Entity::getCombinedPitch() const {
  783. if(parentEntity != NULL)
  784. return parentEntity->getCombinedPitch()+rotation.x;
  785. else
  786. return rotation.x;
  787. }
  788. Number Entity::getCombinedYaw() const {
  789. if(parentEntity != NULL)
  790. return parentEntity->getCombinedYaw()+rotation.y;
  791. else
  792. return rotation.y;
  793. }
  794. Number Entity::getCombinedRoll() const {
  795. if(parentEntity != NULL)
  796. return parentEntity->getCombinedRoll()+rotation.z;
  797. else
  798. return rotation.z;
  799. }
  800. unsigned int Entity::getNumTags() const {
  801. if(!tags) return 0;
  802. return tags->size();
  803. }
  804. String Entity::getTagAtIndex(unsigned int index) const {
  805. if(!tags) return "";
  806. if(index < tags->size())
  807. return (*tags)[index];
  808. return "";
  809. }
  810. bool Entity::hasTag(String tag) const {
  811. if(!tags) return false;
  812. for(int i=0; i < tags->size(); i++) {
  813. if((*tags)[i] == tag)
  814. return true;
  815. }
  816. return false;
  817. }
  818. void Entity::clearTags() {
  819. if(!tags) return;
  820. tags->clear();
  821. }
  822. void Entity::addTag(String tag) {
  823. if(!tags) tags = new std::vector<String>();
  824. if(!hasTag(tag)) {
  825. tags->push_back(tag);
  826. }
  827. }
  828. void Entity::setAnchorPoint(const Vector3 &anchorPoint) {
  829. this->anchorPoint = anchorPoint;
  830. matrixDirty = true;
  831. }
  832. void Entity::setAnchorPoint(Number x, Number y, Number z) {
  833. anchorPoint.set(x,y,z);
  834. matrixDirty = true;
  835. }
  836. Vector3 Entity::getAnchorPoint() const {
  837. return anchorPoint;
  838. }
  839. MouseEventResult Entity::onMouseDown(const Ray &ray, int mouseButton, int timestamp) {
  840. MouseEventResult ret;
  841. ret.hit = false;
  842. ret.blocked = false;
  843. Number hitDistance;
  844. if(processInputEvents && enabled) {
  845. hitDistance = ray.boxIntersect(bBox, getAnchorAdjustedMatrix());
  846. if(hitDistance >= 0.0) {
  847. if(customHitDetection(ray)) {
  848. ret.hit = true;
  849. Vector3 localCoordinate = Vector3(ray.origin.x,ray.origin.y,0);
  850. Matrix4 inverse = getConcatenatedMatrix().Inverse();
  851. localCoordinate = inverse * localCoordinate;
  852. InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp);
  853. inputEvent->hitDistance = hitDistance;
  854. inputEvent->mouseButton = mouseButton;
  855. dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEDOWN);
  856. if(timestamp - lastClickTicks < 400) {
  857. InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp);
  858. inputEvent->mouseButton = mouseButton;
  859. dispatchEvent(inputEvent, InputEvent::EVENT_DOUBLECLICK);
  860. }
  861. lastClickTicks = timestamp;
  862. if(blockMouseInput) {
  863. ret.blocked = true;
  864. }
  865. }
  866. }
  867. for(int i=children.size()-1; i>=0; i--) {
  868. MouseEventResult childRes = children[i]->onMouseDown(ray, mouseButton, timestamp);
  869. if(childRes.hit)
  870. ret.hit = true;
  871. if(childRes.blocked) {
  872. ret.blocked = true;
  873. break;
  874. }
  875. }
  876. }
  877. return ret;
  878. }
  879. MouseEventResult Entity::onMouseUp(const Ray &ray, int mouseButton, int timestamp) {
  880. MouseEventResult ret;
  881. ret.hit = false;
  882. ret.blocked = false;
  883. Number hitDistance;
  884. if(processInputEvents && enabled) {
  885. Vector3 localCoordinate = Vector3(ray.origin.x,ray.origin.y,0);
  886. Matrix4 inverse = getConcatenatedMatrix().Inverse();
  887. localCoordinate = inverse * localCoordinate;
  888. InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp);
  889. inputEvent->mouseButton = mouseButton;
  890. hitDistance = ray.boxIntersect(bBox, getAnchorAdjustedMatrix());
  891. if(hitDistance >= 0.0) {
  892. ret.hit = true;
  893. inputEvent->hitDistance = hitDistance;
  894. dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEUP);
  895. if(blockMouseInput) {
  896. ret.blocked = true;
  897. }
  898. } else {
  899. dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEUP_OUTSIDE);
  900. }
  901. for(int i=children.size()-1; i>=0; i--) {
  902. MouseEventResult childRes = children[i]->onMouseUp(ray, mouseButton, timestamp);
  903. if(childRes.hit)
  904. ret.hit = true;
  905. if(childRes.blocked) {
  906. ret.blocked = true;
  907. break;
  908. }
  909. }
  910. }
  911. return ret;}
  912. MouseEventResult Entity::onMouseMove(const Ray &ray, int timestamp) {
  913. MouseEventResult ret;
  914. ret.hit = false;
  915. ret.blocked = false;
  916. Number hitDistance;
  917. if(processInputEvents && enabled) {
  918. Vector3 localCoordinate = Vector3(ray.origin.x,ray.origin.y,0);
  919. Matrix4 inverse = getConcatenatedMatrix().Inverse();
  920. localCoordinate = inverse * localCoordinate;
  921. hitDistance = ray.boxIntersect(bBox, getAnchorAdjustedMatrix());
  922. if(hitDistance >= 0.0) {
  923. //setColor(1.0, 0.0, 0.0, 1.0);
  924. ret.hit = true;
  925. InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp);
  926. inputEvent->hitDistance = hitDistance;
  927. dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEMOVE);
  928. if(!mouseOver) {
  929. InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp);
  930. inputEvent->hitDistance = hitDistance;
  931. dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEOVER);
  932. mouseOver = true;
  933. }
  934. if(blockMouseInput) {
  935. ret.blocked = true;
  936. }
  937. } else {
  938. if(mouseOver) {
  939. dispatchEvent(new InputEvent(Vector2(localCoordinate.x, localCoordinate.y), timestamp), InputEvent::EVENT_MOUSEOUT);
  940. mouseOver = false;
  941. }
  942. }
  943. for(int i=children.size()-1; i>=0; i--) {
  944. MouseEventResult childRes = children[i]->onMouseMove(ray, timestamp);
  945. if(childRes.hit)
  946. ret.hit = true;
  947. if(childRes.blocked) {
  948. ret.blocked = true;
  949. break;
  950. }
  951. }
  952. }
  953. return ret;
  954. }
  955. MouseEventResult Entity::onMouseWheelUp(const Ray &ray, int timestamp) {
  956. MouseEventResult ret;
  957. ret.hit = false;
  958. ret.blocked = false;
  959. Number hitDistance;
  960. if(processInputEvents && enabled) {
  961. hitDistance = ray.boxIntersect(bBox, getAnchorAdjustedMatrix());
  962. if(hitDistance >= 0.0) {
  963. ret.hit = true;
  964. Vector3 localCoordinate = Vector3(ray.origin.x,ray.origin.y,0);
  965. Matrix4 inverse = getConcatenatedMatrix().Inverse();
  966. localCoordinate = inverse * localCoordinate;
  967. InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp);
  968. inputEvent->hitDistance = hitDistance;
  969. dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEWHEEL_UP);
  970. if(blockMouseInput) {
  971. ret.blocked = true;
  972. }
  973. }
  974. for(int i=children.size()-1; i>=0; i--) {
  975. MouseEventResult childRes = children[i]->onMouseWheelUp(ray, timestamp);
  976. if(childRes.hit)
  977. ret.hit = true;
  978. if(childRes.blocked) {
  979. ret.blocked = true;
  980. break;
  981. }
  982. }
  983. }
  984. return ret;
  985. }
  986. MouseEventResult Entity::onMouseWheelDown(const Ray &ray, int timestamp) {
  987. MouseEventResult ret;
  988. ret.hit = false;
  989. ret.blocked = false;
  990. Number hitDistance;
  991. if(processInputEvents && enabled) {
  992. hitDistance = ray.boxIntersect(bBox, getAnchorAdjustedMatrix());
  993. if(hitDistance >= 0.0) {
  994. ret.hit = true;
  995. Vector3 localCoordinate = Vector3(ray.origin.x,ray.origin.y,0);
  996. Matrix4 inverse = getConcatenatedMatrix().Inverse();
  997. localCoordinate = inverse * localCoordinate;
  998. InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x, localCoordinate.y*yAdjust), timestamp);
  999. inputEvent->hitDistance = hitDistance;
  1000. dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEWHEEL_DOWN);
  1001. if(blockMouseInput) {
  1002. ret.blocked = true;
  1003. }
  1004. }
  1005. for(int i=children.size()-1; i>=0; i--) {
  1006. MouseEventResult childRes = children[i]->onMouseWheelDown(ray, timestamp);
  1007. if(childRes.hit)
  1008. ret.hit = true;
  1009. if(childRes.blocked) {
  1010. ret.blocked = true;
  1011. break;
  1012. }
  1013. }
  1014. }
  1015. return ret;
  1016. }