Files
WickedEngine/WickedEngine/wiBackLog.cpp
T
turanszkij de52c09298 refactors
2018-10-31 19:11:26 +00:00

202 lines
4.0 KiB
C++

#include "wiBackLog.h"
#include "wiMath.h"
#include "wiResourceManager.h"
#include "wiImageEffects.h"
#include "wiRenderer.h"
#include "wiTextureHelper.h"
#include "wiSpinLock.h"
#include "wiFont.h"
#include "wiImage.h"
#include "wiLua.h"
#include <mutex>
#include <sstream>
#include <deque>
#include <deque>
using namespace std;
using namespace wiGraphicsTypes;
wiSpinLock logLock;
namespace wiBackLog
{
enum State {
DISABLED,
IDLE,
ACTIVATING,
DEACTIVATING,
};
deque<string> stream;
deque<string> history;
State state = DISABLED;
const float speed = 50.0f;
unsigned int deletefromline = 100;
float pos = -FLT_MAX;
int scroll = 0;
stringstream inputArea;
int historyPos = 0;
wiFont font;
Texture2D* backgroundTex = nullptr;
void Toggle()
{
switch (state)
{
case IDLE:
state = DEACTIVATING;
break;
case DISABLED:
state = ACTIVATING;
break;
default:break;
};
}
void Scroll(int dir)
{
scroll += dir;
}
void Update()
{
if (state == DEACTIVATING)
pos -= speed;
else if (state == ACTIVATING)
pos += speed;
if (pos <= -wiRenderer::GetDevice()->GetScreenHeight())
{
state = DISABLED;
pos = -(float)wiRenderer::GetDevice()->GetScreenHeight();
}
else if (pos > 0)
{
state = IDLE;
pos = 0;
}
if (scroll + font.textHeight() > int(wiRenderer::GetDevice()->GetScreenHeight() * 0.8f))
{
scroll -= 1;
}
}
void Draw()
{
if (state != DISABLED)
{
if (backgroundTex == nullptr)
{
const unsigned char colorData[] = { 0, 0, 43, 200, 43, 31, 141, 223 };
HRESULT hr = wiTextureHelper::CreateTexture(backgroundTex, colorData, 1, 2, 4);
assert(SUCCEEDED(hr));
}
wiImageEffects fx = wiImageEffects((float)wiRenderer::GetDevice()->GetScreenWidth(), (float)wiRenderer::GetDevice()->GetScreenHeight());
fx.pos = XMFLOAT3(0, pos, 0);
fx.opacity = wiMath::Lerp(1, 0, -pos / wiRenderer::GetDevice()->GetScreenHeight());
wiImage::Draw(backgroundTex, fx, GRAPHICSTHREAD_IMMEDIATE);
font.SetText(getText());
font.props.posX = 50;
font.props.posY = (int)pos + (int)scroll;
font.Draw(GRAPHICSTHREAD_IMMEDIATE);
wiFont(inputArea.str().c_str(), wiFontProps(10, wiRenderer::GetDevice()->GetScreenHeight() - 10, -1, WIFALIGN_LEFT, WIFALIGN_BOTTOM)).Draw(GRAPHICSTHREAD_IMMEDIATE);
}
}
string getText()
{
logLock.lock();
stringstream ss("");
for (unsigned int i = 0; i < stream.size(); ++i)
ss << stream[i];
logLock.unlock();
return ss.str();
}
void clear()
{
logLock.lock();
stream.clear();
logLock.unlock();
}
void post(const char* input)
{
logLock.lock();
stringstream ss("");
ss << input << endl;
stream.push_back(ss.str().c_str());
if (stream.size() > deletefromline) {
stream.pop_front();
}
logLock.unlock();
}
void input(const char& input)
{
inputArea << input;
}
void acceptInput()
{
historyPos = 0;
stringstream commandStream("");
commandStream << inputArea.str();
post(inputArea.str().c_str());
history.push_back(inputArea.str());
if (history.size() > deletefromline) {
history.pop_front();
}
wiLua::GetGlobal()->RunText(inputArea.str());
inputArea.str("");
}
void deletefromInput()
{
stringstream ss(inputArea.str().substr(0, inputArea.str().length() - 1));
inputArea.str("");
inputArea << ss.str();
}
void save(ofstream& file)
{
for (deque<string>::iterator iter = stream.begin(); iter != stream.end(); ++iter)
file << iter->c_str();
file.close();
}
void historyPrev()
{
if (!history.empty())
{
inputArea.str("");
inputArea << history[history.size() - 1 - historyPos];
if ((size_t)historyPos < history.size() - 1)
historyPos++;
}
}
void historyNext()
{
if (!history.empty())
{
if (historyPos > 0)
historyPos--;
inputArea.str("");
inputArea << history[history.size() - 1 - historyPos];
}
}
void setBackground(Texture2D* texture)
{
backgroundTex = texture;
}
void setFontSize(int value)
{
font.props.size = value;
}
void setFontRowspacing(int value)
{
font.props.spacingY = value;
}
bool isActive() { return state == IDLE; }
}