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