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