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