aboutsummaryrefslogtreecommitdiff
path: root/gamemode/core/npc/cl_shop.lua
blob: 9c5ffb21c9bac0d3379d2633a277615b41c11af4 (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
local inv = nrequire("client/cl_inventory.lua")
local itm = nrequire("core/inventory/item.lua")
local w,h = ScrW(),ScrH()
local function DrawShopItemOnDPanel(dp,itemtbl,cost)
	--An item is a string, and int cost

	local shape = itemtbl.Shape
	local twidth,theight = 0,#shape
	for k = 1,#shape do
		twidth = math.max(twidth,#shape[k])
	end

	local slotsize = math.Round(w / 32)
	local backgrid = vgui.Create( "DGrid", dp )
	backgrid:SetPos( 10, 30 )
	backgrid:SetCols( twidth )
	backgrid:SetColWide( theight )
	backgrid:Dock(LEFT)

	local shopicon = vgui.Create( "DModelPanel", dp )
	shopicon:SetSize(slotsize * twidth, slotsize * theight)
	shopicon:SetPos(0,0)
	shopicon:SetText(itemtbl.Name)
	if itemtbl.Tooltip then
		shopicon:SetTooltip(itemtbl.Tooltip)
	end
	if itemtbl.Paint then
		shopicon.Paint = itemtbl.Paint
	end
	if itemtbl.DoOnPanel then
		itemtbl:DoOnPanel(shopicon)
	end
	shopicon.Paint = function(self, wi, hi)
		surface.SetDrawColor( 0, 0, 0, 255 )
		surface.DrawOutlinedRect( 0, 0, wi, hi)
	end
	shopicon.DoClick = function()
		print("You cliked me!")
	end
	shopicon.Item = itemtbl
	shopicon.Cost = cost
	shopicon.ondropped = function(back,j,i,item)
		print("ondropped was called!")
		print("back",back,"j",j,"i",i,"item",item)
		PrintTable(item)
		net.Start("buyitem")
		net.WriteString(item.Name)
		net.WriteUInt(back,8)
		net.WriteUInt(j,8)
		net.WriteUInt(i,8)
		net.SendToServer()
	end

	for k = 1, twidth do
		for i = 1, theight do
			if not shape[k][i] then
				local emptyslot = vgui.Create("DPanel", dp)
				emptyslot:SetSize(slotsize,slotsize)
				emptyslot:SetPos(slotsize * (i - 1) + 2, slotsize * (k - 1) + 2)
			end
		end
	end

	local buybutton = vgui.Create("DButton",dp)
	buybutton:Dock(RIGHT)
	buybutton:SetText("Buy\n(" .. cost .. ")")
	buybutton.DoClick = function()
		net.Start("art_buyitem")
		net.WriteString(itemtbl.Name)
		net.SendToServer()
	end
	
	local sellbutton = vgui.Create("DButton",dp)
	sellbutton:Dock(RIGHT)
	sellbutton:SetText("Sell\n(" .. math.floor(cost - math.log(cost)) .. ")")
	sellbutton.DoClick = function()
		net.Start("art_sellitem")
		net.WriteString(itemtbl.Name)
		net.SendToServer()
	end

end

local slotsize = math.Round(w / 32)

local function DrawShopOnDPanel(dp,items)
	--This gets pretty involved, lets try to not make it a clusterfuck.
	dp.Paint = function(self, wi, hi) draw.RoundedBox(4, 0,0,wi,hi,Color(100,0,0)) end
	local scrollpanel = vgui.Create( "DScrollPanel",dp )
	scrollpanel.Paint = function(self, wi, hi) draw.RoundedBox(4, 0,0,wi,hi,Color(0,0,100)) end
	scrollpanel:Dock(FILL)
	for k,v in pairs(items) do
		local itemtbl = itm.GetItemByName(v[1])
		local invpanel = vgui.Create( "DPanel", scollpanel)
		invpanel.Paint = function(self, wi, hi)
			draw.RoundedBox(4, 1,1,wi-4,hi-4,Color(50,50,50))
			draw.RoundedBox(4, 2,2,wi-5,hi-5,Color(100,100,100))
		end
		DrawShopItemOnDPanel(invpanel,itemtbl,v[2])
		scrollpanel:AddItem(invpanel)
		invpanel:Dock(TOP)
		local x,_ = invpanel:GetSize()
		invpanel:SetSize(x,slotsize * (#itemtbl.Shape) + 4)
		invpanel:Dock(TOP)

	end

end

local shopwindow,shoppanel

local function createshopwindow()
	shopwindow = vgui.Create( "DFrame" )
	shopwindow:SetPos( w - (w / 4), 0 )
	shopwindow:SetSize( w / 4, h )
	shopwindow:SetTitle( "Unset shop" )
	shopwindow:SetDraggable( true )
	shopwindow.OnClose = function(self)
		self:SetVisible(false)
		print("After onclose, shopwindow was",shopwindow)
	end
	shopwindow:SetVisible(false)

	shoppanel = vgui.Create( "DPanel",shopwindow)
	shoppanel:SetPos( 10, 30 ) -- Set the position of the panel
	shoppanel:Dock(FILL)
end
createshopwindow()

net.Receive("art_openshop",function()
	if not shopwindow:IsValid() then createshopwindow() end
	assert(shopwindow,"Shopwindow was not created, even after re-createing!")
	inv.ShowInventory()
	shopwindow:SetVisible(true)
	local stock = net.ReadTable()
	DrawShopOnDPanel(shoppanel,stock)
	shopwindow:MakePopup()
end)