2009-12-19 16:40:53 +01:00
|
|
|
/*********************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009, Simon Kagstrom
|
|
|
|
*
|
|
|
|
* Filename: VirtualKeyboard.c
|
|
|
|
* Author: Simon Kagstrom <simon.kagstrom@gmail.com>
|
|
|
|
* Description: A virtual keyboard
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
********************************************************************/
|
2009-12-19 19:52:40 +01:00
|
|
|
#ifndef __VIRTUAL_KEYBORD_HH__
|
|
|
|
#define __VIRTUAL_KEYBORD_HH__
|
|
|
|
|
2009-12-19 16:40:53 +01:00
|
|
|
#include <SDL.h>
|
|
|
|
|
2009-12-19 19:52:40 +01:00
|
|
|
#include "widget.hh"
|
2009-12-19 20:14:17 +01:00
|
|
|
#include "font.hh"
|
2009-12-19 19:52:40 +01:00
|
|
|
|
2009-12-19 16:40:53 +01:00
|
|
|
struct virtkey;
|
|
|
|
|
|
|
|
class KeyListener
|
|
|
|
{
|
2009-12-19 19:52:40 +01:00
|
|
|
public:
|
|
|
|
/* Each key is a string */
|
|
|
|
virtual void keyCallback(bool shift, const char *str) = 0;
|
2009-12-19 16:40:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class StringListener
|
|
|
|
{
|
2009-12-19 19:52:40 +01:00
|
|
|
public:
|
|
|
|
virtual void stringCallback(const char *str) = 0;
|
2009-12-19 16:40:53 +01:00
|
|
|
};
|
|
|
|
|
2009-12-19 16:46:15 +01:00
|
|
|
class VirtualKeyboard : public Widget
|
2009-12-19 16:40:53 +01:00
|
|
|
{
|
|
|
|
public:
|
2009-12-19 20:14:17 +01:00
|
|
|
VirtualKeyboard(Font *font);
|
2009-12-19 16:40:53 +01:00
|
|
|
|
|
|
|
void registerListener(KeyListener *kl);
|
|
|
|
void registerListener(StringListener *sl);
|
|
|
|
void unregisterListener(KeyListener *kl);
|
|
|
|
void unregisterListener(StringListener *sl);
|
|
|
|
|
|
|
|
/* Conversions */
|
|
|
|
const char *keycodeToString(int kc);
|
|
|
|
const char keycodeToChar(int kc);
|
|
|
|
int charToKeycode(char c);
|
2009-12-19 19:52:40 +01:00
|
|
|
int stringToKeycode(const char *str);
|
2009-12-19 16:40:53 +01:00
|
|
|
|
2009-12-19 19:52:40 +01:00
|
|
|
void activate();
|
|
|
|
|
2009-12-19 20:14:17 +01:00
|
|
|
void setFont(Font *font)
|
|
|
|
{
|
|
|
|
this->font = font;
|
|
|
|
}
|
|
|
|
|
2009-12-19 19:52:40 +01:00
|
|
|
void deactivate()
|
|
|
|
{
|
|
|
|
this->is_active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void runLogic();
|
2009-12-19 16:40:53 +01:00
|
|
|
|
|
|
|
void draw(SDL_Surface *where, int x, int y, int w, int h);
|
|
|
|
|
|
|
|
/* Singleton object */
|
|
|
|
static VirtualKeyboard *kbd;
|
|
|
|
private:
|
|
|
|
KeyListener *keyListeners[8];
|
|
|
|
StringListener *stringListeners[8];
|
|
|
|
|
|
|
|
void selectNext(int dx, int dy);
|
|
|
|
void toggleShift();
|
|
|
|
|
2009-12-19 20:14:17 +01:00
|
|
|
Font *font;
|
2009-12-19 16:40:53 +01:00
|
|
|
int sel_x;
|
|
|
|
int sel_y;
|
|
|
|
bool shift_on;
|
|
|
|
|
2009-12-19 19:52:40 +01:00
|
|
|
bool is_active;
|
2009-12-19 16:40:53 +01:00
|
|
|
char buf[255];
|
2009-12-19 19:52:40 +01:00
|
|
|
unsigned buf_head;
|
2009-12-19 16:40:53 +01:00
|
|
|
};
|
2009-12-19 19:52:40 +01:00
|
|
|
|
|
|
|
#endif /* __VIRTUAL_KEYBORD_HH__ */
|