original 1.0.1 release
[xwelltris.git] / src / sdl / sdlwellinput.cxx
blob9a299a44b678972e7272ade6b0539f76732f23b7
1 // docm_prefix(///)
2 /****************************************************************************
3 * Copyright (C) 2002-2003 by Leo Khramov
4 * email: leo@xnc.dubna.su
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 ****************************************************************************/
16 // $Id: sdlwellinput.cxx,v 1.2 2003/02/20 15:09:41 leo Exp $
18 /// module description
19 /// SDL implementation of WellInput class - make input text from user
20 /// display it through WellImageFont, convert SDL Key event to symbols
22 #include "sdlwellinput.h"
23 #include "sdl_gfxprimitives.h"
24 //===========================================================================
25 /// global SDLWellInput(char*)
26 /// constructor - fill name and get geometry
27 /// tags SDLWellInput
28 SDLWellInput::SDLWellInput(char* iname) : WellInput(iname)
30 engine=(SDLWellEngine*) default_well_engine;
31 mainw=engine->get_main_window();
34 //===========================================================================
35 /// global process_event(wEvent)
36 /// stub that process events
37 /// tags SDLWellInput
38 bool SDLWellInput::process_event(wEvent ev)
40 SDL_Event *xev=(SDL_Event*)ev.data;
41 switch(ev.type)
43 case eKeyPress:
44 process_key(xev);
45 return false;
48 return true;
51 //===========================================================================
52 /// global draw_text()
53 /// draw text on the screen
54 /// tags SDLWellInput
55 void SDLWellInput::draw_text()
57 fnt->draw_text(buf,buflen,BackColor);
58 show_cursor();
62 //===========================================================================
63 /// global show_cursor()
64 /// draw cursor on the screen
65 /// tags SDLWellInput
66 void SDLWellInput::show_cursor()
68 SDL_Rect dest;
69 Uint32 *colors=engine->get_colors();
71 dest.x = geo[1].tox+buflen*FONT2_L;
72 dest.y = geo[1].toy;
73 dest.w = FONT2_L;
74 dest.h = FONT2_H;
77 SDL_FillRect(mainw, &dest, colors[BonusColor2]);
78 SDL_UpdateRects(mainw, 1, &dest);
82 //===========================================================================
83 /// global hide_cursor()
84 /// hide cursor on the screen
85 /// tags SDLWellInput
86 void SDLWellInput::hide_cursor()
88 SDL_Rect dest;
89 Uint32 *colors=engine->get_colors();
91 dest.x = geo[1].tox+buflen*FONT2_L;
92 dest.y = geo[1].toy;
93 dest.w = FONT2_L;
94 dest.h = FONT2_H;
96 SDL_FillRect(mainw, &dest, colors[BackColor]);
97 SDL_UpdateRects(mainw, 1, &dest);
101 //===========================================================================
102 /// global process_key(SDLEvent)
103 /// draw text on the screen
104 /// tags SDLWellInput
106 void SDLWellInput::process_key(SDL_Event *xev)
108 switch(xev->key.keysym.sym)
110 case SDLK_RETURN:
111 if(buflen)
112 object_on_enter.call(wEvent(aInputDone,this));
113 break;
115 case SDLK_DELETE:
116 case SDLK_BACKSPACE:
117 if(buflen>0)
119 hide_cursor();
120 buflen--;
121 buf[buflen]=0;
122 draw_text();
124 break;
125 default:
126 if(buflen<maxlen &&
127 xev->key.keysym.unicode>=32 &&
128 xev->key.keysym.unicode<128)
130 hide_cursor();
131 buf[buflen++]=(char)xev->key.keysym.unicode;
132 buf[buflen]=0;
133 draw_text();