Page 1 of 1

Help with BUTTONS, SWITCHES and SLIDERS, PLEASE.

Posted: Mon Mar 25, 2019 5:47 pm
by Chooch912
I am just getting into programming with the SmartBasic program and even though I am pretty familiar with "Business Basic" language from the 1980 era I am just now learning about graphics in SmartBasic. Can anyone please send me a few short examples of how to use "BUTTONS", "SWITCHES" AND "SLIDERS"? Nothing fancy is needed, just something I can "copy" and "paste" in the program so I can see how they work?
These are new concepts to me and I would like to include them in a program I am working on.

Re: Help with BUTTONS, SWITCHES and SLIDERS, PLEASE.

Posted: Mon Mar 25, 2019 7:26 pm
by matt7
Here's a quick example I threw together. The "Reset" button resets slider value back to 0.5. The switch toggles the "Reset" button between enabled/disabled (using a text field over top the button to dim the button and prevent it being pressed). The slider does nothing, but you could read out the value in the code using SLIDER_VALUE("sldr"), which will return a decimal value between 0 and 1.

Code: Select all

' Create Interface Objects
'--------------------------

BUTTON "btn" TEXT "Reset" AT 50, 50 SIZE 100, 30
FIELD "disableBtn" TEXT "" AT 50, 50 SIZE 110, 40 RO
FIELD "disableBtn" BACK COLOR 1, 1, 1
FIELD "disableBtn" BACK ALPHA 0.7
FIELD "disableBtn" HIDE

SWITCH "sw" STATE 1 AT 50, 100

SLIDER "sldr" VALUE 0.5 AT 50, 150 SIZE 200 ANGLE 0

BUTTON "quit" TEXT "QUIT" AT 200, 50 SIZE 60, 30


' Process Inputs
' ---------------

DO
  SLOWDOWN
  
  sw = SWITCH_STATE("sw")
  sldrReset = BUTTON_PRESSED("btn")
  userQuit = BUTTON_PRESSED("quit")
  
  IF SWITCH_CHANGED("sw") THEN
    IF sw THEN
      FIELD "disableBtn" HIDE
    ELSE
      FIELD "disableBtn" SHOW
    END IF
  END IF
  
  IF sldrReset THEN
    SLIDER "sldr" VALUE 0.5
  END IF
  
UNTIL userQuit

Re: Help with BUTTONS, SWITCHES and SLIDERS, PLEASE.

Posted: Mon Mar 25, 2019 7:37 pm
by matt7
It's also worth mentioning that any interface object you create is added to a "page." If no page is manually set up (like in my example above), then Smart Basic puts the interface object onto the default page (with name "") that has a transparent background and covers the entire screen. But if you set your own pages up, you can group interface objects and control the entire group through PAGE commands. Also, note that interface object coordinates are relative to the coordinates of the page it belongs to.

You can set up pages to provide different "screens" in a program, or you can make smaller pages (using PAGE n$ FRAME x, y, w, h) to arrange objects in "panels".

Code: Select all

GET SCREEN SIZE scrW, scrH

PAGE "p1" SET
PAGE "p1" FRAME 0, 0, scrW, scrH
PAGE "p1" COLOR 1, 1, 0.8, 1
PAGE "p1" SHOW

' Interface objects created here will belong to Page "p1"

PAGE "p2" SET
PAGE "p2" FRAME scrW*0.25, scrH*0.25, scrW*0.5, scrH*0.5 
PAGE "p2" COLOR 1, 0.8, 1, 1
PAGE "p2" SHOW

' Interface objects created here will belong to Page "p2"

PAGE "p1" SET
' Interface objects created here will belong to Page "p1"

PAGE "p2" SET
' Interface objects created here will belong to Page "p2"

Re: Help with BUTTONS, SWITCHES and SLIDERS, PLEASE.

Posted: Tue Mar 26, 2019 12:44 am
by Chooch912
Thank you so much for your reply to my post. I just tried your examples and went through the code. Now I have a better understanding of what I need to do in my project.

Re: Help with BUTTONS, SWITCHES and SLIDERS, PLEASE.

Posted: Tue Mar 26, 2019 5:20 am
by rbytes
It would no doubt be helpful to have a section for beginners where we provide very short examples of interface objects. Libraries might be a good location for them.

Here is one that creates a slider, with a field to display its output.

Code: Select all

/*
Slider values only range from 0 to 1. But by multiplying, you can make your
slider value range as large as you want it.
*/

N$="simpleslider"        ' name your slider
K=.5                     ' set the slider's initial value

' this slider is set to its midpoint and is positioned about the middle of the screen
' it is 400 points wide, and is horizontal
SLIDER N$ VALUE K AT 300,300 SIZE 400 ANGLE 0

' to see its output change, we add a field
' we set the field nackground color to cyan
FIELD N$ TEXT "Value" AT 440,220 SIZE 120,40 RO   ' a read-only field, so you can't type into it
FIELD N$ BACK COLOR 0,1,1

' this next section is the main program loop
' it cycles forever, or until you push the stop button (top right corner of screen)
DO SLOWDOWN
IF SLIDER_CHANGED(N$) THEN
  slide=SLIDER_VALUE(N$)
  FIELD N$ TEXT slide
ENDIF
UNTIL 0
This one creates a switch, also with a field to show its output value

Code: Select all

/*
Switches are often used to allow a user to set preferences within your program
*/

N$="simpleswitch"        ' name your switch
K=0                      ' set the switch's initial state (1=on 0=off)

' this switch is set to off and is positioned about the middle of the screen
' it is a fixed size regardless of the display device
SWITCH N$ STATE K AT 470,300

' to see its output change, we add a field
' we set the field background color to cyan
FIELD N$ TEXT "       0" AT 455,220 SIZE 80,40 RO   ' a read-only field, so you can't type into it
FIELD N$ BACK COLOR 0,1,1

' this next section is the main program loop
' it cycles forever, or until you push the stop button (top right corner of screen)
DO SLOWDOWN
  IF SWITCH_CHANGED(N$) THEN
    state=SWITCH_STATE(N$)
    FIELD N$ TEXT "       "&state
  ENDIF
UNTIL 0
And this one creates a button. The field says "Not Pressed". Pressing the button sounds a beep, and the word Pressed appears in the field.

Code: Select all

/*
Buttons trigger the various actions of your program
*/

N$="simplebutton"        ' name your button
A$="PUSH"                ' label your button

' this button is positioned about the middle of the screen
BUTTON N$ TEXT A$ AT 460,300 SIZE 80,40

' to see its output change, we add a field
' we set the field background color to cyan
FIELD N$ TEXT "     Not Pressed" AT 430,220 SIZE 140,40 RO   ' a read-only field, so you can't type into it
FIELD N$ BACK COLOR 0,1,1

' this next section is the main program loop
' it cycles forever, or until you push the stop button (top right corner of screen)
DO SLOWDOWN
  IF BUTTON_PRESSED(N$) THEN
    FIELD N$ TEXT "        Pressed"
    BEEP
    PAUSE 1
    FIELD N$ TEXT "     Not Pressed"
  ENDIF
UNTIL 0

END

Re: Help with BUTTONS, SWITCHES and SLIDERS, PLEASE.

Posted: Tue Mar 26, 2019 7:32 am
by Mr. Kibernetik
I created this section "For beginners" to discuss these topics.

Re: Help with BUTTONS, SWITCHES and SLIDERS, PLEASE.

Posted: Tue Mar 26, 2019 1:56 pm
by rbytes
Excellent. Thanks!

Re: Help with BUTTONS, SWITCHES and SLIDERS, PLEASE.

Posted: Tue Mar 26, 2019 2:26 pm
by Chooch912
I am so very impressed with the support this forum is providing! There are so many topics with so much information it isn't alway easy for a "newbie" to follow and learn sB, but when I asked my questions I was immediately pleasantly surprised that I was given help. Thank you. My goal is to learn as much as I can and someday provide help to someone like myself. The "Beginner" section is a fantastic idea.

Re: Help with BUTTONS, SWITCHES and SLIDERS, PLEASE.

Posted: Tue Mar 26, 2019 2:52 pm
by rbytes
Chooch912, I would be interested in your reaction to this Program Template that I posted a while back in Basic Programs. (Mr. K, It would probably be better to move it to this section.)

viewtopic.php?f=20&t=2352

It is a working program, but it is thoroughly commented, with sliders, buttons, fields, text display, image display, and so on. The idea is to first run it and play with the controls, then look through it to see how it works, and then change it to do whatever you want it to.

Re: Help with BUTTONS, SWITCHES and SLIDERS, PLEASE.

Posted: Tue Mar 26, 2019 3:22 pm
by Mr. Kibernetik
rbytes wrote:
Tue Mar 26, 2019 2:52 pm
Mr. K, It would probably be better to move it to this section.
Ok