#include "RuScaleformEngine_Core.h"

#pragma managed(push, off)

// ************************************************************************************************************************************************************

ruRTTI_DEFINE_SUBCLASS(CRuScaleformEngine, IRuObject)
ruHEAP_DEFINE_SUBCLASS(CRuScaleformEngine, "CRuScaleformEngine", "IRuObject")
ruCLASSGUID_DEFINE(CRuScaleformEngine, 0x7889DC99, 0x6DC14735, 0xB20D520D, 0xFD0CAC76)

// ************************************************************************************************************************************************************

CRuScaleformEngine::CRuScaleformEngine()
:	m_driver(NULL)
{
}

CRuScaleformEngine::~CRuScaleformEngine()
{
	ruSAFE_RELEASE(m_driver);
}

BOOL CRuScaleformEngine::Initialize(IRuScaleformDriver *driver)
{
	// Store reference to driver
	ruSAFE_ADDREF(driver);
	ruSAFE_RELEASE(m_driver);
	m_driver = driver;

	// Create new draw text manager from loader
	m_drawTextManager = *new GFxDrawTextManager(m_driver->GetLoader());

	// Create new render config
	m_renderConfig = *new GFxRenderConfig(m_driver->GetRenderer(), GFxRenderConfig::RF_StrokeNormal);
//	m_renderConfig->SetRenderFlags(m_renderConfig->GetRenderFlags() | GFxRenderConfig::RF_EdgeAA);

	// Assign render config to draw text manager
	m_drawTextManager->SetRenderConfig(m_renderConfig);

	GPtr<GFxFontProviderWin32> fontProvider = *new GFxFontProviderWin32(::GetDC(0));
	m_drawTextManager->SetFontProvider(fontProvider);

	return TRUE;
}

void CRuScaleformEngine::TestAPI()
{
	if(m_testText.GetPtr() == NULL)
	{
		// Create text using plain text and default text parameters.
		GFxDrawTextManager::TextParams defParams = m_drawTextManager->GetDefaultTextParams();
		defParams.TextColor = GColor(0xFF, 0xFF, 0xFF, 0xFF); // red, alpha = 255
//		defParams.FontName  = "Arial Unicode MS";
		defParams.FontName  = "AR HeiU30 Medium";
		defParams.FontSize  = 12;                       // Slate Mobile, 16 pts will be our default font

		// now, default text will be created with red color, font Slate Mobile, 16 pts
		m_drawTextManager->SetDefaultTextParams(defParams);

		// Since we are using pdm1, the newly created text will use system fonts.

		wchar_t wideStr[1024];
		MultiByteToWideChar(CP_ACP, 0, "Wir begrüßenAabcdefghijklmnopqrstuvwxyz 這是奇怪的測是龍牙山脊的任務「取得清水」。經過修正，現在水槽裡又有水了。", -1, wideStr, 1024);

		m_testText = *m_drawTextManager->CreateText(wideStr, GRectF(0, 20, 500, 400));
	}

	CRuRectangle viewportRect = m_driver->GetViewportRect();
	GViewport vp(viewportRect.m_right, viewportRect.m_bottom, 0, 0, viewportRect.m_right, viewportRect.m_bottom, 0);

	m_drawTextManager->BeginDisplay(vp);

	m_testText->Display();

	m_drawTextManager->EndDisplay();

	m_driver->ClearEngineStates();
}

// ************************************************************************************************************************************************************

#pragma managed(pop)
