/// <summary> /// Widget constructor /// </summary> protected virtual void Construct() { // set some default value m_selectedItem = default(T); m_clickAnywhereToOpen = false; // get allocators and UI elements m_allocator = this.GetWidgetAllocator(); m_listAllocator = this.GetListWidgetAllocator(); m_listArea = this.GetListArea(); m_selectedItemArea = this.GetSelectedItemArea(); m_pushButton = this.GetPushButton(); // create the list and subscribe to the item selection event m_list = m_listAllocator.Construct(); m_list.OnSelectItem.AddListener(this.OnItemSelected); // reparent the list to the list area transform provided by the inheriting class RectTransform listTransform = (RectTransform)m_list.transform; listTransform.SetParent(m_listArea, false); listTransform.Stretch(); // list should be disabled by default m_listArea.gameObject.SetActive(false); // subscribe to the PushButton's click event this.EnablePushButtonListener(true); }
protected void CreateWidgetAt(int p_index) { WidgetType widget = m_allocator.Construct(); RectTransform widgetTransform = widget.transform as RectTransform; widgetTransform.SetParent(m_contentArea, false); this.UpdateWidgetPosition(p_index, widgetTransform); Button button = widget.GetComponent <Button>(); if (button != null) { button.onClick.AddListener(() => { this.OnWidgetClicked(m_items[p_index]); }); } m_widgets[p_index] = widget; widget.Enable(m_items[p_index]); }
/// <summary> /// Sets the selected item and manages the visibility of the widget /// </summary> /// <param name="p_item"></param> protected virtual void SetSelectedItem(T p_item) { m_selectedItem = p_item; // If the item is set to null, make sure the widget is destroyed. // // NOTE: not checking against default(T) here because this would // cause strange behaviour when the type T is a value type (like int). // When calling this method with the parameter zero, the widget would be // destroyed. if (m_selectedItem == null) { if (m_selectedWidget != null) { m_allocator.Destroy(m_selectedWidget); m_selectedWidget = null; } } else { // create a widget if there is none if (m_selectedWidget == null) { m_selectedWidget = m_allocator.Construct(); RectTransform widgetTransform = (RectTransform)m_selectedWidget.transform; widgetTransform.SetParent(m_selectedItemArea, false); widgetTransform.Stretch(); } // ... otherwise disable it. This allows the widget to clean up before passing a new value. else { m_selectedWidget.Disable(); } // assign the value to the widget m_selectedWidget.Enable(m_selectedItem); } }