| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- using UnityEngine;
- using UnityEngine.UIElements;
- public class BuildingUIController : MonoBehaviour
- {
- [Header("UI References")]
- public UIDocument uiDocument;
- [Header("Building System")]
- public NewRoomBuilder roomBuilder;
- private VisualElement root;
- private Button wallModeButton;
- private Button doorModeButton;
- private Button entranceModeButton;
- private Button normalModeButton;
- private Label statusLabel;
- private Label instructionLabel;
- public enum BuildingMode
- {
- Normal,
- CreatingWalls,
- CreatingDoors,
- CreatingEntrance
- }
- private BuildingMode currentMode = BuildingMode.Normal;
- private void Start()
- {
- InitializeUI();
- // Check for existing entrances periodically
- InvokeRepeating(nameof(UpdateEntranceButtonVisibility), 1f, 1f);
- }
- private void InitializeUI()
- {
- if (uiDocument == null)
- {
- uiDocument = GetComponent<UIDocument>();
- }
- // Load the UXML file
- VisualTreeAsset uiAsset = Resources.Load<VisualTreeAsset>("UI/BuildingInterface");
- if (uiAsset == null)
- {
- // Try loading from Assets/UI folder
- uiAsset = UnityEngine.Resources.Load<VisualTreeAsset>("BuildingInterface");
- }
- if (uiAsset != null)
- {
- uiDocument.visualTreeAsset = uiAsset;
- }
- else
- {
- Debug.LogWarning("BuildingInterface.uxml not found. Creating UI programmatically.");
- CreateUIManually();
- return;
- }
- root = uiDocument.rootVisualElement;
- // Get button references
- wallModeButton = root.Q<Button>("WallModeButton");
- doorModeButton = root.Q<Button>("DoorModeButton");
- entranceModeButton = root.Q<Button>("EntranceModeButton");
- normalModeButton = root.Q<Button>("NormalModeButton");
- // Get label references
- statusLabel = root.Q<Label>("StatusLabel");
- instructionLabel = root.Q<Label>("InstructionLabel");
- // Register button callbacks
- if (wallModeButton != null) wallModeButton.clicked += OnWallModeClicked;
- if (doorModeButton != null) doorModeButton.clicked += OnDoorModeClicked;
- if (entranceModeButton != null) entranceModeButton.clicked += OnEntranceModeClicked;
- if (normalModeButton != null) normalModeButton.clicked += OnNormalModeClicked;
- // Initialize UI state
- UpdateUI();
- }
- private void CreateUIManually()
- {
- // Create UI elements manually if UXML file is not found
- root = uiDocument.rootVisualElement;
- // Create main panel
- VisualElement panel = new VisualElement();
- panel.name = "BuildingPanel";
- panel.style.position = Position.Absolute;
- panel.style.top = 20;
- panel.style.left = 20;
- panel.style.minWidth = 200;
- panel.style.backgroundColor = new Color(0, 0, 0, 0.8f);
- panel.style.paddingTop = panel.style.paddingLeft = panel.style.paddingRight = panel.style.paddingBottom = 15;
- // Create title
- Label title = new Label("Building Tools");
- title.name = "Title";
- title.style.fontSize = 18;
- title.style.color = Color.white;
- title.style.unityFontStyleAndWeight = FontStyle.Bold;
- title.style.marginBottom = 10;
- panel.Add(title);
- // Create buttons
- wallModeButton = new Button(() => OnWallModeClicked()) { text = "Create Walls" };
- doorModeButton = new Button(() => OnDoorModeClicked()) { text = "Create Doors" };
- entranceModeButton = new Button(() => OnEntranceModeClicked()) { text = "Create Entrance" };
- normalModeButton = new Button(() => OnNormalModeClicked()) { text = "Normal Mode" };
- wallModeButton.style.marginBottom = doorModeButton.style.marginBottom = entranceModeButton.style.marginBottom = normalModeButton.style.marginBottom = 8;
- wallModeButton.style.minHeight = doorModeButton.style.minHeight = entranceModeButton.style.minHeight = normalModeButton.style.minHeight = 35;
- panel.Add(wallModeButton);
- panel.Add(doorModeButton);
- panel.Add(entranceModeButton);
- panel.Add(normalModeButton);
- // Create status labels
- statusLabel = new Label("Mode: Normal");
- instructionLabel = new Label("Click a button to start building");
- statusLabel.style.fontSize = 14;
- statusLabel.style.color = new Color(0.8f, 1f, 0.8f);
- statusLabel.style.unityFontStyleAndWeight = FontStyle.Bold;
- statusLabel.style.marginTop = 10;
- statusLabel.style.marginBottom = 5;
- instructionLabel.style.fontSize = 12;
- instructionLabel.style.color = new Color(0.8f, 0.8f, 0.8f);
- instructionLabel.style.whiteSpace = WhiteSpace.Normal;
- panel.Add(statusLabel);
- panel.Add(instructionLabel);
- root.Add(panel);
- UpdateUI();
- }
- private void OnWallModeClicked()
- {
- SetBuildingMode(BuildingMode.CreatingWalls);
- }
- private void OnDoorModeClicked()
- {
- SetBuildingMode(BuildingMode.CreatingDoors);
- }
- private void OnEntranceModeClicked()
- {
- SetBuildingMode(BuildingMode.CreatingEntrance);
- }
- private void OnNormalModeClicked()
- {
- SetBuildingMode(BuildingMode.Normal);
- }
- public void SetBuildingMode(BuildingMode mode)
- {
- currentMode = mode;
- // Notify the room builder of the mode change
- if (roomBuilder != null)
- {
- roomBuilder.SetBuildingMode(mode);
- }
- UpdateUI();
- }
- private void UpdateUI()
- {
- // Update entrance button visibility first
- UpdateEntranceButtonVisibility();
- // Remove active class from all buttons
- wallModeButton.RemoveFromClassList("active");
- doorModeButton.RemoveFromClassList("active");
- if (entranceModeButton != null) entranceModeButton.RemoveFromClassList("active");
- normalModeButton.RemoveFromClassList("active");
- // Update button states and labels based on current mode
- switch (currentMode)
- {
- case BuildingMode.Normal:
- normalModeButton.AddToClassList("active");
- statusLabel.text = "Mode: Normal";
- instructionLabel.text = "Click a button to start building";
- break;
- case BuildingMode.CreatingWalls:
- wallModeButton.AddToClassList("active");
- statusLabel.text = "Mode: Creating Walls";
- instructionLabel.text = "Click and drag to create walls. Walls snap to 45° angles.";
- break;
- case BuildingMode.CreatingDoors:
- doorModeButton.AddToClassList("active");
- statusLabel.text = "Mode: Creating Doors";
- instructionLabel.text = "Click on a wall to add a door opening.";
- break;
- case BuildingMode.CreatingEntrance:
- if (entranceModeButton != null) entranceModeButton.AddToClassList("active");
- statusLabel.text = "Mode: Creating Entrance";
- instructionLabel.text = "Click on an outer wall to create the main building entrance.";
- break;
- }
- }
- public BuildingMode GetCurrentMode()
- {
- return currentMode;
- }
- private void UpdateEntranceButtonVisibility()
- {
- if (entranceModeButton == null) return;
- // Check if an entrance exists
- GameObject[] existingEntrances = GameObject.FindGameObjectsWithTag("Entrance");
- bool hasEntrance = existingEntrances != null && existingEntrances.Length > 0;
- // Show/hide the entrance button based on whether an entrance exists
- entranceModeButton.style.display = hasEntrance ? DisplayStyle.None : DisplayStyle.Flex;
- // If we're in entrance creation mode and an entrance was just created, switch to normal mode
- if (hasEntrance && currentMode == BuildingMode.CreatingEntrance)
- {
- SetBuildingMode(BuildingMode.Normal);
- }
- }
- private void OnDestroy()
- {
- // Clean up the repeating invocation
- CancelInvoke(nameof(UpdateEntranceButtonVisibility));
- }
- }
|