armory/blender/arm/props_traits_action.py

140 lines
4.4 KiB
Python
Raw Normal View History

2015-10-30 13:23:09 +01:00
import shutil
import bpy
import os
import json
from bpy.types import Menu, Panel, UIList
from bpy.props import *
2016-08-29 09:56:34 +02:00
class ListActionTraitItem(bpy.types.PropertyGroup):
2015-10-30 13:23:09 +01:00
# Group of properties representing an item in the list
name = bpy.props.StringProperty(
name="Name",
description="A name for this item",
2016-08-29 09:56:34 +02:00
default="")
2015-10-30 13:23:09 +01:00
enabled_prop = bpy.props.BoolProperty(
name="",
description="A name for this item",
default=True)
2016-08-29 09:56:34 +02:00
action_name_prop = bpy.props.StringProperty(
name="Action",
2015-10-30 13:23:09 +01:00
description="A name for this item",
2016-08-29 09:56:34 +02:00
default="")
2015-10-30 13:23:09 +01:00
2016-08-29 09:56:34 +02:00
class MY_UL_ActionTraitList(bpy.types.UIList):
2015-10-30 13:23:09 +01:00
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, "enabled_prop")
2016-08-29 09:56:34 +02:00
layout.label(item.name, icon=custom_icon)
# layout.prop(item, "name", text="", emboss=False, icon=custom_icon)
2015-10-30 13:23:09 +01:00
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label("", icon = custom_icon)
2016-08-29 09:56:34 +02:00
class LIST_OT_ActionTraitNewItem(bpy.types.Operator):
2015-10-30 13:23:09 +01:00
# Add a new item to the list
2016-08-29 09:56:34 +02:00
bl_idname = "my_actiontraitlist.new_item"
2015-10-30 13:23:09 +01:00
bl_label = "Add a new item"
def execute(self, context):
2016-08-29 09:56:34 +02:00
trait = context.object.data
trait.my_actiontraitlist.add()
trait.actiontraitlist_index = len(trait.my_actiontraitlist) - 1
2015-10-30 13:23:09 +01:00
return{'FINISHED'}
2016-08-29 09:56:34 +02:00
class LIST_OT_ActionTraitDeleteItem(bpy.types.Operator):
2015-10-30 13:23:09 +01:00
# Delete the selected item from the list
2016-08-29 09:56:34 +02:00
bl_idname = "my_actiontraitlist.delete_item"
2015-10-30 13:23:09 +01:00
bl_label = "Deletes an item"
@classmethod
def poll(self, context):
""" Enable if there's something in the list """
2016-08-29 09:56:34 +02:00
trait = context.object.data
return len(trait.my_actiontraitlist) > 0
2015-10-30 13:23:09 +01:00
def execute(self, context):
2016-08-29 09:56:34 +02:00
trait = context.object.data
list = trait.my_actiontraitlist
index = trait.actiontraitlist_index
2015-10-30 13:23:09 +01:00
list.remove(index)
if index > 0:
index = index - 1
2016-08-29 09:56:34 +02:00
trait.actiontraitlist_index = index
2015-10-30 13:23:09 +01:00
return{'FINISHED'}
2016-08-29 09:56:34 +02:00
class LIST_OT_ActionTraitMoveItem(bpy.types.Operator):
2015-10-30 13:23:09 +01:00
# Move an item in the list
2016-08-29 09:56:34 +02:00
bl_idname = "my_actiontraitlist.move_item"
2015-10-30 13:23:09 +01:00
bl_label = "Move an item in the list"
direction = bpy.props.EnumProperty(
items=(
('UP', 'Up', ""),
('DOWN', 'Down', ""),))
@classmethod
def poll(self, context):
""" Enable if there's something in the list. """
2016-08-29 09:56:34 +02:00
trait = context.object.data
return len(trait.my_actiontraitlist) > 0
2015-10-30 13:23:09 +01:00
def move_index(self):
# Move index of an item render queue while clamping it
2016-08-29 09:56:34 +02:00
trait = context.object.data
index = trait.actiontraitlist_index
list_length = len(trait.my_actiontraitlist) - 1
2015-10-30 13:23:09 +01:00
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))
index = new_index
def execute(self, context):
2016-08-29 09:56:34 +02:00
trait = context.object.data
list = trait.my_actiontraitlist
index = trait.actiontraitlist_index
2015-10-30 13:23:09 +01:00
if self.direction == 'DOWN':
neighbor = index + 1
#queue.move(index,neighbor)
self.move_index()
elif self.direction == 'UP':
neighbor = index - 1
#queue.move(neighbor, index)
self.move_index()
else:
return{'CANCELLED'}
return{'FINISHED'}
2015-12-07 20:04:23 +01:00
def register():
2017-03-15 12:30:14 +01:00
bpy.utils.register_class(ListActionTraitItem)
bpy.utils.register_class(MY_UL_ActionTraitList)
bpy.utils.register_class(LIST_OT_ActionTraitNewItem)
bpy.utils.register_class(LIST_OT_ActionTraitDeleteItem)
bpy.utils.register_class(LIST_OT_ActionTraitMoveItem)
2015-12-07 20:04:23 +01:00
def unregister():
2017-03-15 12:30:14 +01:00
bpy.utils.unregister_class(ListActionTraitItem)
bpy.utils.unregister_class(MY_UL_ActionTraitList)
bpy.utils.unregister_class(LIST_OT_ActionTraitNewItem)
bpy.utils.unregister_class(LIST_OT_ActionTraitDeleteItem)
bpy.utils.unregister_class(LIST_OT_ActionTraitMoveItem)