Radio Button tool (on/off) (New)

Here you can link to screenshots and demos etc. of programs made using BBC BASIC
michael
Posts: 12
Joined: Mon 02 Apr 2018, 17:13

Radio Button tool (on/off) (New)

Post by michael » Wed 04 Jul 2018, 06:22

Hey Richard and DDRM and others. I offer a new radio button to try out. Hope you are doing well.

Code: Select all

  MODE 8
      VDU 5
      PROCcolor("b","black")
      CLG
      REPEAT
        IF FNrbutton(100,100,"on")="on" THEN PROCcolor("f","white"): MOVE 100,500: PRINT " THE BUTTON IS ON "
        IF FNrbutton(100,100,"on")="0" THEN PROCcolor("f","000,000,000"):MOVE 100,500:PRINT" THE BUTTON IS ON"
      UNTIL FALSE
      END
      REM for radio button (toggle on/off switch) or object
      REM********* bname$- gives the object button a number 0 - 20
      DEFFNrbutton(x%,y%,bname$)
      LOCAL si%,mx%,my%,mb%,br%,bg%,bb%
      MOUSE mx%,my%,mb%
      PROCrgbret(x%,y%,br%,bg%,bb%)
      PROCdotsize(1)
      PROCcolor("f","white")
      CIRCLE x%,y%,20
      PROCcolor("f","15")
      PROCdotsize(2)
      CIRCLE x%,y%,19
      PROCdotsize(2)
      IF mx%>x%-15 AND mx%< x%+15 AND my%<y%+15 AND my%>y%-15 THEN CIRCLE x%,y%,12 ELSE PROCcolor("f","black") CIRCLE x%,y%,12
      IF mx%>x%-15 AND mx%< x%+15 AND my%<y%+15 AND my%>y%-15 AND mb%=4 THEN
        PROCcolor("b","255,255,255")
        IF br%=0 AND bg%=0 AND bb%=0 THEN CIRCLE FILL x%,y%,12 ELSE PROCcolor("b","000,000,000"):PROCcolor("f","0"):CIRCLE FILL x%,y%,12
      ENDIF
      REM required to give mouse a pace so you get a better response & to keep cpu cool
      WAIT 17
      PROCrgbret(x%,y%,br%,bg%,bb%)
      IF br%=0 AND bg%=0 AND bb%=0 THEN ="0"
      =bname$
      REM *****SPECIAL RGB tools (color extraction) has use with PROCdotrgb
      DEF PROCrgbret(x%,y%,RETURN r%,RETURN g%,RETURN b%)
      LOCAL rgb%
      rgb%=TINT(x%,y%)
      r%=rgb% AND &FF
      g%=rgb%>>8 AND &FF
      b%=rgb%>>16 AND &FF
      ENDPROC
      REM Set pixel thickness
      DEFPROCdotsize(n)
      VDU 23,23,n|
      ENDPROC
      REM restore default color palettes
      DEFPROCresetrgb
      COLOUR 0,0,0,0 :COLOUR 1,200,0,0 :COLOUR 2,000,200,000
      COLOUR 3,200,200,000:COLOUR 4,000,000,200:COLOUR 5,200,000,200
      COLOUR 6,000,200,200:COLOUR 7,200,200,200:COLOUR 8,056,056,056
      COLOUR 9,248,056,056:COLOUR 10,056,248,056:COLOUR 11,248,248,056
      COLOUR 12,056,056,248:COLOUR 13,248,056,248:COLOUR 14,056,248,248
      COLOUR 15,248,248,248
      ENDPROC
      DEF PROCcolor(fb$,rgb$)
      PRIVATE assemble$,br%,bg%,bb%
      IF rgb$="0" OR rgb$="black" THEN rgb$="000,000,000"
      IF rgb$="1" OR rgb$="red" THEN rgb$="200,000,000"
      IF rgb$="2" OR rgb$="green" THEN rgb$="000,200,000"
      IF rgb$="3" OR rgb$="yellow" THEN rgb$="200,200,000"
      IF rgb$="4" OR rgb$="blue" THEN rgb$="000,000,200"
      IF rgb$="5" OR rgb$="magenta" THEN rgb$="200,000,200"
      IF rgb$="6" OR rgb$="cyan" THEN rgb$="000,200,200"
      IF rgb$="7" OR rgb$="white" THEN rgb$="200,200,200"
      IF rgb$="8" OR rgb$="grey" THEN rgb$="056,056,056"
      IF rgb$="9" OR rgb$="light red" THEN rgb$="248,056,056"
      IF rgb$="10" OR rgb$="light green" THEN rgb$="056,248,056"
      IF rgb$="11" OR rgb$="light yellow" THEN rgb$="248,248,056"
      IF rgb$="12" OR rgb$="light blue" THEN rgb$="056,056,248"
      IF rgb$="13" OR rgb$="light magenta" THEN rgb$="248,056,248"
      IF rgb$="14" OR rgb$="light cyan" THEN rgb$="056,248,248"
      IF rgb$="15" OR rgb$="light white" THEN rgb$="248,248,248"
      assemble$=rgb$
      br%=VAL(MID$(assemble$,1,3)):bg%=VAL(MID$(assemble$,5,3)):bb%=VAL(MID$(assemble$,9,3))
      IF fb$="f" OR fb$="F" THEN COLOUR 0,br%,bg%,bb% : GCOL 0
      IF fb$="b" OR fb$="B" THEN COLOUR 1,br%,bg%,bb% : GCOL 128+1
      ENDPROC

DDRM
Administrator
Posts: 59
Joined: Mon 02 Apr 2018, 18:04

Re: Radio Button tool (on/off) (New)

Post by DDRM » Wed 04 Jul 2018, 08:36

Hi Michael,

I like the "rose petal" outer ring, and that it "highlights" when you mouse over!

I found it didn't toggle totally reliably: I suspect because the mouse buffer is storing more than one event when it is clicked: I added the line

REPEAT MOUSE tx%,ty%,tz% UNTIL tz%=0

directly after your MOUSE mx%,my%,mz%, and that gave a clean response.

I note that your routine only allows for "white on black" buttons, so a lot of colour handling bits could be simplified - but I presume the longer term plan is to make that configurable.

I suspect you'll want a function to read the state of the button without altering it, but that should be easy...

Best wishes,

D

mikeg
Posts: 13
Joined: Sat 23 Jun 2018, 19:52

Re: Radio Button tool (on/off) (New)

Post by mikeg » Wed 04 Jul 2018, 14:35

Thankyou DDRM.

mikeg
Posts: 13
Joined: Sat 23 Jun 2018, 19:52

Re: Radio Button tool (on/off) (New)

Post by mikeg » Wed 04 Jul 2018, 23:58

Um.. DDRM.. wouldn't REPEAT MOUSE tx%,ty%,tz% UNTIL tz%=0 cause the computer to have problems due to the fact that it has no WAIT n ?

DDRM
Administrator
Posts: 59
Joined: Mon 02 Apr 2018, 18:04

Re: Radio Button tool (on/off) (New)

Post by DDRM » Thu 05 Jul 2018, 08:41

Hi Mike,

The idea is to empty the mouse buffer as fast as possible (before another click or movement can occur) - it will only go round the loop a "few" times, so it isn't a problem - but see comment below. Your WAIT elsewhere in the process will deal with the fact that most of the time the user is not clicking,and stop the processor wasting huge amounts of effort (and generating huge amounts of heat).

The problem arises if you are repeatedly sitting for a "long time" waiting for the user to do something - a user might often take several seconds to do something new, which is an awful lot of clock cycles... If that happens repeatedly, you are wasting a lot of energy.

So I tried what "a few" might be, and the results were interesting, at least to me. It looks like events are being continuously generated while the button is pressed, so if you go round a tight loop you might get round 30-50,000 times during a simple click! Still not a problem, but a lot! What surprised me more was that adding a WAIT 1 reduced it to about 10-12 - in other words the events are not being generated during the WAIT - I had expected that they would just pile up in the buffer. I'd be interested if anyone can explain this behaviour to me. I'm guessing that the mouse buffer belongs to BB4W (Not Windows), and that during the WAIT BB4W is not collecting mouse events from the system, and so not putting them into its buffer.

So I'd revise my answer a little: I still don't think it MATTERS that there's no wait in the loop, because it will typically be very short, but actually it looks like sticking a WAIT 1 in might be a good plan. Again, I'll happily take advice from anyone who knows better!

Best wishes,

D

Code: Select all

      REPEAT
        MOUSE x%,y%,z%
        IF z%>0 THEN
          PRINT "Clicked!";
          c%=0
          REPEAT
            MOUSE x%,y%,z%
            IF z%>0 THEN c%+=1
            WAIT 1    :REM Try REMming this out!
          UNTIL z%=0
          PRINT c%
        ENDIF
        WAIT 1
      UNTIL FALSE

Zaphod
Posts: 39
Joined: Sat 23 Jun 2018, 15:51

Re: Radio Button tool (on/off) (New)

Post by Zaphod » Thu 05 Jul 2018, 16:16

Hi Michael.

How are you planning to get the exclusivity of only having one button in a group on to make it function as a Radio button as he title of the post suggests that that is your ultimate aim?

Z

michael
Posts: 12
Joined: Mon 02 Apr 2018, 17:13

Re: Radio Button tool (on/off) (New)

Post by michael » Fri 06 Jul 2018, 00:25

How are you planning to get the exclusivity of only having one button in a group on to make it function as a Radio button as he title of the post suggests that that is your ultimate aim?

Z

Code: Select all

 IF FNrbutton(100,100,"on")="on" THEN PROCcolor("f","white"): MOVE 100,500: PRINT " THE BUTTON IS ON "

Ok.. lets look at the code ^^^^ FNrbutton is given "on" as the word that must be returned if the button is on. If it returns ANYTHING else then the radio button located at 100,100 is off.. Pretty simple.
FNrbutton location is specific to the word.. and well, you wont likely put your radio button out of sight, nor will you share the location with another button.. sooo… it works. The rest is the responsibility of the programmer that designs the interface layout.

As with other commands that I created that share FNrbutton ability, you basically are unlimited to the amount of buttons, as you echo a name for the location which will echo back the status of the button to the line that checks it. So in a sense, the name you send to FNrbutton is like a virtual object that can be changed to manage any button location. So maybe the next button at 500,500 could be sent a word like "pin2on" as in the case of the raspberry pi3 pins... HA HA!! (and last time I checked this program works on BBC4W and Raspberry pi3 (BBCSDL). Of course my old Mac its not so.

But really, also the state is contained in the RGB color in the center of the button.. So as long as it is highlighted, the result returned to the users variable in the main program will be updated whenever the function is cycled.. When it is checked (mouse position and color contained in the button and other factors, the return should remain the same.) I will look it over carefully to ensure this remains the same regardless. BUT if your variable that checks it is used elsewhere to check another FNrbutton then it would be inappropriate code management by the programmer.

Zaphod
Posts: 39
Joined: Sat 23 Jun 2018, 15:51

Re: Radio Button tool (on/off) (New)

Post by Zaphod » Fri 06 Jul 2018, 00:50

Hi Michael,
I read your reply several times but could not decide whether that was a yes or no answer to my question, sorry. Perhaps a 3 radio button example would show how it might be used better because I am just not seeing how at present. I can see it as a check box that works on mouse clicks but you still can't switch the state programmatically which I think you need to do to make a proper radio button group. I suppose you could just write a color dot to the button but that function doesn't exist at present does it? It all seems a little convoluted to my old brain.

Z

michael
Posts: 12
Joined: Mon 02 Apr 2018, 17:13

Re: Radio Button tool (on/off) (New)

Post by michael » Fri 06 Jul 2018, 01:14

ok ill make an example

michael
Posts: 12
Joined: Mon 02 Apr 2018, 17:13

Re: Radio Button tool (on/off) (New)

Post by michael » Fri 06 Jul 2018, 01:35

Pretty cool how this sample works.. You may want to add DDRMs solution to keep the mouse detection more accurate. Also I think I found a solution that should eliminate the need for the WAIT 1 in the mouse solution.. just set up a counter in the loop to force it to exit after so many cycles.. HA HA! I should mention, that this example would work well if you were switching pins on the Pi3 as the switch only changes when you send a new command to the switch. (on stays on in other words unless its told to switch to off) Variable voltage would have to use a different method unless, say you decided to define each button as a specific voltage for a LED brightness.

Also for me, I would lower my wait times as more of these commands will slow it down..
I am uncertain if this is like object orientated programming, but I have named this method ECHO programming method.. I came up with it a long time ago when I created buttonz and other similar commands that work using the same basic way. It took me a long time to come up with the solution to managing the variables.. Well now variables are not an issue anymore.

Code: Select all

      MODE 8
      VDU 5
      PROCcolor("b","black")
      CLG
      REPEAT
        IF FNrbutton(100,100,"on")="on" THEN PROCcolor("f","white"): MOVE 100,500: PRINT " THE BUTTON IS ON "
        IF FNrbutton(100,100,"on")="0" THEN PROCcolor("f","000,000,000"):MOVE 100,500:PRINT" THE BUTTON IS ON"
        IF FNrbutton(100,50,"button2")="button2" THEN PROCcolor("f","white"): MOVE 100,600: PRINT " button2 is on "
        IF FNrbutton(100,50,"on")="0" THEN PROCcolor("f","000,000,000"):MOVE 100,600:PRINT" button2 is on"
  
      UNTIL FALSE
      END
      REM for radio button (toggle on/off switch) or object
      REM********* bname$- gives the object button a number 0 - 20
      DEFFNrbutton(x%,y%,bname$)
      LOCAL si%,mx%,my%,mb%,br%,bg%,bb%
      MOUSE mx%,my%,mb%
      PROCrgbret(x%,y%,br%,bg%,bb%)
      PROCdotsize(1)
      PROCcolor("f","white")
      CIRCLE x%,y%,20
      PROCcolor("f","15")
      PROCdotsize(2)
      CIRCLE x%,y%,19
      PROCdotsize(2)
      IF mx%>x%-15 AND mx%< x%+15 AND my%<y%+15 AND my%>y%-15 THEN CIRCLE x%,y%,12 ELSE PROCcolor("f","black") CIRCLE x%,y%,12
      IF mx%>x%-15 AND mx%< x%+15 AND my%<y%+15 AND my%>y%-15 AND mb%=4 THEN
        PROCcolor("b","255,255,255")
        IF br%=0 AND bg%=0 AND bb%=0 THEN CIRCLE FILL x%,y%,12 ELSE PROCcolor("b","000,000,000"):PROCcolor("f","0"):CIRCLE FILL x%,y%,12
      ENDIF
      REM required to give mouse a pace so you get a better response & to keep cpu cool
      WAIT 17
      PROCrgbret(x%,y%,br%,bg%,bb%)
      IF br%=0 AND bg%=0 AND bb%=0 THEN ="0"
      =bname$
      REM *****SPECIAL RGB tools (color extraction) has use with PROCdotrgb
      DEF PROCrgbret(x%,y%,RETURN r%,RETURN g%,RETURN b%)
      LOCAL rgb%
      rgb%=TINT(x%,y%)
      r%=rgb% AND &FF
      g%=rgb%>>8 AND &FF
      b%=rgb%>>16 AND &FF
      ENDPROC
      REM Set pixel thickness
      DEFPROCdotsize(n)
      VDU 23,23,n|
      ENDPROC
      REM restore default color palettes
      DEFPROCresetrgb
      COLOUR 0,0,0,0 :COLOUR 1,200,0,0 :COLOUR 2,000,200,000
      COLOUR 3,200,200,000:COLOUR 4,000,000,200:COLOUR 5,200,000,200
      COLOUR 6,000,200,200:COLOUR 7,200,200,200:COLOUR 8,056,056,056
      COLOUR 9,248,056,056:COLOUR 10,056,248,056:COLOUR 11,248,248,056
      COLOUR 12,056,056,248:COLOUR 13,248,056,248:COLOUR 14,056,248,248
      COLOUR 15,248,248,248
      ENDPROC
      DEF PROCcolor(fb$,rgb$)
      PRIVATE assemble$,br%,bg%,bb%
      IF rgb$="0" OR rgb$="black" THEN rgb$="000,000,000"
      IF rgb$="1" OR rgb$="red" THEN rgb$="200,000,000"
      IF rgb$="2" OR rgb$="green" THEN rgb$="000,200,000"
      IF rgb$="3" OR rgb$="yellow" THEN rgb$="200,200,000"
      IF rgb$="4" OR rgb$="blue" THEN rgb$="000,000,200"
      IF rgb$="5" OR rgb$="magenta" THEN rgb$="200,000,200"
      IF rgb$="6" OR rgb$="cyan" THEN rgb$="000,200,200"
      IF rgb$="7" OR rgb$="white" THEN rgb$="200,200,200"
      IF rgb$="8" OR rgb$="grey" THEN rgb$="056,056,056"
      IF rgb$="9" OR rgb$="light red" THEN rgb$="248,056,056"
      IF rgb$="10" OR rgb$="light green" THEN rgb$="056,248,056"
      IF rgb$="11" OR rgb$="light yellow" THEN rgb$="248,248,056"
      IF rgb$="12" OR rgb$="light blue" THEN rgb$="056,056,248"
      IF rgb$="13" OR rgb$="light magenta" THEN rgb$="248,056,248"
      IF rgb$="14" OR rgb$="light cyan" THEN rgb$="056,248,248"
      IF rgb$="15" OR rgb$="light white" THEN rgb$="248,248,248"
      assemble$=rgb$
      br%=VAL(MID$(assemble$,1,3)):bg%=VAL(MID$(assemble$,5,3)):bb%=VAL(MID$(assemble$,9,3))
      IF fb$="f" OR fb$="F" THEN COLOUR 0,br%,bg%,bb% : GCOL 0
      IF fb$="b" OR fb$="B" THEN COLOUR 1,br%,bg%,bb% : GCOL 128+1
      ENDPROC

Post Reply