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):
38 for ctrl in self._glGuiControlList:
39 if ctrl.OnMouseMotion(e.GetX(), e.GetY()):
43 def _OnGuiPaint(self, e):
44 h = self.GetSize().GetHeight()
45 w = self.GetSize().GetWidth()
58 self.SetCurrent(self._context)
65 glDisable(GL_DEPTH_TEST)
67 glDisable(GL_LIGHTING)
68 glColor4ub(255,255,255,255)
70 glMatrixMode(GL_PROJECTION)
73 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
74 glMatrixMode(GL_MODELVIEW)
77 for glButton in self._glGuiControlList:
80 def _OnEraseBackground(self,event):
81 #Workaround for windows background redraw flicker.
87 def OnMouseLeftDown(self,e):
89 def OnMouseMotion(self, e):
94 class glButton(object):
95 def __init__(self, parent, imageID, tooltip, x, y, callback):
96 self._tooltip = tooltip
98 self._imageID = imageID
101 self._callback = callback
102 self._parent._glGuiControlList.append(self)
103 self._selected = False
107 def setSelected(self, value):
108 self._selected = value
110 def setHidden(self, value):
113 def getSelected(self):
114 return self._selected
117 return self._parent._buttonSize
119 def _getPixelPos(self):
121 x = self._x * bs * 1.3 + bs * 0.8
122 y = self._y * bs * 1.3 + bs * 0.8
124 x = self._parent.GetSize().GetWidth() + x - bs * 0.2
130 if self._parent._glButtonsTexture is None:
131 self._parent._glButtonsTexture = opengl.loadGLTexture('glButtons.png')
133 cx = (self._imageID % 4) / 4
134 cy = int(self._imageID / 4) / 4
135 bs = self._parent._buttonSize
136 pos = self._getPixelPos()
139 glTranslatef(pos[0], pos[1], 0)
140 glBindTexture(GL_TEXTURE_2D, self._parent._glButtonsTexture)
141 glEnable(GL_TEXTURE_2D)
147 glScalef(bs * scale, bs * scale, bs * scale)
148 glColor4ub(255,255,255,255)
150 glTexCoord2f(cx+0.25, cy)
151 glVertex2f( 0.5,-0.5)
153 glVertex2f(-0.5,-0.5)
154 glTexCoord2f(cx, cy+0.25)
155 glVertex2f(-0.5, 0.5)
156 glTexCoord2f(cx+0.25, cy+0.25)
157 glVertex2f( 0.5, 0.5)
159 glDisable(GL_TEXTURE_2D)
161 glColor4ub(0,0,0,255)
162 glTranslatef(0, -0.55, 0)
163 opengl.glDrawStringCenter(self._tooltip)
166 def _checkHit(self, x, y):
170 pos = self._getPixelPos()
171 return -bs * 0.5 <= x - pos[0] <= bs * 0.5 and -bs * 0.5 <= y - pos[1] <= bs * 0.5
173 def OnMouseMotion(self, x, y):
174 if self._checkHit(x, y):
180 def OnMouseDown(self, x, y):
181 if self._checkHit(x, y):