• Custom VGUI not getting width?
    6 replies, posted
I have made a custom VGUI which works perfectly fine for the most part.... But for some reason, I can only do self:GetWide() in self.Paint to get the correct width. Otherwise, self:GetWide() just returns 64... How the hell? [CODE]local PANEL = {} function PANEL:Init() self:ShowCloseButton(false) self.Paint = function(panel, width, height) draw.RoundedBox(2, 0, 0, self:GetWide(), self:GetTall(), Color(80, 131, 185, 250)) print(self:GetWidth()) -- returns the right width, 400 end self.chiz_close = vgui.Create("DButton", self) -- changed from self.close to self.chiz_close self.chiz_close:SetSize(46, 21) self.chiz_close:SetPos(self:GetWide() - 52, 1) self.chiz_close:SetText("X") self.chiz_close:SetColor(Color(253, 255, 252)) self.chiz_close.Paint = function(panel, width, height) draw.RoundedBox(0, 0, 0, width, height, Color(232, 51, 55)) print(self:GetWidth()) -- This returns the right width, 400 end print(self:GetWidth()) -- This returns 64.... WTF!? end vgui.Register("MyDFrame", PANEL, "DFrame") [/CODE] Heres the code for the creation of the frame. Sorry if Im missing something simple! [CODE] local mainMenu = vgui.Create("MyDFrame") mainMenu:SetSize(400, 300) mainMenu:SetPos(ScrW() / 2 - 200, ScrH() /2 -150) mainMenu:MakePopup() [/CODE]
Should working, but not tested [lua] local PANEL = {} --[[--------------------------------------------------------- Name: Init -----------------------------------------------------------]] function PANEL:Init() self:ShowCloseButton(false) self.close = vgui.Create("DButton", self) -- works .. self.close.Paint = function(panel, width, height) draw.RoundedBox( 0, 0, 0, width, height, Color( 232, 51, 55, 255 ) ) end self.close.Parent = self self.close.Paint = function (self, w, h) self.Parent:PaintClose( w, h ) end end --[[--------------------------------------------------------- Name: Paint -----------------------------------------------------------]] function PANEL:Paint( w, h ) draw.RoundedBox( 2, 0, 0, w, h, Color( 80, 131, 185, 250 ) ) end --[[--------------------------------------------------------- Name: PaintClose -----------------------------------------------------------]] function PANEL:PaintClose( w, h ) draw.RoundedBox( 0, 0, 0, w, h, Color( 232, 51, 55, 255 ) ) end --[[--------------------------------------------------------- Name: PerformLayout -----------------------------------------------------------]] function PANEL:PerformLayout( w, h ) self.close:SetSize(46, 21) self.close:SetPos(w - 52, 1) self.close:SetText("X") self.close:SetColor(Color(253, 255, 252)) end vgui.Register("MyDFrame", PANEL, "DFrame") [/lua]
PANEL:Init is called after you do vgui.Create. It is also runs once while paint runs a lot and very fast. So it goes like this: [lua] You create panel, init is ran. You print the width (which is not set) Paint functions print the not set width (just as above) You change size AFTER init (since it runs right after vgui.Create is called), so your last print prints only once and 64. Your paint functions however continue to print since they still run, and they now print the right size since it's POST SetSize. [/lua] Solution is add a SetSize in init.
[QUOTE=|Royal|;45848337]Should working, but not tested [lua] local PANEL = {} --[[--------------------------------------------------------- Name: Init -----------------------------------------------------------]] function PANEL:Init() self:ShowCloseButton(false) self.close = vgui.Create("DButton", self) -- works .. self.close.Paint = function(panel, width, height) draw.RoundedBox( 0, 0, 0, width, height, Color( 232, 51, 55, 255 ) ) end self.close.Parent = self self.close.Paint = function (self, w, h) self.Parent:PaintClose( w, h ) end end --[[--------------------------------------------------------- Name: Paint -----------------------------------------------------------]] function PANEL:Paint( w, h ) draw.RoundedBox( 2, 0, 0, w, h, Color( 80, 131, 185, 250 ) ) end --[[--------------------------------------------------------- Name: PaintClose -----------------------------------------------------------]] function PANEL:PaintClose( w, h ) draw.RoundedBox( 0, 0, 0, w, h, Color( 232, 51, 55, 255 ) ) end --[[--------------------------------------------------------- Name: PerformLayout -----------------------------------------------------------]] function PANEL:PerformLayout( w, h ) self.close:SetSize(46, 21) self.close:SetPos(w - 52, 1) self.close:SetText("X") self.close:SetColor(Color(253, 255, 252)) end vgui.Register("MyDFrame", PANEL, "DFrame") [/lua][/QUOTE] Aha! Working okay from what I can make out with this code: [code] function PANEL:PerformLayout(width, height) self.chiz_head:SetSize(width, 38) -- line 44 self.chiz_close:SetPos(self.chiz_head:GetWide() - 62, 1) end [/code] Only thing is, the self.chiz_head:SetSize() seems to throw an error..... Not quite sure why. [code] cl_init.lua:44: attempt to index field 'chiz_head' (a nil value) [/code] Its strange because it sets with width just fine, dunno whats up here.
Where you defined chiz_close ? Why didnt use "width - 62", because self.chiz_head:GetWide() == witdh .... [lua] function PANEL:PerformLayout(width, height) self.chiz_head:SetSize(width, 38) -- line 44 self.chiz_close:SetPos(width - 62, 1) end [/lua]
edit: Got enough disagrees ill just delete my post.
[QUOTE=|Royal|;45852552]Where you defined chiz_close ? Why didnt use "width - 62", because self.chiz_head:GetWide() == witdh .... [lua] function PANEL:PerformLayout(width, height) self.chiz_head:SetSize(width, 38) -- line 44 self.chiz_close:SetPos(width - 62, 1) end [/lua][/QUOTE] This is my chiz_head in my init function: [code] self.chiz_head = vgui.Create("DPanel", self) [/code] I renamed self.close to self.chiz_close and both are defined in the Init() function. (I have got more in my PerformLayout function, just leaving it out to make it more readable) EDIT: Removed chiz_head and it says the same error on chiz_close.... Its like I can't use self at all.
Sorry, you need to Log In to post a reply to this thread.