/*--------------------------------------------------------------------------
  Project:  Manual 2D Library - m2d
  File:     m2d_manualButton.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: 1758 $
 *-------------------------------------------------------------------------*/

#include <ntmv/m2d/Rect.h>
#include <ntmv/m2d/manualButton.h>
#include <ntmv/m2d/objG2dInfo.h>

namespace ntmv
{
namespace m2d
{

/*---------------------------------------------------------------------------*
  Name:         ManualButton::ManualButton

  Description:  Constructor.

  Arguments:    None.

  Returns:      None.
 *---------------------------------------------------------------------------*/
ManualButton::ManualButton()
:   mAllowButtonCell(ObjManager::InvalidCellHandle),
    mTextRectCell(ObjManager::InvalidCellHandle),
    mbAllow(false),
    mbChecked(false)
{}

/*---------------------------------------------------------------------------*
  Name:         ManualButton::Init

  Description:  Performs initialization. Sets the following content.
                    - The button display position
                    - Button and text cell animation handles.

  Arguments:    position:         Display position (upper-left).
                size:             Size.
                textOffsetX:      Display offset for the text (in the x-direction).
                buttonCell:       Animation cell handle for the normal button.
                allowButtonCell:  Animation cell handle for an arrow button.
                textRectCell:     Animation cell handle for the text rectangle.

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
ManualButton::Init(
    Point                   position,
    Size                    size,
    ObjManager::CellHandle  buttonCell,
    ObjManager::CellHandle  allowButtonCell,
    ObjManager::CellHandle  textRectCell
)
{
    Base::Init(position, size, buttonCell);
    mAllowButtonCell    = allowButtonCell;
    mTextRectCell       = textRectCell;
}

/*---------------------------------------------------------------------------*
  Name:         ManualButton::Draw

  Description:  Performs rendering.

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

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

    u16 animFrame = BUTTONSTATE_NORMAL;
    if (IsPressReleased())
    {
        animFrame = BUTTONSTATE_POINTED;
    }
    else if (IsEnabled())
    {
        if (IsPointed())
        {
            animFrame = BUTTONSTATE_POINTED;
        }
        else if (mbChecked)
        {
            animFrame = BUTTONSTATE_CHECKED;
        }
    }
    else
    {
        animFrame = BUTTONSTATE_DISABLED;
    }

    const ObjManager::CellHandle btnCell = mbAllow ? mAllowButtonCell: mCell;

    NNS_ASSERT(btnCell != ObjManager::InvalidCellHandle);

    if (mTextRectCell != ObjManager::InvalidCellHandle)
    {
        info.GetOBJManager()->SetCellAnimFrame(mTextRectCell, animFrame);

        info.GetOBJManager()->DrawCellAnim(
            mTextRectCell,
            mGlbPosition.x * FX32_ONE,
            mGlbPosition.y * FX32_ONE);
    }

    info.GetOBJManager()->SetCellAnimFrame(btnCell, animFrame);

    info.GetOBJManager()->DrawCellAnim(
        btnCell,
        mGlbPosition.x * FX32_ONE,
        mGlbPosition.y * FX32_ONE);
}

/*---------------------------------------------------------------------------*
  Name:         ManualButton::SetAllow

  Description:  Configures whether to use an arrow as the button's type.

  Arguments:    bAllow:  TRUE when an arrow will be used as the button's type.

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
ManualButton::SetAllow(bool bAllow)
{
    NNS_ASSERT(! bAllow || mAllowButtonCell != ObjManager::InvalidCellHandle);

    mbAllow = bAllow;
}

/*---------------------------------------------------------------------------*
  Name:         ManualButton::CheckHit

  Description:  Perform hit detection, and set the pressed state if hit.

  Arguments:    point:  Position to check.

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
ManualButton::CheckHit(Point point)
{
    if (IsVisible() && IsEnabled())
    {
        SetPointed(Contains(point));
    }
}

}   // namespace m2d
}   // namespace ntmv
