Browse Source

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 năm trước cách đây
mục cha
commit
a63db488f3

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

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

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

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

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

@@ -49,7 +49,10 @@ class TransformGizmo : public Entity {
         void setGizmoMode(int newMode);
     
 		void Update();
-		
+    
+        void enableSnap(bool val);
+        void setSnapSize(Number snapSize);
+    
 		void setTransformSelection(std::vector<Entity*> selectedEntities);
 		
 		void transformSelectedEntities(const Vector3 &move, const Vector3 &scale, Number rotate);
@@ -86,6 +89,9 @@ class TransformGizmo : public Entity {
     
 	private:
     
+        bool snapEnabled;
+        Number snapSize;
+    
         void dispatchEndEvent();
     
         int transformMode;
@@ -95,6 +101,7 @@ class TransformGizmo : public Entity {
         int centerMode;
 	
 		std::vector<Entity*> selectedEntities;
+		std::vector<Vector3> entityPositions;
     
 		Scene *targetScene;
 		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"
 
-EditorGridSettingsWindow::EditorGridSettingsWindow(EditorGrid *grid) : UIWindow("Grid Settings", 100, 160) {
+EditorGridSettingsWindow::EditorGridSettingsWindow(EditorGrid *grid) : UIWindow("Grid Settings", 100, 190) {
     
     visible = false;
     enabled = false;
     
     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);
     label->setColor(1.0, 1.0 ,1.0, 1.0);
     addChild(label);
-    label->setPosition(10, 40);
+    label->setPosition(10, 70);
     
     sizeInput = new UITextInput(false, 50, 10);
     addChild(sizeInput);
-    sizeInput->setPosition(60, 38);
+    sizeInput->setPosition(60, 68);
     sizeInput->setNumberOnly(true);
     sizeInput->setText(String::NumberToString(grid->getGridSize()));
     sizeInput->addEventListener(this, UIEvent::CHANGE_EVENT);
@@ -44,28 +49,28 @@ EditorGridSettingsWindow::EditorGridSettingsWindow(EditorGrid *grid) : UIWindow(
     label = new UILabel("Count:", 12);
     label->setColor(1.0, 1.0 ,1.0, 1.0);
     addChild(label);
-    label->setPosition(10, 70);
+    label->setPosition(10, 100);
 
     countInput = new UITextInput(false, 50, 10);
     addChild(countInput);
-    countInput->setPosition(60, 68);
+    countInput->setPosition(60, 98);
     countInput->setNumberOnly(true);
     countInput->setText(String::IntToString(grid->getGridLen()));
     countInput->addEventListener(this, UIEvent::CHANGE_EVENT);
     
     xAxisBox = new UICheckBox("X Axis", grid->isXAxisEnabled());
     addChild(xAxisBox);
-    xAxisBox->setPosition(10, 100);
+    xAxisBox->setPosition(10, 130);
     xAxisBox->addEventListener(this, UIEvent::CHANGE_EVENT);
     
     yAxisBox = new UICheckBox("Y Axis", grid->isYAxisEnabled());
     addChild(yAxisBox);
-    yAxisBox->setPosition(10, 125);
+    yAxisBox->setPosition(10, 155);
     yAxisBox->addEventListener(this, UIEvent::CHANGE_EVENT);
     
     zAxisBox = new UICheckBox("Z Axis", grid->isZAxisEnabled());
     addChild(zAxisBox);
-    zAxisBox->setPosition(10, 150);
+    zAxisBox->setPosition(10, 180);
     zAxisBox->addEventListener(this, UIEvent::CHANGE_EVENT);
 }
 
@@ -80,6 +85,8 @@ void EditorGridSettingsWindow::handleEvent(Event *event) {
         grid->enableYAxis(yAxisBox->isChecked());
     } else if(event->getDispatcher() == zAxisBox) {
         grid->enableZAxis(zAxisBox->isChecked());
+    } else if(event->getDispatcher() == visibleCheck) {
+        grid->visible = visibleCheck->isChecked();
     }
     UIWindow::handleEvent(event);
 }
@@ -122,6 +129,7 @@ EditorGrid::EditorGrid() : Entity() {
 void EditorGrid::setGridSize(Number size) {
     gridSize = size;
     rebuildGrid();
+    dispatchEvent(new Event(), Event::CHANGE_EVENT);
 }
 
 void EditorGrid::setGridLen(int len) {
@@ -163,6 +171,13 @@ void EditorGrid::enableZAxis(bool val) {
 
 void EditorGrid::rebuildGrid() {
     
+    int gridLen = this->gridLen;
+    
+    // make sure the grid is even
+    if((gridLen % 2) != 0) {
+        gridLen++;
+    }
+    
     grid->getMesh()->clearMesh();
 
     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);
     
 	grid = new EditorGrid();
+    grid->addEventListener(this, Event::CHANGE_EVENT);
 	mainScene->addChild(grid);
 	
     objectRootBase = new Entity();
@@ -452,6 +453,13 @@ EntityEditorMainView::EntityEditorMainView(PolycodeEditor *editor) {
     gridSettingsButton->setPosition(120, 2);
     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;
     
     input = CoreServices::getInstance()->getCore()->getInput();
@@ -1142,6 +1150,18 @@ void EntityEditorMainView::handleEvent(Event *event) {
     } else if(event->getDispatcher() == gridSettingsButton) {
         gridSettings->visible = !gridSettings->visible;
         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 {
         if(event->getEventCode() == InputEvent::EVENT_MOUSEDOWN && hasFocus && event->getDispatcher() != renderTextureShape) {
             InputEvent *inputEvent = (InputEvent*) event;

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

@@ -100,6 +100,9 @@ TransformGizmo::TransformGizmo(Scene *targetScene, Camera *targetCamera) : Entit
     enableGizmo = true;
     firstMove = true;
     
+    snapEnabled = false;
+    snapSize = 1.0;
+    
 	this->targetScene = targetScene;
 	this->targetCamera = targetCamera;
 	
@@ -331,6 +334,14 @@ TransformGizmo::TransformGizmo(Scene *targetScene, Camera *targetCamera) : Entit
 	setTransformMode(TRANSFORM_MOVE);
 }
 
+void TransformGizmo::enableSnap(bool val) {
+    snapEnabled = val;
+}
+
+void TransformGizmo::setSnapSize(Number snapSize) {
+    this->snapSize = snapSize;
+}
+
 void TransformGizmo::setTransformOrientation(int orientation) {
     this->orientation = orientation;
 }
@@ -462,6 +473,7 @@ void TransformGizmo::setTransformMode(int newMode) {
 }
 
 void TransformGizmo::setTransformSelection(std::vector<Entity*> selectedEntities) {
+    entityPositions.clear();
 	this->selectedEntities = selectedEntities;
 	if(selectedEntities.size() > 0) {
 		visible = true;
@@ -469,7 +481,8 @@ void TransformGizmo::setTransformSelection(std::vector<Entity*> selectedEntities
 		
 		Vector3 centerPoint;
 		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();
 		setPosition(centerPoint);
@@ -477,6 +490,7 @@ void TransformGizmo::setTransformSelection(std::vector<Entity*> selectedEntities
 		visible = false;
 		enabled = false;
 	}
+    
 }
 
 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();
-    
 	for(int i=0; i < selectedEntities.size(); i++) {
         
         
         if((orientation == ORIENTATION_GLOBAL && mode != TRANSFORM_SCALE_VIEW) || (ORIENTATION_LOCAL && mode == TRANSFORM_MOVE_VIEW)) {
-            selectedEntities[i]->Translate(move);
+            entityPositions[i] += move;
             
             Quaternion q;
             Quaternion currentRotation = selectedEntities[i]->getRotationQuat();
@@ -538,7 +551,7 @@ void TransformGizmo::transformSelectedEntities(const Vector3 &move, const Vector
             }
         } else {
             
-            selectedEntities[i]->Translate(getRotationQuat().applyTo(move));
+            entityPositions[i] += getRotationQuat().applyTo(move);
             
             Quaternion q;
             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]);
+            }
+        }
 		
 	}
 }