-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgui.cpp
More file actions
153 lines (134 loc) · 3.92 KB
/
gui.cpp
File metadata and controls
153 lines (134 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
#include "gui.h"
TextInput::TextInput()
{
this->x = 0;
this->y = 0;
this->w = 100;
this->h = 20;
this->background_color = new short int(3);
this->border_color = new short int(3);
this->color = new short int(3);
this->padding = new short int(4);
for(int i=0; i<3; i++)
{
this->background_color[i]=200;
this->border_color[i]=50;
this->color[i]=0;
}
this->padding[0] = 2; //левая граница
this->padding[1] = 0; //верх
this->padding[2] = 2; //право
this->padding[3] = 2; //низ
this->border_color[1]=255;
this->color[1]=255;
this->border_width = 2;
this->focus = false;
this->cursor_position = 0;
}
void TextInput::set_position(int x, int y){
this->x=x;
this->y=y;
}
void TextInput::set_size(int w, int h){
this->w=h;
this->w=h;
}
int TextInput::inner_height()
{
return this->h + this->padding[1] + this->padding[3] + 2*this->border_width;
}
int TextInput::inner_width()
{
return this->w + this->padding[0] + this->padding[2] + 2*this->border_width;
}
void TextInput::show(){
glColor3ub(this->border_color[0], this->border_color[1], this->border_color[2]);
glLineWidth(this->border_width);
glBegin(GL_LINE_LOOP);
glVertex2i(this->x, this->y);
glVertex2i(this->x + this->inner_width(), this->y);
glVertex2i(this->x + this->inner_width(), this->y+this->inner_height());
glVertex2i(this->x, this->y+this->inner_height());
glEnd();
glViewport(this->x, this->y, this->inner_width(), this->inner_height());
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(0, this->inner_width(), 0, this->inner_height());
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glColor3ub(this->color[0], this->color[1], this->color[2]);
glRasterPos2f(this->padding[0] + this->border_width,this->padding[3] + this->border_width);
short int dx=0;
for (int i=0; i <= this->value.size(); i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, this->value[i]);
dx += glutBitmapWidth(GLUT_BITMAP_HELVETICA_18, this->value[i]);
if(i+1 == this->cursor_position && this->focus){
glColor3ub(this->color[0]/2, this->color[1]/2, this->color[2]/2);
glBegin(GL_LINES);
glVertex2i(dx + 1 + this->padding[0] + this->border_width, this->padding[3] + this->border_width);
glVertex2i(dx + 1 + this->padding[0] + this->border_width, 18 + this->padding[3] + this->border_width);
glEnd();
}
}
if(!this->value.size() && this->focus)
{
glColor3ub(this->color[0]/2, this->color[1]/2, this->color[2]/2);
glBegin(GL_LINES);
glVertex2i(1 + this->padding[0] + this->border_width, this->padding[3] + this->border_width);
glVertex2i(1 + this->padding[0] + this->border_width, 18 + this->padding[3] + this->border_width);
glEnd();
}
}
void TextInput::mouse(int button, int state, int x, int y)
{
if(x>this->x && x<this->x+this->w && y>this->y && y<this->y+this->h)
{
this->focus = true;
}
else
{
this->focus = false;
}
}
void TextInput::keyboard(unsigned char key, int mousePositionX, int mousePositionY)
{
if(this->focus)
{
if(key>='0' && key<='9' || key>='a' && key<='z' || key>='A' && key<='Z')
{
this->value.insert(this->cursor_position,1, key);
this->cursor_position++;
}
else if(key==8){
if(this->value.size())
{
if(this->cursor_position)
{
this->value.erase(this->cursor_position-1,1);
this->cursor_position--;
}
}
}
}
}
void TextInput::keyspecial(int key, int mousePositionX, int mousePositionY){
if(this->focus)
{
if(key == 100 && this->cursor_position>0)
{
this->cursor_position--;
}
else if(key == 102 && this->cursor_position < this->value.size()){
this->cursor_position++;
}
}
//printf("%i %i %i %i\n", key, mousePositionX, mousePositionY, this->focus);
}
TextInput::~TextInput()
{
delete [] this->border_color;
delete [] this->background_color;
}