BenderKit asset bar: fixed re-initialisation of UI on resize

progress bars are back
ui widgets lib now has a simple non-interactive image object
Updated Oauth scripts
This commit is contained in:
Vilem Duha 2021-10-18 13:30:10 +02:00
parent 0d86d42846
commit 3a2d00cba3
4 changed files with 208 additions and 5 deletions

View File

@ -32,7 +32,7 @@ from bpy.props import (
)
CLIENT_ID = "IdFRwa3SGA8eMpzhRVFMg5Ts8sPK93xBjif93x0F"
PORTS = [62485, 65425, 55428, 49452]
PORTS = [62485, 65425, 55428, 49452, 35452, 25152, 5152, 1234]
active_authenticator = None

View File

@ -0,0 +1,193 @@
from . bl_ui_widget import *
import blf
import bpy
class BL_UI_Button(BL_UI_Widget):
def __init__(self, x, y, width, height):
super().__init__(x, y, width, height)
self._text_color = (1.0, 1.0, 1.0, 1.0)
self._hover_bg_color = (0.5, 0.5, 0.5, 1.0)
self._select_bg_color = (0.7, 0.7, 0.7, 1.0)
self._text = "Button"
self._text_size = 16
self._textpos = (x, y)
self.__state = 0
self.__image = None
self.__image_size = (24, 24)
self.__image_position = (4, 2)
@property
def text_color(self):
return self._text_color
@text_color.setter
def text_color(self, value):
self._text_color = value
@property
def text(self):
return self._text
@text.setter
def text(self, value):
self._text = value
@property
def text_size(self):
return self._text_size
@text_size.setter
def text_size(self, value):
self._text_size = value
@property
def hover_bg_color(self):
return self._hover_bg_color
@hover_bg_color.setter
def hover_bg_color(self, value):
self._hover_bg_color = value
@property
def select_bg_color(self):
return self._select_bg_color
@select_bg_color.setter
def select_bg_color(self, value):
self._select_bg_color = value
def set_image_size(self, imgage_size):
self.__image_size = imgage_size
def set_image_position(self, image_position):
self.__image_position = image_position
def set_image(self, rel_filepath):
try:
self.__image = bpy.data.images.load(rel_filepath, check_existing=True)
self.__image.gl_load()
except:
pass
def update(self, x, y):
super().update(x, y)
self._textpos = [x, y]
def draw(self):
if not self.visible:
return
area_height = self.get_area_height()
self.shader.bind()
self.set_colors()
bgl.glEnable(bgl.GL_BLEND)
self.batch_panel.draw(self.shader)
self.draw_image()
bgl.glDisable(bgl.GL_BLEND)
# Draw text
self.draw_text(area_height)
def set_colors(self):
color = self._bg_color
text_color = self._text_color
# pressed
if self.__state == 1:
color = self._select_bg_color
# hover
elif self.__state == 2:
color = self._hover_bg_color
self.shader.uniform_float("color", color)
def draw_text(self, area_height):
font_id = 1
blf.size(font_id, self._text_size, 72)
size = blf.dimensions(0, self._text)
textpos_y = area_height - self._textpos[1] - (self.height + size[1]) / 2.0
blf.position(font_id, self._textpos[0] + (self.width - size[0]) / 2.0, textpos_y + 1, 0)
r, g, b, a = self._text_color
blf.color(font_id, r, g, b, a)
blf.draw(font_id, self._text)
def draw_image(self):
if self.__image is not None:
try:
y_screen_flip = self.get_area_height() - self.y_screen
off_x, off_y = self.__image_position
sx, sy = self.__image_size
# bottom left, top left, top right, bottom right
vertices = (
(self.x_screen + off_x, y_screen_flip - off_y),
(self.x_screen + off_x, y_screen_flip - sy - off_y),
(self.x_screen + off_x + sx, y_screen_flip - sy - off_y),
(self.x_screen + off_x + sx, y_screen_flip - off_y))
self.shader_img = gpu.shader.from_builtin('2D_IMAGE')
self.batch_img = batch_for_shader(self.shader_img, 'TRI_FAN',
{ "pos" : vertices,
"texCoord": ((0, 1), (0, 0), (1, 0), (1, 1))
},)
# send image to gpu if it isn't there already
if self.__image.gl_load():
raise Exception()
bgl.glActiveTexture(bgl.GL_TEXTURE0)
bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.__image.bindcode)
self.shader_img.bind()
self.shader_img.uniform_int("image", 0)
self.batch_img.draw(self.shader_img)
return True
except:
pass
return False
def set_mouse_down(self, mouse_down_func):
self.mouse_down_func = mouse_down_func
def mouse_down(self, x, y):
if self.is_in_rect(x,y):
self.__state = 1
try:
self.mouse_down_func(self)
except Exception as e:
print(e)
return True
return False
def mouse_move(self, x, y):
if self.is_in_rect(x,y):
if(self.__state != 1):
# hover state
self.__state = 2
else:
self.__state = 0
def mouse_up(self, x, y):
if self.is_in_rect(x,y):
self.__state = 2
else:
self.__state = 0

View File

@ -25,6 +25,10 @@ from urllib.parse import parse_qs, quote as urlquote, urlparse
import requests
class PortsBlockedException(Exception):
pass
class SimpleOAuthAuthenticator(object):
def __init__(self, server_url, client_id, ports):
self.server_url = server_url
@ -55,13 +59,14 @@ class SimpleOAuthAuthenticator(object):
return None, None, None
response_json = json.loads(response.content)
refresh_token = response_json ['refresh_token']
access_token = response_json ['access_token']
refresh_token = response_json['refresh_token']
access_token = response_json['access_token']
return access_token, refresh_token, response_json
def get_new_token(self, register=True, redirect_url=None):
class HTTPServerHandler(BaseHTTPRequestHandler):
html_template = '<html>%(head)s<h1>%(message)s</h1></html>'
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
@ -85,10 +90,15 @@ class SimpleOAuthAuthenticator(object):
for port in self.ports:
try:
httpServer = HTTPServer(('localhost', port), HTTPServerHandler)
except OSError:
except Exception as e:
print(f"Port {port}: {e}")
continue
break
self.redirect_uri = "http://localhost:%s/consumer/exchange/" % port
else:
print("All available ports are blocked")
raise PortsBlockedException(f"All available ports are blocked: {self.ports}")
print(f"Choosen port {port}")
self.redirect_uri = f"http://localhost:{port}/consumer/exchange/"
authorize_url = (
"/o/authorize?client_id=%s&state=random_state_string&response_type=code&"
"redirect_uri=%s" % (self.client_id, self.redirect_uri)

0
blenderkit/reports.py Normal file
View File