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 = []
21 self._glButtonsTexture = None
24 wx.EVT_PAINT(self, self._OnGuiPaint)
25 wx.EVT_SIZE(self, self._OnSize)
26 wx.EVT_ERASE_BACKGROUND(self, self._OnEraseBackground)
27 wx.EVT_LEFT_DOWN(self, self._OnGuiMouseLeftDown)
28 wx.EVT_MOTION(self, self._OnGuiMouseMotion)
30 def _OnGuiMouseLeftDown(self,e):
31 for ctrl in self._glGuiControlList:
32 if ctrl.OnMouseDown(e.GetX(), e.GetY()):
34 self.OnMouseLeftDown(e)
36 def _OnGuiMouseMotion(self,e):
39 for ctrl in self._glGuiControlList:
40 if ctrl.OnMouseMotion(e.GetX(), e.GetY()):
45 def _OnGuiPaint(self, e):
46 h = self.GetSize().GetHeight()
47 w = self.GetSize().GetWidth()
60 self.SetCurrent(self._context)
67 glDisable(GL_DEPTH_TEST)
69 glDisable(GL_LIGHTING)
70 glColor4ub(255,255,255,255)
72 glMatrixMode(GL_PROJECTION)
75 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
76 glMatrixMode(GL_MODELVIEW)
79 for glButton in self._glGuiControlList:
82 def _OnEraseBackground(self,event):
83 #Workaround for windows background redraw flicker.
89 def OnMouseLeftDown(self,e):
91 def OnMouseMotion(self, e):
96 class glButton(object):
97 def __init__(self, parent, imageID, tooltip, x, y, callback):
98 self._tooltip = tooltip
100 self._imageID = imageID
103 self._callback = callback
104 self._parent._glGuiControlList.append(self)
105 self._selected = False
109 def setSelected(self, value):
110 self._selected = value
112 def setHidden(self, value):
115 def getSelected(self):
116 return self._selected
119 return self._parent._buttonSize
121 def _getPixelPos(self):
123 x = self._x * bs * 1.3 + bs * 0.8
124 y = self._y * bs * 1.3 + bs * 0.8
126 x = self._parent.GetSize().GetWidth() + x - bs * 0.2
132 if self._parent._glButtonsTexture is None:
133 self._parent._glButtonsTexture = opengl.loadGLTexture('glButtons.png')
135 cx = (self._imageID % 4) / 4
136 cy = int(self._imageID / 4) / 4
137 bs = self._parent._buttonSize
138 pos = self._getPixelPos()
141 glTranslatef(pos[0], pos[1], 0)
142 glBindTexture(GL_TEXTURE_2D, self._parent._glButtonsTexture)
143 glEnable(GL_TEXTURE_2D)
149 glScalef(bs * scale, bs * scale, bs * scale)
150 glColor4ub(255,255,255,255)
152 glTexCoord2f(cx+0.25, cy)
153 glVertex2f( 0.5,-0.5)
155 glVertex2f(-0.5,-0.5)
156 glTexCoord2f(cx, cy+0.25)
157 glVertex2f(-0.5, 0.5)
158 glTexCoord2f(cx+0.25, cy+0.25)
159 glVertex2f( 0.5, 0.5)
161 glDisable(GL_TEXTURE_2D)
163 glColor4ub(0,0,0,255)
164 glTranslatef(0, -0.55, 0)
165 opengl.glDrawStringCenter(self._tooltip)
168 def _checkHit(self, x, y):
172 pos = self._getPixelPos()
173 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
175 def OnMouseMotion(self, x, y):
176 if self._checkHit(x, y):
182 def OnMouseDown(self, x, y):
183 if self._checkHit(x, y):