chiark / gitweb /
Split off some code from preview3d.py, which is becoming way to large.
[cura.git] / Cura / gui / util / openglGui.py
1 from __future__ import absolute_import
2 from __future__ import division
3
4 import OpenGL
5 OpenGL.ERROR_CHECKING = False
6 from OpenGL.GL import *
7
8 from Cura.gui.util import opengl
9
10 glButtonsTexture = None
11
12 class glButton(object):
13         def __init__(self, parent, imageID, x, y, callback):
14                 self._parent = parent
15                 self._imageID = imageID
16                 self._x = x
17                 self._y = y
18                 self._callback = callback
19                 self._parent.glButtonList.append(self)
20                 self._selected = False
21                 self._focus = False
22                 self._hidden = False
23
24         def setSelected(self, value):
25                 self._selected = value
26
27         def setHidden(self, value):
28                 self._hidden = value
29
30         def getSelected(self):
31                 return self._selected
32
33         def draw(self):
34                 global glButtonsTexture
35                 if self._hidden:
36                         return
37                 if glButtonsTexture is None:
38                         glButtonsTexture = opengl.loadGLTexture('glButtons.png')
39
40                 cx = (self._imageID % 4) / 4
41                 cy = int(self._imageID / 4) / 4
42                 bs = self._parent.buttonSize
43
44                 glPushMatrix()
45                 glTranslatef(self._x * bs * 1.3 + bs * 0.8, self._y * bs * 1.3 + bs * 0.8, 0)
46                 glBindTexture(GL_TEXTURE_2D, glButtonsTexture)
47                 glEnable(GL_TEXTURE_2D)
48                 scale = 0.8
49                 if self._selected:
50                         scale = 1.0
51                 elif self._focus:
52                         scale = 0.9
53                 glScalef(bs * scale, bs * scale, bs * scale)
54                 glBegin(GL_QUADS)
55                 glTexCoord2f(cx+0.25, cy)
56                 glVertex2f( 0.5,-0.5)
57                 glTexCoord2f(cx, cy)
58                 glVertex2f(-0.5,-0.5)
59                 glTexCoord2f(cx, cy+0.25)
60                 glVertex2f(-0.5, 0.5)
61                 glTexCoord2f(cx+0.25, cy+0.25)
62                 glVertex2f( 0.5, 0.5)
63                 glEnd()
64                 glDisable(GL_TEXTURE_2D)
65                 glPopMatrix()
66
67         def _checkHit(self, x, y):
68                 if self._hidden:
69                         return False
70                 bs = self._parent.buttonSize
71                 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
72
73         def OnMouseMotion(self, x, y):
74                 if self._checkHit(x, y):
75                         self._focus = True
76                         return True
77                 self._focus = False
78                 return False
79
80         def OnMouseDown(self, x, y):
81                 if self._checkHit(x, y):
82                         self._callback()