Page 1 of 1

Button names created in a loop

Posted: Sun Oct 15, 2017 2:49 am
by rbytes
I am trying to create a 10 x 10 matrix of buttons. Is there a way to give a button a name generated mathematically within a loop, the way it can be done in SB?

Code: Select all

set buttons custom
draw color 0,0,0

FOR x = 1 TO 10
  FOR y = 1 TO 10
    BUTTON btn TEXT str$(x+(y-1)*10) AT x*65,y*65 SIZE 60,60
    btn = btn+1
  NEXT y
NEXT x

Re: Button names created in a loop

Posted: Sun Oct 15, 2017 6:02 pm
by Mr. Kibernetik
Thank you for your information.
Seems there is a bug preventing doing this.

Re: Button names created in a loop

Posted: Sun Oct 15, 2017 6:11 pm
by rbytes
Here is some code I created to try to name and display buttons dynamically. It doesn't work correctly, so I have modified the code to draw button-shaped rectangles where I hoped to generate buttons. The button 'temp' only appears once, as button 100. It does not take its name from the value of the variable temp, but from the letters 'temp'. So all my code does is create the same button 100 times!

temp can be repeatedly defined alternately as a variable and an object (button), as can be seen when I output its value. If I move the output statement to immediately after the line that creates the button, 'temp = q', the program ends with the error Wrong Object Type. I would expect that, but I would also expect the same error on the next loop iteration when I define the button object name 'temp' again as a numeric variable.

Code: Select all

''
Drawbuttons by rbytes
October 2017
''
' hide the control menu - screenwidth increases by 48 points
#.scrview(#.normal)

' get screen size and calculate scaling for device
x,y = #.scrsize()
rw = x/1280 ; rh = y/813

s = 1
u = 0

' create button matrix

' define template button and set common parameters
q=#.button
q.width = 50
q.height = 50

' create 100 buttons in a 10 x 10 matrix
> t, 1..100

  ' name temp as the number t
  temp = #.str(t)
  #.output(temp)
  
  ' create button from temp using button template
  temp = q

  ' position the button
  temp.x = 150 + s*60
  temp.y = 120 + u*60
  #.show(temp)

  ' draw rectangles where the buttons should be
  #.drawrect(temp.x,temp.y,temp.x+50,temp.y+50)

s += 1
  ? s > 10
    s = 1
    u += 1
  .
<

Re: Button names created in a loop

Posted: Sun Oct 15, 2017 6:12 pm
by Mr. Kibernetik
Yes, there is a bug preventing creating interface objects in a loop.

Re: Button names created in a loop

Posted: Sun Oct 15, 2017 9:58 pm
by Mr. Kibernetik
This is an example of objects array, created in a loop:

Code: Select all

> i, 1..5
  a[i] = #.button
  b = ~a[i]
  b.y = i*40
  b.text = "Button "+i
  #.show(b)
<
>
  > i, 1..#.size(a,1)
    b = ~a[i]
    ? #.act(b), #.output(a[i].text)
  <
<
But it will work properly only after an upcoming update.

Re: Button names created in a loop

Posted: Sun Oct 15, 2017 10:08 pm
by Mr. Kibernetik
rbytes wrote:
Sun Oct 15, 2017 6:11 pm
Here is some code I created to try to name and display buttons dynamically. It doesn't work correctly, so I have modified the code to draw button-shaped rectangles where I hoped to generate buttons. The button 'temp' only appears once, as button 100. It does not take its name from the value of the variable temp, but from the letters 'temp'. So all my code does is create the same button 100 times!

temp can be repeatedly defined alternately as a variable and an object (button), as can be seen when I output its value. If I move the output statement to immediately after the line that creates the button, 'temp = q', the program ends with the error Wrong Object Type. I would expect that, but I would also expect the same error on the next loop iteration when I define the button object name 'temp' again as a numeric variable.
In SPL it is Ok to redefine an object during runtime, so using "temp" as a text object and later as a button object is fine.
But your code will not work because you always have the same object "temp" for all your buttons: it is just redefined with another coordinates and that is all. To get multiple buttons you need an archive of objects-buttons. This is your code corrected. But anyway please wait for an update to get it working.

Code: Select all

''
Drawbuttons by rbytes
October 2017
''
' hide the control menu - screenwidth increases by 48 points
#.scrview(#.normal)

' get screen size and calculate scaling for device
x,y = #.scrsize()
rw = x/1280 ; rh = y/813

s = 1
u = 0

' create button matrix

' define template button and set common parameters
q=#.button
q.width = 50
q.height = 50

' create 100 buttons in a 10 x 10 matrix
> t, 1..100

  ' name temp as the number t
  temp = #.str(t)
  #.output(temp)
  
  ' create button from temp using button template
  b[t] = q

  ' position the button
  ~b[t].x = 150 + s*60
  ~b[t].y = 120 + u*60
  #.show(~b[t])

  ' draw rectangles where the buttons should be
  #.drawrect(b[t].x,b[t].y,b[t].x+50,b[t].y+50)

s += 1
  ? s > 10
    s = 1
    u += 1
  .
<

Re: Button names created in a loop

Posted: Mon Oct 16, 2017 5:07 am
by rbytes
Thanks for fixing the button problem detailed above. Your code examples both work fine now with version 26 :)