home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / spiele / argon / argon.exe / src / graphics / argoned next >
Encoding:
Text File  |  2000-01-26  |  8.4 KB  |  311 lines

  1. #!/bin/sh
  2. # the next line restarts using wish \
  3. exec wish8.0 "$0" "$@"
  4.  
  5. #
  6. #  argoned - Argon V level editor, (c) 2000 by Till Harbaum, GPL'd
  7. #
  8. #  V1.0 jan 2000  quick and dirty
  9. #
  10. #  T.Harbaum@tu-bs.de - http://www.ibr.cs.tu-bs.de/~harbaum/pilot
  11. #
  12. #  This program is distributed in the hope that it will be useful,
  13. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #  GNU General Public License for more details.
  16. #
  17.  
  18. set llength 50;   # level length in screens
  19.  
  20. proc disp_level { } {
  21.     global llength buffer fileshort
  22.     set of $fileshort
  23.  
  24.     for {set y 0} {$y<$llength} {incr y} {
  25.     for {set x 0} {$x<10} {incr x} {
  26.         set a [.main.canvas.c itemcget id($x,$y) -image];
  27.  
  28.         if { $buffer($y,$x) != 0 } {
  29.         if { $a != "tile[expr $buffer($y,$x) -1]" } {
  30.             .main.canvas.c itemconfigure id($x,$y)\
  31.                            -image tile[expr $buffer($y,$x) -1] 
  32.         }
  33.         } else {
  34.         if { $a != "none" } {
  35.           .main.canvas.c itemconfigure id($x,$y) -image none
  36.         }
  37.         }
  38.     }
  39.  
  40.     # draw progess line in filename area
  41.     if { [expr $y%10] == 0 } {
  42.         set fileshort [format "processed %d%%" [expr 100*$y/$llength]]
  43.         update idletasks
  44.     }
  45.     }
  46.  
  47.     set fileshort $of
  48. }
  49.  
  50. proc clear_buffer { } {
  51.     global llength buffer filename fileshort
  52.  
  53.     for {set y 0} {$y<$llength} {incr y} {
  54.     for {set x 0} {$x<10} {incr x} {
  55.         set buffer($y,$x) 0;
  56.     }
  57.     }
  58.     set filename "new level.lev";
  59.     set fileshort "new level.lev";
  60.  
  61.     disp_level
  62. }
  63.  
  64. proc fileio {operation} {
  65.     global filename fileshort llength buffer
  66.     set ok false
  67.  
  68.     set types { {"Level files" {.lev}} {"All files" *}}
  69.  
  70.     if { (($operation != 1) && ($operation != 3)) || ([string compare $filename "new level.lev"]) == 0 } {
  71.     
  72.     if {$operation == 0} {
  73.         set file [tk_getOpenFile -filetypes $types -parent .]
  74.     } else {
  75.         set file [tk_getSaveFile -filetypes $types -parent . \
  76.         -initialfile $filename -defaultextension .lev]
  77.     }
  78.  
  79.     if [string compare $file ""] {
  80.         set ok true
  81.         set filename $file
  82.         set fileshort [file tail $file]
  83.     }
  84.     } else { 
  85.     set ok true
  86.     }
  87.  
  88.     if { $ok } {
  89.     # really load/save file 
  90.     if { ($operation == 0)||($operation == 3) } {
  91.         # read
  92.         if { [file size $filename] != [expr $llength*10] } {
  93.         tk_messageBox -message "File size does not match!" -icon error -type ok
  94.         clear_buffer
  95.         } else {
  96.         set dat [open $filename r]
  97.         fconfigure $dat -translation binary
  98.  
  99.             for {set y 0} {$y<$llength} {incr y} {
  100.           set a [read $dat 10]
  101.           binary scan $a cccccccccc \
  102.             buffer($y,0) buffer($y,1) buffer($y,2) buffer($y,3) buffer($y,4) \
  103.             buffer($y,5) buffer($y,6) buffer($y,7) buffer($y,8) buffer($y,9)
  104.                 }
  105.         close $dat
  106.  
  107.         # update view
  108.         disp_level
  109.         }
  110.     } else {
  111.         # write
  112.         set dat [open $filename w]
  113.         fconfigure $dat -translation binary
  114.  
  115.         for {set y 0} {$y<$llength} {incr y} {
  116.           puts -nonewline $dat [binary format cccccccccc \
  117.             $buffer($y,0) $buffer($y,1) $buffer($y,2) $buffer($y,3) $buffer($y,4) \
  118.             $buffer($y,5) $buffer($y,6) $buffer($y,7) $buffer($y,8) $buffer($y,9) ]
  119.         }
  120.         close $dat
  121.     }
  122.     }
  123. }
  124.  
  125. proc select { n } {
  126.     global last_button windows
  127.  
  128.     # windows obviously doesn't use the 'highlightbackground', but bg is visible
  129.     # even with full size icons, so using bg instead
  130.     if { $windows } {
  131.       .main.buttons.code$last_button configure -bg [.main.buttons.code$n cget -bg]
  132.       .main.buttons.code$n configure -bg red
  133.     } else {
  134.       .main.buttons.code$last_button configure -highlightbackground [.main.buttons.code$n cget -highlightbackground]
  135.       .main.buttons.code$n configure -highlightbackground red
  136.     }
  137.  
  138.     set last_button $n;
  139. }
  140.  
  141. proc click { n } {
  142.   global last_button buffer
  143.  
  144.   set id [.main.canvas.c find withtag current]
  145.   if {[lsearch [.main.canvas.c gettags current] tile] >= 0} {
  146.  
  147.     set x [expr int([lindex [.main.canvas.c coords $id] 0]/16)] 
  148.     set y [expr int([lindex [.main.canvas.c coords $id] 1]/16)] 
  149.  
  150.     if { $n == 0 } {
  151.       set buffer($y,$x) [expr $last_button + 1]
  152.       .main.canvas.c itemconfigure id($x,$y) -image tile[expr $buffer($y,$x) -1] 
  153.     } else {
  154.       set buffer($y,$x) 0
  155.       .main.canvas.c itemconfigure id($x,$y) -image none
  156.     }
  157.   }
  158. }
  159.  
  160. #
  161. # Create all windows, and pack them
  162. #
  163. proc createwindows { } {
  164.     global columns
  165.     global llength last_button windows
  166.  
  167.     # determine number of button rows (8 buttons per row)
  168.     set n 0;
  169.     while { [file exist [format button%04x.gif $n]] } {
  170.       incr n;
  171.     }
  172.     set columns [expr ($n+7)/8]
  173.  
  174.     frame .main -borderwidth 0
  175.     
  176.     frame .main.canvas -borderwidth 0
  177.     canvas .main.canvas.c -borderwidth 2 -relief sunken -width 160 -height 160 \
  178.         -scrollregion "0 0 160 [expr $llength*16]" -yscrollcommand ".scroll set" 
  179.     scrollbar .scroll -command ".main.canvas.c yview"
  180.     .main.canvas.c yview moveto 1.0
  181.  
  182.     # show canvas
  183.     pack .main.canvas.c
  184.     grid .main.canvas.c -padx 1 -in .main.canvas -pady 1 \
  185.         -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
  186.     grid .scroll -in .main.canvas -padx 1 -pady 1 \
  187.         -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
  188.  
  189.     frame .main.buttons -width [expr $columns*22]
  190.  
  191.     # draw object buttons
  192.     set n 0;
  193.     while { [file exist [format button%04x.gif $n]] } {
  194.       image create photo tile$n -file [format button%04x.gif $n]
  195.       button .main.buttons.code$n -image tile$n -command "select $n"
  196.       place .main.buttons.code$n -x [expr ($n/8)*20+1] -y [expr ($n%8)*20+1] -width 20 -height 20
  197.       incr n;
  198.     }
  199.  
  200.     # highlight default button
  201.     if { $windows } {
  202.       .main.buttons.code0 configure -bg red
  203.     } else {
  204.       .main.buttons.code0 configure -highlightbackground red
  205.     }
  206.  
  207.     # create background image
  208.     image create photo background -file buttonback.gif
  209.     for {set y 0} {$y<[expr $llength/10]} {incr y} {
  210.       .main.canvas.c create image 0 [expr $y*160] -anchor nw -image background
  211.     }
  212.  
  213.     # display all columns/rows
  214.     for {set y 0} {$y<$llength} {incr y} {
  215.     for {set x 0} {$x<10} {incr x} {
  216.        .main.canvas.c create image [expr 16*$x] [expr 16*$y] -anchor nw -tags "id($x,$y) tile"
  217.     }
  218.     }
  219.  
  220.     # create empty image
  221.     image create photo none -width 16 -height 16
  222.     # and draw a nice blue box on it
  223.     none put blue -to 6 6 8 8
  224.  
  225.     pack .main.canvas .main.buttons -side left -fill both -expand yes
  226.  
  227.     # filename box
  228.     frame .fname
  229.     label .fname.label -textvariable fileshort -relief sunken -bd 1 -font "Helvetica 12" -anchor c
  230.     pack .fname.label -side left -padx 2 -expand yes -fill both
  231.  
  232.     set menustatus "    "
  233.     frame .statusBar
  234.     label .statusBar.label -textvariable menustatus -relief sunken -bd 1 -font "Helvetica 10" -anchor w
  235.     pack .statusBar.label -side left -padx 2 -expand yes -fill both
  236.     menu .menu -tearoff 0
  237.  
  238.     set m .menu.file
  239.     menu $m -tearoff 0
  240.     .menu add cascade -label "File" -menu $m -underline 0
  241.     $m add command -label "About..." -command { tk_messageBox -icon info \
  242.        -message "ArgonED V1.0\nArgon V level editor\n(c) 2000 by Till Harbaum" -type ok\
  243.     -parent . }
  244.     $m add separator
  245.     $m add command -label "Open..." -command { fileio 0 }
  246.     $m add command -label "New" -command { clear_buffer }
  247.     $m add command -label "Save" -command { fileio 1 }
  248.     $m add command -label "Save As..." -command { fileio 2 }
  249.     $m add separator
  250.     $m add command -label "Quit" -command "destroy .; exit"
  251.  
  252.     pack .fname .main .statusBar -side top -fill x -pady 2
  253.  
  254.     . configure -menu .menu
  255.  
  256.     bind Menu <<MenuSelect>> {
  257.     global $menustatus
  258.     if {[catch {%W entrycget active -label} label]} {
  259.         set label "    "
  260.     }
  261.     set menustatus $label
  262.     update idletasks
  263.     }
  264.  
  265.     # left button sets, middle and right button erase
  266.     .main.canvas.c bind all <1> "click 0"
  267.     .main.canvas.c bind all <2> "click 1"
  268.     .main.canvas.c bind all <3> "click 1"
  269.  
  270.     # some hints for the window manager
  271.     wm resizable . 0 0
  272.     wm title . "ArgonED V1.0"
  273. }
  274.  
  275. ##############################################################################
  276. # Main program
  277.  
  278. set last_button 0;
  279.  
  280. # determine if we are running under bills os
  281. if { $tcl_platform(platform) == "windows" } {
  282.   set windows true  
  283. } else {
  284.   set windows false
  285. }
  286.  
  287.  
  288. set llength [expr $llength*10]
  289. set filename ""
  290.  
  291. # get filename from command line
  292. if { $argc > 0 } {
  293.   if { [file exists [lindex $argv 0]] } {
  294.     set filename [lindex $argv 0]
  295.     set fileshort [file tail $filename]
  296.   }
  297. }
  298.  
  299. createwindows
  300.  
  301. # initialize buffer
  302. if { $filename != "" } {
  303.   fileio 3
  304. } else {
  305.   clear_buffer
  306. }
  307.  
  308. #
  309. # Now, wait for user actions...
  310. #
  311.