#include "stdafx.h"
#include "ConsoleScreenView.h"
#include "UiTextBox.h"
#include "UiPanel.h"
#include "UiWidgetManager.h"
#include "UiRenderContext.h"
#include "UiManager.h"
//////////////////////////////////////////////////////////////////////////
#define CONSOLE_SEPARATOR_STR L". . . . . . . . . . . . . . . . . . . . . . . . . . . . ."
#define UI_CONSOLE_FADE_SPEED 12.0f
enum
{
UI_CONSOLE_NUM_TEXTLINES = 21,
UI_CONSOLE_X_OFFSET = 6,
UI_CONSOLE_CURSOR_BLINKING_TIME_MSECONDS = 1200,
UI_CONSOLE_MAX_INPUT_HISTORY_RECORDS = 32,
};
//////////////////////////////////////////////////////////////////////////
ConsoleScreenView::ConsoleScreenView()
: UiView(eUiViewLayer_Overlay)
{
}
void ConsoleScreenView::ToggleConsole()
{
if (IsActive())
{
mShowStatus = CurrentShowStatus_FadeOut;
return;
}
Activate();
}
bool ConsoleScreenView::LoadContent()
{
if (IsHierarchyLoaded())
return true;
mConsoleFont = gUiManager.GetDefaultFont(eDefaultFont::Console);
cxx_assert(mConsoleFont);
if (mConsoleFont)
{
mConsoleTextInvalidated = true;
mConsoleScroll = 0;
mHistoryCursor = -1;
const GameProfile::UserSettings& userSettings = gGameProfile.GetUserSettings();
// setup console dimensions
mConsoleRect.x = 0;
mConsoleRect.y = 0;
mConsoleRect.w = userSettings.mScreenResolution.x;
mConsoleRect.h = (UI_CONSOLE_NUM_TEXTLINES + 1) * mConsoleFont->GetLineHeight();
}
return true;
}
void ConsoleScreenView::Cleanup()
{
UiView::Cleanup();
mHistoryTextBatch.clear();
}
void ConsoleScreenView::RenderFrame(UiRenderContext& renderContext)
{
// animate with transform
if (mShowStatus != CurrentShowStatus_Open)
{
glm::vec3 translation { 0.0f, (mFadeProgress * mConsoleRect.h) - mConsoleRect.h, 0.0f };
glm::mat4 translation_matrix = glm::translate(translation);
renderContext.SetTransform(&translation_matrix);
}
renderContext.FillRect(mConsoleRect, MAKE_RGBA(28, 28, 28, 190));
renderContext.DrawTextQuads(mConsoleFont, mHistoryTextBatch);
Rect2D frameRect = mConsoleRect;
frameRect.y -= 10;
frameRect.h += 10;
renderContext.DrawRect(frameRect, MAKE_RGBA(176, 163, 95, 255));
renderContext.SetTransform(nullptr);
}
void ConsoleScreenView::UpdateFrame(float deltaTime)
{
if (mShowStatus == CurrentShowStatus_FadeIn)
{
mFadeProgress += UI_CONSOLE_FADE_SPEED * deltaTime;
if (mFadeProgress >= 1.0f)
{
mFadeProgress = 1.0f;
mShowStatus = CurrentShowStatus_Open;
}
return;
}
if (mShowStatus == CurrentShowStatus_FadeOut)
{
mFadeProgress -= UI_CONSOLE_FADE_SPEED * deltaTime;
if (mFadeProgress GetLineHeight();
if (mConsoleScroll)
{
positiony = (UI_CONSOLE_NUM_TEXTLINES - iline - 2) * mConsoleFont->GetLineHeight();
}
mConsoleFont->BuildTextMesh(rec.mMessage, Point2D{ UI_CONSOLE_X_OFFSET, positiony }, textColor, mBufferTextBatch);
// combine batch
mHistoryTextBatch.insert(mHistoryTextBatch.end(),
mBufferTextBatch.begin(),
mBufferTextBatch.end());
mBufferTextBatch.clear();
}
// add line separator
if (mConsoleScroll)
{
int positiony = (UI_CONSOLE_NUM_TEXTLINES - 1) * mConsoleFont->GetLineHeight();
mConsoleFont->BuildTextMesh(CONSOLE_SEPARATOR_STR, Point2D{ UI_CONSOLE_X_OFFSET, positiony }, COLOR_WHITE, mBufferTextBatch);
// combine batch
mHistoryTextBatch.insert(mHistoryTextBatch.end(),
mBufferTextBatch.begin(),
mBufferTextBatch.end());
mBufferTextBatch.clear();
}
}