metaforce/Runtime/GuiSys/CGuiTextSupport.cpp

310 lines
8.5 KiB
C++
Raw Normal View History

2016-03-19 00:07:31 +00:00
#include "CGuiTextSupport.hpp"
#include "CSimplePool.hpp"
#include "CFontImageDef.hpp"
2016-03-21 22:01:19 +00:00
#include "CGuiSys.hpp"
#include "CTextExecuteBuffer.hpp"
#include "CTextParser.hpp"
#include "Graphics/CGraphicsPalette.hpp"
2016-03-21 22:01:19 +00:00
#include "Graphics/CGraphics.hpp"
2016-03-22 18:35:52 +00:00
#include "GuiSys/CRasterFont.hpp"
2016-03-19 00:07:31 +00:00
namespace urde
{
2016-04-14 21:42:47 +00:00
CGuiTextSupport::CGuiTextSupport(ResId fontId, const CGuiTextProperties& props,
2016-03-22 02:27:46 +00:00
const zeus::CColor& fontCol, const zeus::CColor& outlineCol,
2017-01-29 03:58:16 +00:00
const zeus::CColor& geomCol, s32 padX, s32 padY, CSimplePool* store,
CGuiWidget::EGuiModelDrawFlags drawFlags)
2016-08-15 20:58:07 +00:00
: x14_props(props), x24_fontColor(fontCol), x28_outlineColor(outlineCol),
2017-01-29 03:58:16 +00:00
x2c_geometryColor(geomCol), x34_extentX(padX), x38_extentY(padY), x5c_fontId(fontId),
m_drawFlags(drawFlags)
{
2016-08-15 20:58:07 +00:00
x2cc_font = store->GetObj({SBIG('FONT'), fontId});
}
2016-12-30 06:37:01 +00:00
CTextRenderBuffer* CGuiTextSupport::GetCurrentPageRenderBuffer() const
{
2016-12-29 05:53:00 +00:00
if (x60_renderBuf && !x308_multipageFlag)
2016-12-16 23:05:29 +00:00
return const_cast<CTextRenderBuffer*>(&*x60_renderBuf);
2016-12-29 05:53:00 +00:00
if (!x308_multipageFlag || x300_ <= x304_pageCounter)
2016-12-16 23:05:29 +00:00
return nullptr;
int idx = 0;
2016-12-31 00:51:51 +00:00
for (const CTextRenderBuffer& buf : x2ec_renderBufferPages)
2016-12-29 05:53:00 +00:00
if (idx++ == x304_pageCounter)
2016-12-16 23:05:29 +00:00
return const_cast<CTextRenderBuffer*>(&buf);
return nullptr;
}
2016-12-16 23:05:29 +00:00
float CGuiTextSupport::GetCurrentAnimationOverAge() const
{
float ret = 0.f;
2016-12-30 06:37:01 +00:00
if (CTextRenderBuffer* buf = GetCurrentPageRenderBuffer())
2016-03-21 22:01:19 +00:00
{
2016-12-16 23:05:29 +00:00
if (x50_typeEnable)
{
if (x40_primStartTimes.size())
{
auto& lastTime = x40_primStartTimes.back();
ret = std::max(ret, (buf->GetPrimitiveCount() - lastTime.second) / x58_chRate + lastTime.first);
}
else
{
ret = std::max(ret, buf->GetPrimitiveCount() / x58_chRate);
}
}
2016-03-21 22:01:19 +00:00
}
2016-12-16 23:05:29 +00:00
return ret;
}
2017-01-09 03:44:00 +00:00
float CGuiTextSupport::GetNumCharsTotal() const
{
if (CTextRenderBuffer* buf = GetCurrentPageRenderBuffer())
if (x50_typeEnable)
return buf->GetPrimitiveCount();
return 0.f;
}
2016-03-21 22:01:19 +00:00
float CGuiTextSupport::GetNumCharsPrinted() const
{
2016-12-30 06:37:01 +00:00
if (CTextRenderBuffer* buf = GetCurrentPageRenderBuffer())
2016-12-16 23:05:29 +00:00
{
if (x50_typeEnable)
{
float charsPrinted = x3c_curTime * x58_chRate;
return std::min(charsPrinted, float(buf->GetPrimitiveCount()));
}
}
2016-03-21 22:01:19 +00:00
return 0.f;
}
2016-03-21 22:01:19 +00:00
float CGuiTextSupport::GetTotalAnimationTime() const
{
2016-12-30 06:37:01 +00:00
if (CTextRenderBuffer* buf = GetCurrentPageRenderBuffer())
2016-12-16 23:05:29 +00:00
if (x50_typeEnable)
return buf->GetPrimitiveCount() / x58_chRate;
return 0.f;
}
2016-12-16 23:05:29 +00:00
bool CGuiTextSupport::IsAnimationDone() const
2016-12-16 04:35:49 +00:00
{
return x3c_curTime >= GetTotalAnimationTime();
}
2016-03-21 22:01:19 +00:00
void CGuiTextSupport::SetTypeWriteEffectOptions(bool enable, float chFadeTime, float chRate)
{
2016-08-15 20:58:07 +00:00
x50_typeEnable = enable;
x54_chFadeTime = std::max(chFadeTime, 0.0001f);
x58_chRate = std::max(chRate, 1.f);
}
void CGuiTextSupport::Update(float dt)
{
2016-08-15 20:58:07 +00:00
if (x50_typeEnable)
2016-03-21 22:01:19 +00:00
{
2016-12-30 06:37:01 +00:00
if (CTextRenderBuffer* buf = GetCurrentPageRenderBuffer())
2016-03-21 22:01:19 +00:00
{
float chStartTime = 0.f;
for (s32 i=0 ; i<buf->GetPrimitiveCount() ; ++i)
2016-03-21 22:01:19 +00:00
{
2016-12-16 23:05:29 +00:00
for (const std::pair<float, int>& p : x40_primStartTimes)
{
if (p.second < i)
continue;
if (p.second != i)
break;
chStartTime = p.first;
2016-03-21 22:01:19 +00:00
break;
2016-12-16 23:05:29 +00:00
}
2016-03-21 22:01:19 +00:00
2016-12-16 23:05:29 +00:00
buf->SetPrimitiveOpacity(i,
std::min(std::max(0.f, (x3c_curTime - chStartTime) / x54_chFadeTime), 1.f));
chStartTime += 1.f / x58_chRate;
2016-12-16 23:05:29 +00:00
}
2016-03-21 22:01:19 +00:00
}
2016-12-16 23:05:29 +00:00
x3c_curTime += dt;
2016-03-21 22:01:19 +00:00
}
2016-12-16 23:05:29 +00:00
x10_curTimeMod900 = std::fmod(x10_curTimeMod900 + dt, 900.f);
}
2016-12-16 23:05:29 +00:00
void CGuiTextSupport::ClearRenderBuffer()
{
2016-08-15 20:58:07 +00:00
x60_renderBuf = std::experimental::nullopt;
2016-12-31 00:51:51 +00:00
x2ec_renderBufferPages.clear();
}
2016-12-30 06:37:01 +00:00
void CGuiTextSupport::CheckAndRebuildTextBuffer()
{
2016-03-21 22:01:19 +00:00
g_TextExecuteBuf->Clear();
2016-12-16 23:05:29 +00:00
g_TextExecuteBuf->x18_textState.x7c_enableWordWrap = x14_props.x0_wordWrap;
2017-01-30 04:16:20 +00:00
g_TextExecuteBuf->BeginBlock(0, 0, x34_extentX, x38_extentY, x30_imageBaseline,
ETextDirection(!x14_props.x1_horizontal),
2016-08-15 20:58:07 +00:00
x14_props.x4_justification, x14_props.x8_vertJustification);
g_TextExecuteBuf->AddColor(EColorType::Main, x24_fontColor);
g_TextExecuteBuf->AddColor(EColorType::Outline, x28_outlineColor);
2016-03-21 22:01:19 +00:00
2017-01-24 07:41:33 +00:00
std::u16string initStr;
2016-08-15 20:58:07 +00:00
if ((x5c_fontId & 0xffff) != 0xffff)
2017-01-24 07:41:33 +00:00
initStr = hecl::Char16Format(L"&font=%08X;", u32(x5c_fontId));
2016-03-21 22:01:19 +00:00
initStr += x0_string;
g_TextParser->ParseText(*g_TextExecuteBuf, initStr.c_str(), initStr.size());
g_TextExecuteBuf->EndBlock();
}
2016-12-30 06:37:01 +00:00
bool CGuiTextSupport::CheckAndRebuildRenderBuffer()
{
if (x308_multipageFlag || x60_renderBuf)
if (!x308_multipageFlag || x300_)
return true;
CheckAndRebuildTextBuffer();
x2bc_assets = g_TextExecuteBuf->GetAssets();
if (!_GetIsTextSupportFinishedLoading())
return false;
CheckAndRebuildTextBuffer();
if (x308_multipageFlag)
{
zeus::CVector2i extent(x34_extentX, x38_extentY);
2017-01-29 03:58:16 +00:00
x2ec_renderBufferPages = g_TextExecuteBuf->BuildRenderBufferPages(extent, m_drawFlags);
2016-12-31 00:51:51 +00:00
}
else
{
2017-01-29 03:58:16 +00:00
x60_renderBuf.emplace(g_TextExecuteBuf->BuildRenderBuffer(m_drawFlags));
2016-12-31 00:51:51 +00:00
x2dc_oneBufBounds = x60_renderBuf->AccumulateTextBounds();
2016-12-30 06:37:01 +00:00
}
2016-12-31 00:51:51 +00:00
g_TextExecuteBuf->Clear();
Update(0.f);
2016-12-30 18:38:01 +00:00
return true;
2016-12-30 06:37:01 +00:00
}
2016-12-31 00:51:51 +00:00
const std::pair<zeus::CVector2i, zeus::CVector2i>& CGuiTextSupport::GetBounds()
{
CheckAndRebuildRenderBuffer();
return x2dc_oneBufBounds;
}
void CGuiTextSupport::AutoSetExtent()
{
auto& bounds = GetBounds();
x34_extentX = bounds.second.x;
x38_extentY = bounds.second.y;
}
void CGuiTextSupport::Render() const
{
2017-01-29 03:58:16 +00:00
const_cast<CGuiTextSupport*>(this)->CheckAndRebuildRenderBuffer();
2016-12-30 06:37:01 +00:00
if (CTextRenderBuffer* buf = GetCurrentPageRenderBuffer())
2016-03-21 22:01:19 +00:00
{
zeus::CTransform oldModel = CGraphics::g_GXModelMatrix;
CGraphics::SetModelMatrix(oldModel * zeus::CTransform::Scale(1.f, 1.f, -1.f));
2016-12-16 23:05:29 +00:00
buf->Render(x2c_geometryColor, x10_curTimeMod900);
2016-03-21 22:01:19 +00:00
CGraphics::SetModelMatrix(oldModel);
}
}
void CGuiTextSupport::SetGeometryColor(const zeus::CColor& col)
{
2016-08-15 20:58:07 +00:00
x2c_geometryColor = col;
}
void CGuiTextSupport::SetOutlineColor(const zeus::CColor& col)
{
2016-08-15 20:58:07 +00:00
if (col != x28_outlineColor)
2016-03-21 22:01:19 +00:00
{
2016-12-16 23:05:29 +00:00
ClearRenderBuffer();
2016-08-15 20:58:07 +00:00
x28_outlineColor = col;
2016-03-21 22:01:19 +00:00
}
}
void CGuiTextSupport::SetFontColor(const zeus::CColor& col)
{
2016-08-15 20:58:07 +00:00
if (col != x24_fontColor)
2016-03-21 22:01:19 +00:00
{
2016-12-16 23:05:29 +00:00
ClearRenderBuffer();
2016-08-15 20:58:07 +00:00
x24_fontColor = col;
2016-03-21 22:01:19 +00:00
}
}
2017-01-24 07:41:33 +00:00
void CGuiTextSupport::AddText(const std::u16string& str)
{
2016-12-16 23:05:29 +00:00
if (x60_renderBuf)
2016-03-21 22:01:19 +00:00
{
float t = GetCurrentAnimationOverAge();
2016-12-16 23:05:29 +00:00
x40_primStartTimes.push_back(std::make_pair(std::max(t, x3c_curTime),
2016-08-15 20:58:07 +00:00
x60_renderBuf->GetPrimitiveCount()));
2016-03-21 22:01:19 +00:00
}
x0_string += str;
2016-12-16 23:05:29 +00:00
ClearRenderBuffer();
}
2017-01-24 07:41:33 +00:00
void CGuiTextSupport::SetText(const std::u16string& str, bool multipage)
{
2016-03-21 22:01:19 +00:00
if (x0_string.compare(str))
{
2016-12-16 23:05:29 +00:00
x40_primStartTimes.clear();
2016-08-15 20:58:07 +00:00
x3c_curTime = 0.f;
2016-03-21 22:01:19 +00:00
x0_string = str;
2016-12-16 23:05:29 +00:00
ClearRenderBuffer();
2016-12-29 05:53:00 +00:00
x308_multipageFlag = multipage;
x304_pageCounter = 0;
2016-03-21 22:01:19 +00:00
}
}
2016-12-29 05:53:00 +00:00
void CGuiTextSupport::SetText(const std::string& str, bool multipage)
{
2017-01-24 07:41:33 +00:00
SetText(hecl::UTF8ToChar16(str), multipage);
}
2016-12-30 06:37:01 +00:00
bool CGuiTextSupport::_GetIsTextSupportFinishedLoading() const
2016-03-19 00:07:31 +00:00
{
2016-12-16 23:05:29 +00:00
for (const CToken& tok : x2bc_assets)
2016-03-21 22:01:19 +00:00
{
2016-12-30 06:37:01 +00:00
const_cast<CToken&>(tok).Lock();
2016-03-21 22:01:19 +00:00
if (!tok.IsLoaded())
return false;
}
2017-01-30 04:16:20 +00:00
if (x2cc_font.IsLoaded())
return x2cc_font->IsFinishedLoading();
return false;
}
void CGuiTextSupport::SetJustification(EJustification j)
{
if (j != x14_props.x4_justification)
{
x14_props.x4_justification = j;
ClearRenderBuffer();
}
}
void CGuiTextSupport::SetVerticalJustification(EVerticalJustification j)
{
if (j != x14_props.x8_vertJustification)
{
x14_props.x8_vertJustification = j;
ClearRenderBuffer();
}
}
void CGuiTextSupport::SetImageBaseline(bool b)
{
if (b != x30_imageBaseline)
{
x30_imageBaseline = b;
ClearRenderBuffer();
}
2016-03-19 00:07:31 +00:00
}
2016-12-30 06:37:01 +00:00
bool CGuiTextSupport::GetIsTextSupportFinishedLoading() const
{
const_cast<CGuiTextSupport*>(this)->CheckAndRebuildRenderBuffer();
return _GetIsTextSupportFinishedLoading();
}
2016-03-19 00:07:31 +00:00
}