/// ****************************************************************************
/// @file   
/// @author Dr.Neeton
/// @date   2010/X/X - 2017/02/01
/// 
/// @brief  カラー設定コンポーネント。
/// 
/// ****************************************************************************

#include "CPL_GUC_ColorScrollBox.h"

namespace CPL {
namespace GUC {


// _____________________________________________________________________________
/// @brief コンストラクタ
/// 
/// @copydoc doxycomment::arg_in_sp_component_for_parent
/// @copydoc doxycomment::arg_in_position_for_component
/// @copydoc doxycomment::arg_in_string_for_label
/// @param[in] in_value : １要素のカラー値(0.0 - 1.0)を指定します。
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ColorScrollBox::ColorScrollBox
( _in<sdptr<Component>> in_sp_component_for_parent
, _in<float2          > in_position_for_component
, pin<td_DefChar      > in_string_for_label
, _in<float           > in_value
) : td_base
    ( in_sp_component_for_parent
    , in_position_for_component
    , float2(-1,-1)
    )
{
  auto const LH = GetLineHeight();

  // インプットボックス
  m_sp_input =
    Factory<InputBox>::Create
      ( GetLinkMyself()
      , float2( 0.0f, 0.0f ) // pos
      , in_string_for_label
      , 15.0f
      , 25.0f
      , 0.0f
      , 255.0f
      , in_value * 100
      , WIN::NumBox::eNumMode::Int
      );

  m_sp_scroll =
    Factory<ScrollBar>::Create
      ( GetLinkMyself()
      , float2(40,0)
      , float2(80,LH)
      , 0
      ); // horizontal mode
  const float page = 10.0f;
  GetComponent_ScrollBar()->SetParameter
    ( in_value * 100
    , 0.0f
    , 100.0f + page
    , 1.0f
    , page
    );

  zzf_setupDrawState( false, false, false ); // 描画ステートのデフォルト設定。
  //
  zzf_SetInitializedLevel( zzf_GetClassLevel() );

  AdjustSize( {2,0} );
}


// _____________________________________________________________________________
/// @brief コピーコンストラクタ
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ColorScrollBox::ColorScrollBox( cref<ColorScrollBox> in_color_scroll_box )
  : td_base( in_color_scroll_box )
  , m_sp_input
    { FindComponentT_by_UserID<InputBox>( in_color_scroll_box.m_sp_input )
    }
  , m_sp_scroll
    { FindComponentT_by_UserID<ScrollBar>( in_color_scroll_box.m_sp_scroll )
    }
{
  m_sp_input ->SetAutoUserID();
  m_sp_scroll->SetAutoUserID();
}


// _____________________________________________________________________________
/// @copydoc CPL::GUC::Component::On_UserProc
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
eEventReturnID
  ColorScrollBox::On_UserProc
    ( _in<sdptr<Component>> in_sp_component
    , _in<long            > in_message
    , _in<WPARAM          > in_wparam
    , _in<LPARAM          > in_lparam
    )
{
  switch( in_message )
  {
  case eID::ScrollBarChangeValue:
    if( in_sp_component == GetComponent_ScrollBar() )
    {
      float value = GetComponent_ScrollBar()->GetPos();
      GetComponent_InputBox()->SetValue( value );
    }
    break;

  case eID::ValueChanged:
    if( in_sp_component == GetComponent_InputBox() )
    {
      float value = static_cast<float>( GetComponent_InputBox()->GetValue() );
//      m_sp_scroll->SetPos( value );
      GetComponent_ScrollBar()->SetPos( value );
    }
    break;
  }

  return eEventReturnID::SendParent_Position;
}


// _____________________________________________________________________________
/// @copydoc CPL::GUC::Component::CreateClone
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sdptr<Component>
  ColorScrollBox::CreateClone
    ( _in<sdptr<Component>> in_sp_component_for_parent
    , _in<float2          > in_offset_position_from_original
    ) const
{
  return
    zzz_CreateClone_Basic<td_self>
      ( in_sp_component_for_parent
      , in_offset_position_from_original
      );
}


// _____________________________________________________________________________
/// @brief コンストラクタ
/// 
/// @copydoc doxycomment::arg_in_sp_component_for_parent
/// @param[in] in_string_for_label  : 表示名への文字列を指定します。
/// @param[in] in_position_for_draw : 表示座標を指定します。
/// @param[in] in_color : カラー（ColorType:RGBA）を指定します。
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ColorSetupBox::ColorSetupBox
( _in<sdptr<Component>> in_sp_component_for_parent
, pin<td_DefChar      > in_string_for_label
, _in<float2          > in_position_for_draw
, _in<DX::ColorType   > in_color
) : Group
    ( in_sp_component_for_parent
    , in_position_for_draw
    , float2(-1,-1) // サイズ
    , in_string_for_label
    )
{
  auto const LH = GetLineHeight();

  td_DefChar const* tmp_label[] =
  { CPL_GUC_DEF_STR("R")
  , CPL_GUC_DEF_STR("G")
  , CPL_GUC_DEF_STR("B")
  , CPL_GUC_DEF_STR("A")
  };

  //
  CPL_LOOP2( 4, color_idx )
  {
    m_wp_color_value[ color_idx ] =
      Factory<ColorScrollBox>::Create
        ( GetLinkMyself()
        , float2( 20, color_idx * LH + 20 )
        , tmp_label[ color_idx ]
        , in_color.p[ color_idx ]
        );
  }

  zzf_SetInitializedLevel( zzf_GetClassLevel() );

  AdjustSize( float2(2,2) );
}


// _____________________________________________________________________________
/// @brief コピーコンストラクタ
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ColorSetupBox::ColorSetupBox( cref<ColorSetupBox> in_color_setup_box )
  : td_base( in_color_setup_box )
{
  for( int i = 0; i < 4; ++i )
  {
    m_wp_color_value[i] =
      FindComponentT_by_UserID<ColorScrollBox>
        ( in_color_setup_box.m_wp_color_value[i].lock()
        );
    m_wp_color_value[i].lock()->SetAutoUserID();
  }
}


// _____________________________________________________________________________
/// @brief デストラクタ
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ColorSetupBox::~ColorSetupBox()
{
}


// _____________________________________________________________________________
/// @copydoc CPL::GUC::Component::CreateClone
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sdptr<Component>
  ColorSetupBox::CreateClone
    ( _in<sdptr<Component>> in_sp_component_for_parent
    , _in<float2          > in_offset_position_from_original
    ) const
{
  return
    zzz_CreateClone_Basic<td_self>
      ( in_sp_component_for_parent
      , in_offset_position_from_original
      );
}


// _____________________________________________________________________________
/// @copydoc CPL::GUC::Component::On_DrawClient
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void ColorSetupBox::On_DrawClient( _in<float4> in_rect )
{
  const float2 size( 14, 14 );
  const float r = GetR();
  const float g = GetG();
  const float b = GetB();
  const float a = GetA();

  for( int i = 0; i < 4; ++i )
  {
    auto sp_col_val = m_wp_color_value[ i ].lock();
    if( sp_col_val == nullptr ) continue;

    auto pos = sp_col_val->GetPosition();

    //
    pos.x += in_rect.x;
    pos.y += in_rect.y;
    pos.x -= 16;

    float4 rt( pos.x, pos.y, pos.x + size.x, pos.y + size.y );
    if( i < 3 )
    {
      rt.x += 1;
      rt.y += 1;
      rt.z -= 1;
      rt.w -= 1;
    }

    DX::ColorType color( 0.0f, 0.0f, 0.0f, 1.0f );
    switch( i )
    {
    case 0: color.r = r; break; // R
    case 1: color.g = g; break; // G
    case 2: color.b = b; break; // B
    case 3: color = CDI::ColorType( r, g, b, a ); break; // A
    }

    if( i == 3 )
    {
      auto const sx = size.x / 2;
      auto const sy = size.y / 2;
      float4 rt0( rt.x,    rt.y,    rt.x + sx,   rt.y + sy );
      float4 rt1( rt.x+sx, rt.y,    rt.x + sx*2, rt.y + sy );
      float4 rt2( rt.x,    rt.y+sy, rt.x + sx,   rt.y + sy*2 );
      float4 rt3( rt.x+sx, rt.y+sy, rt.x + sx*2, rt.y + sy*2 );
      
      zzf_DrawRect( rt0, kCOLOR_BLACK );
      zzf_DrawRect( rt1, kCOLOR_WHITE );
      zzf_DrawRect( rt2, kCOLOR_WHITE );
      zzf_DrawRect( rt3, kCOLOR_BLACK );
    }

    //
    zzf_DrawRect( rt, color );

    if( i == 3 )
    {
      rt.x -= 2;
      rt.y -= 2;
      //rt.z++;
      //rt.w++;
      zzf_DrawFrame( rt, kCOLOR_BLACK );
    }
  }
}




} // namespace GUC
} // namespace CPL