luagarrys-mod

How to detect right-click on DCollapsibleCategory? [GLua]


I believe that Garry's Mod's DCollapsibleCategory derives from Panel so I've tried to create a custom vgui element so I can add a function PANEL:OnMousePressed(keyCode) but that has completely changed how it draws on my scoreboard.

Here's the code for the custom vgui element, perhaps I missed something:

local PANEL = {}

AccessorFunc(PANEL, "hiddenPanel", "HiddenPanel")

function PANEL:Init()
    self.dataPanel = vgui.Create("DCollapsibleCategory", self)
    self.dataPanel:SetExpanded(0)
    self.dataPanel:SetLabel('')
    self.dataPanel:GetChildren()[1]:SetTall(45)
    self.dataPanel.Paint = function(pnl, w, h)
        draw.RoundedBox(0, 0, 0, w, h, Advocate_Scoreboard.Colors.BASE_HEADER)
    end

    self.hiddenList = vgui.Create("DPanelList", self.dataPanel)
    self.hiddenList:SetSpacing(1)
    self.dataPanel:SetContents(self.hiddenList)

    self.hiddenPanel = vgui.Create("DPanel", self.hiddenList)
    self.hiddenList:AddItem(self.hiddenPanel)
    self.hiddenPanel.Paint = function(pnl, w, h)
        draw.RoundedBox(0, 0, 0, w, h, Color(50, 50, 50))
    end
end

function PANEL:PerformLayout()
    self.dataPanel:SetSize(self:GetWide(), self:GetTall())
    self.dataPanel:GetChildren()[1]:SetTall(45)

    self.hiddenList:SetSize(self.dataPanel:GetWide(), self.dataPanel:GetTall())
    self.hiddenPanel:SetSize(self.hiddenList:GetWide(), 39)
end

function PANEL:OnMousePressed(keyCode)
    print(keyCode)
end

vgui.Register("Advocate_Scoreboard.Row", PANEL)

DCollapsibleCategory doesn't respond to any .DoClick functions - I don't believe is has any of them methods with it deriving from Panel.


Solution

  • DCollapsibleCategory is made up of a header row and contents.

    The header does not implement OnMousePressed.

    The contents is made up of whatever UI elements you add to it.
    If you want the contents to respond to mouse events, you should override the OnMousePressed for those individual elements.


    If you really need this sort of functionality in the header row, you could either submit a pull request to the Garry's Mod repository.

    Or alternatively:

    1. Copy the code for the DCollapsibleCategory
    2. Rename DCategoryHeader to DCategoryHeader2
    3. Rename DCollapsibleCategory to DCollapsibleCategory2
    4. Add the following function to the header PANEL table
    local PANEL = {
        ...
    
        OnMousePressed = function(self, mcode)
            self.DoClick(self)
            self:GetParent():OnMousePressed(mcode)
        end,
    
        ...
    }
    
    derma.DefineControl( "DCategoryHeader2", "Category Header", PANEL, "DButton" )
    

    And now you can detect mouse clicks on the header

    local DCollapsible = vgui.Create( "DCollapsibleCategory2", frame )
    DCollapsible:SetLabel( "Collapsible Category" )
    DCollapsible:SetPos( 25, 50 )
    DCollapsible:SetSize( 250, 100 )
    DCollapsible:SetExpanded( false )
    function DCollapsible:OnMousePressed(keyCode)
        chat.AddText("DCollapsible Key pressed- " .. tostring(keyCode))
    end
    

    Full code snippet