Switches

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:

Switches

Post 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()
<
Attachments
sw-on.png
sw-on.png (54.31 KiB) Viewed 2150 times
sw-off.png
sw-off.png (56.41 KiB) Viewed 2150 times
bulb-on.png
bulb-on.png (61.3 KiB) Viewed 2150 times
bulb-off.png
bulb-off.png (64.83 KiB) Viewed 2150 times
Screenshot (97).png
Screenshot (97).png (47.63 KiB) Viewed 2150 times
Screenshot (96).png
Screenshot (96).png (35.17 KiB) Viewed 2150 times
Screenshot (95).png
Screenshot (95).png (35.61 KiB) Viewed 2150 times
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: Switches

Post by Mr. Kibernetik »

Very interesting.
Yes, we need switches...

Post Reply