CLEAR
_TITLE "winbutt1.1"
SCREEN _NEWIMAGE(1600, 900, 32)
_FULLSCREEN _STRETCH
DIM SHARED rgui$
DIM SHARED clickname$
DIM SHARED tbox$
DIM SHARED cpos
DIM SHARED r&

_PRINTMODE _KEEPBACKGROUND
CLS , _RGB32(100, 200, 200)
'r& = _LOADFONT("fonts\abduction2002.ttf", 15, "")

'winbutt01.bas winbutt version 1. create buttons, recieve the clicked button name from the mouse handler. draw windows too.
'Amon Rudolph 2015
'It's not that great of a program, but it's good enough for me.
'syntax for guiaddbutton routine-

'guiaddbutton ("name","text or image file",h,v,x,y,m)
'name is the object name put in clickname$ when it's clicked. h,v,x,y are the box coordinates.
'm is either 0 or 1. 0 makes a text button, using the text for the button text. 1 uses the text to load the image file specified.

'guiwindow ("name",h,v,x,y,m)
'name is the window name, h,v,x,y are the window coordinates, m is currently meaningless, but was intended to change features like buttons or no buttons etc.

'ok so the flow of the program; lets say if you want a button in your program, you would "call guiaddbutton ("Button1","Text or filename of image",h,v,x,y,m)"
' if m=0 then it makes a text button with the text. if m=1 then it makes an image button using the text as the filename.
'next, do "call guirender" to draw the button(s)
'Then, do "call handlemouse" to pass control to the mouse routine.
'When control returns from the mouse routine, clickname$ will equal the name of the button, the text you put in the name field when using guiaddbutton.
'
'there is also a window sub, "guiwindow". The format is call guiwindow("name",h,v,x,y,m)
'name is the name of the window, and the text in the title bar.  h,v,x,y are the screen coordinates, inside of the window. m is for future use, leave as 0.
'the sub guiwindow also creates 3 buttons on your behalf, and will return the data to clickname$ just like other buttons.
'they are the name of the window + "-" + minimize, restore, or close, depending on what was clicked.
'for eg, if you make a window called 'hello' and close is clicked, clickname$ would = "hello-close"
'
'
'Oh, and to start a new screen with different buttons, just use rgui$="" then clear the screen and start over.
'
'in short, to get started using buttons, use guiaddbutton to add your buttons, then use guirender to draw them, then call handlemouse to wait for input.
' the following is a tiny button demo, just remove the remark symbols

myprogram:
'this will add a button to the memory string and run mouse routine
'many many buttons can be added.  Remember buttons can be images too.
CALL guiaddbutton("button1", "Buttons!!!", 50, 50, 150, 70, 0)
CALL guiaddbutton("Down", "icons\1uparrow.png", 50, 150, 150, 250, 1)

'render a bunch of buttons:
FOR x = 10 TO 1500 STEP 100
    i = i + 1
    CALL guiaddbutton(STR$(i), STR$(i), x, 300, x + 50, 325, 0)
NEXT


CALL guirender
CALL handlemouse
_PRINTSTRING (10, 30), clickname$










SUB handlemouse
    _MOUSESHOW
    mouser:
    xx$ = INKEY$
    IF xx$ = "q" THEN END
    xNNOTHING = _MOUSEINPUT
    mh = _MOUSEX
    mv = _MOUSEY
    mlb = _MOUSEBUTTON(1)
    mrb = _MOUSEBUTTON(2)
    IF mlb = 0 THEN GOTO mouser
    IF mlb = -1 THEN CALL mouseclick(mh, mv)
    'IF mrb = -1 THEN mousestatus$ = "RC":  ' not implemented, but you could make it do something...
    es:
END SUB


SUB box3d (h, v, x, y, s)
    'routine for drawing a 3d box for buttons etc.
    'h,v,x,y are screen coordinates, s is 0 normal 1 is pressed
    'unlike window, 3d box will be drawn inside the coordinates
    IF s = 0 THEN GOTO normal
    IF s = 1 THEN GOTO pressed
    normal:
    LINE (h, v)-(x, v), _RGB(255, 255, 255) 'light on top left hand part
    LINE (h, v)-(h, y), _RGB(255, 255, 255)
    LINE (h + 1, v + 1)-(x, v + 1), _RGB(190, 190, 190) 'grey now
    LINE (h + 1, v + 1)-(h + 1, y - 1), _RGB(190, 190, 190)
    'now bottom 2 rows going from black, to grey, to normal box grey
    LINE (x, v)-(x, y), _RGB(0, 0, 0) 'black for outer bottom right
    LINE (h, y)-(x, y), _RGB(0, 0, 0)
    LINE (h + 1, y - 1)-(x - 1, y - 1), _RGB(100, 100, 100)
    LINE (x - 1, v + 1)-(x - 1, y - 1), _RGB(100, 100, 100)
    LINE (h + 2, v + 2)-(x - 2, y - 2), _RGB(150, 150, 150), BF
    GOTO donerender
    pressed:
    LINE (h, v)-(x, v), _RGB(0, 0, 0) 'light on top left hand part
    LINE (h, v)-(h, y), _RGB(0, 0, 0)
    LINE (h + 1, v + 1)-(x, v + 1), _RGB(100, 100, 100) 'grey now
    LINE (h + 1, v + 1)-(h + 1, y - 1), _RGB(100, 100, 100)
    'now bottom 2 rows going from black, to grey, to normal box grey
    LINE (x, v)-(x, y), _RGB(255, 255, 255) 'black for outer bottom right
    LINE (h, y)-(x, y), _RGB(255, 255, 255)
    LINE (h + 1, y - 1)-(x - 1, y - 1), _RGB(190, 190, 190)
    LINE (x - 1, v + 1)-(x - 1, y - 1), _RGB(190, 190, 190)
    LINE (h + 2, v + 2)-(x - 2, y - 2), _RGB(150, 150, 150), BF
    donerender:
END SUB



SUB guiaddbutton (n$, t$, h, v, x, y, m) 'n$ is still the button name, t$ is now either the text, if m=0 or image filename if m=1
    'We'll start with a clickable button being 0. n$ is the name you choose for the button, as in 'button1' or whatever. t$ is the text on the button. h,v,x,y are the coordinates.
    c$ = "buttonog" + "|" + n$ + "|" + t$ + "|" + STR$(h) + "|" + STR$(v) + "|" + STR$(x) + "|" + STR$(y) + "|" + STR$(m) + "|" + CHR$(13) 'creates a a string like 'button1,Exit,10,10,90,25 <CR>'
    rgui$ = rgui$ + c$ 'add the button item to the GUI rendering string.
END SUB


SUB guirender ()
    'loop to go through the rgui$ to render stuff.
    cpos = 1
    ncommand:
    IF cpos = 0 THEN cpos = 1
    IF LEN(rgui$) < cpos + 8 THEN GOTO doneren ' if theres no other command in the list, thats all that needs to be processed.
    i$ = MID$(rgui$, cpos, 8)
    IF i$ <> "buttonog" THEN PRINT "Error object not available:"; i$: END
    'pull n$,t$,h,v,x,y from this position in rgui$
    q$ = ""
    cpos = cpos + 9
    nloader: 'load name string into n$ until the seperator | is reached.
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO tloader
    n$ = n$ + q$
    cpos = cpos + 1
    GOTO nloader
    tloader:
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO hloader
    t$ = t$ + q$
    cpos = cpos + 1
    GOTO tloader
    hloader:
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO vloader
    IF q$ = " " THEN q$ = ""
    h$ = h$ + q$
    cpos = cpos + 1
    GOTO hloader
    vloader:
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO xloader
    IF q$ = " " THEN q$ = ""
    v$ = v$ + q$
    cpos = cpos + 1
    GOTO vloader
    xloader:
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO yloader
    IF q$ = " " THEN q$ = ""
    x$ = x$ + q$
    cpos = cpos + 1
    GOTO xloader
    yloader:
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO mloader
    IF q$ = " " THEN q$ = ""
    y$ = y$ + q$
    cpos = cpos + 1
    GOTO yloader
    mloader:
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO finishedload
    IF q$ = " " THEN q$ = ""
    m$ = m$ + q$
    cpos = cpos + 1
    GOTO mloader

    finishedload:
    h = VAL(h$)
    v = VAL(v$)
    x = VAL(x$)
    y = VAL(y$)
    m = VAL(m$)
    cpos = cpos + 1
    IF m = 0 THEN CALL box3d(h, v, x, y, 0)
    IF m = 1 THEN CALL box3d(h, v, x, y, 0)
    IF m = 3 THEN CALL border(h - 3, v, x, y)
    '  _FONT r&
    IF m = 3 THEN COLOR _RGB(255, 255, 255): LINE (h - 3, v)-(x, y), , BF
    IF m = 3 THEN COLOR _RGB(0, 0, 0): _PRINTSTRING (h, v), t$
    COLOR _RGB(0, 0, 0)
    IF m = 0 THEN _PRINTSTRING (h + 4, v + 4), t$
    IF m = 1 THEN i& = _LOADIMAGE(t$, 32)
    IF m = 1 THEN _PUTIMAGE (h + 3, v + 3)-(x - 3, y - 3), i&: _FREEIMAGE i&
    n$ = ""
    t$ = ""
    q$ = ""
    h$ = ""
    v$ = ""
    x$ = ""
    y$ = ""
    m$ = ""
    GOTO ncommand
    doneren:
END SUB


SUB mouseclick (mh, mv)
    '   _FONT r&
    cpos = 0
    'same as render sub, except we only draw the button that is pushed, if there is one.
    ncommand:
    IF cpos = 0 THEN cpos = 1
    IF LEN(rgui$) < cpos + 8 THEN GOTO doneren ' if theres no other command in the list, thats all that needs to be processed.
    i$ = MID$(rgui$, cpos, 8)
    IF i$ <> "buttonog" THEN PRINT "Error object not available:"; i$: END
    q$ = ""
    cpos = cpos + 9
    nloader: 'load name string into n$ until the seperator | is reached.
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO tloader
    n$ = n$ + q$
    cpos = cpos + 1
    GOTO nloader
    tloader:
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO hloader
    t$ = t$ + q$
    cpos = cpos + 1
    GOTO tloader
    hloader:
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO vloader
    IF q$ = " " THEN q$ = ""
    h$ = h$ + q$
    cpos = cpos + 1
    GOTO hloader
    vloader:
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO xloader
    IF q$ = " " THEN q$ = ""
    v$ = v$ + q$
    cpos = cpos + 1
    GOTO vloader
    xloader:
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO yloader
    IF q$ = " " THEN q$ = ""
    x$ = x$ + q$
    cpos = cpos + 1
    GOTO xloader
    yloader:
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO mloader
    IF q$ = " " THEN q$ = ""
    y$ = y$ + q$
    cpos = cpos + 1
    GOTO yloader
    mloader:
    q$ = MID$(rgui$, cpos, 1)
    IF q$ = "|" THEN q$ = "": cpos = cpos + 1: GOTO finishedload
    IF q$ = " " THEN q$ = ""
    m$ = m$ + q$
    cpos = cpos + 1
    GOTO mloader
    finishedload:
    h = VAL(h$)
    v = VAL(v$)
    x = VAL(x$)
    y = VAL(y$)
    m = VAL(m$)
    cpos = cpos + 1
    butval = 0
    IF mh > h AND mh < x AND mv > v AND mv < y THEN butval = 1
    IF butval = 0 THEN GOTO nbutval
    IF m < 2 AND m > -1 AND butval = 1 THEN CALL box3d(h, v, x, y, 1)
    IF butval = 1 THEN clickname$ = n$
    COLOR _RGB(0, 0, 0)
    '  _FONT r&
    IF m = 0 THEN _PRINTSTRING (h + 5, v + 5), t$ ': butval = 0
    IF m = 1 THEN i& = _LOADIMAGE(t$, 32)
    IF m = 1 THEN _PUTIMAGE (h + 4, v + 4)-(x - 2, y - 2), i&: _FREEIMAGE i&
    GOTO doneren
    nbutval:
    n$ = ""
    t$ = ""
    q$ = ""
    h$ = ""
    v$ = ""
    x$ = ""
    y$ = ""
    m$ = ""
    GOTO ncommand
    doneren:
    FOR w = 0 TO 100
        _DELAY 0.00125
        waste = _MOUSEINPUT
    NEXT
    clickname$ = ""
    IF butval = 1 AND m < 2 AND m > -1 THEN CALL box3d(h, v, x, y, 0)
    IF butval = 1 THEN clickname$ = n$
    COLOR _RGB(0, 0, 0)
    IF m = 0 THEN _PRINTSTRING (h + 4, v + 4), t$ ': butval = 0
    IF m = 1 THEN i& = _LOADIMAGE(t$, 32)
    IF clickname$ > "" AND m = 1 THEN _PUTIMAGE (h + 3, v + 3)-(x - 3, y - 3), i&: _FREEIMAGE i&
END SUB





SUB guiwindow (title$, h, v, x, y)
    LINE (h, v)-(x, y), _RGB(255, 255, 255), BF 'draw window background
    LINE (h, v - 25)-(x, v - 2), _RGB(50, 50, 255), BF 'title bar background
    COLOR _RGB(255, 255, 255) 'set color for title bar text.
    _PRINTSTRING (h + 4, v - 20), title$ 'draw title bar text
    LINE (h, v - 1)-(x, v - 1), _RGB(0, 0, 0) 'rule under title bar
    LINE (h - 1, v - 26)-(x + 1, y + 1), _RGB(0, 0, 0), B
    LINE (h - 2, v - 27)-(x + 2, y + 2), _RGB(100, 100, 100), B
    LINE (h - 3, v - 28)-(x + 3, y + 3), _RGB(190, 190, 190), B
    LINE (h - 4, v - 29)-(x + 4, y + 4), _RGB(255, 255, 255), B
    LINE (h - 5, v - 30)-(x + 5, y + 5), _RGB(190, 190, 190), B
    LINE (h - 6, v - 31)-(x + 6, y + 6), _RGB(100, 100, 100), B
    LINE (h - 7, v - 32)-(x + 7, y + 7), _RGB(0, 0, 0), B
    CALL guiaddbutton(title$ + "-close", "X", x - 20, v - 23, x - 2, v - 4, 0) 'draw close "X" box
    CALL guiaddbutton(title$ + "-restore", "^", x - 44, v - 23, x - 26, v - 4, 0) 'draw "maximize / restore" box
    CALL guiaddbutton(title$ + "-minimize", "_", x - 63, v - 23, x - 45, v - 4, 0) 'draw minimize button
END SUB

SUB border (h, v, x, y)
    LINE (h - 1, v - 1)-(x + 1, y + 1), _RGB(0, 0, 0), B
    LINE (h - 2, v - 2)-(x + 2, y + 2), _RGB(100, 100, 100), B
    LINE (h - 3, v - 3)-(x + 3, y + 3), _RGB(255, 255, 255), B
    LINE (h - 4, v - 4)-(x + 4, y + 4), _RGB(100, 100, 100), B
    LINE (h - 5, v - 5)-(x + 5, y + 5), _RGB(0, 0, 0), B
END SUB




