-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextWin.cpp
More file actions
executable file
·361 lines (327 loc) · 9.69 KB
/
Copy pathTextWin.cpp
File metadata and controls
executable file
·361 lines (327 loc) · 9.69 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "TextWin.h"
// *****************************************************************************
// TEXT WINDOW FUNCTIONS
// *****************************************************************************
TxtWin::TxtWin(int xpos, int ypos, int width, int height,int buffersize,int id,Font* font,SDL_Surface* background,bool textinput) {
XPOS=xpos;
YPOS=ypos;
WIDTH=width;
HEIGHT=height;
BSIZE=buffersize;
ID=id;
FONT=font;
BACKGROUND=background;
FHEIGHT=TTF_FontHeight(FONT->GetFont());
DRAW=true;
DRAWCUR=false;
ACTIVE=false;
DRAG=false;
POSITION=0;
MAXPOS=0;
TEXTINPUT=textinput;
SetButtons();
SetScrollBar();
return;
/*
Might want to clean this up so that if you want text input included with the window then
YPOS=YPOS-FHEIGHT <-- so you have space for 1 more line of text where the input would be..
Also when scrollbars come into place might wanna adjust the width
*/
}
TxtWin::~TxtWin() {
for (int i=0;i < TEXT.size();i++)
delete TEXT[i];
TEXT.clear();
delete UPARROW;
delete DOWNARROW;
delete TEXTINPUTB;
return;
}
int TxtWin::GetID() {
return ID;
}
void TxtWin::AddText(char* text) {
// Also might want to include filters for the text for style/color changes
if (TEXT.size() >= BSIZE) {
MAXPOS=MAXPOS-(TEXT[0]->GetHeight() / FHEIGHT);
delete TEXT[0];
TEXT.erase(TEXT.begin());
}
Text* temp=new Text(text,-1,0,0);
TEXT.insert(TEXT.end(), temp);
temp->RenderTextWrap(FONT->GetFont(),WIDTH);
if (POSITION == MAXPOS)
POSITION=POSITION+(temp->GetHeight() / FHEIGHT);
MAXPOS=MAXPOS+(temp->GetHeight() / FHEIGHT);
SetScrollBar();
DRAW=true;
return;
}
void TxtWin::AddText(Text* text) {
if (TEXT.size() >= BSIZE) {
MAXPOS=MAXPOS-(TEXT[0]->GetHeight() / FHEIGHT);
delete TEXT[0];
TEXT.erase(TEXT.begin());
}
TEXT.insert(TEXT.end(),text);
text->RenderTextWrap(FONT->GetFont(),WIDTH);
if (POSITION == MAXPOS)
POSITION=POSITION+(text->GetHeight() / FHEIGHT);
MAXPOS=MAXPOS+(text->GetHeight() / FHEIGHT);
SetScrollBar();
DRAW=true;
}
SDL_Rect* TxtWin::Display(SDL_Surface* dest) {
SDL_Rect* Rect=new SDL_Rect;
if (DRAW) {
Rect->x=XPOS;Rect->y=YPOS;Rect->w=WIDTH;Rect->h=HEIGHT;
SDL_BlitSurface(BACKGROUND,Rect,dest,Rect);
Rect->w=Rect->w+16;
Rect->h=Rect->h+16;
DisplayText(dest);
SDL_Rect temp;
temp.x=XPOS+WIDTH; temp.y=YPOS+16; temp.w=16; temp.h=HEIGHT-32;
SDL_BlitSurface(BACKGROUND,&temp,dest,&temp);
SCROLLBAR->Display(dest);
UPARROW->Display(dest);
DOWNARROW->Display(dest);
DRAW=false;
}
if (TEXTINPUT) {
SDL_Rect* temp=TEXTINPUTB->Display(dest);
if (temp != NULL)
SDL_UpdateRect(dest,temp->x,temp->y,temp->w,temp->h);
}
return Rect;
}
void TxtWin::DisplayText(SDL_Surface* dsurface) {
if (MAXPOS == 0)
return;
int space=HEIGHT/FHEIGHT;
int pos=0;
int startp=0;
SDL_Rect src,dest;
dest.x=XPOS; dest.y=YPOS+HEIGHT;
// Find where to start displaying from..
for (int i=0;i < TEXT.size();i++) {
pos=pos+ (TEXT[i]->GetHeight() / FHEIGHT);
if (pos >= POSITION) {
startp=i;
break;
}
}
// Display the first line
src.x=0; src.y=0; src.w=TEXT[startp]->GetWidth(); src.h=(pos - POSITION) * FHEIGHT;
dest.w=src.w; dest.h=src.h;
SDL_BlitSurface(TEXT[startp]->RTEXT,&src,dsurface,&dest);
space=space-(src.h/FHEIGHT);
while (space > 0) {
if (startp < 0) // Check to make sure there is even text to display
break;
// Checks if there is space to display the entire line of text
if (space - (TEXT[startp]->GetHeight() / FHEIGHT) < 0)
break;
dest.y=dest.y-TEXT[startp]->GetHeight(); dest.h=TEXT[startp]->GetHeight();
dest.w=TEXT[startp]->GetWidth();
src.x=0; src.y=0; src.h=dest.h; src.w=dest.w;
SDL_BlitSurface(TEXT[startp]->RTEXT,NULL,dsurface,&dest);
startp--;
space=space-( src.h/FHEIGHT);
}
// If we get here and space still exists well display what we can!
if (space > 0 && startp >= 0) {
src.y=TEXT[startp]->GetHeight() - (space * FHEIGHT);
src.h=TEXT[startp]->GetHeight() - src.y;
src.x=0; src.w=TEXT[startp]->GetWidth();
dest.y=dest.y-src.h; dest.w=src.w; dest.h=src.h;
SDL_BlitSurface(TEXT[startp]->RTEXT,&src,dsurface,&dest);
}
}
bool TxtWin::ProcessEvent(SDL_Event* event) {
bool valid=false;
int Page = HEIGHT / FHEIGHT;
if (event->type == SDL_KEYDOWN && ACTIVE) {
switch (event->key.keysym.sym) {
case SDLK_UP:
POSITION--;
if (POSITION < Page)
POSITION++;
SetScrollBar();
DRAW=true;
break;
case SDLK_DOWN:
POSITION++;
if (POSITION > MAXPOS)
POSITION = MAXPOS;
SetScrollBar();
DRAW=true;
break;
case SDLK_PAGEUP:
POSITION=POSITION - Page;
if (POSITION < Page)
if (MAXPOS < Page)
POSITION = MAXPOS;
else
POSITION = Page;
SetScrollBar();
DRAW=true;
break;
case SDLK_PAGEDOWN:
POSITION=POSITION + Page;
if (POSITION > MAXPOS)
POSITION = MAXPOS;
SetScrollBar();
DRAW=true;
break;
default:
if (TEXTINPUT && TEXTINPUTB != NULL && ACTIVE) {
TEXTINPUTB->ProcessEvent(event);
SetScrollBar();
}
break;
}
}
if (event->motion.type == SDL_MOUSEMOTION && DRAG) {
int y;
SDL_GetRelativeMouseState(NULL, &y);
if (y != 0) {
int maxy=YPOS+HEIGHT-16-SCROLLBAR->GetHeight();
int miny=YPOS + 16;
y=y+SCROLLBAR->GetY();
if (y > maxy)
y = maxy;
if (y < miny)
y = miny;
double NumPages = double(MAXPOS) / Page;
int barsize = int((HEIGHT -32) / NumPages);
SCROLLBAR->ChangeCords(SCROLLBAR->GetX(), y);
POSITION = ((SCROLLBAR->GetY() - barsize + 16)* MAXPOS) / (HEIGHT - 32);
if (y == maxy)
POSITION = MAXPOS;
if (y == miny)
POSITION = Page;
if (POSITION < Page)
POSITION = Page;
if (POSITION > MAXPOS)
POSITION = MAXPOS;
DRAW=true;
}
}
if (DRAG && event->button.type == SDL_MOUSEBUTTONUP && event->button.button == SDL_BUTTON_LEFT) {
DRAG = false;
SetScrollBar();
DRAW=true;
SDL_GetRelativeMouseState(NULL, NULL);
}
if (UPARROW->ProcessEvent(event)) {
POSITION--;
if (POSITION < Page)
POSITION++;
SetScrollBar();
DRAW=true;
}
if (DOWNARROW->ProcessEvent(event)) {
POSITION++;
if (POSITION > MAXPOS)
POSITION = MAXPOS;
SetScrollBar();
DRAW=true;
}
if (SCROLLBAR->ProcessEvent(event)) {
DRAG=true;
SDL_GetRelativeMouseState(NULL, NULL);
}
if (event->button.type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_LEFT) {
if (event->button.x >= TBX && event->button.x <= TBX+TBW)
if (event->button.y >= TBY && event->button.y <= TBY+TBH) {
TEXTINPUTB->Activate();
valid=true;
}
else
TEXTINPUTB->Deactivate();
}
if (event->button.type == SDL_MOUSEBUTTONUP && event->button.button == SDL_BUTTON_LEFT) {
if (event->button.x >= XPOS && event->button.x <= XPOS+WIDTH)
if (event->button.y >= YPOS && event->button.y <= YPOS+HEIGHT)
valid=true;
}
return valid;
}
void TxtWin::SetScrollBar() {
int barsize;
int height=HEIGHT-32; // make space for the up/down arrow button
int Page = HEIGHT / FHEIGHT;
double NumPages = double(MAXPOS) / Page;
if (NumPages > 1.0)
barsize = int(height / NumPages);
else
barsize = height;
double Y;
if (MAXPOS != 0)
Y = (double(height) / MAXPOS) * double(POSITION);
else Y=barsize;
SCROLLBAR->ChangeCords(SCROLLBAR->GetX(), YPOS+int(Y)-barsize+16);
SDL_Surface* screen=SDL_GetVideoSurface();
SDL_Surface *button=SDL_CreateRGBSurface(SDL_SWSURFACE, 16, barsize, 32,screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
SDL_FillRect(button, NULL, SDL_MapRGB(screen->format, 128, 128, 128));
SCROLLBAR->ChangeSurface(button);
return;
}
void TxtWin::SetButtons() {
// Gonna need this changed in the future
SCROLLBAR=new Button(0,Button::scrollbar,XPOS+WIDTH,YPOS+16,0,0);
UPARROW=new Button(0,Button::pushbutton,XPOS+WIDTH,YPOS,"uparrow.bmp");
DOWNARROW=new Button(1,Button::pushbutton,XPOS+WIDTH,YPOS+HEIGHT-16,"dwarrow.bmp");
TEXTINPUTB=NULL;
if (TEXTINPUT) {
TBX=XPOS; TBY=YPOS+HEIGHT; TBW=WIDTH; TBH=FONT->GetHeight();
TEXTINPUTB=new Button(ID,Button::textinput,XPOS,(YPOS+HEIGHT),"",TBX,TBH);
TEXTINPUTB->RenderText(FONT);
TEXTINPUTB->SetBG(BACKGROUND);
TEXTINPUTB->SetFont(FONT);
}
return;
}
void TxtWin::Activate() {
ACTIVE=true;
return;
}
void TxtWin::Deactivate() {
ACTIVE=false;
return;
}
bool TxtWin::Active() {
return ACTIVE;
}
void TxtWin::Draw(bool draw) {
DRAW=draw;
SCROLLBAR->Draw(true);
UPARROW->Draw(true);
DOWNARROW->Draw(true);
if (TEXTINPUT)
TEXTINPUTB->Draw(true);
return;
}
int TxtWin::GetSize() {
return TEXT.size();
}
char* TxtWin::GetText(int pos) {
return TEXT[pos]->GetText();
}
char* TxtWin::GetTextInput() {
return TEXTINPUTB->GetText();
}
void TxtWin::ChangeTextBText(char* newtext) {
TEXTINPUTB->ChangeText(newtext);
TEXTINPUTB->RenderText(FONT);
DRAW=true;
return;
}
void TxtWin::ChangeTextInputCoords(int x,int y,int w,int h=-1) {
TBX=x; TBY=y; TBW=w;
if (h != -1)
TBH=h;
if (TEXTINPUTB != NULL)
TEXTINPUTB->ChangeCords(TBX,TBY,TBW,TBH);
return;
}