aboutsummaryrefslogtreecommitdiff
path: root/gamemode/client/cl_inventory.lua
blob: 837f7a1a76e06a904084903d7434eec6e2fd3b49 (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
--[[
    Displays the inventory, for more information see /gamemode/shared/inventory.lua
]]
print("Hello from cl_inventory.lua")
--debug.Trace()
local invfuncs
invfuncs = ART.invfuncs
--invfuncs = include("../gamemodes/artery/gamemode/shared/inventory_common.lua")
--invfuncs = include("../shared/inventory_common.lua")
assert(invfuncs ~= nil, "Dependency failed")
local plyisininventory = false

--Displays a dropdown of options under the players mouse, if the option is clicked, does the function
--Requires a table of strings to functions, or strings to tables of strings to functions.
--Be careful not to make this a recursive table.
local function createMenuFor(menu, 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 DrawBackpackOnDPanel(dp, backpack, backpacknum, tent)
    local width = ScrW()
    local height = ScrH()
    local slotsize = math.Round(width / 32)
    local backgrid = vgui.Create( "DGrid", dp )
    backgrid:SetPos( 10, 30 )
    backgrid:SetCols( backpack[2][1] )
    backgrid:SetColWide( backpack[2][2] )
    backgrid:Dock(FILL)
    for i = 1,#(backpack[1]) do
        for j = 1,#(backpack[1][i]) do
            local item = backpack[1][j][i]
            if type(backpack[1][j][i]) == "table" then
                local itemwidth = 0
                for _,l in pairs(item.Shape) do
                    itemwidth = math.Max(itemwidth,#l)
                end
                local itemheight = #item.Shape
                local invicon = vgui.Create( "DImageButton", dp )
                invicon:SetSize(slotsize * itemwidth, slotsize * itemheight)
                invicon:SetPos(slotsize * (i - 1), slotsize * (j - 1))
                invicon:SetText(item.Name)
                if item.Tooltip then
                    invicon:SetTooltip(item.Tooltip)
                end
                if item.Paint then
                    invicon.Paint = item.Paint
                end
                if item.DoOnPanel then
                    item.DoOnPanel(invicon)
                end
                --invicon.Paint = function(self, w, h) draw.RoundedBox(4, 0,0,w,h,Color(0,100,0)) end
                invicon.DoClick = function()
                    if not item.GetOptions then return end
                    local menu = vgui.Create("DMenu")
        			createMenuFor(menu,item:GetOptions())
        			menu:Open()
                end
                invicon.Item = item
                invicon.invpos = {j,i}
                invicon.ent = tent
                invicon.backpacknum = backpacknum
                invicon:Droppable("Inventory")
            elseif not backpack[1][j][i] then
                local emptyslot = vgui.Create("DPanel", dp)
                emptyslot:SetSize(slotsize,slotsize)
                emptyslot:SetPos(slotsize * (i - 1), slotsize * (j - 1))
                --emptyslot.Paint = function(self, w, h) draw.RoundedBox(4, 0,0,w,h,Color(0,0,100)) end
                emptyslot:Receiver( "Inventory", function( receiver, tableOfDroppedPanels, isDropped, menuIndex, mouseX, mouseY )
                    if not isDropped then return end
                    local icon = tableOfDroppedPanels[1]
                    local item = icon.Item
                    local curpos = icon.invpos
                    --Set the shape it was at to false
                    if not icon.wasequiped and icon.ent == tent then
                        assert(curpos ~= nil, "print curpos was nil when not equiped")
                        for k = 1,#item.Shape do
                            for l = 1,#(item.Shape[k]) do
                                if k == 1 and l == 1 then continue end
                                backpack[1][curpos[1] + k - 1][curpos[2] + l - 1] = false
                            end
                        end
                        backpack[1][curpos[1]][curpos[2]] = false
                    end
                    if invfuncs.CanFitInBackpack(backpack,j,i,item) then
                        local fromtbl = icon.invpos
                        local wasequiped = icon.wasequiped
                        if wasequiped then
                            net.Start("unequipitem")
                            net.WriteString(wasequiped)
                            net.WriteUInt(backpacknum,16)
                            net.WriteUInt(i,16)
                            net.WriteUInt(j,16)
                            net.SendToServer()
                        else
                            net.Start("moveitem")
                            net.WriteEntity(icon.ent) -- from ent
                            net.WriteEntity(tent) -- to ent
                            net.WriteUInt(icon.backpacknum,16) -- from backpack number
                            net.WriteUInt(backpacknum,16) -- to backpack number
                            net.WriteUInt(fromtbl[1],16) -- From position
                            net.WriteUInt(fromtbl[2],16)
                            net.WriteUInt(j,16) -- To position
                            net.WriteUInt(i,16)
                            net.SendToServer()
                            if item.onEquip ~= nil then
                                item:onEquip(LocalPlayer())
                            end
                        end
                    end
                end, {} )
            end
        end
    end
end

function ShowInventory(ply,cmd,args)
    if plyisininventory then return end
    LocalPlayer().invdisplays = LocalPlayer().invdisplays or {}
    plyisininventory = true
    local width = ScrW()
    local height = ScrH()
    local dat = {}
    dat.panel = vgui.Create( "DFrame" )
    dat.redraw = ShowInventory
    table.insert(LocalPlayer().invdisplays,dat)
    local invpanel = dat.panel
    invpanel:SetPos( 0, 0 )
    invpanel:SetSize( width / 4, height )
    invpanel:SetTitle( "Inventory" )
    invpanel:SetDraggable( true )
    invpanel:MakePopup()
    invpanel.OnClose = function(self)
        plyisininventory = false
    end

    local sheet = vgui.Create( "DPropertySheet", invpanel )
    sheet:Dock( FILL )

    local invsheet = vgui.Create( "DPanel", sheet )
    invsheet.Paint = function( self, w, h ) draw.RoundedBox( 4, 0, 0, w, h, Color( 157, 160, 167 ) ) end
    sheet:AddSheet( "Inventory", invsheet, "icon16/cross.png" )

    local skillsheet = vgui.Create( "DPanel", sheet )
    skillsheet.Paint = function( self, w, h ) draw.RoundedBox( 4, 0, 0, w, h, Color( 157, 160, 167 ) ) end
    sheet:AddSheet( "Skills", skillsheet, "icon16/tick.png" )

    local questsheet = vgui.Create( "DPanel", sheet)
    questsheet.Paint = function(self,w,h) draw.RoundedBox(4,0,0,w,h,Color(157,160,167)) end
    sheet:AddSheet( "Quests", questsheet, "icon16/house.png")

    --Display quests
    local questselector = vgui.Create( "DScrollPanel", questsheet )
    questselector:SetSize((width / 4) - 20, (height / 2) - 40)
    questselector:SetPos(0,0)
    local questinfo = vgui.Create("DScrollPanel", questsheet)
    questinfo:SetSize(width / 4, (height / 2))
    --questinfo.Paint = function(self,w,h) draw.RoundedBox(4,0,0,w,h,Color(0,160,167)) end
    questinfo:SetPos(0,height / 2)
    for k,v in pairs(LocalPlayer().Quests or {}) do
        print("Displaying quest:" .. k)
        local questbutton = vgui.Create( "DButton" , questselector )
        questbutton:Dock(TOP)
        questbutton:SetText(k)
        questbutton.DoClick = function()
            print("At point of clicking, art is:")
            PrintTable(ART)
            ART.GetQuest(k).DrawQuestInfo(questinfo,v)
        end
    end

    --Display gear
    local slotsize = math.Round(width / 32)
    local displaypos = {
        ["Head"] = {(width / 8) - slotsize, 25},
        ["Body"] = {(width / 8) - slotsize, slotsize + 26},
        ["Legs"] = {(width / 8) - slotsize, (slotsize * 2) + 27},
        ["Boots"] = {(width / 8) - slotsize, (slotsize * 3) + 28},
        ["Gloves"] = {(width / 8) + (slotsize), (slotsize * 2) + 27},
        ["Left"] = {(width / 8) - (1.5 * slotsize), (slotsize * 4) + 29},
        ["Right"] = {(width / 8) - (0.5 * slotsize), (slotsize * 4) + 29}
    }
    print("Displaying inventory:")
    PrintTable(LocalPlayer().Inventory.Equiped)
    for k,v in pairs (LocalPlayer().Inventory.Equiped) do
        if v == false then
            local eqslot = vgui.Create( "DPanel", invsheet )
            eqslot:SetSize( slotsize, slotsize )
            eqslot:SetPos(displaypos[k][1],displaypos[k][2])
            eqslot:Receiver( "Inventory", function( receiver, tableOfDroppedPanels, isDropped, menuIndex, mouseX, mouseY )
                if not isDropped then return end
                print("Attempting to equip")
                local icon = tableOfDroppedPanels[1]
                local item = icon.Item
                PrintTable(item)
                print("In",k)
                if item.Equipable == k then
                    net.Start("equipitem")
                    net.WriteUInt(icon.backpacknum,16) -- Backpack number
                    local fromtbl = icon.invpos
                    net.WriteUInt(fromtbl[1],16) -- From position
                    net.WriteUInt(fromtbl[2],16)
                    print("Writing string",k)
                    net.WriteString(k)
                    net.SendToServer()
                end
            end, {} )
        else
            print("eqslot",k,"was not false, it was")
            PrintTable(v)
            local eqslot = vgui.Create("DImageButton",invsheet)
            eqslot:SetSize(slotsize,slotsize)
            eqslot:SetPos(displaypos[k][1],displaypos[k][2])
            if v.PaintEquiped then
                eqslot.Paint = v.PaintEquiped
            end
            if v.DoOnEqupPanel then
                v.DoOnEqupPanel(eqslot)
            end
            --eqslot:SetImage( "overviews/de_inferno", "vgui/avatar_default" )
            --eqslot.Paint = function(self, w, h) draw.RoundedBox(4, 0,0,w,h,Color(100,0,0)) end
            eqslot:Droppable("Inventory")
            eqslot.Item = v
            eqslot.wasequiped = k
        end
    end

    local backpacksheet = vgui.Create( "DPropertySheet", invsheet )
    backpacksheet:SetPos(0,slotsize * 6)
    backpacksheet:SetSize((width / 4) - 25, height - (slotsize * 6) - 50)
    print("Displaying backpacks:")
    PrintTable(LocalPlayer().Inventory.Backpacks)
    print("That was all the backpacks")
    for k,v in pairs(LocalPlayer().Inventory.Backpacks) do
        local tbacksheet = vgui.Create( "DPanel", backpacksheet )
        tbacksheet.Paint = function( self, w, h ) draw.RoundedBox( 4, 0, 0, w, h, Color( 157, 160, 167 ) ) end
        backpacksheet:AddSheet( v[3], tbacksheet, "icon16/cross.png" )

        DrawBackpackOnDPanel(tbacksheet,v,k,LocalPlayer())
    end

end

hook.Add("OnSpawnMenuOpen","ArteryOpenInventory",ShowInventory)
hook.Add("OnSpawnMenuClose","ArteryCloseInventory",function()
    for k,v in pairs(LocalPlayer().invdisplays) do
        if not v.panel:IsValid() then continue end
        PrintTable(v)
        v.panel:Close()
        LocalPlayer().invdisplays[k] = nil
    end
    --if LocalPlayer().invpanel == nil then return end
    --LocalPlayer().invpanel:Remove()
    plyisininventory = false
end)

concommand.Add("showinventory",ShowInventory)
local viewdistance = 100
local rotatespeed = 65
hook.Add("CalcView","ArteryInventoryView",function(ply,pos,ang,fov,nearz,farz)
    local view = {}
    view.origin = pos
    if plyisininventory then
        local xoff = viewdistance * math.sin(math.rad(CurTime() * rotatespeed))
        local yoff = viewdistance * math.cos(math.rad(CurTime() * rotatespeed))
        view.origin = view.origin + ( Vector(xoff,yoff,10) )
        ang.pitch = 20
        ang.yaw = (-CurTime() * rotatespeed) - 90
    end
    view.angles = ang
    view.fov = fov
    view.drawviewer = plyisininventory

    return view
end)