Button names created in a loop

Post Reply
User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Button names created in a loop

Post 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
The only thing that gets me down is gravity...

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Button names created in a loop

Post by Mr. Kibernetik »

Thank you for your information.
Seems there is a bug preventing doing this.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Button names created in a loop

Post 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
  .
<
Attachments
Screenshot (102).png
Screenshot (102).png (58.13 KiB) Viewed 2562 times
Last edited by rbytes on Sun Oct 15, 2017 6:21 pm, edited 2 times in total.
The only thing that gets me down is gravity...

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Button names created in a loop

Post by Mr. Kibernetik »

Yes, there is a bug preventing creating interface objects in a loop.

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Button names created in a loop

Post 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.

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Button names created in a loop

Post 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
  .
<

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Button names created in a loop

Post by rbytes »

Thanks for fixing the button problem detailed above. Your code examples both work fine now with version 26 :)
The only thing that gets me down is gravity...

Post Reply