Fri, 03 Jun 2016 21:14:09 +0200
Bugfixing, Added M400 magic
15 | 1 | # This file is part of the Printrun suite. |
2 | # | |
3 | # Printrun is free software: you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
5 | # the Free Software Foundation, either version 3 of the License, or | |
6 | # (at your option) any later version. | |
7 | # | |
8 | # Printrun is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
12 | # | |
13 | # You should have received a copy of the GNU General Public License | |
14 | # along with Printrun. If not, see <http://www.gnu.org/licenses/>. | |
15 | ||
16 | import wx | |
17 | from .bufferedcanvas import BufferedCanvas | |
18 | from printrun.utils import imagefile | |
19 | ||
20 | def sign(n): | |
21 | if n < 0: return -1 | |
22 | elif n > 0: return 1 | |
23 | else: return 0 | |
24 | ||
25 | class ZButtons(BufferedCanvas): | |
26 | button_ydistances = [7, 30, 55, 83] # ,112 | |
27 | move_values = [0.1, 1, 10] | |
28 | center = (30, 118) | |
29 | label_overlay_positions = { | |
30 | 0: (1.1, 18, 9), | |
31 | 1: (1.1, 41.5, 10.6), | |
32 | 2: (1.1, 68, 13), | |
33 | } | |
34 | imagename = "control_z.png" | |
35 | ||
36 | def __init__(self, parent, moveCallback = None, bgcolor = "#FFFFFF", ID=-1): | |
37 | self.bg_bmp = wx.Image(imagefile(self.imagename), wx.BITMAP_TYPE_PNG).ConvertToBitmap() | |
38 | self.range = None | |
39 | self.direction = None | |
40 | self.orderOfMagnitudeIdx = 0 # 0 means '1', 1 means '10', 2 means '100', etc. | |
41 | self.moveCallback = moveCallback | |
42 | self.enabled = False | |
43 | # Remember the last clicked value, so we can repeat when spacebar pressed | |
44 | self.lastValue = None | |
45 | ||
46 | self.bgcolor = wx.Colour() | |
47 | self.bgcolor.SetFromName(bgcolor) | |
48 | self.bgcolormask = wx.Colour(self.bgcolor.Red(), self.bgcolor.Green(), self.bgcolor.Blue(), 128) | |
49 | ||
50 | BufferedCanvas.__init__(self, parent, ID, size=self.bg_bmp.GetSize()) | |
51 | ||
52 | # Set up mouse and keyboard event capture | |
53 | self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) | |
54 | self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDown) | |
55 | self.Bind(wx.EVT_MOTION, self.OnMotion) | |
56 | self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow) | |
57 | ||
58 | def disable(self): | |
59 | self.enabled = False | |
60 | self.update() | |
61 | ||
62 | def enable(self): | |
63 | self.enabled = True | |
64 | self.update() | |
65 | ||
66 | def repeatLast(self): | |
67 | if self.lastValue: | |
68 | self.moveCallback(self.lastValue) | |
69 | ||
70 | def clearRepeat(self): | |
71 | self.lastValue = None | |
72 | ||
73 | def lookupRange(self, ydist): | |
74 | idx = -1 | |
75 | for d in self.button_ydistances: | |
76 | if ydist < d: | |
77 | return idx | |
78 | idx += 1 | |
79 | return None | |
80 | ||
81 | def highlight(self, gc, rng, dir): | |
82 | assert(rng >= -1 and rng <= 3) | |
83 | assert(dir >= -1 and dir <= 1) | |
84 | ||
85 | fudge = 11 | |
86 | x = 0 + fudge | |
87 | w = 59 - fudge * 2 | |
88 | if rng >= 0: | |
89 | k = 1 if dir > 0 else 0 | |
90 | y = self.center[1] - (dir * self.button_ydistances[rng + k]) | |
91 | h = self.button_ydistances[rng + 1] - self.button_ydistances[rng] | |
92 | gc.DrawRoundedRectangle(x, y, w, h, 4) | |
93 | # gc.DrawRectangle(x, y, w, h) | |
94 | # self.drawPartialPie(dc, center, r1-inner_ring_radius, r2-inner_ring_radius, a1+fudge, a2-fudge) | |
95 | ||
96 | def getRangeDir(self, pos): | |
97 | ydelta = self.center[1] - pos[1] | |
98 | return (self.lookupRange(abs(ydelta)), sign(ydelta)) | |
99 | ||
100 | def draw(self, dc, w, h): | |
101 | dc.SetBackground(wx.Brush(self.bgcolor)) | |
102 | dc.Clear() | |
103 | gc = wx.GraphicsContext.Create(dc) | |
104 | if self.bg_bmp: | |
105 | w, h = (self.bg_bmp.GetWidth(), self.bg_bmp.GetHeight()) | |
106 | gc.DrawBitmap(self.bg_bmp, 0, 0, w, h) | |
107 | ||
108 | if self.enabled and self.IsEnabled(): | |
109 | # Draw label overlays | |
110 | gc.SetPen(wx.Pen(wx.Colour(255, 255, 255, 128), 1)) | |
111 | gc.SetBrush(wx.Brush(wx.Colour(255, 255, 255, 128 + 64))) | |
112 | for idx, kpos in self.label_overlay_positions.items(): | |
113 | if idx != self.range: | |
114 | r = kpos[2] | |
115 | gc.DrawEllipse(self.center[0] - kpos[0] - r, self.center[1] - kpos[1] - r, r * 2, r * 2) | |
116 | ||
117 | # Top 'layer' is the mouse-over highlights | |
118 | gc.SetPen(wx.Pen(wx.Colour(100, 100, 100, 172), 4)) | |
119 | gc.SetBrush(wx.Brush(wx.Colour(0, 0, 0, 128))) | |
120 | if self.range is not None and self.direction is not None: | |
121 | self.highlight(gc, self.range, self.direction) | |
122 | else: | |
123 | gc.SetPen(wx.Pen(self.bgcolor, 0)) | |
124 | gc.SetBrush(wx.Brush(self.bgcolormask)) | |
125 | gc.DrawRectangle(0, 0, w, h) | |
126 | ||
127 | # ------ # | |
128 | # Events # | |
129 | # ------ # | |
130 | ||
131 | def OnMotion(self, event): | |
132 | if not self.enabled: | |
133 | return | |
134 | ||
135 | oldr, oldd = self.range, self.direction | |
136 | ||
137 | mpos = event.GetPosition() | |
138 | self.range, self.direction = self.getRangeDir(mpos) | |
139 | ||
140 | if oldr != self.range or oldd != self.direction: | |
141 | self.update() | |
142 | ||
143 | def OnLeftDown(self, event): | |
144 | if not self.enabled: | |
145 | return | |
146 | ||
147 | mpos = event.GetPosition() | |
148 | r, d = self.getRangeDir(mpos) | |
149 | if r >= 0: | |
150 | value = d * self.move_values[r] | |
151 | if self.moveCallback: | |
152 | self.lastValue = value | |
153 | self.moveCallback(value) | |
154 | ||
155 | def OnLeaveWindow(self, evt): | |
156 | self.range = None | |
157 | self.direction = None | |
158 | self.update() | |
159 | ||
160 | class ZButtonsMini(ZButtons): | |
161 | button_ydistances = [7, 30, 55] | |
162 | center = (30, 84) | |
163 | label_overlay_positions = { | |
164 | 0: (1, 18, 9), | |
165 | 1: (1, 42.8, 12.9), | |
166 | } | |
167 | imagename = "control_z_mini.png" | |
168 | move_values = [0.1, 10] |