using System.Collections; using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; using UnityEngine.UI; using UnityEngine.UI.Extensions; public class WallSelection : MonoBehaviour { static private WallSelection _instance; [SerializeField] private GameObject _selectionPanel; [SerializeField] private GameObject _ListOptions; [SerializeField] private RectTransform _selectedElementContainer; private IWallSelection _selectedElement; private bool _isProcessingSelection; [SerializeField] private GameObject _Quote1; [SerializeField] private GameObject _Quote2; public static GameObject Quote1; public static GameObject Quote2; public static GameObject ListOptions; public Toggle _InvisibleBtn; static public bool IsDragging { get; set; } private void Awake() { if (_instance != null && _instance != this) { Destroy(gameObject); return; } else { _instance = this; } ListOptions =_ListOptions; Quote1= _Quote1; Quote2= _Quote2; } private void Update() { // TODO : use state machine? if (!_isProcessingSelection) { return; } //if(_G.SelectedPoint2D!=null) WallPoint.AddMeasure(_G.SelectedPoint2D); if (Input.GetKeyDown(KeyCode.Escape)) { _selectedElement.SetAsSelection(false); } else if (Input.GetKeyDown(KeyCode.Delete)) { _selectedElement.DestroySelected(); } if (IsSelectionNull(_selectedElement)) { _isProcessingSelection = false; Invoke(nameof(ExitSelection), 0.0f); } else { //IsDragging = _selectedElement.GetDragState(); } if (Input.GetKeyDown(KeyCode.Return)) { CreatePointFromKeyInput(); } } static private bool IsSelectionNull(IWallSelection selection) { return (Object)selection == null; } static public bool TryGetCurrentSelection(out IWallSelection selection) { bool hasSelection = !IsSelectionNull(_instance._selectedElement); selection = hasSelection ? _instance._selectedElement : null; return hasSelection; } static public void SetSelection(IWallSelection element, bool isEnabled) { // 0 + 0 = 0 // 0 - 0 = 0 // A + 0 = A // A - 0 = 0 if (IsSelectionNull(element)) { //Debug.Log("set selection is null"); return; } bool hasSelectedElement = !IsSelectionNull(_instance._selectedElement); bool isSameElement = _instance._selectedElement == element; // A + A = A if (isSameElement && isEnabled) { // do nothing } // 0 + A = +A // 0 - A = 0 // A - A = -0 else if (!hasSelectedElement || isSameElement) { _instance._selectedElement = isEnabled ? element : null; _instance.EnableSelectionPanel(isEnabled); List selectables = GetAllSelectableElements(); foreach (IWallSelection item in selectables) { if (CompareSelection(item)) { continue; } item.HandleSelection(false); } element.HandleSelection(isEnabled); //Debug.Log($"open selection or toggle : {element}"); } // A + B = B // A - B = A else if (isEnabled) { _instance._selectedElement.HandleSelection(false); _instance._selectedElement = element; element.HandleSelection(true); //Debug.Log($"change selection : {element}"); } //Debug.Log($"selected : {_instance._selectedElement} ; enabled : {isEnabled}"); } public void ExitSelection() { if (!IsSelectionNull(_selectedElement)) { _selectedElement.SetAsSelection(false); } else { List selectables = GetAllSelectableElements(); foreach (IWallSelection item in selectables) { item.HandleSelection(false); } } if (_selectionPanel.activeInHierarchy) { _instance.EnableSelectionPanel(false); } IsDragging = false; _isProcessingSelection = false; WallCreationManager.HideMeasureInteriorWall(); } public void DestroySelected() { if (!IsSelectionNull(_selectedElement)) { _selectedElement.DestroySelected(); ExitSelection(); } } private void EnableSelectionPanel(bool isEnabled) { if(!_G.isExteriorWall){ _isProcessingSelection = isEnabled; _selectionPanel.SetActive(isEnabled); //_ListOptions.SetActive(isEnabled); DOIT.AllChildOff(_ListOptions); } } public static void AddInputOption(Color ColorHeader,string Title, string Options, bool isInvisibe,float ValueHeight,float ValueHThickness){ print("Title=="+Title); //ListOptions.SetActive(true); Get.o2("Panel_DRAWPlan_NEW/Selection Panel","Panel_Setting").SetActive(true); DOIT.AllChildOff(ListOptions); ListOptions.transform.parent.transform.Find("Header").GetComponent().color = ColorHeader; ListOptions.transform.parent.transform.Find("Header/Title").GetComponent().text = Title; string[] OptionsArray = Options.Split("_"); foreach(string Option in OptionsArray){ ListOptions.transform.Find(Option).gameObject.SetActive(true); } if(OptionsArray.Contains("Invisible")){ ListOptions.transform.Find("Invisible/Show").gameObject.GetComponent().SetIsOnWithoutNotify(isInvisibe); } if(OptionsArray.Contains("InputHeight")){ ListOptions.transform.Find("InputHeight/InputField").gameObject.GetComponent().text=ValueHeight.ToString(); } if(OptionsArray.Contains("InputThickness")){ ListOptions.transform.Find("InputThickness/InputField").gameObject.GetComponent().text=ValueHThickness.ToString(); } if(OptionsArray.Contains("Offset")){ //ListOptions.transform.Find("InputThickness/InputField").gameObject.GetComponent().text=ValueHThickness.ToString(); } ListOptions.transform.parent.transform.GetComponent().sizeDelta =new(200,60+OptionsArray.Length*40); } public void SetInvisibility() { GameObject Wall = _selectedElement.GetGameObject(); float WallThickness = 10; if (Wall.GetComponent()._category == "Interior") { WallThickness = 6; } if (_InvisibleBtn.isOn) { WallThickness = 2; } UILineRenderer LR = Wall.GetComponent(); LR.LineThickness = WallThickness; LR.enabled = false; LR.enabled = true; Wall.GetComponent().Thickness = WallThickness; } static public bool CompareSelection(IWallSelection selection) { return selection == _instance._selectedElement; //return (Object)selection == (Object)_instance._selectedElement; } static public void SetAllToDefaultColor(IWallSelection ignoreSelection) { List selectableElements = GetAllSelectableElements(); foreach (IWallSelection element in selectableElements) { if (element == ignoreSelection) { continue; } element.SetSelectionColor(SelectionModePlan2D.Default); } } static private List GetAllSelectableElements() { List result = new(); foreach (RectTransform item in WallCreationManager.GetWallPointContainer()) { result.Add((IWallSelection)item.GetComponent()); } foreach (RectTransform item in WallCreationManager.GetWallLineContainer()) { result.Add((IWallSelection)item.GetComponent()); } return result; } static public RectTransform GetSelectedElementContainer() { return _instance._selectedElementContainer; } public void CreatePointFromKeyInput(){ //ExitSelection(); //print("_currentWallPoint.name==2="+this.name); } } public interface IWallSelection { public void SetAsSelection(bool isSelected); //public void AddInputOption(string option); public void HandleSelection(bool isSelected); public void DestroySelected(); public void SetSelectionColor(SelectionModePlan2D mode); public bool GetDragState(); public void InsertWall(); // TODO : temporary? public GameObject GetGameObject(); } public enum SelectionModePlan2D { Default, Hover, Select }