rimworld-animation-studio/Assets/Scripts/GUI/ActorBodyPart.cs

106 lines
3.7 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
namespace RimWorldAnimationStudio
{
public class ActorBodyPart : MonoBehaviour, IPointerClickHandler, IDragHandler, IEndDragHandler
{
public SpriteRenderer bodyPartRenderer;
public ActorBody parent;
public bool isHead = false;
public bool isSelected = false;
2022-09-26 04:10:41 +00:00
private Vector3 delta = new Vector3();
public void Update()
{
if ((Workspace.actorID == parent.actorID && Workspace.selectedBodyPart == null) || Workspace.selectedBodyPart == this)
{ bodyPartRenderer.color = Constants.ColorGreen; }
else
{ bodyPartRenderer.color = Constants.ColorWhite; }
}
public void OnPointerClick(PointerEventData eventData)
{
if (eventData.pointerCurrentRaycast.gameObject.GetComponent<ActorBodyPart>() == null)
{ return; }
Activate();
}
public void OnDrag(PointerEventData eventData)
{
Activate();
PawnKeyframe keyframe = Workspace.Instance.GetCurrentPawnKeyframe(true);
2022-09-18 06:52:54 +00:00
if (keyframe == null)
2022-09-18 06:52:54 +00:00
{ Debug.LogWarning("Cannot alter actor - no keyframe data available"); return; }
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
2022-09-26 04:10:41 +00:00
if (delta == Vector3.zero)
{ delta = mousePosition - transform.position; }
if (isHead)
{
if (Workspace.actorManipulationMode == ActorManipulationMode.Pan)
{
2022-09-24 19:12:18 +00:00
// It's stupid but it works
Vector3 localPosA = transform.localPosition;
2022-09-26 04:10:41 +00:00
transform.position = mousePosition - delta;
2022-09-24 19:12:18 +00:00
Vector3 localPosB = transform.localPosition;
transform.localPosition = localPosA;
2022-09-24 19:12:18 +00:00
keyframe.headBob += localPosB.y - localPosA.y;
}
else if (Workspace.actorManipulationMode == ActorManipulationMode.Rotate)
{
2022-09-24 07:17:40 +00:00
float angle = -Vector2.SignedAngle(Vector2.down, (Vector2)mousePosition - (Vector2)transform.position);
keyframe.headAngle = angle;
}
else if (Workspace.actorManipulationMode == ActorManipulationMode.Face)
{
float angle = Vector2.SignedAngle(Vector2.up, (Vector2)mousePosition - (Vector2)transform.position);
int facing = -Mathf.RoundToInt(angle / 90f);
facing = facing < 0 ? facing + 4 : facing;
keyframe.headFacing = facing;
}
}
else
{
if (Workspace.actorManipulationMode == ActorManipulationMode.Rotate)
{
2022-09-27 05:56:35 +00:00
float angle = -Vector2.SignedAngle(Vector2.up, (Vector2)mousePosition - (Vector2)transform.position);
keyframe.genitalAngle = angle;
2022-09-21 21:15:25 +00:00
Workspace.animationDef.actors[Workspace.actorID].controlGenitalAngle = Workspace.animationDef.animationStages.Any(x => x.animationClips[Workspace.actorID].keyframes.Any(y => y.genitalAngle != 0));
}
}
PawnAnimationClip clip = Workspace.Instance.GetPawnAnimationClip(parent.actorID);
clip.BuildSimpleCurves();
}
public void OnEndDrag(PointerEventData eventData)
{
Workspace.Instance.RecordEvent("Actor position / orientation");
2022-09-26 04:10:41 +00:00
delta = Vector3.zero;
}
public void Activate()
{
Workspace.actorID = parent.actorID;
Workspace.selectedBodyPart = this;
}
}
}