Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
7 KB
Referenced Files
None
Subscribers
None
diff --git a/util/input/text-input.cpp b/util/input/text-input.cpp
index 4e3bcee0..5ac3c9c4 100644
--- a/util/input/text-input.cpp
+++ b/util/input/text-input.cpp
@@ -1,210 +1,210 @@
#include "input-map.h"
#include "text-input.h"
#include "input-manager.h"
#include "keyboard.h"
#include <string.h>
#include <sstream>
#include <string>
using namespace std;
static const int Shift = 198;
static const int Control = 199;
static const int Backspace = 200;
TextInput::TextInput(const string & start):
InputMap<unsigned char>(),
blockingKeys(false),
enabled(false),
handle(201){
const int delay = 100;
/*
set(Keyboard::Key_TILDE, delay * 2, false, Toggle);
set(Keyboard::Key_ESC, delay, false, Esc);
set(Keyboard::Key_ENTER, 0, false, Enter);
*/
// set(Keyboard::Key_LCONTROL, 0, false, Control);
set(Keyboard::Key_BACKSPACE, delay, false, Backspace);
/*
set(Keyboard::Key_LSHIFT, 0, false, Shift);
set(Keyboard::Key_RSHIFT, 0, false, Shift);
*/
text.str(start);
text.rdbuf()->pubseekoff(0, ios_base::end, ios_base::out);
text.clear();
}
int TextInput::nextHandle(){
int i = handle;
handle += 1;
return i;
}
void TextInput::addBlockingHandle(int key, callback function, void * data){
int handle = nextHandle();
set(key, 1, true, handle);
callbacks[handle] = Callback(function, data);
}
void TextInput::addHandle(int key, int delay, callback function, void * data){
int handle = nextHandle();
set(key, delay, false, handle);
callbacks[handle] = Callback(function, data);
}
bool TextInput::doInput(){
bool modified = false;
/* TODO: ensure these codes are consistent across platforms. So far
* they seem to work in windows xp and linux (ubuntu)
*/
const Keyboard::unicode_t control_u = 21;
const Keyboard::unicode_t control_w = 23;
if (enabled){
vector<InputEvent> events = InputManager::getEvents(*this);
/* the order of reading input is arbitrary right now. I'm not
* sure it matters what order things are done in, but probably
* a few corner cases exist. When they come up please document them.
*/
for (vector<InputEvent>::iterator it = events.begin(); it != events.end(); it++){
InputEvent event = *it;
if (event.enabled){
if (event.out == Backspace){
backspace();
modified = true;
}
if (callbacks.find(event.out) != callbacks.end()){
Callback & callback = callbacks[event.out];
callback.function(callback.data);
}
if (event.unicode == control_u){
clearInput();
modified = true;
}
if (event.unicode == control_w){
deleteLastWord();
modified = true;
}
/* FIXME: whats the maximum unicode value? */
if (event.unicode >= 32 && event.unicode < 0xffffff){
this->text << (unsigned char) event.unicode;
modified = true;
}
}
}
}
return modified;
}
void TextInput::enable(){
InputManager::captureInput(*this);
enabled = true;
Keyboard::pushRepeatState(true);
}
void TextInput::disable(){
InputManager::releaseInput(*this);
enabled = false;
Keyboard::popRepeatState();
}
-string TextInput::getText(){
+string TextInput::getText() const {
return text.str();
}
/*
void TextInput::clear(){
text.str("");
text.clear();
}
*/
void TextInput::setText(const std::string & text){
this->text.str(text);
this->text.rdbuf()->pubseekoff(0, ios_base::end, ios_base::out);
this->text.clear();
}
void TextInput::clearInput(){
text.str(string());
text.rdbuf()->pubseekoff(0, ios_base::end, ios_base::out);
text.clear();
}
void TextInput::backspace(){
string now = text.str();
now = now.substr(0, now.size()-1);
text.str(now);
text.rdbuf()->pubseekoff(0, ios_base::end, ios_base::out);
text.clear();
}
void TextInput::deleteLastWord(){
string now = text.str();
// Global::debug(0) << "Delete last word '" << now << "'" << endl;
if (now.size() > 0){
if (now.at(now.size() - 1) != ' '){
int here = now.size() - 1;
while (here > 0 && now.at(here) != ' '){
here -= 1;
}
if (now.at(here) == ' '){
here += 1;
}
now.erase(here);
} else {
int here = now.size() - 1;
while (here > 0 && now.at(here) == ' '){
here -= 1;
}
if (here > 0){
while (here > 0 && now.at(here) != ' '){
here -= 1;
}
if (now.at(here) == ' '){
here += 1;
}
}
now.erase(here);
}
text.str(now);
text.rdbuf()->pubseekoff(0, ios_base::end, ios_base::out);
text.clear();
}
/*
size_t get = now.rfind(" ");
if (get != string::npos){
Global::debug(0) << "get " << get << " size " << now.size() << endl;
if (get == now.size() - 1){
get = now.find_last_not_of(' ');
get = now.rfind(' ', get);
if (get != string::npos){
get = 0;
}
now.erase(get + 1);
} else {
now = now.substr(0, get + 1);
}
text.str(now);
text.rdbuf()->pubseekoff(0, ios_base::end, ios_base::out);
text.clear();
} else {
clearInput();
}
*/
}
TextInput::~TextInput(){
disable();
}
diff --git a/util/input/text-input.h b/util/input/text-input.h
index d37dabff..437dc0fa 100644
--- a/util/input/text-input.h
+++ b/util/input/text-input.h
@@ -1,81 +1,81 @@
#ifndef _paintown_text_input_h
#define _paintown_text_input_h
#include "input-map.h"
#include <sstream>
#include <string>
#include <map>
typedef void (*callback)(void * arg);
struct Callback{
Callback():
function(0),
data(0){
}
Callback(callback function, void * data):
function(function),
data(data){
}
callback function;
void * data;
};
class TextInput: public InputMap<unsigned char> {
public:
TextInput(const std::string & start = "");
/* returns true if the text was modified */
bool doInput();
void enable();
void disable();
void addHandle(int key, int delay, callback function, void * data);
void addBlockingHandle(int key, callback function, void * data);
inline void setBlockingKeys(){
blockingKeys = true;
}
- std::string getText();
+ std::string getText() const;
void setText(const std::string & text);
void clearInput();
// void clear();
bool isEnabled() const {
return enabled;
}
void backspace();
void deleteLastWord();
virtual KeyState<unsigned char> * getState(int key){
KeyState<unsigned char> * state = InputMap<unsigned char>::getState(key);
if (state == NULL){
if (blockingKeys){
set(key, 0, true, key);
} else {
set(key, 10, false, key);
}
state = InputMap<unsigned char>::getState(key);
}
return state;
}
virtual ~TextInput();
protected:
int nextHandle();
std::ostringstream text;
/* whether key repeat is on or off */
bool blockingKeys;
bool enabled;
std::map<int, Callback> callbacks;
int handle;
};
#endif

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 11:16 AM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68693
Default Alt Text
(7 KB)

Event Timeline