Jump to content

[REL] GUI Classes v1.0 (scripting GUI MADE EASY!)


50p

Recommended Posts

GUI Classes v1.0

Introduction:

The title speaks for itself. If you've ever had problems with making nice user interface for your gamemode or RPG server then this library is for you! Making "log in" window now is as easy as picking your nose!

Description:

I've read lots of topics where people were having problems with scripting GUI even after reading the scripting GUI tutorial on the wiki so I decided to make those GUI classes to make scripting GUI easier then ever before. Now, it's pretty easy to make almost anything you like even with a little knowledge of scripting. These classes have a few features that are missing in MTA, like "click'n'drag", you can click and drag the control where you want. You can make GridLists with a few lines (an example is included in "test.lua") not even using AddRow, AddColumn, etc.

Example:

This is a simple example of how to create an image and swap it over with a different image when a user clicks it:

addEventHandler( "onClientResourceStart", getResourceRootElement( getThisResource() ), 
    function( ) 
        -- lets make it in the middle of the screen 
        local screenSize = { guiGetScreenSize( ) }; 
        img = StaticImage:Create( screenSize[ 1 ] / 2 - 200, screenSize[ 2 ] / 2 - 200, 400, 400, "images/0.png" ); 
        img:AddOnClick( clickedAnImage ); 
        -- clickedAnImage function will be called when you click an image 
    end 
) 
  
local click_count = 0 
function clickedAnImage( ) 
    click_count = click_count + 1; 
    img:LoadImage( "images/" .. tostring( click_count % 2 ) .. ".png" ); 
end 

This is easy to make even without my classes but it shows you a new way of scripting. There is even more examples in "test.lua" file. They show how to create a "Log in" window, complex gridlists and dragable controls.

Documentation:

Currently, there is a short documentation that just tells you what functions are used to create specific controls. Because of that, I do expect people that would like to know a function for this, for that, etc. If you want to know what function is responsible for something, just ask here and I'll reply as soon as I see the post. HERE is work-in-progress documentation. You can find all the functions there but only a few are documented.

Download:

Click ME!

BUG REPORT:

If you find any bugs, and I'm 99% sure there will be a bug, just speak up! Don't keep it for yourself! I'll try to fix it and release a new version with the bug fixed.

Edited by Guest
Link to comment
  • 4 weeks later...

Why doesn't this work:

function createWindow( ) 
    myWindow = Window:Create( 0.02, 0.3, 0.6, 0.6, "Test", true ); 
    btnCls = myWindow:AddButton ( 0.1, 0.9, 0.2, 0.05, "Close", true ); 
    myWindow:Visible ( true ); 
    myWindow:Movable ( false ); 
    myWindow:Sizable ( false ); 
    showCursor( true ); 
    btnCls:AddOnClick( hide ); 
    img = StaticImage:Create( 0.3, 0.4, 0.3, 0.3, "image.png", false, myWindow ); 
end 
addEventHandler( "onPlayerJoin", getResourceRootElement( getThisResource() ), createWindow ); 
addEventHandler( "onClientResourceStart", getResourceRootElement( getThisResource() ), createWindow ); 
  
  
function hide( button ) 
     
        myWindow:Visible ( false ); 
        showCursor( false ); 
     
end 
addCommandHandler("close", hide) 
  
  
function show( ) 
    myWindow:Visible ( true ); 
    showCursor( true ); 
end 
addCommandHandler( "open", show ) 
  

Gui works, but the image wont show, and i want the image to be in the Window. What is wrong??

Link to comment

niemi, you can also use:

img = myWindow:AddStaticImage( .3, .4, .3, .3, "image.png", true ) 

Also, your code may not work even if you do what Talidan suggested because you pass a Window object (myWindow) where it should be a GUI element (myWindow.gui). Can be something like this:

img = StaticImage:Create( 0.3, 0.4, 0.3, 0.3, "image.png", true, myWindow.gui ); 

I'd suggest using AddStaticImage() method which sets image as a child of the window itself, so you don't have to worry about setting parent. That's why I made Add(StaticImage, Label, etc.) functions which simply add new gui element as a child.

Link to comment

Thanks Alexander for actually starting it... I don't know when I would start it myself but once you started I had an inspiration and made a lot of new pages, templated, etc.. even fully documented 3 methods :)

Link to comment

Your welcome. I will create some documentation also.

I hope that other poeple also create some pages!

I builded this morning a nice, and usefull function for this nice set of gui classes!

Much servers are using this trick, it is called fadeGui, it is not a standard mta function. But with a little help of a event you can create it.

But now it is availible for gui classes also!

I created it, and i might say im proud of my work!

To install it open up sharedfuncs_class.lua, and put somewhere in that file:

  
function GUISharedFuncs:Fade(fadeIn, maxAlpha) 
    maxAlpha = maxAlpha or 1.0 
    if(type(self) ~= "table") then 
        outputDebugString( "calling method incorrectly ( object.method() )! use object:method()", 1 ); 
        return false 
    end 
     
    fFadeIn = function() 
        currentAlpha = tonumber(string.format('%.2f', guiGetAlpha(self.gui))); 
         
        if(currentAlpha == maxAlpha) then 
            guiSetAlpha(self.gui, maxAlpha); 
            removeEventHandler("onClientRender", self.gui, fFadeIn); 
        else 
            guiSetAlpha(self.gui, currentAlpha+0.1); 
        end 
    end 
     
    fFadeOut = function() 
        currentAlpha = tonumber(string.format('%.2f', guiGetAlpha(self.gui))); 
         
        if(currentAlpha == 0.0) then 
            guiSetAlpha(self.gui, 0.0); -- just to be sure. 
            guiSetVisible(self.gui, false); 
            removeEventHandler("onClientRender", self.gui, fFadeOut); 
        else 
            guiSetAlpha(self.gui, currentAlpha-0.1); 
        end 
    end 
     
    if(type(fadeIn) == "boolean") then 
        self.maxAlpha = maxAlpha; 
     
        if fadeIn == true then 
            guiSetAlpha(self.gui, 0.0); 
            guiSetVisible(self.gui, true); 
            addEventHandler("onClientRender", self.gui, fFadeIn) 
        elseif(fadeIn == false) then 
            addEventHandler("onClientRender", self.gui, fFadeOut) 
        end 
    end 
     
    return "fading" 
end 
  

offcourse you can replace the whole file with the new function:

http://mta.pastebin.com/f6f81d402

To use it:

  
-- just a simple window 
wnd = Window:Create( 10, 200, 200, 110, "" ); 
wnd:Visible(false); -- offcourse you may not see the window  
  
-- then 
function toggleWnd(cmd) 
    if wnd:Visible() == false then 
        wnd:Fade(true, 0.8); -- Fade it in, and the max alpha is 0.8. If you don't fill out the alpha it will be 1.0 standard. 
    else 
        wnd:Fade(false); -- Max alpha is not important! 
    end 
end 
  
addCommandHandler('tw', toggleWnd); 
  

Syntax:

Object:Fade(bool fadeIn, Float maxAlpha = 1.0); 

Good luck with it, and have a beer for me :D

And now i going to unban my self from my private server. I banned the serial 0000-0000-0000-0000 for fun. But what happend my stupid server banned me, and not that serial. I just getting crazy. Or am i stupid to ban that serial. Hmmmm....

Alexander

Link to comment

That function isn't bad, but I'll combine arc_'s animation class for gui with mine so it will be possible to slide, fade in/out, resize (animated resizing), etc. For now you can use that function. I would not recommend using string.format() in onClientRender because that function is slow and may drop down FPS.

BTW, if you use DP2.3 and you never had a serial then your serial must be 0000-0000-0000-0000. If you banned that serial you banned yourself too.. that must have been "fun" for you. Anyway, I'm not sure how much time I can get to do more wiki stuff, but hopefully will do some today.

Link to comment
I need to use string.format. Order way i need to enter a bunch of numbers. And it won't end then.

And where can i find arc_'s class?

Edit

In GUIColection.lua on line 23 there is function called showCursor( true );

I think it need to be deleted?

Yeah, it shouldn't be there, I wrote it there to test the scripts. I'll remove it and release a new version with some small bugs fixed too.

The animation class is in some resource, can't remember which one though. I copied it looong time ago to my gamemode which was never released.

Link to comment
Yes it is, but the code it self. I think it is incomplete.

I think it's the one that I'm going to include. It may be not finished but fade in/out and sliding works fine.

And it works with DP2.x too, Mr.Hankey.

Link to comment
  • 5 months later...

Hello all,

I have a little modification for this:

Open the file window_class.lua

Find: (line: ~18)

function Window:Create( x, y, width, height, text, relative )

Replace width:

function Window:Create( x, y, width, height, text, relative )	
if y == nil or y == false and relative == false then
local scr = {guiGetScreenSize()};
if x == "centered" then
		x = (scr[0]/2)-(width/2);
		y = (scr[1]/2)-(height/2);
elseif x == "top-center" then
		x = (scr[0]/2)-(width/2);
		y = 0;
elseif x == " bottom-center" then
		x = (scr[0]/2)-(width/2);
		y = scr[1]-height;
end
end

What this does, is easy:

Centered window:

Window:Create("centered", nil, 500, 300, "login to our server!", false);

Window at the top of the screen centered:

Window:Create("top-center", nil, 500, 60, "Some information here.", false);

Window at the bottom of the screen centered:

Window:Create("bottom-center", nil, 500, 60, "Some information here.", false);

Offcourse you enter just the x and y values. Example:

Window:Create(50, 100, 500, 300, "Title",  false);

Now it is easy to create a centered window. And you don't need to calculate it on your own.

Alexander

Edited by Guest
Link to comment

Alexander, you can't have variable number of arguments if "..." is used as first parameter. It always MUST be at the end of parameters. I don't know how you could make it and didn't give you any errors.

... - this contains every next parameter passed to the function. So, if you use it as the first parameter the next parameters would never be used and therefore Lua would throw an error.

Besides, why do you create a new table of arguments and another one to get the number of arguments if arg (the table with parameters) exists when ... is used?

HERE is some Lua reference that might be useful for you.

Link to comment
Alexander, you can't have variable number of arguments if "..." is used as first parameter. It always MUST be at the end of parameters. I don't know how you could make it and didn't give you any errors.

... - this contains every next parameter passed to the function. So, if you use it as the first parameter the next parameters would never be used and therefore Lua would throw an error.

Besides, why do you create a new table of arguments and another one to get the number of arguments if arg (the table with parameters) exists when ... is used?

HERE is some Lua reference that might be useful for you.

I don't need that sorry.

But i havn't tested yet, but i saw it. My fault. Just created it without my head.

I edit my message.

And also

if var = "value" then

Wont work either :P that little mistake i got on two places

Link to comment
...

But i havn't tested yet, but i saw it. My fault. Just created it without my head.

...

Don't make things without your head because one day you'll wake up with an unexpected baby next to you.

Also, please post suggestions not the code because I'll probably won't use that code anyway.

Link to comment
...

But i havn't tested yet, but i saw it. My fault. Just created it without my head.

...

Don't make things without your head because one day you'll wake up with an unexpected baby next to you.

Also, please post suggestions not the code because I'll probably won't use that code anyway.

Maybe people will edit there file? So if you include it or not, i'll post the code, only for people who are interested!

And i love baby's! So that aint a problem also. Anymore problems?

Link to comment

One more... Find bugs in this library please :P No, seriously now, please make sure the code works before you post it because I don't want people here complaining about your code, you don't want it either do you? Anyway, I have to thank you for actually using it :)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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