Initial commit
This commit is contained in:
+237
@@ -0,0 +1,237 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class GhostStrategyTester : MonoBehaviour
|
||||
{
|
||||
[Header("Assign the scene object to edit")]
|
||||
public GameObject TestTarget;
|
||||
|
||||
[Header("Move settings")]
|
||||
public float ClampMin = -5f;
|
||||
public float ClampMax = 5f;
|
||||
|
||||
[Header("Rotation settings")]
|
||||
public float RotateSensitivity = 0.4f;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private enum Mode { None, AxisMove, AxisRotate }
|
||||
private Mode _mode = Mode.None;
|
||||
|
||||
private AxisMoveStrategy _moveStrat;
|
||||
private AxisRotateStrategy _rotStrat;
|
||||
|
||||
private bool _isEditing;
|
||||
private bool _buttonHeld;
|
||||
private int _activeAxisIndex = -1;
|
||||
private float _lastMouseX;
|
||||
private Vector3 _editingOrigin;
|
||||
|
||||
private static readonly Color[] AxisColors =
|
||||
{
|
||||
new Color(0.9f, 0.25f, 0.25f),
|
||||
new Color(0.25f, 0.85f, 0.25f),
|
||||
new Color(0.25f, 0.45f, 0.95f),
|
||||
};
|
||||
private static readonly string[] AxisLabels = { "X", "Y", "Z" };
|
||||
|
||||
private static readonly Vector3[] LocalMoveAxes =
|
||||
{
|
||||
Vector3.right,
|
||||
Vector3.up,
|
||||
Vector3.forward,
|
||||
};
|
||||
|
||||
private static readonly AxisRotateStrategy.LocalAxis[] RotateAxes =
|
||||
{
|
||||
AxisRotateStrategy.LocalAxis.Right,
|
||||
AxisRotateStrategy.LocalAxis.Up,
|
||||
AxisRotateStrategy.LocalAxis.Forward,
|
||||
};
|
||||
|
||||
private const float BtnW = 96f;
|
||||
private const float BtnH = 36f;
|
||||
private const float PadX = 12f;
|
||||
private const float PadY = 8f;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private void Start()
|
||||
{
|
||||
GhostManagerV2.getInstance().onClearAddListener(() =>
|
||||
{
|
||||
_isEditing = false;
|
||||
_buttonHeld = false;
|
||||
_activeAxisIndex = -1;
|
||||
_moveStrat = null;
|
||||
_rotStrat = null;
|
||||
});
|
||||
}
|
||||
|
||||
// private void Update()
|
||||
// {
|
||||
// if (!_buttonHeld || _activeAxisIndex < 0) return;
|
||||
|
||||
// if (_mode == Mode.AxisRotate && _rotStrat != null)
|
||||
// {
|
||||
// float delta = Input.mousePosition.x - _lastMouseX;
|
||||
// if (Mathf.Abs(delta) > 0.01f)
|
||||
// _rotStrat.Rotate(delta * RotateSensitivity);
|
||||
// }
|
||||
|
||||
// _lastMouseX = Input.mousePosition.x;
|
||||
// }
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// GUI
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
if (TestTarget == null)
|
||||
{
|
||||
GUI.Label(new Rect(PadX, PadX, 300, 24), "Assign TestTarget in the Inspector.");
|
||||
return;
|
||||
}
|
||||
|
||||
float y = PadX;
|
||||
|
||||
// ── Mode buttons ───────────────────────────────────────────────
|
||||
DrawModeButton("Move", Mode.AxisMove, new Rect(PadX, y, BtnW, BtnH));
|
||||
DrawModeButton("Rotate", Mode.AxisRotate, new Rect(PadX + BtnW + PadY, y, BtnW, BtnH));
|
||||
y += BtnH + PadY;
|
||||
|
||||
if (_mode == Mode.None)
|
||||
{
|
||||
GUI.Label(new Rect(PadX, y, 300, 24), "Select a mode above.");
|
||||
return;
|
||||
}
|
||||
|
||||
// ── Axis buttons ───────────────────────────────────────────────
|
||||
string hint = _mode == Mode.AxisMove
|
||||
? "Hold to drag along axis:"
|
||||
: "Hold and drag left/right to rotate:";
|
||||
GUI.Label(new Rect(PadX, y, 350, 20), hint);
|
||||
y += 22f;
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
Rect btnRect = new Rect(PadX + i * (BtnW + PadY), y, BtnW, BtnH);
|
||||
DrawAxisButton(i, btnRect);
|
||||
}
|
||||
|
||||
y += BtnH + PadY * 2;
|
||||
|
||||
// ── Commit / Cancel ────────────────────────────────────────────
|
||||
if (GUI.Button(new Rect(PadX, y, BtnW, BtnH), "Commit"))
|
||||
CommitEdit();
|
||||
|
||||
if (GUI.Button(new Rect(PadX + BtnW + PadY, y, BtnW, BtnH), "Cancel"))
|
||||
StopEditing();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private void DrawAxisButton(int i, Rect rect)
|
||||
{
|
||||
bool held = _buttonHeld && _activeAxisIndex == i;
|
||||
|
||||
GUI.backgroundColor = held ? AxisColors[i] : AxisColors[i] * 0.55f;
|
||||
GUI.contentColor = Color.white;
|
||||
|
||||
bool pressed = GUI.RepeatButton(rect, $"{AxisLabels[i]} axis");
|
||||
|
||||
if (pressed && (!_buttonHeld || _activeAxisIndex != i))
|
||||
OnAxisButtonDown(i);
|
||||
|
||||
if (!pressed && _buttonHeld && _activeAxisIndex == i)
|
||||
OnAxisButtonUp();
|
||||
|
||||
GUI.backgroundColor = Color.white;
|
||||
GUI.contentColor = Color.white;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private void OnAxisButtonDown(int axisIndex)
|
||||
{
|
||||
EnsureEditing();
|
||||
|
||||
_activeAxisIndex = axisIndex;
|
||||
_buttonHeld = true;
|
||||
_lastMouseX = Input.mousePosition.x;
|
||||
|
||||
if (_mode == Mode.AxisMove)
|
||||
{
|
||||
Transform ghost = GhostManagerV2.getInstance().GhostTransform;
|
||||
Vector3 worldAxis = ghost.TransformDirection(LocalMoveAxes[axisIndex]);
|
||||
|
||||
_moveStrat = new AxisMoveStrategy(ghost.rotation, ClampMin, ClampMax);
|
||||
_moveStrat.BeginDrag(worldAxis, ghost.position);
|
||||
GhostManagerV2.getInstance().SetMovementStrategy(_moveStrat);
|
||||
}
|
||||
else
|
||||
{
|
||||
_rotStrat = new AxisRotateStrategy(
|
||||
RotateAxes[axisIndex],
|
||||
GhostManagerV2.getInstance().GhostTransform.position);
|
||||
GhostManagerV2.getInstance().SetMovementStrategy(_rotStrat);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAxisButtonUp()
|
||||
{
|
||||
_buttonHeld = false;
|
||||
_activeAxisIndex = -1;
|
||||
|
||||
if (_mode == Mode.AxisMove)
|
||||
_moveStrat?.EndDrag();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private void EnsureEditing()
|
||||
{
|
||||
if (_isEditing) return;
|
||||
_editingOrigin = TestTarget.transform.position;
|
||||
GhostManagerV2.getInstance().SetupExistingBlock(TestTarget, new FreeMoveStrategy());
|
||||
_isEditing = true;
|
||||
}
|
||||
|
||||
private void CommitEdit()
|
||||
{
|
||||
GhostManagerV2.getInstance().ForceCommit();
|
||||
ResetState();
|
||||
}
|
||||
|
||||
private void StopEditing()
|
||||
{
|
||||
GhostManagerV2.getInstance().ClearGhostData();
|
||||
ResetState();
|
||||
}
|
||||
|
||||
private void ResetState()
|
||||
{
|
||||
_mode = Mode.None;
|
||||
_activeAxisIndex = -1;
|
||||
_buttonHeld = false;
|
||||
_isEditing = false;
|
||||
_moveStrat = null;
|
||||
_rotStrat = null;
|
||||
}
|
||||
|
||||
private void DrawModeButton(string label, Mode target, Rect rect)
|
||||
{
|
||||
bool active = _mode == target;
|
||||
GUI.backgroundColor = active ? Color.white : new Color(0.55f, 0.55f, 0.55f);
|
||||
GUI.contentColor = active ? Color.black : Color.white;
|
||||
|
||||
if (GUI.Button(rect, label) && !active)
|
||||
{
|
||||
StopEditing();
|
||||
_mode = target;
|
||||
}
|
||||
|
||||
GUI.backgroundColor = Color.white;
|
||||
GUI.contentColor = Color.white;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user