allow keystroke repeat when holding down in text entry fields

This commit is contained in:
Dave White 2003-10-17 12:15:54 +00:00
parent f067c49cd3
commit b8c82aa625
6 changed files with 72 additions and 27 deletions

View File

@ -242,11 +242,8 @@ void do_move(display& disp, const gamemap& map, const game_data& gameinfo,
++num_units;
}
const int cash_flow = current_team.towers().size()*
game_config::tower_income +
game_config::base_income - num_units;
const int min_gold = 10 + (cash_flow < 0 ? -cash_flow*10 : 0);
//currently just spend all the gold we can!
const int min_gold = 0;
const int towers = map.towers().size();
int taken_towers = 0;

View File

@ -4,7 +4,10 @@
#include "SDL.h"
#include <algorithm>
#include <cassert>
#include <utility>
#include <vector>
namespace events
{
@ -23,6 +26,25 @@ resize_lock::~resize_lock()
--disallow_resize;
}
std::vector<handler*> event_handlers;
handler::handler()
{
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
event_handlers.push_back(this);
}
handler::~handler()
{
assert(!event_handlers.empty());
if(event_handlers.back() == this) {
event_handlers.pop_back();
} else {
event_handlers.erase(std::find(event_handlers.begin(),
event_handlers.end(),this));
}
}
void pump()
{
SDL_PumpEvents();
@ -31,6 +53,11 @@ void pump()
SDL_Event event;
while(SDL_PollEvent(&event)) {
for(std::vector<handler*>::iterator i = event_handlers.begin();
i != event_handlers.end(); ++i) {
(*i)->handle_event(event);
}
switch(event.type) {
case SDL_VIDEORESIZE: {
const SDL_ResizeEvent* const resize

View File

@ -1,6 +1,8 @@
#ifndef EVENTS_HPP_INCLUDED
#define EVENTS_HPP_INCLUDED
#include "SDL.h"
namespace events
{
struct resize_lock {
@ -8,6 +10,17 @@ struct resize_lock {
~resize_lock();
};
//any classes that derive from this class will automatically
//receive sdl events through the handle function for their lifetime
class handler
{
public:
virtual void handle_event(const SDL_Event& event) {};
protected:
handler();
virtual ~handler();
};
void pump();
}

View File

@ -118,6 +118,7 @@ int main()
} catch(network::error& e) {
if(!e.socket) {
std::cerr << "fatal network error: " << e.message << "\n";
break;
} else {
std::cerr << "socket closed: " << e.message << "\n";

View File

@ -105,26 +105,34 @@ void textbox::draw() const
disp_.video().flip();
}
void textbox::process()
void textbox::handle_event(const SDL_Event& event)
{
if(key_[KEY_LEFT] && !lastLArrow_ && cursor_ > 0) {
if(event.type != SDL_KEYDOWN)
return;
const SDL_keysym& key
= reinterpret_cast<const SDL_KeyboardEvent&>(event).keysym;
int c = key.sym;
if(c == SDLK_LEFT && cursor_ > 0) {
--cursor_;
if(cursor_ < firstOnScreen_)
--firstOnScreen_;
}
if(key_[KEY_RIGHT] && !lastRArrow_ && cursor_ < text_.size()) {
if(c == SDLK_RIGHT && cursor_ < text_.size()) {
++cursor_;
}
if(key_[KEY_BACKSPACE] && !lastBackspace_ && cursor_ > 0) {
if(c == SDLK_BACKSPACE && cursor_ > 0) {
--cursor_;
text_.erase(text_.begin()+cursor_);
if(cursor_ < firstOnScreen_)
--firstOnScreen_;
}
if(key_[KEY_DELETE] && !lastDelete_ && !text_.empty()) {
if(c == SDLK_DELETE && !text_.empty()) {
if(cursor_ == text_.size()) {
text_.resize(text_.size()-1);
--cursor_;
@ -133,26 +141,19 @@ void textbox::process()
}
}
lastLArrow_ = key_[KEY_LEFT];
lastRArrow_ = key_[KEY_RIGHT];
lastBackspace_ = key_[KEY_BACKSPACE];
lastDelete_ = key_[KEY_DELETE];
for(char c = INPUT_CHAR_START; c != INPUT_CHAR_END; ++c) {
char character = c;
if(islower(character) && (key_[KEY_LSHIFT] || key_[KEY_RSHIFT])) {
character = toupper(character);
if(c >= INPUT_CHAR_START && c < INPUT_CHAR_END) {
if(islower(c) && (key_[SDLK_LSHIFT] || key_[SDLK_RSHIFT])) {
c = toupper(c);
}
const bool val = key_[c];
if(val && !previousKeyState_[c-INPUT_CHAR_START]) {
text_.insert(text_.begin()+cursor_,character);
++cursor_;
}
previousKeyState_[c-INPUT_CHAR_START] = val;
text_.insert(text_.begin()+cursor_,c);
++cursor_;
}
}
void textbox::process()
{
draw();
}

View File

@ -10,20 +10,24 @@
See the COPYING file for more details.
*/
#ifndef TEXTBOX_HPP_INCLUDED
#define TEXTBOX_HPP_INCLUDED
#include "../display.hpp"
#include "../events.hpp"
#include "../key.hpp"
#include "../sdl_utils.hpp"
#include "SDL.h"
namespace gui {
#define INPUT_CHAR_START (' ')
#define INPUT_CHAR_END ('~' + 1)
#define CHAR_LENGTH (INPUT_CHAR_END - INPUT_CHAR_START)
class textbox
class textbox : public events::handler
{
display& disp_;
std::string text_;
@ -38,6 +42,8 @@ class textbox
bool lastLArrow_, lastRArrow_, lastDelete_, lastBackspace_;
void handle_event(const SDL_Event& event);
void draw_cursor(int pos) const;
public: