]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/gestures.py
6 #drpython@bluebottle.com
8 #Released under the terms of the wxWindows License.
10 #This is a class to add Mouse Gestures to a program.
11 #It can be used in two ways:
14 # Automatically runs mouse gestures.
15 # You need to set the gestures, and their associated actions,
16 # as well as the Mouse Button/Modifiers to use.
18 # (Mouse Buttons are set in init)
21 # Same as above, but you do not need to set the mouse button/modifiers.
22 # You can launch this from events as you wish.
24 #An example is provided in the demo.
25 #The parent window is where the mouse events will be recorded.
26 #(So if you want to record them in a pop up window, use manual mode,
27 #and set the pop up as the parent).
29 #Start() starts recording mouse movement.
30 #End() stops the recording, compiles all the gestures into a list,
31 #and looks through the registered gestures to find a match.
32 #The first matchs associated action is then run.
34 #The marginoferror is how much to forgive when calculating movement:
35 #If the margin is 25, then movement less than 25 pixels will not be detected.
37 #Recognized: L, R, U, D, 1, 3, 7, 9
39 #Styles: Manual (Automatic By Default), DisplayNumbersForDiagonals (Off By Default).
42 #The criteria for a direction is as follows:
43 #x in a row. (Where x is the WobbleTolerance).
44 #So if the WobbleTolerance is 9
45 # 'URUUUUUUUUUUUUUUURUURUUUU1' is Up.
47 #The higher this number, the less sensitive this class is.
48 #So the more likely something like 1L will translate to 1.
50 #This is good, since the mouse does tend to wobble somewhat,
51 #and a higher number allows for this.
53 #To change this, use SetWobbleTolerance
55 #Also, to help with recognition of a diagonal versus
56 #a vey messy straight line, if the greater absolute value
57 #is not greater than twice the lesser, only the grater value
62 #Add in modifier code (Ctrl, Alt, etc).
64 #SetGestureLine(wx.Colour(), int width)
66 #Add "Ends With": AddGestureEndsWith(self, gesture, action, args)
67 #Add "Starts With": AddGestuteStartsWith(self, gesture, action, args)
69 #At the moment, the mouse button can only be set at startup.
70 #I could use UnBind, but this may limit the wxPython version being used,
71 #and, what if the user has other events bound?
72 #So I think, if the user wants to change the mouse button at runtime,
73 #the best solution is to use manual mode.
79 def __init__(self
, parent
, Auto
=True, MouseButton
=wx
.MOUSE_BTN_MIDDLE
):
84 self
.actionarguments
= []
86 self
.mousebutton
= MouseButton
89 self
.recording
= False
91 self
.lastposition
= (-1, -1)
93 self
.pen
= wx
.Pen(wx
.Colour(0, 144, 255), 5)
95 self
.dc
= wx
.ScreenDC()
96 self
.dc
.SetPen(self
.pen
)
98 self
.showgesture
= False
100 self
.wobbletolerance
= 7
106 def Action(self
, gesture
):
107 if gesture
in self
.gestures
:
108 i
= self
.gestures
.index(gesture
)
109 apply(self
.actions
[i
], self
.actionarguments
[i
])
111 def AddGesture(self
, gesture
, action
, *args
):
112 #Make Sure not a duplicate:
113 self
.RemoveGesture(gesture
)
115 self
.gestures
.append(gesture
)
116 self
.actions
.append(action
)
117 self
.actionarguments
.append(args
)
120 self
.recording
= False
122 #Figure out the gestures (Look for occurances of 5 in a row or more):
129 for g
in self
.rawgesture
:
131 if g
!= tempstring
[l
- 1]:
132 if g
== possiblechange
:
138 if len(tempstring
) >= self
.wobbletolerance
:
141 if directions
[ld
- 1] != g
:
149 def GetDirection(self
, point1
, point2
):
150 #point1 is the old point
156 #(Negative = Left, Up)
157 #(Positive = Right, Down)
162 horizontalchange
= abs(horizontal
) > 0
163 verticalchange
= abs(vertical
) > 0
165 if horizontalchange
and verticalchange
:
171 verticalchange
= False
175 horizontalchange
= False
177 if horizontalchange
and verticalchange
:
179 if (horizontal
> 0) and (vertical
> 0):
181 elif (horizontal
> 0) and (vertical
< 0):
183 elif (horizontal
< 0) and (vertical
> 0):
201 def OnEnd(self
, event
):
208 def OnMotion(self
, event
):
210 currentposition
= event
.GetPosition()
211 if self
.lastposition
!= (-1, -1):
212 self
.rawgesture
+= self
.GetDirection(self
.lastposition
, currentposition
)
215 px1
, py1
= self
.parent
.ClientToScreen(self
.lastposition
)
216 px2
, py2
= self
.parent
.ClientToScreen(currentposition
)
218 self
.dc
.DrawLine(px1
, py1
, px2
, py2
)
220 self
.lastposition
= currentposition
224 def OnStart(self
, event
):
228 def RemoveGesture(self
, gesture
):
229 if gesture
in self
.gestures
:
230 i
= self
.gestures
.index(gesture
)
233 del self
.actionarguments
[i
]
235 def SetAuto(self
, auto
):
236 #I was not sure about making this part of init, so I left it as its own method for now.
238 if self
.mousebutton
== wx
.MOUSE_BTN_LEFT
:
239 self
.parent
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnStart
)
240 self
.parent
.Bind(wx
.EVT_LEFT_UP
, self
.OnEnd
)
241 elif self
.mousebutton
== wx
.MOUSE_BTN_MIDDLE
:
242 self
.parent
.Bind(wx
.EVT_MIDDLE_DOWN
, self
.OnStart
)
243 self
.parent
.Bind(wx
.EVT_MIDDLE_UP
, self
.OnEnd
)
244 elif self
.mousebutton
== wx
.MOUSE_BTN_RIGHT
:
245 self
.parent
.Bind(wx
.EVT_RIGHT_DOWN
, self
.OnStart
)
246 self
.parent
.Bind(wx
.EVT_RIGHT_UP
, self
.OnEnd
)
247 self
.parent
.Bind(wx
.EVT_MOTION
, self
.OnMotion
)
249 def SetGesturesVisible(self
, vis
):
250 self
.showgesture
= vis
252 def SetModifiers(self
, modifers
):
253 self
.modifiers
= modifiers
255 def SetWobbleTolerance(self
, wobbletolerance
):
256 self
.WobbleTolerance
= wobbletolerance
259 self
.recording
= True
261 self
.lastposition
= (-1, -1)
263 self
.parent
.Refresh()