Skip to content
This repository has been archived by the owner on Feb 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #18 from Henry00IS/QuickSelect
Browse files Browse the repository at this point in the history
New Features: Quick Select, Stair Brush Fill to Bottom, Find Hidden Faces
  • Loading branch information
Sabresaurus authored Sep 23, 2017
2 parents 360a921 + 7d990b7 commit 59df1fa
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 20 deletions.
37 changes: 33 additions & 4 deletions Scripts/Brushes/CompoundBrushes/Editor/StairBrushInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public class StairBrushInspector : CompoundBrushInspector
// Whether the stairs align to the bottom edge or the top edge
SerializedProperty leadFromTopProp;

protected override void OnEnable ()
// Whether the height of each step is stretched to the floor to fill it
SerializedProperty fillToBottom;

protected override void OnEnable ()
{
base.OnEnable ();
// Setup the SerializedProperties.
Expand All @@ -38,7 +41,8 @@ protected override void OnEnable ()
autoHeightProp = serializedObject.FindProperty ("autoHeight");

leadFromTopProp = serializedObject.FindProperty ("leadFromTop");
}
fillToBottom = serializedObject.FindProperty ("fillToBottom");
}

public override void OnInspectorGUI()
{
Expand Down Expand Up @@ -76,6 +80,8 @@ public override void OnInspectorGUI()
ApplyAndInvalidate();
}

EditorGUILayout.BeginHorizontal();

bool oldValue = leadFromTopProp.boolValue;
bool newValue = GUILayout.Toggle(oldValue, "Lead From Top", EditorStyles.toolbarButton);
if(newValue != oldValue)
Expand All @@ -84,7 +90,19 @@ public override void OnInspectorGUI()
serializedObject.ApplyModifiedProperties();
System.Array.ForEach(BrushTargets, item => item.Invalidate(true));
}
EditorGUILayout.Space();

oldValue = fillToBottom.boolValue;
newValue = GUILayout.Toggle(oldValue, "Fill To Bottom", EditorStyles.toolbarButton);
if (newValue != oldValue)
{
fillToBottom.boolValue = newValue;
serializedObject.ApplyModifiedProperties();
System.Array.ForEach(BrushTargets, item => item.Invalidate(true));
}

EditorGUILayout.EndHorizontal();

EditorGUILayout.Space();
}

base.OnInspectorGUI();
Expand Down Expand Up @@ -115,11 +133,22 @@ private void DrawStairPreview(Rect rect)
stepRect1.center += new Vector2(-stepUISize.x - stepUISpacing.x/2f, stepUISpacing.y / 2f);
GUI.Box(stepRect1, new GUIContent());

// Calculate top right step size
float stepHeight2 = stepUISize.y;

// Apply the fill to bottom height for drawing
if (fillToBottom.boolValue)
stepHeight2 = stepUISize.y * 2 + stepUISpacing.y;

// Draw top right step
Rect stepRect2 = new Rect(centerOffset.x, centerOffset.y, stepUISize.x, stepUISize.y);
Rect stepRect2 = new Rect(centerOffset.x, centerOffset.y, stepUISize.x, stepHeight2);
stepRect2.center += new Vector2(stepUISpacing.x / 2f, -stepUISize.y - stepUISpacing.y/2f);
GUI.Box(stepRect2, new GUIContent());

// Undo the fill to bottom height
if (fillToBottom.boolValue)
stepRect2.height -= stepUISize.y + stepUISpacing.y;

// Draw depth spacing lines
GUI.color = Color.grey;

Expand Down
12 changes: 9 additions & 3 deletions Scripts/Brushes/CompoundBrushes/StairBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public class StairBrush : CompoundBrush
[SerializeField]
bool leadFromTop = false;

public override int BrushCount
[SerializeField]
bool fillToBottom = false;

public override int BrushCount
{
get
{
Expand Down Expand Up @@ -81,14 +84,17 @@ public override void Invalidate (bool polygonsChanged)

for (int i = 0; i < brushCount; i++)
{
Vector3 localPosition = startPosition + Vector3.forward * i * (activeDepth + stepDepthSpacing) + Vector3.up * i * (activeHeight + stepHeightSpacing);
generatedBrushes[i].transform.localPosition = localPosition;
Vector3 localPosition = startPosition + Vector3.forward * i * (activeDepth + stepDepthSpacing) + Vector3.up * i * (activeHeight + stepHeightSpacing) * (fillToBottom ? 0.5f : 1f);
generatedBrushes[i].transform.localPosition = localPosition;

generatedBrushes[i].Mode = this.Mode;
generatedBrushes[i].IsNoCSG = this.IsNoCSG;
generatedBrushes[i].IsVisible = this.IsVisible;
generatedBrushes[i].HasCollision = this.HasCollision;
BrushUtility.Resize(generatedBrushes[i], stepSize);

if (fillToBottom)
stepSize.y += (activeHeight) + stepHeightSpacing;
}
}
}
Expand Down
189 changes: 176 additions & 13 deletions Scripts/Tools/SurfaceEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Sabresaurus.SabreCSG
public class SurfaceEditor : Tool
{
bool selectHelpersVisible = false;
enum Mode { None, Translate, Rotate };
enum Mode { None, Translate, Rotate, QuickSelect };
enum AlignDirection { Top, Bottom, Left, Right, Center };

Mode currentMode = Mode.None;
Expand Down Expand Up @@ -71,14 +71,21 @@ enum AlignDirection { Top, Bottom, Left, Right, Center };

VertexColorWindow vertexColorWindow = null;

Rect ToolbarRect
// Whether we are actively searching for hidden faces.
bool findingHiddenFaces = false;

// Whether quick select is selecting or deselecting polygons.
enum QuickSelectModes { Additive, Subtractive};
QuickSelectModes QuickSelectMode = QuickSelectModes.Additive;

Rect ToolbarRect
{
get
{
Rect rect = new Rect(toolbarRect);
if(selectHelpersVisible)
{
rect.height += 116;
rect.height += 136;
}
return rect;
}
Expand Down Expand Up @@ -248,6 +255,47 @@ void OnMouseDown (SceneView sceneView, Event e)
{
currentMode = Mode.Translate;
}
else if (!e.control && e.shift)
{
QuickSelectMode = QuickSelectModes.Additive;
// Detect whether quick mode will select or deselect polygons
if (sourcePolygon != null)
{
bool match1 = matchedBrushes.ContainsKey(sourcePolygon);
bool match2 = selectedSourcePolygons.Contains(sourcePolygon);

if (match1 || match2)
{
// Subtractive, immediately deselect the polygon in case the user doesn't drag.
QuickSelectMode = QuickSelectModes.Subtractive;
if (match1)
{
matchedBrushes.Remove(sourcePolygon);
}

if (match2)
{
selectedSourcePolygons.Remove(sourcePolygon);
}
}
else
{
// Additive, immediately select the polygon in case the user doesn't drag.
QuickSelectMode = QuickSelectModes.Additive;
if (!match1)
{
matchedBrushes.Add(sourcePolygon, csgModel.FindBrushFromPolygon(sourcePolygon));
}

if (!match2)
{
selectedSourcePolygons.Add(sourcePolygon);
}
}
}

currentMode = Mode.QuickSelect;
}
else
{
currentMode = Mode.None;
Expand Down Expand Up @@ -306,6 +354,10 @@ void OnMouseDrag(SceneView sceneView, Event e)
}
}
}
else if(currentMode == Mode.QuickSelect)
{
OnMouseDragQuickSelect(sceneView, e);
}
}

void EnsureCurrentPolygonSelected()
Expand Down Expand Up @@ -491,7 +543,51 @@ void OnMouseDragRotate(SceneView sceneView, Event e)
}
}

void OnMouseUp (SceneView sceneView, Event e)
void OnMouseDragQuickSelect(SceneView sceneView, Event e)
{
if (!EditorHelper.IsMousePositionInIMGUIRect(e.mousePosition, ToolbarRect))
{
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
Polygon polygon = csgModel.RaycastBuiltPolygons(ray);

if (polygon != null)
{
Polygon sourcePolygon = csgModel.GetSourcePolygon(polygon.UniqueIndex);

if (sourcePolygon != null)
{
if (QuickSelectMode == QuickSelectModes.Additive)
{
if (!matchedBrushes.ContainsKey(sourcePolygon))
{
matchedBrushes.Add(sourcePolygon, csgModel.FindBrushFromPolygon(sourcePolygon));
}

if (!selectedSourcePolygons.Contains(sourcePolygon))
{
selectedSourcePolygons.Add(sourcePolygon);
}
}
else if (QuickSelectMode == QuickSelectModes.Subtractive)
{
if (matchedBrushes.ContainsKey(sourcePolygon))
{
matchedBrushes.Remove(sourcePolygon);
}

if (selectedSourcePolygons.Contains(sourcePolygon))
{
selectedSourcePolygons.Remove(sourcePolygon);
}
}
}
}
}

e.Use();
}

void OnMouseUp (SceneView sceneView, Event e)
{
// Normal selection mode
if (e.button == 0 && !CameraPanInProgress
Expand Down Expand Up @@ -580,9 +676,8 @@ void OnMouseUp (SceneView sceneView, Event e)
// Most recent hit
previousHits.Insert(0, selectedPolygon);

// If holding control or shift, the action counts as selection toggle (if already selected it's removed)
if (EnumHelper.IsFlagSet(e.modifiers, EventModifiers.Shift)
|| EnumHelper.IsFlagSet(e.modifiers, EventModifiers.Control))
// If holding control, the action counts as selection toggle (if already selected it's removed)
if (EnumHelper.IsFlagSet(e.modifiers, EventModifiers.Control))
{
if (selectedSourcePolygons.Contains(selectedPolygon))
{
Expand All @@ -595,6 +690,11 @@ void OnMouseUp (SceneView sceneView, Event e)
lastSelectedPolygon = selectedPolygon;
matchedBrushes.Add(selectedPolygon, csgModel.FindBrushFromPolygon(selectedPolygon));
}
}
// If holding shift, quick select is used and we don't change anything
else if (EnumHelper.IsFlagSet(e.modifiers, EventModifiers.Shift))
{

}
else // No modifier pressed, add the polygon to selection
{
Expand Down Expand Up @@ -846,13 +946,13 @@ void OnRepaint (SceneView sceneView, Event e)
if(polygon != null)
{
allPolygons = csgModel.BuiltPolygonsByIndex(polygon.UniqueIndex);
SabreGraphics.DrawPolygons(new Color(0,1,0,0.15f), new Color(0,1,0,0.5f), allPolygons);
}
SabreGraphics.DrawPolygons(new Color(0,1,0,0.15f), new Color(0,1,0,0.5f), allPolygons);
}
}


SabreCSGResources.GetSelectedBrushDashedMaterial().SetPass(0);
// Draw each of the selcted polygons
// Draw each of the selected polygons
for (int i = 0; i < selectedSourcePolygons.Count; i++)
{
if(selectedSourcePolygons[i] != null)
Expand Down Expand Up @@ -915,6 +1015,49 @@ void OnRepaint (SceneView sceneView, Event e)
GL.End();
GL.PopMatrix();
}

// Deselect surfaces that are not hidden during "find hidden surfaces"
if (findingHiddenFaces)
{
List<Polygon> toDeselect = new List<Polygon>();
for (int polygonIndex = 0; polygonIndex < selectedSourcePolygons.Count; polygonIndex++)
{
Polygon polygon = selectedSourcePolygons[polygonIndex];
Brush brush = csgModel.FindBrushFromPolygon(polygon);

if (brush.Mode == CSGMode.Add)
{
// is the camera on the positive side of the plane?
if (Vector3.Dot(brush.transform.TransformDirection(polygon.Plane.normal), Camera.current.transform.position - brush.transform.TransformPoint(polygon.GetCenterPoint())) > 0)
{
// deselect polygon.
toDeselect.Add(polygon);
}
}
else
{
// is the camera on the positive side of the plane?
if (Vector3.Dot(brush.transform.TransformDirection(polygon.Plane.normal), Camera.current.transform.position - brush.transform.TransformPoint(polygon.GetCenterPoint())) < 0)
{
// deselect polygon.
toDeselect.Add(polygon);
}
}
}

foreach (Polygon polygon in toDeselect)
{
selectedSourcePolygons.Remove(polygon);
}

// Recalculate the matched brushes
matchedBrushes.Clear();

for (int i = 0; i < selectedSourcePolygons.Count; i++)
{
matchedBrushes.Add(selectedSourcePolygons[i], csgModel.FindBrushFromPolygon(selectedSourcePolygons[i]));
}
}
}

void OnDragPerform (SceneView sceneView, Event e)
Expand Down Expand Up @@ -1695,7 +1838,27 @@ void OnToolbarGUI(int windowID)

GUILayout.EndHorizontal();

GUILayout.Label("Adjacent", SabreGUILayout.GetTitleStyle());
GUILayout.BeginHorizontal(GUILayout.Width(180));

if (!findingHiddenFaces)
{
if (GUILayout.Button("Start Finding Hidden Faces", EditorStyles.miniButton))
{
findingHiddenFaces = true;
SelectAll();
}
}
else
{
if (GUILayout.Button("Stop Finding Hidden Faces", EditorStyles.miniButton))
{
findingHiddenFaces = false;
}
}

GUILayout.EndHorizontal();

GUILayout.Label("Adjacent", SabreGUILayout.GetTitleStyle());

GUILayout.BeginHorizontal(GUILayout.Width(180));

Expand Down Expand Up @@ -1741,8 +1904,8 @@ void OnToolbarGUI(int windowID)

public override void ResetTool()
{
}
findingHiddenFaces = false;
}

public override void OnSelectionChanged ()
{
Expand Down

0 comments on commit 59df1fa

Please sign in to comment.