]> git.tdb.fi Git - r2c2.git/blob - source/designer/input.cpp
Add Id tags and copyright notices to files
[r2c2.git] / source / designer / input.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <SDL_keysym.h>
9 #include <GL/gl.h>
10 #include <msp/gl/texture.h>
11 #include "designer.h"
12 #include "input.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 Input::Input(Designer &d, const string &t, const string &e):
18         designer(d),
19         title(t),
20         text(e),
21         pos(text.size())
22 { }
23
24 void Input::key_press(unsigned key, unsigned, wchar_t ch)
25 {
26         if(key==SDLK_RETURN)
27                 signal_accept.emit();
28         else if(key==SDLK_ESCAPE)
29                 signal_cancel.emit();
30         else if(key==SDLK_BACKSPACE)
31         {
32                 if(pos>0)
33                 {
34                         text.erase(pos-1, 1);
35                         --pos;
36                 }
37         }
38         else if(key==SDLK_DELETE)
39         {
40                 if(pos<text.size())
41                         text.erase(pos, 1);
42         }
43         else if(key==SDLK_LEFT)
44         {
45                 if(pos>0)
46                         --pos;
47         }
48         else if(key==SDLK_RIGHT)
49         {
50                 if(pos<text.size())
51                         ++pos;
52         }
53         else if(ch>=0x20)
54         {
55                 text.insert(pos, 1, ch);
56                 ++pos;
57         }
58 }
59
60 void Input::render()
61 {
62         glLoadIdentity();
63         glTranslatef(300, 450, 0);
64
65         GL::Texture::unbind();
66         glColor4f(0.7, 0.7, 0.7, 0.9);
67         glBegin(GL_QUADS);
68         glVertex2f(0, 0);
69         glVertex2f(680, 0);
70         glVertex2f(680, 60);
71         glVertex2f(0, 60);
72         glEnd();
73
74         glColor4f(0, 0, 0, 1);
75         glTranslatef(5, 35, 0);
76
77         glPushMatrix();
78         glScalef(20, 20, 20);
79         designer.get_font().draw_string(title);
80         glPopMatrix();
81
82         glTranslatef(0, -30, 0);
83         glPushMatrix();
84         glScalef(20, 20, 20);
85         designer.get_font().draw_string(text);
86
87         glTranslatef(designer.get_font().get_string_width(text.substr(0, pos)), 0, 0);
88         glDisable(GL_TEXTURE_2D);
89         glBegin(GL_LINES);
90         glVertex2f(0, 0);
91         glVertex2f(0, 1);
92         glEnd();
93         glPopMatrix();
94 }