• Panel:GetPos() for parented panels?
    7 replies, posted
So, I was doing something that I thought would be very simple. I have a panel parented and docked into another one, and I want that panel to create another panel underneath it, a suggestions box based on what you type. I wanted this panel to be like a DMenu, not really being docked or parented to anything, but still looking in place. However, the problem is that when I tried to align this suggestions box to the other box where you type in stuff, I couldn't do it because [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Panel/GetPos]Panel:GetPos[/url] returns the position RELATIVE TO THE PARENT, and because the panel where you type in stuff is parented, it always returns 0, 0 no matter what I do, which makes it very difficult to align. I need to get a position relative to the screen to put the suggestion box at, but I can't currently. The other way I could position it would be by parenting the suggestion box to the box where you type in stuff, but this makes the suggestion box get clipped to the parent's bounds. I even tried [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Panel/NoClipping]Panel:NoClipping[/url] to disable this, but it didn't work. I even checked the DMenu's code to see how it does it, but it positions itself to the user's cursor, which I don't want. Any ideas? [B]TL;DR:[/B] [t]http://i.imgur.com/tgC8I3D.jpg[/t] Here's some code to explain the problem a bit better: [CODE] local frame = vgui.Create( 'DFrame' ) frame:SetSize( 500, 500 ) frame:Center() frame:MakePopup() local panelbase = vgui.Create( 'DPanel', frame ) -- Panel 1 panelbase:Dock( FILL ) local panel2 = vgui.Create( 'DTextEntry', panelbase ) -- Panel 2 ( HOW CAN I GET THE POSITION OF THIS PANEL RELATIVE TO THE SCREEN?! ) panel2:SetText('Type stuff here') panel2:Dock( TOP ) local suggestbox = vgui.Create( 'DPanel' ) -- Panel 3 -- How can I get this panel to be below panel2? -- I know I could just set its parent to be panelbase and dock it, but I want it to act like a DMenu - that doesn't require a parent to work -- I either want to parent this panel to NOTHING, or parent it to panel2. [/CODE]
[url]http://wiki.garrysmod.com/page/Panel/LocalToScreen[/url]
I tried that, but because the panel is parented, it returns 0,0 no matter what I do [editline]12th July 2016[/editline] Example: [CODE] local frame = vgui.Create( 'DFrame' ) frame:SetSize( 500, 500 ) frame:Center() frame:MakePopup() local panel = vgui.Create( 'DPanel', frame ) panel:Dock( FILL ) print( panel:GetPos() ) -- 0,0 print( panel:LocalToScreen( panel:GetPos() ) ) -- 0,0 [/CODE] And no, I can't do GetPos or LocalToScreen on the parent in my case because my panels are parented to more than 3 different panels at once and the position would stuff up [editline]12th July 2016[/editline] I updated the original post with some code that might make more sense to people [editline]12th July 2016[/editline] If anyone's wondering why I don't just position panel 3 using panel 1 it's because I have panel 2 in a separate file to panel 1, and in panel 2's file I create panel 3, so I kinda want it to be parented relative to panel 2 to stop me from getting confused and it breaking if there's no panel
The position isn't updated by the time you try to get it. You need to wait till the next frame, which you can do with a simple timer of 0 seconds.
[QUOTE=man with hat;50694956]The position isn't updated by the time you try to get it. You need to wait till the next frame, which you can do with a simple timer of 0 seconds.[/QUOTE] Thanks, I feel stupid for not knowing that! But-still one thing- is there a way to instantly update the position without using a timer? I tried [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Panel/InvalidateLayout]Panel:InvalidateLayout[/url] but it didn't seem to do anything Actually, I could just use the panel's PerformLayout function. Thanks for the help!
-no-
:snip: I tried that RobotBoy, but it just returns GetPos and GetSize, which isn't very helpful as neither are updated [editline]12th July 2016[/editline] Anyway, solved now.
[url]http://wiki.garrysmod.com/page/Panel/LocalToScreen[/url] Updated the pages with relevant info.
Sorry, you need to Log In to post a reply to this thread.