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