This script is interesting (imho) in that it demonstrates the power that can be achieved with asynchronous events loops. This script has three modes of operations:

1) One can draw lines, boxes and circles in Line Mode - on a painters canvas with colors chosen from a relatively large palette containing a wide range of colors.

2) One can toggle into LiteBrite Mode and draw using colored pegs - that fit into invisible slots in the canvas (just like the old Milton Bradley LiteBrite set from the 60`s - anybody remember that ?)

3) One can toggle into KiXtart PiXaso Mode where a painters brush that has a life of it`s own, fills your canvas with color. Interesting in that one can still draw in either mode while PiXaso is painting.

The other thing here is that two functions from the new KiXtart UDF ToolKit Library are being used here (ok - one really), specifically, TkAboutBox() and TkInputBox. These can be used just as easily in a non-kixforms script as within a forms script ...

Heres a screenshot:



Heres the sourcecode:

code:
Break On

;
$APPLICATION = "KiXtart PiXaso Painter"
$VERSION = "1.2"
$AUTHOR = "by Shawn Tassie (www.kixtart.org)"

$COPYRIGHT =
"Copyright (c) 2002 - Freeware
Bounce Technology (c) Chris (cj) Matheson
LiteBrite (c) Milton Bradley Inc.
Kixtart v@KIX (c) Ruud Van Velson"

;
; LiteBrite Constants - Used in LiteBrite mode
;

$CELLWIDTH = 12
$CELLHEIGHT = 12
$CIRCLERADIUS = 5
$MAXCOL = 44
$MAXROW = 31
$XOFFSET = 0
$YOFFSET = 0
$MAXX = $MAXCOL * $CELLWIDTH + $XOFFSET
$MAXY = $MAXROW * $CELLHEIGHT + $YOFFSET

SRND((-1)*@TICKS) ; Randomize

$Form = CreateObject("Kixtart.Form")

$Form.Width = 640
$Form.Height = 490
$Form.Center
$Form.FontName = "Arial"
$Form.FontSize = 10
$Form.Caption = $APPLICATION

$Clear = $Form.CommandButton("Clear")
$Clear.Left = 555
$Clear.Top = 20
$Clear.Width = 70
$Clear.OnClick = "Clear_Click"

$Line = $Form.ToggleButton("Line")
$Line.Left = 555
$Line.Top = 70
$Line.Width = 70
$Line.FontBold = 1
$Line.OnClick = "Line_Click"

$Brite = $Form.ToggleButton("LiteBrite")
$Brite.Left = 555
$Brite.Top = 110
$Brite.Width = 70
$Brite.FontBold = 0
$Brite.OnClick = "Brite_Click"

$Pixaso = $Form.ToggleButton("Pixaso!")
$Pixaso.Left = 555
$Pixaso.Top = 200
$Pixaso.Width = 70
$Pixaso.OnClick = "Pixaso_Click"

$Circles = $Form.CommandButton("Circles")
$Circles.Left = 555
$Circles.Top = 230
$Circles.Width = 70
$Circles.OnClick = "Circles_Click"

$Boxes = $Form.CommandButton("Boxes")
$Boxes.Left = 555
$Boxes.Top = 260
$Boxes.Width = 70
$Boxes.OnClick = "Boxes_Click"

$Options = $Form.CommandButton("Options...")
$Options.Left = 555
$Options.Top = 355
$Options.Width = 70
$Options.OnClick = "Options_Click"

$About = $Form.CommandButton("About")
$About.Left = 555
$About.Top = 385
$About.Width = 70
$About.OnClick = "TkAboutBox($$APPLICATION,$$VERSION,$$AUTHOR,$$COPYRIGHT)"

$Exit = $Form.CommandButton("Exit")
$Exit.Left = 555
$Exit.Top = 415
$Exit.Width = 70
$Exit.OnClick = "Exit_Click"

$Canvas = $Form.PictureBox
$Canvas.Left = 10 ; broken
$Canvas.Top = 10 ; broken
$Canvas.Width = 534
$Canvas.Height = 378
$Canvas.BackColor = 0
$Canvas.ForeColor = $Form.RGB(255,255,255)
$Canvas.OnMouseMove = "Canvas_MouseMove"
$Canvas.OnMouseDown = "Canvas_MouseDown"
$Canvas.OnMouseUp = "Canvas_MouseUp"
$Canvas.DrawWidth = 1

$Color = $Form.Label
$Color.Left = 10
$Color.Top = 395
$Color.Width = 48
$Color.Height = 48
$Color.BorderStyle = 1
$Color.BackColor = $Form.RGB(0,255,0)

$Palette = $Form.PictureBox("",73,395)
$Palette.Width = 473
$Palette.Height = 48
$Palette.OnClick = "Palette_Click"

$Left = 0
$Top = 0

$Palette.FillStyle = 1
For $b = 0 to 255 Step 51
For $g = 0 to 255 Step 51
For $r = 0 to 255 Step 51
$Palette.FillColor = ($b*256*256)+($g*256)+$r
$Palette.Rectangle($Left,$Top,14,8)
$Left = $Left + 13
if $Left = 468
$Left = 0
$Top = $Top + 7
EndIf
Next
Next
Next

;=================
; Initalize script
;=================

$DrawWidth = 1 ; thin
$FillStyle = 1 ; solid

$Canvas.ForeColor = $Color.BackColor

$StartPoint_X = -1
$StartPoint_Y = -1
$EndPoint_X = -1
$EndPoint_Y = -1

$Line_Mode = 0
$Brite_Mode = 1

Line_Click()

$Form.Show
While $Form.Visible
$=Execute($Form.DoEvents)
Loop

Exit 1

Function Clear_Click
$Canvas.BackColor = 0
EndFunction

Function Line_Click
$Mode = $Line_Mode
$Line.FontBold = 1
$Line.Value = 1
$Brite.FontBold = 0
$Brite.Value = 0
EndFunction

Function Brite_Click
$Mode = $Brite_Mode
$Line.Fontbold = 0
$Line.Value = 0
$Brite.Fontbold = 1
$Brite.Value = 1
EndFunction

Function Pixaso_Click
If $Pixaso.Caption = "Pixaso!"
$Pixaso.FontBold = 1
$Pixaso.Caption = "Running"
Pixaso($Canvas,1)
Else
$Pixaso.FontBold = 0
$Pixaso.Caption = "Pixaso!"
Pixaso($Canvas,0)
EndIf
EndFunction

Function Exit_Click
Quit()
EndFunction

Function Canvas_MouseMove()
If $Mode = $Line_Mode
$Canvas.ForeColor = $Color.BackColor
$EndPoint_X = $Canvas.MouseX
$EndPoint_Y = $Canvas.MouseY
If ($StartPoint_X <> -1 )
$Canvas.CurrentX = $StartPoint_X
$Canvas.CurrentY = $StartPoint_X
$Canvas.Line($StartPoint_X,$StartPoint_Y,$EndPoint_X,$EndPoint_Y)
$StartPoint_X = $EndPoint_X;
$StartPoint_Y = $EndPoint_Y;
EndIf
Else
If $Drawing
AddCell($Canvas.MouseX,$Canvas.MouseY,$Color.BackColor)
EndIf
EndIf
EndFunction

Function Canvas_MouseDown()
If $Mode = $Line_Mode
$StartPoint_X = $Canvas.MouseX
$StartPoint_Y = $Canvas.MouseY
Else
$Drawing = 1
AddCell($Canvas.MouseX,$Canvas.MouseY,$Color.BackColor)
EndIf
EndFunction

Function Canvas_MouseUp()
If $Mode = $Line_Mode
$StartPoint_X = -1
$StartPoint_Y = -1
Else
$Drawing = 0
EndIf
EndFunction

Function Palette_Click
$Canvas.ForeColor = $Palette.Point($Palette.MouseX,$Palette.MouseY)
$Color.BackColor = $Canvas.ForeColor
EndFunction

Function AddCell($x,$y,$c)
dim $col,$row
If $Drawing
If ( $x > $XOFFSET and $x < $MAXX and $y > $YOFFSET and $y < $MAXY )
$row = ($y - $YOFFSET) / $CELLHEIGHT
$col = ($x - $XOFFSET) / $CELLWIDTH
$Canvas.ForeColor = $c
$Canvas.FillStyle = $FillStyle ; Solid
$Canvas.FillColor = $c
$cx = $col*$CELLWIDTH+$XOFFSET+$CELLWIDTH/2
$cy = $row*$CELLHEIGHT+$YOFFSET+$CELLHEIGHT/2
$Canvas.Circle($cx,$cy,$CIRCLERADIUS)
If $c
$Canvas.ForeColor = &0FFFFFF
$Canvas.FillColor = &0FFFFFF
$Canvas.Circle($cx,$cy-1,2)
EndIf
EndIf
EndIf
EndFunction

Function Pixaso($Form,$State)
;
; The pixaso asynchronous event loop
;
; Performs DoEvents(1) [1=NoWait] while simultaneously
; drawing randomly colored lines on the form
; provided. This function demonstrates the
; multi-threaded-like aspects of running a form
; asynchronously. Calling this function with $State
; set to 1 (true) starts the thread. Called
; a second time with $state set to 0 (false)
; terminates the function. Bascially, this function
; 'hijacks' the main script event loop. The global
; variable $_pixaso controls the looping mechanism
; Only known issue right now is that we loose form
; tabbing and the ability to close the script when
; the thread is running - im working on that.
;
Dim $x1,$y1,$x2,$y2,$s
Dim $x1d,$y1d,$x2d,$y2d
Dim $width,$height
Dim $red,$green,$blue

If Not $State
$_Pixaso = 0 ; Turn off the running thread
Return
EndIf

$_Pixaso = 1

$width = $Form.ScaleWidth
$height = $Form.ScaleHeight

$x1=RND($width)
$y1=RND($height)
$x2=RND($width)
$y2=RND($height)

$s = 3 ; speed (pixel jumps per draw)

; initialize deltas

$x1d = 1
$y1d = 2
$x2d = -1
$y2d = -2

While $_Pixaso

$= Execute($Form.DoEvents(1))

$red=$red+1
if $red > 255
$red=RND(255)
endif

$blue=$blue+1
if $blue > 255
$blue=RND(255)
endif

$green=$green+1
if $green > 255
$green=RND(255)
endif

$Form.ForeColor = $Form.RGB($red,$green,$blue)

$Form.Line($x1, $y1, $x2, $y2)

$x1=$x1+$x1d
if $x1 > $width
$x1d=0-(RND($s)+1)
endif
if $x1<0
$x1d=RND($s)+1 ; 2
endif
$y1=$y1+$y1d
if $y1>$height
$y1d=0-(RND($s)+1)
endif

if $y1<0
$y1d=RND($s)+1
endif
$x2=$x2+$x2d
if $x2>$width
$x2d=0-(RND($s)+1)
endif
if $x2<0;
$x2d=RND($s)+1
endif
$y2=$y2+$y2d
if $y2>$height
$y2d=0-(RND($s)+1)
endif
if $y2<0;
$y2d=RND($s)+1
endif

Loop

EndFunction

Function Circles_Click
;
; Draws a random number of randomly colored circles
; on the canvas
;
$Canvas.fillstyle = $fillstyle
For $i = 0 to RND(15)+5
$Canvas.ForeColor = $Canvas.RGB(RND(255),RND(255),RND(255))
$Canvas.FillColor = $Canvas.RGB(RND(255),RND(255),RND(255))
$Canvas.Circle(RND($Canvas.Width),RND($Canvas.Height),RND($Canvas.Width)/5)
Next
EndFunction

Function Boxes_Click
;
; Draws a random number of randomly colored boxes
; on the canvas
;
$Canvas.FillStyle = $FillStyle
For $i = 0 To RND(15)+5
$Canvas.ForeColor = $Canvas.RGB(RND(255),RND(255),RND(255))
$Canvas.FillColor = $Canvas.RGB(RND(255),RND(255),RND(255))
$Canvas.Rectangle(RND($Canvas.Width),RND($Canvas.Height),RND($Canvas.Width)/3,RND($Canvas.Height)/3)
Next
EndFunction

Function Options_Click
;
; Allows user to change the following (global) script variables:
;
; $DrawWidth
; $FillStyle
;
Dim $Form
Dim $Frame1,$Thin,$Medium,$Thick
Dim $Frame2,$Hollow,$Solid
Dim $Ok

$Form = CreateObject("Kixtart.Form")

$Form.Caption = "Options"
$Form.Width = 240
$Form.Height = 200
$Form.Center
$Form.FontName = "Arial"
$Form.FontSize = 10

$Frame1 = $Form.Frame
$Frame1.Caption = "DrawWidth"
$Frame1.Left = 10
$Frame1.Top = 10
$Frame1.Width = 100
$Frame1.Height = 90

$Thin = $Frame1.OptionButton
$Thin.Caption = "Thin"
$Thin.Left = 10
$Thin.Top = 20
$Thin.Width = 75
$Thin.Height = 20
$Thin.Value = $DrawWidth = 1
$Thin.OnClick = "$$DrawWidth = 1"

$Medium = $Frame1.OptionButton
$Medium.Caption = "Medium"
$Medium.Left = 10
$Medium.Top = 40
$Medium.Width = 75
$Medium.Height = 20
$Medium.Value = $DrawWidth = 3
$Medium.OnClick = "$$DrawWidth = 3"

$Thick = $Frame1.OptionButton
$Thick.Caption = "Thick"
$Thick.Left = 10
$Thick.Top = 60
$Thick.Width = 75
$Thick.Height = 20
$Thick.value = $DrawWidth = 6
$Thick.OnClick = "$$DrawWidth = 6"

$Frame2 = $Form.Frame
$Frame2.Caption = "FillStyle"
$Frame2.Left = 120
$Frame2.Top = 10
$Frame2.Width = 100
$Frame2.Height = 90

$Hollow = $Frame2.OptionButton
$Hollow.Caption = "Hollow"
$Hollow.Left = 10
$Hollow.Top = 20
$Hollow.Width = 75
$Hollow.Height = 20
$Hollow.Value = $FillStyle = 0
$Hollow.OnClick = "$$FillStyle=0"

$Solid = $Frame2.OptionButton
$Solid.Caption = "Solid"
$Solid.Left = 10
$Solid.Top = 40
$Solid.Width = 75
$Solid.Height = 20
$Solid.Value = $FillStyle = 1
$Solid.OnClick = "$$FillStyle = 1"

$Ok = $Form.CommandButton("OK",80,120,70,25)
$Ok.Default = 1
$Ok.OnClick = "$$Form.Hide"

$Form.Show

While $Form.Visible
$=Execute($Form.DoEvents)
Loop

$Canvas.DrawWidth = $DrawWidth

EndFunction

Function TkInputBox (
Optional $Prompt,
Optional $Title,
Optional $Default,
Optional $Left,
Optional $Top
)
;
; TkInputBox() is part of the Kixforms Toolkit.
;
; Displays a dialog containing an inputbox and asks the
; user to enter a value. The user can press OK or cancel
; to dismiss this dialog.
;
Dim $Form,$Label,$Textbox,$Ok,$Cancel
Dim $Okayed,$Canceled
$Form = CreateObject("Kixtart.Form")
$Form.Caption = $title
$Form.Width = 363
$Form.Height = 163
$Form.Center
$Label = $Form.Label($Prompt,10,20,200,75)
$Ok = $Form.CommandButton("OK",275,20,71,21)
$Ok.Default = 1 ; true
$ok.OnClick = "$$okayed=1"
$Cancel = $Form.CommandButton("Cancel",275,50,71,21)
$Cancel.Cancel = 1 ; true
$Cancel.OnClick = "$$Canceled=1"
$TextBox = $Form.TextBox($Default,10,100,334,21)
$Form.show
While $Form.Visible And Not $Okayed And Not $Canceled
$=Execute($Form.DoEvents) ; Synchronous
Loop
If $Okayed
$TkInputBox = $TextBox.Text
Else
$TkInputBox = ""
EndIf
$Form.Hide
EndFunction

Function TkAboutBox(
$Name,
$Version,
$Author,
$Message)
;
; TkAboutBox() is part of the Kixforms Toolkit.
;
; Displays a dialog box containing script name, version,
; author name and a general information. User must press
; the OK button to dismiss.
;
Dim $Form
Dim $Script,$Vers,$Auth,$Msg
Dim $Ok

$Form = CreateObject("Kixtart.Form")

$Form.ScaleWidth = 400
$Form.ScaleHeight = 260
$Form.Center
$Form.Caption = "About "+$name
$Form.FontName = "Verdana"
$Form.FontSize = 10

$Script = $Form.Label("",10,20,$Form.ScaleWidth-20,40)
$Script.FontName = "Arial Black"
$Script.FontSize = 18
$Script.FontBold = 1
$Script.Caption = $Name
$Script.Alignment = 2 ; Centered

$Vers = $Form.Label("",10,70,$Form.ScaleWidth-20,20)
$Vers.Caption = "Version $version"
$Vers.Alignment = 2 ; Centered

$Auth = $Form.Label("",10,100,$Form.ScaleWidth-20,20)
$Auth.Caption = $author
$Auth.Alignment = 2 ; Centered

$Msg = $Form.Label("",10,130,$Form.ScaleWidth-20,80)
$Msg.Caption = $message
$Msg.Alignment = 2 ; Centered

$Ok = $Form.CommandButton("OK",160,220,75,25)
$Ok.OnClick = "$$Form.Hide"
$Ok.Default = 1 ; True

$Form.Show
While $Form.Visible
$=Execute($Form.DoEvents)
Loop

EndFunction

-Shawn

[ 16 March 2002, 15:24: Message edited by: Shawn ]