# Copyright 2009 Autodesk, Inc.  All rights reserved.
# Use of this software is subject to the terms of the Autodesk license agreement 
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
#
# Script description:
# Create a tool that shows how to use a VBoxLayout/HBoxLayout to place controls dynamically on screen.
#
# Topic: HBoxLayout, VBoxLayout, FBAttachType
#

from pyfbsdk import *
from pyfbsdk_additions import *


def PopulateLayout(mainLyt):
    # Create Main region frame:
    x = FBAddRegionParam(5,FBAttachType.kFBAttachLeft,"")
    y = FBAddRegionParam(5,FBAttachType.kFBAttachTop,"")
    w = FBAddRegionParam(-5,FBAttachType.kFBAttachRight,"")
    h = FBAddRegionParam(-5,FBAttachType.kFBAttachBottom,"")

    main = VBoxLayout()
    mainLyt.AddRegion("main","main", x, y, w, h)
    mainLyt.SetControl("main",main)


    # create horizontal boxes packed in a vbox
    hstripes = VBoxLayout()

    # Shows how to create a HBoxLayout that grows from left to right
    box = HBoxLayout(FBAttachType.kFBAttachLeft)
    names = ["from ->", "left ->", "to ->", "right ->"]
    for name in names:
        b = FBButton()
        b.Caption = name
        box.Add(b, 50)
    hstripes.Add(box, 35)

    # Shows how to create a HBoxLayout that grows from right to left
    box = HBoxLayout(FBAttachType.kFBAttachRight)
    names = ["<- from", "<- right", "<- to", "<- left"]
    for name in names:
        b = FBButton()
        b.Caption = name
        box.Add(b, 50)
    hstripes.Add(box, 35)

    # Add a button that is center in its row using "dummy" addRelative
    row = HBoxLayout(FBAttachType.kFBAttachLeft)
    row.AddRelative(None)
    b = FBButton()
    b.Caption = "center"
    row.Add(b, 50)
    row.AddRelative(None)
    hstripes.Add(row,35)

    # Shows how to add buttons that resize to take all spaces available according to ratio
    names = [("1/4 of width", 0.25), ("1/2 of width", 0.5), ("1/4 of width", 0.25)]
    box = HBoxLayout(FBAttachType.kFBAttachLeft)
    for name, ratio in names:
        b = FBButton()
        b.Caption = name
        box.AddRelative(b, ratio)
    hstripes.Add(box, 35)


    # add all vbox into the main layout
    main.AddRelative(hstripes,1.0)

    vstripes = HBoxLayout()

    # Shows how to create a VBoxLayout that grows from Top to bottom
    box = VBoxLayout(FBAttachType.kFBAttachTop)
    names = ["from ", "top", "to", "bottom"]
    for name in names:
        b = FBButton()
        b.Caption = name
        box.Add(b, 50)
    vstripes.Add(box, 70)

    # Shows how to create a VBoxLayout that grows Bottom to Top
    box = VBoxLayout(FBAttachType.kFBAttachBottom)
    names = ["from", "bottom", "to", "top"]
    for name in names:
        b = FBButton()
        b.Caption = name
        box.Add(b, 50)
    vstripes.Add(box, 70)

    # Add a button that is center in its column using "dummy" addRelative
    row = VBoxLayout()
    row.AddRelative(None)
    b = FBButton()
    b.Caption = "center"
    row.Add(b, 35)
    row.AddRelative(None)
    vstripes.Add(row,70)

    # Shows how to add buttons that resize to take all spaces available according to ratio
    names = [("1/4 of width", 0.25), ("1/2 of width", 0.5), ("1/4 of width", 0.25)]
    box = VBoxLayout()
    for name, ratio in names:
        b = FBButton()
        b.Caption = name
        box.AddRelative(b, ratio)
    vstripes.Add(box, 70)

    main.AddRelative(vstripes,1.0)

def CreateTool():
    # Tool creation will serve as the hub for all other controls
    t = CreateUniqueTool("Box Example")
    PopulateLayout(t)
    t.StartSizeX = 800
    t.StartSizeY = 800
    ShowTool(t)


CreateTool()