Jump to content

[TUT] Adding X/Y values to centered windows - for newbs.


glowdemon1

Recommended Posts

This tutorial is not aimed for the community of advanced scripters, this is more of a tutorial for people who are new and do not know what to do. This tutorial will be really short. If you rather wish to skip this, head down to find the final code. If you're an advanced scripter, don't bother commenting with hate, people who are unknown to scripting and willing to get deeper into this could be helped by this.

Please note that I did not made the centerWindow at first, credits go to laserlaser.

Lets take the original code from https://wiki.multitheftauto.com/wiki/CenterWindow

function centerWindow(center_window) 
    local screenW,screenH=guiGetScreenSize() 
    local windowW,windowH=guiGetSize(center_window,false) 
    local x,y = (screenW-windowW)/2,(screenH-windowH)/2 
    guiSetPosition(center_window,x,y,false) 
end 

If I were to do centerWindow(myGUI), the GUI would be completely centered, but that's not what we want, what we want to do is center the window and then add x/y values to it.

Lets first change the function's arguments first, simply add addX and addY to it. Like this :

function centerWindow(center_window,addX,addY) 

After we're done with that we'll go and do our next step.

simply add the arguments "addX, addY" to guiSetPosition so that the script knows we have to add these values.

guiSetPosition(center_window,x+addX,y+addY,false) 

This was the final step to complete the script, congratulations.

The syntax of this function :

string centerWindow( element TheElement int addX int addY) 

For instance :

centerWindow(yourWindow,0,50) 

Would center the window and then add a value of 50 to the Y axis, resulting in a centered window, yet the GUI would be 50 "units" down. Could come in handy for stuff like this : http://puu.sh/4HiK7.jpg

Final code.

function centerWindow(center_window,addX,addY) 
    local screenW,screenH=guiGetScreenSize() 
    local windowW,windowH=guiGetSize(center_window,false) 
    local x,y = (screenW-windowW)/2,(screenH-windowH)/2 
    guiSetPosition(center_window,x+addX,y+addY,false) 
end 

Edited by Guest
Link to comment
  • 1 month later...
  • 2 weeks later...
centerWindow ( yourWindow, 0, 50 ) 

you forgot to add " ) " Add some spaces in these codes because newbies won't even try to read the tutorial when they see these codes, honestly saying. :)

Fixed, ty for reminding me. On the other hand I find this one useful for when I first started and didn't use relative positioning yet.

Link to comment
  • 5 months later...
centerWindow ( yourWindow, 0, 50 ) 

you forgot to add " ) " Add some spaces in these codes because newbies won't even try to read the tutorial when they see these codes, honestly saying. :)

simple but useful @; Saml1er, not need of spaces.

Spaces aren't needed, but it makes the code look better and easier to read. That's why most scripters use a ton of spacing.

Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...