-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathofxDatGuiTextInputField.h
More file actions
303 lines (269 loc) · 10.2 KB
/
Copy pathofxDatGuiTextInputField.h
File metadata and controls
303 lines (269 loc) · 10.2 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
/*
Copyright (C) 2015 Stephen Braitsch [http://braitsch.io]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include "ofxDatGuiIntObject.h"
class ofxDatGuiTextInputField : public ofxDatGuiInteractiveObject{
public:
ofxDatGuiTextInputField()
{
mFocused = false;
mTextChanged = false;
mHighlightText = false;
mMaxCharacters = 99;
mType = ofxDatGuiInputType::ALPHA_NUMERIC;
setTheme(ofxDatGuiComponent::getTheme());
}
void setWidth(int w)
{
mInputRect.width = w;
}
void setPosition(int x, int y)
{
mInputRect.x = x;
mInputRect.y = y;
}
void setTheme(const ofxDatGuiTheme* theme)
{
mFont = theme->font.ptr;
mInputRect.height = theme->layout.height - (theme->layout.padding * 2);
color.active.background = theme->color.textInput.backgroundOnActive;
color.inactive.background = theme->color.inputAreaBackground;
color.active.text = theme->color.label;
color.inactive.text = theme->color.textInput.text;
color.highlight = theme->color.textInput.highlight;
mUpperCaseText = theme->layout.textInput.forceUpperCase;
mHighlightPadding = theme->layout.textInput.highlightPadding;
setText(mText);
}
void draw()
{
// center the text //
int tx = mInputRect.x + mInputRect.width / 2 - mTextRect.width / 2;
int ty = mInputRect.y + mInputRect.height / 2 + mTextRect.height / 2;
ofPushStyle();
// draw the input field background //
if (mFocused && mType != ofxDatGuiInputType::COLORPICKER){
ofSetColor(color.active.background);
} else {
ofSetColor(color.inactive.background);
}
ofDrawRectangle(mInputRect);
// draw the highlight rectangle //
if (mHighlightText){
ofRectangle hRect;
hRect.x = tx - mHighlightPadding;
hRect.width = mTextRect.width + (mHighlightPadding * 2);
hRect.y = ty - mHighlightPadding - mTextRect.height;
hRect.height = mTextRect.height + (mHighlightPadding * 2);
ofSetColor(color.highlight);
ofDrawRectangle(hRect);
}
// draw the text //
ofColor tColor = mHighlightText ? color.active.text : color.inactive.text;
ofSetColor(tColor);
mFont->draw(mType == ofxDatGuiInputType::COLORPICKER ? "#" + mRendered : mRendered, tx, ty);
if (mFocused) {
// draw the cursor //
ofDrawLine(ofPoint(tx + mCursorX, mInputRect.getTop()), ofPoint(tx + mCursorX, mInputRect.getBottom()));
}
ofPopStyle();
}
int getWidth()
{
return mInputRect.width;
}
int getHeight()
{
return mInputRect.height;
}
bool hasFocus()
{
return mFocused;
}
bool hitTest(ofPoint m)
{
return (m.x>=mInputRect.x && m.x<=mInputRect.x+mInputRect.width && m.y>=mInputRect.y && m.y<=mInputRect.y+mInputRect.height);
}
void setText(string text)
{
mText = text;
mTextChanged = true;
mRendered = mUpperCaseText ? ofToUpper(mText) : mText;
mTextRect = mFont->rect(mType == ofxDatGuiInputType::COLORPICKER ? "#" + mRendered : mRendered);
}
string getText()
{
return mText;
}
void setTextActiveColor(ofColor c)
{
color.active.text = c;
}
void setTextInactiveColor(ofColor c)
{
color.inactive.text = c;
}
void setTextUpperCase(bool toUpper)
{
mUpperCaseText = toUpper;
setText(mText);
}
bool getTextUpperCase()
{
return mUpperCaseText;
}
void setTextInputFieldType(ofxDatGuiInputType type)
{
mType = type;
}
void setBackgroundColor(ofColor c)
{
color.inactive.background = c;
}
void setMaxNumOfCharacters(unsigned int max)
{
mMaxCharacters = max;
}
void onFocus()
{
mFocused = true;
mTextChanged = false;
mHighlightText = true;
setCursorIndex(mText.size());
}
void onFocusLost()
{
mFocused = false;
mHighlightText = false;
if (mTextChanged){
mTextChanged = false;
ofxDatGuiInternalEvent e(ofxDatGuiEventType::INPUT_CHANGED, 0);
internalEventCallback(e);
}
}
void onKeyPressed(int key)
{
if (!keyIsValid(key)) return;
if (mHighlightText) {
// if key is printable or delete
if ((key >= 32 && key <= 255) || key == OF_KEY_BACKSPACE || key == OF_KEY_DEL) {
setText("");
setCursorIndex(0);
}
}
if (key == OF_KEY_BACKSPACE){
// delete character at cursor position //
if (mCursorIndex > 0) {
setText(mText.substr(0, mCursorIndex - 1) + mText.substr(mCursorIndex));
setCursorIndex(mCursorIndex - 1);
}
} else if (key == OF_KEY_LEFT) {
setCursorIndex(max( (int) mCursorIndex - 1, 0));
} else if (key == OF_KEY_RIGHT) {
setCursorIndex(std::min( mCursorIndex + 1, (unsigned int) mText.size()));
} else {
// insert character at cursor position //
setText(mText.substr(0, mCursorIndex) + (char)key + mText.substr(mCursorIndex));
setCursorIndex(mCursorIndex + 1);
}
mHighlightText = false;
}
void setCursorIndex(int index)
{
if (index == 0) {
mCursorX = mFont->rect(mRendered.substr(0, index)).getLeft();
} else if (index > 0) {
mCursorX = mFont->rect(mRendered.substr(0, index)).getRight();
// if we're at a space append the width the font's '1' character //
if (mText.at(index - 1) == ' ') mCursorX += mFont->rect("1").width;
}
if (mType == ofxDatGuiInputType::COLORPICKER) mCursorX += mFont->rect("#").width;
mCursorIndex = index;
}
protected:
bool keyIsValid(int key)
{
if (key == OF_KEY_BACKSPACE || key == OF_KEY_LEFT || key == OF_KEY_RIGHT){
return true;
} else if (mType == ofxDatGuiInputType::COLORPICKER){
// limit string length to six hex characters //
if (!mHighlightText && mText.size() == 6){
return false;
// allow numbers 0-9 //
} else if (key>=48 && key<=57){
return true;
// allow letters a-f & A-F //
} else if ((key>=97 && key<=102) || (key>=65 && key<=70)){
return true;
} else{
// an invalid key was entered //
return false;
}
} else if (mType == ofxDatGuiInputType::NUMERIC){
// allow dash (-) or dot (.) //
if (key==45 || key==46){
return true;
// allow numbers 0-9 //
} else if (key>=48 && key<=57){
return true;
} else{
// an invalid key was entered //
return false;
}
} else if (mType == ofxDatGuiInputType::ALPHA_NUMERIC){
// limit range to printable characters http://www.ascii-code.com //
if (key >= 32 && key <= 255) {
return true;
} else {
// an invalid key was entered //
return false;
}
} else{
// invalid textfield type //
return false;
}
}
private:
string mText;
string mRendered;
bool mFocused;
bool mTextChanged;
bool mHighlightText;
bool mUpperCaseText;
float mCursorX;
ofRectangle mTextRect;
ofRectangle mInputRect;
unsigned int mCursorIndex;
unsigned int mMaxCharacters;
unsigned int mHighlightPadding;
struct{
struct {
ofColor text;
ofColor background;
} active;
struct {
ofColor text;
ofColor background;
} inactive;
ofColor cursor;
ofColor highlight;
} color;
ofxDatGuiInputType mType;
shared_ptr<ofxSmartFont> mFont;
};