-
Notifications
You must be signed in to change notification settings - Fork 21
/
Overlaytest.py
executable file
·176 lines (135 loc) · 5.32 KB
/
Overlaytest.py
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
#!/usr/bin/env python
"""
A test of temporary drawing using a method called from OnPaint
NOTE: this is broken -- needs debugging!
"""
import wx
import random
class BufferedWindow(wx.Window):
"""
A Buffered window class.
To use it, subclass it and define a Draw(DC) method that takes a DC
to draw to. In that method, put the code needed to draw the picture
you want. The window will automatically be double buffered, and the
screen will be automatically updated when a Paint event is received.
When the drawing needs to change, you app needs to call the
UpdateDrawing() method. Since the drawing is stored in a bitmap, you
can also save the drawing to file by calling the
SaveToFile(self,file_name,file_type) method.
"""
def __init__(self,
parent,
id,
pos=wx.DefaultPosition,
size=wx.DefaultSize,
style=wx.NO_FULL_REPAINT_ON_RESIZE):
wx.Window.__init__(self, parent, id, pos, size, style)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
# OnSize called to make sure the buffer is initialized.
# This might result in OnSize getting called twice on some
# platforms at initialization, but little harm done.
self.OnSize(None)
def Draw(self, dc):
# just here as a place holder.
# This method should be over-ridden when subclassed
pass
def TempDraw(self, dc):
"""
This is where youput stuff that you want draw temporarily on top
of the buffer, like during Mouse actions, etc.
"""
pass
def OnPaint(self, event):
print("In OnPaint")
# All that is needed here is to draw the buffer to screen
dc = wx.PaintDC(self)
dc.DrawBitmap(self._Buffer, 0, 0)
self.TempDraw(dc)
def OnSize(self, event):
# The Buffer init is done here, to make sure the buffer is always
# the same size as the Window
Size = self.GetClientSize()
# Make sure we don't try to create a 0 size bitmap
Size = (max(Size[0], 1), max(Size[1], 1))
self._Buffer = wx.Bitmap(Size[0], Size[1])
self.UpdateDrawing()
def SaveToFile(self, FileName, FileType):
# This will save the contents of the buffer
# to the specified file. See the wxWindows docs for
# wx.Bitmap::SaveFile for the details
self._Buffer.SaveFile(FileName, FileType)
def UpdateDrawing(self):
"""
This would get called if the drawing needed to change, for whatever reason.
The idea here is that the drawing is based on some data generated
elsewhere in the system. IF that data changes, the drawing needs to
be updated.
"""
# update the buffer
dc = wx.MemoryDC()
dc.SelectObject(self._Buffer)
self.Draw(dc)
# update the screen
wx.ClientDC(self).DrawBitmap(self._Buffer, 0, 0)
class DrawWindow(BufferedWindow):
def __init__(self, parent, id=-1):
# Any data the Draw() function needs must be initialized before
# calling BufferedWindow.__init__, as it will call the Draw
# function.
BufferedWindow.__init__(self, parent, id)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.Bind(wx.EVT_MOTION, self.OnMove)
self.StartMove = None
self.overlay = wx.Overlay()
def OnLeftDown(self, event):
self.CaptureMouse()
self.StartMove = event.GetPosition()
def OnLeftUp(self, event):
if self.HasCapture():
self.ReleaseMouse()
self.StartMove = None
# self.Refresh()
# # When the mouse is released we reset the overlay and it
# # restores the former content to the window.
# dc = wx.ClientDC(self)
# odc = wx.DCOverlay(self.overlay, dc)
# odc.Clear()
# del odc
self.overlay.Reset()
def OnMove(self, event):
if (event.Dragging() and event.LeftIsDown()
and self.StartMove is not None):
pos = event.GetPosition()
# self.Refresh()
dc = wx.ClientDC(self)
odc = wx.DCOverlay(self.overlay, dc)
odc.Clear()
# a black and white line so you can see it over any color.
dc.SetPen(wx.Pen('WHITE', 2))
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.DrawLine(self.StartMove, pos)
dc.SetPen(wx.Pen('BLACK', 2, wx.SHORT_DASH))
dc.DrawLine(self.StartMove, pos)
del odc # to ensure it gets delted before the Client
def Draw(self, dc):
coords = ((40, 40), (200, 220), (210, 120), (120, 300))
dc.SetBackground(wx.Brush("Blue"))
dc.Clear() # make sure you clear the bitmap!
dc.SetPen(wx.RED_PEN)
dc.SetBrush(wx.CYAN_BRUSH)
dc.DrawPolygon(coords)
class TestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.Window = DrawWindow(self)
class DemoApp(wx.App):
def OnInit(self):
frame = TestFrame(None, title="OverlayTest", size=(500, 500))
frame.Show(True)
self.SetTopWindow(frame)
return True
if __name__ == "__main__":
app = DemoApp(0)
app.MainLoop()