dropdownprefab.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. class DropdownPrefab extends ScriptedWidgetEventHandler
  2. {
  3. protected Widget m_Root;
  4. protected Widget m_Parent;
  5. protected ScrollWidget m_Scroller;
  6. protected Widget m_ContentContainer;
  7. protected ref array<Widget> m_Content = new array<Widget>;
  8. protected Widget m_Button;
  9. protected Widget m_Holder;
  10. protected TextWidget m_Text;
  11. protected ImageWidget m_ImageExpand;
  12. protected ImageWidget m_ImageCollapse;
  13. protected bool m_IsExpanded;
  14. ref ScriptInvoker m_OnSelectItem = new ScriptInvoker();
  15. void DropdownPrefab( Widget root, string text = "" )
  16. {
  17. m_Parent = root;
  18. m_Root = GetGame().GetWorkspace().CreateWidgets( "gui/layouts/new_ui/dropdown_prefab/dropdown_prefab.layout", root );
  19. m_Scroller = ScrollWidget.Cast( m_Root.FindAnyWidget( "dropdown_container" ) );
  20. m_ContentContainer = m_Root.FindAnyWidget( "dropdown_content" );
  21. m_Text = TextWidget.Cast( m_Root.FindAnyWidget( "dropdown_text" ) );
  22. m_Holder = m_Root.FindAnyWidget( "holder" );
  23. SetText( text );
  24. m_Button = m_Root.FindAnyWidget( "dropdown_selector_button" );
  25. m_ImageExpand = ImageWidget.Cast( m_Root.FindAnyWidget( "expand_image" ) );
  26. m_ImageCollapse = ImageWidget.Cast( m_Root.FindAnyWidget( "collapse_image" ) );
  27. m_Root.SetHandler( this );
  28. RefreshContent();
  29. }
  30. void RefreshContent()
  31. {
  32. Widget child = m_ContentContainer.GetChildren();
  33. while( child )
  34. {
  35. if( m_Content.Find( child ) > -1 )
  36. {
  37. m_Content.Insert( child );
  38. }
  39. }
  40. m_ContentContainer.Update();
  41. m_Root.Update();
  42. float x, y;
  43. m_ContentContainer.GetScreenSize( x, y );
  44. if( y > m_Scroller.GetContentHeight() )
  45. {
  46. m_Scroller.SetAlpha( 1 );
  47. }
  48. else
  49. {
  50. m_Scroller.SetAlpha( 0 );
  51. }
  52. }
  53. int AddElement( string text, Widget content = null )
  54. {
  55. ButtonWidget element = ButtonWidget.Cast( GetGame().GetWorkspace().CreateWidgets( "gui/layouts/new_ui/dropdown_prefab/dropdown_element.layout", m_ContentContainer ) );
  56. RichTextWidget textWidget = RichTextWidget.Cast(element.FindAnyWidget("dropdown_element_text"));
  57. textWidget.SetText( text );
  58. if( content )
  59. {
  60. element.AddChild( content, false );
  61. }
  62. m_ContentContainer.Update();
  63. textWidget.Update();
  64. m_Root.Update();
  65. element.Update();
  66. m_Content.Insert( element );
  67. return m_Content.Count() - 1;
  68. }
  69. void RemoveElement( int index )
  70. {
  71. if( 0 < index && index < m_Content.Count() )
  72. {
  73. delete m_Content.Get( index );
  74. m_ContentContainer.Update();
  75. m_Root.Update();
  76. m_Parent.Update();
  77. }
  78. }
  79. void Close()
  80. {
  81. if( m_IsExpanded )
  82. {
  83. m_IsExpanded = !m_IsExpanded;
  84. m_Scroller.Show( m_IsExpanded );
  85. m_ImageExpand.Show( !m_IsExpanded );
  86. m_ImageCollapse.Show( m_IsExpanded );
  87. m_Root.Update();
  88. m_Parent.Update();
  89. }
  90. }
  91. void SetText( string text )
  92. {
  93. m_Text.SetText( text );
  94. }
  95. override bool OnClick( Widget w, int x, int y, int button )
  96. {
  97. int index = m_Content.Find( w );
  98. if ( index > -1 )
  99. {
  100. m_OnSelectItem.Invoke( index );
  101. m_IsExpanded = false;
  102. m_Scroller.Show( m_IsExpanded );
  103. m_ImageExpand.Show( !m_IsExpanded );
  104. m_ImageCollapse.Show( m_IsExpanded );
  105. return true;
  106. }
  107. return false;
  108. }
  109. override bool OnMouseButtonDown( Widget w, int x, int y, int button )
  110. {
  111. if ( (w == m_Button || w == m_Text || w == m_Holder) && button == MouseState.LEFT )
  112. {
  113. m_IsExpanded = !m_IsExpanded;
  114. m_Scroller.Show( m_IsExpanded );
  115. m_ImageExpand.Show( !m_IsExpanded );
  116. m_ImageCollapse.Show( m_IsExpanded );
  117. m_Root.Update();
  118. m_Parent.Update();
  119. m_ContentContainer.Update();
  120. return true;
  121. }
  122. return false;
  123. }
  124. }