#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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#END GPL LICENCE BLOCK

bl_info = {
    "name": "Btrace",
    "author": "liero, crazycourier, Atom, Meta-Androcto, MacKracken",
    "version": (1, 1, ),
    "blender": (2, 71, 0),
    "location": "View3D > Toolshelf > Addons Tab",
    "description": "Tools for converting/animating objects/particles into curves",
    "warning": "Still under development, bug reports appreciated",
    "wiki_url": "",
    "tracker_url": "https://developer.blender.org/T29563",
    "category": "Add Curve"}


#### TO DO LIST ####
### [   ]  Add more options to curve radius/modulation plus cyclic/connect curve option

import bpy
import selection_utils
from bpy.props import FloatProperty, EnumProperty, IntProperty, BoolProperty, FloatVectorProperty


def deselect_others(ob, context):
    """For tool menu select, deselects others if one selected"""
    selected = addTracerObjectPanel.selected
    ob[selected] = False
    keys = [key for key in ob.keys() if ob[key]]  # all the True keys
    if len(keys) <= 0:
        ob[selected] = True  # reselect
        return None
    for key in keys:
        addTracerObjectPanel.selected = key
        ob[key] = True


# Class for properties panel
class TracerPropertiesMenu(bpy.types.PropertyGroup):
    """Toolbar show/hide booleans for tool options"""
    tool_objectTrace = BoolProperty(name="Object Trace", default=False, description="Trace selected mesh object with a curve", update=deselect_others)
    tool_objectsConnect = BoolProperty(name="Objects Connect", default=False, description="Connect objects with a curve controlled by hooks", update=deselect_others)
    tool_particleTrace = BoolProperty(name="Particle Trace", default=False, description="Trace particle path with a  curve", update=deselect_others)
    tool_meshFollow = BoolProperty(name="Mesh Follow", default=False, description="Follow selection items on animated mesh object", update=deselect_others)
    tool_particleConnect = BoolProperty(name="Particle Connect", default=False, description="Connect particles with a curves and animated over particle lifetime", update=deselect_others)
    tool_growCurve = BoolProperty(name="Grow Curve", default=False, description="Animate curve bevel over time by keyframing points radius", update=deselect_others)
    tool_handwrite = BoolProperty(name="Handwriting", default=False, description="Create and Animate curve using the grease pencil", update=deselect_others)
    tool_fcurve = BoolProperty(name="F-Curve Noise", default=False, description="Add F-Curve noise to selected objects", update=deselect_others)
    tool_colorblender = BoolProperty(name="Color Blender", default=False, description="Add F-Curve noise to selected objects", update=deselect_others)


# Class to define properties
class TracerProperties(bpy.types.PropertyGroup):
    """Options for tools"""
    curve_spline = EnumProperty(name="Spline", items=(("POLY", "Poly", "Use Poly spline type"),  ("NURBS", "Nurbs", "Use Nurbs spline type"), ("BEZIER", "Bezier", "Use Bezier spline type")), description="Choose which type of spline to use when curve is created", default="BEZIER")
    curve_handle = EnumProperty(name="Handle", items=(("ALIGNED", "Aligned", "Use Aligned Handle Type"), ("AUTOMATIC", "Automatic", "Use Auto Handle Type"), ("FREE_ALIGN", "Free Align", "Use Free Handle Type"), ("VECTOR", "Vector", "Use Vector Handle Type")), description="Choose which type of handle to use when curve is created",  default="VECTOR")
    curve_resolution = IntProperty(name="Bevel Resolution", min=1, max=32, default=4, description="Adjust the Bevel resolution")
    curve_depth = FloatProperty(name="Bevel Depth", min=0.0, max=100.0, default=0.1, description="Adjust the Bevel depth")
    curve_u = IntProperty(name="Resolution U", min=0, max=64, default=12, description="Adjust the Surface resolution")
    curve_join = BoolProperty(name="Join Curves", default=False, description="Join all the curves after they have been created")
    curve_smooth = BoolProperty(name="Smooth", default=True, description="Render curve smooth")
    # Option to Duplicate Mesh
    object_duplicate = BoolProperty(name="Apply to Copy", default=False, description="Apply curve to a copy of object")
    # Distort Mesh options
    distort_modscale = IntProperty(name="Modulation Scale", min=0, max=50, default=2, description="Add a scale to modulate the curve at random points, set to 0 to disable")
    distort_noise = FloatProperty(name="Mesh Noise", min=0.0, max=50.0, default=0.00, description="Adjust noise added to mesh before adding curve")
    # Particle Options
    particle_step = IntProperty(name="Step Size", min=1, max=50, default=5, description="Sample one every this number of frames")
    particle_auto = BoolProperty(name='Auto Frame Range', default=True, description='Calculate Frame Range from particles life')
    particle_f_start = IntProperty(name='Start Frame', min=1, max=5000, default=1, description='Start frame')
    particle_f_end = IntProperty(name='End Frame', min=1, max=5000, default=250, description='End frame')
    # F-Curve Modifier Properties
    fcnoise_rot = BoolProperty(name="Rotation", default=False, description="Affect Rotation")
    fcnoise_loc = BoolProperty(name="Location", default=True, description="Affect Location")
    fcnoise_scale = BoolProperty(name="Scale", default=False, description="Affect Scale")
    fcnoise_amp = IntProperty(name="Amp", min=1, max=500, default=5, description="Adjust the amplitude")
    fcnoise_timescale = FloatProperty(name="Time Scale", min=1, max=500, default=50, description="Adjust the time scale")
    fcnoise_key = BoolProperty(name="Add Keyframe", default=True, description="Keyframe is needed for tool, this adds a LocRotScale keyframe")
    show_curve_settings = BoolProperty(name="Curve Settings", default=False, description="Change the curve settings for the created curve")
    material_settings = BoolProperty(name="Material Settings", default=False, description="Change the material settings for the created curve")
    particle_settings = BoolProperty(name="Particle Settings", default=False, description="Show the settings for the created curve")
    animation_settings = BoolProperty(name="Animation Settings", default=False, description="Show the settings for the Animations")
    distort_curve = BoolProperty(name="Add Distortion", default=False, de
scription="Set options to distort the final curve")
    connect_noise = BoolProperty(name="F-Curve Noise", default=False, description="Adds F-Curve Noise Modifier to selected objects")
    settings_objectTrace = BoolProperty(name="Object Trace Settings", default=False, description="Trace selected mesh object with a curve")
    
settings_objectsConnect = BoolProperty(name="Objects Connect Settings", default=False, description="Connect objects with a curve controlled by hooks")
    settings_objectTrace = BoolProperty(name="Object Trace Settings", default=False, description="Trace selected mesh object with a curve")
    respect_order = BoolProperty(name="Order", default=False, description="Remember order objects were selected")
    settings_particleTrace = BoolProperty(name="Particle Trace Settings", default=False, description="Trace particle path with a  curve")
    settings_particleConnect = BoolProperty(name="Particle Connect Settings", default=False, description="Connect particles with a curves and animated over particle lifetime")
    settings_growCurve = BoolProperty(name="Grow Curve Settings", de