contextmenu.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //--------------------------------------------------------------------------
  2. class ContextMenu extends ScriptedWidgetEventHandler
  3. {
  4. private Widget m_context_menu_root_widget;
  5. private Widget m_context_menu_panel_widget;
  6. private ref array<ref CallQueueContext> m_commands;
  7. private int m_max_item_width;
  8. private int m_count;
  9. const int ITEMS_COUNT = 27;
  10. //--------------------------------------------------------------------------
  11. void ContextMenu()
  12. {
  13. m_commands = new array<ref CallQueueContext>;
  14. m_count = 0;
  15. }
  16. //--------------------------------------------------------------------------
  17. void ~ContextMenu()
  18. {
  19. Clear();
  20. delete m_context_menu_root_widget;
  21. }
  22. //--------------------------------------------------------------------------
  23. void Init(Widget layoutRoot)
  24. {
  25. if (!m_context_menu_root_widget)
  26. {
  27. m_context_menu_root_widget = GetGame().GetWorkspace().CreateWidgets("gui/layouts/day_z_inventory_context_menu.layout", layoutRoot);
  28. m_context_menu_panel_widget = m_context_menu_root_widget.FindAnyWidget("PanelWidget");
  29. m_context_menu_root_widget.Show(false);
  30. m_context_menu_root_widget.SetHandler(this);
  31. }
  32. }
  33. //--------------------------------------------------------------------------
  34. void Show(int x, int y)
  35. {
  36. if ( m_count == 0) return;
  37. int screen_w, screen_h;
  38. float w, h;
  39. float sx, sy;
  40. int offset_x;// = -20;
  41. int offset_y;// = -10;
  42. GetScreenSize(screen_w, screen_h);
  43. // align buttons
  44. float button_height_percent = 0.02; // button height is 4% of screen height
  45. float button_height = screen_h * button_height_percent;
  46. for ( int i = 0; i < m_count; i++)
  47. {
  48. ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + (i+1).ToString() ) ) );
  49. if(menu_button)
  50. {
  51. menu_button.SetSize(0.90, button_height);
  52. menu_button.Show(true);
  53. }
  54. }
  55. AutoHeightSpacer spacer;
  56. m_context_menu_panel_widget.GetScript(spacer);
  57. if ( spacer )
  58. {
  59. spacer.Update();
  60. }
  61. m_context_menu_root_widget.GetSize(w, h);
  62. m_context_menu_panel_widget.GetSize(sx, sy);
  63. m_context_menu_root_widget.SetSize(w, sy);
  64. // set position
  65. m_context_menu_root_widget.GetScreenSize(w,h);
  66. screen_w -= 10;
  67. screen_h -= 10;
  68. int right_edge = x + w - offset_x;
  69. if (right_edge > screen_w)
  70. {
  71. x = screen_w - w - offset_x;
  72. }
  73. else
  74. {
  75. x = x + offset_x;
  76. }
  77. int bottom_edge = y + h - offset_y;
  78. if (bottom_edge > screen_h)
  79. {
  80. y = y - h - offset_y;
  81. }
  82. else
  83. {
  84. y = y + offset_y;
  85. }
  86. m_context_menu_root_widget.SetPos(x, y);
  87. m_context_menu_root_widget.Show(true);
  88. }
  89. //--------------------------------------------------------------------------
  90. void ShowBackdrop(bool show)
  91. {
  92. if (show == true)
  93. {
  94. m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(true);
  95. }
  96. else
  97. {
  98. m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(false);
  99. }
  100. }
  101. //--------------------------------------------------------------------------
  102. void Hide()
  103. {
  104. m_context_menu_root_widget.Show(false);
  105. Clear();
  106. }
  107. //--------------------------------------------------------------------------
  108. bool IsVisible()
  109. {
  110. return m_context_menu_root_widget.IsVisible();
  111. }
  112. //--------------------------------------------------------------------------
  113. override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
  114. {
  115. super.OnMouseLeave(w, enterW, x, y);
  116. if ( enterW && m_context_menu_panel_widget && enterW != m_context_menu_panel_widget && enterW.GetParent() != m_context_menu_panel_widget )
  117. {
  118. Hide();
  119. return true;
  120. }
  121. return false;
  122. }
  123. //--------------------------------------------------------------------------
  124. override bool OnMouseButtonDown(Widget w, int x, int y, int button)
  125. {
  126. super.OnMouseButtonDown(w, x, y, button);
  127. if (button == MouseState.LEFT && w.GetUserID() > -1 && w.GetUserID() < m_commands.Count())
  128. {
  129. CallQueueContext ctx = m_commands.Get(w.GetUserID());
  130. int actionId = Param3<ItemBase, int, int>.Cast(ctx.m_params).param2;
  131. if (actionId == EActions.DELETE)
  132. Hide();
  133. UIScriptedMenu menu = GetGame().GetUIManager().GetMenu();
  134. if (menu)
  135. GetGame().GetCallQueue(CALL_CATEGORY_GUI).Call(menu.Refresh);
  136. ctx.Call();
  137. return true;
  138. }
  139. return false;
  140. }
  141. //--------------------------------------------------------------------------
  142. void Add(string label, Class obj, string fn_name, Param params)
  143. {
  144. AddEx(label, FadeColors.LIGHT_GREY, obj, fn_name, params);
  145. }
  146. void AddEx(string label, int labelColor, Class obj, string funcName, Param params)
  147. {
  148. int count = Count();
  149. ButtonWidget menuButton = ButtonWidget.Cast(m_context_menu_root_widget.FindAnyWidget(string.Format("Button%1", count + 1)));
  150. if (menuButton)
  151. {
  152. label.ToUpper();
  153. menuButton.SetText(label);
  154. menuButton.SetTextColor(labelColor);
  155. menuButton.Show(true);
  156. if (!funcName)
  157. menuButton.SetFlags(menuButton.GetFlags() | WidgetFlags.IGNOREPOINTER);
  158. int itemWidth = label.Length();
  159. if (m_max_item_width < itemWidth)
  160. m_max_item_width = itemWidth;
  161. }
  162. m_count++;
  163. m_commands.Insert(new CallQueueContext(obj, funcName, params));
  164. }
  165. //--------------------------------------------------------------------------
  166. void Remove(int index)
  167. {
  168. if (index < m_commands.Count())
  169. {
  170. m_commands.RemoveOrdered(index);
  171. ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + ( index + 1 ).ToString() ) ) );
  172. menu_button.Show( false );
  173. menu_button.SetText( "" );
  174. m_count--;
  175. }
  176. }
  177. //--------------------------------------------------------------------------
  178. int Count()
  179. {
  180. return m_commands.Count();
  181. }
  182. //--------------------------------------------------------------------------
  183. void Clear()
  184. {
  185. int i;
  186. m_commands.Clear();
  187. Widget child = m_context_menu_panel_widget.GetChildren();
  188. while(child)
  189. {
  190. ButtonWidget button = ButtonWidget.Cast(child);
  191. if(button)
  192. {
  193. button.Show(false);
  194. }
  195. child = child.GetSibling();
  196. }
  197. m_count = 0;
  198. m_max_item_width = 0;
  199. }
  200. };