
-----------------------------------
richcash
Wed Jun 14, 2006 6:45 pm

GUI buttons
-----------------------------------
If I make a button and call to a procedure with a parameter it doesn't work. For example :
var button := GUI.CreateButton (0, 0, 0, "button", do (1))
Why can't I do this?

-----------------------------------
Tony
Wed Jun 14, 2006 6:51 pm


-----------------------------------
GUI module passes a reference to the function, doesn't actually make a call to the function.

-----------------------------------
Ninja
Wed Jun 14, 2006 6:55 pm


-----------------------------------
GUI module passes a reference to the function, doesn't actually make a call to the function.

Yep. for example look at this code, this is from the turing manual:

import GUI
        
        procedure DrawRandomCircle
            var r : int := Rand.Int (20, 50)
            var x : int := Rand.Int (r, maxx - r)
            var y : int := Rand.Int (r, maxy - r)
            var c : int := Rand.Int (0, maxcolor)
            Draw.FillOval (x, y, r, r, c)
            % In case we drew over the buttons, redraw them.
            GUI.Refresh
        end DrawRandomCircle
        
        View.Set ("graphics:300;200,nobuttonbar ")
        var draw : int := GUI.CreateButtonFull (50, 10, 0, "Draw Circle",
            DrawRandomCircle, 0, '^D', true)
        var quitBtn : int := GUI.CreateButton (200, 10, 0, "Quit", GUI.Quit)
        loop
            exit when GUI.ProcessEvent
        end loop
when you are making a button using the GUI module, you have to specify what function to pass reference to. In this program the first button is passing reference to the DrawRandomCircle procedure, but the other one uses the built in function the GUI.Quit for which you dont have to write a separate procedure.

-----------------------------------
richcash
Wed Jun 14, 2006 7:02 pm


-----------------------------------
Oh yeah, that's right, thx. That's kind of inconvenient, but so is the whole GUI module!

-----------------------------------
Tony
Wed Jun 14, 2006 7:06 pm


-----------------------------------
you could always hack away at the GUI module's code to get it to work for you. Remember that you'd need to include all of the files with source to compile properly.

What are you trying to do anyways? It sounds like you're trying to generate a load of buttons inside some loop.

-----------------------------------
richcash
Wed Jun 14, 2006 8:44 pm


-----------------------------------
Oh, I just wanted the button to call my draw procedure for a card game but I wanted to make the same procedure for both the cpu and the user. So, draw (1) gives the user a new card in his hand, and draw (2) gives the computer a new card. I only want the 'Draw' button I create to give the user a card. Obviously I can work around it, I just like doing things really efficiently!

-----------------------------------
Tony
Wed Jun 14, 2006 8:52 pm


-----------------------------------
what you can do is set up a mini-function for the button

var button := GUI.CreateButton (0, 0, 0, "button", mini_fnc() )

where

func mini_fnc()
   %call draw on user's behalf
   draw(1)
end mini_fnc


-----------------------------------
richcash
Wed Jun 14, 2006 8:59 pm


-----------------------------------
Could you further explain this technique because I know you can't call functions as the action for a button? Or did you mean to make mini_fcn a proc?

-----------------------------------
Tony
Wed Jun 14, 2006 9:02 pm


-----------------------------------
a procedure is a void function (does not return results). And yes, I meant a procedure.

-----------------------------------
richcash
Wed Jun 14, 2006 9:07 pm


-----------------------------------
Yeah, I know a procedure is really a function. I never thought of this, just calling my proc with parameters in another one for the button. Thanks for the help!!!
