using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; public class CommandHandler : MonoBehaviour { private static CommandHandler instance; private CommandManager commandManager; private Environment environment; public static CommandHandler getInstance() { if (instance == null) { instance = FindObjectOfType(); if (instance == null) { GameObject commandHandlerObject = new GameObject("CommandHandler"); instance = commandHandlerObject.AddComponent(); } } return instance; } private void Awake() { if (instance == null) { instance = this; } else { Destroy(gameObject); } } private void Start() { commandManager = CommandManager.getInstance(); environment = Environment.getInstance(); } private void Update() { // Detect Ctrl+Z for undo and Ctrl+Y for redo bool ctrl = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl); if (ctrl) { if (Input.GetKeyDown(KeyCode.Z)) { Debug.Log("Ctrl+Z detected: Undo action"); undoAction(); } else if (Input.GetKeyDown(KeyCode.Y)) { Debug.Log("Ctrl+Y detected: Redo action"); redoAction(); } } } public void createBlock(string prefabId, Vector3 position, Quaternion rotation) { ICommand createCommand = new CreateBlockCommand(prefabId, position, rotation); commandManager.executeCommand(createCommand); } public void moveBlock(string blockId, Vector3 prePosition, Vector3 newPosition, Quaternion preRotation, Quaternion newRotation) { ICommand moveCommand = new MoveBlockCommand(blockId, prePosition, newPosition, preRotation, newRotation); commandManager.executeCommand(moveCommand); } public void removeBlock(string blockId) { ICommand deleteCommand = new DeleteBlockCommand(blockId); commandManager.executeCommand(deleteCommand); } public void removeGroup(List blockIds) { ICommand deleteCommand = new DeleteGroupCommand(blockIds); commandManager.executeCommand(deleteCommand); } public void rotateBlock(string blockId, Quaternion newRotation) { ICommand rotateCommand = new RotateblockCommand(blockId, newRotation); commandManager.executeCommand(rotateCommand); } public void undoAction() { commandManager.undo(); } public void redoAction() { commandManager.redo(); } public void clearHistory() { commandManager.clearHistory(); } }