File: event.tk

package info (click to toggle)
xmorph 1%3A20011220
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,468 kB
  • ctags: 1,534
  • sloc: ansic: 16,401; sh: 2,651; makefile: 556; tcl: 516
file content (277 lines) | stat: -rw-r--r-- 7,761 bytes parent folder | download | duplicates (3)
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
# event.tk: Miscellaneous TCL/Tk event handlers
#
# This file is part of the tkmorph package.
#
# Written and Copyright (C) 1996-1997 by Michael J. Gourlay
#
# Provided as is.  No warrenties, express or implied.




proc fakeExpose { } {
  global morph

  # Convert the rgbaImage to photoImage (also shows up in canvas now).
  rgbaImage_src_orig toPhoto $morph(image,src,display)
  rgbaImage_dst_orig toPhoto $morph(image,dst,display)
  rgbaImage_twn_orig toPhoto $morph(image,twn,display)

  # Make sure canvas is the same size as the image
  #$morph(canvas,src) configure -width [expr [rgbaImage_src_orig cget -ncols] + 0]
  #$morph(canvas,src) configure -height [expr [rgbaImage_src_orig cget -nrows] + 0]

  set xregion [expr [rgbaImage_src_orig cget -ncols] + 2]
  set yregion [expr [rgbaImage_src_orig cget -nrows] + 2]
  $morph(canvas,src) configure -scrollregion [list -2 -2 $xregion $yregion ]

  set xregion [expr [rgbaImage_dst_orig cget -ncols] + 2]
  set yregion [expr [rgbaImage_dst_orig cget -nrows] + 2]
  $morph(canvas,dst) configure -scrollregion [list -2 -2 $xregion $yregion ]

  set xregion [expr [rgbaImage_twn_orig cget -ncols] + 2]
  set yregion [expr [rgbaImage_twn_orig cget -nrows] + 2]
  $morph(canvas,twn) configure -scrollregion [list -2 -2 $xregion $yregion ]

  meshDrawAll
}



proc meshPointDraw { this } {
  global morph

  verbose "meshPointDraw this= $this"

  # Draw a circle to indicate which mesh point was picked:
  # Find the window location of the mesh point
  set xp [$morph(mesh,$this) pointGet \
	  $morph(mesh,picked_index,x) $morph(mesh,picked_index,y) 0]

  set yp [$morph(mesh,$this) pointGet \
	  $morph(mesh,picked_index,x) $morph(mesh,picked_index,y) 1]

  # Compute the size of the circle:
  set xu [expr $xp - 4]
  set xl [expr $xp + 4]
  set yu [expr $yp - 4]
  set yl [expr $yp + 4]

  # Draw the circle
  $morph(canvas,$this) create oval $xu $yu $xl $yl -fill white -tag mesh_$this
}




##
## Mouse event handlers
##




proc buttonPress { x y W } {
  #
  # ARGUMENTS
  #   x: mouse pointer x location in screen units
  #   y: mouse pointer y location in screen units
  #   W: widget which was active when the event occured

  global morph
  global MP_PICK_DIST

  verbose "buttonPress: x= $x y= $y W= $W"
  verbose "buttonPress: mode is $morph(mode)"
  verbose "buttonPress: morph(mesh,selected) is $morph(mesh,selected)"
  verbose "buttonPress: morph(mesh,other)    is $morph(mesh,other)"

  # Map mouse location from screen coordinates to canvas coordinates.
  # This takes care of discrepencies caused by a scrollbar,
  # a scaling, and that sort of thing.
  set cx [$W canvasx $x]
  set cy [$W canvasy $y]

  verbose "buttonPress: Canvas coordinates $cx $cy"

  switch $morph(mode) {

    drag {
      ## Pick a mesh point
      set morph(mesh,picked_index,x) [$morph(mesh,selected) pick \
                                      $cx $cy 0 $MP_PICK_DIST]

      set morph(mesh,picked_index,y) [$morph(mesh,selected) pick \
                                      $cx $cy 1 $MP_PICK_DIST]

      ## If a mesh point was close enough to the pointer...
      if { $morph(mesh,picked_index,x) != "-1" } {

        verbose "buttonPress: Picked mesh point $morph(mesh,picked_index,x) $morph(mesh,picked_index,y)"

        # Store the starting location of the pointer
        set morph(mouse_start,x) $cx
        set morph(mouse_start,y) $cy

        # Draw a circle to indicate which mesh point was picked:
	meshPointDraw selected
	meshPointDraw other
      }
    }

    add_horiz {
      verbose "add_horiz $morph(mesh,selected) $morph(mesh,other) $cx $cy h a"
      $morph(mesh,selected) lineModify [$morph(mesh,other) cget -this] $cx $cy h a
    }

    add_vert {
      verbose "add_vert $morph(mesh,selected) $morph(mesh,other) $cx $cy v a"
      $morph(mesh,selected) lineModify [$morph(mesh,other) cget -this] $cx $cy v a
    }

    delete_horiz {
      verbose "delete_horiz $morph(mesh,selected) $morph(mesh,other) $cx $cy h d"
      $morph(mesh,selected) lineModify [$morph(mesh,other) cget -this] $cx $cy h d
    }

    delete_vert {
      verbose "delete_vert $morph(mesh,selected) $morph(mesh,other) $cx $cy v d"
      $morph(mesh,selected) lineModify [$morph(mesh,other) cget -this] $cx $cy v d
    }

    default {
      puts "buttonPress: invalid mode: $morph(mode)"
    }
  }
}




proc buttonRelease { x y W } {
  global morph

  verbose "buttonRelease: x= $x y= $y W= $W "
  verbose "buttonRelease: mode is $morph(mode)"

  # Map from screen coordinates to canvas coordinates.
  # This takes care of discrepencies caused by a scrollbar,
  # a scaling, and that sort of thing.
  set cx [$W canvasx $x]
  set cy [$W canvasy $y]

  verbose "buttonRelease: Canvas coordinates $cx $cy"

  switch $morph(mode) {
    drag {
      if {    ($morph(mesh,picked_index,x) != "")
           && ($morph(mesh,picked_index,x) >= 0) } {
        set dx [expr $cx - $morph(mouse_start,x)]
        set dy [expr $cy - $morph(mouse_start,y)]

        set xp [$morph(mesh,selected) pointGet \
                $morph(mesh,picked_index,x) $morph(mesh,picked_index,y) 0]

        set yp [$morph(mesh,selected) pointGet \
                $morph(mesh,picked_index,x) $morph(mesh,picked_index,y) 1]

        set newx [expr $xp + $dx]
        set newy [expr $yp + $dy]

        if {$newx >= [rgbaImage_src_orig cget -ncols]} {
          set newx [expr [rgbaImage_src_orig cget -ncols] - 1]
        }
        if {$newy >= [rgbaImage_src_orig cget -nrows ]} {
          set newy [expr [rgbaImage_src_orig cget -nrows ] - 1]
        }

        verbose "buttonRelease: mesh index $morph(mesh,picked_index,x) $morph(mesh,picked_index,y)"
        $morph(mesh,selected) set $morph(mesh,picked_index,x) $morph(mesh,picked_index,y) \
                                   $newx $newy
        set morph(mesh,picked_index,x) ""
        set morph(mesh,picked_index,y) ""
        set morph(mouse_start,x) ""
        set morph(mouse_start,y) ""

        # Delete the image of the highlighted mesh point
        $morph(canvas,selected) delete mesh_selected
        $morph(canvas,other) delete mesh_other

        # Update the locations of the tween mesh
        meshTweenInterpolate
      }
    }

    default {
    }
  }

  # Draw the new mesh shape
  meshDrawAll
}




proc canvasEnter { this widget } {
  # NAME
  #   canvasEnter: Action for entering a canvas
  #
  #
  # ARGUMENTS
  #   this: either src or dst depending on which canvas was entered.
  #
  #
  # DESCRIPTION
  #   Highlights the entered canvas.
  #   Selects the mesh appropriate for that canvas.
  #   Sets up reference to "other" mesh.

  global morph

  verbose "canvasEnter: this= $this widget= $widget "

  $morph(canvas,$this) configure -bg white

  set morph(mesh,selected) mesh_$this
  set morph(canvas,selected) $morph(canvas,$this)

  if { $this == "src" } {
    set morph(mesh,other) mesh_dst
    set morph(canvas,other) $morph(canvas,dst)
  } elseif {$this == "dst" } {
    set morph(mesh,other) mesh_src
    set morph(canvas,other) $morph(canvas,src)
  } else {
    puts "canvasEnter: ERROR: invalid value for this: $this"
  }
}




# -----------------
#    Bind events
# -----------------


proc bindMouseEvents {} {
  global morph

  bind $morph(canvas,src) <Enter> {canvasEnter src %W}
  bind $morph(canvas,dst) <Enter> {canvasEnter dst %W}


  bind Canvas <Leave> {%W configure -bg gray}


  $morph(canvas,src) bind all <ButtonPress> {buttonPress %x %y %W}
  $morph(canvas,dst) bind all <ButtonPress> {buttonPress %x %y %W}


  # $morph(canvas) bind all <B1-Motion> {buttonMotion %x %y %W $morph(canvas)}


  $morph(canvas,src) bind all <ButtonRelease> {buttonRelease %x %y %W}
  $morph(canvas,dst) bind all <ButtonRelease> {buttonRelease %x %y %W}
}