closablecontainer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. class ClosableContainer extends Container
  2. {
  3. protected ref ClosableHeader m_ClosableHeader;
  4. protected bool m_LockCargo;
  5. void ClosableContainer( LayoutHolder parent, int sort = -1 )
  6. {
  7. m_Body = new array<ref LayoutHolder>;
  8. m_ClosableHeader = new ClosableHeader( this, "CloseButtonOnMouseButtonDown" );
  9. if( sort > -1 )
  10. m_RootWidget.SetSort( sort + 2 );
  11. m_MainWidget = m_MainWidget.FindWidget( "body" );
  12. }
  13. override bool IsDisplayable()
  14. {
  15. for(int i = 0; i < m_Body.Count(); i++)
  16. {
  17. LayoutHolder c = m_Body[i];
  18. if( c && c.IsDisplayable())
  19. return true;
  20. }
  21. return false;
  22. }
  23. override void UpdateRadialIcon()
  24. {
  25. if ( m_SlotIcon )
  26. {
  27. bool show_radial_icon;
  28. show_radial_icon = IsOpened();
  29. show_radial_icon = show_radial_icon && ( ( m_Entity.GetInventory().GetCargo() && m_Entity.CanDisplayCargo()) || m_Entity.GetSlotsCountCorrect() > 0 );
  30. show_radial_icon = show_radial_icon && !m_Entity.GetInventory().IsInventoryLockedForLockType( HIDE_INV_FROM_SCRIPT );
  31. if ( IsDisplayable() )
  32. {
  33. m_SlotIcon.GetRadialIconPanel().Show( true );
  34. SetOpenForSlotIcon(show_radial_icon);
  35. }
  36. else
  37. {
  38. m_SlotIcon.GetRadialIconPanel().Show( false );
  39. }
  40. }
  41. }
  42. void SetOpenState( bool state )
  43. {
  44. ItemManager.GetInstance().SetDefaultOpenState( m_Entity.GetType(), state );
  45. m_Closed = !state;
  46. if( !m_Closed )
  47. {
  48. OnShow();
  49. }
  50. else
  51. {
  52. OnHide();
  53. }
  54. }
  55. override Header GetHeader()
  56. {
  57. return m_ClosableHeader;
  58. }
  59. override void Open()
  60. {
  61. if( IsDisplayable() )
  62. {
  63. super.Open();
  64. ItemManager.GetInstance().SetDefaultOpenState( m_Entity.GetType(), true );
  65. SetOpenForSlotIcon(true);
  66. OnShow();
  67. m_Parent.m_Parent.Refresh();
  68. }
  69. }
  70. override void Close()
  71. {
  72. ItemManager.GetInstance().SetDefaultOpenState( m_Entity.GetType(), false );
  73. super.Close();
  74. SetOpenForSlotIcon(false);
  75. OnHide();
  76. m_Parent.m_Parent.Refresh(); //TODO: ???
  77. }
  78. override void SetLayoutName()
  79. {
  80. m_LayoutName = WidgetLayoutName.ClosableContainer;
  81. }
  82. override void OnShow()
  83. {
  84. if( IsOpened() )
  85. {
  86. super.OnShow();
  87. }
  88. }
  89. override LayoutHolder Get( int x )
  90. {
  91. if( m_Body && x < m_Body.Count() && x >= 0 )
  92. return m_Body.Get( x );
  93. return null;
  94. }
  95. override void Refresh()
  96. {
  97. if( !m_Closed )
  98. {
  99. super.Refresh();
  100. }
  101. }
  102. void CloseButtonOnMouseButtonDown()
  103. {
  104. Close();
  105. }
  106. override float GetFocusedContainerHeight( bool contents = false )
  107. {
  108. float x, y;
  109. if( contents && GetFocusedContainer() )
  110. y = GetFocusedContainer().GetFocusedContainerHeight( contents );
  111. else if( GetRootWidget() )
  112. GetRootWidget().GetScreenSize( x, y );
  113. return y;
  114. }
  115. override float GetFocusedContainerYPos( bool contents = false )
  116. {
  117. float x, y;
  118. if( contents && GetFocusedContainer() )
  119. y = GetFocusedContainer().GetFocusedContainerYPos( contents );
  120. else if( GetRootWidget() )
  121. GetRootWidget().GetPos( x, y );
  122. return y;
  123. }
  124. override float GetFocusedContainerYScreenPos( bool contents = false )
  125. {
  126. float x, y;
  127. if( contents && GetFocusedContainer() )
  128. y = GetFocusedContainer().GetFocusedContainerYScreenPos( contents );
  129. else if( GetRootWidget() )
  130. GetRootWidget().GetScreenPos( x, y );
  131. return y;
  132. }
  133. void MoveContainerUp( Widget cont )
  134. {
  135. if( m_Entity )
  136. {
  137. InventoryLocation loc = new InventoryLocation;
  138. m_Entity.GetInventory().GetCurrentInventoryLocation( loc );
  139. if( loc.IsValid() )
  140. {
  141. int slot = loc.GetSlot();
  142. Inventory.MoveAttachmentUp( slot );
  143. UpdateSelectionIcons();
  144. }
  145. }
  146. }
  147. void MoveContainerDown( Widget cont )
  148. {
  149. if( m_Entity )
  150. {
  151. InventoryLocation loc = new InventoryLocation;
  152. m_Entity.GetInventory().GetCurrentInventoryLocation( loc );
  153. if( loc.IsValid() )
  154. {
  155. int slot = loc.GetSlot();
  156. Inventory.MoveAttachmentDown( slot );
  157. UpdateSelectionIcons();
  158. }
  159. }
  160. }
  161. override void CheckHeaderDragability()
  162. {
  163. super.CheckHeaderDragability();
  164. if (m_ClosableHeader && m_Entity) //TODO: do the entity check here?
  165. {
  166. int flag = m_ClosableHeader.GetMainWidget().GetFlags();
  167. bool old = flag & WidgetFlags.DRAGGABLE;
  168. bool current = ItemBase.Cast(m_Entity) && m_Entity.IsTakeable();
  169. //bool changed = false;
  170. if (old && !current)
  171. {
  172. m_ClosableHeader.GetMainWidget().ClearFlags( WidgetFlags.DRAGGABLE );
  173. Widget drag = GetDragWidget();
  174. if (drag && drag == m_ClosableHeader.GetMainWidget())
  175. {
  176. CancelWidgetDragging();
  177. m_ClosableHeader.OnDropHeader(null);
  178. }
  179. //changed = true;
  180. }
  181. else if (!old && current)
  182. {
  183. m_ClosableHeader.GetMainWidget().SetFlags( WidgetFlags.DRAGGABLE );
  184. //changed = true;
  185. }
  186. /*if (old != current)
  187. {
  188. flag &= ~WidgetFlags.DRAGGABLE;
  189. m_ClosableHeader.GetMainWidget().SetFlags( flag );
  190. }*/
  191. }
  192. }
  193. }