BuildingUIController.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. public class BuildingUIController : MonoBehaviour
  4. {
  5. [Header("UI References")]
  6. public UIDocument uiDocument;
  7. [Header("Building System")]
  8. public NewRoomBuilder roomBuilder;
  9. private VisualElement root;
  10. private Button wallModeButton;
  11. private Button doorModeButton;
  12. private Button entranceModeButton;
  13. private Button normalModeButton;
  14. private Label statusLabel;
  15. private Label instructionLabel;
  16. public enum BuildingMode
  17. {
  18. Normal,
  19. CreatingWalls,
  20. CreatingDoors,
  21. CreatingEntrance
  22. }
  23. private BuildingMode currentMode = BuildingMode.Normal;
  24. private void Start()
  25. {
  26. InitializeUI();
  27. // Check for existing entrances periodically
  28. InvokeRepeating(nameof(UpdateEntranceButtonVisibility), 1f, 1f);
  29. }
  30. private void InitializeUI()
  31. {
  32. if (uiDocument == null)
  33. {
  34. uiDocument = GetComponent<UIDocument>();
  35. }
  36. // Load the UXML file
  37. VisualTreeAsset uiAsset = Resources.Load<VisualTreeAsset>("UI/BuildingInterface");
  38. if (uiAsset == null)
  39. {
  40. // Try loading from Assets/UI folder
  41. uiAsset = UnityEngine.Resources.Load<VisualTreeAsset>("BuildingInterface");
  42. }
  43. if (uiAsset != null)
  44. {
  45. uiDocument.visualTreeAsset = uiAsset;
  46. }
  47. else
  48. {
  49. Debug.LogWarning("BuildingInterface.uxml not found. Creating UI programmatically.");
  50. CreateUIManually();
  51. return;
  52. }
  53. root = uiDocument.rootVisualElement;
  54. // Get button references
  55. wallModeButton = root.Q<Button>("WallModeButton");
  56. doorModeButton = root.Q<Button>("DoorModeButton");
  57. entranceModeButton = root.Q<Button>("EntranceModeButton");
  58. normalModeButton = root.Q<Button>("NormalModeButton");
  59. // Get label references
  60. statusLabel = root.Q<Label>("StatusLabel");
  61. instructionLabel = root.Q<Label>("InstructionLabel");
  62. // Register button callbacks
  63. if (wallModeButton != null) wallModeButton.clicked += OnWallModeClicked;
  64. if (doorModeButton != null) doorModeButton.clicked += OnDoorModeClicked;
  65. if (entranceModeButton != null) entranceModeButton.clicked += OnEntranceModeClicked;
  66. if (normalModeButton != null) normalModeButton.clicked += OnNormalModeClicked;
  67. // Initialize UI state
  68. UpdateUI();
  69. }
  70. private void CreateUIManually()
  71. {
  72. // Create UI elements manually if UXML file is not found
  73. root = uiDocument.rootVisualElement;
  74. // Create main panel
  75. VisualElement panel = new VisualElement();
  76. panel.name = "BuildingPanel";
  77. panel.style.position = Position.Absolute;
  78. panel.style.top = 20;
  79. panel.style.left = 20;
  80. panel.style.minWidth = 200;
  81. panel.style.backgroundColor = new Color(0, 0, 0, 0.8f);
  82. panel.style.paddingTop = panel.style.paddingLeft = panel.style.paddingRight = panel.style.paddingBottom = 15;
  83. // Create title
  84. Label title = new Label("Building Tools");
  85. title.name = "Title";
  86. title.style.fontSize = 18;
  87. title.style.color = Color.white;
  88. title.style.unityFontStyleAndWeight = FontStyle.Bold;
  89. title.style.marginBottom = 10;
  90. panel.Add(title);
  91. // Create buttons
  92. wallModeButton = new Button(() => OnWallModeClicked()) { text = "Create Walls" };
  93. doorModeButton = new Button(() => OnDoorModeClicked()) { text = "Create Doors" };
  94. entranceModeButton = new Button(() => OnEntranceModeClicked()) { text = "Create Entrance" };
  95. normalModeButton = new Button(() => OnNormalModeClicked()) { text = "Normal Mode" };
  96. wallModeButton.style.marginBottom = doorModeButton.style.marginBottom = entranceModeButton.style.marginBottom = normalModeButton.style.marginBottom = 8;
  97. wallModeButton.style.minHeight = doorModeButton.style.minHeight = entranceModeButton.style.minHeight = normalModeButton.style.minHeight = 35;
  98. panel.Add(wallModeButton);
  99. panel.Add(doorModeButton);
  100. panel.Add(entranceModeButton);
  101. panel.Add(normalModeButton);
  102. // Create status labels
  103. statusLabel = new Label("Mode: Normal");
  104. instructionLabel = new Label("Click a button to start building");
  105. statusLabel.style.fontSize = 14;
  106. statusLabel.style.color = new Color(0.8f, 1f, 0.8f);
  107. statusLabel.style.unityFontStyleAndWeight = FontStyle.Bold;
  108. statusLabel.style.marginTop = 10;
  109. statusLabel.style.marginBottom = 5;
  110. instructionLabel.style.fontSize = 12;
  111. instructionLabel.style.color = new Color(0.8f, 0.8f, 0.8f);
  112. instructionLabel.style.whiteSpace = WhiteSpace.Normal;
  113. panel.Add(statusLabel);
  114. panel.Add(instructionLabel);
  115. root.Add(panel);
  116. UpdateUI();
  117. }
  118. private void OnWallModeClicked()
  119. {
  120. SetBuildingMode(BuildingMode.CreatingWalls);
  121. }
  122. private void OnDoorModeClicked()
  123. {
  124. SetBuildingMode(BuildingMode.CreatingDoors);
  125. }
  126. private void OnEntranceModeClicked()
  127. {
  128. SetBuildingMode(BuildingMode.CreatingEntrance);
  129. }
  130. private void OnNormalModeClicked()
  131. {
  132. SetBuildingMode(BuildingMode.Normal);
  133. }
  134. public void SetBuildingMode(BuildingMode mode)
  135. {
  136. currentMode = mode;
  137. // Notify the room builder of the mode change
  138. if (roomBuilder != null)
  139. {
  140. roomBuilder.SetBuildingMode(mode);
  141. }
  142. UpdateUI();
  143. }
  144. private void UpdateUI()
  145. {
  146. // Update entrance button visibility first
  147. UpdateEntranceButtonVisibility();
  148. // Remove active class from all buttons
  149. wallModeButton.RemoveFromClassList("active");
  150. doorModeButton.RemoveFromClassList("active");
  151. if (entranceModeButton != null) entranceModeButton.RemoveFromClassList("active");
  152. normalModeButton.RemoveFromClassList("active");
  153. // Update button states and labels based on current mode
  154. switch (currentMode)
  155. {
  156. case BuildingMode.Normal:
  157. normalModeButton.AddToClassList("active");
  158. statusLabel.text = "Mode: Normal";
  159. instructionLabel.text = "Click a button to start building";
  160. break;
  161. case BuildingMode.CreatingWalls:
  162. wallModeButton.AddToClassList("active");
  163. statusLabel.text = "Mode: Creating Walls";
  164. instructionLabel.text = "Click and drag to create walls. Walls snap to 45° angles.";
  165. break;
  166. case BuildingMode.CreatingDoors:
  167. doorModeButton.AddToClassList("active");
  168. statusLabel.text = "Mode: Creating Doors";
  169. instructionLabel.text = "Click on a wall to add a door opening.";
  170. break;
  171. case BuildingMode.CreatingEntrance:
  172. if (entranceModeButton != null) entranceModeButton.AddToClassList("active");
  173. statusLabel.text = "Mode: Creating Entrance";
  174. instructionLabel.text = "Click on an outer wall to create the main building entrance.";
  175. break;
  176. }
  177. }
  178. public BuildingMode GetCurrentMode()
  179. {
  180. return currentMode;
  181. }
  182. private void UpdateEntranceButtonVisibility()
  183. {
  184. if (entranceModeButton == null) return;
  185. // Check if an entrance exists
  186. GameObject[] existingEntrances = GameObject.FindGameObjectsWithTag("Entrance");
  187. bool hasEntrance = existingEntrances != null && existingEntrances.Length > 0;
  188. // Show/hide the entrance button based on whether an entrance exists
  189. entranceModeButton.style.display = hasEntrance ? DisplayStyle.None : DisplayStyle.Flex;
  190. // If we're in entrance creation mode and an entrance was just created, switch to normal mode
  191. if (hasEntrance && currentMode == BuildingMode.CreatingEntrance)
  192. {
  193. SetBuildingMode(BuildingMode.Normal);
  194. }
  195. }
  196. private void OnDestroy()
  197. {
  198. // Clean up the repeating invocation
  199. CancelInvoke(nameof(UpdateEntranceButtonVisibility));
  200. }
  201. }