From 41d3b4aabc2fb52034f7d165d0e950cf81af73aa Mon Sep 17 00:00:00 2001 From: AlmTech Software Date: Sun, 5 Jan 2020 23:19:18 +0100 Subject: [PATCH] Added equipping UI --- .../engine/core/client/cl_character_net.lua | 1 + gamemode/engine/core/client/cl_fonts.lua | 6 + .../engine/derma/lib/cl_menu_iteminfo.lua | 15 ++- gamemode/engine/derma/lib/cl_menu_theme.lua | 4 +- gamemode/engine/derma/menus/menu_charinfo.lua | 103 ++++++++++++++++-- gamemode/engine/lib/server/sv_inventory.lua | 8 +- gamemode/engine/lib/server/sv_networking.lua | 1 + gamemode/engine/vars/cl_vars.lua | 10 +- 8 files changed, 124 insertions(+), 24 deletions(-) diff --git a/gamemode/engine/core/client/cl_character_net.lua b/gamemode/engine/core/client/cl_character_net.lua index 8bd9ed2..fde5ee1 100644 --- a/gamemode/engine/core/client/cl_character_net.lua +++ b/gamemode/engine/core/client/cl_character_net.lua @@ -37,6 +37,7 @@ end) net.Receive( "quantum_char_update", function( len, pl ) Quantum.Client.Inventory = net.ReadTable() || {} Quantum.Client.Character = net.ReadTable() + Quantum.Client.Equipped = net.ReadTable() Quantum.Debug( "Updated character." ) end) diff --git a/gamemode/engine/core/client/cl_fonts.lua b/gamemode/engine/core/client/cl_fonts.lua index ccb50e0..08e0ca0 100644 --- a/gamemode/engine/core/client/cl_fonts.lua +++ b/gamemode/engine/core/client/cl_fonts.lua @@ -109,6 +109,12 @@ surface.CreateFont( "q_tooltip_rarity", { antialias = true }) +surface.CreateFont( "q_tooltip_equiptype", { + font = "Cambria", + size = 18 * Quantum.Client.ResolutionScale, + antialias = true +}) + surface.CreateFont( "q_item_amount", { font = "Cambria Bold", size = 22 * Quantum.Client.ResolutionScale, diff --git a/gamemode/engine/derma/lib/cl_menu_iteminfo.lua b/gamemode/engine/derma/lib/cl_menu_iteminfo.lua index bb0b74e..05a0075 100644 --- a/gamemode/engine/derma/lib/cl_menu_iteminfo.lua +++ b/gamemode/engine/derma/lib/cl_menu_iteminfo.lua @@ -166,7 +166,7 @@ function iteminfo.giveoptions( p, page ) if( item.equipslot != nil ) then -- Equip op.equip = vgui.Create( "DButton", options ) - op.equip:SetText( "Equip" ) + op.equip:SetText( "Equip (" .. Quantum.EquipSlotsNames[item.equipslot] .. ")" ) op.equip:SetFont( "q_item_option_button" ) op.equip:SizeToContents() op.equip.w, op.equip.h = op.equip:GetSize() @@ -179,6 +179,8 @@ function iteminfo.giveoptions( p, page ) surface.PlaySound( "UI/buttonclick.wav" ) options.Close() + page.equippanels[item.equipslot].SetItem( item.id ) -- set its item + ---- EQUIP NET HERE ---- end ypos = ypos + op.equip.h + yspacing @@ -332,13 +334,22 @@ function iteminfo.givetooltip( p, page ) rare:SetPos( title.x, title.y + title.h + padding_s ) rare.x, rare.y = rare:GetPos() + local equip = vgui.Create( "DLabel", self ) + equip:SetText( Quantum.EquipSlotsNames[self.item.equipslot] || "ERROR EQUIPTYPE" ) + equip:SetFont( "q_tooltip_equiptype" ) + equip:SetTextColor( Color( 255, 255, 255, 255 ) ) + equip:SizeToContents() + equip.w, equip.h = equip:GetSize() + equip:SetPos( title.x, rare.y + rare.h + padding_s ) + equip.x, equip.y = equip:GetPos() + local desc = vgui.Create( "DLabel", self ) desc:SetText( self.item.desc || "ERROR DESC" ) desc:SetFont( "q_tooltip_desc" ) desc:SetTextColor( Color( 205, 205, 205, 255 ) ) desc:SizeToContents() desc.w, desc.h = desc:GetSize() - desc:SetPos( title.x, rare.y + rare.h + padding_s ) + desc:SetPos( title.x, equip.y + equip.h + padding_s ) desc.x, desc.y = desc:GetPos() if( self.item.soulbound == true ) then diff --git a/gamemode/engine/derma/lib/cl_menu_theme.lua b/gamemode/engine/derma/lib/cl_menu_theme.lua index f36fd7e..3d337a0 100644 --- a/gamemode/engine/derma/lib/cl_menu_theme.lua +++ b/gamemode/engine/derma/lib/cl_menu_theme.lua @@ -135,7 +135,7 @@ function theme.pagetext( p ) surface.DrawRect( 0, 0, w, h ) end -function theme.itempanel( p, rarecolor ) +function theme.itempanel( p, rarecolor, disableHover ) local w, h = p:GetSize() local icon if( p.icon == nil || !p.icon:IsValid() || !p.icon:IsVisible() ) then @@ -147,7 +147,7 @@ function theme.itempanel( p, rarecolor ) surface.SetDrawColor( theme.color.setalpha( rarecolor || Color( 0, 0, 0, 120 ), 150 ) ) surface.DrawRect( 0, 0, w, h ) - if( icon:IsHovered() ) then + if( icon:IsHovered() && !disableHover ) then theme.borderpanel( p, Color( 116, 185, 255, 90 ) ) else theme.borderpanel( p, Color( 255, 255, 255, 200 ) ) diff --git a/gamemode/engine/derma/menus/menu_charinfo.lua b/gamemode/engine/derma/menus/menu_charinfo.lua index bbfdd4c..0c62dd3 100644 --- a/gamemode/engine/derma/menus/menu_charinfo.lua +++ b/gamemode/engine/derma/menus/menu_charinfo.lua @@ -17,6 +17,8 @@ local padding = 10 * resScale local padding_s = 4 * resScale local errorMdl = "models/player.mdl" +local itemWidth, itemHeight = 65 * resScale, 65 * resScale + local function createItemAmountLabel( icon, item ) icon.amountpanel = vgui.Create( "DLabel", icon ) icon.amountpanel:SetText( tostring( item.amount ) ) @@ -28,8 +30,86 @@ local function createItemAmountLabel( icon, item ) return icon.amountpanel end +local function configureCamLookPos( icon ) + local mn, mx = icon.Entity:GetRenderBounds() + local size = 0 + + size = math.max( size, math.abs( mn.x ) + math.abs( mx.x ) ) + size = math.max( size, math.abs( mn.y ) + math.abs( mx.y ) ) + size = math.max( size, math.abs( mn.z ) + math.abs( mx.z ) ) + icon:SetCamPos( Vector( size/2, size, size ) ) + icon:SetLookAt( ( mn + mx )/2 ) +end + +local function createEquipSlotPanel( equiptype, x, y, scale, parent ) + local p = vgui.Create( "DPanel", parent ) + p:SetSize( itemWidth * scale, itemHeight * scale ) + p.w, p.h = p:GetSize() + p:SetPos( x - p.w/2, y - p.h/2 ) + p.x, p.y = p:GetPos() + + p.Paint = function( self ) + theme.itempanel( self, self.itemcolor, true ) + end + + function p.SetItem( itemid ) + --local itemid = itempanel.item.id + local itemTbl = Quantum.Item.Get( itemid ) + if( itemTbl != nil ) then + --itempanel:Remove() + p.itemid = itemid -- give it its equipped item + p.itemcolor = itemTbl.rarity.color -- give it its color + + p.item = itemTbl + p.item.amount = 1 + + if( IsValid( p.icon.tooltip ) ) then + p.icon.tooltip:Remove() -- remove the old + end + + p.icon.tooltip = iteminfo.givetooltip( p.icon, parent ) -- create a new + p.icon.tooltip:CreateInfo() + + p.icon:SetVisible( true ) + p.icon:SetModel( itemTbl.model ) + configureCamLookPos( p.icon ) + else + print( "REMOVE" ) + p.icon:SetVisible( false ) -- hide it if there is no item + if( IsValid( p.icon.tooltip ) ) then + p.icon.tooltip:Remove() + end + p.itemid = nil -- remove its item id + p.itemcolor = nil -- remove the background color + end + end + + p.icon = vgui.Create( "DModelPanel", p ) + p.icon:SetSize( p:GetSize() ) + p.icon.w, p.icon.h = p.icon:GetSize() + p.icon:SetPos( 0, 0 ) + p.icon:SetFOV( 45 ) + + p.title = vgui.Create( "DLabel", parent ) + p.title:SetText( Quantum.EquipSlotsNames[equiptype] ) + p.title:SetFont( "q_tooltip_equiptype" ) + p.title:SetEnabled( false ) + p.title:SetTextColor( Color( 255, 255, 255, 255 ) ) + p.title:SizeToContents() + p.title.w, p.title.h = p.title:GetSize() + + p.title:SetPos( p.x, ( p.y - p.title.h ) - padding_s ) -- move it over the slot + + p.title.Paint = function( self, w, h ) + draw.RoundedBox( 5, 0, 0, w, h, Color( 0, 0, 0, 90 ) ) + end + + return p +end + function menu.open( dt ) local items = Quantum.Client.Inventory + local equipped = Quantum.Client.Equipped if( Quantum.Client.Character == nil ) then chat.AddText( Color( 255, 25, 25 ), "[Quantum] - [ERROR] Check console for details.\n" ) @@ -111,6 +191,16 @@ function menu.open( dt ) ent:SetEyeTarget( eyepos + Vector( 40, -5, 2 ) ) function char:LayoutEntity( Entity ) return end + ---- EQUIP INFO---- + + f.equippanels = {} -- so we can access it later + + local slotScale = 1.2 + local slotXpos = char.x + char.w*0.75 + padding*4 + + f.equippanels[Quantum.EquipSlots.Head] = createEquipSlotPanel( Quantum.EquipSlots.Head, slotXpos, char.y + char.h/5, slotScale, f ) -- create the panel + f.equippanels[Quantum.EquipSlots.Head].SetItem( equipped[ Quantum.EquipSlots.Head ] ) -- give its current item + ---- Inventory panel ---- local inv = vgui.Create( "DPanel", f ) -- section for all of the item panels @@ -122,7 +212,6 @@ function menu.open( dt ) surface.DrawRect( 0, 0, w, h ) end - local itemWidth, itemHeight = 65 * resScale, 65 * resScale local maxW, maxH = Quantum.Inventory.Width, Quantum.Inventory.Height local itempanels = {} @@ -214,17 +303,7 @@ function menu.open( dt ) itempanels[ii].icon:SetFOV( 45 ) -- get the dimensions of the models entity - local mn, mx = itempanels[ii].icon.Entity:GetRenderBounds() - local size = 0 - - -- calculate the vector axises so that the view doesn't go outside of the models renderbounds -- - size = math.max( size, math.abs( mn.x ) + math.abs( mx.x ) ) - size = math.max( size, math.abs( mn.y ) + math.abs( mx.y ) ) - size = math.max( size, math.abs( mn.z ) + math.abs( mx.z ) ) - - -- Apply the new "data" -- - itempanels[ii].icon:SetCamPos( Vector( size/2, size, size ) ) - itempanels[ii].icon:SetLookAt( ( mn + mx )/2 ) + configureCamLookPos( itempanels[ii].icon ) ---- Amount Text ---- if( itempanels[ii].item.amount > 1 ) then diff --git a/gamemode/engine/lib/server/sv_inventory.lua b/gamemode/engine/lib/server/sv_inventory.lua index 9dcea06..988fd82 100644 --- a/gamemode/engine/lib/server/sv_inventory.lua +++ b/gamemode/engine/lib/server/sv_inventory.lua @@ -11,13 +11,7 @@ Quantum.Inventory.Size = Quantum.Inventory.Width * Quantum.Inventory.Height function Quantum.Server.Inventory.Create( char ) char.inventory = {} - char.equipped = { - [Quantum.EquipSlots.Head] = -1, -- -1 means that it is empty - [Quantum.EquipSlots.Chest] = -1, - [Quantum.EquipSlots.Legs] = -1, - [Quantum.EquipSlots.Boots] = -1, - [Quantum.EquipSlots.Weapon] = -1 - } + char.equipped = {} char.effects = {} return char.inventory diff --git a/gamemode/engine/lib/server/sv_networking.lua b/gamemode/engine/lib/server/sv_networking.lua index b763b04..45d7527 100644 --- a/gamemode/engine/lib/server/sv_networking.lua +++ b/gamemode/engine/lib/server/sv_networking.lua @@ -140,6 +140,7 @@ function Quantum.Net.Inventory.Update( pl ) net.Start( "quantum_char_update" ) net.WriteTable( Quantum.Server.Char.GetInventory( charTbl ) ) net.WriteTable( char ) + net.WriteTable( charTbl.equipped ) net.Send( pl ) end diff --git a/gamemode/engine/vars/cl_vars.lua b/gamemode/engine/vars/cl_vars.lua index ddea923..14a7d77 100644 --- a/gamemode/engine/vars/cl_vars.lua +++ b/gamemode/engine/vars/cl_vars.lua @@ -6,4 +6,12 @@ -- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ Quantum.Client.ResolutionScale = ScrH() / 1080 -Quantum.Client.ServerBannerPath = "gamemodes/" .. GM.FolderName .. "/gamemode/content/materials/quantum/server_banner.png" \ No newline at end of file +Quantum.Client.ServerBannerPath = "gamemodes/" .. GM.FolderName .. "/gamemode/content/materials/quantum/server_banner.png" + +Quantum.EquipSlotsNames = { + [Quantum.EquipSlots.Head] = "Helmet", + [Quantum.EquipSlots.Chest] = "Chest", + [Quantum.EquipSlots.Legs] = "Legs", + [Quantum.EquipSlots.Boots] = "Boots", + [Quantum.EquipSlots.Weapon] = "Weapon", +} \ No newline at end of file