/*---------------------------------------------------------------------------* Project: NintendoWare File: SmButton.cpp Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. 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: 1 $ *---------------------------------------------------------------------------*/ #include #include #include #include "../include/SmButton.h" #include "../include/SmTouchPanelStatus.h" namespace { //---------------------------------------- // コンストラクタ SmButton::SmButton( uint x, uint y, uint w, uint h ) : SmMessage( NULL ), m_Button( NULL ), m_PushButton( NULL ), m_IsPressed( false ), m_IsGrab( false ), m_IsSwitch( true ), m_OnColor( nw::ut::Color8::BLACK ), m_OffColor( nw::ut::Color8::RED ) { NW_NULL_ASSERT( m_Allocator ); // ボタンポリゴン生成 m_Button = new Sm2DPrimPC( 4 ); NW_NULL_ASSERT( m_Button ); { m_Button->SetVertex( 0, x, y, nw::ut::Color8::BLACK ); m_Button->SetVertex( 1, x, y + h, nw::ut::Color8::BLACK ); m_Button->SetVertex( 2, x + w, y, nw::ut::Color8::BLACK ); m_Button->SetVertex( 3, x + w, y + h, nw::ut::Color8::BLACK ); } m_Button->Update(); // 押されたボタンポリゴン生成 #define PRESSED_BTN_MARGIN (4.f) m_PushButton = new Sm2DPrimPC( 4 ); NW_NULL_ASSERT( m_PushButton ); { m_PushButton->SetVertex( 0, x - PRESSED_BTN_MARGIN, y - PRESSED_BTN_MARGIN, nw::ut::Color8::YELLOW ); m_PushButton->SetVertex( 1, x - PRESSED_BTN_MARGIN, y + h + PRESSED_BTN_MARGIN, nw::ut::Color8::YELLOW ); m_PushButton->SetVertex( 2, x + w + PRESSED_BTN_MARGIN, y - PRESSED_BTN_MARGIN, nw::ut::Color8::YELLOW ); m_PushButton->SetVertex( 3, x + w + PRESSED_BTN_MARGIN, y + h + PRESSED_BTN_MARGIN, nw::ut::Color8::YELLOW ); } m_PushButton->Update(); // コリジョン形状を登録 m_Collision.SetRect( x, y, w, h ); // 初期状態 SetSwitch( m_IsSwitch ); } //---------------------------------------- // デストラクタ SmButton::~SmButton() { if ( m_Button ) delete m_Button; m_Button = NULL; if ( m_PushButton ) delete m_PushButton; m_PushButton = NULL; } //---------------------------------------- // 描画 void SmButton::Render() { if ( m_IsPressed ) { m_PushButton->Render( GL_TRIANGLE_STRIP ); } if ( m_OnColor != 0 && m_OffColor != 0 ) { m_Button->Render( GL_TRIANGLE_STRIP ); } } //---------------------------------------- // ボタン状態を設定する void SmButton::SetSwitch( bool bswitch ) { m_IsSwitch = bswitch; if ( m_IsSwitch ) { m_Button->SetVertexColor( m_OnColor ); } else { m_Button->SetVertexColor( m_OffColor ); } m_Button->Update(); } //---------------------------------------- // ボタン状態をスワップする void SmButton::SwapSwitch() { if ( m_IsSwitch ) { m_IsSwitch = false; m_Button->SetVertexColor( m_OffColor ); } else { m_IsSwitch = true; m_Button->SetVertexColor( m_OnColor ); } m_Button->Update(); } //---------------------------------------- // ボタンカラーを設定する void SmButton::SetColor( nw::ut::Color8 onColor, nw::ut::Color8 offColor ) { m_OnColor = onColor; m_OffColor = offColor; } //---------------------------------------- // メッセージ受信 bool SmButton::ReceveMessage( SmMessageType type, void* object, uint targetId ) { NW_UNUSED_VARIABLE(targetId); SmTouchPanelStatus* touchPanelStaus = NULL; switch( type ) { case SM_MESSAGE_TUCHPANEL_PRESS: { touchPanelStaus = static_cast(object); NW_NULL_ASSERT( touchPanelStaus ); // 他処理で使用中でなければ if ( !touchPanelStaus->IsGrab() ) { // コリジョン内であれば、 if ( m_Collision.CheckInner( touchPanelStaus->GetX(), touchPanelStaus->GetY() ) ) { // 掴む touchPanelStaus->Grab(); m_IsGrab = true; m_IsPressed = true; } } } break; case SM_MESSAGE_TUCHPANEL_MOTION: { touchPanelStaus = static_cast(object); NW_NULL_ASSERT( touchPanelStaus ); m_LastX = touchPanelStaus->GetX(); m_LastY = touchPanelStaus->GetY(); } break; case SM_MESSAGE_TUCHPANEL_RELEASE: { touchPanelStaus = static_cast(object); NW_NULL_ASSERT( touchPanelStaus ); // 放す if ( m_IsGrab ) { touchPanelStaus->UnGrab(); m_IsGrab = false; m_IsPressed = false; // リリースの際、範囲内であればアップデートをかける if ( m_Collision.CheckInner( m_LastX, m_LastY ) ) { // 状態をスワップする SwapSwitch(); // Updateを送信 if ( m_Target ) { m_Target->SendMessage( SM_MESSAGE_UPDATE_PARAM, NULL, m_Id ); } } } } break; default: break; } return 0; } } // namespace