forked from jayanam/bl_ui_widgets
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bl_ui_patch.py
179 lines (144 loc) · 6 KB
/
bl_ui_patch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# --- ### Header
bl_info = {"name": "BL UI Widgets",
"description": "UI Widgets to draw in the 3D view",
"author": "Marcelo M. Marques",
"version": (1, 0, 1),
"blender": (2, 80, 75),
"location": "View3D > viewport area",
"support": "COMMUNITY",
"category": "3D View",
"warning": "",
"doc_url": "https://github.com/mmmrqs/bl_ui_widgets",
"tracker_url": "https://github.com/mmmrqs/bl_ui_widgets/issues"
}
# --- ### Change log
# v1.0.1 (09.20.2021) - by Marcelo M. Marques
# Chang: just some pep8 code formatting
# v1.0.0 (09.01.2021) - by Marcelo M. Marques
# Added: initial creation
# Added: This new class to paint custom rectangles on screen. Useful for creating header and subpanel areas.
# It is used as a base class for the following widgets:
# {'BL_UI_Drag_Panel','BL_UI_Button','BL_UI_Slider','BL_UI_Checkbox','BL_UI_Tooltip'}
# --- ### Imports
import bpy
import time
from . bl_ui_widget import BL_UI_Widget
class BL_UI_Patch(BL_UI_Widget):
def __init__(self, x, y, width, height):
super().__init__(x, y, width, height)
# Note: '_style' value will always be ignored if the bg_color value is overriden after object initialization.
self._style = 'NONE' # Patch background color styles are: {HEADER,PANEL,SUBPANEL,BOX,TOOLTIP,NONE}
self._bg_color = None # Patch background color (defaults to invisible)
self._shadow_color = None # Panel shadow color (defaults to invisible)
self._outline_color = None # Panel outline color (defaults to invisible)
self._roundness = 0 # Patch corners roundness factor [0..1]
self._radius = 0 # Patch corners circular radius
self._rounded_corners = (0, 0, 0, 0) # 1=Round/0=Straight, coords:(bottomLeft,topLeft,topRight,bottomRight)
self._has_shadow = False # Indicates whether a shadow must be drawn around the patch
self._image = None # Image file to be loaded
self._image_size = (24, 24) # Image size in pixels; values are (width, height)
self._image_position = (4, 2) # Image position inside the patch area; values are (x, y)
self.__image_file = None
self.__image_time = 0
@property
def bg_color(self):
return self._bg_color
@bg_color.setter
def bg_color(self, value):
self._bg_color = value
@property
def shadow_color(self):
return self._shadow_color
@shadow_color.setter
def shadow_color(self, value):
self._shadow_color = value
@property
def outline_color(self):
return self._outline_color
@outline_color.setter
def outline_color(self, value):
self._outline_color = value
@property
def roundness(self):
return self._roundness
@roundness.setter
def roundness(self, value):
if value is None:
self._roundness = None
elif value < 0:
self._roundness = 0.0
elif value > 1:
self._roundness = 1.0
else:
self._roundness = value
@property
def corner_radius(self):
return self._radius
@corner_radius.setter
def corner_radius(self, value):
self._radius = value
@property
def rounded_corners(self):
return self._rounded_corners
@rounded_corners.setter
def rounded_corners(self, value):
self._rounded_corners = value
@property
def shadow(self):
return self._has_shadow
@shadow.setter
def shadow(self, value):
self._has_shadow = value
def set_image_size(self, image_size):
self._image_size = image_size
def set_image_position(self, image_position):
self._image_position = image_position
def set_image(self, rel_filepath):
self.__image_file = rel_filepath
self.__image_time = time.time()
try:
self._image = bpy.data.images.load(self.__image_file, check_existing=True)
self._image.gl_load()
self._image.pack(as_png=True)
except Exception as e:
pass
# Overrides base class function
def is_in_rect(self, x, y):
"""
The statement with super() is equivalent to writing either one of the following,
but with the advantage of not having the class name hard coded.
- if type(self).__name__ == "BL_UI_Patch":
- if type(self) is BL_UI_Patch:
"""
# This distincts whether it is the Base or a Derived class
if super().__self_class__ is super().__thisclass__:
# This object type must not react to mouse events
return False
else:
return super().is_in_rect(x, y)
# Overrides base class function
def draw(self):
super().draw()
if not self._is_visible:
return
# Attempt to refresh the image because it has an issue that causes it to black out after a while
if self._image is not None:
if time.time() - self.__image_time >= 10:
self.set_image(self.__image_file)