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