Guest User

cl_menu.lua

a guest
Jun 12th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.44 KB | None | 0 0
  1. --[[
  2. © 2011 CloudSixteen.com do not share, re-distribute or modify
  3. without permission of its author (kurozael@gmail.com).
  4. --]]
  5.  
  6. local GRADIENT = surface.GetTextureID("gui/gradient");
  7. local PANEL = {};
  8.  
  9. -- Called when the panel is initialized.
  10. function PANEL:Init()
  11. if ( !openAura.theme:Call("PreMainMenuInit", self) ) then
  12. local smallTextFont = openAura.option:GetFont("menu_text_small");
  13. local tinyTextFont = openAura.option:GetFont("menu_text_tiny");
  14. local scrW = ScrW();
  15. local scrH = ScrH();
  16.  
  17. self:SetPos(0, 0);
  18. self:SetSize( ScrW(), ScrH() );
  19. self:SetDrawOnTop(false);
  20. self:SetPaintBackground(false);
  21. self:SetMouseInputEnabled(true);
  22. self:SetKeyboardInputEnabled(true);
  23.  
  24. self.closeMenuLabel = vgui.Create("aura_LabelButton", self);
  25. self.closeMenuLabel:SetFont(smallTextFont);
  26. self.closeMenuLabel:SetText("LEAVE MENU");
  27. self.closeMenuLabel:SetCallback(function(button)
  28. self:SetOpen(false);
  29. end);
  30. self.closeMenuLabel:SetToolTip("Click here to close the menu.");
  31. self.closeMenuLabel:SizeToContents();
  32. self.closeMenuLabel:OverrideTextColor( openAura.option:GetColor("information") );
  33. self.closeMenuLabel:SetMouseInputEnabled(true);
  34. self.closeMenuLabel:SetPos(scrW * 0.1, scrH * 0.1);
  35.  
  36. self.characterMenuLabel = vgui.Create("aura_LabelButton", self);
  37. self.characterMenuLabel:SetFont(smallTextFont);
  38. self.characterMenuLabel:SetText("CHARACTERS");
  39. self.characterMenuLabel:SetCallback(function(button)
  40. self:SetOpen(false);
  41. openAura.character:SetPanelOpen(true);
  42. end);
  43. self.characterMenuLabel:SetToolTip("Click here to view the character menu.");
  44. self.characterMenuLabel:SizeToContents();
  45. self.characterMenuLabel:SetMouseInputEnabled(true);
  46. self.characterMenuLabel:SetPos(scrW * 0.1, self.closeMenuLabel.y + self.closeMenuLabel:GetTall() + 8);
  47.  
  48. openAura:SetNoticePanel(self);
  49.  
  50. self.CreateTime = SysTime();
  51. self.activePanel = nil;
  52.  
  53. openAura.theme:Call("PostMainMenuInit", self);
  54.  
  55. self:Rebuild();
  56. end;
  57. end;
  58.  
  59. -- A function to return to the main menu.
  60. function PANEL:ReturnToMainMenu(performCheck)
  61. if (!performCheck) then
  62. if ( IsValid(self.activePanel) and self.activePanel:IsVisible() ) then
  63. self.activePanel:MakePopup();
  64. self:FadeOut(0.5, self.activePanel, function()
  65. self.activePanel = nil;
  66. end);
  67. end;
  68. elseif ( IsValid(self.activePanel) and self.activePanel:IsVisible() ) then
  69. self.activePanel:MakePopup();
  70. end;
  71. end;
  72.  
  73. -- A function to rebuild the panel.
  74. function PANEL:Rebuild()
  75. if ( !openAura.theme:Call("PreMainMenuRebuild", self) ) then
  76. local activePanel = openAura.menu:GetActivePanel();
  77. local isVisible = false;
  78. local width = self.characterMenuLabel:GetWide();
  79. local scrH = ScrH();
  80. local scrW = ScrW();
  81. local oy = self.characterMenuLabel.y + self.characterMenuLabel:GetTall() + 16;
  82. local ox = ScrW() * 0.1;
  83. local y = oy;
  84. local x = ox;
  85.  
  86. for k, v in pairs(openAura.menu.stored) do
  87. if ( IsValid(v.button) ) then
  88. v.button:Remove();
  89. end;
  90. end;
  91.  
  92. openAura.MenuItems.items = {};
  93. openAura.plugin:Call("MenuItemsAdd", openAura.MenuItems);
  94. openAura.plugin:Call("MenuItemsDestroy", openAura.MenuItems);
  95.  
  96. table.sort(openAura.MenuItems.items, function(a, b)
  97. return a.text < b.text;
  98. end);
  99.  
  100. for k, v in ipairs(openAura.MenuItems.items) do
  101. local button, panel = nil, nil;
  102.  
  103. if ( openAura.menu.stored[v.panel] ) then
  104. panel = openAura.menu.stored[v.panel].panel;
  105. else
  106. panel = vgui.Create(v.panel, self);
  107. panel:SetVisible(false);
  108. panel:SetSize( openAura.menu:GetWidth(), panel:GetTall() );
  109. panel:SetPos(0, 0);
  110. end;
  111.  
  112. if ( !panel.IsButtonVisible or panel:IsButtonVisible() ) then
  113. button = vgui.Create("aura_LabelButton", self);
  114. end;
  115.  
  116. if (button) then
  117. button:SetFont( openAura.option:GetFont("menu_text_tiny") );
  118. button:SetText( string.upper(v.text) );
  119. button:SetAlpha(0);
  120. button:FadeIn(0.5);
  121. button:SetToolTip(v.tip);
  122. button:SetCallback(function(button)
  123. if (openAura.menu:GetActivePanel() != panel) then
  124. self:OpenPanel(panel);
  125. end;
  126. end);
  127. button:SizeToContents();
  128. button:SetMouseInputEnabled(true);
  129. button:SetPos(x, y);
  130.  
  131. y = y + button:GetTall() + 8;
  132. isVisible = true;
  133.  
  134. if (button:GetWide() > width) then
  135. width = button:GetWide();
  136. end;
  137. end;
  138.  
  139. openAura.menu.stored[v.panel] = {
  140. button = button,
  141. panel = panel
  142. };
  143. end;
  144.  
  145. for k, v in pairs(openAura.menu.stored) do
  146. if (activePanel == v.panel) then
  147. if ( !IsValid(v.button) ) then
  148. self:FadeOut(0.5, activePanel, function()
  149. self.activePanel = nil;
  150. end);
  151. end;
  152. end;
  153. end;
  154.  
  155. openAura.theme:Call("PostMainMenuRebuild", self);
  156. end;
  157. end;
  158.  
  159. -- A function to open a panel.
  160. function PANEL:OpenPanel(panelToOpen)
  161. if ( !openAura.theme:Call("PreMainMenuOpenPanel", self, panelToOpen) ) then
  162. local scrW = ScrW();
  163. local scrH = ScrH();
  164.  
  165. if ( IsValid(self.activePanel) ) then
  166. self:FadeOut(0.5, self.activePanel, function()
  167. self.activePanel = nil;
  168. self:OpenPanel(panelToOpen);
  169. end);
  170.  
  171. return;
  172. end;
  173.  
  174. self.activePanel = panelToOpen;
  175. self.activePanel:SetAlpha(0);
  176. self.activePanel:SetSize( openAura.menu:GetWidth(), self.activePanel:GetTall() );
  177. self.activePanel:MakePopup();
  178. self.activePanel:SetPos( (scrW * 0.9) - self.activePanel:GetWide(), scrH * 0.1 );
  179.  
  180. self:FadeIn(0.5, self.activePanel, function()
  181. timer.Simple(FrameTime() * 0.5, function()
  182. if ( IsValid(self.activePanel) ) then
  183. if (self.activePanel.OnSelected) then
  184. self.activePanel:OnSelected();
  185. end;
  186. end;
  187. end);
  188. end);
  189.  
  190. openAura.theme:Call("PostMainMenuOpenPanel", self, panelToOpen);
  191. end;
  192. end;
  193.  
  194. -- A function to make a panel fade out.
  195. function PANEL:FadeOut(speed, panel, Callback)
  196. if ( panel:GetAlpha() > 0 and ( !self.fadeOutAnimation or !self.fadeOutAnimation:Active() ) ) then
  197. self.fadeOutAnimation = Derma_Anim("Fade Panel", panel, function(panel, animation, delta, data)
  198. panel:SetAlpha( 255 - (delta * 255) );
  199.  
  200. if (animation.Finished) then
  201. self.fadeOutAnimation = nil;
  202. panel:SetVisible(false);
  203. end;
  204.  
  205. if (animation.Finished and Callback) then
  206. Callback();
  207. end;
  208. end);
  209.  
  210. if (self.fadeOutAnimation) then
  211. self.fadeOutAnimation:Start(speed);
  212. end;
  213.  
  214. openAura.option:PlaySound("rollover");
  215. else
  216. panel:SetVisible(false);
  217. panel:SetAlpha(0);
  218.  
  219. if (Callback) then
  220. Callback();
  221. end;
  222. end;
  223. end;
  224.  
  225. -- A function to make a panel fade in.
  226. function PANEL:FadeIn(speed, panel, Callback)
  227. if ( panel:GetAlpha() == 0 and ( !self.fadeInAnimation or !self.fadeInAnimation:Active() ) ) then
  228. self.fadeInAnimation = Derma_Anim("Fade Panel", panel, function(panel, animation, delta, data)
  229. panel:SetVisible(true);
  230. panel:SetAlpha(delta * 255);
  231.  
  232. if (animation.Finished) then
  233. self.fadeInAnimation = nil;
  234. end;
  235.  
  236. if (animation.Finished and Callback) then
  237. Callback();
  238. end;
  239. end);
  240.  
  241. if (self.fadeInAnimation) then
  242. self.fadeInAnimation:Start(speed);
  243. end;
  244.  
  245. openAura.option:PlaySound("click_release");
  246. else
  247. panel:SetVisible(true);
  248. panel:SetAlpha(255);
  249.  
  250. if (Callback) then
  251. Callback();
  252. end;
  253. end;
  254. end;
  255.  
  256. -- Called when the panel is painted.
  257. function PANEL:Paint()
  258. if ( !openAura.theme:Call("PreMainMenuPaint", self) ) then
  259. derma.SkinHook("Paint", "Panel", self);
  260. openAura.theme:Call("PostMainMenuPaint", self);
  261. end;
  262.  
  263. return true;
  264. end;
  265.  
  266. -- Called every fame.
  267. function PANEL:Think()
  268. if ( !openAura.theme:Call("PreMainMenuThink", self) ) then
  269. if ( openAura.plugin:Call("ShouldDrawMenuBackgroundBlur") ) then
  270. openAura:RegisterBackgroundBlur(self, self.CreateTime);
  271. else
  272. openAura:RemoveBackgroundBlur(self);
  273. end;
  274.  
  275. self:SetVisible( openAura.menu:GetOpen() );
  276. self:SetSize( ScrW(), ScrH() );
  277.  
  278. openAura.menu.height = ScrH() * 0.75;
  279. openAura.menu.width = ScrW() * 0.6;
  280.  
  281. if (self.fadeOutAnimation) then
  282. self.fadeOutAnimation:Run();
  283. end;
  284.  
  285. if (self.fadeInAnimation) then
  286. self.fadeInAnimation:Run();
  287. end;
  288.  
  289. openAura.theme:Call("PostMainMenuThink", self);
  290. end;
  291. end;
  292.  
  293. -- A function to set whether the panel is open.
  294. function PANEL:SetOpen(isOpen)
  295. self:SetVisible(isOpen);
  296. self:ReturnToMainMenu(true);
  297.  
  298. openAura.menu.isOpen = isOpen;
  299. gui.EnableScreenClicker(isOpen);
  300.  
  301. if (isOpen) then
  302. self:Rebuild();
  303. self.CreateTime = SysTime();
  304.  
  305. openAura.plugin:Call("MenuOpened");
  306. else
  307. openAura.plugin:Call("MenuClosed");
  308. end;
  309. end;
  310.  
  311. vgui.Register("aura_Menu", PANEL, "DPanel");
  312.  
  313. hook.Add("VGUIMousePressed", "openAura.menu:VGUIMousePressed", function(panel, code)
  314. local activePanel = openAura.menu:GetActivePanel();
  315. local menuPanel = openAura.menu:GetPanel();
  316.  
  317. if (openAura.menu:GetOpen() and activePanel and menuPanel == panel) then
  318. menuPanel:ReturnToMainMenu();
  319. end;
  320. end);
  321.  
  322. usermessage.Hook("aura_MenuOpen", function(msg)
  323. local panel = openAura.menu:GetPanel();
  324.  
  325. if (panel) then
  326. openAura.menu:SetOpen( msg:ReadBool() );
  327. else
  328. openAura.menu:Create( msg:ReadBool() );
  329. end;
  330. end);
  331.  
  332. usermessage.Hook("aura_MenuToggle", function(msg)
  333. if ( openAura.Client:HasInitialized() ) then
  334. if ( !openAura.menu:GetPanel() ) then
  335. openAura.menu:Create(false);
  336. end;
  337.  
  338. local panel = openAura.menu:GetPanel();
  339.  
  340. if (panel) then
  341. openAura.menu:ToggleOpen();
  342. end;
  343. end;
  344. end);
Add Comment
Please, Sign In to add comment