' iBClock.ibas
{CREATORID "LDIC"}
{VERSION "3.3"}
{RESOURCEFILE "iBClock.rsrc"}

BEGIN
GOSUB _InitPgm
REPEAT
   GOSUB _ChangeSec
   D=DOEVENTS
   IF D=5 LET D=NOTICEBOX(2)
   IF D=6 LET D=NOTICEBOX(1) : IF D=2 LET D=NOTICEBOX(2)
   IF D=7 LET C=COLORSELECT(C) : GOSUB _DrawScreen
   IF D=8 LET B=COLORSELECT(B) : GOSUB _DrawScreen
UNTIL D=-1
IF Z>0 THEN
   SAVEPREF #1,B
   SAVEPREF #2,C
ENDIF
END

_DrawBinary:
   H$="00000"+BIN$(S)
   A$(3)=RIGHT$(H$,6)
   H$="00000"+BIN$(M)
   A$(2)=RIGHT$(H$,6)
   H$="00000"+BIN$(H)
   A$(1)=RIGHT$(H$,6)
   Y=80
   FOR J=1 TO 3
      J$=A$(J)
      X=20 : Y=Y+20 : W=Y+18
      FOR I=1 TO 6
         X=X+20 : V=X+18
         I$=CHAR$(J$,I)
         IF I$="0" THEN
            COLOR A
            BOXFILLED X,Y TO V,W
            COLOR B
            BOX X,Y TO V,W
         ELSE
            COLOR B
            BOXFILLED X,Y TO V,W
         ENDIF
      NEXT
   NEXT
RETURN

_DrawClock:
   I=H : IF I>=12 LET I=I-12
   I=M/60+I-3*PI/6
   X=COS(I)*20+120
   Y=SIN(I)*20+55
   LINE 120,55 TO X,Y
   I=M-15*PI/30
   X=COS(I)*25+120
   Y=SIN(I)*25+55
   LINE 120,55 TO X,Y
   I=S-15*PI/30
   X=COS(I)*30+120
   Y=SIN(I)*30+55
   LINE 120,55 TO X,Y
RETURN

_DrawHexa:
   H$="0"+HEX$(S)
   U$=RIGHT$(H$,2)
   H$="0"+HEX$(M)
   U$=RIGHT$(H$,2)+":"+U$
   H$="0"+HEX$(H)
   U$=RIGHT$(H$,2)+":"+U$
   UPDATELABEL #2,U$
RETURN

_DrawOctal:
   H$="0"+OCT$(S)
   U$=RIGHT$(H$,2)
   H$="0"+OCT$(M)
   U$=RIGHT$(H$,2)+":"+U$
   H$="0"+OCT$(H)
   U$=RIGHT$(H$,2)+":"+U$
   UPDATELABEL #3,U$
RETURN

_Convert1RomanNum:
   I=P MOD 10
   IF I=1 LET N$="I"+N$
   IF I=2 LET N$="II"+N$
   IF I=3 LET N$="III"+N$
   IF I=4 LET N$="IV"+N$
   IF I=5 LET N$="V"+N$
   IF I=6 LET N$="VI"+N$
   IF I=7 LET N$="VII"+N$
   IF I=8 LET N$="VIII"+N$
   IF I=9 LET N$="IX"+N$
   J=P MOD 100 -I
   IF J=10 LET N$="X"+N$
   IF J=20 LET N$="XX"+N$
   IF J=30 LET N$="XXX"+N$
   IF J=40 LET N$="XL"+N$
   IF J=50 LET N$="L"+N$
   IF N$="" LET N$="  "
RETURN

_DrawRoman:
   N$="" : P=S
   GOSUB _Convert1RomanNum
   U$=N$
   N$="" : P=M
   GOSUB _Convert1RomanNum
   U$=N$+":"+U$
   N$="" : P=H
   GOSUB _Convert1RomanNum
   U$=N$+":"+U$
   UPDATELABEL #4,U$
RETURN

_DrawWatches:
   UPDATELABEL #1,T$
   COLOR A : GOSUB _DrawClock
   S=VAL(H$)
   H$=MID$(T$,4,2) : M=VAL(H$)
   H$=LEFT$(T$,2) : H=VAL(H$)
   COLOR C : GOSUB _DrawClock
   GOSUB _DrawHexa
   GOSUB _DrawOctal
   GOSUB _DrawRoman
   GOSUB _DrawBinary
RETURN

_ChangeSec:
   T$=TIME$
   H$=RIGHT$(T$,2) : U=VAL(H$)-T
   IF U<>0 LET T=VAL(H$) : GOSUB _DrawWatches
RETURN

_DrawScreen:
   SCREEN Z
   SETFONT 1
   LABEL #1,"",55,1
   SETFONT 0
   LABEL #2,"",15,25
   LABEL #3,"",15,50
   LABEL #4,"",5,75
   BUTTON #5,"$",149,1,10,10
   SETFONT 1
   BUTTON #6,"i",136,1,10,10
   SETFONT 0
   IF Z>0 THEN
      BUTTON #7,"C",123,1,10,10
      BUTTON #8,"B",110,1,10,10
   ENDIF
   LABEL #9,"Hour",5,102
   LABEL #10,"Minute",5,122
   LABEL #11,"Second",5,142
   X=154 : Y=55 : PSET X,Y
   J=2*PI
   FOR I=0.04 TO J STEP 0.02
      X=COS(I)*35+120
      Y=SIN(I)*35+55
      LINE TO X,Y
   NEXT
RETURN

_InitPgm:
   Z=SCREENMODES
   Z=MIN(Z,3)
   SCREEN Z
   A=COLOR(0)
   IF Z=0 THEN
      B=COLOR(1)
      C=COLOR(1)
   ELSE
      B=LOADPREF(#1)
      C=LOADPREF(#2)
      IF B=0 THEN
         R=RND(256)
         G=RND(256)
         B=RND(256)
         B=COLORRGB(R,G,B)
      ENDIF
      IF C=0 THEN
         R=RND(256)
         G=RND(256)
         C=RND(256)
         C=COLORRGB(R,G,C)
      ENDIF
   ENDIF
   GOSUB _DrawScreen
RETURN