-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCONFIG_UI.py
117 lines (91 loc) · 3.76 KB
/
DCONFIG_UI.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
# ------------------------------------------------------------
# Copyright(c) 2018-2020 Jesse Yurkovich
# Licensed under the MIT License <http://opensource.org/licenses/MIT>.
# See the LICENSE file in the repo root for full license information.
# ------------------------------------------------------------
#
# Misc UI
#
import re
import blf
import bpy
def draw_stats(font_id, line_height, ui_scale):
view_layer = bpy.context.view_layer
if bpy.context.space_data.overlay.show_stats:
bpy.context.space_data.overlay.show_stats = False
# Gather up stats...
stats = bpy.context.scene.statistics(view_layer).split("|")
if bpy.context.mode == 'OBJECT':
all_count = 8 if bpy.app.version < (3, 6, 0) else 9
if len(stats) == all_count:
stats = [stats[5], stats[2], stats[3]]
else:
stats = [stats[4], stats[1], stats[2]]
elif bpy.context.mode == 'EDIT_MESH':
stats = [stats[5], stats[1], stats[2], stats[3]]
elif bpy.context.mode == 'EDIT_CURVE':
stats = [stats[2], stats[1]]
elif bpy.context.mode == 'EDIT_LATTICE':
stats = [stats[2], stats[1]]
elif bpy.context.mode == 'SCULPT':
stats = [stats[3], stats[1], stats[2]]
else:
return
# Initial positions and offsets to handle tool region and top text...
toolbar_width = next((region.width for region in bpy.context.area.regions if region.type == 'TOOLS'), 100)
top_offset = line_height * 10
x_pos = (10 * ui_scale) + toolbar_width
y_pos = bpy.context.area.height - ((26 * ui_scale) if bpy.context.space_data.show_region_tool_header else 0) - top_offset
digit_width = blf.dimensions(font_id, "0")[0]
longest_digits = digit_width * 10
longest_title = 0
# Calculate dimensions for each piece of data...
class Item:
def __init__(self, text, font_id):
self.text = text
self.dim_x = blf.dimensions(font_id, text)[0]
lines = []
for value in stats:
line_data = [Item(val, font_id) for val in filter(None, re.split("[ :/]", value))]
longest_title = max(longest_title, line_data[0].dim_x)
lines.append(line_data)
# Aligned layout using dimensions above (special case first piece of data for the title)...
blf.color(font_id, 1, 1, 1, 1)
for line_index, line in enumerate(lines):
x = x_pos + longest_title
for item_index, item in enumerate(line):
if item_index > 0:
x += longest_digits
blf.position(font_id, x - item.dim_x, y_pos, 0)
blf.draw(font_id, item.text.replace(",", "\u2009"))
if line_index == 0:
y_pos -= line_height / 2
y_pos -= line_height
def draw_func(ignore):
# Only draw when allowed...
space_data = bpy.context.space_data
if not (space_data.overlay.show_overlays and space_data.overlay.show_text):
return
# Setup font and scaling parameters...
font_id = draw_settings["font_id"]
font_size = draw_settings["font_size"]
ui_scale = bpy.context.preferences.system.ui_scale
if bpy.app.version < (3, 4, 0):
blf.size(font_id, round(font_size * ui_scale), 72)
else:
blf.size(font_id, round(font_size * ui_scale))
blf.enable(font_id, blf.SHADOW)
blf.shadow(font_id, 5, 0.0, 0.0, 0.0, 0.9)
blf.shadow_offset(font_id, 1, -1)
line_height = blf.dimensions(font_id, "M")[1] * 1.55
# Draw all the things...
draw_stats(font_id, line_height, ui_scale)
draw_settings = {
"font_id": 0,
"font_size": 11,
"handler": None
}
def register():
draw_settings["handler"] = bpy.types.SpaceView3D.draw_handler_add(draw_func, (None, ), 'WINDOW', 'POST_PIXEL')
def unregister():
bpy.types.SpaceView3D.draw_handler_remove(draw_settings["handler"], 'WINDOW')