TGC Codebase Backup



Simple buttons by SunDawg

22nd Jun 2007 0:32
Summary

Simple buttons that are easy to code.



Description

This is a short program showcasing how to make a 3 state button. Those states being normal, hover, and pressed. With this code, you can easily see how to tell if the button is pressed, how to change the button when pressed, and how to abort the buttons action if the mouse is released off of the button. Just like any other button in linux or windows!



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    global clicked
global hover
global press

cls 

while clicked<>1
    ink RGB(0, 0, 255),RGB(0, 0, 160)
    box screen width()/2-50, screen height()/2-12, screen width()/2+50, screen height()/2+12
    
    if mousein(screen width()/2-50, screen width()/2+50, screen height()/2-12, screen height()/2+12)=1
        ink RGB(255,0,0),RGB(0, 0, 160)
        box screen width()/2-48, screen height()/2-10, screen width()/2+48, screen height()/2+10
    endif            
    if mousein(screen width()/2-50, screen width()/2+50, screen height()/2-12, screen height()/2+12)=0
        ink RGB(0, 128, 255),RGB(0, 0, 160)
        box screen width()/2-48, screen height()/2-10, screen width()/2+48, screen height()/2+10
    endif
    if mousein(screen width()/2-50, screen width()/2+50, screen height()/2-12, screen height()/2+12)=1
        if mouseclick()=1
            while mouseclick()=1
                ink RGB(255, 255, 0),RGB(0, 0, 160)
                box screen width()/2-48, screen height()/2-10, screen width()/2+48, screen height()/2+10
                ink RGB(0, 0, 0),RGB(0,0,160)
                center text screen width()/2, screen height()/2-7, "Ok"
            endwhile
            if mousein(screen width()/2-50, screen width()/2+50, screen height()/2-12, screen height()/2+12)=1
                clicked=1
            endif
        endif
    endif
    ink RGB(0,0,0), RGB(0, 0, 160)
    center text screen width()/2, screen height()/2-8, "Ok"
endwhile

end

function mousein(left,right,top,bottom)
    hover=0
    if mousex()>left then if mousex()<right then if mousey()>top then if mousey()<bottom then hover=1
endfunction hover