chiark / gitweb /
Show SD card copy progress. Slightly reduce the memory usage of the GCode loading.
[cura.git] / Cura / gui / util / openglGui.py
1 from __future__ import absolute_import
2 from __future__ import division
3
4 import wx
5 import traceback
6 import sys
7 import os
8 import time
9
10 from wx import glcanvas
11 import OpenGL
12 OpenGL.ERROR_CHECKING = False
13 from OpenGL.GL import *
14
15 from Cura.gui.util import opengl
16
17 class animation(object):
18         def __init__(self, gui, start, end, runTime):
19                 self._start = start
20                 self._end = end
21                 self._startTime = time.time()
22                 self._runTime = runTime
23                 gui._animationList.append(self)
24
25         def isDone(self):
26                 return time.time() > self._startTime + self._runTime
27
28         def getPosition(self):
29                 if self.isDone():
30                         return self._end
31                 f = (time.time() - self._startTime) / self._runTime
32                 ts = f*f
33                 tc = f*f*f
34                 #f = 6*tc*ts + -15*ts*ts + 10*tc
35                 f = tc + -3*ts + 3*f
36                 return self._start + (self._end - self._start) * f
37
38 class glGuiControl(object):
39         def __init__(self, parent, pos):
40                 self._parent = parent
41                 self._base = parent._base
42                 self._pos = pos
43                 self._size = (0,0, 1, 1)
44                 self._parent.add(self)
45
46         def setSize(self, x, y, w, h):
47                 self._size = (x, y, w, h)
48
49         def getSize(self):
50                 return self._size
51
52         def getMinSize(self):
53                 return 1, 1
54
55         def updateLayout(self):
56                 pass
57
58         def focusNext(self):
59                 for n in xrange(self._parent._glGuiControlList.index(self) + 1, len(self._parent._glGuiControlList)):
60                         if self._parent._glGuiControlList[n].setFocus():
61                                 return
62                 for n in xrange(0, self._parent._glGuiControlList.index(self)):
63                         if self._parent._glGuiControlList[n].setFocus():
64                                 return
65
66         def focusPrevious(self):
67                 for n in xrange(self._parent._glGuiControlList.index(self) -1, -1, -1):
68                         if self._parent._glGuiControlList[n].setFocus():
69                                 return
70                 for n in xrange(len(self._parent._glGuiControlList) - 1, self._parent._glGuiControlList.index(self), -1):
71                         if self._parent._glGuiControlList[n].setFocus():
72                                 return
73
74         def setFocus(self):
75                 return False
76
77         def hasFocus(self):
78                 return self._base._focus == self
79
80         def OnMouseUp(self, x, y):
81                 pass
82
83         def OnKeyChar(self, key):
84                 pass
85
86 class glGuiContainer(glGuiControl):
87         def __init__(self, parent, pos):
88                 self._glGuiControlList = []
89                 glGuiLayoutButtons(self)
90                 super(glGuiContainer, self).__init__(parent, pos)
91
92         def add(self, ctrl):
93                 self._glGuiControlList.append(ctrl)
94                 self.updateLayout()
95
96         def OnMouseDown(self, x, y, button):
97                 for ctrl in self._glGuiControlList:
98                         if ctrl.OnMouseDown(x, y, button):
99                                 return True
100                 return False
101
102         def OnMouseUp(self, x, y):
103                 for ctrl in self._glGuiControlList:
104                         if ctrl.OnMouseUp(x, y):
105                                 return True
106                 return False
107
108         def OnMouseMotion(self, x, y):
109                 handled = False
110                 for ctrl in self._glGuiControlList:
111                         if ctrl.OnMouseMotion(x, y):
112                                 handled = True
113                 return handled
114
115         def draw(self):
116                 for ctrl in self._glGuiControlList:
117                         ctrl.draw()
118
119         def updateLayout(self):
120                 self._layout.update()
121                 for ctrl in self._glGuiControlList:
122                         ctrl.updateLayout()
123
124 class glGuiPanel(glcanvas.GLCanvas):
125         def __init__(self, parent):
126                 attribList = (glcanvas.WX_GL_RGBA, glcanvas.WX_GL_DOUBLEBUFFER, glcanvas.WX_GL_DEPTH_SIZE, 32, glcanvas.WX_GL_STENCIL_SIZE, 8)
127                 glcanvas.GLCanvas.__init__(self, parent, style=wx.WANTS_CHARS, attribList = attribList)
128                 self._base = self
129                 self._focus = None
130                 self._container = None
131                 self._container = glGuiContainer(self, (0,0))
132                 self._shownError = False
133
134                 self._context = glcanvas.GLContext(self)
135                 self._glButtonsTexture = None
136                 self._glRobotTexture = None
137                 self._buttonSize = 64
138
139                 self._animationList = []
140                 self.glReleaseList = []
141                 self._refreshQueued = False
142
143                 wx.EVT_PAINT(self, self._OnGuiPaint)
144                 wx.EVT_SIZE(self, self._OnSize)
145                 wx.EVT_ERASE_BACKGROUND(self, self._OnEraseBackground)
146                 wx.EVT_LEFT_DOWN(self, self._OnGuiMouseDown)
147                 wx.EVT_LEFT_DCLICK(self, self._OnGuiMouseDown)
148                 wx.EVT_LEFT_UP(self, self._OnGuiMouseUp)
149                 wx.EVT_RIGHT_DOWN(self, self._OnGuiMouseDown)
150                 wx.EVT_RIGHT_DCLICK(self, self._OnGuiMouseDown)
151                 wx.EVT_RIGHT_UP(self, self._OnGuiMouseUp)
152                 wx.EVT_MIDDLE_DOWN(self, self._OnGuiMouseDown)
153                 wx.EVT_MIDDLE_DCLICK(self, self._OnGuiMouseDown)
154                 wx.EVT_MIDDLE_UP(self, self._OnGuiMouseUp)
155                 wx.EVT_MOTION(self, self._OnGuiMouseMotion)
156                 wx.EVT_CHAR(self, self._OnGuiKeyChar)
157                 wx.EVT_KILL_FOCUS(self, self.OnFocusLost)
158                 wx.EVT_IDLE(self, self._OnIdle)
159
160         def _OnIdle(self, e):
161                 if len(self._animationList) > 0 or self._refreshQueued:
162                         self._refreshQueued = False
163                         for anim in self._animationList:
164                                 if anim.isDone():
165                                         self._animationList.remove(anim)
166                         self.Refresh()
167
168         def _OnGuiKeyChar(self, e):
169                 if self._focus is not None:
170                         self._focus.OnKeyChar(e.GetKeyCode())
171                         self.Refresh()
172                 else:
173                         self.OnKeyChar(e.GetKeyCode())
174
175         def OnFocusLost(self, e):
176                 self._focus = None
177                 self.Refresh()
178
179         def _OnGuiMouseDown(self,e):
180                 self.SetFocus()
181                 if self._container.OnMouseDown(e.GetX(), e.GetY(), e.GetButton()):
182                         self.Refresh()
183                         return
184                 self.OnMouseDown(e)
185
186         def _OnGuiMouseUp(self, e):
187                 if self._container.OnMouseUp(e.GetX(), e.GetY()):
188                         self.Refresh()
189                         return
190                 self.OnMouseUp(e)
191
192         def _OnGuiMouseMotion(self,e):
193                 self.Refresh()
194                 if not self._container.OnMouseMotion(e.GetX(), e.GetY()):
195                         self.OnMouseMotion(e)
196
197         def _OnGuiPaint(self, e):
198                 h = self.GetSize().GetHeight()
199                 w = self.GetSize().GetWidth()
200                 oldButtonSize = self._buttonSize
201                 if h / 3 < w / 4:
202                         w = h * 4 / 3
203                 if w < 64 * 8:
204                         self._buttonSize = 32
205                 elif w < 64 * 10:
206                         self._buttonSize = 48
207                 elif w < 64 * 15:
208                         self._buttonSize = 64
209                 elif w < 64 * 20:
210                         self._buttonSize = 80
211                 else:
212                         self._buttonSize = 96
213                 if self._buttonSize != oldButtonSize:
214                         self._container.updateLayout()
215
216                 dc = wx.PaintDC(self)
217                 try:
218                         self.SetCurrent(self._context)
219                         for obj in self.glReleaseList:
220                                 obj.release()
221                         del self.glReleaseList[:]
222                         self.OnPaint(e)
223                         self._drawGui()
224                         glFlush()
225                         self.SwapBuffers()
226                 except:
227                         errStr = 'An error has occurred during the 3D view drawing.'
228                         tb = traceback.extract_tb(sys.exc_info()[2])
229                         errStr += "\n%s: '%s'" % (str(sys.exc_info()[0].__name__), str(sys.exc_info()[1]))
230                         for n in xrange(len(tb)-1, -1, -1):
231                                 locationInfo = tb[n]
232                                 errStr += "\n @ %s:%s:%d" % (os.path.basename(locationInfo[0]), locationInfo[2], locationInfo[1])
233                         if not self._shownError:
234                                 wx.CallAfter(wx.MessageBox, errStr, '3D window error', wx.OK | wx.ICON_EXCLAMATION)
235                                 self._shownError = True
236
237         def _drawGui(self):
238                 if self._glButtonsTexture is None:
239                         self._glButtonsTexture = opengl.loadGLTexture('glButtons.png')
240                         self._glRobotTexture = opengl.loadGLTexture('UltimakerRobot.png')
241
242                 glDisable(GL_DEPTH_TEST)
243                 glEnable(GL_BLEND)
244                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
245                 glDisable(GL_LIGHTING)
246                 glColor4ub(255,255,255,255)
247
248                 glMatrixMode(GL_PROJECTION)
249                 glLoadIdentity()
250                 size = self.GetSize()
251                 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
252                 glMatrixMode(GL_MODELVIEW)
253                 glLoadIdentity()
254
255                 self._container.draw()
256
257                 glBindTexture(GL_TEXTURE_2D, self._glRobotTexture)
258                 glEnable(GL_TEXTURE_2D)
259                 glPushMatrix()
260                 glColor4f(1,1,1,1)
261                 glTranslate(size.GetWidth() - 8,size.GetHeight() - 16,0)
262                 s = self._buttonSize * 1.4
263                 glScale(s,s,s)
264                 glBegin(GL_QUADS)
265                 glTexCoord2f(1, 0)
266                 glVertex2f(0,-1)
267                 glTexCoord2f(0, 0)
268                 glVertex2f(-1,-1)
269                 glTexCoord2f(0, 1)
270                 glVertex2f(-1, 0)
271                 glTexCoord2f(1, 1)
272                 glVertex2f(0, 0)
273                 glEnd()
274                 glDisable(GL_TEXTURE_2D)
275                 glPopMatrix()
276
277         def _OnEraseBackground(self,event):
278                 #Workaround for windows background redraw flicker.
279                 pass
280
281         def _OnSize(self,e):
282                 self._container.setSize(0, 0, self.GetSize().GetWidth(), self.GetSize().GetHeight())
283                 self._container.updateLayout()
284                 self.Refresh()
285
286         def OnMouseDown(self,e):
287                 pass
288         def OnMouseUp(self,e):
289                 pass
290         def OnMouseMotion(self, e):
291                 pass
292         def OnKeyChar(self, keyCode):
293                 pass
294         def OnPaint(self, e):
295                 pass
296         def OnKeyChar(self, keycode):
297                 pass
298
299         def QueueRefresh(self):
300                 wx.CallAfter(self._queueRefresh)
301
302         def _queueRefresh(self):
303                 self._refreshQueued = True
304
305         def add(self, ctrl):
306                 if self._container is not None:
307                         self._container.add(ctrl)
308
309 class glGuiLayoutButtons(object):
310         def __init__(self, parent):
311                 self._parent = parent
312                 self._parent._layout = self
313
314         def update(self):
315                 bs = self._parent._base._buttonSize
316                 x0, y0, w, h = self._parent.getSize()
317                 gridSize = bs * 1.0
318                 for ctrl in self._parent._glGuiControlList:
319                         pos = ctrl._pos
320                         if pos[0] < 0:
321                                 x = w + pos[0] * gridSize - bs * 0.2
322                         else:
323                                 x = pos[0] * gridSize + bs * 0.2
324                         if pos[1] < 0:
325                                 y = h + pos[1] * gridSize * 1.2 - bs * 0.2
326                         else:
327                                 y = pos[1] * gridSize * 1.2 + bs * 0.2
328                         ctrl.setSize(x, y, gridSize, gridSize)
329
330         def getLayoutSize(self):
331                 _, _, w, h = self._parent.getSize()
332                 return w, h
333
334 class glGuiLayoutGrid(object):
335         def __init__(self, parent):
336                 self._parent = parent
337                 self._parent._layout = self
338                 self._size = 0,0
339                 self._alignBottom = True
340
341         def update(self):
342                 borderSize = self._parent._base._buttonSize * 0.2
343                 x0, y0, w, h = self._parent.getSize()
344                 x0 += borderSize
345                 y0 += borderSize
346                 widths = {}
347                 heights = {}
348                 for ctrl in self._parent._glGuiControlList:
349                         x, y = ctrl._pos
350                         w, h = ctrl.getMinSize()
351                         if not x in widths:
352                                 widths[x] = w
353                         else:
354                                 widths[x] = max(widths[x], w)
355                         if not y in heights:
356                                 heights[y] = h
357                         else:
358                                 heights[y] = max(heights[y], h)
359                 self._size = sum(widths.values()) + borderSize * 2, sum(heights.values()) + borderSize * 2
360                 if self._alignBottom:
361                         y0 -= self._size[1] - self._parent.getSize()[3]
362                         self._parent.setSize(x0 - borderSize, y0 - borderSize, self._size[0], self._size[1])
363                 for ctrl in self._parent._glGuiControlList:
364                         x, y = ctrl._pos
365                         x1 = x0
366                         y1 = y0
367                         for n in xrange(0, x):
368                                 if not n in widths:
369                                         widths[n] = 3
370                                 x1 += widths[n]
371                         for n in xrange(0, y):
372                                 if not n in heights:
373                                         heights[n] = 3
374                                 y1 += heights[n]
375                         ctrl.setSize(x1, y1, widths[x], heights[y])
376
377         def getLayoutSize(self):
378                 return self._size
379
380 class glButton(glGuiControl):
381         def __init__(self, parent, imageID, tooltip, pos, callback, size = None):
382                 self._buttonSize = size
383                 self._hidden = False
384                 super(glButton, self).__init__(parent, pos)
385                 self._tooltip = tooltip
386                 self._parent = parent
387                 self._imageID = imageID
388                 self._callback = callback
389                 self._selected = False
390                 self._focus = False
391                 self._disabled = False
392                 self._showExpandArrow = False
393                 self._progressBar = None
394                 self._altTooltip = ''
395
396         def setSelected(self, value):
397                 self._selected = value
398
399         def setExpandArrow(self, value):
400                 self._showExpandArrow = value
401
402         def setHidden(self, value):
403                 self._hidden = value
404
405         def setDisabled(self, value):
406                 self._disabled = value
407
408         def setProgressBar(self, value):
409                 self._progressBar = value
410
411         def setBottomText(self, value):
412                 self._altTooltip = value
413
414         def getSelected(self):
415                 return self._selected
416
417         def getMinSize(self):
418                 if self._hidden:
419                         return 0, 0
420                 if self._buttonSize is not None:
421                         return self._buttonSize, self._buttonSize
422                 return self._base._buttonSize, self._base._buttonSize
423
424         def _getPixelPos(self):
425                 x0, y0, w, h = self.getSize()
426                 return x0 + w / 2, y0 + h / 2
427
428         def draw(self):
429                 if self._hidden:
430                         return
431
432                 cx = (self._imageID % 4) / 4
433                 cy = int(self._imageID / 4) / 4
434                 bs = self.getMinSize()[0]
435                 pos = self._getPixelPos()
436
437                 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
438                 scale = 0.8
439                 if self._selected:
440                         scale = 1.0
441                 elif self._focus:
442                         scale = 0.9
443                 if self._disabled:
444                         glColor4ub(128,128,128,128)
445                 else:
446                         glColor4ub(255,255,255,255)
447                 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, 0)
448                 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, self._imageID)
449                 if self._showExpandArrow:
450                         if self._selected:
451                                 opengl.glDrawTexturedQuad(pos[0]+bs*scale/2-bs*scale/4*1.2, pos[1]-bs*scale/2*1.2, bs*scale/4, bs*scale/4, 1)
452                         else:
453                                 opengl.glDrawTexturedQuad(pos[0]+bs*scale/2-bs*scale/4*1.2, pos[1]-bs*scale/2*1.2, bs*scale/4, bs*scale/4, 1, 2)
454                 glPushMatrix()
455                 glTranslatef(pos[0], pos[1], 0)
456                 glDisable(GL_TEXTURE_2D)
457                 if self._focus:
458                         glTranslatef(0, -0.55*bs*scale, 0)
459
460                         glPushMatrix()
461                         glColor4ub(60,60,60,255)
462                         glTranslatef(-1, -1, 0)
463                         opengl.glDrawStringCenter(self._tooltip)
464                         glTranslatef(0, 2, 0)
465                         opengl.glDrawStringCenter(self._tooltip)
466                         glTranslatef(2, 0, 0)
467                         opengl.glDrawStringCenter(self._tooltip)
468                         glTranslatef(0, -2, 0)
469                         opengl.glDrawStringCenter(self._tooltip)
470                         glPopMatrix()
471
472                         glColor4ub(255,255,255,255)
473                         opengl.glDrawStringCenter(self._tooltip)
474                 glPopMatrix()
475                 progress = self._progressBar
476                 if progress is not None:
477                         glColor4ub(255,255,255,192)
478                         opengl.glDrawTexturedQuad(pos[0]-bs/2, pos[1]+bs/2, bs, bs / 4, 0)
479                         glColor4ub(255,255,255,255)
480                         opengl.glDrawTexturedQuad(pos[0]-bs/2, pos[1]+bs/2, bs * progress, bs / 4, 0)
481                 elif len(self._altTooltip) > 0:
482                         glPushMatrix()
483                         glTranslatef(pos[0], pos[1], 0)
484                         glTranslatef(0.6*bs*scale, 0, 0)
485
486                         glPushMatrix()
487                         glColor4ub(60,60,60,255)
488                         glTranslatef(-1, -1, 0)
489                         opengl.glDrawStringLeft(self._altTooltip)
490                         glTranslatef(0, 2, 0)
491                         opengl.glDrawStringLeft(self._altTooltip)
492                         glTranslatef(2, 0, 0)
493                         opengl.glDrawStringLeft(self._altTooltip)
494                         glTranslatef(0, -2, 0)
495                         opengl.glDrawStringLeft(self._altTooltip)
496                         glPopMatrix()
497
498                         glColor4ub(255,255,255,255)
499                         opengl.glDrawStringLeft(self._altTooltip)
500                         glPopMatrix()
501
502         def _checkHit(self, x, y):
503                 if self._hidden or self._disabled:
504                         return False
505                 bs = self.getMinSize()[0]
506                 pos = self._getPixelPos()
507                 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
508
509         def OnMouseMotion(self, x, y):
510                 if self._checkHit(x, y):
511                         self._focus = True
512                         return True
513                 self._focus = False
514                 return False
515
516         def OnMouseDown(self, x, y, button):
517                 if self._checkHit(x, y):
518                         self._callback(button)
519                         return True
520                 return False
521
522 class glRadioButton(glButton):
523         def __init__(self, parent, imageID, tooltip, pos, group, callback):
524                 super(glRadioButton, self).__init__(parent, imageID, tooltip, pos, self._onRadioSelect)
525                 self._group = group
526                 self._radioCallback = callback
527                 self._group.append(self)
528
529         def setSelected(self, value):
530                 self._selected = value
531
532         def _onRadioSelect(self, button):
533                 self._base._focus = None
534                 for ctrl in self._group:
535                         if ctrl != self:
536                                 ctrl.setSelected(False)
537                 if self.getSelected():
538                         self.setSelected(False)
539                 else:
540                         self.setSelected(True)
541                 self._radioCallback(button)
542
543 class glComboButton(glButton):
544         def __init__(self, parent, tooltip, imageIDs, tooltips, pos, callback):
545                 super(glComboButton, self).__init__(parent, imageIDs[0], tooltip, pos, self._onComboOpenSelect)
546                 self._imageIDs = imageIDs
547                 self._tooltips = tooltips
548                 self._comboCallback = callback
549                 self._selection = 0
550
551         def _onComboOpenSelect(self, button):
552                 if self.hasFocus():
553                         self._base._focus = None
554                 else:
555                         self._base._focus = self
556
557         def draw(self):
558                 if self._hidden:
559                         return
560                 self._selected = self.hasFocus()
561                 super(glComboButton, self).draw()
562
563                 bs = self._base._buttonSize / 2
564                 pos = self._getPixelPos()
565
566                 if not self._selected:
567                         return
568
569                 glPushMatrix()
570                 glTranslatef(pos[0]+bs*0.5, pos[1] + bs*0.5, 0)
571                 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
572                 for n in xrange(0, len(self._imageIDs)):
573                         glTranslatef(0, bs, 0)
574                         glColor4ub(255,255,255,255)
575                         opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, 0)
576                         opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, self._imageIDs[n])
577                         glDisable(GL_TEXTURE_2D)
578
579                         glPushMatrix()
580                         glTranslatef(-0.55*bs, 0.1*bs, 0)
581
582                         glPushMatrix()
583                         glColor4ub(60,60,60,255)
584                         glTranslatef(-1, -1, 0)
585                         opengl.glDrawStringRight(self._tooltips[n])
586                         glTranslatef(0, 2, 0)
587                         opengl.glDrawStringRight(self._tooltips[n])
588                         glTranslatef(2, 0, 0)
589                         opengl.glDrawStringRight(self._tooltips[n])
590                         glTranslatef(0, -2, 0)
591                         opengl.glDrawStringRight(self._tooltips[n])
592                         glPopMatrix()
593
594                         glColor4ub(255,255,255,255)
595                         opengl.glDrawStringRight(self._tooltips[n])
596                         glPopMatrix()
597                 glPopMatrix()
598
599         def getValue(self):
600                 return self._selection
601
602         def setValue(self, value):
603                 self._selection = value
604                 self._imageID = self._imageIDs[self._selection]
605                 self._comboCallback()
606
607         def OnMouseDown(self, x, y, button):
608                 if self._hidden or self._disabled:
609                         return False
610                 if self.hasFocus():
611                         bs = self._base._buttonSize / 2
612                         pos = self._getPixelPos()
613                         if 0 <= x - pos[0] <= bs and 0 <= y - pos[1] - bs <= bs * len(self._imageIDs):
614                                 self._selection = int((y - pos[1] - bs) / bs)
615                                 self._imageID = self._imageIDs[self._selection]
616                                 self._base._focus = None
617                                 self._comboCallback()
618                                 return True
619                 return super(glComboButton, self).OnMouseDown(x, y, button)
620
621 class glFrame(glGuiContainer):
622         def __init__(self, parent, pos):
623                 super(glFrame, self).__init__(parent, pos)
624                 self._selected = False
625                 self._focus = False
626                 self._hidden = False
627
628         def setSelected(self, value):
629                 self._selected = value
630
631         def setHidden(self, value):
632                 self._hidden = value
633                 for child in self._glGuiControlList:
634                         if self._base._focus == child:
635                                 self._base._focus = None
636
637         def getSelected(self):
638                 return self._selected
639
640         def getMinSize(self):
641                 return self._base._buttonSize, self._base._buttonSize
642
643         def _getPixelPos(self):
644                 x0, y0, w, h = self.getSize()
645                 return x0, y0
646
647         def draw(self):
648                 if self._hidden:
649                         return
650
651                 bs = self._parent._buttonSize
652                 pos = self._getPixelPos()
653
654                 size = self._layout.getLayoutSize()
655                 glColor4ub(255,255,255,255)
656                 opengl.glDrawStretchedQuad(pos[0], pos[1], size[0], size[1], bs*0.75, 0)
657                 #Draw the controls on the frame
658                 super(glFrame, self).draw()
659
660         def _checkHit(self, x, y):
661                 if self._hidden:
662                         return False
663                 pos = self._getPixelPos()
664                 w, h = self._layout.getLayoutSize()
665                 return 0 <= x - pos[0] <= w and 0 <= y - pos[1] <= h
666
667         def OnMouseMotion(self, x, y):
668                 super(glFrame, self).OnMouseMotion(x, y)
669                 if self._checkHit(x, y):
670                         self._focus = True
671                         return True
672                 self._focus = False
673                 return False
674
675         def OnMouseDown(self, x, y, button):
676                 if self._checkHit(x, y):
677                         super(glFrame, self).OnMouseDown(x, y, button)
678                         return True
679                 return False
680
681 class glNotification(glFrame):
682         def __init__(self, parent, pos):
683                 self._anim = None
684                 super(glNotification, self).__init__(parent, pos)
685                 glGuiLayoutGrid(self)._alignBottom = False
686                 self._label = glLabel(self, "Notification", (0, 0))
687                 self._buttonEject = glButton(self, 31, "Eject", (1, 0), self.onEject, 25)
688                 self._button = glButton(self, 30, "", (2, 0), self.onClose, 25)
689                 self._padding = glLabel(self, "", (0, 1))
690                 self.setHidden(True)
691
692         def setSize(self, x, y, w, h):
693                 w, h = self._layout.getLayoutSize()
694                 baseSize = self._base.GetSizeTuple()
695                 if self._anim is not None:
696                         super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._anim.getPosition() - self._base._buttonSize * 0.2, 1, 1)
697                 else:
698                         super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._base._buttonSize * 0.2, 1, 1)
699
700         def draw(self):
701                 self.setSize(0,0,0,0)
702                 self.updateLayout()
703                 super(glNotification, self).draw()
704
705         def message(self, text, ejectCallback = None):
706                 if self._anim is not None:
707                         self._anim = animation(self._base, self._anim.getPosition(), 25, 1)
708                 else:
709                         self._anim = animation(self._base, -20, 25, 1)
710                 self.setHidden(False)
711                 self._label.setLabel(text)
712                 self._buttonEject.setHidden(ejectCallback is None)
713                 self._ejectCallback = ejectCallback
714                 self._base._queueRefresh()
715                 self.updateLayout()
716
717         def onEject(self, button):
718                 self.onClose(button)
719                 self._ejectCallback()
720
721         def onClose(self, button):
722                 if self._anim is not None:
723                         self._anim = animation(self._base, self._anim.getPosition(), -20, 1)
724                 else:
725                         self._anim = animation(self._base, 25, -20, 1)
726
727 class glLabel(glGuiControl):
728         def __init__(self, parent, label, pos):
729                 self._label = label
730                 super(glLabel, self).__init__(parent, pos)
731
732         def setLabel(self, label):
733                 self._label = label
734
735         def getMinSize(self):
736                 w, h = opengl.glGetStringSize(self._label)
737                 return w + 10, h + 4
738
739         def _getPixelPos(self):
740                 x0, y0, w, h = self.getSize()
741                 return x0, y0
742
743         def draw(self):
744                 x, y, w, h = self.getSize()
745
746                 glPushMatrix()
747                 glTranslatef(x, y, 0)
748
749 #               glColor4ub(255,255,255,128)
750 #               glBegin(GL_QUADS)
751 #               glTexCoord2f(1, 0)
752 #               glVertex2f( w, 0)
753 #               glTexCoord2f(0, 0)
754 #               glVertex2f( 0, 0)
755 #               glTexCoord2f(0, 1)
756 #               glVertex2f( 0, h)
757 #               glTexCoord2f(1, 1)
758 #               glVertex2f( w, h)
759 #               glEnd()
760
761                 glTranslate(5, h - 5, 0)
762                 glColor4ub(255,255,255,255)
763                 opengl.glDrawStringLeft(self._label)
764                 glPopMatrix()
765
766         def _checkHit(self, x, y):
767                 return False
768
769         def OnMouseMotion(self, x, y):
770                 return False
771
772         def OnMouseDown(self, x, y, button):
773                 return False
774
775 class glNumberCtrl(glGuiControl):
776         def __init__(self, parent, value, pos, callback):
777                 self._callback = callback
778                 self._value = str(value)
779                 self._selectPos = 0
780                 self._maxLen = 6
781                 self._inCallback = False
782                 super(glNumberCtrl, self).__init__(parent, pos)
783
784         def setValue(self, value):
785                 if self._inCallback:
786                         return
787                 self._value = str(value)
788
789         def getMinSize(self):
790                 w, h = opengl.glGetStringSize("VALUES")
791                 return w + 10, h + 4
792
793         def _getPixelPos(self):
794                 x0, y0, w, h = self.getSize()
795                 return x0, y0
796
797         def draw(self):
798                 x, y, w, h = self.getSize()
799
800                 glPushMatrix()
801                 glTranslatef(x, y, 0)
802
803                 if self.hasFocus():
804                         glColor4ub(255,255,255,255)
805                 else:
806                         glColor4ub(255,255,255,192)
807                 glBegin(GL_QUADS)
808                 glTexCoord2f(1, 0)
809                 glVertex2f( w, 0)
810                 glTexCoord2f(0, 0)
811                 glVertex2f( 0, 0)
812                 glTexCoord2f(0, 1)
813                 glVertex2f( 0, h-1)
814                 glTexCoord2f(1, 1)
815                 glVertex2f( w, h-1)
816                 glEnd()
817
818                 glTranslate(5, h - 5, 0)
819                 glColor4ub(0,0,0,255)
820                 opengl.glDrawStringLeft(self._value)
821                 if self.hasFocus():
822                         glTranslate(opengl.glGetStringSize(self._value[0:self._selectPos])[0] - 2, -1, 0)
823                         opengl.glDrawStringLeft('|')
824                 glPopMatrix()
825
826         def _checkHit(self, x, y):
827                 x1, y1, w, h = self.getSize()
828                 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
829
830         def OnMouseMotion(self, x, y):
831                 return False
832
833         def OnMouseDown(self, x, y, button):
834                 if self._checkHit(x, y):
835                         self.setFocus()
836                         return True
837                 return False
838
839         def OnKeyChar(self, c):
840                 self._inCallback = True
841                 if c == wx.WXK_LEFT:
842                         self._selectPos -= 1
843                         self._selectPos = max(0, self._selectPos)
844                 if c == wx.WXK_RIGHT:
845                         self._selectPos += 1
846                         self._selectPos = min(self._selectPos, len(self._value))
847                 if c == wx.WXK_UP:
848                         try:
849                                 value = float(self._value)
850                         except:
851                                 pass
852                         else:
853                                 value += 0.1
854                                 self._value = str(value)
855                                 self._callback(self._value)
856                 if c == wx.WXK_DOWN:
857                         try:
858                                 value = float(self._value)
859                         except:
860                                 pass
861                         else:
862                                 value -= 0.1
863                                 if value > 0:
864                                         self._value = str(value)
865                                         self._callback(self._value)
866                 if c == wx.WXK_BACK and self._selectPos > 0:
867                         self._value = self._value[0:self._selectPos - 1] + self._value[self._selectPos:]
868                         self._selectPos -= 1
869                         self._callback(self._value)
870                 if c == wx.WXK_DELETE:
871                         self._value = self._value[0:self._selectPos] + self._value[self._selectPos + 1:]
872                         self._callback(self._value)
873                 if c == wx.WXK_TAB or c == wx.WXK_NUMPAD_ENTER or c == wx.WXK_RETURN:
874                         if wx.GetKeyState(wx.WXK_SHIFT):
875                                 self.focusPrevious()
876                         else:
877                                 self.focusNext()
878                 if (ord('0') <= c <= ord('9') or c == ord('.')) and len(self._value) < self._maxLen:
879                         self._value = self._value[0:self._selectPos] + chr(c) + self._value[self._selectPos:]
880                         self._selectPos += 1
881                         self._callback(self._value)
882                 self._inCallback = False
883
884         def setFocus(self):
885                 self._base._focus = self
886                 self._selectPos = len(self._value)
887                 return True
888
889 class glCheckbox(glGuiControl):
890         def __init__(self, parent, value, pos, callback):
891                 self._callback = callback
892                 self._value = value
893                 self._selectPos = 0
894                 self._maxLen = 6
895                 self._inCallback = False
896                 super(glCheckbox, self).__init__(parent, pos)
897
898         def setValue(self, value):
899                 if self._inCallback:
900                         return
901                 self._value = str(value)
902
903         def getValue(self):
904                 return self._value
905
906         def getMinSize(self):
907                 return 20, 20
908
909         def _getPixelPos(self):
910                 x0, y0, w, h = self.getSize()
911                 return x0, y0
912
913         def draw(self):
914                 x, y, w, h = self.getSize()
915
916                 glPushMatrix()
917                 glTranslatef(x, y, 0)
918
919                 glColor3ub(255,255,255)
920                 if self._value:
921                         opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 28)
922                 else:
923                         opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 29)
924
925                 glPopMatrix()
926
927         def _checkHit(self, x, y):
928                 x1, y1, w, h = self.getSize()
929                 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
930
931         def OnMouseMotion(self, x, y):
932                 return False
933
934         def OnMouseDown(self, x, y, button):
935                 if self._checkHit(x, y):
936                         self._value = not self._value
937                         return True
938                 return False
939
940 class glSlider(glGuiControl):
941         def __init__(self, parent, value, minValue, maxValue, pos, callback):
942                 super(glSlider, self).__init__(parent, pos)
943                 self._callback = callback
944                 self._focus = False
945                 self._hidden = False
946                 self._value = value
947                 self._minValue = minValue
948                 self._maxValue = maxValue
949
950         def setValue(self, value):
951                 self._value = value
952                 self._value = max(self._minValue, self._value)
953                 self._value = min(self._maxValue, self._value)
954
955         def getValue(self):
956                 return self._value
957
958         def setRange(self, minValue, maxValue):
959                 if maxValue < minValue:
960                         maxValue = minValue
961                 self._minValue = minValue
962                 self._maxValue = maxValue
963                 self._value = max(minValue, self._value)
964                 self._value = min(maxValue, self._value)
965
966         def getMinValue(self):
967                 return self._minValue
968
969         def getMaxValue(self):
970                 return self._maxValue
971
972         def setHidden(self, value):
973                 self._hidden = value
974
975         def getMinSize(self):
976                 return self._base._buttonSize * 0.2, self._base._buttonSize * 4
977
978         def _getPixelPos(self):
979                 x0, y0, w, h = self.getSize()
980                 minSize = self.getMinSize()
981                 return x0 + w / 2 - minSize[0] / 2, y0 + h / 2 - minSize[1] / 2
982
983         def draw(self):
984                 if self._hidden:
985                         return
986
987                 w, h = self.getMinSize()
988                 pos = self._getPixelPos()
989
990                 glPushMatrix()
991                 glTranslatef(pos[0], pos[1], 0)
992                 glDisable(GL_TEXTURE_2D)
993                 if self.hasFocus():
994                         glColor4ub(60,60,60,255)
995                 else:
996                         glColor4ub(60,60,60,192)
997                 glBegin(GL_QUADS)
998                 glVertex2f( w/2,-h/2)
999                 glVertex2f(-w/2,-h/2)
1000                 glVertex2f(-w/2, h/2)
1001                 glVertex2f( w/2, h/2)
1002                 glEnd()
1003                 scrollLength = h - w
1004                 glTranslate(0.0,scrollLength/2,0)
1005                 if self._focus:
1006                         glColor4ub(0,0,0,255)
1007                         glPushMatrix()
1008                         glTranslate(-w/2,opengl.glGetStringSize(str(self._minValue))[1]/2,0)
1009                         opengl.glDrawStringRight(str(self._minValue))
1010                         glTranslate(0,-scrollLength,0)
1011                         opengl.glDrawStringRight(str(self._maxValue))
1012                         if self._maxValue-self._minValue > 0:
1013                                 glTranslate(w,scrollLength-scrollLength*((self._value-self._minValue)/(self._maxValue-self._minValue)),0)
1014                         opengl.glDrawStringLeft(str(self._value))
1015                         glPopMatrix()
1016                 glColor4ub(255,255,255,240)
1017                 if self._maxValue - self._minValue != 0:
1018                         glTranslate(0.0,-scrollLength*((self._value-self._minValue)/(self._maxValue-self._minValue)),0)
1019                 glBegin(GL_QUADS)
1020                 glVertex2f( w/2,-w/2)
1021                 glVertex2f(-w/2,-w/2)
1022                 glVertex2f(-w/2, w/2)
1023                 glVertex2f( w/2, w/2)
1024                 glEnd()
1025                 glPopMatrix()
1026
1027         def _checkHit(self, x, y):
1028                 if self._hidden:
1029                         return False
1030                 pos = self._getPixelPos()
1031                 w, h = self.getMinSize()
1032                 return -w/2 <= x - pos[0] <= w/2 and -h/2 <= y - pos[1] <= h/2
1033
1034         def setFocus(self):
1035                 self._base._focus = self
1036                 return True
1037
1038         def OnMouseMotion(self, x, y):
1039                 if self.hasFocus():
1040                         w, h = self.getMinSize()
1041                         scrollLength = h - w
1042                         pos = self._getPixelPos()
1043                         self.setValue(int(self._minValue + (self._maxValue - self._minValue) * -(y - pos[1] - scrollLength/2) / scrollLength))
1044                         self._callback()
1045                         return True
1046                 if self._checkHit(x, y):
1047                         self._focus = True
1048                         return True
1049                 self._focus = False
1050                 return False
1051
1052         def OnMouseDown(self, x, y, button):
1053                 if self._checkHit(x, y):
1054                         self.setFocus()
1055                         self.OnMouseMotion(x, y)
1056                         return True
1057                 return False
1058
1059         def OnMouseUp(self, x, y):
1060                 if self.hasFocus():
1061                         self._base._focus = None
1062                         return True
1063                 return False