
-----------------------------------
Nick
Sun Nov 25, 2007 11:35 pm

button GUI
-----------------------------------
well i got bored again (shocker) so i decieded to recreate turing's GUI button module thingy inspired by a recent project Dan had to do that he explained over the IRC channel (although mines not as elborate as his was nor was in the same language)

heres the code feel free to modify it but if you do please post the source (also the module cannot handle procedures with parameters atm):%unit
module CreateButton
    export DoGUI, CreateBut
    var but : boolean := false
    var procToDO : procedure x
    % proc putHi
    %     put "hi"   /*test procedure*/
    % end putHi
    proc checkBut (butx1, buty1, butx2, buty2 : int, procToDo : procedure x)
        var x, y, b : int
        mousewhere (x, y, b)
        if (x > butx1 and x < butx2) and (y > buty1 and y < buty2) and b > 0 then
            procToDo
        end if
    end checkBut

    function DoGUI : boolean
        checkBut (150, 150, 200, 200, procToDO)
        result false
    end DoGUI

    proc CreateBut (butx1, buty1, butx2, buty2 : int, procToDo : procedure x)
        but := true
        procToDO := procToDo
        drawfillbox (butx1, buty1, butx2, buty2, black)
    end CreateBut

    % createBut (150, 150, 200, 200, putHi)
    % loop
    %     exit when doGUI       /*testing of the button*/
    % end loop
end CreateButton

proc testProc
    put "Yo"
end testProc

CreateButton.CreateBut (150, 150, 200, 200, testProc)
loop
    exit when CreateButton.DoGUI
end loop

as you can see there are some commented parts
these were just me testing it out

-----------------------------------
Tony
Sun Nov 25, 2007 11:42 pm

RE:button GUI
-----------------------------------
It's a good start, but the setup is hardcoded to work just for a single button at a fixed location (DoGUI checks for a fixed position click, reguardless of where the button is actually drawn (!) ).

The next step would be to generalize this to move the button around, and have more than one button.

-----------------------------------
Nick
Sun Nov 25, 2007 11:44 pm

RE:button GUI
-----------------------------------
thanks for the post ill get on that as i am still bored (mythbusters helps a little :P)

-----------------------------------
Nick
Mon Nov 26, 2007 12:02 am

RE:button GUI
-----------------------------------
ok im back and hows this?
module CreateButton
    export DoGUI, CreateBut
    var but : boolean := false
    type buttonProperties :
        record
            x, y : array 1 .. 2 of int
            procToDo : procedure x
        end record
    var b : flexible array 1 .. 0 of ^buttonProperties
    var buttonCount : int := 0

    proc checkBut ( /*butx1, buty1, butx2, buty2*/
            whichBut : int, procToDo : procedure x)
        var mx, my, mb : int
        mousewhere (mx, my, mb)
        if (mx > b (whichBut) -> x (1) and mx < b (whichBut) -> x (2))
                and (my > b (whichBut) -> y (1) and my < b (whichBut) -> y (2))
                and mb > 0 then
            b (whichBut) -> procToDo
        end if
    end checkBut

    function DoGUI : boolean
        for i : 1 .. buttonCount
            checkBut ( /*b (i) -> x (1), b (i) -> y (1), b (i) -> x (2), b (i) -> y (2),*/
                i, b (i) -> procToDo)
        end for
        result false
    end DoGUI

    proc CreateBut (butx1, buty1, butx2, buty2 : int, procToDo : procedure x)
        but := true
        new b, upper (b) + 1
        new b (upper (b))
        buttonCount += 1
        b (buttonCount) -> x (1) := butx1
        b (buttonCount) -> y (1) := buty1
        b (buttonCount) -> x (2) := butx2
        b (buttonCount) -> y (2) := buty2
        b (buttonCount) -> procToDo := procToDo
        drawfillbox (b (buttonCount) -> x (1), b (buttonCount) -> y (1),
            b (buttonCount) -> x (2), b (buttonCount) -> y (2), black)
    end CreateBut
end CreateButton

proc testProc
    put "Yo"
end testProc
proc testProc2
    put "Yo2"
end testProc2

CreateButton.CreateBut (150, 150, 200, 200, testProc)
CreateButton.CreateBut (100, 100, 125, 125, testProc2)
loop
    exit when CreateButton.DoGUI
end loop



whats next:
images, black boxes arent that great
text, for when images arent needed

-----------------------------------
Tony
Mon Nov 26, 2007 12:13 am

RE:button GUI
-----------------------------------
Looking good. You can either expend to have more different elements, or add new features to the button. Having different visuals for mouse-over, mouse-down, mouse-up states could be interesting.

That would require you to redraw all of your GUI setup, each time through the DoGUI loop.

-----------------------------------
Saad
Mon Nov 26, 2007 3:42 pm

RE:button GUI
-----------------------------------
This screams out to me saying make a button class, and a module which takes care of the buttons.

Still looks good

-----------------------------------
Clayton
Mon Nov 26, 2007 4:02 pm

RE:button GUI
-----------------------------------
Hmm... you're handling actions like Turing does in it's predefined GUI module. Using action procedures isn't necessarily a bad thing, in other languages. The issue with Turing is that it forces you to use procedures (not even functions) that have no parameters. This can be a very clunky thing when you need a button to point to a procedure/function with parameters. In [url=http://compsci.ca/v3/viewtopic.php?t=14410]my old button class, I did things a bit differently. You may want to check that out for inspiration. Also, your DoGUI method seems to be doing something.... odd. After pressing your black square  (that I assume to be a button), it just loops indefinitely, not good. You should probably check into that.

-----------------------------------
Nick
Mon Nov 26, 2007 4:06 pm

RE:button GUI
-----------------------------------
hmm thanks clayton i never thought of that :P 
also i got the images done (it never was that hard im just a little lazy)
edit:
also Clayton if the user wants to he could do the following:
function realFunction(x,y:int)
result x+y
end realFunction
proc hold
put realFunction
end hold
CreateButton.CreateBut(x1,y1,x2,y2,hold)

but that sometimes gets annoying and uses useless code

-----------------------------------
Clayton
Mon Nov 26, 2007 4:33 pm

RE:button GUI
-----------------------------------
That's the point. Putting in useless code is an ugly workaround. I've been thinking of a way to make this work lately, and I think I have an idea of how to do it.
