######################################################################
#
#       GLSIllumination.py
#
#       Copyright (c) 2003 C. Cigas
#
#       craig@castironflamingo.com
#
#       Manipulate light values.
#
import poser
from tkMessageBox import *
from Tkinter import *
#
#        Utility Functions
#
def Negate(value):
        if (value == 0):
                return 1
        else:
                return 0

def SetFirstFigure():
        for fig in poser.Scene().Figures():
                poser.Scene().SelectFigure(fig)
                break

def SetFirstLight():
        for act in poser.Scene().Lights():
                poser.Scene().SelectActor(act)
                break

def CountLights(p):
        count = 0
        for act in poser.Scene().Lights():
                if (act.LightType() == p):
                        count = count + 1
        return str(count)

def CountTotal():
        return str(poser.Scene().NumLights())

def CountInfinite():
        return CountLights(0)

def CountSpot():
        return CountLights(1)

def CountLocal():
        return CountLights(2)
        
def RendersMeUser():
        poser.Scene().Render()
        poser.Scene().ProcessSomeEvents()

def RendersMeFlash():
        vraa = poser.Scene().RenderAntiAliased()
        poser.Scene().SetRenderAntiAliased(0)
        vrbm = poser.Scene().RenderBumpMaps()
        poser.Scene().SetRenderBumpMaps(0)
        poser.Scene().SetRenderCastShadows(0)

        poser.Scene().Render()

        poser.Scene().SetRenderAntiAliased(vraa)
        poser.Scene().SetRenderBumpMaps(vrbm)
        poser.Scene().SetRenderCastShadows(1)
        poser.Scene().ProcessSomeEvents()

def RendersMeQuick():
        vraa = poser.Scene().RenderAntiAliased()
        poser.Scene().SetRenderAntiAliased(0)
        vrbm = poser.Scene().RenderBumpMaps()
        poser.Scene().SetRenderBumpMaps(1)
        actl = []
        for act in poser.Scene().Lights():
                param = act.Parameter("Map Size")
                actl.append(param.Value())
                param.SetValue(16)

        poser.Scene().Render()

        poser.Scene().SetRenderAntiAliased(vraa)
        poser.Scene().SetRenderBumpMaps(vrbm)
        actl.reverse()
        for act in poser.Scene().Lights():
                param = act.Parameter("Map Size")
                param.SetValue(actl.pop())
        poser.Scene().ProcessSomeEvents()

def RendersMeFull():
        vraa = poser.Scene().RenderAntiAliased()
        poser.Scene().SetRenderAntiAliased(1)
        vrbm = poser.Scene().RenderBumpMaps()
        poser.Scene().SetRenderBumpMaps(1)
        actl = []
        for act in poser.Scene().Lights():
                param = act.Parameter("Map Size")
                actl.append(param.Value())
                param.SetValue(256)

        poser.Scene().Render()

        poser.Scene().SetRenderAntiAliased(vraa)
        poser.Scene().SetRenderBumpMaps(vrbm)
        actl.reverse()
        for act in poser.Scene().Lights():
                param = act.Parameter("Map Size")
                param.SetValue(actl.pop())
        poser.Scene().ProcessSomeEvents()

def KillsMe():
        root.destroy()

######################################################################
#
#        Script Specific Functions
#
def RemoveAllLights():
        count = 0
        if askyesno("Warning", "You are about to remove lights from the scene. Continue?"):
                actl = []
                for act in poser.Scene().Lights():
                        typ = act.LightType()
                        if ((app.iInfinite == 1 and typ == 0) or (app.iSpot == 1 and typ == 1) or (app.iLocal == 1 and typ == 2)):
                                actl.append(act)
                for act in actl:
                        poser.Scene().SelectActor(act)
                        poser.Scene().DeleteCurrentProp()
                        count = count + 1
                SetFirstFigure()
                if (count > 0):
                        KillsMe()

def RemoveInvisibleLights():
        count = 0
        if askyesno("Warning", "You are about to remove lights from the scene. Continue?"):
                actl = []
                for act in poser.Scene().Lights():
                        if (act.OnOff() == 0):
                                typ = act.LightType()
                                if ((app.iInfinite == 1 and typ == 0) or (app.iSpot == 1 and typ == 1) or (app.iLocal == 1 and typ == 2)):
                                        actl.append(act)
                for act in actl:        
                        poser.Scene().SelectActor(act)
                        poser.Scene().DeleteCurrentProp()
                        count = count + 1
                SetFirstFigure()
                if (count > 0):
                        KillsMe()
#
#        Global Light Intensity
#
def LightAbs(c, p):
        for act in poser.Scene().Lights():
                typ = act.LightType()
                if ((app.iInfinite == 1 and typ == 0) or (app.iSpot == 1 and typ == 1) or (app.iLocal == 1 and typ == 2)):
                        param = act.Parameter(c)
                        param.SetValue(p)
        SetFirstLight()
        poser.Scene().DrawAll()
        poser.Scene().ProcessSomeEvents()

def LightIncDec(c, p):
        for act in poser.Scene().Lights():
                typ = act.LightType()
                if ((app.iInfinite == 1 and typ == 0) or (app.iSpot == 1 and typ == 1) or (app.iLocal == 1 and typ == 2)):
                        param = act.Parameter(c)
                        param.SetValue(param.Value() + p)
        SetFirstLight()
        poser.Scene().DrawAll()
        poser.Scene().ProcessSomeEvents()

######################################################################
#
#        User Interface
#
class App:
        def __init__(self, master):
                self.__parmKey = [ "Intensity", "Red", "Green", "Blue" ]
                
                master.resizable(width=NO, height=NO)
                master.title("Cast Iron Flamingo")

                currow = 0
                Label(master, text="Global Light Settings", font=("Helvetica", 14, "bold")).grid(row=currow, columnspan=4, column=0)
                currow = currow + 1
                Label(master, text="", height=2).grid(row=currow, column=3)
                Label(master, text="Illumination", font=("Helvetica", 12, "normal")).grid(row=currow, columnspan=4, column=0, sticky=N)

                currow = currow + 1
                Label(master, text="Selection", font=("Helvetica", 10, "bold")).grid(row=currow, columnspan=4, column=0)
                currow = currow + 1
                Label(master, text="Total").grid(row=currow, column=0)
                Label(master, text="Infinite").grid(row=currow, column=1)
                Label(master, text="Spot").grid(row=currow, column=2)
                Label(master, text="Local").grid(row=currow, column=3)

                currow = currow + 1
                Label(master, text=CountTotal(), font=("Helvetica", 10, "normal")).grid(row=currow, column=0)
                Label(master, text=CountInfinite(), font=("Helvetica", 10, "normal")).grid(row=currow, column=1)
                Label(master, text=CountSpot(), font=("Helvetica", 10, "normal")).grid(row=currow, column=2)
                Label(master, text=CountLocal(), font=("Helvetica", 10, "normal")).grid(row=currow, column=3)

                currow = currow + 1
                self.vInfinite = IntVar()
                self.iInfinite = 0
                self.bInfinite = Checkbutton(master, variable=self.vInfinite, command=self.cInfinite)
                self.bInfinite.grid(row=currow, column=1)
                self.bInfinite.invoke()

                self.vSpot = IntVar()
                self.iSpot = 0
                self.bSpot = Checkbutton(master, variable=self.vSpot, command=self.cSpot)
                self.bSpot.grid(row=currow, column=2)
                self.bSpot.invoke()

                self.vLocal = IntVar()
                self.iLocal = 0
                self.bLocal = Checkbutton(master, variable=self.vLocal, command=self.cLocal)
                self.bLocal.grid(row=currow, column=3)
                self.bLocal.invoke()

                currow = currow + 1
                Label(master, text="Intensity and Hue ", font=("Helvetica", 10, "bold")).grid(row=currow, columnspan=4, column=0)
                currow = currow + 1
                self.vMode = IntVar()
                self.vMode.set(0)
                self.radioIntensity = Radiobutton(master, text="Intensity", variable=self.vMode, value=0)
                self.radioIntensity.grid(row=currow, column=0)
                Radiobutton(master, variable=self.vMode, text="Red", value=1).grid(row=currow, column=1)
                Radiobutton(master, variable=self.vMode, text="Green", value=2).grid(row=currow, column=2)
                Radiobutton(master, variable=self.vMode, text="Blue", value=3).grid(row=currow, column=3)
                self.radioIntensity.select()
                
                currow = currow + 1
                Button(master, text="- 10%", width=11, borderwidth=1, padx=0, command=self.LightDecrease10).grid(row=currow, column=0)
                Button(master, text="- 1%", width=11, borderwidth=1, padx=0, command=self.LightDecrease1).grid(row=currow, column=1)
                Button(master, text="+ 1%", width=11, borderwidth=1, padx=0, command=self.LightIncrease1).grid(row=currow, column=2)
                Button(master, text="+ 10%", width=11, borderwidth=1, padx=0, command=self.LightIncrease10).grid(row=currow, column=3)
                currow = currow + 1
                Button(master, text="0%", width=24, borderwidth=1, padx=0, command=self.Light0).grid(row=currow, columnspan=2, column=0)
                Button(master, text="100%", width=24, borderwidth=1, padx=0, command=self.Light100).grid(row=currow, columnspan=2, column=2)

                currow = currow + 1
                Label(master, text="Shadow Intensity", font=("Helvetica", 10, "bold")).grid(row=currow, columnspan=4, column=0)
                currow = currow + 1
                Button(master, text="- 0.10", width=11, borderwidth=1, padx=0, command=self.ShadowDecrease10).grid(row=currow, column=0)
                Button(master, text="- 0.01", width=11, borderwidth=1, padx=0, command=self.ShadowDecrease1).grid(row=currow, column=1)
                Button(master, text="+ 0.01", width=11, borderwidth=1, padx=0, command=self.ShadowIncrease1).grid(row=currow, column=2)
                Button(master, text="+ 0.10", width=11, borderwidth=1, padx=0, command=self.ShadowIncrease10).grid(row=currow, column=3)
                currow = currow + 1
                Button(master, text="0.00", width=24, borderwidth=1, padx=0, command=self.Shadow0).grid(row=currow, columnspan=2, column=0)
                Button(master, text="1.00", width=24, borderwidth=1, padx=0, command=self.Shadow100).grid(row=currow, columnspan=2, column=2)

                currow = currow + 1
                Label(master, text="Shadow Maps", font=("Helvetica", 10, "bold")).grid(row=currow, columnspan=4, column=0)
                currow = currow + 1
                Button(master, text="256", width=11, borderwidth=1, padx=0, command=self.SetMapSize256).grid(row=currow, column=0)
                Button(master, text="512", width=11, borderwidth=1, padx=0, command=self.SetMapSize512).grid(row=currow, column=1)
                Button(master, text="1024", width=11, borderwidth=1, padx=0, command=self.SetMapSize1024).grid(row=currow, column=2)
                Button(master, text="2048", width=11, borderwidth=1, padx=0, command=self.SetMapSize2048).grid(row=currow, column=3)

                currow = currow + 1
                Label(master, text="Remove Lights", font=("Helvetica", 10, "bold")).grid(row=currow, columnspan=4, column=0)
                currow = currow + 1
                Button(master, text="Unused", width=24, borderwidth=1, padx=0, command=RemoveInvisibleLights).grid(row=currow, columnspan=2, column=0)
                Button(master, text="All", width=24, borderwidth=1, padx=0, command=RemoveAllLights).grid(row=currow, columnspan=2, column=2)

                currow = currow + 1
                Label(master, text="Render", font=("Helvetica", 10, "bold")).grid(row=currow, columnspan=4, column=0)
                currow = currow + 1
                Button(master, text="Flash", width=11, borderwidth=1, padx=0, command=RendersMeFlash).grid(row=currow, column=0)
                Button(master, text="Quick", width=11, borderwidth=1, padx=0, command=RendersMeQuick).grid(row=currow, column=1)
                Button(master, text="Full", width=11, borderwidth=1, padx=0, command=RendersMeFull).grid(row=currow, column=2)
                Button(master, text="User", width=11, borderwidth=1, padx=0, command=RendersMeUser).grid(row=currow, column=3)

                currow = currow + 1
                Label(master, text="", height=3).grid(row=currow, column=3)
                Button(master, text="Close", width=50, borderwidth=1, padx=0, command=KillsMe).grid(row=currow, columnspan=4, column=0, sticky=SE)

                master.grid()
                master.update_idletasks()
                master.geometry(str(master.winfo_width()) + "x" + str(master.winfo_height()) + "+0+" + str((master.maxsize()[1] - master.winfo_height())/2))
                master.deiconify()
                poser.Scene().DrawAll()

        def cInfinite(self):
                self.iInfinite = Negate(self.iInfinite)        

        def cSpot(self):
                self.iSpot = Negate(self.iSpot)

        def cLocal(self):
                self.iLocal = Negate(self.iLocal)
        #
        #       Intensity and Hue
        #
        def Light0(self):
                LightAbs(self.__parmKey[self.vMode.get()], 0)
        def Light100(self):
                LightAbs(self.__parmKey[self.vMode.get()], 1)
        def LightIncrease1(self):
                LightIncDec(self.__parmKey[self.vMode.get()], +0.01)
        def LightIncrease10(self):
                LightIncDec(self.__parmKey[self.vMode.get()], +0.10)
        def LightDecrease1(self):
                LightIncDec(self.__parmKey[self.vMode.get()], -0.01)
        def LightDecrease10(self):
                LightIncDec(self.__parmKey[self.vMode.get()], -0.10)
        #
        #        Global Shadow Intensity and Maps
        #
        def Shadow0(self):
                LightAbs("Shadow", 0)
        def Shadow100(self):
                LightAbs("Shadow", 1)
        def ShadowDecrease10(self):
                LightIncDec("Shadow", -0.10)
        def ShadowDecrease1(self):
                LightIncDec("Shadow", -0.01)
        def ShadowIncrease1(self):
                LightIncDec("Shadow", +0.10)
        def ShadowIncrease10(self):
                LightIncDec("Shadow", +0.10)

        def SetMapSize256(self):
                LightAbs("Map Size", 256)
        def SetMapSize512(self):
                LightAbs("Map Size", 512)
        def SetMapSize1024(self):
                LightAbs("Map Size", 1024)
        def SetMapSize2048(self):
                LightAbs("Map Size", 2048)

######################################################################
#
#       Main
#
if CountTotal() != "0":
        SetFirstLight()
        poser.Scene().DrawAll()
        poser.Scene().ProcessSomeEvents()
        root = Tk()
        app = App(root)
        root.mainloop()
