lua - 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.
解决方案
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:
- Copy the code for the DCollapsibleCategory
- Rename DCategoryHeader to DCategoryHeader2
- Rename DCollapsibleCategory to DCollapsibleCategory2
- 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
推荐阅读
- c# - 如何使用 VS 2019 为我的应用程序构建在线安装程序
- java - Spring boot 中的 PostgreSQL 继承命名问题
- javascript - 缓存/预加载 SVG 文件反应原生
- drools - 如何使用 RestWorkItemHandler 调用需要 application/x-www-form-urlencoded content-type 的服务
- scala - 如何在 Scala 中处理嵌套的期货序列函数
- c# - 控制器动作中的日期参数
- python - 如果 GPU 没有“预热”,Pytorch CUDA 内存不足?
- java - java交换混合和最大数组超出范围
- ios - WatchOS 6 套接字编程
- javascript - 有没有办法用一定数量的“空”条目创建“矩阵”对象?