1 | ############################################################################ |
---|
2 | # |
---|
3 | # DIM - A Direct Interaction Manager for SAGE |
---|
4 | # Copyright (C) 2007 Electronic Visualization Laboratory, |
---|
5 | # University of Illinois at Chicago |
---|
6 | # |
---|
7 | # All rights reserved. |
---|
8 | # |
---|
9 | # Redistribution and use in source and binary forms, with or without |
---|
10 | # modification, are permitted provided that the following conditions are met: |
---|
11 | # |
---|
12 | # * Redistributions of source code must retain the above copyright |
---|
13 | # notice, this list of conditions and the following disclaimer. |
---|
14 | # * Redistributions in binary form must reproduce the above |
---|
15 | # copyright notice, this list of conditions and the following disclaimer |
---|
16 | # in the documentation and/or other materials provided with the distribution. |
---|
17 | # * Neither the name of the University of Illinois at Chicago nor |
---|
18 | # the names of its contributors may be used to endorse or promote |
---|
19 | # products derived from this software without specific prior written permission. |
---|
20 | # |
---|
21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
25 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
26 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
27 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
28 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
29 | # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
30 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
31 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | # |
---|
33 | # Direct questions, comments etc about SAGE UI to www.evl.uic.edu/cavern/forum |
---|
34 | # |
---|
35 | # Author: Ratko Jagodic |
---|
36 | # |
---|
37 | ############################################################################ |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | import device |
---|
43 | from globals import * |
---|
44 | import time |
---|
45 | from threading import Thread |
---|
46 | |
---|
47 | |
---|
48 | def makeNew(deviceId): |
---|
49 | """ this is how we instantiate the device object from |
---|
50 | the main program that loads this plugin """ |
---|
51 | return Mouse(deviceId) |
---|
52 | |
---|
53 | |
---|
54 | MOVE = 1 |
---|
55 | CLICK = 2 |
---|
56 | WHEEL = 3 |
---|
57 | COLOR = 4 |
---|
58 | |
---|
59 | LEFT=1 |
---|
60 | RIGHT=2 |
---|
61 | MIDDLE=3 |
---|
62 | |
---|
63 | |
---|
64 | WHEEL_CLICK_TIME = 0.75 # seconds before there is a click event after wheel event |
---|
65 | |
---|
66 | |
---|
67 | class Mouse(device.Device): |
---|
68 | |
---|
69 | def __init__(self, deviceId): |
---|
70 | device.Device.__init__(self, "mouse", deviceId, needPointer=True) |
---|
71 | |
---|
72 | # current state of the device |
---|
73 | self.x = 0 # position in SAGE coords |
---|
74 | self.y = 0 # position in SAGE coords |
---|
75 | self.clickX = 0 # where the click happened |
---|
76 | self.clickY = 0 # where the click happened |
---|
77 | |
---|
78 | self.buttons = {} #key=BTN_ID, value=True|False |
---|
79 | self.buttons[0] = False |
---|
80 | self.buttons[LEFT] = False |
---|
81 | self.buttons[RIGHT] = False |
---|
82 | self.buttons[MIDDLE] = False |
---|
83 | self.lastBtn = 0 |
---|
84 | |
---|
85 | ## self.lastWheelTime = time.time() |
---|
86 | ## self.wheelClickThread = Thread(target=self.postWheelUpClick) |
---|
87 | ## self.wheelClickThread.start() |
---|
88 | ## self.wheeling = False |
---|
89 | |
---|
90 | |
---|
91 | ## def postWheelUpClick(self): |
---|
92 | ## while doRun(): |
---|
93 | ## if (time.time() - self.lastWheelTime) > WHEEL_CLICK_TIME and self.wheeling: |
---|
94 | ## forEvt = EVT_ZOOM |
---|
95 | ## self.postEvtClick(self.x, self.y, 4, False, forEvt) |
---|
96 | ## self.wheeling = False |
---|
97 | |
---|
98 | ## time.sleep(0.1) |
---|
99 | |
---|
100 | |
---|
101 | def onMessage(self, data, firstMsg=False): |
---|
102 | tokens = data.strip().split() |
---|
103 | msgCode = int(tokens[0]) |
---|
104 | |
---|
105 | |
---|
106 | # click |
---|
107 | if msgCode == CLICK: |
---|
108 | btnId = int(tokens[1]) |
---|
109 | isDown = bool(int(tokens[2])) |
---|
110 | forEvt = 0 |
---|
111 | |
---|
112 | if self.buttons[btnId] != isDown: |
---|
113 | self.buttons[btnId] = isDown |
---|
114 | self.lastBtn = btnId |
---|
115 | |
---|
116 | if isDown: |
---|
117 | self.clickX, self.clickY = self.x, self.y |
---|
118 | |
---|
119 | if btnId == LEFT: |
---|
120 | forEvt = EVT_PAN |
---|
121 | elif btnId == RIGHT: |
---|
122 | forEvt = EVT_ROTATE |
---|
123 | elif btnId == MIDDLE: |
---|
124 | forEvt = EVT_ZOOM |
---|
125 | |
---|
126 | self.postEvtClick(self.x, self.y, btnId, isDown, forEvt) |
---|
127 | |
---|
128 | |
---|
129 | elif msgCode == WHEEL: |
---|
130 | pass |
---|
131 | ## forEvt = EVT_ZOOM |
---|
132 | ## numSteps = -1*int(tokens[1]) |
---|
133 | |
---|
134 | ## if (time.time() - self.lastWheelTime) > WHEEL_CLICK_TIME: |
---|
135 | ## self.wheeling = True |
---|
136 | ## self.postEvtClick(self.x, self.y, 4, True, forEvt) |
---|
137 | |
---|
138 | ## #self.postEvtAnalog3(self.clickX, self.clickY, self.x, self.y, numSteps*15, 0, 0) |
---|
139 | ## self.postEvtAnalog3(self.clickX, self.clickY, numSteps*15, 0, 0) |
---|
140 | ## self.lastWheelTime = time.time() |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | elif msgCode == MOVE: |
---|
145 | x = float(tokens[1].strip()) |
---|
146 | y = float(tokens[2].strip()) |
---|
147 | |
---|
148 | # always convert to SAGE coords first |
---|
149 | x = self.bounds.getX() + int(round(float(x) * self.bounds.getWidth())) |
---|
150 | y = self.bounds.getY() + int(round(float(y) * self.bounds.getHeight())) |
---|
151 | |
---|
152 | x,y = self.smooth(x,y) |
---|
153 | |
---|
154 | |
---|
155 | dx = dy = 0 |
---|
156 | if self.x != x or self.y != y: |
---|
157 | dx = x - self.x ; self.x = x |
---|
158 | dy = y - self.y ; self.y = y |
---|
159 | |
---|
160 | if self.buttons[self.lastBtn]: |
---|
161 | if self.lastBtn == LEFT: |
---|
162 | self.postEvtAnalog1(self.clickX, self.clickY, dx, dy, 0) |
---|
163 | elif self.lastBtn == RIGHT: |
---|
164 | self.postEvtAnalog2(self.clickX, self.clickY, dx, dy, 0) |
---|
165 | elif self.lastBtn == MIDDLE: |
---|
166 | self.postEvtAnalog3(self.clickX, self.clickY, dx, dy, 0) |
---|
167 | else: |
---|
168 | self.postEvtMove(self.x, self.y, dx, dy) |
---|
169 | |
---|
170 | # move the pointer |
---|
171 | if self.pointer: self.pointer.movePointer(self.x, self.y) |
---|
172 | |
---|
173 | |
---|
174 | elif msgCode == COLOR: |
---|
175 | #if self.pointer: self.pointer.setColor(tokens[1], tokens[2], tokens[3]) |
---|
176 | pass |
---|