######################################################################
#
#       FloorTile.py
#
#       Copyright (c) 2003 C. Cigas
#
#       craig@castironflamingo.com
#
#       Create an array of floor props with tiling.
#
import poser
from tkMessageBox import *
from Tkinter import *
from string import *

prpn = ":Runtime:Libraries:Props:Prop Types:square.pp2"
prpd = "FloorTile"
#
#       Load a prop from the library and return a reference to it
#
def LoadProp():
        actd = []
        for act in poser.Scene().Actors():
                actd.append(act.Name())
        poser.Scene().LoadLibraryProp(prpn)
        for act in poser.Scene().Actors():
                try:
                        actd.remove(act.Name())
                except:
                        actp = act
        return actp
        
def CopyParm(srcAct, dstAct):
        for parm in srcAct.Parameters():
                dstAct.Parameter(parm.Name()).SetValue(parm.Value())
                
def CopyMats(srcAct, dstAct):
        for mat in srcAct.Materials():
                dstMat = dstAct.Material(mat.Name())
                rgb = mat.AmbientColor()
                dstMat.SetAmbientColor(rgb[0], rgb[1], rgb[2])
                rgb = mat.DiffuseColor()
                dstMat.SetDiffuseColor(rgb[0], rgb[1], rgb[2])
                rgb = mat.SpecularColor()
                dstMat.SetSpecularColor(rgb[0], rgb[1], rgb[2])
                rgb = mat.TextureColor()
                dstMat.SetTextureColor(rgb[0], rgb[1], rgb[2])
                
                dstMat.SetBumpStrength(mat.BumpStrength())
                dstMat.SetNs(mat.Ns())
                dstMat.SetReflectionStrength(mat.ReflectionStrength())
                dstMat.SetTransparencyExpo(mat.TransparencyExpo())
                dstMat.SetTransparencyMax(mat.TransparencyMax())
                dstMat.SetTransparencyMin(mat.TransparencyMin())

                if mat.ReflectionMapFileName():
                        dstMat.SetReflectionMapFileName(None)
                        dstMat.SetReflectionMapFileName(mat.ReflectionMapFileName())
                if mat.BumpMapFileName():
                        dstMat.SetBumpMapFileName(None)
                        dstMat.SetBumpMapFileName(mat.BumpMapFileName())
                if mat.TextureMapFileName():
                        dstMat.SetTextureMapFileName(None)
                        dstMat.SetTextureMapFileName(mat.TextureMapFileName())
                if mat.TransparencyMapFileName():
                        dstMat.SetTransparenctyMapFileName()
                        dstMat.SetTransparencyMapFileName(mat.TransparencyMapFileName())
#
#       Create a master tile prop
#
def CreateMasterTile():
        try:
                actp = poser.Scene().Actor(prpd)
        except:
                actp = LoadProp()
                actp.SetName(prpd)
                actp.Parameter("xRotate").SetValue(-90)
                poser.Scene().SelectActor(actp)
        KillsMe()
#
#       Remove all array tile props
#
def RemoveArray():
        for i in range(2,401):
                try:
                        poser.Scene().SelectActor(poser.Scene().Actor(prpd + zfill(str(i),3)))
                        poser.Scene().ProcessSomeEvents()
                        poser.Scene().DrawAll()
                        poser.Scene().DeleteCurrentProp()
                except:
                        pass
        #
        #       This must be here. Why? I don't know.
        #
        poser.Scene().SelectActor(LoadProp())
        poser.Scene().DeleteCurrentProp()
        poser.Scene().ProcessSomeEvents()
        poser.Scene().DrawAll()
#
#       Create a new array of tile props
#
def CreateArray():
        global gMode
        global gSize
#
#       Figure what Poser will name the props when they're loaded so we don't have to
#       check each iteration. The variable prpc is the name.
#
        RemoveArray()
        act = LoadProp()
        prpc = act.Name()
        poser.Scene().SelectActor(act)
        poser.Scene().DeleteCurrentProp()
#
#       Create the array
#
        actp = poser.Scene().Actor(prpd)
        incx = 0.1 * actp.Parameter("Scale").Value() * actp.Parameter("xScale").Value()
        incz = 0.1 * actp.Parameter("Scale").Value() * actp.Parameter("zScale").Value()
        for x in range(0,gSize):
                for z in range(0,gSize):
                        if x + z > 0:
                                poser.Scene().LoadLibraryProp(prpn)
                                act = poser.Scene().Actor(prpc)
                                act.SetName(prpd + zfill(str(x * gSize + z + 1),3))
                                act.SetParent(actp.Parent())
                                CopyParm(actp, act)
                                act.Parameter("xRotate").SetValue(-90)
                                act.Parameter("xTran").SetValue(x * incx)
                                act.Parameter("zTran").SetValue(z * incz)
                                if gMode == 1:
                                        if ((z % 2) == 1):
                                                act.Parameter("xRotate").SetValue(actp.Parameter("xRotate").Value()+180)
                                                act.Parameter("zTran").SetValue((z-1)*incz)
                                        if ((x % 2) == 1):
                                                act.Parameter("yRotate").SetValue(actp.Parameter("yRotate").Value()+180)
                                CopyMats(actp, act)
                                poser.Scene().ProcessSomeEvents()
                                poser.Scene().DrawAll()

def KillsMe():
        root.destroy()

############################################################
#
#        User Interface
#

class App:
        def __init__(self, master):
                master.resizable(width=NO, height=NO)
                master.title("Cast Iron Flamingo")

                currow = 0
                Label(master, text="Floor Tile", font=("Helvetica", 14, "bold")).grid(row=currow, columnspan=4, column=0)

                currow = currow + 1
                self.textInstructions = Text(master, width=34, height=10, background="SystemButtonFace", borderwidth=0, font=("Helvetica", 8, "normal"), wrap=WORD)
                self.textInstructions.insert(END, "\n")
                self.textInstructions.insert(END, "1. Create a master tile.\n\n")
                self.textInstructions.insert(END, "2. Apply the texture map to the master tile prop and adjust the scale dial to a suitable value.\n\n")
                self.textInstructions.insert(END, "3. Set the array size and tile mode then create the array.\n\n")
                self.textInstructions.grid(row=currow, column=0, columnspan=4)
                
                currow = currow + 1
                Button(master, text="Create Master Tile", width=34, borderwidth=1, padx=0, command=CreateMasterTile).grid(row=currow, columnspan=4, column=0)

                currow = currow + 1
                Label(master, text="______________________________", font=("Helvetica", 8, "normal")).grid(row=currow, columnspan=4, column=0)

                currow = currow + 1
                Label(master, text="Square Array Size", font=("Helvetica", 8, "normal")).grid(row=currow, columnspan=4, column=0)
                currow = currow + 1
                Label(master, text="(think before setting over 10)", font=("Helvetica", 8, "normal")).grid(row=currow, columnspan=4, column=0)
                currow = currow + 1
                self.sliderSize = Scale(master, from_=2, to=20, length=207, orient=HORIZONTAL, resolution=1)
                self.sliderSize.grid(row=currow, column=0,columnspan=4)
                self.sliderSize.set(4)
                
                currow = currow + 1
                self.vMode = IntVar()
                self.vMode.set(0)
                self.radioRepeat = Radiobutton(master, text="Repeat", variable=self.vMode, value=0)
                self.radioRepeat.grid(row=currow, column=0, columnspan=2)
                self.radioSymmetric = Radiobutton(master, text="Symmetric", variable=self.vMode, value=1)
                self.radioSymmetric.grid(row=currow, column=2, columnspan=2)
                self.radioRepeat.select()

                currow = currow + 1
                Button(master, text="Create Array", width=16, borderwidth=1, padx=0, command=self.cmdCreateArray).grid(row=currow, columnspan=2, column=0)
                Button(master, text="Remove Array", width=16, borderwidth=1, padx=0, command=RemoveArray).grid(row=currow, columnspan=2, column=2)

                currow = currow + 1
                Label(master, text="", font=("Helvetica", 8, "normal")).grid(row=currow, columnspan=4, column=0)

                currow = currow + 1
                Button(master, text="Close", width=34, borderwidth=1, padx=0, command=KillsMe).grid(row=currow, columnspan=4, column=0)
                
                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 cmdCreateArray(self):
                global gMode
                global gSize
                
                gMode = self.vMode.get()
                gSize = self.sliderSize.get()
                
                CreateArray()

root = Tk()
app = App(root)
root.mainloop()
