/*--------------------------------------------------------------------------
  Project:  Manual 2D Library - m2d
  File:     m2d_uiPanel.cpp

  Copyright (C)2007-2009 Nintendo  All Rights Reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law. They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

  $Revision: 502 $
 *-------------------------------------------------------------------------*/

#include <ntmv/m2d/Rect.h>
#include <ntmv/m2d/objectConstructor.h>
#include <ntmv/m2d/uiPanel.h>

namespace ntmv
{
namespace m2d
{

/*---------------------------------------------------------------------------*
  Name:         UIPanel::UIPanel

  Description:  Constructor.

  Arguments:    None.

  Returns:      None.
 *---------------------------------------------------------------------------*/
UIPanel::UIPanel()
:   mElements(0),
    mElementNum(0)
{}

/*---------------------------------------------------------------------------*
  Name:         UIPanel::Init

  Description:  Performs initialization. Sets the following content.
                    - Sets the font array
                    - Initializes buttons

  Arguments:    objectConstructor:  A reference to an ObjectConstructor object.
                elements:           uiElement array.
                elementNum:         Number of elements in the uiElement array.
                position:           Display position (center).
                size:               Size.

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
UIPanel::Init(
    const ObjectConstructor&    objectConstructor,
    UIElement*                  elements[],
    u16                         elementNum,
    Point                       position,
    Size                        size
)
{
    mElements = objectConstructor.NewArray<UIElement*>(elementNum);
    NNS_NULL_ASSERT(mElements);

    mElementNum = elementNum;

    for (int i = 0; i < elementNum; ++i)
    {
        mElements[i] = elements[i];
    }

    mPosition = position;
    mSize = size;
}

/*---------------------------------------------------------------------------*
  Name:         UIPanel::Finalize

  Description:  Performs shutdown.

  Arguments:    objectConstructor:  A reference to an ObjectConstructor object.

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
UIPanel::Finalize(const ObjectConstructor& objectConstructor)
{
    // NTMV_LOG("UIPanel::Finalize()\n");

    for (int i = 0; i < mElementNum; ++i)
    {
        objectConstructor.DeleteObj(mElements[i]);
    }

    objectConstructor.DeletePrimArray(mElements);

    Base::Finalize(objectConstructor);
}

/*---------------------------------------------------------------------------*
  Name:         UIPanel::Draw

  Description:  Performs rendering.

  Arguments:    info:      A reference to an ObjG2dInfo object.
                parentPos: Parent position (global coordinates).

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
UIPanel::Draw(
    const ObjG2dInfo&   info,
    Point               parentPos
)
{
    if (! IsVisible())
    {
        return;
    }

    CalcGlbPosition(parentPos);

    for (int i = 0; i < mElementNum; ++i)
    {
        mElements[i]->Draw(info, mGlbPosition);
    }
}

}   // namespace m2d
}   // namespace ntmv
