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