mainWindow.tcl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #tk scaling 1.2
  2. wm title . "Checkbox tanks"
  3. wm resizable . 0 0
  4. ttk::frame .header -relief sunken
  5. #menu
  6. menu .mainMenu -tearoff 0
  7. .mainMenu add command -label "Connect" -command connectWindow
  8. .mainMenu add command -label "Disconnect" -command { dropConnection; bench1_core 0; set statusText "Offline"; }
  9. .mainMenu add separator
  10. .mainMenu add command -label "Help" -command { helpMessage }
  11. .mainMenu add command -label "Exit" -command { destroy . }
  12. #menubutton
  13. ttk::menubutton .header.menuButton -text "Menu" -menu .mainMenu -takefocus 0
  14. #statusbar
  15. ttk::label .header.statusBar -textvariable statusText -anchor center -width 12
  16. #spawn buttons
  17. ttk::labelframe .spawnButtons -text "Respawn" -padding "5"
  18. ttk::button .spawnButtons.b1 -text "1" -command { tankCmd s 1 } -takefocus 0
  19. ttk::button .spawnButtons.b2 -text "2" -command { tankCmd s 2 } -takefocus 0
  20. ttk::button .spawnButtons.b3 -text "3" -command { tankCmd s 3 } -takefocus 0
  21. ttk::button .spawnButtons.b4 -text "4" -command { tankCmd s 4 } -takefocus 0
  22. #battelfield
  23. ttk::labelframe .battlefield -padding "5 0 5 5" -text ":)"
  24. for { set i 0 } { $i < 200 } { incr i } {
  25. ttk::checkbutton ".battlefield.f$i" -padding "0 0 0 0" -variable "fld$i" -takefocus 0
  26. }
  27. set i 0
  28. for { set row 0 } { $row < 20 } { incr row } {
  29. for { set col 0 } { $col < 10 } { incr col } {
  30. grid ".battlefield.f$i" -row $row -column $col
  31. incr i
  32. }
  33. }
  34. #attach all elements to window
  35. grid .header -row 0 -column 0 -sticky we
  36. grid .header.menuButton -row 0 -column 0 -sticky w
  37. grid .header.statusBar -row 0 -column 1 -sticky e -pady 5
  38. grid .spawnButtons -row 1 -column 0 -pady 2
  39. grid .spawnButtons.b1 -row 0 -column 0
  40. grid .spawnButtons.b2 -row 0 -column 1
  41. grid .spawnButtons.b3 -row 1 -column 0
  42. grid .spawnButtons.b4 -row 1 -column 1
  43. grid .battlefield -row 2 -column 0 -padx 5 -pady 5
  44. #functions
  45. proc connect2server {} {
  46. set resmsg 0
  47. .connectWindow.frame1.ip state disabled
  48. .connectWindow.frame1.port state disabled
  49. .connectWindow.buttons.connect state disabled
  50. .connectWindow.buttons.cancel state disabled
  51. if { ![winfo exist .connectWindow.process] } {
  52. ttk::label .connectWindow.process
  53. grid .connectWindow.process -row 1 -column 0 -pady 10
  54. }
  55. .connectWindow.process configure -text "Connecting..."
  56. connect2serverCore
  57. if { $resmsg != 0 } {
  58. connect2serverRes $resmsg
  59. }
  60. }
  61. proc connect2serverRes { res } {
  62. if { ![winfo exists .connectWindow] } { return }
  63. if { $res != 0 } {
  64. .connectWindow.process configure -text $res
  65. .connectWindow.frame1.ip state !disabled
  66. .connectWindow.frame1.port state !disabled
  67. .connectWindow.buttons.connect state !disabled
  68. .connectWindow.buttons.cancel state !disabled
  69. } else {
  70. destroy .connectWindow
  71. }
  72. }
  73. proc connectWindow {} {
  74. if { [winfo exists .connectWindow] } { return }
  75. toplevel .connectWindow -relief raised
  76. wm title .connectWindow "Connect to server"
  77. wm resizable .connectWindow 0 0
  78. ttk::labelframe .connectWindow.frame1 -padding "5" -text "Server address"
  79. ttk::label .connectWindow.frame1.iplabel -text "IP"
  80. ttk::label .connectWindow.frame1.portlabel -text "Port"
  81. ttk::entry .connectWindow.frame1.ip -justify center -width 17 -textvariable serverIp
  82. ttk::entry .connectWindow.frame1.port -justify center -width 17 -textvariable serverPort
  83. ttk::frame .connectWindow.buttons
  84. ttk::button .connectWindow.buttons.connect -text "Connect" -command { connect2server }
  85. ttk::button .connectWindow.buttons.cancel -text "Cancel" -command { destroy .connectWindow }
  86. grid .connectWindow.frame1 -row 0 -padx 10 -pady 5
  87. grid .connectWindow.frame1.iplabel -row 0 -column 0 -padx 5
  88. grid .connectWindow.frame1.ip -row 0 -column 1 -padx 5
  89. grid .connectWindow.frame1.portlabel -row 1 -column 0 -padx 5
  90. grid .connectWindow.frame1.port -row 1 -column 1 -padx 5
  91. grid .connectWindow.buttons -row 2 -column 0 -pady 5
  92. grid .connectWindow.buttons.connect -row 0 -column 0 -padx 10
  93. grid .connectWindow.buttons.cancel -row 0 -column 1 -padx 10
  94. bind .connectWindow <Return> { .connectWindow.buttons.connect invoke }
  95. bind .connectWindow <KP_Enter> { .connectWindow.buttons.connect invoke }
  96. bind .connectWindow <Escape> { .connectWindow.buttons.cancel invoke }
  97. raise .connectWindow
  98. focus -force .connectWindow
  99. focus .connectWindow.frame1.ip
  100. }
  101. proc helpMessage { } {
  102. tk_messageBox -title "Help" -message "Controls" -detail "1,2,3,4 - spawn\nw,a,s,d or arrows - move\nspace - shot"
  103. }
  104. set savedKey 0
  105. set savedTime 0
  106. proc tankControlDown { key timestamp } {
  107. global savedKey
  108. global savedTime
  109. if { $key != $savedKey || $timestamp != $savedTime } {
  110. tankControl $key 1
  111. } else {
  112. after cancel tankControl $key 0
  113. }
  114. }
  115. proc tankControlUp { key timestamp } {
  116. global savedKey
  117. global savedTime
  118. set savedKey $key
  119. set savedTime $timestamp
  120. after 5 tankControl $key 0
  121. }
  122. #key binding
  123. bind . <Control-q> { destroy . }
  124. bind . <space> { tankCmd f f }
  125. bind . <Key-1> { .spawnButtons.b1 invoke }
  126. bind . <Key-2> { .spawnButtons.b2 invoke }
  127. bind . <Key-3> { .spawnButtons.b3 invoke }
  128. bind . <Key-4> { .spawnButtons.b4 invoke }
  129. bind . <KeyPress-w> { tankControlDown u %t }
  130. bind . <KeyPress-a> { tankControlDown l %t }
  131. bind . <KeyPress-s> { tankControlDown d %t }
  132. bind . <KeyPress-d> { tankControlDown r %t }
  133. bind . <KeyPress-Up> { tankControlDown u %t }
  134. bind . <KeyPress-Left> { tankControlDown l %t }
  135. bind . <KeyPress-Down> { tankControlDown d %t }
  136. bind . <KeyPress-Right> { tankControlDown r %t }
  137. bind . <KeyRelease-w> { tankControlUp u %t }
  138. bind . <KeyRelease-a> { tankControlUp l %t }
  139. bind . <KeyRelease-s> { tankControlUp d %t }
  140. bind . <KeyRelease-d> { tankControlUp r %t }
  141. bind . <KeyRelease-Up> { tankControlUp u %t }
  142. bind . <KeyRelease-Left> { tankControlUp l %t }
  143. bind . <KeyRelease-Down> { tankControlUp d %t }
  144. bind . <KeyRelease-Right> { tankControlUp r %t }
  145. #open connect to server dialog
  146. connectWindow
  147. set statusText "Offline"
  148. ttk::style layout TCheckbutton {
  149. Checkbutton.indicator
  150. }