[ Kaleida ]


Kaleida 1.1
(open-source code by Dave O'Brien)

A simple Palm kaleidoscope program for doodling. Demonstrates borderless image buttons.

 ' Kaleida.ibas
 {CREATORID "Klda"}
 {VERSION "1.1"}
 {RESOURCEFILE "Kaleida.rsrc"}
 {SECUREFILES ON}
 {PARSER ON}
 'add color picker on tap-hold
 'add rainbow color
 'optimize doEvent - use waitEvent?
 'add move,turn,zoom
 'save to pic file
 'replay current drawing

 'drawing
 dim %topOffset% : const %topOffset%=16
 dim %sideOffset%
 dim %oldPenState%,%newPenState%
 dim %oldX%,%oldY%,%newX%,%newY%
 dim %oldX2%,%oldY2%,%newX2%,%newY2%
 dim %screenWidth%,%middleX%
 dim %screenHeight%,%middleY%
 dim %rightBound%,%bottomBound%
 dim %penWidth%,%small%,%medium%
 const %small%=1 : const %medium%=2
 dim %inBounds%
 dim %buttonOffset%
 
 'mirrors
 dim %mirrorType%
 dim %none%,%vertical%,%slash%
 dim %horizontal%,%backSlash%
 dim %verticalHorizontal%,%diagonal%
 dim %all%
 const %none%=0
 const %vertical%=1
 const %slash%=2
 const %horizontal%=3
 const %backSlash%=4
 const %verticalHorizontal%=5
 const %diagonal%=6
 const %all%=7
 
 'colors
 dim %black%,%white%,%gray%
 dim %red%,%orange%,%green%
 dim %blue%,%pink%,%yellow%
 dim %colorValue%,%penColor%
 dim %colorIsCurrent%
 
 'buttons
 dim %colorButtonWidth% : const %colorButtonWidth% = 7
 dim %blackOffset% : const %blackOffset%=76
 dim %redOffset%,%orangeOffset%
 dim %greenOffset%,%blueOffset%,%pinkOffset%
 dim %sizeOffset% : const %sizeOffset%=59
 dim %mirrorOffset% : const %mirrorOffset%=46
 dim %undoOffset% : const %undoOffset%=122
 dim %clearOffset% : const %clearOffset%=135
 dim %helpOffset% : const %helpOffset%=148
 dim %leftEdge%,%rightEdge%
 
 'button images
 dim %noneBmp% : const %noneBmp%=401
 dim %verticalBmp% : const %verticalBmp%=402
 dim %slashBmp% : const %slashBmp%=403
 dim %horizontalBmp% : const %horizontalBmp%=404
 dim %backSlashBmp% : const %backSlashBmp%=405
 dim %vhBmp% : const %vhBmp%=406
 dim %diagonalBmp% : const %diagonalBmp%=407
 dim %allBmp% : const %allBmp%=408
 dim %smallBmp% : const %smallBmp%=410
 dim %mediumBmp% : const %mediumBmp%=411
 dim %undoBmp% : const %undoBmp%=415
 dim %clearBmp% : const %clearBmp%=421
 dim %helpBmp% : const %helpBmp%=430
 
 'misc
 dim %event%
 dim %screenDepth%
 
 'temp
 dim %temp1$
 dim %temp1%
 dim %x1%,%x2%,%x3%,%x4%,%x5%,%x6%,%x7%
 dim %y1%,%y2%,%y3%,%y4%,%y5%,%y6%,%y7%
 dim %a1%,%a2%

 BEGIN
 %screenDepth%=screenMode
 gosub _setupDimensions
 gosub _setupColors
 gosub _setupColorOffsets
 gosub _setupDisplay
 %penWidth%=%small%
 gosub _toggleSize 'cycle to medium
 %penColor%=%black%
 if %screenDepth%>=3 then
    %buttonOffset%=%blackOffset%
    gosub _setBlack
 endIf
 %mirrorType%=%diagonal%
 gosub _setMirrors 'cycle to all mirrors
 gosub _clearScreen
 savescreen 'allow undo from here
 repeat
    %event%=doEvents
    select case %event%
       case 1
          gosub _setMirrors
       case 2
          gosub _toggleSize
       case 3
          gosub _setBlack
       case 4
          gosub _setRed
       case 5
          gosub _setOrange
       case 6
          gosub _setGreen
       case 7
          gosub _setBlue
       case 8
          gosub _setPink
       case 15
          gosub _undo
       case 20
          gosub _clearScreen
       case 30
          gosub _showHelp
       case 1000
          gosub _handlePen
       case 1001
          gosub _handleMenu
    end select
 until %event%<0
 END

 _setupDimensions:
 %bottomBound%=160
 %screenHeight%=%bottomBound%-%topOffset%
 %sideOffset%=%topOffset%/2
 %screenWidth%=%screenHeight%
 %rightBound%=%sideOffset%+%screenWidth%
 %middleX%=(%screenWidth%/2)+%sideOffset%
 %middleY%=(%screenHeight%/2)+%topOffset%
 return

 _setupColors:
 if %screenDepth%<3 then  'not color
    %white%=0
    %black%=1
    %gray%=1
 else  'color
    %white%=colorRGB(255,255,255)
    %black%=0
    %gray%=colorRGB(192,192,192)
    %red%=colorRGB(255,0,0)
    %orange%=colorRGB(255,165,0)
    %green%=colorRGB(0,255,0)
    %blue%=colorRGB(0,0,255)
    %pink%=colorRGB(255,0,255)
    %yellow%=colorRGB(255,255,0)
 endIf
 return

 _setupColorOffsets:
 %redOffset%=%blackOffset%+%colorButtonWidth%
 %orangeOffset%=%redOffset%+%colorButtonWidth%
 %greenOffset%=%orangeOffset%+%colorButtonWidth%
 %blueOffset%=%greenOffset%+%colorButtonWidth%
 %pinkOffset%=%blueOffset%+%colorButtonWidth%
 return

 _setupDisplay:
 title "Kaleida"
 setFont 0
 'screen 3 'try 256 colors
 ' mirror
 %x1%=%mirrorOffset%+1
 pushButton #1," ",0,%x1%,1,10,10
 ' pen size
 %x1%=%sizeOffset%+1
 pushButton #2," ",0,%x1%,1,10,10
 if %screenDepth%>=3 then  'color
    gosub _setupColorButtons
 endIf
 ' undo
 %x1%=%undoOffset%+1
 pushButton #15," ",0,%x1%,1,10,10
 image %undoBmp%,%undoOffset%,0
 ' clear
 %x1%=%clearOffset%+1
 pushButton #20," ",0,%x1%,1,10,10
 image %clearBmp%,%clearOffset%,0
 ' help
 %x1%=%helpOffset%+1
 pushButton #30," ",0,%x1%,1,10,10
 image %helpBmp%,%helpOffset%,0
 menu 100
 return

 _setupColorButtons:
 %a1%=%colorButtonWidth%-1
 pushButton #3," ",0,%blackOffset%,1,%a1%,10
 %colorValue%=%black%
 %colorIsCurrent%=true
 %buttonOffset%=%blackOffset%
 gosub _drawColorButton
 pushButton #4," ",0,%redOffset%,1,%a1%,10
 %colorValue%=%red%
 %colorIsCurrent%=false
 %buttonOffset%=%redOffset%
 gosub _drawColorButton
 pushButton #5," ",0,%orangeOffset%,1,%a1%,10
 %colorValue%=%orange%
 %buttonOffset%=%orangeOffset%
 gosub _drawColorButton
 pushButton #6," ",0,%greenOffset%,1,%a1%,10
 %colorValue%=%green%
 %buttonOffset%=%greenOffset%
 gosub _drawColorButton
 pushButton #7," ",0,%blueOffset%,1,%a1%,10
 %colorValue%=%blue%
 %buttonOffset%=%blueOffset%
 gosub _drawColorButton
 pushButton #8," ",0,%pinkOffset%,1,%a1%,10
 %colorValue%=%pink%
 %buttonOffset%=%pinkOffset%
 gosub _drawColorButton
 return

 _drawColorButton: '%colorValue%,%buttonOffset%,%colorIsCurrent%
 'draw a color-coded mask over a pushbutton
 'draw outline
 color %black%
 %leftEdge%=%buttonOffset%-1
 %rightEdge%=%buttonOffset%+%colorButtonWidth%
 boxFilled %leftEdge%,0 to %rightEdge%,12
 'color fill
 color %colorValue%
 %leftEdge%=%buttonOffset%
 dec %rightEdge%
 boxFilled %leftEdge%,1 to %rightEdge%,11
 'selection indicator
 if %colorIsCurrent%=1 then
    color %yellow%
    %leftEdge%=%buttonOffset%-1
    inc %rightEdge%
    box %leftEdge%,0 to %rightEdge%,12
 endIf
 return

 _setMirrors:
 if %mirrorType%=%none% then
    %mirrorType%=%vertical%
    image %verticalBmp%,%mirrorOffset%,0
 else if %mirrorType%=%vertical% then
    %mirrorType%=%slash%
    image %slashBmp%,%mirrorOffset%,0
 else if %mirrorType%=%slash% then
    %mirrorType%=%horizontal%
    image %horizontalBmp%,%mirrorOffset%,0
 else if %mirrorType%=%horizontal% then
    %mirrorType%=%backSlash%
    image %backSlashBmp%,%mirrorOffset%,0
 else if %mirrorType%=%backSlash% then
    %mirrorType%=%verticalHorizontal%
    image %vhBmp%,%mirrorOffset%,0
 else if %mirrorType%=%verticalHorizontal% then
    %mirrorType%=%diagonal%
    image %diagonalBmp%,%mirrorOffset%,0
 else if %mirrorType%=%diagonal% then
    %mirrorType%=%all%
    image %allBmp%,%mirrorOffset%,0
 else
    %mirrorType%=%none%
    image %noneBmp%,%mirrorOffset%,0
 endIf : endIf : endIf : endIf
 endIf : endIf : endIf
 return

 _toggleSize:
 if %penWidth%=%small% then
    image %mediumBmp%,%sizeOffset%,0
    %penWidth%=%medium%
 else
    image %smallBmp%,%sizeOffset%,0
    %penWidth%=%small%
 endIf
 return

 _setBlack:
 gosub _deselectCurrentColor
 %penColor%=%black%
 %colorValue%=%black%
 %colorIsCurrent%=true
 %buttonOffset%=%blackOffset%
 gosub _drawColorButton
 return

 _setRed:
 gosub _deselectCurrentColor
 %penColor%=%red%
 %colorValue%=%red%
 %colorIsCurrent%=true
 %buttonOffset%=%redOffset%
 gosub _drawColorButton
 return

 _setOrange:
 gosub _deselectCurrentColor
 %penColor%=%orange%
 %colorValue%=%orange%
 %colorIsCurrent%=true
 %buttonOffset%=%orangeOffset%
 gosub _drawColorButton
 return

 _setGreen:
 gosub _deselectCurrentColor
 %penColor%=%green%
 %colorValue%=%green%
 %colorIsCurrent%=true
 %buttonOffset%=%greenOffset%
 gosub _drawColorButton
 return

 _setBlue:
 gosub _deselectCurrentColor
 %penColor%=%blue%
 %colorValue%=%blue%
 %colorIsCurrent%=true
 %buttonOffset%=%blueOffset%
 gosub _drawColorButton
 return

 _setPink:
 gosub _deselectCurrentColor
 %penColor%=%pink%
 %colorValue%=%pink%
 %colorIsCurrent%=true
 %buttonOffset%=%pinkOffset%
 gosub _drawColorButton
 return

 _deselectCurrentColor: '%penColor%,%buttonOffset%
 %colorValue%=%penColor%
 %colorIsCurrent%=false
 gosub _drawColorButton
 return

 _clearScreen:
 image %clearBmp%,%clearOffset%,0 'mask button
 savescreen 'save latest
 color %gray%
 boxFilled 0,%topOffset% to %bottomBound%,%bottomBound%
 color %white%
 boxFilled %sideOffset%,%topOffset% to %rightBound%,%bottomBound%
 return

 _handlePen:
 %newPenState%=penDown
 %newX%=penX : %newY%=penY
 gosub _checkBounds
 if %inBounds%=true then
    if %newPenState%=%oldPenState% then 'pen drag
       %x1%=%oldX% : %y1%=%oldY%
       %x2%=%newX% : %y2%=%newY%
       gosub _drawLine
       if %mirrorType%=%vertical% then
          gosub _drawVerticalMirror
       else if %mirrorType%=%slash% then
          gosub _drawSlashMirror
       else if %mirrorType%=%horizontal% then
          gosub _drawHorizontalMirror
       else if %mirrorType%=%backSlash% then
          gosub _drawBackSlashMirror
       else if %mirrorType%=%verticalHorizontal% then
          gosub _drawVHMirrors
       else if %mirrorType%=%diagonal% then
          gosub _drawDiagonalMirrors
       else if %mirrorType%=%all% then
          gosub _drawAllMirrors
       endIf : endIf : endIf : endIf
       endIf : endIf : endIf
    else
       if %newPenState%=1 then 'before drag
          savescreen 'save for undo
       endIf
    endIf
    %oldX%=%newX% : %oldY%=%newY%
    %oldPenState%=%newPenState%
 else
    %oldPenState%=0 'off screen = pen up
 endIf
 return

 _checkBounds: '%newX%,%newY%
 %inBounds%=false 'until proven true
 if %newY%>%topOffset% then
    if %newY%<%bottomBound% then
       if %newX%>%sideOffset% then 'in bounds?
          if %newX%<%rightBound% then
             %inBounds%=true
          endif
       endif
    endif
 endif
 return

 _drawVerticalMirror: '%oldX%,%oldY%,%newX%,%newY%
 %x3%=%oldX%
 gosub _calcMirrorX
 %x1%=%x3%
 %y1%=%oldY% 'same y
 %x3%=%newX%
 gosub _calcMirrorX
 %x2%=%x3%
 %y2%=%newY% 'same y
 gosub _drawLine
 return

 _drawSlashMirror: '%oldX%,%oldY%,%newX%,%newY%
 %x1%=%middleX%-(%oldY%-%middleY%)
 %y1%=%middleY%-(%oldX%-%middleX%)
 %x2%=%middleX%-(%newY%-%middleY%)
 %y2%=%middleY%-(%newX%-%middleX%)
 gosub _drawLine
 return

 _drawHorizontalMirror: '%oldX%,%oldY%,%newX%,%newY%
 %y3%=%oldY%
 gosub _calcMirrorY
 %y1%=%y3%
 %x1%=%oldX% 'same y
 %y3%=%newY%
 gosub _calcMirrorY
 %y2%=%y3%
 %x2%=%newX% 'same y
 gosub _drawLine
 return

 _drawBackSlashMirror: '%oldX%,%oldY%,%newX%,%newY%
 %x1%=%middleX%+(%oldY%-%middleY%)
 %y1%=%middleY%+(%oldX%-%middleX%)
 %x2%=%middleX%+(%newY%-%middleY%)
 %y2%=%middleY%+(%newX%-%middleX%)
 gosub _drawLine
 return

 _drawVHMirrors: '%oldX%,%oldY%,%newX%,%newY%
 gosub _drawVerticalMirror
 gosub _drawHorizontalMirror
 %x4%=%oldX% : %y4%=%oldY% 'preserve
 %x5%=%newX% : %y5%=%newY%
 %oldX%=%x1% : %oldY%=%y1% 'switch
 %newX%=%x2% : %newY%=%y2%
 gosub _drawVerticalMirror
 %oldX%=%x4% : %oldY%=%y4% 'restore
 %newX%=%x5% : %newY%=%y5%
 return

 _drawDiagonalMirrors: '%oldX%,%oldY%,%newX%,%newY%
 gosub _drawSlashMirror
 gosub _drawBackSlashMirror
 %x4%=%oldX% : %y4%=%oldY% 'preserve
 %x5%=%newX% : %y5%=%newY%
 %oldX%=%x1% : %oldY%=%y1% 'switch
 %newX%=%x2% : %newY%=%y2%
 gosub _drawSlashMirror
 %oldX%=%x4% : %oldY%=%y4% 'restore
 %newX%=%x5% : %newY%=%y5%
 return

 _drawAllMirrors: '%oldX%,%oldY%,%newX%,%newY%
 gosub _drawVHMirrors
 gosub _drawSlashMirror
 %x6%=%oldX% : %y6%=%oldY% 'preserve
 %x7%=%newX% : %y7%=%newY%
 %oldX%=%x1% : %oldY%=%y1% 'switch
 %newX%=%x2% : %newY%=%y2%
 gosub _drawVHMirrors
 %oldX%=%x6% : %oldY%=%y6% 'restore
 %newX%=%x7% : %newY%=%y7%
 return

 _drawLine: '%x1%,%y1%,%x2%,%y2%
 color %penColor%
 line %x1%,%y1% to %x2%,%y2%
 if %penWidth%=%medium% then
    gosub _drawThickLine
 endIf
 'gosub _playSound 'experimental
 return

 _drawThickLine: '%x1%,%y1%,%x2%,%y2%
 dec %y1% : dec %y2%
 line %x1%,%y1% to %x2%,%y2%
 dec %x1% : dec %x2%
 line %x1%,%y1% to %x2%,%y2%
 inc %y1% : inc %y2%
 line %x1%,%y1% to %x2%,%y2%
 inc %y1% : inc %y2%
 line %x1%,%y1% to %x2%,%y2%
 inc %x1% : inc %x2%
 line %x1%,%y1% to %x2%,%y2%
 inc %x1% : inc %x2%
 line %x1%,%y1% to %x2%,%y2%
 dec %y1% : dec %y2%
 line %x1%,%y1% to %x2%,%y2%
 dec %y1% : dec %y2%
 line %x1%,%y1% to %x2%,%y2%
 dec %x1% : dec %x2% 'restore
 inc %y1% : inc %y2% 'restore
 return

 _playSound: '%x2%,%y2%
 e=%middleY%-%y2%
 f=abs(e)*10
 u=%middleX%-%x2%
 v=abs(u)/2+10
 sound f,v,10
 return

 _calcMirrorX: '%x3%
 %x3%=%middleX%-(%x3%-%middleX%)
 return '%x3%

 _calcMirrorY: '%y3%
 '%y3%=%y3%-%topOffset%
 %y3%=%middleY%-(%y3%-%middleY%)
 return '%y3%

 _showHelp:
 savescreen
 adviceBox 200
 restorescreen
 image %helpBmp%,%helpOffset%,0
 return

 _undo:
 restorescreen
 image %undoBmp%,%undoOffset%,0
 savescreen 'always have a screen stored
 return

 _handleMenu:
 %a1%=menuItem
 select case %a1%
    case 101 'About
       %a2%=noticeBox(300)
 end select
 return
  pages read