aboutsummaryrefslogtreecommitdiff
path: root/gamemode/hud/draw_qmenu.lua
blob: 14667041e60f36c13ea0f6f9ae9538ff01e45040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
local Inventory 	= nil
local RecipeMenu	= nil
local MCO = Color(0,0,0,150)
local BCO = Color(0,50,100,150)
local GCO = Color(0,100,0,150)
local HCO = Color(30,30,30,150)

--Garry... for the love of god, this fucking function is spamming the console with debug information.
--I decided to override it so it shuts up!
dragndrop.HandleDroppedInGame = function() end

function ReloadInventory()
	if (!IsInventoryOpen()) then return end
	Inventory.List:Clear()

	ReloadRecipes()

	for k,v in pairs(LocalPlayer():GetInventory()) do
		local a = Inventory.List:Add("DPanel")
		a:SetSize(64,64)
		a.Item = GetItemByName(v.Name)
		a.Quantity = v.Quantity
		a:Droppable("INVENTORY")
		a.Paint = function(s,w,h)
			DrawRect(0,0,w,h,MCO)
			DrawMaterialRect(0,0,w,h,MAIN_WHITECOLOR,s.Item.Icon)

			DrawText("x"..v.Quantity,"ChatFont",1,h-18,MAIN_TEXTCOLOR)
		end

		a.OnStopDragging = function(s,a,b,c)
			local ID = IsMouseInSlot()

			if (!ID) then return end

			RequestEquip(ID,s.Item.Name)
		end

		local Ab = a.OnMousePressed
		a.OnMousePressed = function(s,m)
			if (m == MOUSE_RIGHT) then
				local X,Y = gui.MousePos()

				local menu = DermaMenu()
				menu.Paint = function(s,w,h) DrawRect(0,0,w,h,MCO) end
				menu:AddOption( "Use", function() if (s.Item) then RequestUseItem(s.Item.Name) end end ):SetColor(MAIN_TEXTCOLOR)
				menu:AddOption( "Drop", function() if (s.Item) then RequestDropItem(s.Item.Name) end end ):SetColor(MAIN_TEXTCOLOR)

				menu:Open()
				menu:SetPos(X,Y)
			end

			Ab(s,m)
		end

		a.OnCursorEntered = function(s)
			Inventory.Desc:SetVisible(true)
			Inventory.Desc:SetText(s.Item.Name.."\n\n"..s.Item.Desc)
			Inventory.CraftingButton:SetVisible(false)

			s.Hover = true
		end

		a.OnCursorExited = function(s)
			Inventory.Desc:SetVisible(false)

			if (RecipeMenu.SelectedRecipe) then
				Inventory.CraftingButton:SetVisible(true)
			end

			s.Hover = false
		end
	end
end

function ReloadRecipes()
	if (!IsInventoryOpen()) then return end
	RecipeMenu.List:Clear()
	RecipeMenu.Combine.Items = {}

	for k,v in pairs(GAMEMODE.KnownRecipes) do
		local a = RecipeMenu.List:Add("MBButton")
		a:SetText(v.Name)
		a:SetSize(RecipeMenu.List:GetWide()-5,23)
		a.Item = v
		a.Paint = function(s,w,h)
			if (s.Pressed) then DrawRect(0,0,w,h-3,BCO)
			elseif (s.Hover) then DrawRect(0,0,w,h-3,HCO)
			else DrawRect(0,0,w,h-3,MCO) end

			DrawText(s.Item.Name,"Trebuchet18",4,0,MAIN_TEXTCOLOR)
		end
		a.DoClick = function(s)
			if (RecipeMenu.SelectedRecipe == a.Item) then RecipeMenu.SelectedRecipe = nil Inventory.CraftingButton:SetVisible(false)
			else RecipeMenu.SelectedRecipe = a.Item Inventory.CraftingButton:SetVisible(true) end
		end
	end
end

function GM:OnSpawnMenuOpen()
	if (LocalPlayer():IsPigeon()) then return end
	if (IsInventoryOpen()) then return end

	surface.PlaySound("wintersurvival2/hud/itemopen.wav")

	if (!Inventory) then
		--Recipes
		RecipeMenu = vgui.Create("MBFrame")
		RecipeMenu:SetPos(485,ScrH()-480)
		RecipeMenu:SetSize(200,400)
		RecipeMenu:SetTitle("Recipes")
		RecipeMenu:ShowCloseButton(false)
		RecipeMenu.Paint = function(s,w,h) DrawRect(0,0,w,h,MCO) end
		RecipeMenu.SelectedRecipe = nil

		local Pane = vgui.Create( "DScrollPanel", RecipeMenu )
		Pane:SetPos(5,25)
		Pane:SetSize(RecipeMenu:GetWide()-10,RecipeMenu:GetTall()-150)
		Pane.Paint = function(s,w,h) DrawRect(0,0,w,h,MCO) end

		local l = vgui.Create("DListLayout",Pane)
		l:SetSize(Pane:GetWide()-10,Pane:GetTall()-10)
		l:SetPos(5,5)

		RecipeMenu.List = l

		local c = vgui.Create( "MBFrame", RecipeMenu )
		c:SetPos(5,RecipeMenu:GetTall()-120)
		c:SetSize(RecipeMenu:GetWide()-10,115)
		c:SetTitle("")
		c:ShowCloseButton(false)
		c.Items = {}
		c.Paint = function(s,w,h)
			DrawRect(0,0,w,h,MCO)

			local Press = input.MousePress(MOUSE_LEFT,"MenuPress")

			if (table.Count(s.Items) < 1) then
				DrawText("Draw items here!","Trebuchet24",w/2,h/2-3,MAIN_WHITECOLOR,1)
			else
				for i = 1,4 do
					local v = s.Items[i]

					if (v and v.Name) then
						local Item = GetItemByName(v.Name)
						local y    = -15+20*i
						local x    = 5

						local X,Y  	= s:GetParent():GetPos()
						local SX,SY = s:GetPos()
						X = X+SX
						Y = Y+SY

						if (!input.IsMouseInBox(X+x,Y+y,w-10,18)) then DrawRect(x,y,w-10,18,MCO)
						else
							DrawRect(x,y,w-10,18,GCO)

							if (Press) then
								s.Items[i] = nil
								break
							end
						end

						if (Item.Icon) then
							DrawMaterialRect(x+1,y+1,16,16,MAIN_WHITECOLOR,Item.Icon)
						end

						DrawText("x "..v.Quantity.." "..v.Name,"Trebuchet18",23,y,MAIN_WHITECOLOR)
					end
				end
			end
		end
		c:Receiver("INVENTORY", function(s,a,b,c)
			local p = a[1]

			if (p.Item and b) then
				local Find = false

				for i = 1,4 do
					if (s.Items[i] and s.Items[i].Name == p.Item.Name) then
						if (LocalPlayer():HasItem(s.Items[i].Name,s.Items[i].Quantity+1)) then
							s.Items[i].Quantity = s.Items[i].Quantity + 1
						end

						Find = true
						break
					end
				end

				if (!Find) then
					for i = 1,4 do
						if (!s.Items[i]) then
							s.Items[i] = {Name = p.Item.Name,Quantity = 1}
							break
						end
					end
				end
			end
		end)

		local B = vgui.Create("MBButton",c)
		B:SetPos(5,c:GetTall()-25)
		B:SetSize(c:GetWide()-10,20)
		B:SetVisible(true)
		B.DoClick = function(s)
			local Items = RecipeMenu.Combine.Items

			if (Items and table.Count(Items) > 0) then
				DiscoverItems(Items)
			end
		end
		B.Paint = function(s,w,h)
			if (s.Pressed) then DrawRect(0,0,w,h-3,BCO)
			elseif (s.Hover) then DrawRect(0,0,w,h-3,HCO)
			else DrawRect(0,0,w,h-3,MCO) end

			DrawText("Combine","Trebuchet18",w/2,h/2,MAIN_TEXTCOLOR,1)
		end

		RecipeMenu.Combine = c

		--Inventory
		Inventory = vgui.Create("MBFrame")
		Inventory:SetPos(80,ScrH()-480)
		Inventory:SetSize(400,400)
		Inventory:SetTitle("Inventory")
		Inventory:SetDeleteOnClose(false)
		Inventory:MakePopup()
		Inventory:SetVisible(false)
		Inventory:ShowCloseButton(false)
		Inventory.OnKeyCodePressed = function(panel, key)
			if(key == 27 or key == 15) then
				if(Inventory and Inventory:IsVisible())then
					Inventory:SetVisible(false)
					RecipeMenu:SetVisible(false)
					CloseLootventory()
					surface.PlaySound("wintersurvival2/hud/itemequip.wav")
				else
					Inventory:SetVisible(true)
					RecipeMenu:SetVisible(true)
				end
			end
		end
		Inventory.Paint = function(s,w,h)
			DrawRect(0,0,w,h,MCO)

			local X,Y,W,H = 5,h-120,w-10,115
			DrawRect(X,Y,W,H,MCO)

			local Rec = RecipeMenu.SelectedRecipe

			if (Rec and Inventory.CraftingButton:IsVisible()) then
				local Recipe 	= Rec.Recipe
				local pl 		= LocalPlayer()
				local C 		= 0
				local Step		= W/3

				DrawText("Products:","Trebuchet18",X+5,Y,MAIN_TEXTCOLOR)
				DrawText("Reagents:","Trebuchet18",X+Step+5,Y,MAIN_TEXTCOLOR)
				DrawText("Tools:","Trebuchet18",X+Step*2+5,Y,MAIN_TEXTCOLOR)

				for k,v in pairs(Recipe.Resources) do
					C = C+1
					if (pl:HasItem(k,v)) then DrawText(v.." x "..k,"Trebuchet18",X+Step+5,Y+16*C,MAIN_GREENCOLOR)
					else DrawText(v.." x "..k,"Trebuchet18",X+Step+5,Y+16*C,MAIN_REDCOLOR) end
				end

				C = 0
				for k,v in pairs(Recipe.Tools) do
					C = C+1
					if (pl:HasItem(k,v)) then DrawText(v.."x "..k,"Trebuchet18",X+5+Step*2,Y+16*C,MAIN_GREENCOLOR)
					else DrawText(v.."x "..k,"Trebuchet18",X+5+Step*2,Y+16*C,MAIN_REDCOLOR) end
				end

				DrawText("1 x "..Rec.Name,"Trebuchet18",X+5,Y+16,MAIN_GREENCOLOR)
			end
		end
		Inventory.OnClose = function(s) CloseLootventory() RecipeMenu:SetVisible(false) surface.PlaySound("wintersurvival2/hud/itemequip.wav") end

		local Pane = vgui.Create( "DScrollPanel", Inventory )
		Pane:SetPos(5,25)
		Pane:SetSize(Inventory:GetWide()-10,Inventory:GetTall()-150)
		Pane.Paint = function(s,w,h) DrawRect(0,0,w,h,MCO) end
		Pane:Receiver("LOOTVENTORY", function(s,a,b,c)
			local p = a[1]

			if (p.Item and IsValid(p.Par.Entity) and b) then
				DemandItems(p.Item.Name,p.Quantity,p.Par.Entity)
				DemandLootventoryUpdate(p.Par.Entity)
			end
		end)

		local l = vgui.Create("DIconLayout",Pane)
		l:SetSize(Pane:GetWide()-10,Pane:GetTall()-10)
		l:SetPos(5,5)
		l:SetSpaceY(5)
		l:SetSpaceX(5)

		Inventory.List = l

		local B = vgui.Create("MBButton",Inventory)
		B:SetPos(Inventory:GetWide()-110,Inventory:GetTall()-25)
		B:SetSize(100,20)
		B:SetVisible(false)
		B.DoClick = function(s)
			if (RecipeMenu.SelectedRecipe) then RequestCreateRecipe(RecipeMenu.SelectedRecipe.Name) end
		end
		B.Paint = function(s,w,h)
			if (s.Pressed) then DrawRect(0,0,w,h-3,BCO)
			elseif (s.Hover) then DrawRect(0,0,w,h-3,HCO)
			else DrawRect(0,0,w,h-3,MCO) end

			DrawText("Craft!","Trebuchet18",w/2,h/2,MAIN_TEXTCOLOR,1)
		end

		Inventory.CraftingButton = B

		local a = vgui.Create("DLabel",Inventory)
		a:SetPos(10,Inventory:GetTall()-120)
		a:SetSize(Inventory:GetWide()-2,1)
		a:SetText("")
		a:SetWrap(true)
		a:SetVisible(false)
		a:SetTextColor(MAIN_TEXTCOLOR)
		a:SetFont("Trebuchet18")
		a:SetAutoStretchVertical(true)

		Inventory.Desc = a
	end

	--RecipeMenu:SetVisible(true)
	--Inventory:SetVisible(true)
	if(Inventory and Inventory:IsVisible())then
		Inventory:SetVisible(false)
		RecipeMenu:SetVisible(false)
	else
		Inventory:SetVisible(true)
		RecipeMenu:SetVisible(true)
	end

	ReloadInventory()
end

function IsInventoryOpen()
	return (Inventory and Inventory:IsVisible())
end