The-Powder-Toy/src/interface/Textbox.cpp

169 lines
3.0 KiB
C++
Raw Normal View History

2012-01-20 16:07:49 -06:00
#include <string>
2012-01-24 17:33:32 -06:00
#include <iostream>
2012-01-20 16:07:49 -06:00
#include <stdexcept>
#include "Config.h"
#include "interface/Point.h"
#include "interface/Textbox.h"
#include "interface/Keys.h"
using namespace ui;
Textbox::Textbox(Point position, Point size, std::string textboxText):
Component(position, size),
text(textboxText),
2012-01-24 14:19:19 -06:00
actionCallback(NULL),
masked(false),
border(true)
2012-01-20 16:07:49 -06:00
{
SetText(textboxText);
2012-01-20 16:07:49 -06:00
cursor = text.length();
}
Textbox::~Textbox()
{
if(actionCallback)
delete actionCallback;
}
void Textbox::TextPosition()
{
if(cursor)
{
cursorPosition = Graphics::textnwidth((char *)displayText.c_str(), cursor);
}
else
{
cursorPosition = 0;
}
Component::TextPosition(displayText);
}
2012-01-20 16:07:49 -06:00
void Textbox::SetText(std::string text)
{
cursor = text.length();
2012-01-20 16:07:49 -06:00
this->text = text;
this->displayText = text;
TextPosition();
2012-01-20 16:07:49 -06:00
}
void Textbox::SetDisplayText(std::string text)
{
displayText = text;
TextPosition();
}
2012-01-20 16:07:49 -06:00
std::string Textbox::GetText()
{
return text;
}
void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
2012-01-20 16:07:49 -06:00
{
bool changed = false;
try
{
switch(key)
{
case KEY_HOME:
cursor = 0;
break;
case KEY_END:
cursor = text.length();
break;
case KEY_LEFT:
if(cursor > 0)
cursor--;
break;
case KEY_RIGHT:
if(cursor < text.length())
cursor++;
break;
case KEY_DELETE:
if(text.length() && cursor < text.length())
{
if(ctrl)
text.erase(cursor, text.length()-cursor);
else
text.erase(cursor, 1);
changed = true;
}
break;
case KEY_BACKSPACE:
if(text.length() && cursor > 0)
{
if(ctrl)
{
text.erase(0, cursor);
cursor = 0;
}
else
{
text.erase(cursor-1, 1);
cursor--;
}
changed = true;
}
break;
}
if(character >= ' ' && character < 127)
{
if(cursor == text.length())
2012-01-20 16:07:49 -06:00
{
text += character;
2012-01-20 16:07:49 -06:00
}
else
{
text.insert(cursor, 1, (char)character);
}
cursor++;
changed = true;
2012-01-20 16:07:49 -06:00
}
}
catch(std::out_of_range &e)
2012-01-20 16:07:49 -06:00
{
cursor = 0;
text = "";
}
if(changed)
{
if(masked)
{
char * tempText = new char[text.length()+1];
std::fill(tempText, tempText+text.length(), 0x8d);
tempText[text.length()] = 0;
displayText = std::string(tempText);
delete tempText;
}
else
{
displayText = text;
}
if(actionCallback)
actionCallback->TextChangedCallback(this);
}
TextPosition();
2012-01-20 16:07:49 -06:00
}
void Textbox::Draw(const Point& screenPos)
{
2012-05-15 12:13:17 -05:00
if(!drawn)
{
TextPosition();
2012-05-15 12:13:17 -05:00
drawn = true;
}
2012-01-20 16:07:49 -06:00
Graphics * g = Engine::Ref().g;
if(IsFocused())
{
if(border) g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255);
2012-01-20 16:07:49 -06:00
g->draw_line(screenPos.X+textPosition.X+cursorPosition, screenPos.Y+3, screenPos.X+textPosition.X+cursorPosition, screenPos.Y+12, 255, 255, 255, XRES+BARSIZE);
}
else
{
if(border) g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 160, 160, 160, 255);
2012-01-20 16:07:49 -06:00
}
g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, displayText, 255, 255, 255, 255);
if(Appearance.icon)
g->draw_icon(screenPos.X+iconPosition.X, screenPos.Y+iconPosition.Y, Appearance.icon);
2012-01-20 16:07:49 -06:00
}