chiark / gitweb /
Some small fixes suggested by Erik after testing.
[cura.git] / Cura / gui / util / openglGui.py
1 from __future__ import absolute_import
2 from __future__ import division
3
4 import wx
5 from wx import glcanvas
6 import OpenGL
7 OpenGL.ERROR_CHECKING = False
8 from OpenGL.GL import *
9
10 from Cura.gui.util import opengl
11
12 glButtonsTexture = None
13
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)
18                 self._parent = parent
19                 self._context = glcanvas.GLContext(self)
20                 self._glGuiControlList = []
21                 self._glButtonsTexture = None
22                 self._buttonSize = 64
23
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)
29
30         def _OnGuiMouseLeftDown(self,e):
31                 for ctrl in self._glGuiControlList:
32                         if ctrl.OnMouseDown(e.GetX(), e.GetY()):
33                                 return
34                 self.OnMouseLeftDown(e)
35
36         def _OnGuiMouseMotion(self,e):
37                 self.Refresh()
38                 for ctrl in self._glGuiControlList:
39                         if ctrl.OnMouseMotion(e.GetX(), e.GetY()):
40                                 return
41                 self.OnMouseMotion(e)
42
43         def _OnGuiPaint(self, e):
44                 h = self.GetSize().GetHeight()
45                 w = self.GetSize().GetWidth()
46                 if h / 3 > w / 4:
47                         w = h * 4 / 3
48                 if w < 64 * 10:
49                         self._buttonSize = 48
50                 elif w < 64 * 15:
51                         self._buttonSize = 64
52                 elif w < 64 * 20:
53                         self._buttonSize = 80
54                 else:
55                         self._buttonSize = 96
56
57                 dc = wx.PaintDC(self)
58                 self.SetCurrent(self._context)
59                 self.OnPaint(e)
60                 self._drawGui()
61                 glFlush()
62                 self.SwapBuffers()
63
64         def _drawGui(self):
65                 glDisable(GL_DEPTH_TEST)
66                 glEnable(GL_BLEND)
67                 glDisable(GL_LIGHTING)
68                 glColor4ub(255,255,255,255)
69
70                 glMatrixMode(GL_PROJECTION)
71                 glLoadIdentity()
72                 size = self.GetSize()
73                 glOrtho(0, size.GetWidth()-1, size.GetHeight()-1, 0, -1000.0, 1000.0)
74                 glMatrixMode(GL_MODELVIEW)
75                 glLoadIdentity()
76
77                 for glButton in self._glGuiControlList:
78                         glButton.draw()
79
80         def _OnEraseBackground(self,event):
81                 #Workaround for windows background redraw flicker.
82                 pass
83
84         def _OnSize(self,e):
85                 self.Refresh()
86
87         def OnMouseLeftDown(self,e):
88                 pass
89         def OnMouseMotion(self, e):
90                 pass
91         def OnPaint(self, e):
92                 pass
93
94 class glButton(object):
95         def __init__(self, parent, imageID, tooltip, x, y, callback):
96                 self._tooltip = tooltip
97                 self._parent = parent
98                 self._imageID = imageID
99                 self._x = x
100                 self._y = y
101                 self._callback = callback
102                 self._parent._glGuiControlList.append(self)
103                 self._selected = False
104                 self._focus = False
105                 self._hidden = False
106
107         def setSelected(self, value):
108                 self._selected = value
109
110         def setHidden(self, value):
111                 self._hidden = value
112
113         def getSelected(self):
114                 return self._selected
115
116         def _getSize(self):
117                 return self._parent._buttonSize
118
119         def _getPixelPos(self):
120                 bs = self._getSize()
121                 x = self._x * bs * 1.3 + bs * 0.8
122                 y = self._y * bs * 1.3 + bs * 0.8
123                 if self._x < 0:
124                         x = self._parent.GetSize().GetWidth() + x - bs * 0.2
125                 return x, y
126
127         def draw(self):
128                 if self._hidden:
129                         return
130                 if self._parent._glButtonsTexture is None:
131                         self._parent._glButtonsTexture = opengl.loadGLTexture('glButtons.png')
132
133                 cx = (self._imageID % 4) / 4
134                 cy = int(self._imageID / 4) / 4
135                 bs = self._parent._buttonSize
136                 pos = self._getPixelPos()
137
138                 glPushMatrix()
139                 glTranslatef(pos[0], pos[1], 0)
140                 glBindTexture(GL_TEXTURE_2D, self._parent._glButtonsTexture)
141                 glEnable(GL_TEXTURE_2D)
142                 scale = 0.8
143                 if self._selected:
144                         scale = 1.0
145                 elif self._focus:
146                         scale = 0.9
147                 glScalef(bs * scale, bs * scale, bs * scale)
148                 glColor4ub(255,255,255,255)
149                 glBegin(GL_QUADS)
150                 glTexCoord2f(cx+0.25, cy)
151                 glVertex2f( 0.5,-0.5)
152                 glTexCoord2f(cx, cy)
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)
158                 glEnd()
159                 glDisable(GL_TEXTURE_2D)
160                 if self._focus:
161                         glColor4ub(0,0,0,255)
162                         glTranslatef(0, -0.55, 0)
163                         opengl.glDrawStringCenter(self._tooltip)
164                 glPopMatrix()
165
166         def _checkHit(self, x, y):
167                 if self._hidden:
168                         return False
169                 bs = self._getSize()
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
172
173         def OnMouseMotion(self, x, y):
174                 if self._checkHit(x, y):
175                         self._focus = True
176                         return True
177                 self._focus = False
178                 return False
179
180         def OnMouseDown(self, x, y):
181                 if self._checkHit(x, y):
182                         self._callback()
183                         return True
184                 return False