-
Notifications
You must be signed in to change notification settings - Fork 1
/
GCP_Edit.cpp
267 lines (223 loc) · 6.93 KB
/
GCP_Edit.cpp
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
#include "GCP_Edit.h"
#include "GCP_FormComponent.h"
#include "GCP_Math.h"
using namespace gcp;
GCP_Edit::GCP_Edit()
{
_isEditingText = false; //Ðåäàêòèðóåì ââåäåííûé òåêñò. (âûäåëåíèÿ ìûøêîé ìîêà íåò)
_sTextInputDraw = ""; //Òåêñò êîòîðûé ðèñóåì
_iDrawDash = 0; //Ïåðåìåííàÿ äëÿ òàéìåðà äëÿ âûâîäà òåêñòà
_iTextDrawIndex = 0; //Ñ êàêîãî èíäåêñà âûâîäèì òåêñò (åñëè îí ñëèøêîì äëèííûé)
inputType = ALL;//Ôîðìàò ââîäà äàííûõ
setStyle(GCP_ButtonWhiteStyle);
}
void GCP_Edit::OnDraw(const GCP_Event &event)
{
if(!isVisible())
return;
//Âûâîäèì òåêñò â ðàìî÷êå
GCP_Draw::Render()->Draw_FillRound(_position, 1, getStyle()->backgroundColor);
GCP_Draw::Render()->Draw_Round(_position, 1, getStyle()->borderColor);
int iBorderWidth = getStyle()->borderWidth;
GCP_Draw::Render()->Draw_FillRound(_position.x() + iBorderWidth, _position.y() + iBorderWidth, _position.width() - iBorderWidth * 2, _position.height() - iBorderWidth * 2, 1, getStyle()->textFieldColor);
//TTF_Font* font = TTF_OpenFont(getFont().c_str(),14);
SDL_Rect rect = {_position.x()+iBorderWidth,_position.y()+iBorderWidth*2,_position.width()-iBorderWidth*2,_position.height()-iBorderWidth*2 };
SDL_SetTextInputRect(&rect); //õç âîîáùå çà÷åì è ÷òî îíî äàåò
GCP_Rect<int> textPos(_position.x() + iBorderWidth * 2, _position.y() + iBorderWidth * 2, _position.width(), _position.height());
GCP_Draw::Render()->Draw_Text(_sTextInputDraw, textPos, getStyle(), &drawdata);
basicOnDraw(event);
_iDrawDash++;
if(_iDrawDash>25 && _isEditingText)
{
//MAKE THIS AS A GCP_DRAW FUNCTION
int sw,sh;
//òóò ìîãóò áûòü ïðîáëåìû ñ îïðåäåëåíèåì ðàçìåðà ñòðîêè ñ ðóññêèìè èëè þíèêîä ÷àðàêòåðàìè êîãäà òå ïðåäñòàâëåíû â ðàçíîé êîäèðîâêå (1 èëè 2 áàéòà)
//Ñàé÷àñ îíè îáðàáàòûâàþòñÿ â íîðìàëüíóþ êîäèðîâêó ôóíêöèåé CP1251TOUTF íî ýòî ðàáîòàòåò íå íà âñåõ óñòðîéñòâàõ
GCP_Draw::Render()->GetTextSize(_sTextInputDraw, sw, sh, getStyle());
//Ðèñóåì ïîä÷åðêèâàíèå
int iBorderWidth = getStyle()->borderWidth;
GCP_Draw::Render()->Draw_Line(_position.x() + sw + iBorderWidth * 2, _position.y() + iBorderWidth * 2, _position.x() + sw + iBorderWidth * 2, _position.y() + iBorderWidth * 2 + sh, getStyle()->textColor);
if(_iDrawDash>20) //!!! òàéìåð ìåðöàíèÿ
_iDrawDash = 0;
}
}
gcp_formEvent GCP_Edit::OnEvent(const GCP_Event &event)
{
gcp_formEvent evt;
evt.isEventInsideForm = false;
evt.isEventOnFormHead = false;
evt.isFormDragged = false;
if(!isVisible())
return evt;
switch(event.eventType)
{
case GCP_ON_LEFT_CLICK:
_isEditingText = true;
break;
case GCP_ON_GLEFT_CLICK:
_isEditingText = false;
break;
case GCP_ON_GKEYDOWN:
onKeyDown(event.keycode);
break;
case GCP_ON_GTEXTINPUT:
onTextInput(event.sTextInput);
break;
}
basicOnEvent(event);
return evt;
}
/*bool GCP_Edit::OnMouseLeftClick(SDL_MouseButtonEvent mousebutton) {
if(!isVisible)
return false;
_isEditingText = true;
basicOnMouseLeftClick(mousebutton);
return true;
}*/
/*gcp_formEvent GCP_Edit::OnMouseGlobalLeftClick(SDL_MouseButtonEvent mousebutton) {
gcp_formEvent evt;
evt.isEventInsideForm = false;
evt.isEventOnFormHead = false;
evt.isFormDragged = false;
if(!isVisible)
return evt;
_isEditingText = false;
basicOnMouseGlobalLeftClick(mousebutton);
return evt;
}*/
void GCP_Edit::onKeyDown(int keycode)
{
if(!isVisible() || !_isEditingText)
return;
switch (keycode)
{
case SDLK_RETURN:
_sTextInput = "";
break;
case SDLK_BACKSPACE:
{
if(_sTextInput.size() > 0)
{
if (_inputSizeMap.size() < 1)
throw;
int letterSize = _inputSizeMap.pop_back();
while (letterSize > 0)
{
if(_sTextInput.size() > 0)
_sTextInput.erase(_sTextInput.size() - 1);
letterSize--;
}
}
corelateText();
}
break;
}
}
bool GCP_Edit::onTextInput(const string &text)
{
if(!isVisible() || !_isEditingText)
return false;
//unsigned int i = 0;
//while(textevent.text[i]!=0)
// i++;
///This convert two digits from unicode input to one digit that point to russian ASCI
///Of course it is not. but some of digits
///Find the function to convert this
///OR PLACE IN STRING CONTAINING THE UTF SOMETHING THAT WILL INDICATE THIS
///AND LATER USE TTF_RenderUTF8_Blended TO DRAW THIS
///CRAP THIS WHILL REQUIRE TO PARSE _sCAption to multiple captions
//TTF_RenderUNICODE_Solid and TTF_RenderUTF8_Blended
/*wchar_t textR;
char mbchar;
mbstowcs(&textR,text.c_str(),2);
wctomb(&mbchar,textR);*/
/*if(i==2){
s[0] = textevent.text[1]-textevent.text[0]; //çàìåòèë ÷òî èíîãäà ýòà øòóêà åñòü ïðàâèëüíîå ñìåùåíå èç 2 áàéò â 1 áàéò ðóññêèé ñèìâîë
s[1] = 0;
}
else{
s[0] = textevent.text[0];
s[1] = 0;
}*/
//????
//Ïðîáëåìà â òîì ÷òî ñèìâîëû ðóññêèå ìîãóò ïîñòóïàòü êàê â âèäå 2 áàéò òàê è â âèäå 1 áàéòà
//????
char* character = (char*)text.c_str();
switch(inputType)
{
case DIGITONLY:
if (text.size() > 1 || !GCP_Math::isNum(character))
return false;
break;
case DOUBLEDIGIT:
if (text.size() > 1 || !GCP_Math::isNum(character) && text[0] != '.' && text[0] != '-' )
return false;
if (text[0] == '.')
{
for(size_t i=0; i<_sTextInput.size(); i++)
if(_sTextInput.at(i) == '.')
return false;
}
if (text[0] == '-')
{
for(size_t i=0; i<_sTextInput.size(); i++)
if(_sTextInput.at(i) == '-')
return false;
}
break;
case TEXTONLY:
//if (text.size() == 1 && GCP_Math::isNum(character) && text[0] != ' ')
// return false;
break;
//case GCP_Edit_InputType_Enum::ALL:
}
_inputSizeMap.push_back(text.size());
_sTextInput.append(character);
corelateText();
return true;
}
void GCP_Edit::corelateText()
{
//Åñëè òåêñò ñëèøêîì áîëüøîé èëè íàîáîðîò óìåíüøèëñÿ ïîñëå ðåäàêòèðîâàíèÿ
//Ñìàùàåì èíäåêñ ñòàðòîâîé ïîçèöèè ñ êîòîðîé îí âûâîäèòñÿ â êîìïîíåíòå
if(getStyle().getPointer() == NULL)
return;
_sTextInputDraw = _sTextInput;
_iTextDrawIndex = 0;
int sw,sh;
GCP_Draw::Render()->GetTextSize(_sTextInput, sw, sh, getStyle());
int k = 0;
while(sw >_position.width() - getStyle()->borderWidth*3)
{
_sTextInputDraw = "";
_iTextDrawIndex += _inputSizeMap.at(k);
k++;
char buffer[100];
_sTextInput.copy(buffer,_sTextInput.size()-_iTextDrawIndex,_iTextDrawIndex);
buffer[_sTextInput.size()-_iTextDrawIndex]='\0';
_sTextInputDraw.append(buffer);
GCP_Draw::Render()->GetTextSize(_sTextInputDraw, sw, sh, getStyle());
}
}
/*bool GCP_Edit::OnTextEdit(const GCP_Event &event)
{
if(!isVisible() || !_isEditingText)
return false;
return true;
}*/
void GCP_Edit::setText(const std::string &str, int byteperletter)
{
for(size_t i = 0; i < str.size(); i++)
{
bool isOneByte = GCP_Math::isOneByteChar(str.at(i));
_inputSizeMap.push_back(isOneByte ? 1 : 2);
if (!isOneByte)
i++;
}
_sTextInput = str;
corelateText();
}
string GCP_Edit::getText()
{
return _sTextInput;
}