ctr_Repair/trunk/ConsoleDataMigration/sources/common/LogConsole.cpp
N2614 6904d77e61 末尾を表示中のみ自動スクロールするように
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-05-23%20-%20ctr.7z%20+%20svn_v1.068.zip/ctr/svn/ctr_Repair@307 385bec56-5757-e545-9c3a-d8741f4650f1
2011-06-03 02:30:54 +00:00

173 lines
4.2 KiB
C++

/*---------------------------------------------------------------------------*
Project: Horizon
File: LogConsole.cpp
Copyright 2009 Nintendo. All rights reserved.
These coded instructions, statements, and computer programs contain
proprietary information of Nintendo of America Inc. and/or Nintendo
Company Ltd., and are protected by Federal copyright law. They may
not be disclosed to third parties or copied or duplicated in any form,
in whole or in part, without the prior written consent of Nintendo.
$Rev$
*---------------------------------------------------------------------------*/
#include "LogConsole.h"
#include <nn/nstd.h>
namespace common
{
const size_t FONT_WIDTH = 8;
const size_t FONT_HEIGHT = 10;
static LogConsole s_LogConsole;
LogConsole* GetConsoleInstance()
{
return &s_LogConsole;
}
LogConsole::LogConsole()
{
}
LogConsole::~LogConsole()
{
}
void LogConsole::Initialize(u32 width, u32 height, u32 maxLine, demo::RenderSystemDrawing* renderSystem)
{
m_Width = width;
m_Height = height;
m_MaxLine = maxLine;
m_RenderSystem = renderSystem;
m_CurrentViewLine = 0;
m_LineNum = 0;
}
void LogConsole::AddText(const char* fmt, ::std::va_list arg)
{
s32 stringSize;
const size_t STRING_BUFFER_SIZE = 256;
char formatStr[STRING_BUFFER_SIZE];
stringSize = nn::nstd::TVSNPrintf(formatStr, sizeof(formatStr), fmt, arg);
::std::string str(formatStr);
size_t addedText = 0;
while (addedText < stringSize)
{
if(m_LineNum >= m_MaxLine)
{
// 満杯なので先頭を削除する
::std::vector<std::string>::iterator it;
it = m_Log.begin();
m_Log.erase(it);
m_LineNum--;
}
// 部分文字列を追加
AddWrapedText(str.substr(addedText, m_Width).c_str());
m_LineNum++;
// 画面領域の末尾を描画中 かつ
// 画面領域以上追加したら末尾にスクロールする
if(m_CurrentViewLine == (m_LineNum - m_Height - 1) && m_LineNum > m_Height)
{
ScrollToEnd();
}
if(stringSize - addedText > m_Width)
{
addedText += m_Width;
}
else
{
addedText += stringSize - addedText;
}
}
}
void LogConsole::ScrollUp()
{
if(m_CurrentViewLine > 0)
{
m_CurrentViewLine--;
}
}
void LogConsole::ScrollDown()
{
if (m_LineNum > m_Height)
{
if (m_CurrentViewLine < m_LineNum - m_Height)
{
m_CurrentViewLine++;
}
}
}
void LogConsole::ScrollToBegin()
{
m_CurrentViewLine = 0;
}
void LogConsole::ScrollToEnd()
{
if(m_LineNum > m_Height)
{
m_CurrentViewLine = m_LineNum - m_Height;
}
}
void LogConsole::Print()
{
::std::vector<std::string>::iterator it;
it = m_Log.begin();
it += m_CurrentViewLine;
u32 count = 0;
for(; it != m_Log.end() && count < m_Height && count < m_MaxLine; it++)
{
m_RenderSystem->DrawText(0, count++ * 10, "%s", it->c_str());
}
if(m_LineNum > m_Height)
{
DrawScrollBar();
}
}
void LogConsole::AddWrapedText(const char* str)
{
m_Log.push_back(::std::string(str));
}
void LogConsole::DrawScrollBar()
{
m_RenderSystem->SetColor(0.4f, 0.4f, 0.4f);
m_RenderSystem->DrawLine((m_Width + 1) * FONT_WIDTH, 0, (m_Width + 2) * FONT_WIDTH - 1, 0);
m_RenderSystem->DrawLine((m_Width + 1)* FONT_WIDTH, 0, (m_Width + 1)* FONT_WIDTH, m_Height * FONT_HEIGHT);
m_RenderSystem->DrawLine((m_Width + 2) * FONT_WIDTH - 1, 0, (m_Width + 2) * FONT_WIDTH - 1, m_Height * FONT_HEIGHT);
m_RenderSystem->DrawLine((m_Width + 1)* FONT_WIDTH, m_Height * FONT_HEIGHT - 1, (m_Width + 2) * FONT_WIDTH - 1, m_Height * FONT_HEIGHT - 1);
u32 y = (m_Height * FONT_HEIGHT - 2) * m_CurrentViewLine / m_MaxLine;
m_RenderSystem->SetColor(0.7f, 0.7f, 0.7f);
m_RenderSystem->FillRectangle((m_Width + 1) * FONT_WIDTH, y + 1, FONT_WIDTH - 1, FONT_HEIGHT - 4);
m_RenderSystem->SetColor(1.f, 1.f, 1.f);
}
} //namespace ConsoleBackup