Skip to content

Commit

Permalink
Improved selection of overlapping parts
Browse files Browse the repository at this point in the history
  • Loading branch information
keiwando committed Sep 20, 2019
1 parent 6a8a9b2 commit 7e80be2
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 204 deletions.
2 changes: 1 addition & 1 deletion Assets/Scenes/EditorScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -23359,7 +23359,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
zoomAnchor: {x: 0.5, y: 0.5}
zoomInLength: 4
zoomInLength: 7
zoomOutLength: 10
bottomMovementPadding: 10
topMovementPadding: 10
Expand Down
51 changes: 0 additions & 51 deletions Assets/Scripts/Controllers/CreatureBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,30 +418,6 @@ public void Delete(List<BodyComponent> selection) {
RemoveDeletedObjects();
}

/// <summary>
/// Deletes the placed body components that are currently being
/// hovered over.
/// </summary>
/// <returns>Returns whether the creature design was modified.</returns>
public bool DeleteHoveringBodyComponent() {

BodyComponent joint = HoveringUtil.GetHoveringObject<Joint>(joints);
BodyComponent bone = HoveringUtil.GetHoveringObject<Bone>(bones);
BodyComponent muscle = HoveringUtil.GetHoveringObject<Muscle>(muscles);

BodyComponent toDelete = joint != null ? joint : ( bone != null ? bone : muscle ) ;

if (toDelete != null) {
toDelete.Delete();
RemoveDeletedObjects();
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
// The creature was modified
return true;
} else {
return false;
}
}

public void SetBodyComponents(List<Joint> joints, List<Bone> bones, List<Muscle> muscles) {
this.joints = joints;
this.bones = bones;
Expand All @@ -453,8 +429,6 @@ public void SetBodyComponents(List<Joint> joints, List<Bone> bones, List<Muscle>
/// </summary>
public Creature Build() {

ResetHoverableColliders();

GameObject creatureObj = new GameObject();
creatureObj.name = "Creature";
Creature creature = creatureObj.AddComponent<Creature>();
Expand Down Expand Up @@ -500,31 +474,6 @@ public void RefreshMuscleColliders() {
}
}

/// <summary>
/// Enabled / Disables highlighting on hover for the specified body components
/// </summary>
public void EnableHighlighting(bool joint, bool bone, bool muscle) {

HoveringUtil.SetShouldHighlight(this.joints, joint);
HoveringUtil.SetShouldHighlight(this.bones, bone);
HoveringUtil.SetShouldHighlight(this.muscles, muscle);
}

/// <summary>
/// Enlarges the hoverable colliders on joints and bones.
/// </summary>
public void ResetHoverableColliders() {

HoveringUtil.ResetHoverableColliders(joints);
HoveringUtil.ResetHoverableColliders(bones);
}

public void EnlargeHoverableColliders() {

HoveringUtil.EnlargeHoverableColliders(joints);
HoveringUtil.EnlargeHoverableColliders(bones);
}

#endregion

#region Utils
Expand Down
17 changes: 6 additions & 11 deletions Assets/Scripts/Controllers/CreatureEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ void Start() {
} else {
creatureBuilder = new CreatureBuilder();
}
#if UNITY_IOS || UNITY_ANDROID
creatureBuilder.EnlargeHoverableColliders();
#endif

selectionManager = new EditorSelectionManager(this, selectionArea, mouseDeleteTexture);

Expand All @@ -87,6 +84,9 @@ void Start() {
if (InputRegistry.shared.MayHandle(InputType.AndroidBack, this))
Application.Quit();
};

// Physics.autoSimulation = true;
Physics.autoSyncTransforms = true;
}

void Update() {
Expand All @@ -112,10 +112,6 @@ public void LoadDesign(CreatureDesign design) {
creatureBuilder.Reset();
creatureBuilder = new CreatureBuilder(design);
viewController.Refresh();

#if UNITY_IOS || UNITY_ANDROID
creatureBuilder.EnlargeHoverableColliders();
#endif
}

/// <summary>
Expand Down Expand Up @@ -185,6 +181,9 @@ public void StartSimulation(SimulationData simulationData) {
DontDestroyOnLoad(containerObject);

InputRegistry.shared.Deregister(this);

Physics.autoSimulation = false;
Physics.autoSyncTransforms = false;

// Load simulation scene
SceneController.LoadSync(SceneController.Scene.SimulationContainer);
Expand Down Expand Up @@ -368,10 +367,6 @@ private void HandleClicks() {
if (selectedTool == Tool.Move) {
creatureBuilder.RefreshMuscleColliders();
}
#if UNITY_IOS || UNITY_ANDROID
creatureBuilder.EnlargeHoverableColliders();
#endif
Physics.Simulate(Time.fixedDeltaTime);
}

viewController.Refresh();
Expand Down
64 changes: 59 additions & 5 deletions Assets/Scripts/Controllers/EditorSelectionManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// #define TEST
using System;
using System.Collections.Generic;
using UnityEngine;
Expand All @@ -8,6 +9,8 @@ public class EditorSelectionManager {

private static RaycastHit[] _cachedPointCollisions = new RaycastHit[10];
private static int _cachedPointCollisionsLength = 0;
private static Collider[] _cachedSphereCollisions = new Collider[10];
private static int _cachedSphereCollisionsLength = 0;

/// <summary>
/// The treshold distance used to distinguish between a click/touch and a drag
Expand Down Expand Up @@ -63,6 +66,11 @@ private void UpdateHoveringSelection(Vector3 mouseScreenPos) {
hovering = GetComponentAtScreenPoint<Joint>(mouseScreenPos);
if (hovering == null)
hovering = CheckCachedCollisionsFor<Bone>();
#if TEST || UNITY_IOS || UNITY_ANDROID
if (hovering == null)
hovering = CheckCachedSphereCollisionsFor<Bone>();
#endif

break;

case CreatureEditor.Tool.Delete:
Expand All @@ -72,6 +80,12 @@ private void UpdateHoveringSelection(Vector3 mouseScreenPos) {
hovering = CheckCachedCollisionsFor<Bone>();
if (hovering == null)
hovering = CheckCachedCollisionsFor<Muscle>();
#if TEST || UNITY_IOS || UNITY_ANDROID
if (hovering == null)
hovering = CheckCachedSphereCollisionsFor<Bone>();
if (hovering == null)
hovering = CheckCachedSphereCollisionsFor<Muscle>();
#endif
break;

default: break;
Expand Down Expand Up @@ -153,6 +167,8 @@ public void UpdateSelection(Vector3 currentEndPosition) {
}

public void EndSelection() {

if (!selectionArea.gameObject.activeSelf) return;

selectionArea.gameObject.SetActive(false);

Expand Down Expand Up @@ -241,12 +257,22 @@ private bool SelectInArea<T>(Rect selectionArea, List<BodyComponent> selection)
where T: BodyComponent {

GetComponentsInRect<T>(selectionArea, selection);

// Only select a single component with a click
if (selectionArea.size.magnitude < TAP_THRESHOLD * 2f && selection.Count > 1) {
var item = selection[0];
selection.Clear();
selection.Add(item);
}

foreach (var item in selection) {
item.EnableHighlight();
}

DisableHighlightsOnNonSelected();

if (selectionArea.size.magnitude >= TAP_THRESHOLD * 2f) {
DisableHighlightsOnNonSelected();
}

return this.selection.Count > 0;
}

Expand Down Expand Up @@ -277,11 +303,29 @@ private static void GetComponentsInRect<T>(Rect rect, List<BodyComponent> result
private static T GetComponentAtScreenPoint<T>(Vector3 point)
where T: BodyComponent {

Ray ray = Camera.main.ScreenPointToRay(point);
Camera camera = Camera.main;
Ray ray = camera.ScreenPointToRay(point);
var worldPoint = camera.ScreenToWorldPoint(point);
worldPoint.z = 0;
var hitCount = Physics.RaycastNonAlloc(ray, _cachedPointCollisions);
_cachedPointCollisionsLength = Math.Min(hitCount, 10);

_cachedPointCollisionsLength = Math.Min(hitCount, 10);

#if TEST || UNITY_IOS || UNITY_ANDROID
T item = CheckCachedCollisionsFor<T>();
if (item == null) {
// Check again but with a larger radius
var originInWorld = camera.ScreenToWorldPoint(Vector3.zero);
var radiusInWorld = camera.ScreenToWorldPoint(new Vector3(44, 0));
var radius = Vector3.Distance(originInWorld, radiusInWorld);

_cachedSphereCollisionsLength = Physics.OverlapSphereNonAlloc(worldPoint, radius, _cachedSphereCollisions);
_cachedPointCollisionsLength = Math.Min(_cachedPointCollisionsLength, 10);
return CheckCachedSphereCollisionsFor<T>();
}
return item;
#else
return CheckCachedCollisionsFor<T>();
#endif
}

private static T CheckCachedCollisionsFor<T>() where T: BodyComponent {
Expand All @@ -293,5 +337,15 @@ private static T CheckCachedCollisionsFor<T>() where T: BodyComponent {
}
return null;
}

private static T CheckCachedSphereCollisionsFor<T>() where T: BodyComponent {
for (int i = 0; i < _cachedSphereCollisionsLength; i++) {
if (_cachedSphereCollisions[i] == null) return null;
T component = _cachedSphereCollisions[i].GetComponent<T>();
if (component == null) continue;
return component;
}
return null;
}
}
}
8 changes: 2 additions & 6 deletions Assets/Scripts/Creature/Body/BodyComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

abstract public class BodyComponent: Hoverable {

public bool deleted;
private bool deleted;

public override void Start () {
base.Start();
Expand All @@ -15,11 +15,7 @@ virtual public void Delete(){
/// <summary>
/// Prepares the component for the evolution simulation.
/// </summary>
virtual public void PrepareForEvolution() {
#if UNITY_IOS || UNITY_ANDROID
ResetHitbox();
#endif
}
abstract public void PrepareForEvolution();

/// <summary>
/// Removes the already destroyed object that are still left in the list.
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Creature/Body/Bone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ public void Disconnect(Muscle muscle) {
}

public override void PrepareForEvolution () {
base.PrepareForEvolution();

body = GetComponent<Rigidbody>();
body.isKinematic = false;
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Creature/Body/Joint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public override void Delete() {
}

public override void PrepareForEvolution() {
base.PrepareForEvolution();

body = GetComponent<Rigidbody>();
body.isKinematic = false;
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Creature/Body/Muscle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ public void UpdateLinePoints(){
}

public override void PrepareForEvolution () {
base.PrepareForEvolution();
living = true;
}

Expand Down
52 changes: 0 additions & 52 deletions Assets/Scripts/Util/HoveringUtil.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/Scripts/Util/HoveringUtil.cs.meta

This file was deleted.

4 changes: 4 additions & 0 deletions Assets/Scripts/View/FileSelectionViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public void Show(FileSelectionViewControllerDelegate Delegate) {

Refresh();
ScrollSelectedItemToTop();

#if !UNITY_IOS && !UNITY_ANDROID
searchInputField.Select();
#endif
}

public void Refresh() {
Expand Down
Loading

0 comments on commit 7e80be2

Please sign in to comment.