ui redraw on UIlist actions and code cleanup

2.0.9

- fix: prefix/suffix UIlist actions trigger UI redraw to see changes live
- changed: PropertyGroups are now registered in their own file
- code: cleanup
This commit is contained in:
Pullusb
2022-10-13 00:09:12 +02:00
parent c32ea207c6
commit e0a50ea49a
8 changed files with 226 additions and 198 deletions
+9 -154
View File
@@ -4,7 +4,7 @@ bl_info = {
"name": "GP toolbox",
"description": "Tool set for Grease Pencil in animation production",
"author": "Samuel Bernou, Christophe Seux",
"version": (2, 0, 8),
"version": (2, 0, 9),
"blender": (3, 0, 0),
"location": "Sidebar (N menu) > Gpencil > Toolbox / Gpencil properties",
"warning": "",
@@ -43,23 +43,22 @@ from . import OP_layer_picker
from . import OP_layer_nav
from . import OP_material_picker
from . import OP_git_update
from . import OP_layer_namespace
from . import OP_pseudo_tint
# from . import OP_eraser_brush
# from . import TOOL_eraser_brush
from . import handler_draw_cam
from . import keymaps
from .OP_pseudo_tint import GPT_OT_auto_tint_gp_layers
from . import UI_tools
from .properties import (
GP_PG_ToolsSettings,
GP_PG_FixSettings,
GP_PG_namespace_props,
GP_PG_namespaces,
)
from bpy.props import (FloatProperty,
BoolProperty,
EnumProperty,
@@ -109,145 +108,6 @@ def remap_on_save_update(self, context):
# km, kmi = TOOL_eraser_brush.addon_keymaps[0]
# kmi.active = self.use_precise_eraser
class GPTB_OT_add_namespace_entry(bpy.types.Operator):
bl_idname = "gptb.add_namespace_entry"
bl_label = "Add Namespace Entry"
bl_description = "Add item in list"
bl_options = {'REGISTER', 'INTERNAL'}
idx : bpy.props.IntProperty()
new : bpy.props.BoolProperty(default=True, options={'SKIP_SAVE'})
propname : bpy.props.StringProperty(default='prefixes', options={'SKIP_SAVE'})
def invoke(self, context, event):
self.pg = getattr(get_addon_prefs(), self.propname)
self.proptype = self.propname[:-2]
## Basic:
# self.pg.namespaces.add()
# return {'FINISHED'}# can just add empty entry and leave...
if self.new:
self.pg.namespaces.add()
self.idx = len(self.pg.namespaces) - 1
return context.window_manager.invoke_props_dialog(self, width=450)
def draw(self, context):
import re
layout = self.layout
# layout.use_property_split = True
item = self.pg.namespaces[self.idx]
layout.label(text=f'Enter {self.proptype.title()}:', icon='INFO')
layout.prop(item, 'tag', text=self.proptype.title())
if item.tag and not re.match(r'^[A-Z]{2}$', item.tag):
layout.label(text=f'{self.propname.title()} are preferably two capital letter (ex: CO)', icon='ERROR')
layout.separator()
layout.label(text='Provide a name (Optional):', icon='INFO')
layout.prop(item, 'name')
def execute(self, context):
item = self.pg.namespaces[self.idx]
## Here can perform post add checks
# (check for duplicate ?)
# all_prefix = [n.tag for i, n in enumerate(self.pg.namespaces) if i != self.pg.idx]
if self.new:
# in case of new addition, remove just added if nothing specified
if not item.tag and not item.name:
self.pg.namespaces.remove(self.idx)
context.area.tag_redraw()
return {'FINISHED'}
class GPTB_OT_remove_namespace_entry(bpy.types.Operator):
bl_idname = "gptb.remove_namespace_entry"
bl_label = "Remove Namespace Entry"
bl_description = "Remove item in list"
bl_options = {'REGISTER', 'INTERNAL'}
propname : bpy.props.StringProperty(default='prefixes', options={'SKIP_SAVE'})
def execute(self, context):
self.pg = getattr(get_addon_prefs(), self.propname)
entry_count = len(self.pg.namespaces)
if not entry_count:
return {'CANCELLED'}
# check if index is out of range
if not (0 <= self.pg.idx < entry_count):
self.report({"ERROR"}, 'Must select an entry to remove it')
return {'CANCELLED'}
item = self.pg.namespaces[self.pg.idx]
if item.is_project:
self.report({"ERROR"}, 'Cannot remove a prefix that is defined by project, hide it instead')
return {'CANCELLED'}
self.pg.namespaces.remove(self.pg.idx)
self.pg.idx -= 1
context.area.tag_redraw()
return {'FINISHED'}
class GPTB_OT_move_item(bpy.types.Operator):
bl_idname = "gptb.move_item"
bl_label = "Move Item"
bl_description = "Move item in list up or down"
bl_options = {'REGISTER', 'INTERNAL'}
# direction : bpy.props.IntProperty(default=1)
direction : bpy.props.EnumProperty(
items=(
('UP', 'Move Up', 'Move up'),
('DOWN', 'Move down', 'Move down'),
),
default='UP',
)
propname : bpy.props.StringProperty()
def execute(self, context):
pg = getattr(get_addon_prefs(), self.propname)
uilist = pg.namespaces
index = pg.idx
neighbor = index + (-1 if self.direction == 'UP' else 1)
uilist.move(neighbor, index)
list_length = len(uilist) - 1 # (index starts at 0)
new_index = index + (-1 if self.direction == 'UP' else 1)
list_index = max(0, min(new_index, list_length))
setattr(pg, 'idx', list_index)
context.area.tag_redraw()
return {'FINISHED'}
class GPTB_UL_namespace_list(bpy.types.UIList):
# show_desc : BoolProperty(name="Show Description", default=True,
# description="Display Description")
def draw_item(self, context, layout, data, item, icon, active_data, active_propname):
# self.use_filter_show = True # force open/close the search feature
# prefs = get_addon_prefs()
# split = layout.split(align=False, factor=0.3)
row = layout.row()
hide_ico = 'HIDE_ON' if item.hide else 'HIDE_OFF'
source_ico = 'NETWORK_DRIVE' if item.is_project else 'USER' # BLANK1
row.label(text='', icon=source_ico)
row.prop(item, 'hide', text='', icon=hide_ico, invert_checkbox=True)
subrow = row.row(align=True)
subrow.prop(item, 'tag', text='')
subrow.prop(item, 'name', text='')
subrow.enabled = not item.is_project
# row = layout.split(align=False)
# row.label(text=item.prefix)
# row.label(text=item.name)
# if self.show_desc:
# row.label(text=item.description)
# row.operator('sbam.open_online_repo', text='', icon='URL')
class GPTB_prefs(bpy.types.AddonPreferences):
bl_idname = __name__
@@ -883,23 +743,13 @@ class GPTB_set_env_settings(bpy.types.Operator):
classes = (
## layer name management
GP_PG_namespace_props,
GP_PG_namespaces,
GPTB_OT_add_namespace_entry,
GPTB_OT_remove_namespace_entry,
GPTB_OT_move_item,
GPTB_UL_namespace_list,
GP_PG_FixSettings,
GP_PG_ToolsSettings,
GPTB_set_env_settings,
GPTB_prefs,
GPT_OT_auto_tint_gp_layers,
)
addon_modules = (
OP_helpers,
OP_pseudo_tint,
OP_keyframe_jump,
OP_file_checker,
OP_breakdowner,
@@ -916,6 +766,7 @@ addon_modules = (
OP_realign,
OP_depth_move,
OP_key_duplicate_send,
OP_layer_namespace,
OP_layer_manager,
OP_material_picker,
OP_git_update,
@@ -929,6 +780,8 @@ addon_modules = (
)
def register():
# Register property group first
properties.register()
for cls in classes:
bpy.utils.register_class(cls)
@@ -960,6 +813,8 @@ def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
properties.unregister()
del bpy.types.Scene.gptoolprops