EditorGrid.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Copyright (C) 2013 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 "EditorGrid.h"
  20. EditorGridSettingsWindow::EditorGridSettingsWindow(EditorGrid *grid) : UIWindow("Grid Settings", 100, 190) {
  21. visible = false;
  22. enabled = false;
  23. this->grid = grid;
  24. visibleCheck = new UICheckBox("Enabled", grid->visible);
  25. addChild(visibleCheck);
  26. visibleCheck->setPosition(10, 40);
  27. visibleCheck->addEventListener(this, UIEvent::CHANGE_EVENT);
  28. UILabel *label = new UILabel("Size:", 12);
  29. label->setColor(1.0, 1.0 ,1.0, 1.0);
  30. addChild(label);
  31. label->setPosition(10, 70);
  32. sizeInput = new UITextInput(false, 50, 10);
  33. addChild(sizeInput);
  34. sizeInput->setPosition(60, 68);
  35. sizeInput->setNumberOnly(true);
  36. sizeInput->setText(String::NumberToString(grid->getGridSize()));
  37. sizeInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  38. label = new UILabel("Count:", 12);
  39. label->setColor(1.0, 1.0 ,1.0, 1.0);
  40. addChild(label);
  41. label->setPosition(10, 100);
  42. countInput = new UITextInput(false, 50, 10);
  43. addChild(countInput);
  44. countInput->setPosition(60, 98);
  45. countInput->setNumberOnly(true);
  46. countInput->setText(String::IntToString(grid->getGridLen()));
  47. countInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  48. xAxisBox = new UICheckBox("X Axis", grid->isXAxisEnabled());
  49. addChild(xAxisBox);
  50. xAxisBox->setPosition(10, 130);
  51. xAxisBox->addEventListener(this, UIEvent::CHANGE_EVENT);
  52. yAxisBox = new UICheckBox("Y Axis", grid->isYAxisEnabled());
  53. addChild(yAxisBox);
  54. yAxisBox->setPosition(10, 155);
  55. yAxisBox->addEventListener(this, UIEvent::CHANGE_EVENT);
  56. zAxisBox = new UICheckBox("Z Axis", grid->isZAxisEnabled());
  57. addChild(zAxisBox);
  58. zAxisBox->setPosition(10, 180);
  59. zAxisBox->addEventListener(this, UIEvent::CHANGE_EVENT);
  60. }
  61. void EditorGridSettingsWindow::handleEvent(Event *event) {
  62. if(event->getDispatcher() == sizeInput) {
  63. grid->setGridSize(sizeInput->getText().toNumber());
  64. } else if(event->getDispatcher() == countInput) {
  65. grid->setGridLen(countInput->getText().toInteger());
  66. } else if(event->getDispatcher() == xAxisBox) {
  67. grid->enableXAxis(xAxisBox->isChecked());
  68. } else if(event->getDispatcher() == yAxisBox) {
  69. grid->enableYAxis(yAxisBox->isChecked());
  70. } else if(event->getDispatcher() == zAxisBox) {
  71. grid->enableZAxis(zAxisBox->isChecked());
  72. } else if(event->getDispatcher() == visibleCheck) {
  73. grid->visible = visibleCheck->isChecked();
  74. }
  75. UIWindow::handleEvent(event);
  76. }
  77. EditorGridSettingsWindow::~EditorGridSettingsWindow() {
  78. }
  79. EditorGrid::EditorGrid() : Entity() {
  80. grid = NULL;
  81. gridMode = GRID_MODE_3D;
  82. Mesh *gridMesh = new Mesh(Mesh::LINE_MESH);
  83. grid = new SceneMesh(gridMesh);
  84. grid->setColor(0.3, 0.3, 0.3, 1.0);
  85. grid->setLineWidth(CoreServices::getInstance()->getRenderer()->getBackingResolutionScaleX());
  86. addChild(grid);
  87. yLine = new SceneLine(Vector3(), Vector3());
  88. addChild(yLine);
  89. yLine->setColor(0.0, 0.8, 0.0, 1.0);
  90. yLine->visible = false;
  91. xLine = new SceneLine(Vector3(), Vector3());
  92. addChild(xLine);
  93. xLine->setColor(0.8, 0.0, 0.0, 1.0);
  94. zLine = new SceneLine(Vector3(), Vector3());
  95. addChild(zLine);
  96. zLine->setColor(0.0, 0.0, 0.8, 1.0);
  97. gridSize = 1.0;
  98. gridLen = 16;
  99. rebuildGrid();
  100. grid->setPitch(90);
  101. }
  102. void EditorGrid::setGridSize(Number size) {
  103. gridSize = size;
  104. rebuildGrid();
  105. dispatchEvent(new Event(), Event::CHANGE_EVENT);
  106. }
  107. void EditorGrid::setGridLen(int len) {
  108. gridLen = len;
  109. rebuildGrid();
  110. }
  111. Number EditorGrid::getGridSize() {
  112. return gridSize;
  113. }
  114. int EditorGrid::getGridLen() {
  115. return gridLen;
  116. }
  117. bool EditorGrid::isXAxisEnabled() {
  118. return xLine->visible;
  119. }
  120. bool EditorGrid::isYAxisEnabled() {
  121. return yLine->visible;
  122. }
  123. bool EditorGrid::isZAxisEnabled() {
  124. return zLine->visible;
  125. }
  126. void EditorGrid::enableXAxis(bool val) {
  127. xLine->visible = val;
  128. }
  129. void EditorGrid::enableYAxis(bool val) {
  130. yLine->visible = val;
  131. }
  132. void EditorGrid::enableZAxis(bool val) {
  133. zLine->visible = val;
  134. }
  135. void EditorGrid::rebuildGrid() {
  136. int gridLen = this->gridLen;
  137. // make sure the grid is even
  138. if((gridLen % 2) != 0) {
  139. gridLen++;
  140. }
  141. grid->getMesh()->clearMesh();
  142. for(int x=0; x < gridLen+1; x++) {
  143. grid->getMesh()->addVertex((-gridSize * gridLen * 0.5) + (x * gridSize), (-gridSize * gridLen * 0.5), 0);
  144. grid->getMesh()->addVertex((-gridSize * gridLen * 0.5) + (x * gridSize) , (-gridSize * gridLen * 0.5) + (gridSize * gridLen), 0);
  145. }
  146. for(int y=0; y < gridLen+1; y++) {
  147. grid->getMesh()->addVertex((-gridSize * gridLen * 0.5), (-gridSize * gridLen * 0.5) + (y * gridSize), 0);
  148. grid->getMesh()->addVertex((-gridSize * gridLen * 0.5) + (gridSize * gridLen), (-gridSize * gridLen * 0.5) + (y * gridSize), 0);
  149. }
  150. yLine->setStart(Vector3(0.0, gridSize * gridLen * 0.5, 0.0));
  151. yLine->setEnd(Vector3(0.0, gridSize * gridLen * -0.5, 0.0));
  152. xLine->setStart(Vector3(gridSize * gridLen * 0.5, 0.0, 0.0));
  153. xLine->setEnd(Vector3(gridSize * gridLen * -0.5, 0.0, 0.0));
  154. zLine->setStart(Vector3(0.0, 0.0, gridSize * gridLen * 0.5));
  155. zLine->setEnd(Vector3(0.0, 0.0, gridSize * gridLen * -0.5));
  156. grid->setLocalBoundingBox(gridSize * gridLen, 0.0, gridSize * gridLen);
  157. grid->cacheToVertexBuffer(true);
  158. }
  159. void EditorGrid::setGridMode(int mode) {
  160. gridMode = mode;
  161. if(gridMode == GRID_MODE_3D) {
  162. grid->setPitch(90);
  163. } else {
  164. grid->setPitch(0);
  165. }
  166. }
  167. EditorGrid::~EditorGrid() {
  168. }