Show InputBuffer.cpp syntax highlighted
/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG *
*Copyright (C) 2006, Tony Sparks *
* *
*This library is free software; you can redistribute it and/or *
*modify it under the terms of the GNU Lesser General Public *
*License as published by the Free Software Foundation; either *
*version 2.1 of the License, or (at your option) any later version. *
* *
*This library is distributed in the hope that it will be useful, *
*but WITHOUT ANY WARRANTY; without even the implied warranty of *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
*Lesser General Public License for more details. *
* *
*You should have received a copy of the GNU Lesser General Public *
*License along with this library; if not, write to the Free Software *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
**************************************************************************************
*/
// InputBuffer.cpp: implementation of the InputBuffer class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "InputBuffer.h"
#include "keyconverter.h"
#include <SDL.h>
/**
=============================
Constructor -
=============================
*/
InputBuffer::InputBuffer()
{
m_carrot = m_top = 0;
m_active = false;
m_upperCase = false;
buffer = "";
}
/**
=============================
Deconstructor
=============================
*/
InputBuffer::~InputBuffer()
{
}
/**
=============================
Retrieve the string to this buffer
=============================
*/
std::string InputBuffer::GetBuffer()
{
return buffer;
}
/*
=================
Set a new buffer
=================
*/
void InputBuffer::SetBuffer(const std::string &newBuffer)
{
buffer = newBuffer;
m_carrot = m_top =buffer.length();
}
/*
================
Get the current carrot pos
================
*/
int InputBuffer::GetCarrotPos()
{
return m_carrot;
}
/**
=================
Clear the buffer
=================
*/
void InputBuffer::Clear()
{
buffer.clear();
m_carrot = m_top = 0;
}
/*
===============
Append a char to the buffer
===============
*/
void InputBuffer::Append(char c)
{
if ( m_top < BUFFER_SIZE-1 )
{
// account for spaces
if ( c == 32 ) {
buffer.insert(m_carrot," ");
} else {
std::string temp(1, c);
buffer.insert(m_carrot, temp);
}
m_carrot++;
m_top++;
}
}
/*
================
Process this key
Process the key with SDL input
================
*/
void InputBuffer::ProcessKey( bool down, int key )
{
switch( key )
{
case SDLK_BACKSPACE:
m_carrot--;
if ( m_carrot < 0 ) {
m_carrot = 0;
return;
}
buffer.erase(m_carrot,1);
break;
case SDLK_DELETE:
if(m_carrot < m_top)
{ // delete that char
if ( m_carrot >= 0 ) {
buffer.erase(m_carrot, 1);
m_top--;
}
}
break;
case SDLK_RIGHT:
m_carrot++;
if ( m_carrot > m_top )
m_carrot = m_top;
break;
case SDLK_LEFT:
m_carrot--;
if ( m_carrot < 0 )
m_carrot = 0;
break;
case SDLK_LSHIFT:
case SDLK_RSHIFT:
m_upperCase = true;
break;
default:
if ( m_carrot < BUFFER_SIZE )
{
if ( key > 31 && key < 127 ) {
if ( key == 32 )
Append( (char)32 );
else if ( m_upperCase )
Append ( KeyConverter::GetShiftValue( key ) );
else
Append( (*SDL_GetKeyName(static_cast<SDLKey>(key)) ) );
}
}
m_upperCase = false;
}
}
/**
========================
Test to see if this buffer is
active
========================
*/
bool InputBuffer::IsActive()
{
return m_active;
}
/**
========================
Set the active status of this
buffer
========================
*/
void InputBuffer::SetActive(bool b)
{
m_active = b;
}
See more files for this project here