Kaynağa Gözat

Added grid-snapping to the entity editor, added grid snap buttons to the bottom menu, added checkbox to enabled and disable grid to the grid settings panel

Ivan Safrin 12 yıl önce
ebeveyn
işleme
a63db488f3

+ 3 - 0
IDE/Contents/Include/EditorGrid.h

@@ -80,6 +80,9 @@ public:
 protected:
 protected:
     
     
     EditorGrid *grid;
     EditorGrid *grid;
+    
+    UICheckBox *visibleCheck;
+    
     UITextInput *sizeInput;
     UITextInput *sizeInput;
     UITextInput *countInput;
     UITextInput *countInput;
     
     

+ 1 - 0
IDE/Contents/Include/PolycodeEntityEditor.h

@@ -248,6 +248,7 @@ class EntityEditorMainView : public UIElement {
     
     
             UIImageButton *gridSettingsButton;
             UIImageButton *gridSettingsButton;
             EditorGridSettingsWindow *gridSettings;
             EditorGridSettingsWindow *gridSettings;
+            UIIconSelector *snapSelector;
     
     
             CameraPreviewWindow *cameraPreview;
             CameraPreviewWindow *cameraPreview;
     
     

+ 8 - 1
IDE/Contents/Include/TransformGizmo.h

@@ -49,7 +49,10 @@ class TransformGizmo : public Entity {
         void setGizmoMode(int newMode);
         void setGizmoMode(int newMode);
     
     
 		void Update();
 		void Update();
-		
+    
+        void enableSnap(bool val);
+        void setSnapSize(Number snapSize);
+    
 		void setTransformSelection(std::vector<Entity*> selectedEntities);
 		void setTransformSelection(std::vector<Entity*> selectedEntities);
 		
 		
 		void transformSelectedEntities(const Vector3 &move, const Vector3 &scale, Number rotate);
 		void transformSelectedEntities(const Vector3 &move, const Vector3 &scale, Number rotate);
@@ -86,6 +89,9 @@ class TransformGizmo : public Entity {
     
     
 	private:
 	private:
     
     
+        bool snapEnabled;
+        Number snapSize;
+    
         void dispatchEndEvent();
         void dispatchEndEvent();
     
     
         int transformMode;
         int transformMode;
@@ -95,6 +101,7 @@ class TransformGizmo : public Entity {
         int centerMode;
         int centerMode;
 	
 	
 		std::vector<Entity*> selectedEntities;
 		std::vector<Entity*> selectedEntities;
+		std::vector<Vector3> entityPositions;
     
     
 		Scene *targetScene;
 		Scene *targetScene;
 		Camera *targetCamera;
 		Camera *targetCamera;

BIN
IDE/Contents/Resources/Images/entityEditor/snap_off.png


BIN
IDE/Contents/Resources/Images/entityEditor/snap_on.png


+ 23 - 8
IDE/Contents/Source/EditorGrid.cpp

@@ -22,21 +22,26 @@
  
  
 #include "EditorGrid.h"
 #include "EditorGrid.h"
 
 
-EditorGridSettingsWindow::EditorGridSettingsWindow(EditorGrid *grid) : UIWindow("Grid Settings", 100, 160) {
+EditorGridSettingsWindow::EditorGridSettingsWindow(EditorGrid *grid) : UIWindow("Grid Settings", 100, 190) {
     
     
     visible = false;
     visible = false;
     enabled = false;
     enabled = false;
     
     
     this->grid = grid;
     this->grid = grid;
     
     
+    visibleCheck = new UICheckBox("Enabled", grid->visible);
+    addChild(visibleCheck);
+    visibleCheck->setPosition(10, 40);
+    visibleCheck->addEventListener(this, UIEvent::CHANGE_EVENT);
+    
     UILabel *label = new UILabel("Size:", 12);
     UILabel *label = new UILabel("Size:", 12);
     label->setColor(1.0, 1.0 ,1.0, 1.0);
     label->setColor(1.0, 1.0 ,1.0, 1.0);
     addChild(label);
     addChild(label);
-    label->setPosition(10, 40);
+    label->setPosition(10, 70);
     
     
     sizeInput = new UITextInput(false, 50, 10);
     sizeInput = new UITextInput(false, 50, 10);
     addChild(sizeInput);
     addChild(sizeInput);
-    sizeInput->setPosition(60, 38);
+    sizeInput->setPosition(60, 68);
     sizeInput->setNumberOnly(true);
     sizeInput->setNumberOnly(true);
     sizeInput->setText(String::NumberToString(grid->getGridSize()));
     sizeInput->setText(String::NumberToString(grid->getGridSize()));
     sizeInput->addEventListener(this, UIEvent::CHANGE_EVENT);
     sizeInput->addEventListener(this, UIEvent::CHANGE_EVENT);
@@ -44,28 +49,28 @@ EditorGridSettingsWindow::EditorGridSettingsWindow(EditorGrid *grid) : UIWindow(
     label = new UILabel("Count:", 12);
     label = new UILabel("Count:", 12);
     label->setColor(1.0, 1.0 ,1.0, 1.0);
     label->setColor(1.0, 1.0 ,1.0, 1.0);
     addChild(label);
     addChild(label);
-    label->setPosition(10, 70);
+    label->setPosition(10, 100);
 
 
     countInput = new UITextInput(false, 50, 10);
     countInput = new UITextInput(false, 50, 10);
     addChild(countInput);
     addChild(countInput);
-    countInput->setPosition(60, 68);
+    countInput->setPosition(60, 98);
     countInput->setNumberOnly(true);
     countInput->setNumberOnly(true);
     countInput->setText(String::IntToString(grid->getGridLen()));
     countInput->setText(String::IntToString(grid->getGridLen()));
     countInput->addEventListener(this, UIEvent::CHANGE_EVENT);
     countInput->addEventListener(this, UIEvent::CHANGE_EVENT);
     
     
     xAxisBox = new UICheckBox("X Axis", grid->isXAxisEnabled());
     xAxisBox = new UICheckBox("X Axis", grid->isXAxisEnabled());
     addChild(xAxisBox);
     addChild(xAxisBox);
-    xAxisBox->setPosition(10, 100);
+    xAxisBox->setPosition(10, 130);
     xAxisBox->addEventListener(this, UIEvent::CHANGE_EVENT);
     xAxisBox->addEventListener(this, UIEvent::CHANGE_EVENT);
     
     
     yAxisBox = new UICheckBox("Y Axis", grid->isYAxisEnabled());
     yAxisBox = new UICheckBox("Y Axis", grid->isYAxisEnabled());
     addChild(yAxisBox);
     addChild(yAxisBox);
-    yAxisBox->setPosition(10, 125);
+    yAxisBox->setPosition(10, 155);
     yAxisBox->addEventListener(this, UIEvent::CHANGE_EVENT);
     yAxisBox->addEventListener(this, UIEvent::CHANGE_EVENT);
     
     
     zAxisBox = new UICheckBox("Z Axis", grid->isZAxisEnabled());
     zAxisBox = new UICheckBox("Z Axis", grid->isZAxisEnabled());
     addChild(zAxisBox);
     addChild(zAxisBox);
-    zAxisBox->setPosition(10, 150);
+    zAxisBox->setPosition(10, 180);
     zAxisBox->addEventListener(this, UIEvent::CHANGE_EVENT);
     zAxisBox->addEventListener(this, UIEvent::CHANGE_EVENT);
 }
 }
 
 
@@ -80,6 +85,8 @@ void EditorGridSettingsWindow::handleEvent(Event *event) {
         grid->enableYAxis(yAxisBox->isChecked());
         grid->enableYAxis(yAxisBox->isChecked());
     } else if(event->getDispatcher() == zAxisBox) {
     } else if(event->getDispatcher() == zAxisBox) {
         grid->enableZAxis(zAxisBox->isChecked());
         grid->enableZAxis(zAxisBox->isChecked());
+    } else if(event->getDispatcher() == visibleCheck) {
+        grid->visible = visibleCheck->isChecked();
     }
     }
     UIWindow::handleEvent(event);
     UIWindow::handleEvent(event);
 }
 }
@@ -122,6 +129,7 @@ EditorGrid::EditorGrid() : Entity() {
 void EditorGrid::setGridSize(Number size) {
 void EditorGrid::setGridSize(Number size) {
     gridSize = size;
     gridSize = size;
     rebuildGrid();
     rebuildGrid();
+    dispatchEvent(new Event(), Event::CHANGE_EVENT);
 }
 }
 
 
 void EditorGrid::setGridLen(int len) {
 void EditorGrid::setGridLen(int len) {
@@ -163,6 +171,13 @@ void EditorGrid::enableZAxis(bool val) {
 
 
 void EditorGrid::rebuildGrid() {
 void EditorGrid::rebuildGrid() {
     
     
+    int gridLen = this->gridLen;
+    
+    // make sure the grid is even
+    if((gridLen % 2) != 0) {
+        gridLen++;
+    }
+    
     grid->getMesh()->clearMesh();
     grid->getMesh()->clearMesh();
 
 
     for(int x=0; x < gridLen+1; x++) {
     for(int x=0; x < gridLen+1; x++) {

+ 20 - 0
IDE/Contents/Source/PolycodeEntityEditor.cpp

@@ -371,6 +371,7 @@ EntityEditorMainView::EntityEditorMainView(PolycodeEditor *editor) {
 	mainScene->getDefaultCamera()->setClippingPlanes(0.01, 10000);
 	mainScene->getDefaultCamera()->setClippingPlanes(0.01, 10000);
     
     
 	grid = new EditorGrid();
 	grid = new EditorGrid();
+    grid->addEventListener(this, Event::CHANGE_EVENT);
 	mainScene->addChild(grid);
 	mainScene->addChild(grid);
 	
 	
     objectRootBase = new Entity();
     objectRootBase = new Entity();
@@ -452,6 +453,13 @@ EntityEditorMainView::EntityEditorMainView(PolycodeEditor *editor) {
     gridSettingsButton->setPosition(120, 2);
     gridSettingsButton->setPosition(120, 2);
     gridSettingsButton->addEventListener(this, UIEvent::CLICK_EVENT);
     gridSettingsButton->addEventListener(this, UIEvent::CLICK_EVENT);
     
     
+    snapSelector = new UIIconSelector();
+    snapSelector->addIcon("entityEditor/snap_off.png");
+    snapSelector->addIcon("entityEditor/snap_on.png");
+    bottomBar->addChild(snapSelector);
+    snapSelector->setPosition(156, 2);
+    snapSelector->addEventListener(this, UIEvent::SELECT_EVENT);
+    
     editorMode = EDITOR_MODE_3D;
     editorMode = EDITOR_MODE_3D;
     
     
     input = CoreServices::getInstance()->getCore()->getInput();
     input = CoreServices::getInstance()->getCore()->getInput();
@@ -1142,6 +1150,18 @@ void EntityEditorMainView::handleEvent(Event *event) {
     } else if(event->getDispatcher() == gridSettingsButton) {
     } else if(event->getDispatcher() == gridSettingsButton) {
         gridSettings->visible = !gridSettings->visible;
         gridSettings->visible = !gridSettings->visible;
         gridSettings->enabled = !gridSettings->enabled;
         gridSettings->enabled = !gridSettings->enabled;
+    } else if(event->getDispatcher() == snapSelector) {
+        switch(snapSelector->getSelectedIndex()) {
+            case 0:
+                transformGizmo->enableSnap(false);
+            break;
+            case 1:
+                transformGizmo->enableSnap(true);
+                transformGizmo->setSnapSize(grid->getGridSize());
+            break;
+        }
+    } else if(event->getDispatcher() == grid) {
+                transformGizmo->setSnapSize(grid->getGridSize());        
     } else {
     } else {
         if(event->getEventCode() == InputEvent::EVENT_MOUSEDOWN && hasFocus && event->getDispatcher() != renderTextureShape) {
         if(event->getEventCode() == InputEvent::EVENT_MOUSEDOWN && hasFocus && event->getDispatcher() != renderTextureShape) {
             InputEvent *inputEvent = (InputEvent*) event;
             InputEvent *inputEvent = (InputEvent*) event;

+ 30 - 4
IDE/Contents/Source/TransformGizmo.cpp

@@ -100,6 +100,9 @@ TransformGizmo::TransformGizmo(Scene *targetScene, Camera *targetCamera) : Entit
     enableGizmo = true;
     enableGizmo = true;
     firstMove = true;
     firstMove = true;
     
     
+    snapEnabled = false;
+    snapSize = 1.0;
+    
 	this->targetScene = targetScene;
 	this->targetScene = targetScene;
 	this->targetCamera = targetCamera;
 	this->targetCamera = targetCamera;
 	
 	
@@ -331,6 +334,14 @@ TransformGizmo::TransformGizmo(Scene *targetScene, Camera *targetCamera) : Entit
 	setTransformMode(TRANSFORM_MOVE);
 	setTransformMode(TRANSFORM_MOVE);
 }
 }
 
 
+void TransformGizmo::enableSnap(bool val) {
+    snapEnabled = val;
+}
+
+void TransformGizmo::setSnapSize(Number snapSize) {
+    this->snapSize = snapSize;
+}
+
 void TransformGizmo::setTransformOrientation(int orientation) {
 void TransformGizmo::setTransformOrientation(int orientation) {
     this->orientation = orientation;
     this->orientation = orientation;
 }
 }
@@ -462,6 +473,7 @@ void TransformGizmo::setTransformMode(int newMode) {
 }
 }
 
 
 void TransformGizmo::setTransformSelection(std::vector<Entity*> selectedEntities) {
 void TransformGizmo::setTransformSelection(std::vector<Entity*> selectedEntities) {
+    entityPositions.clear();
 	this->selectedEntities = selectedEntities;
 	this->selectedEntities = selectedEntities;
 	if(selectedEntities.size() > 0) {
 	if(selectedEntities.size() > 0) {
 		visible = true;
 		visible = true;
@@ -469,7 +481,8 @@ void TransformGizmo::setTransformSelection(std::vector<Entity*> selectedEntities
 		
 		
 		Vector3 centerPoint;
 		Vector3 centerPoint;
 		for(int i=0; i < selectedEntities.size(); i++) {
 		for(int i=0; i < selectedEntities.size(); i++) {
-			centerPoint += selectedEntities[i]->getConcatenatedMatrix().getPosition();			
+			centerPoint += selectedEntities[i]->getConcatenatedMatrix().getPosition();
+            entityPositions.push_back(selectedEntities[i]->getPosition());
 		}
 		}
 		centerPoint = centerPoint / selectedEntities.size();
 		centerPoint = centerPoint / selectedEntities.size();
 		setPosition(centerPoint);
 		setPosition(centerPoint);
@@ -477,6 +490,7 @@ void TransformGizmo::setTransformSelection(std::vector<Entity*> selectedEntities
 		visible = false;
 		visible = false;
 		enabled = false;
 		enabled = false;
 	}
 	}
+    
 }
 }
 
 
 void TransformGizmo::transformSelectedEntities(const Vector3 &move, const Vector3 &scale, Number rotate) {
 void TransformGizmo::transformSelectedEntities(const Vector3 &move, const Vector3 &scale, Number rotate) {
@@ -487,12 +501,11 @@ void TransformGizmo::transformSelectedEntities(const Vector3 &move, const Vector
     }
     }
     
     
     Vector3 globalCenter = getConcatenatedMatrix().getPosition();
     Vector3 globalCenter = getConcatenatedMatrix().getPosition();
-    
 	for(int i=0; i < selectedEntities.size(); i++) {
 	for(int i=0; i < selectedEntities.size(); i++) {
         
         
         
         
         if((orientation == ORIENTATION_GLOBAL && mode != TRANSFORM_SCALE_VIEW) || (ORIENTATION_LOCAL && mode == TRANSFORM_MOVE_VIEW)) {
         if((orientation == ORIENTATION_GLOBAL && mode != TRANSFORM_SCALE_VIEW) || (ORIENTATION_LOCAL && mode == TRANSFORM_MOVE_VIEW)) {
-            selectedEntities[i]->Translate(move);
+            entityPositions[i] += move;
             
             
             Quaternion q;
             Quaternion q;
             Quaternion currentRotation = selectedEntities[i]->getRotationQuat();
             Quaternion currentRotation = selectedEntities[i]->getRotationQuat();
@@ -538,7 +551,7 @@ void TransformGizmo::transformSelectedEntities(const Vector3 &move, const Vector
             }
             }
         } else {
         } else {
             
             
-            selectedEntities[i]->Translate(getRotationQuat().applyTo(move));
+            entityPositions[i] += getRotationQuat().applyTo(move);
             
             
             Quaternion q;
             Quaternion q;
             Quaternion currentRotation = selectedEntities[i]->getRotationQuat();
             Quaternion currentRotation = selectedEntities[i]->getRotationQuat();
@@ -581,6 +594,19 @@ void TransformGizmo::transformSelectedEntities(const Vector3 &move, const Vector
 
 
             
             
         }
         }
+
+        // snap if moving and snap is on
+        if(scale.length() == 0.0 && rotate == 0.0) {
+            if(snapEnabled) {
+                Vector3 snappedPositon = entityPositions[i];
+                snappedPositon.x = round(((Number)snappedPositon.x)/(snapSize)) * snapSize;
+                snappedPositon.y = round(((Number)snappedPositon.y)/(snapSize)) * snapSize;
+                snappedPositon.z = round(((Number)snappedPositon.z)/(snapSize)) * snapSize;
+                selectedEntities[i]->setPosition(snappedPositon);
+            } else {
+                selectedEntities[i]->setPosition(entityPositions[i]);
+            }
+        }
 		
 		
 	}
 	}
 }
 }