1 from __future__ import absolute_import
2 from __future__ import division
5 from wx import glcanvas
7 OpenGL.ERROR_CHECKING = False
8 from OpenGL.GL import *
10 from Cura.gui.util import opengl
12 glButtonsTexture = None
14 class glGuiPanel(glcanvas.GLCanvas):
15 def __init__(self, parent):
16 attribList = (glcanvas.WX_GL_RGBA, glcanvas.WX_GL_DOUBLEBUFFER, glcanvas.WX_GL_DEPTH_SIZE, 24, glcanvas.WX_GL_STENCIL_SIZE, 8)
17 glcanvas.GLCanvas.__init__(self, parent, attribList = attribList)
19 self._context = glcanvas.GLContext(self)
20 self._glGuiControlList = []
23 wx.EVT_PAINT(self, self._OnGuiPaint)
24 wx.EVT_SIZE(self, self._OnSize)
25 wx.EVT_ERASE_BACKGROUND(self, self._OnEraseBackground)
26 wx.EVT_MOUSE_EVENTS(self, self._OnGuiMouseEvents)
27 wx.EVT_MOTION(self, self._OnGuiMouseMotion)
29 def _OnGuiMouseEvents(self,e):
30 if e.ButtonDown() and e.LeftIsDown():
31 for ctrl in self._glGuiControlList:
32 if ctrl.OnMouseDown(e.GetX(), e.GetY()):
35 def _OnGuiMouseMotion(self,e):
37 for ctrl in self._glGuiControlList:
38 if ctrl.OnMouseMotion(e.GetX(), e.GetY()):
42 def _OnGuiPaint(self, e):
44 self.SetCurrent(self._context)
51 glDisable(GL_DEPTH_TEST)
53 glDisable(GL_LIGHTING)
54 glColor4ub(255,255,255,255)
56 glMatrixMode(GL_PROJECTION)
59 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
60 glMatrixMode(GL_MODELVIEW)
63 for glButton in self._glGuiControlList:
66 def _OnEraseBackground(self,event):
67 #Workaround for windows background redraw flicker.
73 def OnMouseEvents(self,e):
76 def OnMouseMotion(self, e):
81 class glButton(object):
82 def __init__(self, parent, imageID, tooltip, x, y, callback):
83 self._tooltip = tooltip
85 self._imageID = imageID
88 self._callback = callback
89 self._parent._glGuiControlList.append(self)
90 self._selected = False
94 def setSelected(self, value):
95 self._selected = value
97 def setHidden(self, value):
100 def getSelected(self):
101 return self._selected
104 global glButtonsTexture
107 if glButtonsTexture is None:
108 glButtonsTexture = opengl.loadGLTexture('glButtons.png')
110 cx = (self._imageID % 4) / 4
111 cy = int(self._imageID / 4) / 4
112 bs = self._parent._buttonSize
115 glTranslatef(self._x * bs * 1.3 + bs * 0.8, self._y * bs * 1.3 + bs * 0.8, 0)
116 glBindTexture(GL_TEXTURE_2D, glButtonsTexture)
117 glEnable(GL_TEXTURE_2D)
123 glScalef(bs * scale, bs * scale, bs * scale)
124 glColor4ub(255,255,255,255)
126 glTexCoord2f(cx+0.25, cy)
127 glVertex2f( 0.5,-0.5)
129 glVertex2f(-0.5,-0.5)
130 glTexCoord2f(cx, cy+0.25)
131 glVertex2f(-0.5, 0.5)
132 glTexCoord2f(cx+0.25, cy+0.25)
133 glVertex2f( 0.5, 0.5)
135 glDisable(GL_TEXTURE_2D)
137 glColor4ub(0,0,0,255)
138 glTranslatef(0, -0.55, 0)
139 opengl.glDrawStringCenter(self._tooltip)
142 def _checkHit(self, x, y):
145 bs = self._parent._buttonSize
146 return -bs * 0.5 <= x - (self._x * bs * 1.3 + bs * 0.8) <= bs * 0.5 and -bs * 0.5 <= y - (self._y * bs * 1.3 + bs * 0.8) <= bs * 0.5
148 def OnMouseMotion(self, x, y):
149 if self._checkHit(x, y):
155 def OnMouseDown(self, x, y):
156 if self._checkHit(x, y):