File: RPGEdMap.tcl

package info (click to toggle)
roleplaying 2.0-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 16,620 kB
  • ctags: 1,039
  • sloc: tcl: 7,155; cpp: 2,709; ansic: 2,227; makefile: 559; sh: 330; csh: 3
file content (1111 lines) | stat: -rw-r--r-- 34,129 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
#* 
#* ------------------------------------------------------------------
#* Role PlayingDB V2.0 by Deepwoods Software
#* ------------------------------------------------------------------
#* RPGEdMap.tcl - Map editor
#* Created by Robert Heller on Mon Aug 24 14:31:22 1998
#* ------------------------------------------------------------------
#* Modification History: 
#* $Log: RPGEdMap.tcl,v $
#* Revision 1.6  1999/07/13 01:29:16  heller
#* Fix documentation: spelling, punctuation, etc.
#*
#* Revision 1.5  1999/04/19 21:36:01  heller
#* Update HelpTopics to match help topic links.
#*
#* Revision 1.4  1999/03/28 06:20:44  heller
#* Update on-line help.
#*
#* Revision 1.3  1999/01/02 00:07:53  heller
#* Small error in WriteMap.
#*
#* Revision 1.2  1999/01/01 18:00:12  heller
#* Remove Load button and add Help button
#*
#* Revision 1.1  1998/12/29 22:13:30  heller
#* Initial revision
#*
#* ------------------------------------------------------------------
#* Contents:
#* ------------------------------------------------------------------
#*  
#*     Role Playing DB -- A database package that creates and maintains
#* 		       a database of RPG characters, monsters, treasures,
#* 		       spells, and playing environments.
#* 
#*     Copyright (C) 1995,1998  Robert Heller D/B/A Deepwoods Software
#* 			51 Locke Hill Road
#* 			Wendell, MA 01379-9728
#* 
#*     This program is free software; you can redistribute it and/or modify
#*     it under the terms of the GNU General Public License as published by
#*     the Free Software Foundation; either version 2 of the License, or
#*     (at your option) any later version.
#* 
#*     This program is distributed in the hope that it will be useful,
#*     but WITHOUT ANY WARRANTY; without even the implied warranty of
#*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#*     GNU General Public License for more details.
#* 
#*     You should have received a copy of the GNU General Public License
#*     along with this program; if not, write to the Free Software
#*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#* 
#*  
#* 

#@Chapter:RPGEdMap.tcl -- Maps of the adventure
#@Label:RPGEdMap.tcl
#$Id: RPGEdMap.tcl,v 1.6 1999/07/13 01:29:16 heller Rel1 $
# This file implements the GUIs that create and edit maps.


proc RPGEdMap {{filename {}}} {
# This procedure edits a map file.  A map contains a series of levels, each of
# which contain one or more spaces.
# <in> filename -- the name of the map file to edit.
# [index] RPGEdMap!procedure

  global tk_version

  set toplevel [GenerateToplevelName rpgEdMap]

  RPGToplevel .$toplevel {Role Playing V2 Map Editor} Map
  wm withdraw .$toplevel
  global .$toplevel
  upvar #0 .$toplevel data
  set data(filename) "$filename"
  set data(filetype) "map"
  if {[string length "$filename"] > 0} {
    if {[ReadMap .$toplevel "$filename"] == 0} {
      CloseWindow .$toplevel
      tkerror "File does not exist or is not readable: $filename"
      return
    }
  } elseif {[CreateNewMap .$toplevel] == 0} {
    CloseWindow .$toplevel
    return
  }

  # build widget .$toplevel.upper
  frame .$toplevel.upper \
    -borderwidth {2} \
    -relief {ridge}

  # build widget .$toplevel.upper.canvasMF
  frame .$toplevel.upper.canvasMF

  # build widget .$toplevel.upper.canvasMF.canvasVscroll
  frame .$toplevel.upper.canvasMF.canvasVscroll \
    -borderwidth {2}

  # build widget .$toplevel.upper.canvasMF.canvasVscroll.mapCanvas
  canvas .$toplevel.upper.canvasMF.canvasVscroll.mapCanvas \
    -borderwidth {2} \
    -confine {0} \
    -height {150} \
    -relief {sunken} \
    -width {150} \
    -scrollregion "$data(scrollregion)" \
    -xscrollcommand ".$toplevel.upper.canvasMF.hScrollFill.hscroll set" \
    -yscrollcommand ".$toplevel.upper.canvasMF.canvasVscroll.vScroll set"
  bind .$toplevel.upper.canvasMF.canvasVscroll.mapCanvas <1> \
		[list UnselectLoadedSpace ".$toplevel" %x %y]

  # build widget .$toplevel.upper.canvasMF.canvasVscroll.vScroll
  scrollbar .$toplevel.upper.canvasMF.canvasVscroll.vScroll \
    -command ".$toplevel.upper.canvasMF.canvasVscroll.mapCanvas yview"
  # build widget .$toplevel.upper.canvasMF.hScrollFill
  frame .$toplevel.upper.canvasMF.hScrollFill \
    -borderwidth {2}

  # build widget .$toplevel.upper.canvasMF.hScrollFill.hscroll
  scrollbar .$toplevel.upper.canvasMF.hScrollFill.hscroll \
    -command ".$toplevel.upper.canvasMF.canvasVscroll.mapCanvas xview" \
    -orient {horizontal}

  # build widget .$toplevel.upper.canvasMF.hScrollFill.frame22
  frame .$toplevel.upper.canvasMF.hScrollFill.frame22 \
    -borderwidth {2} \
    -height {17} \
    -width {24}

  # build widget .$toplevel.upper.info
  frame .$toplevel.upper.info \
    -borderwidth {2}

  # build widget .$toplevel.upper.info.name
  frame .$toplevel.upper.info.name

  # build widget .$toplevel.upper.info.name.label9
  label .$toplevel.upper.info.name.label9 \
    -text {Name:}

  # build widget .$toplevel.upper.info.name.value
  entry .$toplevel.upper.info.name.value \
    -width {0} \
    -textvariable ".[set toplevel](name)"

  # build widget .$toplevel.upper.info.level
  frame .$toplevel.upper.info.level

  # build widget .$toplevel.upper.info.level.label9
  label .$toplevel.upper.info.level.label9 \
    -text {Current Level:}

  # build widget .$toplevel.upper.info.level.value
  scale .$toplevel.upper.info.level.value \
    -length {13} \
    -orient {horizontal} \
    -sliderlength {4} \
    -from $data(highestLevel) \
    -to $data(deepestLevel) \
    -width {4} \
    -command "SelectLevel .$toplevel"

  # build widget .$toplevel.upper.info.description
  frame .$toplevel.upper.info.description \
    -relief {raised}

  # build widget .$toplevel.upper.info.description.scrollbar1
  scrollbar .$toplevel.upper.info.description.scrollbar1 \
    -command ".$toplevel.upper.info.description.value yview"

  # build widget .$toplevel.upper.info.description.value
  text .$toplevel.upper.info.description.value \
    -height {9} \
    -width {28} \
    -wrap {word} \
    -yscrollcommand ".$toplevel.upper.info.description.scrollbar1 set"
  bindtags .$toplevel.upper.info.description.value \
	[list .$toplevel.upper.info.description.value Text .$toplevel all UpdDescription]

  # build widget .$toplevel.controls
  frame .$toplevel.controls \
    -borderwidth {2} \
    -relief {ridge}

  # build widget .$toplevel.controls.spaceEdit
  frame .$toplevel.controls.spaceEdit \
    -borderwidth {2}

  # build widget .$toplevel.controls.spaceEdit.label18
  label .$toplevel.controls.spaceEdit.label18 \
    -borderwidth {4} \
    -relief {ridge} \
    -text {Space:}

  # build widget .$toplevel.controls.spaceEdit.button19
  button .$toplevel.controls.spaceEdit.button19 \
    -text {New} \
    -command "AddNewSpace .$toplevel"

  # build widget .$toplevel.controls.spaceEdit.button20
  button .$toplevel.controls.spaceEdit.button20 \
    -text {Delete} \
    -command "DeleteSpace .$toplevel" \
    -state disabled

  # build widget .$toplevel.controls.spaceEdit.name
  label .$toplevel.controls.spaceEdit.name \
    -relief {sunken} \
    -textvariable ".[set toplevel](selectedSpaceName)"

  # build widget .$toplevel.buttons
  frame .$toplevel.buttons \
    -borderwidth {2}

  # build widget .$toplevel.buttons.button24
  button .$toplevel.buttons.button24 \
    -text {Save} \
    -command "SaveMap .$toplevel"

  # pack master .$toplevel.upper
  pack configure .$toplevel.upper.canvasMF \
    -expand 1 \
    -fill both \
    -side left
  pack configure .$toplevel.upper.info \
    -expand 1 \
    -fill both \
    -side right

  # pack master .$toplevel.upper.canvasMF
  pack configure .$toplevel.upper.canvasMF.canvasVscroll \
    -expand 1 \
    -fill both
  pack configure .$toplevel.upper.canvasMF.hScrollFill \
    -fill x \
    -side bottom

  # pack master .$toplevel.upper.canvasMF.canvasVscroll
  pack configure .$toplevel.upper.canvasMF.canvasVscroll.mapCanvas \
    -expand 1 \
    -fill both \
    -side left
  pack configure .$toplevel.upper.canvasMF.canvasVscroll.vScroll \
    -fill y \
    -side right

  # pack master .$toplevel.upper.canvasMF.hScrollFill
  pack configure .$toplevel.upper.canvasMF.hScrollFill.hscroll \
    -expand 1 \
    -fill x \
    -side left
  pack configure .$toplevel.upper.canvasMF.hScrollFill.frame22 \
    -side right

  # pack master .$toplevel.upper.info
  pack configure .$toplevel.upper.info.name \
    -expand 1 \
    -fill x
  pack configure .$toplevel.upper.info.level \
    -expand 1 \
    -fill x
  pack configure .$toplevel.upper.info.description \
    -expand 1 \
    -fill both

  # pack master .$toplevel.upper.info.name
  pack configure .$toplevel.upper.info.name.label9 \
    -side left
  pack configure .$toplevel.upper.info.name.value \
    -expand 1 \
    -fill x \
    -side right

  # pack master .$toplevel.upper.info.level
  pack configure .$toplevel.upper.info.level.label9 \
    -side left
  pack configure .$toplevel.upper.info.level.value \
    -expand 1 \
    -fill x \
    -side right

  # pack master .$toplevel.upper.info.description
  pack configure .$toplevel.upper.info.description.scrollbar1 \
    -fill y \
    -side right
  pack configure .$toplevel.upper.info.description.value \
    -expand 1 \
    -fill both

  # pack master .$toplevel.controls
  pack configure .$toplevel.controls.spaceEdit \
    -expand 1 \
    -fill x

  # pack master .$toplevel.controls.spaceEdit
  pack configure .$toplevel.controls.spaceEdit.label18 \
    -side left
  pack configure .$toplevel.controls.spaceEdit.button19 \
    -side left
  pack configure .$toplevel.controls.spaceEdit.button20 \
    -side left
  pack configure .$toplevel.controls.spaceEdit.name \
    -expand 1 \
    -fill x \
    -side right

  # pack master .$toplevel.buttons
  pack configure .$toplevel.buttons.button24 \
    -expand 1 \
    -side left

  # pack master .$toplevel
  pack configure .$toplevel.upper \
    -expand 1 \
    -fill both
  pack configure .$toplevel.controls \
    -expand 1 \
    -fill x
  pack configure .$toplevel.buttons \
    -expand 1 \
    -fill x

  # build canvas items .$toplevel.upper.canvasMF.canvasVscroll.mapCanvas
  .$toplevel.upper.info.description.value insert end "$data(description)"
  SelectLevel .$toplevel [.$toplevel.upper.info.level.value get]
  set data(selectedSpaceId) {}
  set data(selectedSpaceIndexString) {}
  set data(selectedSpaceName) {}
# end of widget tree

  wm deiconify .$toplevel

}

proc SelectLevel {tl level} {
# This procedure selects the level to display.  It is bound to the level
# scale widget.
# <in> tl -- the toplevel.
# <in> level -- the new level.
# [index] SelectLevel!procedure

  upvar #0 $tl data
  set level [expr int($level)]
  set data(level)  $level
  $tl.upper.canvasMF.canvasVscroll.mapCanvas delete all
  set data(selectedSpaceId) {}
  set data(selectedSpaceIndexString) {}
  set data(selectedSpaceName) {}
  ReDrawSpaces $tl
}

proc ReadCompleteList {fp bufferVar} {
# Helper function to read in a complete in the Tcl sense -- balanced parens,
# braces, brackets, and quote marks.
# <in> fp -- file pointer object.
# <name> bufferVar -- the name of the buffer variable to collect the input in.
# [index] ReadCompleteList!procedure

  upvar 1 $bufferVar buffer
  set buffer {}
  set result -1
  while {1} {
    if {[gets $fp line] < 0} {
      return $result
    }
    append buffer "$line"
    if {$result < 0} {
      set result [string length "$buffer"]
    } else {
      incr result [string length "$buffer"]
    }
    if {[string length "$buffer"] > 0 && \
	[info complete "$buffer"] == 1} {return result}
  }
}


proc ReadMap {tl filename} {
# Procedure to read a map into a GUI toplevel. A map file contains a set of 
# name value items that map to the data array used to represent the map while
# in memory.  The saved fields include: shape, name, description, deepestLevel,
# highestLevel, maxX, maxY, minX, minY, and all of the space file names 
# (Spaces,l,x,y). 
# <in> tl -- the name of the toplevel.
# <in> filename -- the name of the file to read.
# [index] ReadMap!procedure

  upvar #0 $tl data

  if {[catch [list open "$filename" r] rfp]} {
    bgerror "Could not open map file: $filename for reading: $rfp"
    return 0
  }

  while {[ReadCompleteList $rfp list] >= 0} {
    set s [lindex $list 0]
    set v "[lindex $list 1]"
    set data($s) "$v"
    if {[regexp {^Spaces,([^,]+),([^,]+),(.+)$} "$s" whole l x y] > 0} {
      set buffer [Record]
      if {[catch [list $buffer ReadRecord "$v"] err]} {
        bgerror "Missing or unreadable space file: $v, skipped: $err"
	unset data($s)
	rename $buffer {}
	continue
      }
      if {[string compare {*Space} "[lindex [$buffer ReturnRecord] 0]"] != 0} {
	bgerror "Not a space file: $v"
	unset data($s)
	rename $buffer {}
	continue
      }
      set data(SpaceDirty,$l,$x,$y) 0
      set data(Spaces,$l,$x,$y) [Space]
      set data(SpaceFiles,$l,$x,$y) "$v"
      $data(Spaces,$l,$x,$y) UpdateFromRecord $buffer
      rename $buffer {}
    }
  }

  set data(cleanupFun) CleanUpMap
  set data(scrollregion) [list [expr $data(minX) * 10] \
			       [expr $data(minY) * 10] \
			       [expr $data(maxX) * 10] \
			       [expr $data(maxY) * 10]]
  set data(dirty) 0

  return 1
}

proc CreateNewMap {tl} {
# This procedure creates a new map.
# <in> tl -- the toplevel.
# [index] CreateNewMap!procedure

  upvar #0 $tl data

  # build widget .createMapDialog
  if {"[info procs XFEdit]" != ""} {
    catch "XFDestroy .createMapDialog"
  } {
    catch "destroy .createMapDialog"
  }
  toplevel .createMapDialog 

  # Window manager configurations
  wm positionfrom .createMapDialog ""
  wm sizefrom .createMapDialog ""
  wm maxsize .createMapDialog 1000 1000
  wm minsize .createMapDialog 10 10
  wm title .createMapDialog {Create New Map}
  wm transient .createMapDialog .


  # build widget .createMapDialog.name
  frame .createMapDialog.name \
    -borderwidth {2} \
    -relief {ridge}

  # build widget .createMapDialog.name.label4
  label .createMapDialog.name.label4 \
    -text {Name:}

  # build widget .createMapDialog.name.value
  entry .createMapDialog.name.value \
    -width {0} \
    -textvariable "[set tl](name)"

  # build widget .createMapDialog.sqhex
  frame .createMapDialog.sqhex \
    -borderwidth {2} \
    -relief {ridge}

  # build widget .createMapDialog.sqhex.radiobutton6
  radiobutton .createMapDialog.sqhex.radiobutton6 \
    -relief {raised} \
    -text {Squares} \
    -value {Space::Square} \
    -variable "[set tl](shape)"

  # build widget .createMapDialog.sqhex.radiobutton7
  radiobutton .createMapDialog.sqhex.radiobutton7 \
    -relief {raised} \
    -text {Hexes} \
    -value {Space::Hexagon} \
    -variable "[set tl](shape)"

  # build widget .createMapDialog.description
  frame .createMapDialog.description \
    -borderwidth {2} \
    -relief {ridge}

  # build widget .createMapDialog.description.scrollbar1
  scrollbar .createMapDialog.description.scrollbar1 \
    -command {.createMapDialog.description.value yview} \
    -relief {raised}

  # build widget .createMapDialog.description.value
  text .createMapDialog.description.value \
    -height {10} \
    -width {70} \
    -wrap {word} \
    -yscrollcommand {.createMapDialog.description.scrollbar1 set}

  # build widget .createMapDialog.buttons
  frame .createMapDialog.buttons \
    -borderwidth {2}

  # build widget .createMapDialog.buttons.button8
  button .createMapDialog.buttons.button8 \
    -text {Create} \
    -command "CreateNewMapCreateButton $tl .createMapDialog"

  # build widget .createMapDialog.buttons.button9
  button .createMapDialog.buttons.button9 \
    -text {Dismiss} \
    -command "CreateNewMapDismisButton $tl .createMapDialog"

  # build widget .createMapDialog.buttons.button10
  button .createMapDialog.buttons.button10 \
    -text {Help} \
    -command {HelpTopic {Create Map Dialog}}

  # build widget .createMapDialog.extents
  frame .createMapDialog.extents \
    -borderwidth {2} \
    -relief {ridge}

  # build widget .createMapDialog.extents.label2
  label .createMapDialog.extents.label2 \
    -text {Min X:}

  # build widget .createMapDialog.extents.minX
  entry .createMapDialog.extents.minX \
    -width {0} \
    -textvariable "[set tl](minX)"
  bindtags .createMapDialog.extents.minX \
	[list .createMapDialog.extents.minX Entry .createMapDialog all IntEntry]

  # build widget .createMapDialog.extents.label4
  label .createMapDialog.extents.label4 \
    -text {Min Y:}

  # build widget .createMapDialog.extents.minY
  entry .createMapDialog.extents.minY \
    -width {0} \
    -textvariable "[set tl](minY)"
  bindtags .createMapDialog.extents.minY \
	[list .createMapDialog.extents.minY Entry .createMapDialog all IntEntry]

  # build widget .createMapDialog.extents.label6
  label .createMapDialog.extents.label6 \
    -text {Max X:}

  # build widget .createMapDialog.extents.maxX
  entry .createMapDialog.extents.maxX \
    -width {0} \
    -textvariable "[set tl](maxX)"
  bindtags .createMapDialog.extents.maxX \
	[list .createMapDialog.extents.maxX Entry .createMapDialog all IntEntry]

  # build widget .createMapDialog.extents.label8
  label .createMapDialog.extents.label8 \
    -text {Max Y:}

  # build widget .createMapDialog.extents.maxY
  entry .createMapDialog.extents.maxY \
    -width {0} \
    -textvariable "[set tl](maxY)"
  bindtags .createMapDialog.extents.maxY \
	[list .createMapDialog.extents.maxY Entry .createMapDialog all IntEntry]

  # build widget .createMapDialog.extents.label10
  label .createMapDialog.extents.label10 \
    -text {Max Depth:}

  # build widget .createMapDialog.extents.deepestLevel
  entry .createMapDialog.extents.deepestLevel \
    -width {0} \
    -textvariable "[set tl](deepestLevel)"
  bindtags .createMapDialog.extents.deepestLevel \
	[list .createMapDialog.extents.deepestLevel Entry .createMapDialog all IntEntry]

  # build widget .createMapDialog.extents.label12
  label .createMapDialog.extents.label12 \
    -text {Max Height:}

  # build widget .createMapDialog.extents.highestLevel
  entry .createMapDialog.extents.highestLevel \
    -width {0} \
    -textvariable "[set tl](highestLevel)"
  bindtags .createMapDialog.extents.highestLevel \
	[list .createMapDialog.extents.minX Entry .createMapDialog all IntEntry]

  # pack master .createMapDialog.name
  pack configure .createMapDialog.name.label4 \
    -side left
  pack configure .createMapDialog.name.value \
    -expand 1 \
    -fill x \
    -side right

  # pack master .createMapDialog.sqhex
  pack configure .createMapDialog.sqhex.radiobutton6 \
    -expand 1 \
    -side left
  pack configure .createMapDialog.sqhex.radiobutton7 \
    -expand 1 \
    -side right

  # pack master .createMapDialog.description
  pack configure .createMapDialog.description.scrollbar1 \
    -fill y \
    -side right
  pack configure .createMapDialog.description.value \
    -expand 1 \
    -fill both

  # pack master .createMapDialog.buttons
  pack configure .createMapDialog.buttons.button8 \
    -expand 1 \
    -side left
  pack configure .createMapDialog.buttons.button9 \
    -expand 1 \
    -side left
  pack configure .createMapDialog.buttons.button10 \
    -expand 1 \
    -side right

  # pack master .createMapDialog.extents
  pack configure .createMapDialog.extents.label2 \
    -side left
  pack configure .createMapDialog.extents.minX \
    -expand 1 \
    -fill x \
    -side left
  pack configure .createMapDialog.extents.label4 \
    -side left
  pack configure .createMapDialog.extents.minY \
    -expand 1 \
    -fill x \
    -side left
  pack configure .createMapDialog.extents.label6 \
    -side left
  pack configure .createMapDialog.extents.maxX \
    -expand 1 \
    -side left
  pack configure .createMapDialog.extents.label8 \
    -side left
  pack configure .createMapDialog.extents.maxY \
    -expand 1 \
    -fill x \
    -side left
  pack configure .createMapDialog.extents.label10 \
    -side left
  pack configure .createMapDialog.extents.deepestLevel \
    -expand 1 \
    -fill x \
    -side left
  pack configure .createMapDialog.extents.label12 \
    -side left
  pack configure .createMapDialog.extents.highestLevel \
    -expand 1 \
    -fill x \
    -side left

  # pack master .createMapDialog
  pack configure .createMapDialog.name \
    -expand 1 \
    -fill x
  pack configure .createMapDialog.sqhex \
    -expand 1 \
    -fill x
  pack configure .createMapDialog.description \
    -fill both
  pack configure .createMapDialog.extents \
    -expand 1 \
    -fill x
  pack configure .createMapDialog.buttons \
    -expand 1 \
    -fill x

  set data(name) {}
  .createMapDialog.description.value insert end {}
  set data(minX) -75
  set data(minY) -75
  set data(maxX) 75
  set data(maxY) 75
  set data(deepestLevel) 5
  set data(highestLevel) 1
  set data(shape) {Space::Square}
  set data(cleanupFun) CleanUpMap
  set data(CreateResult) 0
  set data(dirty) 0
# end of widget tree

  set oldFocus [focus]
  set oldGrab [grab current .createMapDialog]
  if {$oldGrab != ""} {
    set grabStatus [grab status $oldGrab]
  }

  focus .createMapDialog.name.value
  grab .createMapDialog
  tkwait window .createMapDialog

  catch {focus $oldFocus}
  if {$oldGrab != ""} {
        if {$grabStatus == "global"} {
            grab -global $oldGrab
        } else {
            grab $oldGrab
        }
  }
  return $data(CreateResult)
}

proc CheckWriteDirtyRecordMap {tl} {
# This procedure is called when the toplevel window is being closed and the
# data is ``dirty'' (modified).  It takes care of saving the data if the user
# chooses to save it.
# <in> tl -- the toplevel.
# [index] CheckWriteDirtyRecordMap!procedure

  upvar #0 $tl data
  global $tl

#  get Map base file name

  set filename "$data(filename)"
  set filetype "$data(filetype)"
  set saveP [tk_dialog .askDirty "Save map?" "Save modified Map data?" \
	questhead 0 "Yes" "No"]
  if {$saveP == 1} {
    CleanUpMap $tl
    return
  }
  if {[string length "$filename"] == 0} {
    set filename [tk_getSaveFile -defaultextension ".$filetype" \
				 -initialfile "new.$filetype" \
				 -initialdir "[pwd]" \
				 -filetypes [list [list "Map files" \
							  "*.$filetype"]]\
				 -parent . \
				 -title {File to save map data in}]
    if {[string length "$filename"] == 0} {
      CleanUpMap $tl
      return
    }
    if {[string length "[file extension $filename]"] == 0} {
      set filename "$filename.$filetype"
    }
  }
  WriteMap $tl $filename 1

  CleanUpMap $tl
}

proc SaveMap {tl} {
# Procedure to save a map object.  Bound to the ``Save'' menu item on the 
# ``File'' menu.
# <in> tl -- the toplevel.
# [index] SaveMap!procedure

  upvar #0 $tl data
  set filename "$data(filename)"
  set filetype "$data(filetype)"
  if {[string length "$filename"] == 0} {
    set filename [tk_getSaveFile -defaultextension ".$filetype" \
				 -initialfile "new.$filetype" \
				 -initialdir "[pwd]" \
				 -filetypes [list [list "Map files" \
							  "*.$filetype"]]\
				 -parent . \
				 -title {File to save map data in}]
    if {[string length "$filename"] == 0} {
      return
    }
    if {[string length "[file extension $filename]"] == 0} {
      set filename "$filename.$filetype"
    }
    set data(filename) "$filename"
  }
  WriteMap $tl $filename 1
  set data(dirty) 0
  foreach s [array names data SpaceDirty,*] {
    set data($s) 0
  }
}

proc SaveAsMap {tl} {
# This procedure saves a map in an alternative file.  Bound to the ``SaveAs...''
# menu item of the ``File'' menu.
# <in> tl -- the toplevel to save.
# [index] SaveAsMap!procedure

  upvar #0 $tl data
  set filename "$data(filename)"
  set filetype "$data(filetype)"
  if {[string length "$filename"] == 0} {set filename "new.$filetype"}
  set filename [tk_getSaveFile -defaultextension ".$filetype" \
                                 -initialfile "$filename" \
                                 -initialdir "[pwd]" \
                                 -filetypes [list [list "Map files" \
                                                          "*.$filetype"]]\
                                 -parent . \
                                 -title {File to save map data in}]
  if {[string length "$filename"] == 0} {
      return
  }
  if {[string length "[file extension $filename]"] == 0} {
      set filename "$filename.$filetype"
  }
  set data(filename) "$filename"
  WriteMap $tl $filename 0 1
  set data(dirty) 0
  foreach s [array names data SpaceDirty,*] {
    set data($s) 0
  }
}

proc WriteMap {tl filename {IfDirty 0} {forceName 0}} {
# Procedure to write out a map object to a file.  The data is written out as 
# a series of Tcl 2 element lists: slot name slot value.  The slots saved are:
# shape, name, description, deepestLevel, highestLevel, maxX, maxY, minX,
# minY, and all of the space files.  Each (dirty) space is also written out.
# The space file names are formed from the base map file name with the spaces
# l, x, and y coordinates encoded into the filenames
# <in> tl -- the toplevel to write.
# <in> filename -- the filename to write to.
# <in> IfDirty -- write out only dirty spaces.
# <in> forceName -- compute new filenames for all spaces.
# [index] WriteMap!procedure

  upvar #0 $tl data

# write base map file
#data(shape)                    = Space::Hexagon
#data(name)                     = Test
#data(description)              = 
#data(deepestLevel)             = 5
#data(highestLevel)             = 1
#data(maxX)                     = 75
#data(maxY)                     = 75
#data(minX)                     = -75
#data(minY)                     = -75

  if {[catch [list open "$filename" w] wfp]} {
    bgerror "Could not open map file: $filename for writing: $wfp"
    return 0
  }

  foreach s {shape name description deepestLevel highestLevel maxX maxY 
             minX minY} {
    puts $wfp [list $s "$data($s)"]
  }

  set fBase [file rootname "$filename"]

  foreach spI [array names data Spaces,*] {
    set space $data($spI)
    if {[string compare "[info command $space]" {}] == 0} {
      set space [Space -this $space]
    }
    regsub -all {Spaces} "$spI" {SpaceFiles} spFileI
    regsub -all {Spaces} "$spI" {SpaceDirty} spDI
    set spFile "$data($spFileI)"
    regexp {^Spaces,([^,]+),([^,]+),(.+)$} "$spI" whole l x y
    if {[regsub -all {\.} "$x" {_} xx] > 0} {set x "$xx"}
    if {[regsub -all {\.} "$y" {_} yy] > 0} {set y "$yy"}
    if {$data($spDI) > 0 || $IfDirty == 0 || $forceName == 1 || \
	[string length "$spFile"] == 0} {
      # Write Space file
#data(SpaceFiles,1,0.0,0.0)     = 
      if {[string length "$spFile"] == 0 || $forceName == 1} {
	set spFile "${fBase}SpaceL${l}X${x}Y${y}.space"
      }
      set buffer [Record -this [$space RawData]]
      $buffer WriteRecord "$spFile"
      rename $buffer {}
    }
    # write space info
    puts $wfp [list $spI "$spFile"]
  }  

  close $wfp
  return 1
}

proc CleanUpMap {tl} {
# Clean up procedure.  Free up all of the space objects and then free up the
# map array itself.
# <in> tl -- the toplevel to clean up.
# [index] CleanUpMap!procedure

  upvar #0 $tl data
  global $tl

  foreach spI [array names data Spaces,*] {
    set space $data($spI)
    if {[string compare "[info command $space]" {}] == 0} {
      set space [Space -this $space]
    }
    rename $space {}
  }  
  unset $tl
}


proc CreateNewMapCreateButton {tl dialog} {
# Procedure to handle the Create button on the create new map dialog.
# <in> tl -- the toplevel to attach the new map to.
# <in> dialog -- the dialog used to set up the creation parameters.
# [index] CreateNewMapCreateButton!procedure

  upvar #0 $tl data
  if {[string length "set data(name)"] == 0} {
    return
  }
  set data(CreateResult) 1
  set data(description) "[.createMapDialog.description.value get 1.0 end]"
  set data(scrollregion) [list [expr $data(minX) * 10] \
			       [expr $data(minY) * 10] \
			       [expr $data(maxX) * 10] \
			       [expr $data(maxY) * 10]]
  set data(dirty) 1
  if {"[info procs XFEdit]" != ""} {
    catch "XFDestroy $dialog"
  } {
    catch "destroy $dialog"
  }
}

proc CreateNewMapDismisButton {tl dialog} {
# Procedure to handle the Dismiss button on the create new map dialog.
# <in> tl -- the toplevel to attach the new map to.
# <in> dialog -- the dialog used to set up the creation parameters.
# [index] CreateNewMapDismisButton!procedure

  upvar #0 $tl data
  set data(CreateResult) 0
  if {"[info procs XFEdit]" != ""} {
    catch "XFDestroy $dialog"
  } {
    catch "destroy $dialog"
  }
}  

proc AddNewSpace {tl} {
# Procedure to add a new space to the current level.
# <in> tl -- the toplevel.
# [index] AddNewSpace!procedure

  upvar #0 $tl data
  set newSpace "[CreateNewSpace $tl]"
  if {[string length "$newSpace"] > 0} {
    if {[catch [list set data(Spaces,$data(level),[$newSpace CenterX],[$newSpace CenterY])] old] == 0} {
      rename $old {}
    }
    if {[catch [list set data(SpaceFiles,$data(level),[$newSpace CenterX],[$newSpace CenterY])] old] == 0} {
      file delete -force "$old"
    }
    set data(Spaces,$data(level),[$newSpace CenterX],[$newSpace CenterY]) $newSpace
    set data(SpaceFiles,$data(level),[$newSpace CenterX],[$newSpace CenterY]) {}
    set data(SpaceDirty,$data(level),[$newSpace CenterX],[$newSpace CenterY]) 1
    set data(dirty) 1
    ReDrawSpaces $tl
  }
}

proc ReDrawSpaces {tl} {
# Procedure to re-draw the spaces in the current level.
# <in> tl -- the toplevel.
# [index] ReDrawSpaces!procedure

  upvar #0 $tl data
  set canvas $tl.upper.canvasMF.canvasVscroll.mapCanvas
  $canvas delete all
  set data(selectedSpaceId) {}
  set spaceElts [array names data "Spaces,$data(level),*,*"]
  foreach spelt $spaceElts {
    set space $data($spelt)
    set drawcmd "[$space MakeGraphicCommannd .1]"
    set id [eval [concat $canvas create $drawcmd]]
    $canvas bind $id <Double-Button-1> [list EditLoadedSpace \
			       $tl \
			       "$data(SpaceFiles,$data(level),[$space CenterX],[$space CenterY])" \
			       $space \
			       $data(level)]
    
    $canvas bind $id <1> [list SelectLoadedSpace \
			       $tl \
			       $space \
			       $id \
			       "$data(level),[$space CenterX],[$space CenterY]"]
    if {[string compare "$data(selectedSpaceIndexString)" \
			"$data(level),[$space CenterX],[$space CenterY]"] == 0} {
      SelectLoadedSpace $tl $space $id \
			"$data(level),[$space CenterX],[$space CenterY]"
    }    
  }
}

proc SelectLoadedSpace {tl space id indexString} {
# Procedure bound to button 1 -- Select.  This procedure marks the space as
# selected.  The space's border is changed from black to read (highlighted)
# and the space's access info is saved in the data array for use elsewhere.
# <in> tl -- the toplevel.
# <in> space -- the Space object.
# <in> id -- the space's canvas id.
# <in> indexString -- the space's index string.
# [index] SelectLoadedSpace!procedure

  upvar #0 $tl data
  set canvas $tl.upper.canvasMF.canvasVscroll.mapCanvas
  if {[string length "$data(selectedSpaceId)"] > 0} {
    $canvas itemconfigure $data(selectedSpaceId) -outline black
  }
  $canvas itemconfigure $id -outline red
  set data(selectedSpaceId) $id
  set data(selectedSpaceIndexString) "$indexString"
  set data(selectedSpaceName) "[$space Name]"
  $tl.controls.spaceEdit.button20 configure -state normal
}

proc UnselectLoadedSpace {tl Mx My} {
# Procedure bound to the base canvas's button 1 event. This procedure is used
# to deselect the currently selected space when the user clicks on the canvas 
# background.
# <in> tl -- the toplevel.
# <in> Mx -- mouse X coordinate.
# <in> My -- mouse Y coordinate.
# [index] UnselectLoadedSpace!procedure

  upvar #0 $tl data
  set canvas $tl.upper.canvasMF.canvasVscroll.mapCanvas
  set x [$canvas canvasx $Mx]
  set y [$canvas canvasy $My]
  if {[string length "$data(selectedSpaceId)"] > 0} {
    set newitem [$canvas find overlapping $x $y [expr $x + 1.0] [expr $y + 1.0]]
    set olditem $data(selectedSpaceId)
    if {[string compare "$newitem" "$olditem"] == 0} {return}
    $canvas itemconfigure $olditem -outline black
  }
  set data(selectedSpaceId) {}
  set data(selectedSpaceIndexString) {}
  set data(selectedSpaceName) {}
  $tl.controls.spaceEdit.button20 configure -state disabled
}

proc DeleteSpace {tl} {
# Procedure to delete the selected space.
# <in> tl -- the toplevel.
# [index] DeleteSpace!procedure

  upvar #0 $tl data
  $tl.controls.spaceEdit.button20 configure -state disabled
  if {[string compare "$data(selectedSpaceId)" {}] == 0} {
    return
  }
  set canvas $tl.upper.canvasMF.canvasVscroll.mapCanvas
  $canvas delete $data(selectedSpaceId)
  set sp $data(Spaces,$data(selectedSpaceIndexString))
  set spFile $data(SpaceFiles,$data(selectedSpaceIndexString))
  unset data(SpaceDirty,$data(selectedSpaceIndexString))
  rename $sp {}
  if {[string length "$spFile"] > 0} {
    catch [list file delete -force "$spFile"]
  }
  unset data(SpaceFiles,$data(selectedSpaceIndexString))
  unset data(Spaces,$data(selectedSpaceIndexString))
  set data(selectedSpaceId) {}
  set data(selectedSpaceIndexString) {}
  set data(selectedSpaceName) {}
}  
    
proc OpenMap {tl} {
# Procedure to open a new Map GUI toplevel and load a map file into it.
# <in> tl -- the current toplevel.
# [index] OpenMap!procedure

  if {"$tl" == {.}} {
    set data(filename) {}
    set data(filetype) map
    set data(class) Map
  } else {
    upvar #0 $tl data
  }
  set filename "$data(filename)"
  set filetype "$data(filetype)"
  set initdir "[file dirname $filename]"
  if {[string compare "$initdir" {.}] == 0} {set initdir "[pwd]"}
  if {[string compare "[string index "$initdir" 0]" {/}] != 0} {
    set initdir [file join "[pwd]" "$initdir"]
  }
  set filename [tk_getOpenFile -defaultextension ".$data(filetype)" \
			   -filetypes [list [list "$data(class) files" \
					"*.$filetype"]]\
			   -parent $tl \
			   -initialfile "$filename" \
			   -initialdir "$initdir" \
			   -title "File to load $data(class) data from"]
  if {[string length "$filename"] == 0} {return}
  RPGEd$data(class) "$filename"
}


package provide RPGEdMap 1.0