Page 1 of 1

Switches

Posted: Fri Oct 13, 2017 3:42 am
by rbytes
There is not yet a switch object in SPL, but there are several ways of simulating them. I find the button object can be used to make a realistic switch. What you will need are images of the switch in its off and on positions. I have a set I am attaching to this post, along with images of a light bulb off and on. The switch and light bulb images need to be in the same folder as the program.

Code: Select all

''
Switches by rbytes, October 2017

 each one is a button that toggles to an alternate image when touched

#.button is a button. Pressing a button registers an action. Group elements are:
  backcolor(r,g,b,a) - function to set button color with red, green, blue and alpha r,g,b,a components, alpha component is optional;
  fontcolor(r,g,b,a) - function to set font color with red, green, blue and alpha r,g,b,a components, alpha component is optional;
  fontname - font name;
  fontsize - font size;
  height - button height;
  image - path to button background image;
  text - text on button;
  width - button width;
  x - offset from left screen border;
  y - offset from top screen border.

''
bs,cs,ds,es,fs = 0       ' flags for switches

' light bulbs off
j = #.image
j.source = "bulb-off.png"
j.width = 60
j.height = 100
j.y = 100
k,l,m,n = j
j.x = 270
k.x = 420
l.x = 570
m.x = 720
n.x = 870
#.show(j,k,l,m,n)

' light bulbs on
j2 = #.image
j2.source = "bulb-on.png"
j2.width = 60
j2.height = 100
j2.y = 100
k2,l2,m2,n2 = j2
j2.x = 270
k2.x = 420
l2.x = 570
m2.x = 720
n2.x = 870

b = #.button
b.width = 100
b.height = 40
b.y = 300
b.text = ""
b.image = "sw-off.png"
c,d,e,f = b
b.x = 250
c.x = 400
d.x = 550
e.x = 700
f.x = 850
#.show(b,c,d,e,f)

>
? #.act(b)
  bs = 1 - bs
  ? bs
    b.image = "sw-on.png"
    #.show(j2)
  !
    b.image = "sw-off.png"
    #.hide(j2)
  .
.
? #.act(c)
  cs = 1 - cs
  ? cs
    c.image = "sw-on.png"
    #.show(k2)
  !
    c.image = "sw-off.png"
    #.hide(k2)
  .
.
? #.act(d)
  ds = 1 - ds
  ? ds
    d.image = "sw-on.png"
    #.show(l2)
  !
    d.image = "sw-off.png"
    #.hide(l2)
  .
.
? #.act(e)
  es = 1 - es
  ? es
    e.image = "sw-on.png"
    #.show(m2)
  !
    e.image = "sw-off.png"
    #.hide(m2)
  .
.
? #.act(f)
  fs = 1 - fs
  ? fs
    f.image = "sw-on.png"
    #.show(n2)
  !
    f.image = "sw-off.png"
    #.hide(n2)
  .
.
#.relax()
<

Re: Switches

Posted: Fri Oct 13, 2017 11:54 am
by Mr. Kibernetik
Very interesting.
Yes, we need switches...