text: #include "stdafx.h"
  #include "UIZoneMap.h"
  
  #include "InfoPortion.h"
  #include "Pda.h"
  
  #include "Grenade.h"
  #include "level.h"
  
  
  #include "actor.h"
  #include "ai_space.h"
  #include "game_graph.h"
  
  #include "ui/UIMap.h"
  #include "../xrUICore/UIXmlInit.h"
  #include "../xrUICore/UIHelper.h"
  #include "ui/UIInventoryUtilities.h"
  //////////////////////////////////////////////////////////////////////////
  
  CUIZoneMap::CUIZoneMap()
  :m_current_map_idx(u8(-1)),
  visible(true)
  {	
      m_clock_wnd = nullptr;
      m_date_wnd = nullptr;
      m_activeMap = nullptr;
  }
  
  CUIZoneMap::~CUIZoneMap()
  {
      xr_delete(m_activeMap);
      xr_delete(m_clock_wnd);
      xr_delete(m_date_wnd);
  }
  
  void CUIZoneMap::Init()
  {
  	CUIXml uiXml;
  	uiXml.Load						(CONFIG_PATH, UI_PATH, "ui_HUD.xml");
  
  	CUIXmlInit						xml_init;
  	xml_init.InitStatic				(uiXml, "minimap:background",	0, &m_background);
  	xml_init.InitWindow				(uiXml, "minimap:level_frame",	0, &m_clipFrame);
  	xml_init.InitStatic				(uiXml, "minimap:center",		0, &m_center);
  	
  	m_clock_wnd	= UIHelper::CreateStatic(uiXml, "minimap:clock_wnd", &m_background);
      m_clock_wnd->SetAutoDelete(false);
  
      //Giperion: Date widget initialized in update() (ConditionalInitDateWidget()). Option can be enabled/disabled in runtime
  
  	m_activeMap						= xr_new<CUIMiniMap>();
  	m_clipFrame.AttachChild			(m_activeMap);
  	m_activeMap->SetAutoDelete		(false);
  
  	m_activeMap->EnableHeading		(true);  
  	xml_init.InitStatic				(uiXml, "minimap:compass", 0, &m_compass);
  	m_background.AttachChild		(&m_compass);
  
  	m_clipFrame.AttachChild			(&m_center);
  
  	m_Counter_text.SetText( "" );
  	visible = true;
  
  	Fvector2 sz_k				= m_clipFrame.GetWndSize();
  	Fvector2 sz					= sz_k;
  
      float k = UI().get_current_kx();
  
      sz.y *= UI_BASE_HEIGHT * k;
      sz.x = sz.y / k;
  
      m_clipFrame.SetWndSize(sz);
  
      Fvector2 p = m_clipFrame.GetWndPos();
      p.mul(UI_BASE_HEIGHT);
      m_clipFrame.SetWndPos(p);
  
      m_background.SetHeight(m_background.GetHeight() * UI_BASE_HEIGHT);
      m_background.SetWidth(m_background.GetHeight() * k);
  
  	Fvector2				map_center;
  	m_clipFrame.GetWndRect().getcenter(map_center);
  	m_background.SetWndPos	(map_center);
  
  	Fvector2 cp;
  	cp.x = m_clipFrame.GetWidth()/2.0f;
  	cp.y = m_clipFrame.GetHeight()/2.0f;
  	m_center.SetWndPos		(cp);
  
  	Fvector2 rel_pos		= m_compass.GetWndPos();
  	rel_pos.mul				(m_background.GetWndSize());
  	m_compass.SetWndPos		(rel_pos);
  
  	rel_pos					= m_clock_wnd->GetWndPos();
  	rel_pos.mul				(m_background.GetWndSize());
  	m_clock_wnd->SetWndPos	(rel_pos);
  
  	xml_init.InitStatic			(uiXml, "minimap:static_counter", 0, &m_Counter);
  	m_background.AttachChild	(&m_Counter);
  	xml_init.InitTextWnd		(uiXml, "minimap:static_counter:text_static", 0, &m_Counter_text);
  	m_Counter.AttachChild		(&m_Counter_text);
  
  	rel_pos						= m_Counter.GetWndPos();
  	rel_pos.mul					(m_background.GetWndSize());
  	m_Counter.SetWndPos			(rel_pos);
  }
  
  void CUIZoneMap::ConditionalInitDateWidget()
  {
      if (psActorFlags.test(AF_SHOWDATE))
      {
          if (m_date_wnd == nullptr)
          {
              CUIXml uiXml;
              uiXml.Load(CONFIG_PATH, UI_PATH, "ui_HUD.xml");
              m_date_wnd = UIHelper::CreateStatic(uiXml, "minimap:date_wnd", &m_background);
              m_date_wnd->SetAutoDelete(false);
  
              Fvector2 rel_pos;
              rel_pos = m_date_wnd->GetWndPos();
              rel_pos.mul(m_background.GetWndSize());
              m_date_wnd->SetWndPos(rel_pos);
          }
      }
      else
      {
          if (m_date_wnd != nullptr)
          {
              xr_delete(m_date_wnd);
          }
      }
  }
  
  void CUIZoneMap::Render			()
  {
      if (visible && !psActorFlags.test(AF_HARDCORE))
      {
          m_clipFrame.Draw();
          m_background.Draw();
      }
  }
  
  void CUIZoneMap::Update()
  {
  //	SetupCurrentMap();
      if (!visible) return; //Don't update, if we hided
  	CActor* pActor = smart_cast<CActor*>( Level().CurrentViewEntity() );
  	if ( !pActor ) return;
  
  	if (!(Device.dwFrame % 20))
  	{
  		string16	text_str;
  		xr_strcpy(text_str, sizeof(text_str), "");
  
  		CPda* pda = pActor->GetPDA();
  		if (pda)
  		{
  			u32 cn = pda->ActiveContactsNum();
  			if (cn > 0)
  				xr_sprintf(text_str, sizeof(text_str), "%d", cn);
  		}
  		m_Counter_text.SetText(text_str);
  	}
  
  	UpdateRadar( Level().CurrentControlEntity()->Position() );
  	float h, p;
  	Device.vCameraDirection.getHP( h, p );
  	SetHeading( -h );
  
  	m_clock_wnd->TextItemControl()->SetText( InventoryUtilities::GetGameTimeAsString( InventoryUtilities::etpTimeToMinutes ).c_str() );
  
      ConditionalInitDateWidget();
      if (psActorFlags.test(AF_SHOWDATE))
      {
  	    m_date_wnd->TextItemControl()->SetText( InventoryUtilities::GetGameNumDateAsString( InventoryUtilities::edpDateToDay ).c_str() );
      }
  }
  
  void CUIZoneMap::SetHeading		(float angle)
  {
  	m_activeMap->SetHeading(angle);
  	m_compass.SetHeading(angle + 110.0f);
  };
  
  void CUIZoneMap::UpdateRadar		(Fvector pos)
  {
  	m_clipFrame.Update();
  	m_background.Update();
  	m_activeMap->SetActivePoint( pos );
  }
  
  bool CUIZoneMap::ZoomIn()
  {
  	return true;
  }
  
  bool CUIZoneMap::ZoomOut()
  {
  	return true;
  }
  extern float minimap_zoom_factor;
  void CUIZoneMap::SetupCurrentMap()
  {
  	m_activeMap->Initialize			(Level().name(), "hud\\default");
  
  	Frect r;
  	m_clipFrame.GetAbsoluteRect		(r);	
  	m_activeMap->WorkingArea().set	(r);
  	
  	Fvector2						wnd_size;
  	float zoom_factor				= float(m_clipFrame.GetWidth())/100.0f * minimap_zoom_factor;
  
  	LPCSTR ln						= Level().name().c_str();
  	if(	pGameIni->section_exist(ln) )
  	{
  		if(pGameIni->line_exist(ln, "minimap_zoom"))
  			zoom_factor *= pGameIni->r_float(ln, "minimap_zoom");
  	}else
  	if(g_pGameLevel->pLevel->section_exist("minimap_zoom"))
  	{
  		zoom_factor *= g_pGameLevel->pLevel->r_float("minimap_zoom", "value");
  	}
  	wnd_size.x						= m_activeMap->BoundRect().width()*zoom_factor;
  	wnd_size.y						= m_activeMap->BoundRect().height()*zoom_factor;
  	m_activeMap->SetWndSize			(wnd_size);
  }
  
  void CUIZoneMap::OnSectorChanged(int sector)
  {
  	if(!g_pGameLevel->pLevel->section_exist("sub_level_map") )
  		return;
  	u8			map_idx = u8(-1);
  	string64	s_sector;
  	xr_sprintf	(s_sector, "%d", sector);
  	
  	if(!g_pGameLevel->pLevel->line_exist("sub_level_map", s_sector) )
  		return;
  
  	map_idx		= g_pGameLevel->pLevel->r_u8("sub_level_map", s_sector);
  	if(m_current_map_idx == map_idx)
  		return;
  
  	m_current_map_idx = map_idx;
  
  	string_path sub_texture;
  	xr_sprintf(sub_texture,"%s#%d", m_activeMap->m_texture.c_str(), m_current_map_idx);
  	
  	if(map_idx==u8(-1))
  		xr_sprintf(sub_texture,"%s", m_activeMap->m_texture.c_str());
  
  	m_activeMap->InitTextureEx(sub_texture, m_activeMap->m_shader_name.c_str());
  }
  
  void CUIZoneMap::Counter_ResetClrAnimation()
  {
  	m_Counter_text.ResetColorAnimation();
  }
  
  CUIMiniMap* CUIZoneMap::GetMinimap()
  {
      R_ASSERT2(m_activeMap, "Minimap is not created yet!");
      return m_activeMap;
  }
  