Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Is it possible to use if commands when dealing with buttons?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DaBigOne




PostPosted: Sat Dec 28, 2013 1:04 pm   Post subject: Is it possible to use if commands when dealing with buttons?

What is it you are trying to achieve?
Is it possible to use if commands when dealing with buttons?


What is the problem you are having?
I drew 5 buttons with a for loop and array, and since they all share the same procedure, I was wondering whether if commands could be used to distinguish one button from another.


Describe what you have tried to solve this problem
I searched through the Turing help, and Google.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)


Turing:


import GUI

var name : array 1 .. 5 of string := init ("hippo", "lion", "cat", "tiger", "dinosaur")

proc hi
    put "hi"
    %is it possible to say:%

    %if button1 is pressed then
    %put "hi"
    %if button2 is pressed then
    %put "hello"
    %end if
end hi

for i : 1 .. 5
    var button : int := GUI.CreateButtonFull (i * 100, 50, 0, name (i), hi, 0, KEY_ENTER, false)
end for

loop
    exit when GUI.ProcessEvent
end loop



Please specify what version of Turing you are using
4.11
Sponsor
Sponsor
Sponsor
sponsor
DaBigOne




PostPosted: Sat Dec 28, 2013 1:39 pm   Post subject: RE:Is it possible to use if commands when dealing with buttons?

I forgot to mention, the buttons must be displayed in a random order, so I don?t know how to do this if the buttons are placed in a random order.
DaBigOne




PostPosted: Sat Dec 28, 2013 5:15 pm   Post subject: RE:Is it possible to use if commands when dealing with buttons?

bump
Raknarg




PostPosted: Sat Dec 28, 2013 8:13 pm   Post subject: RE:Is it possible to use if commands when dealing with buttons?

If you're willing to wait for a bit, I'll look into it in a couple hours (if no one answers)
DaBigOne




PostPosted: Sat Dec 28, 2013 11:40 pm   Post subject: RE:Is it possible to use if commands when dealing with buttons?

That would be appreciated, thanks.
Raknarg




PostPosted: Sun Dec 29, 2013 12:23 am   Post subject: RE:Is it possible to use if commands when dealing with buttons?

Ok. As I see it, there's two things you could do:

1) Make your own buttons, which you would have some more access too. If you decide this route, I can provide you with a button method (which I made) that looks a little different and teach you how to use it

2) Use arrays of procedures, which I will show you.

Turing:

var buttonProc : array 1 .. 5 of proc x % the name doesn't matter. You can say proc x, y, hallelujah, whatever.

proc hi1
    put "Proc 1"
end hi1
proc hi2
    put "Proc 2"
end hi2
proc hi3
    put "Proc 3"
end hi3
proc hi4
    put "Proc 4"
end hi4
proc hi5
    put "Proc 5"
end hi5

buttonProc (1) := hi1
buttonProc (2) := hi2
buttonProc (3) := hi3
buttonProc (4) := hi4
buttonProc (5) := hi5

for i : 1 .. 5
    % make a gui button, but instead of hi like before, you can use buttonProc(i)
end for


This is a neat trick that a lot of turing users don't know about. Variables can be procedures, as well as functions. So in out case, if you put buttonProc(1), the procedure hi1 would run. Neat huh? This is what the GUI function is doing. You can put in a procedure as a parameter because variables can be procedures. In the GUI module, there is a button which now has a field of whatever procedure you gave it.
DaBigOne




PostPosted: Sun Dec 29, 2013 1:21 pm   Post subject: Re: Is it possible to use if commands when dealing with buttons?

Thanks for the help.

When you mean use your own buttons, I assume you mean with mouse where, but the thing is the buttons must be placed in a random order, so I don?t think mousewhere would be able to work, unless I?m missing something huge.

The array of procedures is a great idea, but I?m having trouble with the randomizing of the button order. I tried modifying your code to randomize the button order, but the buttons repeat, and I don?t know how to stop this.

Here?s the modified code:

import GUI

var buttonProc : array 1 .. 5 of proc x
var buttons : array 1 .. 5 of int
var name : array 1 .. 5 of string := init ("1", "2", "3", "4", "5")
var randNum : int

proc hi1
put "Proc 1"
end hi1
proc hi2
put "Proc 2"
end hi2
proc hi3
put "Proc 3"
end hi3
proc hi4
put "Proc 4"
end hi4
proc hi5
put "Proc 5"
end hi5

buttonProc (1) := hi1
buttonProc (2) := hi2
buttonProc (3) := hi3
buttonProc (4) := hi4
buttonProc (5) := hi5

for i : 1 .. 5
randNum := Rand.Int (1, i)
buttons (i) := GUI.CreateButtonFull (i * 40, 200, 0, name (randNum), buttonProc (randNum), 0, KEY_ENTER, false)
name (randNum) := name (i)
buttonProc (randNum) := buttonProc (i)
end for

loop
exit when GUI.ProcessEvent
end loop
Raknarg




PostPosted: Sun Dec 29, 2013 10:40 pm   Post subject: RE:Is it possible to use if commands when dealing with buttons?

You could definitely mae your own buttons with mousewhere. That is in fact how I do it. All you need to do is basically keep track of where you want the button to be. After that, you use mousewhere to see it the button is inside or clicking or all that. If I showed you it would likely make more sense.

How many buttons are you going to have? If it's only a few, you could keep track of the numbers you have let in a flexible array or use a string. Or you can just randomly sort your array.
Sponsor
Sponsor
Sponsor
sponsor
DaBigOne




PostPosted: Sun Dec 29, 2013 11:04 pm   Post subject: Re: Is it possible to use if commands when dealing with buttons?

Our teacher already covered mouse where, so I?m pretty familiar with it. The thing is, I don?t really want the code to be longer then it should be, because our teacher marks for elegance/simplicity of code, and so I kind of want to stay away from mousewhere. Also, I have to make 10 buttons, which is why I don?t really want to use mousewhere.

Do you have any idea on how to fix the buttons repeating in the example you gave me that I modified?

Oh, and this question probably should go in a new topic, but when you assign a variable an image, is it possible for Turing to return the image name?
This would be really useful if I wanted to compare the image name with something else.

Thanks
DaBigOne
Raknarg




PostPosted: Sun Dec 29, 2013 11:36 pm   Post subject: RE:Is it possible to use if commands when dealing with buttons?

If you used the method I showed you and you gave me credit for it, you would be allowed to use it. My buttons are as simple as they come. Sometimes there's no way to get around having a bunch of code.

Anyways, Like I said, you just have to create a list that you take elements out of (like a string or a flexible array. If your random number is 2, take 2 out of the list and do it again). Or you could randomly sort your list, and you would have no repetition. Here's a program that randomly puts out 10 numbers without repetition:

Turing:

%This takes in a list of unknown size of integers, whose values you may change
proc shuffle (var list : array 1 .. * of int)
    for i : 1 .. upper (list) ** 2
        %This picks two random items in the list and swaps them around
        var rand1 := Rand.Int (1, upper (list))
        var rand2 := Rand.Int (1, upper (list))
        var temp := list (rand1)
        list (rand1) := list (rand2)
        list (rand2) := temp
    end for
end shuffle

var list : array 1 .. 10 of int

for i : 1 .. upper (list)
    list (i) := i
end for

shuffle (list)

for i : 1 .. upper (list)
    put list (i)
end for
DaBigOne




PostPosted: Mon Dec 30, 2013 2:27 pm   Post subject: RE:Is it possible to use if commands when dealing with buttons?

Oh OK, I get it now. Thanks.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: