diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-02-24 02:13:59 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-02-24 02:13:59 +0000 |
commit | a6d58bb6052ac8cb01805a48c4ad2f129126116f (patch) | |
tree | dd867a099fcbb263a8009a9fb22695b87855dad6 /doc/scriptexamples | |
download | kvirc-a6d58bb6052ac8cb01805a48c4ad2f129126116f.tar.gz kvirc-a6d58bb6052ac8cb01805a48c4ad2f129126116f.zip |
Added KDE3 version of kvirc
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/kvirc@1095341 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'doc/scriptexamples')
43 files changed, 3207 insertions, 0 deletions
diff --git a/doc/scriptexamples/Makefile.am b/doc/scriptexamples/Makefile.am new file mode 100644 index 00000000..a63d4435 --- /dev/null +++ b/doc/scriptexamples/Makefile.am @@ -0,0 +1,7 @@ +############################################################################### +# KVirc IRC client Makefile - 16.12.98 Szymon Stefanek <stefanek@tin.it> +############################################################################### + +SUBDIRS = antiidle minesweeper mp3share playing runmenu simplehttp tutorial + +EXTRA_DIST = *.kvs diff --git a/doc/scriptexamples/README b/doc/scriptexamples/README new file mode 100644 index 00000000..be35ddce --- /dev/null +++ b/doc/scriptexamples/README @@ -0,0 +1,6 @@ +This directory and the subdirectories contain a set of KVIrc scripting examples/tests. + +The *.kvs files in this directory are mostly tests written while implementing scripting +language features. They're rather simple (but also useless) so you can use them to learn basic scripting :) + +The subdirectories contain more complex scripts written with the demonstration purpose. diff --git a/doc/scriptexamples/antiidle/Makefile.am b/doc/scriptexamples/antiidle/Makefile.am new file mode 100644 index 00000000..840a6b4e --- /dev/null +++ b/doc/scriptexamples/antiidle/Makefile.am @@ -0,0 +1,5 @@ +############################################################################### +# KVirc IRC client Makefile - 16.12.98 Szymon Stefanek <stefanek@tin.it> +############################################################################### + +EXTRA_DIST = antiidle.kvs diff --git a/doc/scriptexamples/antiidle/antiidle.kvs b/doc/scriptexamples/antiidle/antiidle.kvs new file mode 100644 index 00000000..a4b59cca --- /dev/null +++ b/doc/scriptexamples/antiidle/antiidle.kvs @@ -0,0 +1,159 @@ +# +# anti idle script +# : some people like it, some people need it +# : by Balboy :)) +# + +# creating the main alias + +alias(aidle) +{ + # using a switch to see what option has been selected + switch($0) + { + # ok, so the script should start + case(start): + { + # checking if it wasn't running already + if(!%Aidlestatus{$ic}) + { + # the script wasn't running already, so it should be started by calling the + # script with the run parameter + aidle internal_run + # saving the status of the anti idle script + %Aidlestatus{$ic} = 1 + # an anti idle var is set, to define the speed of the anti itde script + # there are 2 possible ways + %Aidletype{$ic} = 1 + echo "Anti idle has been enabled" + } else { + # the script was already running + echo "Anti idle has already been enabled on this server" + } + } + break + # the script should be halted + case(stop): + { + # checking if it was already running + if(%Aidlestatus{$ic}) + { + # the script was running, so it should be stopped by killing the timer + killtimer antiidle{$ic} + # clearing variabled + %Aidlestatus{$ic} = "" + %Aidletype{$ic} = "" + echo "Anti idle has been disabled" + } else { + # the script wasn't running + echo "Anti idle has not been enabled on this server" + } + } + break + # the script status should be shown + case(status): + { + # checking the status variable + if(%Aidlestatus{$ic}) + { + echo "anti idle is enabled on this server" + } else { + echo "anti idle is disabled on this server" + } + } + break + # this is a parameter that should only be called by the script itself + # this is where the idle time gets actualy killed + + case(internal_run): + { + # this part will make a varianle to switch random anti idle modes (fast/slow) + # note: once a type has been decided, changing the type works by a small chance + %aidletypecheck{$ic} = $rand(100) + if(%aidletypecheck{$ic} < 10) + { + if(%Aidletype{$ic} == 1) + { + %Aidletype{$ic} = 2 + } else { + %Aidletype{$ic} = 1 + } + } + # use the var just created to start one of the anti idle modes + if(%Aidletype{$ic} == 1){ + # starting a timer who will keep sending messages to yourself, no output will be shown + timer -r=$console -s (antiidle{$ic},$(10000 + $rand(50000))) + { + aidle internal_run + # checking if we are online + if($server) + { + raw -q privmsg $me : + } + } + } else { + # this part is simular as above + timer -r=$console -s (antiidle{$ic},$(10000 + $rand(30000))) + { + aidle internal_run + if($server) + { + raw -q privmsg $me : + } + } + } + } + break + # the script should be uninstalled + case(uninstall): + { + echo "the anti idle script has been uninstalled succesfully" + # defining the alias by empty code will remove it + alias(aidle){} + event(412,aidle){} + + } + break + # if help was asked + case(help): + { + echo "usage: /aidle <option>" + echo "" + echo "possible options:" + echo "start start the anti idle" + echo "stop stop the anti idle" + echo "status tell you if the anti idle script is enabled or not" + echo "uninstall uninstall the script" + echo "help this help output" + } + break + # if the parameter you give with the anti idle script is not valid + # a small program usage is printed + default + { + echo "Unsupported anti idle parameter ($0)" + echo "Try '/aidle help' for more information." + } + } +} + + +# creating an event + +event(412,aidle) +{ + # this raw will stop the error messages you will get from the server, + # every time you send an empty msg to yourself + halt; +} + +# and at last, we also put some output, with some information :) + +echo "installation of the anti idle script succesful" +echo "you can always uninstall it by typing /aidle uinstall" +echo "just type '/aidle help' to get all the possible commands" +echo "---" +echo "Happy ircing or should i say idling :)" +echo "Balboy and The KVIrc Development Team" + +# hint for later: $window.list(console,all) diff --git a/doc/scriptexamples/class1.kvs b/doc/scriptexamples/class1.kvs new file mode 100644 index 00000000..6098000d --- /dev/null +++ b/doc/scriptexamples/class1.kvs @@ -0,0 +1,102 @@ +# Example of object function overriding and calling +# This is quite intricated...try to get out of this +# by examining the code. + +class (A,object) +{ + # The base class at all + # Implements $A::function and $A::virtual + function + { + echo -i=10 "$0\Entering A::function()" + $$->$virtual("----$0") + echo -i=10 "$0\Exiting A::function()" + } + virtual + { + echo -i=10 "$0\This is A::virtual()" + } +} + +class (B,A) +{ + # Derived from A : inherits $A::function() + # overrides $A::virtual + virtual + { + echo -i=20 "$0\Entering B::virtual()" + $$->$A::virtual("----$0") + echo -i=20 "$0\Exiting B::virtual()" + } +} + +class (C,B) +{ + # Derived from B : inherits $B::virtual and + # overrides $B::function (that is $A::function) + function + { + echo -i=30 "$0\This is C::function" + } +} + +echo -i=7 "Ok...now try to gusess what's going on :)" + +%o = $new(C,0,test) + +echo "# Calling \%o->\$function" +%o->$function +echo "# Calling \%o->\$C::function" +%o->$C::function +echo "# Calling \%o->\$B::function" +%o->$B::function +echo "# Calling \%o->\$A::function" +%o->$A::function +echo "# Implementing private \%o->\$function" +privateimpl(%o,function) +{ + # This is a private implementation that + # overrides $C::function + echo "$0\Entering \$\$::function" + $$->$C::function("----$0"); + echo "$0\Exiting \$\$::function" +} + +echo "# Calling \%o->\$function" +%o->$function +echo "# Calling \%o->\$C::function" +%o->$C::function +echo "# Calling \%o->\$B::function" +%o->$B::function +echo "# Calling \%o->\$A::function" +%o->$A::function +echo "# Implementing private \%o->\$virtual" +privateimpl(%o,virtual) +{ + # This is a private implementation that + # overrides $C::virtual (that is $B::virtual in fact) + echo "$0\Entering \$\$::virtual" + $$->$C::virtual("----$0") + echo "$0\Exiting \$\$::virtual" +} + +echo "# Calling \%o->\$function" +%o->$function +echo "# Calling \%o->\$C::function" +%o->$C::function +echo "# Calling \%o->\$B::function" +%o->$B::function +echo "# Calling \%o->\$A::function" +%o->$A::function +echo "# Removing private \%o->\$function" +privateimpl(%o,function){} +echo "# Calling \%o->\$function" +%o->$function +echo "# Calling \%o->\$C::function" +%o->$C::function +echo "# Calling \%o->\$B::function" +%o->$B::function +echo "# Calling \%o->\$A::function" +%o->$A::function + +delete %o diff --git a/doc/scriptexamples/exprtest.kvs b/doc/scriptexamples/exprtest.kvs new file mode 100644 index 00000000..c07d199d --- /dev/null +++ b/doc/scriptexamples/exprtest.kvs @@ -0,0 +1,24 @@ +# A stresstest for the expression evaluation engine + +alias(exprtest) +{ + %txt = "Testing expression:" $0; + + %ret = ${ eval "return \$(" $0 ")"; } + if(%ret != $1) + { + %txt .= " ... Failed (return value" %ret "!=" $1")"; + echo %txt + } else { + %txt .= " ... Success (return value $1)"; + echo %txt + } +} + +exprtest "5 - 0 - 1" 4 +exprtest "5 - (0 - 1)" 6 +exprtest "2+3-5+3+1-2-3-8+5*3-2" 4 +exprtest "2*2 + 2*2 - 2*2" 4 +exprtest "-1" -1 +exprtest "--1--1" 2 +exprtest "-1+1" -2 diff --git a/doc/scriptexamples/label1.kvs b/doc/scriptexamples/label1.kvs new file mode 100644 index 00000000..d66cf154 --- /dev/null +++ b/doc/scriptexamples/label1.kvs @@ -0,0 +1,39 @@ +# A little label test. + +class(myLabel, label) +{ + constructor + { + $$->$setBackgroundColor(dcdc00); + $$->$setAutoResize(0); + $$->$setGeometry(10, 10, 500, 150); + $$->$setAlignment(Center); + $$->$setText("Come here !"); + } + + mouseEnterEvent + { + $$->$setMargin(10); + $$->$setAlignment(Right, Top); + %al = "My alignment is $$->$alignment(), and I have margin $$->$margin() pixels wide !"; + $$->$setText(%al); + $$->$setFrameStyle(Panel, Raised); + echo "Label frame style: $$->$frameStyle()"; + } + + mouseLeaveEvent + { + $$->$setMargin(0); + $$->$setAlignment(Left, Bottom); + %al = "My alignment is $$->$alignment(), and I have margin $$->$margin() pixels wide !"; + $$->$setText(%al); + $$->$setFrameStyle(Panel, Sunken); + echo "Label frame style: $$->$frameStyle()"; + } +} + +%w = $new(widget, 0); +%w->$resize(520, 170); +%w->$move(50, 50); +%l = $new(myLabel, %w); +%w->$show(); diff --git a/doc/scriptexamples/mail.kvs b/doc/scriptexamples/mail.kvs new file mode 100644 index 00000000..94fb70e2 --- /dev/null +++ b/doc/scriptexamples/mail.kvs @@ -0,0 +1,159 @@ +# This is a really basic mail sender script +# It uses exec and a telnet slave process +# You will probably need to hack it a bit since +# most mail servers will simply refuse to +# send so spam-looking mail :D + +# Ah... remember that spamming creates bad karma. + + +# exec a telnet slave. +# Since we want to make sure that it does not run for too long, +# we give it 60 seconds to complete the operations + +exec -x -k=60000 -t -e ("telnet my.mail.server.com 25") +{ + # We keep the state in the %:state extended scope variable + + if($0 == "started") + { + # This is the first event triggered + # Initialize our variables... + echo [process started] + %:state = 0 + # This is the source address as specified in the MAIL command + %:from = "<me@me.com>" + %:rcpt = "<me@me.com>" + # This is the From field + %:sender = "\"Me\" <me@me.com>" + %:receiver = "\"Me\" <me@me.com>" + %:body = "Hello, this is a test mesage for the small KVIrc mail script$cr$lf" + %:body .= "I hope that you have fun writing in kvs :)))" + %:subject = "This is a test subject" + # Returning an empty string does not write to stdin + return + } + + if($0 == "stderr") + { + # This is our stderr handler.. just in case something goes wrong + echo "[stderr] $1" + return + } + + if($0 == "terminated") + { + echo "[process terminated]" + return + } + + # Ok.. this must be a stdout data event + # Echo the line to the user + echo "[stdout] $1" + + # And make our little state machine work + switch(%:state) + { + case(0): + { + # We're waiting for 220 (ready) + if($str.match("220*",$1)) + { + # Send the HELO and go to next state + %:state++ + echo "Sending HELO..." + return "HELO myhostname$cr$lf"; + } + } + break + case(1): + { + # Waiting for 250 (after the HELO) + if($str.match("250*",$1)) + { + # Send the MAIL command + %:state++ + echo "Sending MAIL..." + return "MAIL From: %:from$cr$lf" + } else { + # The server replied with something that is not a 250... + # Fail :/ + echo "HELO command not accepted: $1" + halt + } + } + break; + case(2): + { + # Waiting for another 250 (MAIL accepted) + if($str.match("250*",$1)) + { + # ... + %:state++ + echo "Sending RCPT..." + return "RCPT To: %:rcpt$cr$lf" + } else { + echo "MAIL command not accepted: $1" + halt + } + } + break; + case(3): + { + # Waiting for another 250 (RCPT accepted) + if($str.match("250*",$1)) + { + %:state++ + echo "Sending DATA..." + return "DATA$cr$lf" + } else { + echo "RCPT not accepted: $1" + halt + } + } + break; + case(4): + { + # Waiting for 354 (ok, go on) + if($str.match("354*",$1)) + { + # You will probably need to hack in the Date: field :) + %:state++ + echo "Sending body..." + %x = "From: %:sender$cr$lf" + %x .= "To: %:receiver$cr$lf" + %x .= "Subject: %:subject$cr$lf" + %x .= "Reply-To: %:sender$cr$lf" + %x .= "Date: Thu, 8 Apr 2004 05:28:01 +0200$cr$lf" + %x .= "X-Mailer: KVIrc funky KVS script$cr$lf" + %x .= "$cr$lf" + %x .= "%:body$cr$lf$cr$lf" + %x .= ".$cr$lf" + return %x + } else { + echo "DATA not accepted: $1" + halt + } + } + break; + case(5): + { + # We don't wait anymore :) + %:state++ + echo "Sending QUIT..." + return "QUIT$cr$lf" + } + break; + default: + { + # Usually the mail server closes the connection + %:state++ + if(%:state > 10) + { + # But if it does not in few messages + # Then force the process to die + halt + } + } + } +}
\ No newline at end of file diff --git a/doc/scriptexamples/minesweeper/Makefile.am b/doc/scriptexamples/minesweeper/Makefile.am new file mode 100644 index 00000000..914e3823 --- /dev/null +++ b/doc/scriptexamples/minesweeper/Makefile.am @@ -0,0 +1,5 @@ +############################################################################### +# KVirc IRC client Makefile - 16.12.98 Szymon Stefanek <stefanek@tin.it> +############################################################################### + +EXTRA_DIST = *.kvs diff --git a/doc/scriptexamples/minesweeper/minesweeper.kvs b/doc/scriptexamples/minesweeper/minesweeper.kvs new file mode 100644 index 00000000..24f3b0cb --- /dev/null +++ b/doc/scriptexamples/minesweeper/minesweeper.kvs @@ -0,0 +1,131 @@ + + +class(minelabel,label) +{ + mousePressEvent() + { + $$->$parent()->$mineLabelPressed($this) + } +} + +class(minesweeper,widget) +{ + constructor() + { + $$->$setCaption("KVIrc's Minesweeper (0.1.0)"); + + $$->%rows = 10 + $$->%cols = 10 + $$->%mines = 10 + + $$->%layout = $new(layout,$this) + + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + $$->%label{%i,%j}=$new(minelabel,$this,"%i_%j") + $$->%label{%i,%j}->%row = %i + $$->%label{%i,%j}->%col = %j + $$->%layout->$addWidget($$->%label{%i,%j},%i,%j) + } + } + + $$->$newGame() + } + + destructor() + { + } + + newGame() + { + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + %l = $$->%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->$setText("") + } + } + # drop the mines + for(%i = 0;%i < $$->%mines;%i++) + { + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + while($$->%label{%row,%col}->%bIsMine != 0) + { + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + } + $$->%label{%row,%col}->%bIsMine = 1 + # increase the mine count for the adiacent cells + if(%row > 0) + { + $$->%label{$(%row - 1),%col}->%numMines++ + if(%col > 0)$$->%label{$(%row - 1),$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < ($$->%rows - 1)) + { + $$->%label{$(%row + 1),%col}->%numMines++ + if(%col > 0)$$->%label{$(%row + 1),$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + if(%col > 0)$$->%label{%row,$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{%row,$(%col + 1)}->%numMines++ + } + } + + mineLabelPressed($0 = mine label object that has been pressed) + { + #echo "MINE LABEL PRESSED $0" + if($0->%bIsMine) + { + #echo "IS MINE!" + $0->$setFrameStyle(WinPanel,Sunken) + $0->$setText("*") + } else { + #echo "IS NOT MINE" + $$->$discoverCells($0) + } + } + + discoverCells($0 = mine label that has to be discovered) + { + #echo "Discover cells $0" + if($0->%bIsMine)return; + if($0->%bIsDiscovered)return; + $0->%bIsDiscovered = 1 + $0->$setFrameStyle(WinPanel,Sunken) + if($0->%numMines > 0)$0->$setText($0->%numMines) + else { + if($0->%row > 0) + { + $$->$discoverCells($$->%label{$($0->%row - 1),$0->%col}) + if($0->%col > 0)$$->$discoverCells($$->%label{$($0->%row - 1),$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$($0->%row - 1),$($0->%col + 1)}) + } + if($0->%row < ($$->%rows - 1)) + { + $$->$discoverCells($$->%label{$($0->%row + 1),$0->%col}) + if($0->%col > 0)$$->$discoverCells($$->%label{$($0->%row + 1),$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$($0->%row + 1),$($0->%col + 1)}) + } + if($0->%col > 0)$$->$discoverCells($$->%label{$0->%row,$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$0->%row,$($0->%col + 1)}) + } + } + +} + +alias(mines) +{ + %m = $new(minesweeper) + %m->$show() + +} diff --git a/doc/scriptexamples/mp3_kdemediaplayers.kvs b/doc/scriptexamples/mp3_kdemediaplayers.kvs new file mode 100644 index 00000000..98be29e9 --- /dev/null +++ b/doc/scriptexamples/mp3_kdemediaplayers.kvs @@ -0,0 +1,206 @@ + + +%Xmms_play=$mp3player.getfilename; + + + +%Prefix = $str.left(%Xmms_play,4) +%Postfix = $str.right(%Xmms_play,4) + %Http_prefix = "http" + %Cda_postfix = ".cda" + if(%Prefix == %Http_prefix) { + + %Stime = $($mp3player.gettime/1000) + %Mtime = 0 + %Ctime =" "; + + while(%Stime > 59) { + %Mtime++ + %Stime-=60 + } + + %Htime=0 + + while(%Mtime > 59) { + %Htime++ + %Mtime-=60 + } + + if (%Stime < 10){ + %tmp = %Stime + %Stime = $str.append("0",%tmp) + } + if (%Mtime < 10 && %Htime > 1){ + %tmp = %Mtime + %Mtime = $str.append("0",%tmp) + } + + + + if (%Htime > 0) + %Ctime = $str.append %Htime:%Mtime:%Stime + + + + if (%Mtime > 0 && %Htime < 1) + %Ctime = $str.append %Mtime:%Stime + + + + if (%Mtime < 1 && %Htime < 1) + %Ctime = %Stime + + + + me muckelt grad [$mp3player.gettitle @ %Xmms_play - $($mp3player.getrate/1000) kbps - $($mp3player.getfreq/1000) kHz - seit: %Ctime (h:m:s)] + } + else if(%Postfix == %Cda_postfix) { + me muckelt grad [$mp3player.gettitle - Audio CD] + + } + + + else { + + if ($mp3player.getchannels == 2) + %Channels = Stereo + else + %Channels = Mono + + %Frequ = $mp3player.getfreq + %Frequ2 = $(%Frequ/1000) + + %tmins = $($mp3player.getlength / 60000) + %tsecs = $(($mp3player.getlength % 60000) / 1000) + %tsecs1 = $(%tsecs / 10) + %tsecs2 = $(%tsecs % 10) + %mins = $($mp3player.gettime / 60000) + %secs = $(($mp3player.gettime % 60000) / 1000) + %secs1 = $(%secs / 10) + %secs2 = $(%secs % 10) + %rate = $($mp3player.getrate / 1000) + %size = $file.size("$mp3player.getfilename") + %msize = $(%size / (1024*1024)) + %ksize = $((%size - ((%size / (1024*1024)) * 1024 * 1024) - (%size % 1024)) / 1024) + + %bsize = $(%size % 1024) + + if(%msize > 0) + { + %size = "%msize MByte %ksize KByte" + }else{ + if(%ksize > 0) + { + %size = "%ksize KByte %bsize Byte" + }else{ + %size = "%bsize Byte" + } + } + if ($mp3player.isplaying){ + + if($mp3player.getmp3tag_artist != ""){ + %outartist= $mp3player.getmp3tag_artist + } + else { + %outartist="" + } + if ($mp3player.getmp3tag_title != ""){ + %outtitle= $mp3player.getmp3tag_title + } + else { + %outtitle="" + } + if ((%outartitst != "") && (%outtitle != "")) { + %outartisttitle="["%outartitst" - "%autartisttitle"]" + } + else { + if (%outtitle == ""){ + %outartisttitle="["%outartist"]" + } + else { + %outartisttitle="["%outtitle"]" + } + + } + + if (mp3player.getmp3tag_album !=""){ + %outalbum =" - [Album: "$mp3player.getmp3tag_album"]" + } + else { + %outalbum ="" + } + + if (mp3player.getmp3tag_genre !=""){ + %outgenre =" - [Genre: "$mp3player.getmp3tag_genre"]" + } + else { + %outgenre ="" + } + + + if ($mp3player.getmp3tag_comment !=""){ + %outcomment =" - [Comment: "$mp3player.getmp3tag_comment"]" + } + else { + %outcomment="" + } + if ($mp3player.getmp3tag_version != ""){ + %outmp3tagversion = "MPEG"$mp3player.getmp3tag_version + } + else { + %outmp3tagversion="" + } + if ($mp3player.getmp3tag_layer != ""){ + %outmp3taglayer = "Layer"$mp3player.getmp3tag_layer + } + else { + %outmp3taglayer = "" + } + if (%outmp3taglayer != "" && %outmp3tagversion != ""){ + %outmp3tagversionlayer = " - ["%outmp3tagversion" - "%outmp3taglayer"]" + } + else { + %outmp3tagversionlayer= "" + if (%outmp3tagversion != ""){ + %outmp3tagversionlayer= "["%mp3tagversion"]" + } + if (%outmp3taglayer != ""){ + %outmp3tagversionlayer= "["%mp3taglayer"]" + } + + } + + + %rate = $($mp3player.getrate/1000) + if (%rate != 0){ + %outrate=" - ["%rate KBps"]" + } + else { + %outrate="" + } + if (%Frequ2 != 0){ + %outfrequ2=" - ["%Frequ2 KHz"]" + } + else { + %outfrequ2="" + } + if (%Channels != 0){ + %outchannels = " - ["%Channels"]" + } + else { + %outchannels="" + } + if (%size != 0){ + %outsize = " - ["%size"]" + } + else { + %outsize="" + } + + me muckelt grad %outartisttitle %outalbum %outgenre %outcomment %outmp3tagversionlayer %outrate %outfrequ2 - [%mins:%secs1%secs2 min of %tmins:%tsecs1%tsecs2 min] - [Player: $mp3player.getplayer ] + } + } + if (!$mp3player.isplaying){ + me muckelt grad * nix * + + }
\ No newline at end of file diff --git a/doc/scriptexamples/mp3info.kvs b/doc/scriptexamples/mp3info.kvs new file mode 100644 index 00000000..b5324145 --- /dev/null +++ b/doc/scriptexamples/mp3info.kvs @@ -0,0 +1,109 @@ +%Xmms_play=$mp3player.getfilename; + +%Prefix = $str.left(%Xmms_play,4) +%Postfix = $str.right(%Xmms_play,4) + + %Http_prefix = "http" + %Cda_postfix = ".cda" + if(%Prefix == %Http_prefix) { + + + %Stime = $($mp3player.gettime/1000) + %Mtime = 0 + %Ctime =" "; + + while(%Stime > 59) { + %Mtime++ + %Stime-=60 + } + + %Htime=0 + + while(%Mtime > 59) { + %Htime++ + %Mtime-=60 + } + + if (%Stime < 10){ + %tmp = %Stime + %Stime = $str.append("0",%tmp) + } + if (%Mtime < 10 && %Htime > 1){ + %tmp = %Mtime + %Mtime = $str.append("0",%tmp) + } + + + + if (%Htime > 0) + %Ctime = $str.append %Htime:%Mtime:%Stime + + + + if (%Mtime > 0 && %Htime < 1) + %Ctime = $str.append %Mtime:%Stime + + + + if (%Mtime < 1 && %Htime < 1) + %Ctime = %Stime + + + + me has currently [$mp3player.gettitle @ %Xmms_play - $($mp3player.getrate/1000) kbps - $($mp3player.getfreq/1000) kHz - seit: %Ctime (h:m:s)] + } + + + else if(%Postfix == %Cda_postfix) { + %Xmms_play=$mp3player.getfilename; + + } + + + else { + + if ($mp3player.getchannels == 2) { + %Channels = Stereo + } + else { + %Channels = Mono + } + + %Frequ = $mp3player.getfreq + %Frequ2 = $(%Frequ/1000) + + %tmins = $($mp3player.getlength / 60000) + %tsecs = $(($mp3player.getlength % 60000) / 1000) + %tsecs1 = $(%tsecs / 10) + %tsecs2 = $(%tsecs % 10) + %mins = $($mp3player.gettime / 60000) + %secs = $(($mp3player.gettime % 60000) / 1000) + %secs1 = $(%secs / 10) + %secs2 = $(%secs % 10) + %rate = $($mp3player.getrate / 1000) + %size = $file.size("$mp3player.getfilename") + %msize = $(%size / (1024*1024)) + %ksize = $((%size - ((%size / (1024*1024)) * 1024 * 1024) - (%size % 1024)) / 1024) + + %bsize = $(%size % 1024) + + + if(%msize > 0){ + %size = "%msize MByte %ksize KByte" + } + else { + if(%ksize > 0){ + %size = "%ksize KByte %bsize Byte" + } + else { + %size = "%bsize Byte" + } + } + me has currently [$mp3player.getmp3tag_artist - $mp3player.getmp3tag_title] - [Album: $mp3player.getmp3tag_album] - [Genre: $mp3player.getmp3tag_genre] - [Comment: $mp3player.getmp3tag_comment] - [MPEG$mp3player.getmp3tag_version Layer$mp3player.getmp3tag_layer] - [$($mp3player.getrate/1000) Bit] - [%Frequ2 kHz] - [%Channels] - [%size] - [%mins:%secs1%secs2 min von %tmins:%tsecs1%tsecs2 min] + } + + if (!$mp3player.isplaying) + { + me's player has currently stopped + +} diff --git a/doc/scriptexamples/mp3share/Makefile.am b/doc/scriptexamples/mp3share/Makefile.am new file mode 100644 index 00000000..a48a9984 --- /dev/null +++ b/doc/scriptexamples/mp3share/Makefile.am @@ -0,0 +1,5 @@ +############################################################################### +# KVirc IRC client Makefile - 16.12.98 Szymon Stefanek <stefanek@tin.it> +############################################################################### + +EXTRA_DIST = mp3share.kvs mp3share_icon.png diff --git a/doc/scriptexamples/mp3share/mp3share.kvs b/doc/scriptexamples/mp3share/mp3share.kvs new file mode 100644 index 00000000..57c30032 --- /dev/null +++ b/doc/scriptexamples/mp3share/mp3share.kvs @@ -0,0 +1,84 @@ +###################################################################################################### +# +# MP3Share KVIrc script +# +# This small script communicates with the platform mp3 player +# and advertises the currently played song, allowing its download by dcc +# +# +# The following things are left as exercise to the readed: +# +# - "max downloads" checking is done +# - starting the platform mp3player if it is not running +# +###################################################################################################### + +file.mkdir $file.localdir("pics") +file.copy -o $file.extractPath($0)"/mp3share_icon.png" $file.localdir("pics/mp3share_icon.png") + + +action.create -w=cqd (mp3share_advertise,$tr("Advertise MP3"),$tr("Advertises the currently played song and shares it"),mp3share_icon.png) +{ + if(!$mp3player.isplaying) + { + echo It looks like your mp3 player is not playing + halt + } + + if($mp3player.getlength <= 0) + { + # Playing a stream + + me plays$k(4) $mp3player.gettitle() $k(2)(streaming from $mp3player.getfilename()) + + } else { + %mins = $($mp3player.getlength / 60000) + %secs = $(($mp3player.getlength % 60000) / 1000) + %secs1 = $(%secs / 10) + %secs2 = $(%secs % 10) + %rate = $($mp3player.getrate / 1000) + + %text = $k(4) $mp3player.gettitle() $k(2)[%mins:%secs1%secs2 %rate Kbps $mp3player.getfreq Hz \ + $file.size("$mp3player.getfilename") Bytes]$o + + if(%type != "dccchat") + { + srand $unixtime + %a = $rand(9) + %b = $rand(9) + %c = $rand(9) + + me plays%text (Use $b/CTCP $me XDCC GET mp3_%a%b%c$b to download) + + sharedfile.add -t=$(($mp3player.getlength / 1000) + 60) \ + -n="mp3_%a%b%c" $mp3player.getfilename; + } else me plays%text + } +} + +toolbar.create mp3share $tr("MP3 Sharing") +toolbar.additem mp3share mp3share_advertise +toolbar.show mp3share + +alias(mp3share_uninstall) +{ + action.destroy mp3share_advertise + toolbar.destroy mp3share + alias(mp3share_uninstall){} +} + + +echo "The Mp3share script is installed." +if("$system.ostype" == "windows") +{ + echo "On Windows you need the Winamp multimedia player" + echo "freely available at http://www.winamp.com" + echo "You also need to install the gen_kvirc.dll from the KVIrc" + echo "distribution as a Winamp plugin (this involves copying the" + echo "file to the winamp \"plugins\" directory)." +} else { + echo "On Unix you need the Xmms multimedia player" + echo "freely available at http://www.xmms.org" +} +echo "Use mp3share_uninstall to uninstall the script." +echo "Have fun :)" diff --git a/doc/scriptexamples/mp3share/mp3share_icon.png b/doc/scriptexamples/mp3share/mp3share_icon.png Binary files differnew file mode 100644 index 00000000..6aeec0e1 --- /dev/null +++ b/doc/scriptexamples/mp3share/mp3share_icon.png diff --git a/doc/scriptexamples/newkvstest.kvs b/doc/scriptexamples/newkvstest.kvs new file mode 100644 index 00000000..0fe06a25 --- /dev/null +++ b/doc/scriptexamples/newkvstest.kvs @@ -0,0 +1,101 @@ +# This file contains test code for the new KVS parser +# It does nothing useful + + +echo "Foreach test 1" + +%arry[0] = "test0" +%arry[2] = "test2" + +%i = 0 + +foreach(%x,%arry) +{ + echo "Array item %i: (%x)" + %i++ +} + +echo "Foreach test 2" + +%ahash{"keyA"} = "dataA" +%ahash{"keyB"} = "dataB" + +%i = 0 + +foreach(%x,%ahash) +{ + echo "Hash item %i: (%x)" + %i++ +} + +echo "Foreach test 3" + +%i = 0 + +foreach(%x,$keys(%ahash)) +{ + echo "Hash entry %i: key->(%x), item->(%ahash{%x})" + %i++ +} + +echo "Foreach test 4" + +%i = 0 + +foreach(%x,%arry,$keys(%ahash)) +{ + echo "Item %i: %x" + %i++ +} + + +%tmp = 1 +switch(%tmp) +{ + case(1): + echo \%tmp was 1! + break; + case(2) + echo \%tmp was 2! + break; + default: + echo \%tmp was not 1 nor 2: it was %tmp! + break; +} + +%tmp = 1 +switch(%tmp) +{ + case(1): + echo \%tmp was 1! + case(2) + echo \%tmp was 2! + break; + default: + echo \%tmp was either 1 or something different from 2 (%tmp) + break; +} + +%tmp = "This is a test" +%tmp2 = "This is not a test" +switch(%tmp) +{ + case(%tmp2) + echo \%tmp == \%tmp2 + break; + case(%tmp) + { + # do not break here + echo "Yeah.. it's stupid.. \%tmp == \%tmp :D" + } + match("*TEST"): + echo "Matched *TEST" + regexp("[a-zA-Z ]*test"): + echo "Matched [a-zA-Z ]*text" + regexp("[a-zA-Z ]*not[a-zA-Z ]*"): + echo "Matched [a-zA-Z ]*not[a-zA-Z ]*" + default: + echo This is executed anyway (unless some break was called) + break; +} + diff --git a/doc/scriptexamples/playing/Makefile.am b/doc/scriptexamples/playing/Makefile.am new file mode 100644 index 00000000..d673dfb9 --- /dev/null +++ b/doc/scriptexamples/playing/Makefile.am @@ -0,0 +1,5 @@ +############################################################################### +# KVirc IRC client Makefile - 16.12.98 Szymon Stefanek <stefanek@tin.it> +############################################################################### + +EXTRA_DIST = playing.kvs playing_icon.png diff --git a/doc/scriptexamples/playing/playing.kvs b/doc/scriptexamples/playing/playing.kvs new file mode 100644 index 00000000..984dd8c2 --- /dev/null +++ b/doc/scriptexamples/playing/playing.kvs @@ -0,0 +1,108 @@ +###################################################################################################### +# +# playing KVIrc script +# +# I started this script cause I was scared about the colors of Pragmas mp3share script +# and the insecierness of dcc download. +# +# So I did set up this little script for mp3 advertising based on Pragmas ugly mp3share. +# +# Hints, donations and stuff can be delivered to istari@kvirc.net. +# +# This small script communicates with the platform mp3 player +# and advertises the currently played song +# +# The following things are left as exercise to the readed: +# +# - starting the platform mp3player if it is not running +# +###################################################################################################### + +file.mkdir $file.localdir(pics) +file.copy -o $file.extractPath($0)/playing_icon.png $file.localdir(pics/playing_icon.png) + +action.create -w=cqd (playing_adv,$tr("Advertise MP3"),$tr("Advertises the currently played song"),playing_icon.png) +{ + if(!($mp3player.isplaying)) + { + echo It looks like your mp3 player is not playing + halt + } + + switch ($mp3player.getchannels()) + { + case("1"): %channel="mono" + break + case("2"): %channel="stereo" + break + } + + %tmins = $($mp3player.getlength / 60000) + %tsecs = $(($mp3player.getlength % 60000) / 1000) + %tsecs1 = $(%tsecs / 10) + %tsecs2 = $(%tsecs % 10) + %mins = $($mp3player.gettime / 60000) + %secs = $(($mp3player.gettime % 60000) / 1000) + %secs1 = $(%secs / 10) + %secs2 = $(%secs % 10) + %rate = $($mp3player.getrate / 1000) + %size = $file.size("$mp3player.getfilename") + %msize = $(%size / (1024*1024)) + %ksize = $((%size - ((%size / (1024*1024)) * 1024 * 1024) - (%size % 1024)) / 1024) + %bsize = $(%size % 1024) + + + if(%size > 0) + { + if(%msize > 0) + { + %size = "%msize MByte %ksize KByte" + }else{ + if(%ksize > 0) + { + %size = "%ksize KByte %bsize Byte" + }else{ + %size = "%bsize Byte" + } + } + + me is listening to $mp3player.gettitle() =- %mins:%secs1%secs2 of %tmins:%tsecs1%tsecs2 -=- %rate Kbps -=- \ + $mp3player.getfreq Hz -=- %channel -=- %size -=$o + }else{ + me is streaming \"$mp3player.gettitle()\" from $mp3player.getfilename =- %mins:%secs1%secs2 mins -=- %rate Kbps -=- \ + $mp3player.getfreq Hz -=- %channel -=$o + } + +# offer.add -t=$(($mp3player.getlength / 1000) + 60) \ +# -n="mp3_$mp3player.getposition" $mp3player.getfilename; +} + +toolbar.create playing $tr("MP3 Sharing") +toolbar.additem playing playing_advertise +toolbar.show playing + +alias(playing_uninstall) +{ + # Kill the playing_adv alias + action.destroy -q playing_adv + # Kill this alias + alias(playing_uninstall){} +} + + +playing_addbutton + +echo The playing script is installed. +if("$system.ostype" == "windows") +{ + echo On Windows you need the Winamp multimedia player + echo freely available at http://www.winamp.com + echo You also need to install the gen_kvirc.dll from the KVIrc + echo distribution as a Winamp plugin (this involves copying the + echo file to the winamp \"plugins\" directory). +} else { + echo On Unix you need the Xmms multimedia player + echo freely available at http://www.xmms.org +} +echo Use playing_uninstall to uninstall the script. +echo Have fun :) diff --git a/doc/scriptexamples/playing/playing_icon.png b/doc/scriptexamples/playing/playing_icon.png Binary files differnew file mode 100644 index 00000000..2635e618 --- /dev/null +++ b/doc/scriptexamples/playing/playing_icon.png diff --git a/doc/scriptexamples/popup1.kvs b/doc/scriptexamples/popup1.kvs new file mode 100644 index 00000000..107c5a6e --- /dev/null +++ b/doc/scriptexamples/popup1.kvs @@ -0,0 +1,17 @@ + +defpopup (test) +{ + item(Say "$0 ($1)" , 0)echo $0 ($1) + separator + popup(Popup) + { + item(Say "$0" twice) + { + echo $0 (1) + echo $0 (2) + } + } +} + + +echo "Use '/popup test <parameter>' to test the popup" diff --git a/doc/scriptexamples/popup2.kvs b/doc/scriptexamples/popup2.kvs new file mode 100644 index 00000000..10aa07a1 --- /dev/null +++ b/doc/scriptexamples/popup2.kvs @@ -0,0 +1,124 @@ +# This code adds a channel popup that accepts: +# a comma separated list of nicknames + +defpopup (channel) +{ + prologue + { + # the local variables are visible in the entire defpopup scope + %visible=$0 + %multiple = 0 + if($str.findfirst(%visible,",") != -1) + { + %visible = "multiple users" + %multiple = 1 + } + } + + popup(Control,9)($isMeOp) + { + label(Operator power) + item(Op %visible,34)op $0 + item(Deop %visible,62)deop $0 + separator; + item(Voice %visible,35)voice $0 + item(Devoice %visible,63)devoice $0 + separator; + item(Kick %visible (You're not welcome here!),110)kick $0 You're not welcome here! + item(Ban %visible (Nick!*@* mask),67)ban $0 + } + + popup(Info,49) + { + item(Who %visible,75)who $0 + item(Whois %visible,75)whois $0 + item(Whois %visible (with idle time),75) whois $0 $0 + item(Whowas %visible,92)whowas $0 + } + + popup(Ctcp,51) + { + item(PING,36)ping $0 + item(VERSION,16)ctcp $0 VERSION + item(USERINFO,57)ctcp $0 USERINFO + item(CLIENTINFO,52)ctcp $0 CLIENTINFO + item(SOURCE,14)ctcp $0 SOURCE + item(FINGER,53)ctcp $0 FINGER + item(TIME,93)ctcp $0 TIME + } + + popup(Dcc,77) + { + item(Chat with %visible,78) dcc.chat $0 + item(Send to %visible,79) dcc.send $0 + item(TDCC Send to %visible,77) dcc.send -t $0 + } + + popup("Registration",111)(!%multiple) + { + prologue + { + # This is really smart...we're loading + # the reguser module only if this popup is being requested. + %regName = $reguser.match($mask(%visible)) + %labelText = "<center><b>%visible</b> is"; + if("%regName" != "") + { + %labelText << "registered as<br><b>%regName</b>" + # We want to display masks here!!! + %inNotifyList = $reguser.property(%regName,notify); + if("%inNotifyList" != "")%labelText << "<br>$0 is in the notify list" + %curAvatar = $avatar($0); + %defAvatar = $reguser.property(%regName,avatar); + if("%defAvatar" != "")%labelText << "<br>$0 has a default avatar" + %labelText << "</center>" + } else { + %labelText << "not registered</center>" + } + } + label("%labelText") + popup("Register")("%regName" == "") + { + item("Register as $0 ($mask(%visible,11))")("$reguser.exactMatch($mask(%visible,11))" == "")reguser.add %visible $mask(%visible,11) + item("Register as $0 ($mask(%visible,12))")("$reguser.exactMatch($mask(%visible,12))" == "")reguser.add %visible $mask(%visible,12) + item("Register as $0 ($mask(%visible,13))")("$reguser.exactMatch($mask(%visible,13))" == "")reguser.add %visible $mask(%visible,13) + item("Register as $0 ($mask(%visible,3))")("$reguser.exactMatch($mask(%visible,3))" == "")reguser.add %visible $mask(%visible,3) + item("Register as $0 ($mask(%visible,4))")("$reguser.exactMatch($mask(%visible,4))" == "")reguser.add %visible $mask(%visible,4) + item("Register as $0 ($mask(%visible,5))")("$reguser.exactMatch($mask(%visible,5))" == "")reguser.add %visible $mask(%visible,5) + } + item("Edit registration entry",113)("%regName" != "")reguser.edit "%regName" + item("Unregister %regName")("%regName" != "")reguser.remove "%regName" + + item("Add to notify list")(("%regName" != "") && ("%inNotifyList" == "")) + reguser.setproperty -n "%regName" notify %visible + + item("Remove from notify list") + (("%regName" != "") && ("%inNotifyList" != "")) + reguser.setproperty -n "%regName" notify + + item("Set the current avatar as default")(("%curAvatar" != "") && ("%curAvatar" != "%defAvatar")) + reguser.setproperty "%regName" avatar %curAvatar + + item("Unset the default avatar")("%defAvatar" != "") + reguser.setproperty %regName avatar + +# item("Choose the default avatar")("%regMask" != "") +# dialog.fileopen("Choose the avatar filename",%regMask){ reguser.setproperty -e $magic $selectedFile; } + + } + + separator; + + #item(Notify avatar,57)("$0" != "$myNick")avatar $0 + #item(Notify avatar (No offer),57)("$0" != "$myNick")avatar -n $0 + item(Notify avatar,57)avatar $0 + item(Notify avatar (No offer),57)avatar -n $0 + + separator; + item(Query %visible,47)(!%multiple)query $0 + item(Query %visible (Single queries),47)(%multiple)query $0 + item(Query %visible (Multiple query),47)(%multiple)query -m $0 +} + + +event (OnChannelUserListRightClicked,default){ if("$selected" != "")popup channel $selected; }; diff --git a/doc/scriptexamples/popup3.kvs b/doc/scriptexamples/popup3.kvs new file mode 100644 index 00000000..0ad7d4a6 --- /dev/null +++ b/doc/scriptexamples/popup3.kvs @@ -0,0 +1,46 @@ +defpopup (channeltextview) +{ + item(Leave $0,40){ part $0; } +} + + +event (OnChannelTextViewRightClicked,default){ if("$target" != "")popup channeltextview $target; } + + +defpopup (querytextview) +{ + item(Whois $0,57){ whois $0; } +} + + +event (OnQueryTextViewRightClicked,default){ if("$target" != "")popup querytextview $target; } + + + +defpopup (consoletextview) +{ + popup(Server queries,13) + { + item(Version,16)version + item(Motd,30)motd + item(Info,29)info + item(Admin,29)admin + item(Lusers,57)lusers + item(Time,93)time + item(Help,49)raw help + popup(Stats,22) + { + item(d (debug/dns ?))stats d + item(l (connections))stats l + item(m (command usage))stats m + item(o (operators))stats o + item(t (connection stats ?),23)stats t + item(u (uptime),93)stats u + item(y (y-lines))stats y + item(z (debug stats ?),11)stats z + } + } +} + + +event (OnConsoleTextViewRightClicked,default){ popup consoletextview; } diff --git a/doc/scriptexamples/popup4.kvs b/doc/scriptexamples/popup4.kvs new file mode 100644 index 00000000..1680bbeb --- /dev/null +++ b/doc/scriptexamples/popup4.kvs @@ -0,0 +1,40 @@ +defpopup (windowpopup) +{ + label("$caption") + + popup(Logging,119) + { + prologue + { + %logfile = $log.file + } + label("Logging to file<br><b>%logfile</b>")("%logfile" != "") + item("Stop logging")("%logfile" != "")log.stop + item("Flush log file")("%logfile" != "")log.flush + + label("Logging is disabled")("%logfile" == "") + item("Log (default file)")("%logfile" == "")log.start + item("Log (default file) (prepend existing data)")("%logfile" == "")log.start -e + } +# popup(Buffer) +# { +# item("Save to file...")window.savebuffer +# item("Clear")window.clear +# item("Find...")window.findtext +# } +# popup(View) +# { +# item("Show images")($window.images == 0)window.images 1 +# item("Enable timestamps")($window.timestamp == 0)window.timestamp 1 +# item("Disable timestamps")($window.timestam == 1)window.timestamp 0 +# item("Set font...")$window.setfont +# item("Set background...")$window.setbackground +# } +# popup(Mdi) +# { +# item("Minimize")window.minimize +# item("Maximize")window.maximize +# } +} + +event (OnWindowPopupRequest,default){ popup windowpopup; } diff --git a/doc/scriptexamples/relay.kvs b/doc/scriptexamples/relay.kvs new file mode 100644 index 00000000..d8689866 --- /dev/null +++ b/doc/scriptexamples/relay.kvs @@ -0,0 +1,33 @@ +# Relay bot (unfinished for now :D) +# TODO : finish me! + + +alias (relay_start) +{ + # Usage : relay_start <window1> <window2> +} + + +alias (relay_stop) +{ + # Usage : relay_stop <window1> <window2> +} + +event (OnChannelPrivmsg) +{ + %target = %RelayTarget[$window]; + if("%target" != "") + { + msg -w=%t $target <$0> $1- + } +} + +alias (relay_uninstall) +{ + timer -s (relay_uninstall_timer,0) + { + alias(relay_start){} + alias(relay_stop){} + alias(relay_uninstall){} + } +} diff --git a/doc/scriptexamples/runmenu/Makefile.am b/doc/scriptexamples/runmenu/Makefile.am new file mode 100644 index 00000000..d91889ea --- /dev/null +++ b/doc/scriptexamples/runmenu/Makefile.am @@ -0,0 +1,5 @@ +############################################################################### +# KVirc IRC client Makefile - 16.12.98 Szymon Stefanek <stefanek@tin.it> +############################################################################### + +EXTRA_DIST = runmenu.kvs diff --git a/doc/scriptexamples/runmenu/runmenu.kvs b/doc/scriptexamples/runmenu/runmenu.kvs new file mode 100644 index 00000000..dea6a27d --- /dev/null +++ b/doc/scriptexamples/runmenu/runmenu.kvs @@ -0,0 +1,111 @@ +# This simple script adds a sample "run external program" popup +# to your menubar +# This is just a sample popup... you will probably +# want to add/remove entries here +# This will also work mainly on unix... windows has not so much +# proggies to be run as example + + +# Define the popup with run entries... +# You prolly will change a lot here :) + +defpopup(runmenu) +{ + popup(Terminal,138) + { + item(XTerm,25) ($system.ostype == unix) + { + run xterm + } + + item(Konsole,151) ($system.ostype == unix) + { + run konsole + } + + item(ETerm,25) ($system.ostype == unix) + { + run eterm + } + } + + popup(Browser,172) + { + item(konqueror,135) ($system.ostype == unix) + { + run konqueror + } + + item(netscape,164) + { + if($system.ostype == unix)run kvi_run_netscape + else run netscape.exe + } + } + + popup(Multimedia,177) + { + item(xmms) + { + run xmms + } + item(mplayer) + { + run mplayer + } + item(kscd) + { + run kscd + } + } + + popup(Utils) + { + item(xcalc) + { + run xcalc; + } + item(kcalc) + { + run kcalc; + } + } + + separator; + + # Let's allow to run an user specified command + + item(Run...,183) + { + dialog.textinput(Run,<center>Please enter the command name</center>,Ok,Cancel) + { + if($0 == 0 && $1)run $1 + } + } + + separator; + + # This is an interesting item + # It allows this script to be uninstalled :) + + popup(Uninstall,110) + { + item(Uninstall this menu,110) + { + timer -s (runmenu_uninstall,0){ defpopup(runmenu){}; } + } + } +} + +# add it to the menubar of each new frame + +event(OnFrameWindowCreated,runmenu) +{ + setmenu -i=3 "&Run" runmenu +} + +# set it also just now, in the current frame + +setmenu -i=3 "&Run" runmenu + +# done :) diff --git a/doc/scriptexamples/signal1.kvs b/doc/scriptexamples/signal1.kvs new file mode 100644 index 00000000..7777e679 --- /dev/null +++ b/doc/scriptexamples/signal1.kvs @@ -0,0 +1,47 @@ +# This implements a "fake timer" object that emits the "timeout" signal +# and a target object slot connected to this signal. + +# We also test the immediate object deletion...it seems to work + +class(faketimer,object) +{ + timerEvent(<timer id>) + { + $this->$emit(timeout,$0) + } +} + + +class(target,object) +{ + constructor + { + $$->%num = 10; + } + + timerSlot(<timer id>) + { + echo "Timer $0 fired $$->%num" + $$->%num--; + if($$->%num == 0) + { + echo "Deleting sender $$->$signalSender()->$name() : Kaboom!" + # Immediate sender deletion.... + delete -i $$->$signalSender + $$->$startTimer(1000) + } + } + + timerEvent(<timer id>) + { + echo "Deleting self! : Kaboom!" + delete $this; + } +} + + +%source = $new(faketimer,0,sender) +%target = $new(target,0,target) +connect %source timeout %target timerSlot +echo "Starting timer...." +%source->$startTimer(1000) diff --git a/doc/scriptexamples/simplehttp/Makefile.am b/doc/scriptexamples/simplehttp/Makefile.am new file mode 100644 index 00000000..540ab61c --- /dev/null +++ b/doc/scriptexamples/simplehttp/Makefile.am @@ -0,0 +1,5 @@ +############################################################################### +# KVirc IRC client Makefile - 16.12.98 Szymon Stefanek <stefanek@tin.it> +############################################################################### + +EXTRA_DIST = simplehttp.kvs diff --git a/doc/scriptexamples/simplehttp/simplehttp.kvs b/doc/scriptexamples/simplehttp/simplehttp.kvs new file mode 100644 index 00000000..8c49014a --- /dev/null +++ b/doc/scriptexamples/simplehttp/simplehttp.kvs @@ -0,0 +1,105 @@ +# +# This is a really simple and senseless HTTP server +# It listens on port 8080 and serves a "Hello world" page +# to the incoming browser connections. +# + + +# Define the "startup" alias +alias(simplehttp_start) +{ + # If the server is already running , well... give up + if($classDefined(simpleHttpListeningSocket) && (%SimpleHttpListeningSocket)) + { + echo "The Simple HTTP server is already running" + halt + } + + # Define the server class (if the class is already defined it will be overridden) + class(simpleHttpListeningSocket,socket) + { + # The constructor fails if we can't listen on port 8080! + constructor() + { + if(!$$->$listen(8080)) + { + return 0 + } + return 1 + } + + # Handle the incoming connections + incomingConnectionEvent() + { + # Create a slave socket + %tmp = $new(socket) + # Accept the connection + $$->$accept(%tmp) + # And handle it + echo "Incoming connection:" + echo "Remote end: %tmp->$remoteIp : %tmp->$remotePort" + echo "Local end: %tmp->$localIp : %tmp->$localPort" + # Write a "Hello world" page to the client + %tmp->$write("HTTP/1.0 200 OK\r\n"); + %tmp->$write("Content-type: text/html\r\n\r\n"); + %tmp->$write("<html>\n") + %tmp->$write(" <head><title>KVIrc simple http server</title></head>\n") + %tmp->$write(" <body bgcolor=\"#000000\" text=\"#FF0000\">\n") + %tmp->$write(" <center><h1><b>Hello World!</b></h1></center>\n") + %tmp->$write(" </body>\n") + %tmp->$write("</html>\n\r\n\r\n") + # Close (this ensures data delivery) + %tmp->$close() + # Kill the slave + delete %tmp; + # Some browsers may tell you that the connection is "broken" + # (konqueror , for example) since they can't send + # the GET request... (we don't read it!) + # Well...this is left as exercise to the scripter: + # Write the data to the socket only in response to the GET message :) + # You CAN do it. + } + } + + # Create the server socket + %SimpleHttpListeningSocket = $new(simpleHttpListeningSocket) + + if(!%SimpleHttpListeningSocket)echo "Ops.. can't start the simple http server :(((" + else { + echo "Ok.. up and running :)" + echo "Point your browser to http://127.0.0.1:8080" + } + +} + +# Stop alias : this is easy + +alias(simplehttp_stop) +{ + if(!%SimpleHttpListeningSocket) + { + echo "The simple HTTP server is not running" + halt + } + %SimpleHttpListeningSocket->$close() + delete %SimpleHttpListeningSocket + %SimpleHttpListeningSocket = "" +} + +# Uninstall alias: another easy one + +alias(simplehttp_uninstall) +{ + # Stop the service eventually + if(%SimpleHttpListeningSocket)simplehttp_stop + # Kill the class (again eventually) + killclass -q simpleHttpListeningSocket + # And kill the aliases (including "myself") + alias(simplehttp_start){} + alias(simplehttp_stop){} + alias(simplehttp_uninstall){} +} + +# and let's start it the first time + +simplehttp_start diff --git a/doc/scriptexamples/socket1.kvs b/doc/scriptexamples/socket1.kvs new file mode 100644 index 00000000..382b48c9 --- /dev/null +++ b/doc/scriptexamples/socket1.kvs @@ -0,0 +1,45 @@ +# This is a simple socket class to play with from the console +# It just echoes the events to the console window + +class(testsocket,socket) +{ + connectEvent() + { + echo "[$$] Connected to $$->$remoteIp() $$->$remotePort()" + } + + disconnectEvent() + { + echo "[$$] Disconnected ($0-)" + } + + connectFailedEvent() + { + echo "[$$] Connect failed ($0-)" + } + + dataAvailableEvent() + { + echo "[$$] Data: $$->$read()" + } +} + +echo "testsocket class installed" +echo "You can play with it by using:" + +echo "/\%X = \$new(testsocket); # To create it" +echo "/\%X->\$connect(<host>,<port>); # To connect to a host" +echo "/\%X->\$write(<data>); # To write ASCII data" +echo "/\%X->\$close(); # To terminate a connection" +echo "/delete \%X; # To destroy it" +echo "...." +echo "Tip: why don't you try to IRC ?" +echo "The sequence might be something like:" +echo "/\%X = \$new(testsocket)" +echo "/\%X->\$connect(<your_irc_server>,6667)" +echo "/\%X->\$write(\"USER <youruser> <somestring> <somestring> :<realname>\$cr\$lf\")" +echo "/\%X->\$write(\"NICK <yournick>\$cr\$lf\")" +echo "/\%X->\$write(\"JOIN #kvirc\$cr\$lf\")" +echo "/\%X->\$write(\"PRIVMSG #kvirc :Hello IRC-World! :D\$cr\$lf\")" +echo "...." +echo "Have fun! :)" diff --git a/doc/scriptexamples/srfs.kvs b/doc/scriptexamples/srfs.kvs new file mode 100644 index 00000000..1d4bd2e7 --- /dev/null +++ b/doc/scriptexamples/srfs.kvs @@ -0,0 +1,42 @@ +# Connect to firewalled sysreset fserves without /dccserver +# SlickMcSly@mail.com +# Everything here was derived through simple experimentation while learning how everything involved +# works. If there's a better way I'm in no rush figure it out. +# +# Firewalled sysreset fserves try to open a dcc chat connection to the target port and send +# the message "100 fservenick". After the fserve sends the "100 fservenick" message it expects a +# "101 recipientnick" message, with "recipientnick" being the nick of whoever got the message. +# I wasn't able to send it some1 else's nick and get a connection that way, which would be +# interesting since you could theoretically serve connections to other ppl behind firewalls. +# +# This code requires opening a listening socket, but since sysreset is mircx specific (essentially +# winhozed specific) it doesn't hestitate to asking people who connect to open a listening port in +# the well known/privileged range which requires root (NONO). To deal with this, I setup iptables +# to forward packets bound for port 59 to port 12345 (which can be bound by non-root) by using: +# "iptables -t nat -I PREROUTING -p tcp -m multiport --dports 59 -j DNAT --to-destination :12345" +# (this requires root) +# +# There also appears to also be an option for some firewalled dcc send protocol. I've only +# encountered this once though and didn't care to figure it out. + +# Execute all the following code in the "New code tester" window +alias(srfs) # Create alias: srfs + { + if($0 && $1) # make sure there are enough parameters + { + dcc.chat -n -u -p=12345 $0; # listen on port 12345 for incoming chat + ctcp $0 $1-; # send the ctcp trigger + } + else + echo "Usage: /srfs <nick> <trigger>"; # when there aren't enough parameters + } + +event(OnDCCChatMessage,srfsHook) # Create OnDCCChatMessage event: srfs + { + # Since this event will parse ALL dcc chat messages, it looks for a message in the format + # "100 word" with no other trailing text. This is only a hack so it doesn't verify that + # "word" is a nickname, much less the nick of an expected fserve. + if(!$str.cmp($str.word(0, $0-), "100") && $str.cmp($str.word(1, $0-), "") && !$str.cmp($str.word(2, $0-), "")) + say "101 $me"; # reply 101 mynick # reply "101 mynick" + } +# End diff --git a/doc/scriptexamples/switchstress.kvs b/doc/scriptexamples/switchstress.kvs new file mode 100644 index 00000000..883383d2 --- /dev/null +++ b/doc/scriptexamples/switchstress.kvs @@ -0,0 +1,46 @@ +echo "Beginning the test! ==========================" + +switch(test) +{ + case(test) + { + echo "case(test)" + } + match(test) + { + echo "match(test)" + # comment + } + match(T*) + { + echo "match(T*)" + } + # Comment + match(T*): echo "match(T*) 2" + default: echo "default 1" + case(texto): + { + echo "case(texto)" + break + break + } + break; + default: + { + echo "default 2" + } + default: + { + echo "default 3" + } + break + regexp([a-z]*) + { + echo "match([a-z]*)" + } + default: + { + echo "default 4" + } + +} diff --git a/doc/scriptexamples/timer1.kvs b/doc/scriptexamples/timer1.kvs new file mode 100644 index 00000000..b123c6b7 --- /dev/null +++ b/doc/scriptexamples/timer1.kvs @@ -0,0 +1,25 @@ +# This is a simple example of using the builtin object timer +# capabilities. + +# we create a new object +%bomb = $new(object,0,myobject) + +# implement its timerEvent +privateimpl(%bomb,timerEvent) +{ + if($$->%secsToGo > 0) + { + echo "Countdown: $$->%secsToGo" + $$->%secsToGo--; + } else { + echo "Boom!" + delete $this + } +} + +# and trigger it +echo "Countdown: 10 seconds to go" +%bomb->%secsToGo = 9 +%bomb->$startTimer(1000) + +# now just have to wait... diff --git a/doc/scriptexamples/tutorial/Makefile.am b/doc/scriptexamples/tutorial/Makefile.am new file mode 100644 index 00000000..914e3823 --- /dev/null +++ b/doc/scriptexamples/tutorial/Makefile.am @@ -0,0 +1,5 @@ +############################################################################### +# KVirc IRC client Makefile - 16.12.98 Szymon Stefanek <stefanek@tin.it> +############################################################################### + +EXTRA_DIST = *.kvs diff --git a/doc/scriptexamples/tutorial/minesweeper1.kvs b/doc/scriptexamples/tutorial/minesweeper1.kvs new file mode 100644 index 00000000..f4f5f5ee --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper1.kvs @@ -0,0 +1,78 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 1 + + + +# First of all we create the main game widget +# The minesweeper widget inherits from the widget class +class(minesweepermain,widget) +{ + # The constructor sets the basic widget properties + # and creates the child widgets + constructor() + { + # Set the widget caption + $$->$setCaption("KVIrc's Minesweeper (0.1.0)"); + + # We will have a variable number of rows , columns and mines + # For now we hardcorde it , later they might become user definable parameters + $$->%rows = 10 + $$->%cols = 10 + $$->%mines = 10 + + # The child labels will be put in a layout that will manage automatically their geometries + $$->%layout = $new(layout,$this) + + # Time to create the child labels + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + # We use a dictionary to simulate a two dimensional array + # The label references are stored in the dictionary associated to a key + # that is build from the row and column index + $$->%label{%i,%j}=$new(label,$this,"%i_%j") + # Each label must remember its position + $$->%label{%i,%j}->%row = %i + $$->%label{%i,%j}->%col = %j + # We add the labels to the layout grid + $$->%layout->$addWidget($$->%label{%i,%j},%i,%j) + } + } + + # Time to initialize a new game + $$->$newGame(); + } + + # We need no destructor for now : the child widgets and the layout will be + # destroyed when the user will close the main widget + + + # Here we start a new game + newGame() + { + # We set the labels + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + # KVIrc is parsed on-the-fly so we use the following line as optimisation. + # parsing %l is really faster than parsing a $$->%label{%i,%j} + %l = $$->%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); # raised labels are "unpressed" buttons + %l->%bIsMine = 0 # for now it is NOT a mine + %l->%numMines = 0 # number of adiacent mines , for now 0 + %l->%bIsDiscovered = 0 # this label has been pressed ? + %l->$setText("") # set the text to an empty string + } + } + } +} + +# Create an instance of the minesweepermain object + +%m = $new(minesweepermain) +%m->$show() + +# /parse this file + diff --git a/doc/scriptexamples/tutorial/minesweeper2.kvs b/doc/scriptexamples/tutorial/minesweeper2.kvs new file mode 100644 index 00000000..16eb3172 --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper2.kvs @@ -0,0 +1,93 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 2 + +class(minesweepermain,widget) +{ + constructor() + { + $$->$setCaption("KVIrc's Minesweeper (0.1.0)"); + + $$->%rows = 10 + $$->%cols = 10 + $$->%mines = 10 + + $$->%layout = $new(layout,$this) + + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + $$->%label{%i,%j}=$new(label,$this,"%i_%j") + $$->%label{%i,%j}->%row = %i + $$->%label{%i,%j}->%col = %j + $$->%layout->$addWidget($$->%label{%i,%j},%i,%j) + } + } + + $$->$newGame() + } + + newGame() + { + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + %l = $$->%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->$setText("") + } + } + # Here we drop the mines around: it is a bit complex problem: + # We want to have a fixed number of mines placed randomly in our grid + # + # So .. for each mine that we have to place... + for(%i = 0;%i < $$->%mines;%i++) + { + # Choose a random position for this mine + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + # Ensure that we're not placing this mine over an existing one + while($$->%label{%row,%col}->%bIsMine != 0) + { + # If there was already a mine, then choose the position again + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + } + # Ok.. this is a mine then + $$->%label{%row,%col}->%bIsMine = 1 + + # increase the mine count for the adiacent cells: this is again a bit complex thingie + if(%row > 0) + { + # There is a row over our mine: the cells above must have their mine count updated + # The cell just above us + $$->%label{$(%row - 1),%col}->%numMines++ + # The cell above on the left (if exists) + if(%col > 0)$$->%label{$(%row - 1),$(%col - 1)}->%numMines++ + # The cell above on the right (if exists) + if(%col < ($$->%cols - 1))$$->%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < ($$->%rows - 1)) + { + # There is a row below our mine: the cells below must have their mine count updated + # The cell just below us + $$->%label{$(%row + 1),%col}->%numMines++ + # The cell below on the left (if exists) + if(%col > 0)$$->%label{$(%row + 1),$(%col - 1)}->%numMines++ + # The cell below on the right (if exists) + if(%col < ($$->%cols - 1))$$->%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + # Now the cell on the left side (if exists) + if(%col > 0)$$->%label{%row,$(%col - 1)}->%numMines++ + # And on the right side (if exists) + if(%col < ($$->%cols - 1))$$->%label{%row,$(%col + 1)}->%numMines++ + } + } +} + +%m = $new(minesweepermain) +%m->$show() diff --git a/doc/scriptexamples/tutorial/minesweeper3.kvs b/doc/scriptexamples/tutorial/minesweeper3.kvs new file mode 100644 index 00000000..7522c390 --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper3.kvs @@ -0,0 +1,147 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 3 + +# We want the labels to report us the clicks thus we will use +# a class derived from label +class(minelabel,label) +{ + # We ovverride the implementation for mousePressEvent() + # and we signal the mouse press to the parent minesweeper widget + mousePressEvent() + { + # We could check that $$->$parent() is a minesweeper in fact... + # but well... let's keep it simple :) + $$->$parent()->$mineLabelPressed($this) + } +} + +class(minesweepermain,widget) +{ + constructor() + { + $$->$setCaption("KVIrc's Minesweeper (0.1.0)"); + + $$->%rows = 10 + $$->%cols = 10 + $$->%mines = 10 + + $$->%layout = $new(layout,$this) + + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + # we change label to minelabel then + #$$->%label{%i,%j}=$new(label,$this,"%i_%j") + $$->%label{%i,%j}=$new(minelabel,$this,"%i_%j") + $$->%label{%i,%j}->%row = %i + $$->%label{%i,%j}->%col = %j + $$->%layout->$addWidget($$->%label{%i,%j},%i,%j) + } + } + + $$->$newGame() + } + + newGame() + { + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + %l = $$->%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->$setText("") + } + } + # drop the mines + for(%i = 0;%i < $$->%mines;%i++) + { + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + while($$->%label{%row,%col}->%bIsMine != 0) + { + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + } + $$->%label{%row,%col}->%bIsMine = 1 + # increase the mine count for the adiacent cells + if(%row > 0) + { + $$->%label{$(%row - 1),%col}->%numMines++ + if(%col > 0)$$->%label{$(%row - 1),$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < ($$->%rows - 1)) + { + $$->%label{$(%row + 1),%col}->%numMines++ + if(%col > 0)$$->%label{$(%row + 1),$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + if(%col > 0)$$->%label{%row,$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{%row,$(%col + 1)}->%numMines++ + } + } + + # A mine label was pressed! + # This function is called by our children minelabel objects + # and the parameter passed is the minelabel reference + mineLabelPressed($0 = mine label object that has been pressed) + { + # Is is a mine ? + if($0->%bIsMine) + { + # Yes , a mine :( + # Game over + $0->$setFrameStyle(WinPanel,Sunken) + $0->$setText("*") + } else { + # Not a mine.. discover the adiacent cells + # We pass the minelabel reference + $$->$discoverCells($0) + } + } + + # This function is recursive: it discovers the adjacent cells + # that are not mines + discoverCells($0 = mine label that has to be discovered) + { + # If it is a mine ,then do not discover it + if($0->%bIsMine)return; + # If it is already discovered , return too + if($0->%bIsDiscovered)return; + # Ok.. this is discovered + $0->%bIsDiscovered = 1 + $0->$setFrameStyle(WinPanel,Sunken) + # If this cell has adjacent mines then show their number and return + if($0->%numMines > 0)$0->$setText($0->%numMines) + else { + # There are no adjacent mines : discover the cells recursively + # This block of code is similar to the one used in dropping the bombs around + if($0->%row > 0) + { + # Discover the cells in the row above + $$->$discoverCells($$->%label{$($0->%row - 1),$0->%col}) + if($0->%col > 0)$$->$discoverCells($$->%label{$($0->%row - 1),$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$($0->%row - 1),$($0->%col + 1)}) + } + if($0->%row < ($$->%rows - 1)) + { + # Discover the cells in the row below + $$->$discoverCells($$->%label{$($0->%row + 1),$0->%col}) + if($0->%col > 0)$$->$discoverCells($$->%label{$($0->%row + 1),$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$($0->%row + 1),$($0->%col + 1)}) + } + # Discover on the left and right + if($0->%col > 0)$$->$discoverCells($$->%label{$0->%row,$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$0->%row,$($0->%col + 1)}) + } + } + +} + +%m = $new(minesweepermain) +%m->$show() diff --git a/doc/scriptexamples/tutorial/minesweeper4.kvs b/doc/scriptexamples/tutorial/minesweeper4.kvs new file mode 100644 index 00000000..0013736b --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper4.kvs @@ -0,0 +1,187 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 4 + +class(minelabel,label) +{ + mousePressEvent() + { + $$->$parent()->$mineLabelPressed($this) + } +} + +class(minesweepermain,widget) +{ + constructor() + { + # This is no longer needed here : we move it to the container widget + + $$->%rows = 10 + $$->%cols = 10 + $$->%mines = 10 + # is there a running game ? + $$->%bGameRunning = 0 + + + $$->%layout = $new(layout,$this) + + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + $$->%label{%i,%j}=$new(minelabel,$this,"%i_%j") + $$->%label{%i,%j}->%row = %i + $$->%label{%i,%j}->%col = %j + # Let's set the minimum size of the labels: they look better this way + $$->%label{%i,%j}->$setMinimumWidth(26) + $$->%label{%i,%j}->$setMinimumHeight(26) + $$->%label{%i,%j}->$setAlignment(HCenter , VCenter) + $$->%layout->$addWidget($$->%label{%i,%j},%i,%j) + } + } + # We remove this call, $$->$newGame() will be called from the container widget + #$$->$newGame() + } + + newGame() + { + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + %l = $$->%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->$setText("") + # Set enabled ! + %l->$setEnabled(1) + } + } + # drop the mines + for(%i = 0;%i < $$->%mines;%i++) + { + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + while($$->%label{%row,%col}->%bIsMine != 0) + { + %row = $rand($($$->%rows - 1)) + %col = $rand($($$->%cols - 1)) + } + $$->%label{%row,%col}->%bIsMine = 1 + # increase the mine count for the adiacent cells + if(%row > 0) + { + $$->%label{$(%row - 1),%col}->%numMines++ + if(%col > 0)$$->%label{$(%row - 1),$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < ($$->%rows - 1)) + { + $$->%label{$(%row + 1),%col}->%numMines++ + if(%col > 0)$$->%label{$(%row + 1),$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + if(%col > 0)$$->%label{%row,$(%col - 1)}->%numMines++ + if(%col < ($$->%cols - 1))$$->%label{%row,$(%col + 1)}->%numMines++ + } + # A game is running + $$->%bGameRunning = 1 + } + + mineLabelPressed($0 = mine label object that has been pressed) + { + # When the game isn't running we don't care about the mouse press events + if(!$$->%bGameRunning)return + if($0->%bIsMine) + { + $0->$setFrameStyle(WinPanel,Sunken) + $0->$setImage(82); # 82 is a bomb + # We emit the gameOver signal so the container widget knows + $$->$emit(gameOver) + # And disable all the cells + for(%i = 0;%i < $$->%rows;%i++) + { + for(%j = 0;%j < $$->%cols;%j++) + { + $$->%label{%i,%j}->$setEnabled(0) + } + } + # leave the bomb image enabled :D + $0->$setEnabled(1) + # Game finished :( + $$->%bGameRunning = 0 + } else { + $$->$discoverCells($0) + } + } + + discoverCells($0 = mine label that has to be discovered) + { + if($0->%bIsMine)return; + if($0->%bIsDiscovered)return; + $0->%bIsDiscovered = 1 + $0->$setFrameStyle(WinPanel,Sunken) + if($0->%numMines > 0)$0->$setText($0->%numMines) + else { + if($0->%row > 0) + { + $$->$discoverCells($$->%label{$($0->%row - 1),$0->%col}) + if($0->%col > 0)$$->$discoverCells($$->%label{$($0->%row - 1),$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$($0->%row - 1),$($0->%col + 1)}) + } + if($0->%row < ($$->%rows - 1)) + { + $$->$discoverCells($$->%label{$($0->%row + 1),$0->%col}) + if($0->%col > 0)$$->$discoverCells($$->%label{$($0->%row + 1),$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$($0->%row + 1),$($0->%col + 1)}) + } + if($0->%col > 0)$$->$discoverCells($$->%label{$0->%row,$($0->%col - 1)}) + if($0->%col < ($$->%cols - 1))$$->$discoverCells($$->%label{$0->%row,$($0->%col + 1)}) + } + } + +} + +# We want a bigger window around our minesweeper widget +# we want to provide some output and user menus... +# Thus we create a container widget class , that will be also our new toplevel widget + +class(minesweeper,widget) +{ + constructor() + { + $$->$setCaption("KVIrc's Minesweeper (0.1.0)"); + + # we need a layout to manage the children + $$->%layout = $new(layout,$$) + + + $$->%gamepool = $new(minesweepermain,$$) + $$->%layout->$addWidget($$->%gamepool,1,0) + connect $$->%gamepool gameOver $$ gameOver + + $$->%output = $new(label,$$) + $$->%output->$setFrameStyle(WinPanel,Sunken) + $$->%layout->$addWidget($$->%output,2,0) + + $$->$newGame() + } + + gameOver() + { + $$->%output->$setText("Game Over :(") + } + + newGame() + { + $$->%gamepool->$newGame() + $$->%output->$setText("Ready") + } +} + + +#We substitute the old creation call with the new container object creation call +#%m = $new(minesweepermain) +%m = $new(minesweeper) +%m->$show() diff --git a/doc/scriptexamples/tutorial/minesweeper5.kvs b/doc/scriptexamples/tutorial/minesweeper5.kvs new file mode 100644 index 00000000..1bda6c83 --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper5.kvs @@ -0,0 +1,201 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 5 + + +# First of all we convert all the $$-> to @ : this is faster both to write and for KVIrc to process + +class(minelabel,label) +{ + mousePressEvent() + { + # We also pass the mouse button pressed + @$parent()->$mineLabelPressed($this,$0) + } +} + +class(minesweepermain,widget) +{ + constructor() + { + @%rows = 20 + @%cols = 20 + @%mines = 60 + @%bGameRunning = 0 + + @%layout = $new(layout,$this) + + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + @%label{%i,%j}=$new(minelabel,$this,"%i_%j") + @%label{%i,%j}->%row = %i + @%label{%i,%j}->%col = %j + @%label{%i,%j}->$setMinimumWidth(26) + @%label{%i,%j}->$setMinimumHeight(26) + @%label{%i,%j}->$setAlignment(HCenter , VCenter) + @%layout->$addWidget(@%label{%i,%j},%i,%j) + } + } + } + + newGame() + { + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + %l = @%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->%iState = 0; # In state 0 it doesn't show anything + %l->$setText("") + %l->$setImage(); # show no image + %l->$setEnabled(1) + } + } + + for(%i = 0;%i < @%mines;%i++) + { + %row = $rand($(@%rows - 1)) + %col = $rand($(@%cols - 1)) + while(@%label{%row,%col}->%bIsMine != 0) + { + %row = $rand($(@%rows - 1)) + %col = $rand($(@%cols - 1)) + } + @%label{%row,%col}->%bIsMine = 1 + if(%row > 0) + { + @%label{$(%row - 1),%col}->%numMines++ + if(%col > 0)@%label{$(%row - 1),$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < (@%rows - 1)) + { + @%label{$(%row + 1),%col}->%numMines++ + if(%col > 0)@%label{$(%row + 1),$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + if(%col > 0)@%label{%row,$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{%row,$(%col + 1)}->%numMines++ + } + @%bGameRunning = 1 + } + + mineLabelPressed($0 = mine label object that has been pressed,$1 = the button that has been pressed) + { + if(!@%bGameRunning)return + + # We add the right button handling : the user can mark the cells discovered + if($1 == 1) + { + # Right button was pressed + if($0->%bIsDiscovered)return; # does nothing + # we loop the state thru 0->1->2->0 + switch($0->%iState) + { + case(0): + { + $0->$setImage(58) + $0->%iState = 1 + } + case(1): + { + $0->$setImage(28) + $0->%iState = 2 + } + case(2): + { + $0->$setImage() + $0->%iState = 0 + } + } + return; + } + + if($0->%bIsMine) + { + $0->$setFrameStyle(WinPanel,Sunken) + $0->$setImage(82); # 82 is a bomb + @$emit(gameOver) + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + @%label{%i,%j}->$setEnabled(0) + } + } + $0->$setEnabled(1) + @%bGameRunning = 0 + } else { + @$discoverCells($0) + } + } + + discoverCells($0 = mine label that has to be discovered) + { + if($0->%bIsMine)return; + if($0->%bIsDiscovered)return; + $0->%bIsDiscovered = 1 + $0->$setFrameStyle(WinPanel,Sunken) + if($0->%iState != 0)$0->$setImage(); # ensure that no image is shown + if($0->%numMines > 0)$0->$setText($0->%numMines) + else { + if($0->%row > 0) + { + @$discoverCells(@%label{$($0->%row - 1),$0->%col}) + if($0->%col > 0)@$discoverCells(@%label{$($0->%row - 1),$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$($0->%row - 1),$($0->%col + 1)}) + } + if($0->%row < (@%rows - 1)) + { + @$discoverCells(@%label{$($0->%row + 1),$0->%col}) + if($0->%col > 0)@$discoverCells(@%label{$($0->%row + 1),$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$($0->%row + 1),$($0->%col + 1)}) + } + if($0->%col > 0)@$discoverCells(@%label{$0->%row,$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$0->%row,$($0->%col + 1)}) + } + } + +} + + +class(minesweeper,widget) +{ + constructor() + { + @$setCaption("KVIrc's Minesweeper (0.1.0)"); + + @%layout = $new(layout,$$) + + + @%gamepool = $new(minesweepermain,$$) + @%layout->$addWidget(@%gamepool,1,0) + connect @%gamepool gameOver $$ gameOver + + @%output = $new(label,$$) + @%output->$setFrameStyle(WinPanel,Sunken) + @%layout->$addWidget(@%output,2,0) + + @$newGame() + } + + gameOver() + { + @%output->$setText("Game Over :(") + } + + newGame() + { + @%gamepool->$newGame() + @%output->$setText("Ready") + } +} + + +%m = $new(minesweeper) +%m->$show() diff --git a/doc/scriptexamples/tutorial/minesweeper6.kvs b/doc/scriptexamples/tutorial/minesweeper6.kvs new file mode 100644 index 00000000..f03ffb05 --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper6.kvs @@ -0,0 +1,254 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 6 + +class(minelabel,label) +{ + mousePressEvent() + { + # We also pass the mouse button pressed + @$parent()->$mineLabelPressed($this,$0) + } +} + +class(minesweepermain,widget) +{ + # We accept constructor parameters now + constructor($0 = rows,$1 = cols,$2 = num mines) + { + # We should check the parameters passed here, and maybe return 0 + # if the params are not positive numbers + echo $0,$1,$2 + @%rows = $0 + @%cols = $1 + @%mines = $2 + @%bGameRunning = 0 + + @%layout = $new(layout,$this) + + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + @%label{%i,%j}=$new(minelabel,$this,"%i_%j") + @%label{%i,%j}->%row = %i + @%label{%i,%j}->%col = %j + @%label{%i,%j}->$setMinimumWidth(26) + @%label{%i,%j}->$setMinimumHeight(26) + @%label{%i,%j}->$setAlignment(HCenter , VCenter) + @%layout->$addWidget(@%label{%i,%j},%i,%j) + } + } + } + + newGame() + { + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + %l = @%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->%iState = 0; # In state 0 it doesn't show anything + %l->$setText("") + %l->$setImage(); # show no image + %l->$setEnabled(1) + } + } + + for(%i = 0;%i < @%mines;%i++) + { + %row = $rand($(@%rows - 1)) + %col = $rand($(@%cols - 1)) + while(@%label{%row,%col}->%bIsMine != 0) + { + %row = $rand($(@%rows - 1)) + %col = $rand($(@%cols - 1)) + } + @%label{%row,%col}->%bIsMine = 1 + if(%row > 0) + { + @%label{$(%row - 1),%col}->%numMines++ + if(%col > 0)@%label{$(%row - 1),$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < (@%rows - 1)) + { + @%label{$(%row + 1),%col}->%numMines++ + if(%col > 0)@%label{$(%row + 1),$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + if(%col > 0)@%label{%row,$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{%row,$(%col + 1)}->%numMines++ + } + @%bGameRunning = 1 + # We keep track of the number of the discovered cells + @%iTotalNonMineCells = $((@%rows * @%cols) - @%mines) + @%iUndiscoveredNonMineCells = @%iTotalNonMineCells + } + + mineLabelPressed($0 = mine label object that has been pressed,$1 = the button that has been pressed) + { + if(!@%bGameRunning)return + + # We add the right button handling : the user can mark the cells discovered + if($1 == 1) + { + # Right button was pressed + if($0->%bIsDiscovered)return; # does nothing + # we loop the state thru 0->1->2->0 + switch($0->%iState) + { + case(0): + { + $0->$setImage(58) + $0->%iState = 1 + } + case(1): + { + $0->$setImage(28) + $0->%iState = 2 + } + case(2): + { + $0->$setImage() + $0->%iState = 0 + } + } + return; + } + + if($0->%bIsMine) + { + @$gameFailure($0) + } else { + @$discoverCells($0) + if(@%iUndiscoveredNonMineCells == 0) + { + # Finished! + @$gameSuccess() + } else { + @$emit(userMoved) + } + } + } + + discoverAllCells() + { + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + %l = @%label{%i,%j} + if(!%l->%bIsDiscovered) + { + %l->$setFrameStyle(WinPanel,Sunken) + if(%l->%bIsMine)%l->$setImage(82) + else { + %l->$setImage("") + if(%l->%numMines > 0)%l->$setText(%l->%numMines) + else %l->$setText("") + } + } + %l->$setEnabled(0) + } + } + } + + gameFailure($0 = the game cell that had the bomb) + { + @$discoverAllCells() + $0->$setFrameStyle(WinPanel,Sunken) + $0->$setImage(82); # 82 is a bomb + $0->$setEnabled(1) + @%bGameRunning = 0 + @$emit(gameOver) + } + + gameSuccess() + { + @$discoverAllCells() + @%bGameRunning = 0 + @$emit(success) + } + + discoverCells($0 = mine label that has to be discovered) + { + if($0->%bIsMine)return; + if($0->%bIsDiscovered)return; + $0->%bIsDiscovered = 1 + $0->$setFrameStyle(WinPanel,Sunken) + @%iUndiscoveredNonMineCells-- + if($0->%iState != 0)$0->$setImage(); # ensure that no image is shown + if($0->%numMines > 0)$0->$setText($0->%numMines) + else { + if($0->%row > 0) + { + @$discoverCells(@%label{$($0->%row - 1),$0->%col}) + if($0->%col > 0)@$discoverCells(@%label{$($0->%row - 1),$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$($0->%row - 1),$($0->%col + 1)}) + } + if($0->%row < (@%rows - 1)) + { + @$discoverCells(@%label{$($0->%row + 1),$0->%col}) + if($0->%col > 0)@$discoverCells(@%label{$($0->%row + 1),$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$($0->%row + 1),$($0->%col + 1)}) + } + if($0->%col > 0)@$discoverCells(@%label{$0->%row,$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$0->%row,$($0->%col + 1)}) + } + } + +} + + +class(minesweeper,widget) +{ + constructor() + { + @$setCaption("KVIrc's Minesweeper (0.1.0)"); + + @%layout = $new(layout,$$) + + # Create a 10x10 game pool with 10 mines + @%gamepool = $new(minesweepermain,$$,myGamepool,10,10,10) + @%layout->$addWidget(@%gamepool,1,0) + connect @%gamepool gameOver $$ gameOver + # We connect the two new signals + connect @%gamepool userMoved $$ showState + connect @%gamepool success $$ success + @%output = $new(label,$$) + @%output->$setFrameStyle(WinPanel,Sunken) + @%layout->$addWidget(@%output,2,0) + + @$newGame() + } + + showState() + { + @%output->$setText("@%gamepool->%iUndiscoveredNonMineCells to go") + } + + success() + { + @%output->$setText("You did it!") + } + + gameOver() + { + @%output->$setText("Game Over :(") + } + + newGame() + { + @%gamepool->$newGame() + @%output->$setText("Ready") + @$showState() + } +} + + +%m = $new(minesweeper) +%m->$show() diff --git a/doc/scriptexamples/tutorial/minesweeper7.kvs b/doc/scriptexamples/tutorial/minesweeper7.kvs new file mode 100644 index 00000000..701c09d3 --- /dev/null +++ b/doc/scriptexamples/tutorial/minesweeper7.kvs @@ -0,0 +1,253 @@ +# The scripts in this tutorial implement the famous minesweeper game +# STEP 7 + +class(minelabel,label) +{ + mousePressEvent() + { + @$parent()->$mineLabelPressed($this,$0) + } +} + +class(minesweepermain,widget) +{ + constructor($0 = rows,$1 = cols,$2 = num mines) + { + # We should check the parameters passed here, and maybe return 0 + # if the params are not positive numbers + echo $0,$1,$2 + @%rows = $0 + @%cols = $1 + @%mines = $2 + @%bGameRunning = 0 + + @%layout = $new(layout,$this) + + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + @%label{%i,%j}=$new(minelabel,$this,"%i_%j") + @%label{%i,%j}->%row = %i + @%label{%i,%j}->%col = %j + @%label{%i,%j}->$setMinimumWidth(26) + @%label{%i,%j}->$setMinimumHeight(26) + @%label{%i,%j}->$setAlignment(HCenter , VCenter) + @%layout->$addWidget(@%label{%i,%j},%i,%j) + } + } + } + + newGame() + { + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + %l = @%label{%i,%j} + %l->$setFrameStyle(Raised,WinPanel); + %l->%bIsMine = 0 + %l->%numMines = 0 + %l->%bIsDiscovered = 0 + %l->%iState = 0; # In state 0 it doesn't show anything + %l->$setText("") + %l->$setImage(); # show no image + %l->$setEnabled(1) + } + } + + for(%i = 0;%i < @%mines;%i++) + { + %row = $rand($(@%rows - 1)) + %col = $rand($(@%cols - 1)) + while(@%label{%row,%col}->%bIsMine != 0) + { + %row = $rand($(@%rows - 1)) + %col = $rand($(@%cols - 1)) + } + @%label{%row,%col}->%bIsMine = 1 + if(%row > 0) + { + @%label{$(%row - 1),%col}->%numMines++ + if(%col > 0)@%label{$(%row - 1),$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{$(%row - 1),$(%col + 1)}->%numMines++ + } + if(%row < (@%rows - 1)) + { + @%label{$(%row + 1),%col}->%numMines++ + if(%col > 0)@%label{$(%row + 1),$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{$(%row + 1),$(%col + 1)}->%numMines++ + } + if(%col > 0)@%label{%row,$(%col - 1)}->%numMines++ + if(%col < (@%cols - 1))@%label{%row,$(%col + 1)}->%numMines++ + } + @%bGameRunning = 1 + # We keep track of the number of the discovered cells + @%iTotalNonMineCells = $((@%rows * @%cols) - @%mines) + @%iUndiscoveredNonMineCells = @%iTotalNonMineCells + } + + mineLabelPressed($0 = mine label object that has been pressed,$1 = the button that has been pressed) + { + if(!@%bGameRunning)return + + if($1 == 1) + { + # Right button + if($0->%bIsDiscovered)return; # does nothing + switch($0->%iState) + { + case(0): + { + $0->$setImage(58) + $0->%iState = 1 + } + case(1): + { + $0->$setImage(28) + $0->%iState = 2 + } + case(2): + { + $0->$setImage() + $0->%iState = 0 + } + } + return; + } + + if($0->%bIsMine) + { + @$gameFailure($0) + } else { + @$discoverCells($0) + if(@%iUndiscoveredNonMineCells == 0) + { + # Finished! + @$gameSuccess() + } else { + @$emit(userMoved) + } + } + } + + discoverAllCells() + { + for(%i = 0;%i < @%rows;%i++) + { + for(%j = 0;%j < @%cols;%j++) + { + %l = @%label{%i,%j} + if(!%l->%bIsDiscovered) + { + %l->$setFrameStyle(WinPanel,Sunken) + if(%l->%bIsMine)%l->$setImage(82) + else { + %l->$setImage("") + if(%l->%numMines > 0)%l->$setText(%l->%numMines) + else %l->$setText("") + } + } + %l->$setEnabled(0) + } + } + } + + gameFailure($0 = the game cell that had the bomb) + { + @$discoverAllCells() + $0->$setFrameStyle(WinPanel,Sunken) + $0->$setImage(82); # 82 is a bomb + $0->$setEnabled(1) + @%bGameRunning = 0 + @$emit(gameOver) + } + + gameSuccess() + { + @$discoverAllCells() + @%bGameRunning = 0 + @$emit(success) + } + + discoverCells($0 = mine label that has to be discovered) + { + if($0->%bIsMine)return; + if($0->%bIsDiscovered)return; + $0->%bIsDiscovered = 1 + $0->$setFrameStyle(WinPanel,Sunken) + @%iUndiscoveredNonMineCells-- + if($0->%iState != 0)$0->$setImage(); # ensure that no image is shown + if($0->%numMines > 0)$0->$setText($0->%numMines) + else { + if($0->%row > 0) + { + @$discoverCells(@%label{$($0->%row - 1),$0->%col}) + if($0->%col > 0)@$discoverCells(@%label{$($0->%row - 1),$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$($0->%row - 1),$($0->%col + 1)}) + } + if($0->%row < (@%rows - 1)) + { + @$discoverCells(@%label{$($0->%row + 1),$0->%col}) + if($0->%col > 0)@$discoverCells(@%label{$($0->%row + 1),$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$($0->%row + 1),$($0->%col + 1)}) + } + if($0->%col > 0)@$discoverCells(@%label{$0->%row,$($0->%col - 1)}) + if($0->%col < (@%cols - 1))@$discoverCells(@%label{$0->%row,$($0->%col + 1)}) + } + } + +} + + +class(minesweeper,window) +{ + constructor() + { + @$setCaption("KVIrc's Minesweeper (0.1.0)"); + + @%layout = $new(layout,$$) + + @%menubar = $new(menubar,$$) + @%menubar->$insertItem(test) + @%layout->$addWidget(@%menubar,0,0) + + @%gamepool = $new(minesweepermain,$$,myGamepool,10,10,10) + @%layout->$addWidget(@%gamepool,1,0) + connect @%gamepool gameOver $$ gameOver + # We connect the two new signals + connect @%gamepool userMoved $$ showState + connect @%gamepool success $$ success + @%output = $new(label,$$) + @%output->$setFrameStyle(WinPanel,Sunken) + @%layout->$addWidget(@%output,2,0) + + @$newGame() + } + + showState() + { + @%output->$setText("@%gamepool->%iUndiscoveredNonMineCells to go") + } + + success() + { + @%output->$setText("You did it!") + } + + gameOver() + { + @%output->$setText("Game Over :(") + } + + newGame() + { + @%gamepool->$newGame() + @%output->$setText("Ready") + @$showState() + } +} + + +%m = $new(minesweeper) +%m->$show() diff --git a/doc/scriptexamples/widget1.kvs b/doc/scriptexamples/widget1.kvs new file mode 100644 index 00000000..21511b34 --- /dev/null +++ b/doc/scriptexamples/widget1.kvs @@ -0,0 +1,20 @@ +# a stupid game :) + +srand $unixTime + +%w = $new(widget,0,mywidget) +%w->$setCaption(":)"); +%w->$setGeometry($rand(770),$rand(570),30,30); + +privateimpl(%w,mouseEnterEvent) +{ + $$->$move($rand(770),$rand(570)); +} + +privateimpl(%w,mousePressEvent) +{ + echo "You got me!" + delete $$ +} + +%w->$show() diff --git a/doc/scriptexamples/widget2.kvs b/doc/scriptexamples/widget2.kvs new file mode 100644 index 00000000..8c9b5b7a --- /dev/null +++ b/doc/scriptexamples/widget2.kvs @@ -0,0 +1,23 @@ +# a stupid game :) (version 2) + +class(mystupidgame,widget) +{ + constructor + { + srand $unixTime; + $$->$setCaption(":)"); + $$->$setGeometry($rand(770),$rand(570),30,30); + } + mouseEnterEvent + { + $$->$move($rand(770),$rand(570)); + } + mousePressEvent + { + echo "You got me!" + delete $$ + } +} + +%w = $new(mystupidgame,0) +%w->$show() |