-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcEditBox.h
More file actions
252 lines (225 loc) · 5.68 KB
/
cEditBox.h
File metadata and controls
252 lines (225 loc) · 5.68 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
#pragma once
#include "cScrollBar.h"
class cEditBox : public CWindow, private KeyboardEventsAbstractor< cEditBox >, private ResizeEventAbstractor< cEditBox >, private RenderEventAbstractor< cEditBox >
{
public:
cEditBox()
{
m_sbScroll.SetPosition(GetWidth()-16, 0);
m_sbScroll.SetSize(16, GetHeight());
m_sbScroll.SetAnchorBottom(true);
m_sbScroll.SetAnchorLeft(false);
m_sbScroll.SetAnchorRight(true);
m_sbScroll.SetValue(100);
AddChild(m_sbScroll);
SetActiveLine(0);
SetMultiLine(false);
SetReadOnly(true);
SetFontSize(12);
SetShowReverse(false);
SetAddReverse(false);
AddKeyboardEventHandler( *(KeyboardEventsAbstractor< cEditBox > *)this );
AddResizeEventHandler( *(ResizeEventAbstractor< cEditBox > *)this );
AddRenderEventHandler( *(RenderEventAbstractor< cEditBox > *)this );
}
private:
bool RenderEventAbstractor< cEditBox >::OnRender( IWindow & Window, double TimeSlice )
{
float iLeft = GetAbsoluteLeft(),
iRight = iLeft + GetWidth(),
iTop = GetAbsoluteTop(),
iBottom = iTop + GetHeight(),
iWidth = GetWidth(),
iHeight = GetHeight();
glBindTexture(GL_TEXTURE_2D, 0);
int iTpCount = (int) m_sbScroll.GetValue();
if (m_bShowReverse)
iTpCount = (int) m_vLines.size() - iTpCount;
int iCount = 0;
int iMaxLines = (int) (iHeight/13);
if (!m_bMultiLine)
{
iMaxLines = 1;
iTpCount = 0;
}
std::list<stChatLine>::iterator i = m_vLines.begin();
for (int h=0; h<iTpCount; h++)
i++;
glListBase(0x01000000 + (m_dwFontSize << 8));
for (; (i != m_vLines.end()) && (iCount < iMaxLines); i++, iCount++)
{
glColor4ub(cColor[(*i).Color][0],cColor[(*i).Color][1],cColor[(*i).Color][2], (BYTE) (1.0f/*m_fTrans*/*255));
if (m_bShowReverse)
glRasterPos2f(iLeft+1, iBottom-((m_dwFontSize+1)*(iCount))-5);
else
glRasterPos2f(iLeft+1, iTop+11+(float) (((m_dwFontSize+1)*iCount)));
int iLen = (int) (*i).String.length();
const char *Str = (*i).String.c_str();
glCallLists(iLen, GL_UNSIGNED_BYTE, Str);
}
//border
glBegin(GL_LINE_LOOP);
glColor4f(0.5,0.5,0.5,1.0f/*m_fTrans*/);
glVertex2f(iLeft,iTop);
glVertex2f(iRight,iTop);
glVertex2f(iRight,iBottom);
glVertex2f(iLeft,iBottom);
glEnd();
return true;
}
bool ResizeEventAbstractor< cEditBox >::OnResize( IWindow & Window, float NewWidth, float NewHeight)
{
if (NewWidth < 32)
return false;
if (NewHeight < m_dwFontSize + 2)
return false;
return true;
}
bool ResizeEventAbstractor< cEditBox >::OnResized( IWindow & Window )
{
return false;
}
bool KeyboardEventsAbstractor< cEditBox >::OnKeyPress( IWindow & Window, unsigned long KeyCode )
{
if (m_bReadOnly)
return true;
if ((!m_vLines.size()) && (!m_bMultiLine))
{
AddLine("", eWhite);
SetActiveLine(0);
}
if (KeyCode == 0x08) return true; //backspace
if (KeyCode == 0x0A) return true; //linefeed
if (KeyCode == 0x09) return true; //tab
if (KeyCode == 0x1B) return true; //escape
if (KeyCode == 0x0D) return true; //CR
char bleh[2] = { 0, 0 };
bleh[0] = (char) KeyCode;
m_viActiveLine->String = m_viActiveLine->String + bleh;
return true;
}
bool KeyboardEventsAbstractor< cEditBox >::OnKeyDown( IWindow & Window, unsigned long KeyCode )
{
if (m_bReadOnly)
return true;
if ((!m_vLines.size()) && (!m_bMultiLine))
{
AddLine("", eWhite);
SetActiveLine(0);
}
char bleh[2] = { 0, 0 };
bleh[0] = (char) KeyCode;
if (KeyCode == VK_BACK)
{
if (m_viActiveLine->String.size())
m_viActiveLine->String = m_viActiveLine->String.substr(0, m_viActiveLine->String.size()-1);
}
else if (KeyCode == VK_RETURN)
{
if (!m_bMultiLine)
{
m_vSubmitQueue.push_back(m_viActiveLine->String);
m_viActiveLine->String = "";
}
}
return true;
}
bool KeyboardEventsAbstractor< cEditBox >::OnKeyUp( IWindow &Window, unsigned long KeyCode )
{
return false;
}
public:
bool NeedSubmit() const
{
if (!m_bMultiLine)
return (m_vSubmitQueue.size() > 0);
else
return false;
//TODO: Implement a multiline submit of some sort... Needs an event callback...
}
std::string GetSubmit()
{
if ((!m_bMultiLine) && (m_vSubmitQueue.size()))
{
std::string toret = m_vSubmitQueue.front();
m_vSubmitQueue.pop_front();
return toret;
}
return "";
}
/* void SetText(std::string Text, int iIndex)
{
if (iIndex < (int) m_vLines.size())
m_vLines[iIndex] = Text;
}
std::string GetText(int iIndex)
{
return m_vLines[iIndex].String;
}*/
void SetMultiLine(bool ML)
{
m_bMultiLine = ML;
if (m_bMultiLine)
{
m_sbScroll.SetVisible(true);
}
else
{
m_sbScroll.SetVisible(false);
}
}
void SetReadOnly(bool RO)
{
m_bReadOnly = RO;
}
void SetFontSize(int FS)
{
m_dwFontSize = FS;
}
void SetShowReverse(bool SR)
{
m_bShowReverse = SR;
}
void SetAddReverse(bool AR)
{
m_bAddReverse = AR;
}
void AddLine(std::string String, int Color)
{
stChatLine CL = { String, (eColor) Color };
if (m_bAddReverse)
m_vLines.push_front(CL);
else
m_vLines.push_back(CL);
bool ResetAfter = (m_sbScroll.GetValue() == m_sbScroll.GetMax());
m_sbScroll.SetMin(1);
m_sbScroll.SetMax((float) m_vLines.size());
if (ResetAfter)
m_sbScroll.SetValue(m_sbScroll.GetMax());
}
void SetActiveLine(int Line)
{
if (Line < (int) m_vLines.size())
{
m_iActiveLine = Line;
m_viActiveLine = m_vLines.begin();
for (int i=0; i<Line; i++)
m_viActiveLine++;
}
}
private:
struct stChatLine {
std::string String;
eColor Color;
};
DWORD m_dwFontSize;
bool m_bMultiLine;
bool m_bReadOnly;
int m_iActiveLine;
bool m_bShowReverse;
bool m_bAddReverse;
std::list<std::string> m_vSubmitQueue;
std::list<stChatLine> m_vLines;
std::list<stChatLine>::iterator m_viActiveLine;
cScrollBar m_sbScroll;
};