source: trunk/src/testing/dim/hwcapture/localPointer.py @ 4

Revision 4, 4.9 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
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
41import sys, wx
42#sys.path.append("../dim/hwcapture")
43from managerConn import ManagerConnection
44from threading import Timer
45import time
46
47#if len(sys.argv) > 2:
48SHOW_WINDOW = True
49#else:
50#    SHOW_WINDOW = False
51
52MOVE = 1
53CLICK = 2
54WHEEL = 3
55
56LEFT=1
57RIGHT=2
58MIDDLE = 3 
59
60global lastX, lastY
61lastX = lastY = 0
62global frame, manager, panel, buttons, w, h, label
63buttons = {}  #key=BTN_ID, value=True|False
64
65
66def sendMsg(*data):
67    msg = ""
68    for m in data:
69        msg+=str(m)+" "
70    manager.sendMessage("", "mouse", msg)
71
72
73def onKey(event):
74    if event.GetKeyCode() == wx.WXK_ESCAPE:
75        if panel.HasCapture():
76            panel.ReleaseMouse()
77        frame.Close()
78    elif event.GetKeyCode() == wx.WXK_SPACE:
79        global label
80        if panel.HasCapture():
81            label.SetLabel("Press SPACE to capture mouse")
82            panel.ReleaseMouse()
83        else:
84            label.SetLabel("Press SPACE to release mouse")
85            panel.CaptureMouse()
86
87
88def onMove(event):
89    global lastX, lastY
90    ms = wx.GetMouseState()
91    mx = ms.GetX()
92    my = ms.GetY()
93    x = float(mx/float(w))
94    y = 1-float(my/float(h))
95    if lastX != x or lastY != y:
96        sendMsg(MOVE, x,y)
97        lastX = x
98        lastY = y
99
100
101def onClick(event):
102    isDown=False
103    if event.ButtonDown():  isDown=True
104
105    if event.GetButton() == wx.MOUSE_BTN_LEFT:
106        btnId = LEFT
107    elif event.GetButton() == wx.MOUSE_BTN_RIGHT:
108        btnId = RIGHT
109    else:
110        btnId = MIDDLE
111
112    sendMsg(CLICK, btnId, int(isDown))
113
114
115def onWheel(event):
116    numSteps = event.GetWheelRotation()/event.GetWheelDelta()
117    sendMsg(WHEEL, numSteps)
118           
119
120def onClose(event):
121    manager.quit()
122    frame.Destroy()
123    wx.GetApp().ExitMainLoop()
124
125
126def doCapture():
127    wx.CallAfter(panel.CaptureMouse)
128   
129
130# runs in a thread, captures mouse position and sends to dim regularly
131def captureMouse(mgr):
132    app = wx.App()
133   
134    global manager,w,h
135    manager = mgr
136
137    (w,h) = wx.DisplaySize()
138
139    if SHOW_WINDOW:
140        global frame, panel, label
141        frame = wx.Frame(None, size=(250,80), pos=(100,100))
142        frame.Bind(wx.EVT_CLOSE, onClose)
143       
144        panel = wx.Panel(frame, size=(250,80), pos=(0,0))
145        panel.Bind(wx.EVT_KEY_DOWN, onKey)
146        panel.Bind(wx.EVT_LEFT_DOWN, onClick)
147        panel.Bind(wx.EVT_LEFT_UP, onClick)
148        panel.Bind(wx.EVT_RIGHT_DOWN, onClick)
149        panel.Bind(wx.EVT_RIGHT_UP, onClick)
150        panel.Bind(wx.EVT_MOTION, onMove)
151        panel.Bind(wx.EVT_MOUSEWHEEL, onWheel)
152
153        #label = wx.StaticText(panel, wx.ID_ANY, "Press SPACE to release mouse")
154        label = wx.StaticText(panel, wx.ID_ANY, "Press SPACE to capture mouse")
155        panel.Fit()
156        panel.SetFocus()
157
158        #t = Timer(2.0, doCapture)
159        #t.start()
160             
161        frame.Show()
162        app.MainLoop()
163#    else:
164#        while True:
165#            onIdle()
166   
167if len(sys.argv) < 2:
168    print "USAGE: python localPointer.py SAGE_IP"
169else:
170    print "Connecting to: ", sys.argv[1]
171    captureMouse(ManagerConnection(sys.argv[1], 20005))
Note: See TracBrowser for help on using the repository browser.