/// ****************************************************************************
/// @file   
/// @author Dr.Neeton
/// @date   2010/X/X - 2017/02/01
/// 
/// @brief  グループコンポーネント。
/// 
/// ****************************************************************************


#include "CPL_GUC_Group.h"


namespace CPL {
namespace GUC {

//
//// デフォルトフレームカラー
//static auto
//  sp_default_group_frame_color =
//    CPL_MAKE_SHARED
//      ( DrawColorSet
//      , COLOr_BLACK
//      , COLOr_BLUE
//      , COLOr_BLUE
//      , COLOr_BLACK
//      );



// _____________________________________________________________________________
/// @brief コンストラクタ
/// 
/// @copydoc doxycomment::arg_in_sp_component_for_parent
/// @copydoc doxycomment::arg_in_position_for_component
/// @copydoc doxycomment::arg_in_size_for_component
/// @param[in] in_title : グループのタイトルを指定します。
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Group::Group
( _in<sdptr<Component>> in_sp_component_for_parent
, _in<float2          > in_position_for_component
, _in<float2          > in_size_for_component
, pin<td_DefChar      > in_title
) : td_base
    ( in_sp_component_for_parent
    , in_position_for_component
    , in_size_for_component
    )
{
  m_title = in_title;

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

  //SetColorParameterFrame( sp_default_group_frame_color );
}


// _____________________________________________________________________________
/// @brief コピーコンストラクタ
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Group::Group( cref<Group> in_group )
  : td_base( in_group )
  , m_title( in_group.m_title )
{
}


// _____________________________________________________________________________
/// @copydoc CPL::GUC::Component::CreateClone
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sdptr<Component>
  Group::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_DrawFrame
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void Group::On_DrawFrame( _in<float4> in_rect )
{
  // color
  auto sp_colorset_text = GetColorParameterText();
  auto ptr_color = &sp_colorset_text->color_default;
  if( !IsEnable() )
  {
    ptr_color = &sp_colorset_text->color_invalid;
  }
  else
  if( sm_sp_ac_comp() == this->GetLinkMyself() )
  {
    if( WIN::Mouse::GetDownL() )
    {
      ptr_color = &sp_colorset_text->color_active;
    }
    else
    {
      ptr_color = &sp_colorset_text->color_highlight;
    }
  }
  U32x4 color_text;
  ptr_color->ConvertColor_to_U32x4( color_text );

  // タイトル描画
//#ifdef CPL_UNICODE
  float2 size =
    sm_def_font_mng()->GetStringLineSize( m_title, 0, nullptr );
  DX::FontDrawManager::MsgInfo
    mi( m_title
      , WIN::Rect
          ( static_cast<long>(in_rect.x + 8)
          , static_cast<long>(in_rect.y + 3)
          , static_cast<long>(in_rect.x + 8 + size.x)
          , static_cast<long>(in_rect.y + 3 + size.y)
          )
      , 0.0f // depth
      , color_text
      );
//#else
//  Core::StringW wstr;
//  Core::ChangeCodeStrings( wstr, m_title );
//  float2 size =
//    sm_def_font_mng()->GetStringLineSize( wstr, 0, nullptr );
//  DX::FontDrawManager::MsgInfo
//    mi( wstr
//      , &WIN::Rect( in_rect.x + 8, in_rect.y + 3, -1, -1)
//      , 0.0f // depth
//      , color_text
//      );
//#endif

  sm_def_font_mng()->DrawMessage( mi );

  mi.rect.left -= 2;
  mi.rect.top  -= 1;
  mi.default_color.r = 1 - mi.default_color.r;
  mi.default_color.g = 1 - mi.default_color.g;
  mi.default_color.b = 1 - mi.default_color.b;
  mi.default_color.a = 1 - mi.default_color.a;
  sm_def_font_mng()->DrawMessage( mi );
/*
  sm_def_font_mng()->DrawString(
    in_rect.x + 8.0f,
    in_rect.y + 3.0f,
    0.0f,
    m_title,
    COLOr_WHITE );
  sm_def_font_mng()->DrawString(
    in_rect.x + 6.0f,
    in_rect.y + 2.0f,
    0.0f,
    m_title,
    COLOr_BLACK );
    */
  sm_def_font_mng()->Translate( sm_ptr_d3ddev() );

  if( IsDrawFrame() )
  {
    // フレーム描画
    zzf_drawGroupFrameLine_( in_rect, size, kCOLOR_BLACK );
  }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// グループ用枠の描画
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void
  Group::zzf_drawGroupFrameLine_
    ( _in<float4       > in_rect
    , _in<float2       > in_str_size
    , _in<DX::ColorType> in_color
    )
{
  float offs_h = in_str_size.y / 2;
  float4 rt( in_rect.x+1, in_rect.y+offs_h, in_rect.z-1, in_rect.w-1 );
  
  // 頂点準備
  DrawVtxInfo vtx_table[] = {
    DrawVtxInfo( rt.x + in_str_size.x + 10.0f, float(rt.y)+0.5f, 0.0f, in_color ),
    DrawVtxInfo( rt.z + 0.5f, rt.y + 0.5f, 0.0f, in_color ),
    DrawVtxInfo( rt.z + 0.5f, rt.w + 0.5f, 0.0f, in_color ),
    DrawVtxInfo( rt.x + 0.5f, rt.w + 0.5f, 0.0f, in_color ),
    DrawVtxInfo( rt.x + 0.5f, rt.y + 0.5f, 0.0f, in_color ),
    DrawVtxInfo( static_cast<float>(rt.x + 2) + 0.5f, rt.y + 0.5f, 0.0f, in_color )
  };

  static U32 const index[] = {
    0,1,2,3,4,5,
  };
  
  zzf_DrawPolygon
    ( vtx_table
    , CPL_ARRAY_NUM(vtx_table)
    , index
    , CPL_ARRAY_NUM(index)
    , D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP
    );
}



} // namespace GUC
} // namespace CPL

