Skip to content

Commit

Permalink
Preview object scale in editor; close #375
Browse files Browse the repository at this point in the history
  • Loading branch information
vanjac committed Jun 6, 2024
1 parent 5a835b4 commit a163601
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
10 changes: 9 additions & 1 deletion Assets/Behaviors/Scale.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using UnityEngine;

[EditorPreviewBehavior(marker = true)]
public class ScaleBehavior : GenericEntityBehavior<ScaleBehavior, ScaleComponent>
{
public static new BehaviorType objectType = new BehaviorType(
Expand Down Expand Up @@ -35,7 +36,14 @@ public class ScaleComponent : BehaviorComponent<ScaleBehavior>
public override void BehaviorEnabled()
{
storedScale = transform.localScale;
transform.localScale = behavior.scale;
var scale = behavior.scale;
if (GetComponent<ObjectMarker>()) // editor preview
{
scale.x = Mathf.Max(scale.x, 0.1f);
scale.y = Mathf.Max(scale.y, 0.1f);
scale.z = Mathf.Max(scale.z, 0.1f);
}
transform.localScale = scale;
}

public override void BehaviorDisabled()
Expand Down
4 changes: 2 additions & 2 deletions Assets/Files/MessagePackWorldReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ private void ReadWorld(MessagePackObjectDictionary world, Transform cameraPivot,
}
ObjectEntity obj = (ObjectEntity)objType.Create();
ReadObjectEntity(objDict, obj, materials, overlays);
if (editor)
obj.InitObjectMarker((VoxelArrayEditor)voxelArray);
voxelArray.AddObject(obj);
}
}
Expand All @@ -177,8 +179,6 @@ private void ReadWorld(MessagePackObjectDictionary world, Transform cameraPivot,
{
foreach (Substance s in substances)
EntityPreviewManager.UpdateEntityPosition(s);
foreach (ObjectEntity obj in voxelArray.IterateObjects())
obj.InitObjectMarker((VoxelArrayEditor)voxelArray);
}
}

Expand Down
50 changes: 39 additions & 11 deletions Assets/VoxelEditor/EditorPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,60 @@

public class EditorPreviewBehaviorAttribute : System.Attribute
{
public bool marker;
public EditorPreviewBehaviorAttribute(bool marker = false)
{
this.marker = marker;
}
}

public static class EntityPreviewManager
{
private static Dictionary<Entity, List<GameObject>> entityPreviewObjects = new Dictionary<Entity, List<GameObject>>();
private static Dictionary<Entity, List<Behaviour>> markerPreviewComponents = new Dictionary<Entity, List<Behaviour>>();

public static bool IsEditorPreviewBehavior(System.Type type) =>
System.Attribute.GetCustomAttribute(type, typeof(EditorPreviewBehaviorAttribute)) != null;
public static EditorPreviewBehaviorAttribute GetEditorPreviewAttribute(System.Type type) =>
(EditorPreviewBehaviorAttribute)System.Attribute.GetCustomAttribute(type, typeof(EditorPreviewBehaviorAttribute));

public static void AddEntity(Entity entity)
{
if (entityPreviewObjects.ContainsKey(entity))
RemoveEntity(entity);
RemoveEntity(entity);

var objectEntity = entity as ObjectEntity;
var previewObjects = new List<GameObject>();
var previewComponents = new List<Behaviour>();
foreach (EntityBehavior behavior in entity.behaviors)
{
if (IsEditorPreviewBehavior(behavior.GetType()))
var attr = GetEditorPreviewAttribute(behavior.GetType());
if (attr != null && (!attr.marker || (objectEntity != null && objectEntity.marker != null)))
{
if (behavior.targetEntity.entity != null && behavior.targetEntity.entity != entity)
continue; // TODO: targeted behaviors not supported
if (behavior.condition != EntityBehavior.Condition.BOTH)
continue;
var previewObj = new GameObject();
previewObj.tag = "EditorPreview";
previewObj.transform.position = entity.PositionInEditor();
behavior.MakeComponent(previewObj);
previewObjects.Add(previewObj);
GameObject previewObj;
if (attr.marker)
{
previewObj = objectEntity.marker.gameObject;
}
else
{
previewObj = new GameObject();
previewObj.tag = "EditorPreview";
previewObj.transform.position = entity.PositionInEditor();
previewObjects.Add(previewObj);
}
var component = behavior.MakeComponent(previewObj);
if (attr.marker)
{
previewComponents.Add(component);
}
}
}
if (previewObjects.Count != 0)
entityPreviewObjects[entity] = previewObjects;
if (previewComponents.Count != 0)
markerPreviewComponents[entity] = previewComponents;
}

public static void RemoveEntity(Entity entity)
Expand All @@ -45,11 +67,17 @@ public static void RemoveEntity(Entity entity)
GameObject.Destroy(obj);
entityPreviewObjects.Remove(entity);
}
if (markerPreviewComponents.TryGetValue(entity, out var previewComponents))
{
foreach (var component in previewComponents)
Object.Destroy(component);
markerPreviewComponents.Remove(entity);
}
}

public static void BehaviorUpdated(IEnumerable<Entity> entities, System.Type behaviorType)
{
if (IsEditorPreviewBehavior(behaviorType))
if (GetEditorPreviewAttribute(behaviorType) != null)
{
foreach (var entity in entities)
AddEntity(entity);
Expand Down

0 comments on commit a163601

Please sign in to comment.