summaryrefslogtreecommitdiff
path: root/gamemode/client/cl_inventory.lua
blob: 80af83f83d1dbe283a00c4f341f630a8bed72718 (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
print("Custom inventory loaded")

--Some calculations to draw the inventory nicely
local scrx = ScrW()
local scry = ScrH()
--Make the menu 30% of the x to the closest 64 and 100% of the y
local invxadj = ((scrx*0.4)%64)
local invxsize = (scrx*0.40)-invxadj-38 --no idea why the 38 works, but it does
local invysize = scry

local function createMenuFor(menu, tbl)
  print("Createing actions for:")
  PrintTable(tbl)
  for k,v in pairs(tbl) do
    if(isfunction(v)) then --This is a dead-end, add the menu
      local thisoption = menu:AddOption(k,v)
    else --Otherwise it should be a table, recursively call to create
      local submenu = menu:AddSubMenu(k)
      createMenuFor(submenu,v)
    end
  end
end

local function createTooltipTextFor(tbl)
  local string = ""
  for k,v in pairs(tbl) do
    string = string .. (k .. " x" .. v .. "\n")
  end
  return string
end

local function createPanel()
  local frame = vgui.Create( "DFrame" )
  frame:SetSize( invxsize, invysize )
  frame:SetTitle( "Inventory" )
  frame:MakePopup()
  frame:SetPos(scrx-invxsize,0)
  frame:SetKeyboardInputEnabled(true)
  frame:ShowCloseButton(false)
  frame.OnKeyCodePressed = function(self, key)
    if(key == KEY_Q) then
      frame:Close()
    end
  end

  local tabsheet = vgui.Create("DPropertySheet", frame)
  tabsheet:Dock(FILL)

  local invtab = vgui.Create("DPanel",tabsheet)
  tabsheet:AddSheet( "Inventory", invtab, "icon16/database.png" )
  local structtab = vgui.Create("DPanel",tabsheet)
  tabsheet:AddSheet( "Structures", structtab, "icon16/bullet_blue.png")
  local combitab = vgui.Create("DPanel",tabsheet)
  tabsheet:AddSheet( "Combinations", combitab, "icon16/bullet_picture.png")
  local equiptab = vgui.Create("DPanel",tabsheet)
  tabsheet:AddSheet( "Equipment", equiptab, "icon16/user.png" )
  local proptab = vgui.Create("DPanel",tabsheet)
  tabsheet:AddSheet( "Props", proptab, "icon16/wrench.png" )
  local tooltab = vgui.Create("DPanel", tabsheet)
  tabsheet:AddSheet( "Tools", tooltab, "icon16/bullet_red.png")
  if(LocalPlayer():IsAdmin()) then
    local admintab = vgui.Create("DPanel",tabsheet)
    tabsheet:AddSheet("Admin", admintab, "icon16/bullet_star.png")
  end

  --Inventory
  local layout = vgui.Create( "DTileLayout", invtab )
  layout:SetBaseSize( 64 ) -- Tile size
  layout:Dock( FILL )

  layout:MakeDroppable( "unique_name" ) -- Allows us to rearrange children
  for k, v in SortedPairs( Resources ) do
    if(v == 0) then
      continue
    end
    local selection = vgui.Create("DImageButton")
    if(GMS.Resources[k] == nil) then --This resource is not registered!
      selection:SetImage("vgui/avatar_default")
      print("Resource:" .. k .. " not registed! This might be a bug!")
      continue
    elseif(GMS.Resources[k].Icon == nil) then
      selection:SetImage("vgui/avatar_default")
      print("Resource:" .. k .. " does not have an .Icon field! This might be a bug!")
      continue
    else
      selection:SetImage(GMS.Resources[k].Icon)
      selection:SetTooltip(GMS.Resources[k].Description)
    end

    selection:SetSize(64,64)
    selection.DoClick = function()
      if(GMS.Resources[k].UniqueData) then
        print("We should expand menu to show all uniqueid's")
      else
        if(GMS.Resources[k].Actions == nil) then
          print("gamemode/client/cl_inventory.lua: Looking for actions for " .. k .. " but found nil!")
          return
        end
        local menu = vgui.Create("DMenu")
        --print("makeing menu for " .. k)
        PrintTable(GMS.Resources[k])
        createMenuFor(menu,GMS.Resources[k].Actions)
        menu:Open()
      end
    end
    layout:Add( selection )
  end

  --Structure building
  local layout = vgui.Create( "DTileLayout", structtab)
  layout:SetBaseSize( 64 ) -- Tile size
  layout:Dock( FILL )
  layout:MakeDroppable( "unique_name" ) -- Allows us to rearrange children
  for k,v in pairs(GMS.Combinations["Structures"]) do
    --print("Createing panel for " .. v.BuildSiteModel)
    local modelPanel = vgui.Create( "DModelPanel", layout )
    --modelPanel:SetPos( 0, 0 )
    modelPanel:SetSize( 64, 64 )
    modelPanel:SetModel( v.BuildSiteModel )
    modelPanel:SetCamPos( Vector( 50,50,50 ) )
    modelPanel:SetLookAt( Vector( 0, 0, -40 ) )
    modelPanel.DoClick = function()
      --print("I want to construct a " .. k)
      LocalPlayer():ConCommand("gms_MakeCombination Structures " .. k )
    end
    layout:Add(modelPanel)
  end

  --Combinations in the combinations button
  local layout = vgui.Create( "DTileLayout", combitab)
  layout:SetBaseSize( 64 ) -- Tile size
  layout:Dock( FILL )
  layout:MakeDroppable( "unique_name" ) -- Allows us to rearrange children
  for k,v in pairs(GMS.Combinations["Combinations"]) do
    --PrintTable(v)
    local combipanel = vgui.Create("DButton", layout)
    combipanel:SetSize(64,64)
    combipanel:SetText(v.Name)
    combipanel:SetTooltip(createTooltipTextFor(v.Req))
    combipanel.DoClick = function()
      --print("I want to combine a " .. v.Name)
      LocalPlayer():ConCommand("gms_MakeCombination Combinations " .. v.Name)
    end
    layout:Add(combipanel)
  end

  --Make the prop spawn menu
  local layout = vgui.Create( "DListLayout", proptab)
  layout:Dock( FILL )
  local labelnum = 0
  for k,v in pairs(GMS_SpawnLists) do
    local DCollapsible = vgui.Create( "DCollapsibleCategory" )
    DCollapsible:SetLabel(k)
    DCollapsible:SetSize(frame:GetWide(),200)
    DCollapsible:SetPadding(5)
    --DCollapsible:SetPos(0,labelnum)
    DCollapsible:SetExpanded( 0 )
    layout:Add(DCollapsible)
    labelnum = labelnum + 20
    local propgrid = vgui.Create("DTileLayout", DCollapsible)
    propgrid:SetBaseSize( 64 ) -- Tile size
    propgrid:Dock( FILL )
    propgrid:MakeDroppable( "unique_name" ) -- Allows us to rearrange children
    DCollapsible:SetContents(propgrid)
    for i,j in pairs(v) do
      local itm = vgui.Create("DModelPanel", propgrid)
      itm:SetModel(j)
      itm:SetSize( 64, 64 )
      itm.DoClick = function()
        RunConsoleCommand( "gm_spawn", j, 0 )
      end
      propgrid:Add(itm)
    end
    --layout:Add(combipanel)
  end

  --Make the tools menu
	local toollist = vgui.Create( "DPanelList", tooltab )
  toollist:Dock(FILL)

	local bAlt = true
	local NumTools = 0
  PrintTable(spawnmenu.GetTools()[1]["Items"][1])
	for k, v in pairs( spawnmenu.GetTools()[1]["Items"][1] ) do
    if(!isnumber(k)) then return end
    local Item = vgui.Create( "DButton", toollist)
    Item:SetSize(frame:GetWide(),20)
    Item:SetText(v.Text)
    Item.DoClick = function()
      LocalPlayer():ConCommand( v.Command )
    end
    --[[
		if ( table.HasValue( GMS.ProhibitedStools, v.ItemName ) ) then continue end
		NumTools = NumTools + 1

		local Item = vgui.Create( "DComboBox", toollist )
		Item:SetText( v.Text or "Test")
		Item.OnSelect = function( button ) self:EnableControlPanel( button ) end
		concommand.Add( Format( "tool_%s", v.ItemName ), function() Item:OnSelect() end )

		if ( v.SwitchConVar ) then
			Item:AddCheckBox( v.SwitchConVar )
		end

		Item.ControlPanelBuildFunction = v.CPanelFunction
		Item.Command = v.Command
		Item.Name = v.ItemName
		Item.Controls = v.Controls
		Item.Text = v.Text

		--Item:SetAlt( bAlt )
		bAlt = !bAlt
    ]]
		toollist:AddItem( Item )
	end

  return frame
end

local invpanel = nil
function GM:OnSpawnMenuOpen()

  if(invpanel == nil) then
    invpanel = createPanel()
    return
  end
  if(! invpanel:IsValid()) then
    invpanel = createPanel()
    return
  end
end

function GM:ReloadSpawnMenu()
  if(invpanel == nil) then
    return
  end
  if(!invpanel:IsValid()) then
    return
  end
  if(invpanel != nil) then
    invpanel:Close()
    invpanel = createPanel()
    return
  end
end

function GM:OnSpawnMenuClose()

end

function GM:SpawnMenuEnabled()
	return false
end