1from __future__ import absolute_import 2from __future__ import division 3from __future__ import print_function 4from __future__ import unicode_literals 5 6from arcade.arcadetheme import ArcadeTheme 7from fsui.qt import QImage 8 9# import numpy 10# from PIL import Image 11# from fsbc.Application import app 12from arcade.resources import resources 13from arcade.glui.opengl import gl, fs_emu_texturing, fs_emu_blending 14 15 16class Texture(object): 17 shadow = None 18 shadow2 = None 19 gloss = None 20 screen_gloss = None 21 static = None 22 default_item = None 23 missing_screenshot = None 24 missing_cover = None 25 logo = None 26 top = None 27 top_logo = None 28 top_logo_selected = None 29 splash = None 30 31 add = None 32 add_selected = None 33 home = None 34 home_selected = None 35 minimize = None 36 minimize_selected = None 37 close = None 38 close_selected = None 39 shutdown = None 40 shutdown_selected = None 41 42 bottom_bar = None 43 screen_border_1 = None 44 screen_border_2 = None 45 top_background = None 46 top_item = None 47 top_item_selected = None 48 top_item_left = None 49 top_item_left_selected = None 50 top_item_right = None 51 top_item_arrow = None 52 top_item_arrow_selected = None 53 54 glow_top = None 55 glow_top_left = None 56 glow_left = None 57 58 sidebar_background = None 59 sidebar_background_shadow = None 60 heading_strip = None 61 item_background = None 62 top_item_background = None 63 logo_32 = None 64 65 stretch = None 66 aspect = None 67 square_pixels = None 68 69 def __init__(self, name, target=gl.GL_TEXTURE_2D, **kwargs): 70 # print(repr(type(name))) 71 # if isinstance(name, (int, long)): 72 if isinstance(name, int): 73 self.size = kwargs["size"] 74 self.texture = name 75 else: 76 self.size = [0, 0] 77 # print(name, kwargs) 78 out_data = {} 79 self.texture = self.from_theme( 80 name, 81 target=target, 82 size=self.size, 83 out_data=out_data, 84 **kwargs 85 ) 86 self.data = out_data["im_data"] 87 self.gl_type = out_data["type"] 88 self.w, self.h = self.size 89 self.target = target 90 91 def bind(self): 92 gl.glBindTexture(self.target, self.texture) 93 94 def render(self, x, y, w=None, h=None, z=0.0, opacity=1.0): 95 if w is None: 96 w = self.w 97 if h is None: 98 h = self.h 99 self.bind() 100 fs_emu_texturing(True) 101 if self.gl_type == gl.GL_RGBA or opacity < 1.0: 102 fs_emu_blending(True) 103 else: 104 fs_emu_blending(False) 105 gl.glBegin(gl.GL_QUADS) 106 if opacity < 1.0: 107 gl.glColor4f(opacity, opacity, opacity, opacity) 108 else: 109 gl.glColor3f(1.0, 1.0, 1.0) 110 gl.glTexCoord(0.0, 1.0) 111 gl.glVertex3f(x, y, z) 112 gl.glTexCoord(1.0, 1.0) 113 gl.glVertex3f(x + w, y, z) 114 gl.glTexCoord(1.0, 0.0) 115 gl.glVertex3f(x + w, y + h, z) 116 gl.glTexCoord(0.0, 0.0) 117 gl.glVertex3f(x, y + h, z) 118 gl.glEnd() 119 120 @classmethod 121 def load( 122 cls, 123 im, 124 mipmap=False, 125 min_filter=None, 126 wrap_s=gl.GL_CLAMP_TO_EDGE, 127 wrap_t=gl.GL_CLAMP_TO_EDGE, 128 target=gl.GL_TEXTURE_2D, 129 size=None, 130 out_data=None, 131 ): 132 if size is None: 133 size = [0, 0] 134 # type = "RGB" 135 # gl_type = gl.GL_RGB 136 # if im.mode == "RGBA": 137 # # convert to premultiplied alpha 138 # #pix = im.load() 139 # #for x in range(im.size[0]): 140 # # for y in range(im.size[1]): 141 # # r, g, b, a = pix[x, y] 142 # # if a: 143 # # pix[x, y] = r * 255 // a, g * 255 // a, \ 144 # # b * 255 // a, a 145 # # else: 146 # # pix[x, y] = 0, 0, 0, 0 147 # a = numpy.fromstring(im.tostring(), dtype=numpy.uint8) 148 # alpha_layer = a[3::4] / 255.0 149 # a[0::4] *= alpha_layer 150 # a[1::4] *= alpha_layer 151 # a[2::4] *= alpha_layer 152 # #im = Image.fromstring("RGBA", im.size, a.tostring()) 153 # im_data = a.tostring() 154 # # type = "RGBA" 155 # gl_type = gl.GL_RGBA 156 # else: 157 # im_data = im.tostring("raw", "RGB") 158 # 159 # size[0] = im.size[0] 160 # size[1] = im.size[1] 161 # #texture = glGenTextures(1) 162 163 internal_format = gl.GL_RGBA 164 texture_format = gl.GL_BGRA 165 166 if im.format() != QImage.Format_ARGB32_Premultiplied: 167 im = im.convertToFormat(QImage.Format_ARGB32_Premultiplied) 168 169 bits = im.bits() 170 try: 171 pixels = bits.tobytes() 172 except AttributeError: 173 bits.setsize(im.byteCount()) 174 pixels = bytes(bits) 175 size[0] = im.width() 176 size[1] = im.height() 177 178 from arcade.glui.render import Render 179 180 texture = Render.get().create_texture() 181 gl.glBindTexture(target, texture) 182 gl.glTexImage2D( 183 target, 184 0, 185 internal_format, 186 size[0], 187 size[1], 188 0, 189 texture_format, 190 gl.GL_UNSIGNED_BYTE, 191 pixels, 192 ) 193 if mipmap: 194 gl.glGenerateMipmap(target) 195 if min_filter is None: 196 min_filter = gl.GL_LINEAR_MIPMAP_LINEAR 197 else: 198 if min_filter is None: 199 min_filter = gl.GL_LINEAR 200 gl.glTexParameteri(target, gl.GL_TEXTURE_MIN_FILTER, min_filter) 201 gl.glTexParameteri(target, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) 202 gl.glTexParameteri(target, gl.GL_TEXTURE_WRAP_S, wrap_s) 203 gl.glTexParameteri(target, gl.GL_TEXTURE_WRAP_T, wrap_t) 204 if out_data is not None: 205 out_data["im_data"] = pixels 206 out_data["type"] = internal_format 207 return texture 208 209 @classmethod 210 def from_resource(cls, name, size=None, **kwargs): 211 if size is None: 212 size = [0, 0] 213 im = resources.resource_qt_image(name) 214 return cls.load(im, size=size, **kwargs) 215 216 # @classmethod 217 # def from_qimage(cls, name, size=None, **kwargs): 218 # if size is None: 219 # size = [0, 0] 220 # im = resources.resource_qt_image(name) 221 # return cls.load(im, size=size, **kwargs) 222 223 @classmethod 224 def from_theme(cls, name, size=None, **kwargs): 225 if size is None: 226 size = [0, 0] 227 # im = resources.resource_qt_image(name) 228 im = ArcadeTheme.get().qimage(name) 229 return cls.load(im, size=size, **kwargs) 230