chiark / gitweb /
Change how the engine is interfaced from the python code. Put the GCode viewer in...
[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                                 traceback.print_exc()
249                                 wx.CallAfter(wx.MessageBox, errStr, _("3D window error"), wx.OK | wx.ICON_EXCLAMATION)
250                                 self._shownError = True
251
252         def _drawGui(self):
253                 if self._glButtonsTexture is None:
254                         self._glButtonsTexture = opengl.loadGLTexture('glButtons.png')
255                         self._glRobotTexture = opengl.loadGLTexture('UltimakerRobot.png')
256
257                 glDisable(GL_DEPTH_TEST)
258                 glEnable(GL_BLEND)
259                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
260                 glDisable(GL_LIGHTING)
261                 glColor4ub(255,255,255,255)
262
263                 glMatrixMode(GL_PROJECTION)
264                 glLoadIdentity()
265                 size = self.GetSize()
266                 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
267                 glMatrixMode(GL_MODELVIEW)
268                 glLoadIdentity()
269
270                 self._container.draw()
271
272                 # glBindTexture(GL_TEXTURE_2D, self._glRobotTexture)
273                 # glEnable(GL_TEXTURE_2D)
274                 # glPushMatrix()
275                 # glColor4f(1,1,1,1)
276                 # glTranslate(size.GetWidth(),size.GetHeight(),0)
277                 # s = self._buttonSize * 1
278                 # glScale(s,s,s)
279                 # glTranslate(-1.2,-0.2,0)
280                 # glBegin(GL_QUADS)
281                 # glTexCoord2f(1, 0)
282                 # glVertex2f(0,-1)
283                 # glTexCoord2f(0, 0)
284                 # glVertex2f(-1,-1)
285                 # glTexCoord2f(0, 1)
286                 # glVertex2f(-1, 0)
287                 # glTexCoord2f(1, 1)
288                 # glVertex2f(0, 0)
289                 # glEnd()
290                 # glDisable(GL_TEXTURE_2D)
291                 # glPopMatrix()
292
293         def _OnEraseBackground(self,event):
294                 #Workaround for windows background redraw flicker.
295                 pass
296
297         def _OnSize(self,e):
298                 self._container.setSize(0, 0, self.GetSize().GetWidth(), self.GetSize().GetHeight())
299                 self._container.updateLayout()
300                 self.Refresh()
301
302         def OnMouseDown(self,e):
303                 pass
304         def OnMouseUp(self,e):
305                 pass
306         def OnMouseMotion(self, e):
307                 pass
308         def OnKeyChar(self, keyCode):
309                 pass
310         def OnPaint(self, e):
311                 pass
312         def OnKeyChar(self, keycode):
313                 pass
314
315         def QueueRefresh(self):
316                 wx.CallAfter(self._queueRefresh)
317
318         def _queueRefresh(self):
319                 if self._idleCalled:
320                         wx.CallAfter(self.Refresh)
321                 else:
322                         self._refreshQueued = True
323
324         def add(self, ctrl):
325                 if self._container is not None:
326                         self._container.add(ctrl)
327
328 class glGuiLayoutButtons(object):
329         def __init__(self, parent):
330                 self._parent = parent
331                 self._parent._layout = self
332
333         def update(self):
334                 bs = self._parent._base._buttonSize
335                 x0, y0, w, h = self._parent.getSize()
336                 gridSize = bs * 1.0
337                 for ctrl in self._parent._glGuiControlList:
338                         pos = ctrl._pos
339                         if pos[0] < 0:
340                                 x = w + pos[0] * gridSize - bs * 0.2
341                         else:
342                                 x = pos[0] * gridSize + bs * 0.2
343                         if pos[1] < 0:
344                                 y = h + pos[1] * gridSize * 1.2 - bs * 0.0
345                         else:
346                                 y = pos[1] * gridSize * 1.2 + bs * 0.2
347                         ctrl.setSize(x, y, gridSize, gridSize)
348
349         def getLayoutSize(self):
350                 _, _, w, h = self._parent.getSize()
351                 return w, h
352
353 class glGuiLayoutGrid(object):
354         def __init__(self, parent):
355                 self._parent = parent
356                 self._parent._layout = self
357                 self._size = 0,0
358                 self._alignBottom = True
359
360         def update(self):
361                 borderSize = self._parent._base._buttonSize * 0.2
362                 x0, y0, w, h = self._parent.getSize()
363                 x0 += borderSize
364                 y0 += borderSize
365                 widths = {}
366                 heights = {}
367                 for ctrl in self._parent._glGuiControlList:
368                         x, y = ctrl._pos
369                         w, h = ctrl.getMinSize()
370                         if not x in widths:
371                                 widths[x] = w
372                         else:
373                                 widths[x] = max(widths[x], w)
374                         if not y in heights:
375                                 heights[y] = h
376                         else:
377                                 heights[y] = max(heights[y], h)
378                 self._size = sum(widths.values()) + borderSize * 2, sum(heights.values()) + borderSize * 2
379                 if self._alignBottom:
380                         y0 -= self._size[1] - self._parent.getSize()[3]
381                         self._parent.setSize(x0 - borderSize, y0 - borderSize, self._size[0], self._size[1])
382                 for ctrl in self._parent._glGuiControlList:
383                         x, y = ctrl._pos
384                         x1 = x0
385                         y1 = y0
386                         for n in xrange(0, x):
387                                 if not n in widths:
388                                         widths[n] = 3
389                                 x1 += widths[n]
390                         for n in xrange(0, y):
391                                 if not n in heights:
392                                         heights[n] = 3
393                                 y1 += heights[n]
394                         ctrl.setSize(x1, y1, widths[x], heights[y])
395
396         def getLayoutSize(self):
397                 return self._size
398
399 class glButton(glGuiControl):
400         def __init__(self, parent, imageID, tooltip, pos, callback, size = None):
401                 self._buttonSize = size
402                 self._hidden = False
403                 super(glButton, self).__init__(parent, pos)
404                 self._tooltip = tooltip
405                 self._parent = parent
406                 self._imageID = imageID
407                 self._callback = callback
408                 self._selected = False
409                 self._focus = False
410                 self._disabled = False
411                 self._showExpandArrow = False
412                 self._progressBar = None
413                 self._altTooltip = ''
414
415         def setSelected(self, value):
416                 self._selected = value
417
418         def setExpandArrow(self, value):
419                 self._showExpandArrow = value
420
421         def setHidden(self, value):
422                 self._hidden = value
423
424         def setDisabled(self, value):
425                 self._disabled = value
426
427         def setProgressBar(self, value):
428                 self._progressBar = value
429
430         def getProgressBar(self):
431                 return self._progressBar
432
433         def setBottomText(self, value):
434                 self._altTooltip = value
435
436         def getSelected(self):
437                 return self._selected
438
439         def getMinSize(self):
440                 if self._hidden:
441                         return 0, 0
442                 if self._buttonSize is not None:
443                         return self._buttonSize, self._buttonSize
444                 return self._base._buttonSize, self._base._buttonSize
445
446         def _getPixelPos(self):
447                 x0, y0, w, h = self.getSize()
448                 return x0 + w / 2, y0 + h / 2
449
450         def draw(self):
451                 if self._hidden:
452                         return
453
454                 cx = (self._imageID % 4) / 4
455                 cy = int(self._imageID / 4) / 4
456                 bs = self.getMinSize()[0]
457                 pos = self._getPixelPos()
458
459                 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
460                 scale = 0.8
461                 if self._selected:
462                         scale = 1.0
463                 elif self._focus:
464                         scale = 0.9
465                 if self._disabled:
466                         glColor4ub(128,128,128,128)
467                 else:
468                         glColor4ub(255,255,255,255)
469                 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, 0)
470                 opengl.glDrawTexturedQuad(pos[0]-bs*scale/2, pos[1]-bs*scale/2, bs*scale, bs*scale, self._imageID)
471                 if self._showExpandArrow:
472                         if self._selected:
473                                 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)
474                         else:
475                                 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)
476                 glPushMatrix()
477                 glTranslatef(pos[0], pos[1], 0)
478                 glDisable(GL_TEXTURE_2D)
479                 if self._focus:
480                         glTranslatef(0, -0.55*bs*scale, 0)
481
482                         glPushMatrix()
483                         glColor4ub(60,60,60,255)
484                         glTranslatef(-1, -1, 0)
485                         opengl.glDrawStringCenter(self._tooltip)
486                         glTranslatef(0, 2, 0)
487                         opengl.glDrawStringCenter(self._tooltip)
488                         glTranslatef(2, 0, 0)
489                         opengl.glDrawStringCenter(self._tooltip)
490                         glTranslatef(0, -2, 0)
491                         opengl.glDrawStringCenter(self._tooltip)
492                         glPopMatrix()
493
494                         glColor4ub(255,255,255,255)
495                         opengl.glDrawStringCenter(self._tooltip)
496                 glPopMatrix()
497                 progress = self._progressBar
498                 if progress is not None:
499                         glColor4ub(60,60,60,255)
500                         opengl.glDrawQuad(pos[0]-bs/2, pos[1]+bs/2, bs, bs / 4)
501                         glColor4ub(255,255,255,255)
502                         opengl.glDrawQuad(pos[0]-bs/2+2, pos[1]+bs/2+2, (bs - 5) * progress + 1, bs / 4 - 4)
503                 elif len(self._altTooltip) > 0:
504                         glPushMatrix()
505                         glTranslatef(pos[0], pos[1], 0)
506                         glTranslatef(0, 0.6*bs, 0)
507                         glTranslatef(0, 6, 0)
508                         #glTranslatef(0.6*bs*scale, 0, 0)
509
510                         for line in self._altTooltip.split('\n'):
511                                 glPushMatrix()
512                                 glColor4ub(60,60,60,255)
513                                 glTranslatef(-1, -1, 0)
514                                 opengl.glDrawStringCenter(line)
515                                 glTranslatef(0, 2, 0)
516                                 opengl.glDrawStringCenter(line)
517                                 glTranslatef(2, 0, 0)
518                                 opengl.glDrawStringCenter(line)
519                                 glTranslatef(0, -2, 0)
520                                 opengl.glDrawStringCenter(line)
521                                 glPopMatrix()
522
523                                 glColor4ub(255,255,255,255)
524                                 opengl.glDrawStringCenter(line)
525                                 glTranslatef(0, 18, 0)
526                         glPopMatrix()
527
528         def _checkHit(self, x, y):
529                 if self._hidden or self._disabled:
530                         return False
531                 bs = self.getMinSize()[0]
532                 pos = self._getPixelPos()
533                 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
534
535         def OnMouseMotion(self, x, y):
536                 if self._checkHit(x, y):
537                         self._focus = True
538                         return True
539                 self._focus = False
540                 return False
541
542         def OnMouseDown(self, x, y, button):
543                 if self._checkHit(x, y):
544                         self._callback(button)
545                         return True
546                 return False
547
548 class glRadioButton(glButton):
549         def __init__(self, parent, imageID, tooltip, pos, group, callback):
550                 super(glRadioButton, self).__init__(parent, imageID, tooltip, pos, self._onRadioSelect)
551                 self._group = group
552                 self._radioCallback = callback
553                 self._group.append(self)
554
555         def setSelected(self, value):
556                 self._selected = value
557
558         def _onRadioSelect(self, button):
559                 self._base._focus = None
560                 for ctrl in self._group:
561                         if ctrl != self:
562                                 ctrl.setSelected(False)
563                 if self.getSelected():
564                         self.setSelected(False)
565                 else:
566                         self.setSelected(True)
567                 self._radioCallback(button)
568
569 class glComboButton(glButton):
570         def __init__(self, parent, tooltip, imageIDs, tooltips, pos, callback):
571                 super(glComboButton, self).__init__(parent, imageIDs[0], tooltip, pos, self._onComboOpenSelect)
572                 self._imageIDs = imageIDs
573                 self._tooltips = tooltips
574                 self._comboCallback = callback
575                 self._selection = 0
576
577         def _onComboOpenSelect(self, button):
578                 if self.hasFocus():
579                         self._base._focus = None
580                 else:
581                         self._base._focus = self
582
583         def draw(self):
584                 if self._hidden:
585                         return
586                 self._selected = self.hasFocus()
587                 super(glComboButton, self).draw()
588
589                 bs = self._base._buttonSize / 2
590                 pos = self._getPixelPos()
591
592                 if not self._selected:
593                         return
594
595                 glPushMatrix()
596                 glTranslatef(pos[0]+bs*0.5, pos[1] + bs*0.5, 0)
597                 glBindTexture(GL_TEXTURE_2D, self._base._glButtonsTexture)
598                 for n in xrange(0, len(self._imageIDs)):
599                         glTranslatef(0, bs, 0)
600                         glColor4ub(255,255,255,255)
601                         opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, 0)
602                         opengl.glDrawTexturedQuad(-0.5*bs,-0.5*bs,bs,bs, self._imageIDs[n])
603                         glDisable(GL_TEXTURE_2D)
604
605                         glPushMatrix()
606                         glTranslatef(-0.55*bs, 0.1*bs, 0)
607
608                         glPushMatrix()
609                         glColor4ub(60,60,60,255)
610                         glTranslatef(-1, -1, 0)
611                         opengl.glDrawStringRight(self._tooltips[n])
612                         glTranslatef(0, 2, 0)
613                         opengl.glDrawStringRight(self._tooltips[n])
614                         glTranslatef(2, 0, 0)
615                         opengl.glDrawStringRight(self._tooltips[n])
616                         glTranslatef(0, -2, 0)
617                         opengl.glDrawStringRight(self._tooltips[n])
618                         glPopMatrix()
619
620                         glColor4ub(255,255,255,255)
621                         opengl.glDrawStringRight(self._tooltips[n])
622                         glPopMatrix()
623                 glPopMatrix()
624
625         def getValue(self):
626                 return self._selection
627
628         def setValue(self, value):
629                 self._selection = value
630                 self._imageID = self._imageIDs[self._selection]
631                 self._comboCallback()
632
633         def OnMouseDown(self, x, y, button):
634                 if self._hidden or self._disabled:
635                         return False
636                 if self.hasFocus():
637                         bs = self._base._buttonSize / 2
638                         pos = self._getPixelPos()
639                         if 0 <= x - pos[0] <= bs and 0 <= y - pos[1] - bs <= bs * len(self._imageIDs):
640                                 self._selection = int((y - pos[1] - bs) / bs)
641                                 self._imageID = self._imageIDs[self._selection]
642                                 self._base._focus = None
643                                 self._comboCallback()
644                                 return True
645                 return super(glComboButton, self).OnMouseDown(x, y, button)
646
647 class glFrame(glGuiContainer):
648         def __init__(self, parent, pos):
649                 super(glFrame, self).__init__(parent, pos)
650                 self._selected = False
651                 self._focus = False
652                 self._hidden = False
653
654         def setSelected(self, value):
655                 self._selected = value
656
657         def setHidden(self, value):
658                 self._hidden = value
659                 for child in self._glGuiControlList:
660                         if self._base._focus == child:
661                                 self._base._focus = None
662
663         def getSelected(self):
664                 return self._selected
665
666         def getMinSize(self):
667                 return self._base._buttonSize, self._base._buttonSize
668
669         def _getPixelPos(self):
670                 x0, y0, w, h = self.getSize()
671                 return x0, y0
672
673         def draw(self):
674                 if self._hidden:
675                         return
676
677                 bs = self._parent._buttonSize
678                 pos = self._getPixelPos()
679
680                 size = self._layout.getLayoutSize()
681                 glColor4ub(255,255,255,255)
682                 opengl.glDrawStretchedQuad(pos[0], pos[1], size[0], size[1], bs*0.75, 0)
683                 #Draw the controls on the frame
684                 super(glFrame, self).draw()
685
686         def _checkHit(self, x, y):
687                 if self._hidden:
688                         return False
689                 pos = self._getPixelPos()
690                 w, h = self._layout.getLayoutSize()
691                 return 0 <= x - pos[0] <= w and 0 <= y - pos[1] <= h
692
693         def OnMouseMotion(self, x, y):
694                 super(glFrame, self).OnMouseMotion(x, y)
695                 if self._checkHit(x, y):
696                         self._focus = True
697                         return True
698                 self._focus = False
699                 return False
700
701         def OnMouseDown(self, x, y, button):
702                 if self._checkHit(x, y):
703                         super(glFrame, self).OnMouseDown(x, y, button)
704                         return True
705                 return False
706
707 class glNotification(glFrame):
708         def __init__(self, parent, pos):
709                 self._anim = None
710                 super(glNotification, self).__init__(parent, pos)
711                 glGuiLayoutGrid(self)._alignBottom = False
712                 self._label = glLabel(self, "Notification", (0, 0))
713                 self._buttonExtra = glButton(self, 31, "???", (1, 0), self.onExtraButton, 25)
714                 self._button = glButton(self, 30, "", (2, 0), self.onClose, 25)
715                 self._padding = glLabel(self, "", (0, 1))
716                 self.setHidden(True)
717
718         def setSize(self, x, y, w, h):
719                 w, h = self._layout.getLayoutSize()
720                 baseSize = self._base.GetSizeTuple()
721                 if self._anim is not None:
722                         super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._anim.getPosition() - self._base._buttonSize * 0.2, 1, 1)
723                 else:
724                         super(glNotification, self).setSize(baseSize[0] / 2 - w / 2, baseSize[1] - self._base._buttonSize * 0.2, 1, 1)
725
726         def draw(self):
727                 self.setSize(0,0,0,0)
728                 self.updateLayout()
729                 super(glNotification, self).draw()
730
731         def message(self, text, extraButtonCallback = None, extraButtonIcon = None, extraButtonTooltip = None):
732                 self._anim = animation(self._base, -20, 25, 1)
733                 self.setHidden(False)
734                 self._label.setLabel(text)
735                 self._buttonExtra.setHidden(extraButtonCallback is None)
736                 self._buttonExtra._imageID = extraButtonIcon
737                 self._buttonExtra._tooltip = extraButtonTooltip
738                 self._extraButtonCallback = extraButtonCallback
739                 self._base._queueRefresh()
740                 self.updateLayout()
741
742         def onExtraButton(self, button):
743                 self.onClose(button)
744                 self._extraButtonCallback()
745
746         def onClose(self, button):
747                 if self._anim is not None:
748                         self._anim = animation(self._base, self._anim.getPosition(), -20, 1)
749                 else:
750                         self._anim = animation(self._base, 25, -20, 1)
751
752 class glLabel(glGuiControl):
753         def __init__(self, parent, label, pos):
754                 self._label = label
755                 super(glLabel, self).__init__(parent, pos)
756
757         def setLabel(self, label):
758                 self._label = label
759
760         def getMinSize(self):
761                 w, h = opengl.glGetStringSize(self._label)
762                 return w + 10, h + 4
763
764         def _getPixelPos(self):
765                 x0, y0, w, h = self.getSize()
766                 return x0, y0
767
768         def draw(self):
769                 x, y, w, h = self.getSize()
770
771                 glPushMatrix()
772                 glTranslatef(x, y, 0)
773
774 #               glColor4ub(255,255,255,128)
775 #               glBegin(GL_QUADS)
776 #               glTexCoord2f(1, 0)
777 #               glVertex2f( w, 0)
778 #               glTexCoord2f(0, 0)
779 #               glVertex2f( 0, 0)
780 #               glTexCoord2f(0, 1)
781 #               glVertex2f( 0, h)
782 #               glTexCoord2f(1, 1)
783 #               glVertex2f( w, h)
784 #               glEnd()
785
786                 glTranslate(5, h - 5, 0)
787                 glColor4ub(255,255,255,255)
788                 opengl.glDrawStringLeft(self._label)
789                 glPopMatrix()
790
791         def _checkHit(self, x, y):
792                 return False
793
794         def OnMouseMotion(self, x, y):
795                 return False
796
797         def OnMouseDown(self, x, y, button):
798                 return False
799
800 class glNumberCtrl(glGuiControl):
801         def __init__(self, parent, value, pos, callback):
802                 self._callback = callback
803                 self._value = str(value)
804                 self._selectPos = 0
805                 self._maxLen = 6
806                 self._inCallback = False
807                 super(glNumberCtrl, self).__init__(parent, pos)
808
809         def setValue(self, value):
810                 if self._inCallback:
811                         return
812                 self._value = str(value)
813
814         def getMinSize(self):
815                 w, h = opengl.glGetStringSize("VALUES")
816                 return w + 10, h + 4
817
818         def _getPixelPos(self):
819                 x0, y0, w, h = self.getSize()
820                 return x0, y0
821
822         def draw(self):
823                 x, y, w, h = self.getSize()
824
825                 glPushMatrix()
826                 glTranslatef(x, y, 0)
827
828                 if self.hasFocus():
829                         glColor4ub(255,255,255,255)
830                 else:
831                         glColor4ub(255,255,255,192)
832                 glBegin(GL_QUADS)
833                 glTexCoord2f(1, 0)
834                 glVertex2f( w, 0)
835                 glTexCoord2f(0, 0)
836                 glVertex2f( 0, 0)
837                 glTexCoord2f(0, 1)
838                 glVertex2f( 0, h-1)
839                 glTexCoord2f(1, 1)
840                 glVertex2f( w, h-1)
841                 glEnd()
842
843                 glTranslate(5, h - 5, 0)
844                 glColor4ub(0,0,0,255)
845                 opengl.glDrawStringLeft(self._value)
846                 if self.hasFocus():
847                         glTranslate(opengl.glGetStringSize(self._value[0:self._selectPos])[0] - 2, -1, 0)
848                         opengl.glDrawStringLeft('|')
849                 glPopMatrix()
850
851         def _checkHit(self, x, y):
852                 x1, y1, w, h = self.getSize()
853                 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
854
855         def OnMouseMotion(self, x, y):
856                 return False
857
858         def OnMouseDown(self, x, y, button):
859                 if self._checkHit(x, y):
860                         self.setFocus()
861                         return True
862                 return False
863
864         def OnKeyChar(self, c):
865                 self._inCallback = True
866                 if c == wx.WXK_LEFT:
867                         self._selectPos -= 1
868                         self._selectPos = max(0, self._selectPos)
869                 if c == wx.WXK_RIGHT:
870                         self._selectPos += 1
871                         self._selectPos = min(self._selectPos, len(self._value))
872                 if c == wx.WXK_UP:
873                         try:
874                                 value = float(self._value)
875                         except:
876                                 pass
877                         else:
878                                 value += 0.1
879                                 self._value = str(value)
880                                 self._callback(self._value)
881                 if c == wx.WXK_DOWN:
882                         try:
883                                 value = float(self._value)
884                         except:
885                                 pass
886                         else:
887                                 value -= 0.1
888                                 if value > 0:
889                                         self._value = str(value)
890                                         self._callback(self._value)
891                 if c == wx.WXK_BACK and self._selectPos > 0:
892                         self._value = self._value[0:self._selectPos - 1] + self._value[self._selectPos:]
893                         self._selectPos -= 1
894                         self._callback(self._value)
895                 if c == wx.WXK_DELETE:
896                         self._value = self._value[0:self._selectPos] + self._value[self._selectPos + 1:]
897                         self._callback(self._value)
898                 if c == wx.WXK_TAB or c == wx.WXK_NUMPAD_ENTER or c == wx.WXK_RETURN:
899                         if wx.GetKeyState(wx.WXK_SHIFT):
900                                 self.focusPrevious()
901                         else:
902                                 self.focusNext()
903                 if (ord('0') <= c <= ord('9') or c == ord('.')) and len(self._value) < self._maxLen:
904                         self._value = self._value[0:self._selectPos] + chr(c) + self._value[self._selectPos:]
905                         self._selectPos += 1
906                         self._callback(self._value)
907                 self._inCallback = False
908
909         def setFocus(self):
910                 self._base._focus = self
911                 self._selectPos = len(self._value)
912                 return True
913
914 class glCheckbox(glGuiControl):
915         def __init__(self, parent, value, pos, callback):
916                 self._callback = callback
917                 self._value = value
918                 self._selectPos = 0
919                 self._maxLen = 6
920                 self._inCallback = False
921                 super(glCheckbox, self).__init__(parent, pos)
922
923         def setValue(self, value):
924                 if self._inCallback:
925                         return
926                 self._value = str(value)
927
928         def getValue(self):
929                 return self._value
930
931         def getMinSize(self):
932                 return 20, 20
933
934         def _getPixelPos(self):
935                 x0, y0, w, h = self.getSize()
936                 return x0, y0
937
938         def draw(self):
939                 x, y, w, h = self.getSize()
940
941                 glPushMatrix()
942                 glTranslatef(x, y, 0)
943
944                 glColor3ub(255,255,255)
945                 if self._value:
946                         opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 28)
947                 else:
948                         opengl.glDrawTexturedQuad(w/2-h/2,0, h, h, 29)
949
950                 glPopMatrix()
951
952         def _checkHit(self, x, y):
953                 x1, y1, w, h = self.getSize()
954                 return 0 <= x - x1 <= w and 0 <= y - y1 <= h
955
956         def OnMouseMotion(self, x, y):
957                 return False
958
959         def OnMouseDown(self, x, y, button):
960                 if self._checkHit(x, y):
961                         self._value = not self._value
962                         return True
963                 return False
964
965 class glSlider(glGuiControl):
966         def __init__(self, parent, value, minValue, maxValue, pos, callback):
967                 super(glSlider, self).__init__(parent, pos)
968                 self._callback = callback
969                 self._focus = False
970                 self._hidden = False
971                 self._value = value
972                 self._minValue = minValue
973                 self._maxValue = maxValue
974
975         def setValue(self, value):
976                 self._value = value
977
978         def getValue(self):
979                 if self._value < self._minValue:
980                         return self._minValue
981                 if self._value > self._maxValue:
982                         return self._maxValue
983                 return self._value
984
985         def setRange(self, minValue, maxValue):
986                 if maxValue < minValue:
987                         maxValue = minValue
988                 self._minValue = minValue
989                 self._maxValue = maxValue
990
991         def getMinValue(self):
992                 return self._minValue
993
994         def getMaxValue(self):
995                 return self._maxValue
996
997         def setHidden(self, value):
998                 self._hidden = value
999
1000         def getMinSize(self):
1001                 return self._base._buttonSize * 0.2, self._base._buttonSize * 4
1002
1003         def _getPixelPos(self):
1004                 x0, y0, w, h = self.getSize()
1005                 minSize = self.getMinSize()
1006                 return x0 + w / 2 - minSize[0] / 2, y0 + h / 2 - minSize[1] / 2
1007
1008         def draw(self):
1009                 if self._hidden:
1010                         return
1011
1012                 w, h = self.getMinSize()
1013                 pos = self._getPixelPos()
1014
1015                 glPushMatrix()
1016                 glTranslatef(pos[0], pos[1], 0)
1017                 glDisable(GL_TEXTURE_2D)
1018                 if self.hasFocus():
1019                         glColor4ub(60,60,60,255)
1020                 else:
1021                         glColor4ub(60,60,60,192)
1022                 glBegin(GL_QUADS)
1023                 glVertex2f( w/2,-h/2)
1024                 glVertex2f(-w/2,-h/2)
1025                 glVertex2f(-w/2, h/2)
1026                 glVertex2f( w/2, h/2)
1027                 glEnd()
1028                 scrollLength = h - w
1029                 if self._maxValue-self._minValue != 0:
1030                         valueNormalized = ((self.getValue()-self._minValue)/(self._maxValue-self._minValue))
1031                 else:
1032                         valueNormalized = 0
1033                 glTranslate(0.0,scrollLength/2,0)
1034                 if True:  # self._focus:
1035                         glColor4ub(0,0,0,255)
1036                         glPushMatrix()
1037                         glTranslate(-w/2,opengl.glGetStringSize(str(self._minValue))[1]/2,0)
1038                         opengl.glDrawStringRight(str(self._minValue))
1039                         glTranslate(0,-scrollLength,0)
1040                         opengl.glDrawStringRight(str(self._maxValue))
1041                         glTranslate(w,scrollLength-scrollLength*valueNormalized,0)
1042                         opengl.glDrawStringLeft(str(self.getValue()))
1043                         glPopMatrix()
1044                 glColor4ub(255,255,255,240)
1045                 glTranslate(0.0,-scrollLength*valueNormalized,0)
1046                 glBegin(GL_QUADS)
1047                 glVertex2f( w/2,-w/2)
1048                 glVertex2f(-w/2,-w/2)
1049                 glVertex2f(-w/2, w/2)
1050                 glVertex2f( w/2, w/2)
1051                 glEnd()
1052                 glPopMatrix()
1053
1054         def _checkHit(self, x, y):
1055                 if self._hidden:
1056                         return False
1057                 pos = self._getPixelPos()
1058                 w, h = self.getMinSize()
1059                 return -w/2 <= x - pos[0] <= w/2 and -h/2 <= y - pos[1] <= h/2
1060
1061         def setFocus(self):
1062                 self._base._focus = self
1063                 return True
1064
1065         def OnMouseMotion(self, x, y):
1066                 if self.hasFocus():
1067                         w, h = self.getMinSize()
1068                         scrollLength = h - w
1069                         pos = self._getPixelPos()
1070                         self.setValue(int(self._minValue + (self._maxValue - self._minValue) * -(y - pos[1] - scrollLength/2) / scrollLength))
1071                         self._callback()
1072                         return True
1073                 if self._checkHit(x, y):
1074                         self._focus = True
1075                         return True
1076                 self._focus = False
1077                 return False
1078
1079         def OnMouseDown(self, x, y, button):
1080                 if self._checkHit(x, y):
1081                         self.setFocus()
1082                         self.OnMouseMotion(x, y)
1083                         return True
1084                 return False
1085
1086         def OnMouseUp(self, x, y):
1087                 if self.hasFocus():
1088                         self._base._focus = None
1089                         return True
1090                 return False