private bool CacheVoxelStructure() { voxelStructure = (VoxelStructure)target; voxelStructure.ClampDimensions(); if (voxelStructure == null) { return(false); } else { return(true); } }
//Here is where we handle any user interactions with the scene view void OnSceneGUI() { VoxelStructure voxelStructure = (VoxelStructure)target; //If we are here, we must have received some event. Grab it for later usage. Event e = Event.current; currentMousePosition = e.mousePosition; //small hack to ensure the mouse events aren't overridden by the default unity actions if (e.shift && e.type == EventType.Layout) { HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); } //If it's an undo or redo, just redraw. //This doesn't always seem to fire, I'm think there must be a race condition if (e.commandName == "UndoRedoPerformed") { voxelStructure.Draw(); Repaint(); return; } //if shift && mousedown, do that spraypaint thang if (e.shift && (e.type == EventType.MouseDrag || e.type == EventType.MouseDown)) { var currentTime = DateTime.Now; var dTime = currentTime.Subtract(lastSprayPaintAction); if (dTime.Milliseconds > sprayPaintThrottle) { lastSprayPaintAction = currentTime; if (e.button == 0) { HandleClick(e.mousePosition, VoxelStructure.LMB_Action, e.button); } else if (e.button == 1) { HandleClick(e.mousePosition, VoxelStructure.RMB_Action, e.button); } e.Use(); } } else if (e.type == EventType.MouseDown) { if (e.button == 0) { wasLeftMouseDown = true; HandleClick(currentMousePosition, VoxelStructure.LMB_Action, e.button); } else if (e.button == 1) { wasRightMouseDown = true; HandleClick(currentMousePosition, VoxelStructure.RMB_Action, e.button); } } //Keyboard shortcuts for tools if (e.type == EventType.KeyUp && e.keyCode == KeyCode.Alpha1) { EditorPrefs.SetInt("LMBAction", (int)VoxelStructure.Action.Add); VoxelStructure.LMB_Action = VoxelStructure.Action.Add; Repaint(); e.Use(); } if (e.type == EventType.KeyUp && e.keyCode == KeyCode.Alpha2) { EditorPrefs.SetInt("LMBAction", (int)VoxelStructure.Action.MaxPull); VoxelStructure.LMB_Action = VoxelStructure.Action.MaxPull; Repaint(); e.Use(); } if (e.type == EventType.KeyUp && e.keyCode == KeyCode.Alpha3) { EditorPrefs.SetInt("LMBAction", (int)VoxelStructure.Action.Paint); VoxelStructure.LMB_Action = VoxelStructure.Action.Paint; Repaint(); e.Use(); } if (e.type == EventType.KeyUp && e.keyCode == KeyCode.Alpha4) { EditorPrefs.SetInt("LMBAction", (int)VoxelStructure.Action.Erase); VoxelStructure.LMB_Action = VoxelStructure.Action.Erase; Repaint(); e.Use(); } if (e.type == EventType.KeyUp && e.keyCode == KeyCode.Alpha5) { EditorPrefs.SetInt("LMBAction", (int)VoxelStructure.Action.Flood); VoxelStructure.LMB_Action = VoxelStructure.Action.Flood; Repaint(); e.Use(); } }
void HandleClick(Vector2 mousePosition, VoxelStructure.Action action, int button) { VoxelStructure voxelStructure = (VoxelStructure)target; //Strange for the editor, but can sometimes happen) if (Camera.current == null) { Debug.Log("Strange, Camera.current was null. Please try again in a moment."); } else { RaycastHit hit; Ray ray = HandleUtility.GUIPointToWorldRay(currentMousePosition); //If we hit something if (Physics.Raycast(ray, out hit, 100f)) { //get the position of the voxel that was clicked var targetVoxelPositionPreScale = (hit.point - (hit.normal * .01f)) - voxelStructure.transform.position; var targetVoxelPosition = new Vector3( targetVoxelPositionPreScale.x / voxelStructure.transform.localScale.x, targetVoxelPositionPreScale.y / voxelStructure.transform.localScale.y, targetVoxelPositionPreScale.z / voxelStructure.transform.localScale.z ); //and the neighbor on the side it was clicked on var normalNeighborVoxelPositionPreScale = (hit.point + (hit.normal * .01f)) - voxelStructure.transform.position; var normalNeighborVoxelPosition = new Vector3( normalNeighborVoxelPositionPreScale.x / voxelStructure.transform.localScale.x, normalNeighborVoxelPositionPreScale.y / voxelStructure.transform.localScale.y, normalNeighborVoxelPositionPreScale.z / voxelStructure.transform.localScale.z ); //Use the currently selected action to determine which function to call on the Voxel Structure switch (action) { case VoxelStructure.Action.MaxPull: Undo.RecordObject(voxelStructure, "Max Pull"); voxelStructure.MaxExtrude(targetVoxelPosition, hit.normal); voxelStructure.Draw(); EditorUtility.SetDirty(target); break; case VoxelStructure.Action.Paint: Undo.RecordObject(voxelStructure, "Paint"); if (button == 0) { voxelStructure.SetVoxel(targetVoxelPosition, VoxelStructure.SelectedVoxelIndex); } else if (button == 1) { voxelStructure.SetVoxel(targetVoxelPosition, VoxelStructure.SelectedVoxelIndex); } voxelStructure.Draw(); EditorUtility.SetDirty(target); break; case VoxelStructure.Action.Add: Undo.RecordObject(voxelStructure, "Add"); if (button == 0) { voxelStructure.SetVoxel(normalNeighborVoxelPosition, VoxelStructure.SelectedVoxelIndex); } else if (button == 1) { voxelStructure.SetVoxel(normalNeighborVoxelPosition, VoxelStructure.SelectedVoxelIndex); } voxelStructure.Draw(); EditorUtility.SetDirty(target); break; case VoxelStructure.Action.Erase: Undo.RecordObject(voxelStructure, "Erase"); voxelStructure.SetVoxel(targetVoxelPosition, 0); voxelStructure.Draw(); EditorUtility.SetDirty(target); break; case VoxelStructure.Action.Flood: Undo.RecordObject(voxelStructure, "Flood"); voxelStructure.FloodBasic((int)Mathf.Floor(hit.point.y)); voxelStructure.Draw(); EditorUtility.SetDirty(target); break; } } } }
void HandleClick(Vector2 mousePosition, VoxelStructure.Action action) { VoxelStructure voxelStructure = (VoxelStructure)target; //Strange for the editor, but can sometimes happen) if(Camera.current==null){ Debug.Log("Strange, Camera.current was null. Please try again in a moment."); }else{ RaycastHit hit; Ray ray = HandleUtility.GUIPointToWorldRay(currentMousePosition); //If we hit something if (Physics.Raycast (ray, out hit, Mathf.Infinity)){ //get the position of the voxel that was clicked var targetVoxelPositionPreScale = (hit.point - (hit.normal*.01f)) - voxelStructure.transform.position; var targetVoxelPosition = new Vector3( targetVoxelPositionPreScale.x/voxelStructure.transform.localScale.x, targetVoxelPositionPreScale.y/voxelStructure.transform.localScale.y, targetVoxelPositionPreScale.z/voxelStructure.transform.localScale.z ); //and the neighbor on the side it was clicked on var normalNeighborVoxelPositionPreScale = (hit.point + (hit.normal*.01f)) - voxelStructure.transform.position; var normalNeighborVoxelPosition = new Vector3( normalNeighborVoxelPositionPreScale.x / voxelStructure.transform.localScale.x, normalNeighborVoxelPositionPreScale.y / voxelStructure.transform.localScale.y, normalNeighborVoxelPositionPreScale.z / voxelStructure.transform.localScale.z ); //Use the currently selected action to determine which function to call on the Voxel Structure switch(action){ case VoxelStructure.Action.MaxPull: Undo.RecordObject(voxelStructure, "Max Pull"); voxelStructure.MaxExtrude(targetVoxelPosition, hit.normal); voxelStructure.Draw(); EditorUtility.SetDirty(target); break; case VoxelStructure.Action.Paint: Undo.RecordObject(voxelStructure, "Paint"); voxelStructure.SetVoxel(targetVoxelPosition, VoxelStructure.SelectedVoxelType); voxelStructure.Draw(); EditorUtility.SetDirty(target); break; case VoxelStructure.Action.Add: Undo.RecordObject(voxelStructure, "Add"); voxelStructure.SetVoxel(normalNeighborVoxelPosition, VoxelStructure.SelectedVoxelType); voxelStructure.Draw(); EditorUtility.SetDirty(target); break; case VoxelStructure.Action.Erase: Undo.RecordObject(voxelStructure, "Erase"); voxelStructure.SetVoxel(targetVoxelPosition, 0); voxelStructure.Draw(); EditorUtility.SetDirty(target); break; case VoxelStructure.Action.Flood: Undo.RecordObject(voxelStructure, "Flood"); voxelStructure.FloodBasic((int)Mathf.Floor(hit.point.y)); voxelStructure.Draw(); EditorUtility.SetDirty(target); break; } } } }