LNXSDK/leenkx/blender/lnx/props_tilesheet.py
2025-01-22 16:18:30 +01:00

277 lines
8.6 KiB
Python

import bpy
from bpy.props import *
class LnxTilesheetActionListItem(bpy.types.PropertyGroup):
name: StringProperty(
name="Name",
description="A name for this item",
default="Untitled")
start_prop: IntProperty(
name="Start",
description="A name for this item",
default=0)
end_prop: IntProperty(
name="End",
description="A name for this item",
default=0)
loop_prop: BoolProperty(
name="Loop",
description="A name for this item",
default=True)
class LNX_UL_TilesheetActionList(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
# We could write some code to decide which icon to use here...
custom_icon = 'OBJECT_DATAMODE'
# Make sure your code supports all 3 layout types
if self.layout_type in {'DEFAULT', 'COMPACT'}:
layout.prop(item, "name", text="", emboss=False, icon=custom_icon)
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text="", icon = custom_icon)
class LnxTilesheetActionListNewItem(bpy.types.Operator):
# Add a new item to the list
bl_idname = "lnx_tilesheetactionlist.new_item"
bl_label = "Add a new item"
def execute(self, context):
wrd = bpy.data.worlds['Lnx']
trait = wrd.lnx_tilesheetlist[wrd.lnx_tilesheetlist_index]
trait.lnx_tilesheetactionlist.add()
trait.lnx_tilesheetactionlist_index = len(trait.lnx_tilesheetactionlist) - 1
return{'FINISHED'}
class LnxTilesheetActionListDeleteItem(bpy.types.Operator):
"""Delete the selected item from the list"""
bl_idname = "lnx_tilesheetactionlist.delete_item"
bl_label = "Deletes an item"
@classmethod
def poll(self, context):
"""Enable if there's something in the list"""
wrd = bpy.data.worlds['Lnx']
if len(wrd.lnx_tilesheetlist) == 0:
return False
trait = wrd.lnx_tilesheetlist[wrd.lnx_tilesheetlist_index]
return len(trait.lnx_tilesheetactionlist) > 0
def execute(self, context):
wrd = bpy.data.worlds['Lnx']
trait = wrd.lnx_tilesheetlist[wrd.lnx_tilesheetlist_index]
list = trait.lnx_tilesheetactionlist
index = trait.lnx_tilesheetactionlist_index
list.remove(index)
if index > 0:
index = index - 1
trait.lnx_tilesheetactionlist_index = index
return{'FINISHED'}
class LnxTilesheetActionListMoveItem(bpy.types.Operator):
"""Move an item in the list"""
bl_idname = "lnx_tilesheetactionlist.move_item"
bl_label = "Move an item in the list"
bl_options = {'INTERNAL'}
direction: EnumProperty(
items=(
('UP', 'Up', ""),
('DOWN', 'Down', "")
))
@classmethod
def poll(self, context):
"""Enable if there's something in the list"""
wrd = bpy.data.worlds['Lnx']
if len(wrd.lnx_tilesheetlist) == 0:
return False
trait = wrd.lnx_tilesheetlist[wrd.lnx_tilesheetlist_index]
return len(trait.lnx_tilesheetactionlist) > 0
def move_index(self):
# Move index of an item render queue while clamping it
wrd = bpy.data.worlds['Lnx']
trait = wrd.lnx_tilesheetlist[wrd.lnx_tilesheetlist_index]
index = trait.lnx_tilesheetactionlist_index
list_length = len(trait.lnx_tilesheetactionlist) - 1
new_index = 0
if self.direction == 'UP':
new_index = index - 1
elif self.direction == 'DOWN':
new_index = index + 1
new_index = max(0, min(new_index, list_length))
trait.lnx_tilesheetactionlist.move(index, new_index)
trait.lnx_tilesheetactionlist_index = new_index
def execute(self, context):
wrd = bpy.data.worlds['Lnx']
trait = wrd.lnx_tilesheetlist[wrd.lnx_tilesheetlist_index]
list = trait.lnx_tilesheetactionlist
index = trait.lnx_tilesheetactionlist_index
if self.direction == 'DOWN':
neighbor = index + 1
self.move_index()
elif self.direction == 'UP':
neighbor = index - 1
self.move_index()
else:
return{'CANCELLED'}
return{'FINISHED'}
class LnxTilesheetListItem(bpy.types.PropertyGroup):
name: StringProperty(
name="Name",
description="A name for this item",
default="Untitled")
tilesx_prop: IntProperty(
name="Tiles X",
description="A name for this item",
default=0)
tilesy_prop: IntProperty(
name="Tiles Y",
description="A name for this item",
default=0)
framerate_prop: FloatProperty(
name="Frame Rate",
description="A name for this item",
default=4.0)
lnx_tilesheetactionlist: CollectionProperty(type=LnxTilesheetActionListItem)
lnx_tilesheetactionlist_index: IntProperty(name="Index for lnx_tilesheetactionlist", default=0)
class LNX_UL_TilesheetList(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
# We could write some code to decide which icon to use here...
custom_icon = 'OBJECT_DATAMODE'
# Make sure your code supports all 3 layout types
if self.layout_type in {'DEFAULT', 'COMPACT'}:
layout.prop(item, "name", text="", emboss=False, icon=custom_icon)
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text="", icon=custom_icon)
class LnxTilesheetListNewItem(bpy.types.Operator):
"""Add a new item to the list"""
bl_idname = "lnx_tilesheetlist.new_item"
bl_label = "Add a new item"
def execute(self, context):
wrd = bpy.data.worlds['Lnx']
wrd.lnx_tilesheetlist.add()
wrd.lnx_tilesheetlist_index = len(wrd.lnx_tilesheetlist) - 1
return{'FINISHED'}
class LnxTilesheetListDeleteItem(bpy.types.Operator):
"""Delete the selected item from the list"""
bl_idname = "lnx_tilesheetlist.delete_item"
bl_label = "Deletes an item"
@classmethod
def poll(self, context):
""" Enable if there's something in the list """
wrd = bpy.data.worlds['Lnx']
return len(wrd.lnx_tilesheetlist) > 0
def execute(self, context):
wrd = bpy.data.worlds['Lnx']
list = wrd.lnx_tilesheetlist
index = wrd.lnx_tilesheetlist_index
list.remove(index)
if index > 0:
index = index - 1
wrd.lnx_tilesheetlist_index = index
return{'FINISHED'}
class LnxTilesheetListMoveItem(bpy.types.Operator):
"""Move an item in the list"""
bl_idname = "lnx_tilesheetlist.move_item"
bl_label = "Move an item in the list"
bl_options = {'INTERNAL'}
direction: EnumProperty(
items=(
('UP', 'Up', ""),
('DOWN', 'Down', "")
))
@classmethod
def poll(self, context):
""" Enable if there's something in the list. """
wrd = bpy.data.worlds['Lnx']
return len(wrd.lnx_tilesheetlist) > 0
def move_index(self):
# Move index of an item render queue while clamping it
wrd = bpy.data.worlds['Lnx']
index = wrd.lnx_tilesheetlist_index
list_length = len(wrd.lnx_tilesheetlist) - 1
new_index = 0
if self.direction == 'UP':
new_index = index - 1
elif self.direction == 'DOWN':
new_index = index + 1
new_index = max(0, min(new_index, list_length))
wrd.lnx_tilesheetlist.move(index, new_index)
wrd.lnx_tilesheetlist_index = new_index
def execute(self, context):
wrd = bpy.data.worlds['Lnx']
list = wrd.lnx_tilesheetlist
index = wrd.lnx_tilesheetlist_index
if self.direction == 'DOWN':
neighbor = index + 1
self.move_index()
elif self.direction == 'UP':
neighbor = index - 1
self.move_index()
else:
return{'CANCELLED'}
return{'FINISHED'}
__REG_CLASSES = (
LnxTilesheetActionListItem,
LNX_UL_TilesheetActionList,
LnxTilesheetActionListNewItem,
LnxTilesheetActionListDeleteItem,
LnxTilesheetActionListMoveItem,
LnxTilesheetListItem,
LNX_UL_TilesheetList,
LnxTilesheetListNewItem,
LnxTilesheetListDeleteItem,
LnxTilesheetListMoveItem,
)
__reg_classes, unregister = bpy.utils.register_classes_factory(__REG_CLASSES)
def register():
__reg_classes()
bpy.types.World.lnx_tilesheetlist = CollectionProperty(type=LnxTilesheetListItem)
bpy.types.World.lnx_tilesheetlist_index = IntProperty(name="Index for lnx_tilesheetlist", default=0)