-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathofxInputField.cpp
More file actions
813 lines (717 loc) · 23.2 KB
/
ofxInputField.cpp
File metadata and controls
813 lines (717 loc) · 23.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
//
// ofxInputField.cpp
// ofxInputField
//
// Based on ofxInputField by Felix Lange
//
//
#include "ofxInputField.h"
#include "ofGraphics.h"
using namespace std;
namespace{
template<typename Type>
typename std::enable_if<std::is_integral<Type>::value, Type>::type
getRange(Type min, Type max, float width){
double range = max - min;
range /= width*4;
return std::max(range,1.0);
}
template<typename Type>
typename std::enable_if<std::is_floating_point<Type>::value, Type>::type
getRange(Type min, Type max, float width){
double range = max - min;
range /= width*4;
return range;
}
template<typename Type>
std::string toString(Type t){
return ofToString(t);
}
template<>
std::string toString(uint8_t t){
return ofToString((int) t);
}
template<>
std::string toString(int8_t t){
return ofToString((int) t);
}
template<>
std::string toString(std::string t){
return t;
}
bool isHexNotation(std::string const& s){
return s.compare(0, 2, "0x") == 0
&& s.size() > 2
&& s.find_first_not_of("0123456789abcdefABCDEF", 2) == std::string::npos;
}
bool isANumber(std::string const& s){
return s.find_first_not_of("0123456789.-e", 0) == std::string::npos
&& std::count(s.begin(), s.end(), '.') <= 1;
}
template<typename Type>
Type fromString(const std::string & str){
if(isHexNotation(str)){
return ofHexToInt(str);
}else if(isANumber(str)){
return ofFromString<Type>(str);
}else{
throw std::exception();
}
}
template<>
uint8_t fromString(const std::string & str){
auto ret = 0;
if(isHexNotation(str)){
ret = ofHexToInt(str);
}else if(isANumber(str)){
ret = ofFromString<int>(str);
}else{
throw std::exception();
}
return std::max(std::min(ret, 255), 0);
}
template<>
int8_t fromString(const std::string & str){
auto ret = 0;
if(isHexNotation(str)){
ret = ofHexToInt(str);
}else if(isANumber(str)){
ret = ofFromString<int>(str);
}else{
throw std::exception();
}
return std::max(std::min(ret, -127), 127);
}
ofMesh rectangle(const ofRectangle & r, const ofFloatColor & c){
ofMesh mesh;
mesh.addVertex(r.position);
mesh.addVertex(glm::vec3(r.x + r.width, r.y, 0));
mesh.addVertex(glm::vec3(r.x + r.width, r.y + r.height, 0));
mesh.addVertex(glm::vec3(r.x + r.width, r.y + r.height, 0));
mesh.addVertex(glm::vec3(r.x, r.y + r.height, 0));
mesh.addVertex(glm::vec3(r.x, r.y, 0));
mesh.addColor(c);
mesh.addColor(c);
mesh.addColor(c);
mesh.addColor(c);
mesh.addColor(c);
mesh.addColor(c);
return mesh;
}
}
//-----------------------------------------------------------
template<typename Type>
ofxInputField<Type>::ofxInputField(){
}
//-----------------------------------------------------------
template<typename Type>
ofxInputField<Type>::ofxInputField(ofParameter<Type> _val, float width, float height){
setup(_val,width,height);
}
//-----------------------------------------------------------
template<typename Type>
ofxInputField<Type>* ofxInputField<Type>::setup(ofParameter<Type> _val, float width, float height){
value.makeReferenceTo(_val);
visibleInput = input = toString(value.get());
b.x = 0;
b.y = 0;
b.width = width;
b.height = height;
bGuiActive = false;
setNeedsRedraw();
if(!insideSlider){
registerMouseEvents();
}
listeners.push(value.newListener(this,&ofxInputField::valueChanged));
listeners.push(ofEvents().charEvent.newListener(this, &ofxInputField<Type>::charPressed, OF_EVENT_ORDER_BEFORE_APP));
listeners.push(ofEvents().keyPressed.newListener(this, &ofxInputField<Type>::keyPressed, OF_EVENT_ORDER_BEFORE_APP));
setNameListener();
return this;
}
//-----------------------------------------------------------
template<typename Type>
ofxInputField<Type>* ofxInputField<Type>::setup(const std::string& _name, Type _val, Type _min, Type _max, float width, float height){
value.set(_name,_val,_min,_max);
return setup(value,width,height);
}
//-----------------------------------------------------------
template<typename Type>
ofxInputField<Type>* ofxInputField<Type>::setup(const std::string& _name, Type _val){
value.set(_name,_val);
return setup(value);
}
//-----------------------------------------------------------
template<typename Type>
void ofxInputField<Type>::setMin(Type min){
value.setMin(min);
}
//-----------------------------------------------------------
template<typename Type>
Type ofxInputField<Type>::getMin(){
return value.getMin();
}
//-----------------------------------------------------------
template<typename Type>
void ofxInputField<Type>::setMax(Type max){
value.setMax(max);
}
//-----------------------------------------------------------
template<typename Type>
Type ofxInputField<Type>::getMax(){
return value.getMax();
}
//-----------------------------------------------------------
template<typename Type>
void ofxInputField<Type>::calculateSelectionArea(int selectIdx1, int selectIdx2){
selectStartPos = selectIdx1;
selectEndPos = selectIdx2;
if(selectEndPos>visibleInputEnd){
visibleInputEnd = selectEndPos;
}else if(std::max(selectEndPos-1,0)<visibleInputStart){
visibleInputStart = std::max(selectEndPos-1,0);
}
visibleInput = ofUTF8Substring(input, visibleInputStart, visibleInputEnd - visibleInputStart);
auto substrWidth = getTextBoundingBox(visibleInput,0,0).width;
auto totalTextWidth = b.width - textPadding * 2;
if(substrWidth > totalTextWidth){
substrWidth = 0;
visibleInput = "";
auto substrLen = 0;
while(substrWidth < totalTextWidth){
substrLen += 1;
visibleInput = ofUTF8Substring(input, visibleInputStart, substrLen);
substrWidth = getTextBoundingBox(ofUTF8Substring(input, visibleInputStart, substrLen),0,0).width;
}
substrLen -= 1;
visibleInput = ofUTF8Substring(input, visibleInputStart, substrLen);
substrWidth = getTextBoundingBox(ofUTF8Substring(input, visibleInputStart, substrLen),0,0).width;
visibleInputEnd = visibleInputStart + substrLen;
if(selectEndPos > visibleInputEnd){
substrWidth = 0;
visibleInput = "";
visibleInputEnd = selectEndPos;
visibleInputStart = selectEndPos;
while(substrWidth < totalTextWidth){
visibleInputStart -= 1;
visibleInput = ofUTF8Substring(input, visibleInputStart, visibleInputEnd - visibleInputStart);
substrWidth = getTextBoundingBox(ofUTF8Substring(input, visibleInputStart, visibleInputEnd - visibleInputStart),0,0).width;
}
visibleInputStart += 1;
visibleInput = ofUTF8Substring(input, visibleInputStart, visibleInputEnd - visibleInputStart);
substrWidth = getTextBoundingBox(ofUTF8Substring(input, visibleInputStart, visibleInputEnd - visibleInputStart),0,0).width;
}
}
auto first = std::min(selectStartPos, selectEndPos);
auto last = std::max(selectStartPos, selectEndPos);
auto substrLength = ofUTF8Length(visibleInput);
float preSelectWidth = 0;
auto substrFirst = ofClamp(first-visibleInputStart, 0, substrLength);
auto substrLast = ofClamp(last-visibleInputStart, 0, substrLength);
if(first > visibleInputStart){
std::string preSelectStr = ofUTF8Substring(visibleInput, 0, substrFirst);
preSelectWidth = getTextBoundingBox(preSelectStr,0,0).width;
}
if(showLabelWhileEditing){
selectStartX = b.getMaxX() - textPadding - substrWidth + preSelectWidth;
}else{
selectStartX = b.x + textPadding + preSelectWidth;
}
if(hasSelectedText()){
std::string selectStr = ofUTF8Substring(visibleInput, substrFirst, substrLast - substrFirst);
selectionWidth = getTextBoundingBox(selectStr,0,0).width;
}
setNeedsRedraw();
}
//-----------------------------------------------------------
template<typename Type>
void ofxInputField<Type>::moveCursor(int cursorPos){
if ( cursorPos < 0 ){
return;
}
lastCursorMoveTime = ofGetElapsedTimeMillis();
selectStartPos = selectEndPos = cursorPos;
selectionWidth = 0;
if ( visibleInputEnd < ofUTF8Length( input ) ){
// Attempt to extend visible input range. If a character was being entered
// somewhere inside the text, there might be enough space to the right to
// show the newly extended input.
visibleInputEnd += 1;
}
if(cursorPos>visibleInputEnd){
visibleInputEnd = cursorPos;
}else if(cursorPos<visibleInputStart){
visibleInputStart = cursorPos;
}
visibleInput = ofUTF8Substring(input, visibleInputStart, visibleInputEnd - visibleInputStart);
auto substrWidth = getTextBoundingBox(visibleInput,0,0).width;
auto totalTextWidth = b.width - textPadding * 2;
if(substrWidth > totalTextWidth){
substrWidth = 0;
visibleInput = "";
auto substrLen = 0;
while(substrWidth < totalTextWidth){
substrLen += 1;
visibleInput = ofUTF8Substring(input, visibleInputStart, substrLen);
substrWidth = getTextBoundingBox(ofUTF8Substring(input, visibleInputStart, substrLen),0,0).width;
}
substrLen -= 1;
visibleInput = ofUTF8Substring(input, visibleInputStart, substrLen);
substrWidth = getTextBoundingBox(ofUTF8Substring(input, visibleInputStart, substrLen),0,0).width;
visibleInputEnd = visibleInputStart + substrLen;
if(cursorPos > visibleInputEnd){
substrWidth = 0;
visibleInput = "";
visibleInputEnd = cursorPos;
visibleInputStart = cursorPos;
while(substrWidth < totalTextWidth){
visibleInputStart -= 1;
visibleInput = ofUTF8Substring(input, visibleInputStart, visibleInputEnd - visibleInputStart);
substrWidth = getTextBoundingBox(ofUTF8Substring(input, visibleInputStart, visibleInputEnd - visibleInputStart),0,0).width;
}
visibleInputStart += 1;
visibleInput = ofUTF8Substring(input, visibleInputStart, visibleInputEnd - visibleInputStart);
substrWidth = getTextBoundingBox(ofUTF8Substring(input, visibleInputStart, visibleInputEnd - visibleInputStart),0,0).width;
}
}
float beforeCursorWidth = 0;
if(selectStartPos > visibleInputStart){
auto beforeCursorStr = ofUTF8Substring(visibleInput, 0, selectStartPos - visibleInputStart);
beforeCursorWidth = getTextBoundingBox(beforeCursorStr,0,0).width;
}
if(showLabelWhileEditing){
selectStartX = b.getMaxX() - textPadding - substrWidth + beforeCursorWidth;
}else{
selectStartX = b.x + textPadding + beforeCursorWidth;
}
setNeedsRedraw();
}
//-----------------------------------------------------------
template<typename Type>
bool ofxInputField<Type>::mouseMoved(ofMouseEventArgs & mouse){
bool mouseOver = b.inside(mouse);
if(mouseOver != bMouseOver && !bGuiActive && overlappingLabel){
setNeedsRedraw();
}
bMouseOver = mouseOver;
return (isGuiDrawing() || insideSlider) && bMouseOver;
}
//-----------------------------------------------------------
template<typename Type>
bool ofxInputField<Type>::mousePressed(ofMouseEventArgs & mouse){
if(!isGuiDrawing() && !insideSlider){
return false;
}
if(b.inside(mouse)){
if(!insideSlider || mouse.button == OF_MOUSE_BUTTON_LEFT){
bMousePressed = true;
if(bGuiActive){
auto inputWidth = getTextBoundingBox(visibleInput,0,0).width;
float cursorX;
if(showLabelWhileEditing){
cursorX = mouse.x - (b.x + b.width - textPadding - inputWidth);
}else{
cursorX = mouse.x - (b.x + textPadding);
}
mousePressedPos = round(visibleInputStart + ofMap(cursorX, 0, inputWidth, 0, ofUTF8Length(visibleInput), true));
moveCursor(mousePressedPos);
}else{
calculateSelectionArea(0, ofUTF8Length(input));
bGuiActive = true;
}
setNeedsRedraw();
}
return true;
}else{
if(bGuiActive){
leaveFocus();
}
}
return false;
}
//-----------------------------------------------------------
template<typename Type>
bool ofxInputField<Type>::mouseDragged(ofMouseEventArgs & mouse){
if(!isGuiDrawing() && !insideSlider){
return false;
}
if(!bGuiActive){
return false;
}
if(!insideSlider || mouse.button == OF_MOUSE_BUTTON_LEFT){
auto inputWidth = getTextBoundingBox(visibleInput,0,0).width;
float cursorX;
if(showLabelWhileEditing){
cursorX = mouse.x - (b.x + b.width - textPadding - inputWidth);
}else{
cursorX = mouse.x - (b.x + textPadding);
}
auto cursorPos = round(visibleInputStart + ofMap(cursorX, 0, inputWidth, 0, ofUTF8Length(visibleInput)));
cursorPos = ofClamp(cursorPos, 0, ofUTF8Length(input));
calculateSelectionArea(mousePressedPos, cursorPos);
setNeedsRedraw();
}
return true;
}
//-----------------------------------------------------------
template<typename Type>
bool ofxInputField<Type>::mouseReleased(ofMouseEventArgs &){
bMousePressed = false;
return bGuiActive;
}
//-----------------------------------------------------------
template<typename Type>
bool ofxInputField<Type>::mouseScrolled(ofMouseEventArgs & mouse){
if(!isGuiDrawing() || insideSlider){
//when insideSlider it is the slider object who is in charge of handling the scrolling
return false;
}
if(b.inside(mouse)){
if(!bGuiActive){
if(mouse.scrollY>0 || mouse.scrollY<0){
double range = getRange(value.getMin(), value.getMax(), b.width);
Type newValue = value + ofMap(mouse.scrollY,-1,1,-range, range);
newValue = ofClamp(newValue,value.getMin(),value.getMax());
value = newValue;
}
}
return true;
}else{
return false;
}
}
//-----------------------------------------------------------
template<>
bool ofxInputField<string>::mouseScrolled(ofMouseEventArgs & mouse){
if(b.inside(mouse)){
return true;
}else{
return false;
}
}
//-----------------------------------------------------------
template<typename Type>
bool ofxInputField<Type>::charPressed(uint32_t & key){
if(!isGuiDrawing() && !insideSlider){
return false;
}
if(bGuiActive && !bMousePressed){
if(key >= '0' && key <= '9'){
moveCursor(insertKeystroke(key));
}else if(key == '.' || key == '-' || key == '+'){
moveCursor(insertKeystroke(key));
}else{
moveCursor(insertAlphabetic(key));
}
return true;
}
return false;
}
//-----------------------------------------------------------
template<typename Type>
bool ofxInputField<Type>::keyPressed(ofKeyEventArgs & args){
if(!isGuiDrawing()){
return false;
}
if(bGuiActive && !bMousePressed){
auto key = args.key;
auto first = std::min(selectStartPos, selectEndPos);
auto last = std::max(selectStartPos, selectEndPos);
auto selectLen = last - first;
if(key == OF_KEY_BACKSPACE || key == OF_KEY_DEL){
if(hasSelectedText()){
ofUTF8Erase(input, first, selectLen);
moveCursor(selectStartPos);
}else{
int deleteIdx = -1;
if(key == OF_KEY_BACKSPACE){
deleteIdx = selectStartPos-1;
}else if(key == OF_KEY_DEL){
deleteIdx = selectStartPos;
}
//erase char if valid deleteIdx
if(deleteIdx >= 0 && deleteIdx < (int)ofUTF8Length(input)){
ofUTF8Erase(input, deleteIdx, 1);
moveCursor(deleteIdx);
}
}
}else if(key == OF_KEY_LEFT){
if(args.hasModifier(OF_KEY_SHIFT)){
selectEndPos -= 1;
selectEndPos = std::max(0, selectEndPos);
calculateSelectionArea(selectStartPos, selectEndPos);
}else{
if(hasSelectedText()){
moveCursor(first);
}else{
moveCursor(std::max(0, selectEndPos - 1));
}
}
}else if(key == OF_KEY_RIGHT){
if(args.hasModifier(OF_KEY_SHIFT)){
auto inputSize = ofUTF8Length(input);
selectEndPos += 1;
selectEndPos = std::min(selectEndPos, int(inputSize));
calculateSelectionArea(selectStartPos, selectEndPos);
}else{
if(hasSelectedText()){
moveCursor(last);
}else{
auto inputSize = ofUTF8Length(input);
moveCursor(std::min(selectEndPos + 1, int(inputSize)));
}
}
}else if(key == OF_KEY_RETURN){
leaveFocus();
}else if(key == OF_KEY_ESC){
input = toString(value.get());
leaveFocus();
}else if(key == 'a' && args.hasModifier(OF_KEY_CONTROL)){
calculateSelectionArea(0, ofUTF8Length(input));
}else if(key == 'c' && args.hasModifier(OF_KEY_CONTROL)){
if(selectLen>0){
auto selection = ofUTF8Substring(input, first, selectLen);
ofSetClipboardString(selection);
}
}else if(key == 'v' && args.hasModifier(OF_KEY_CONTROL)){
auto cursorPos = first;
if(selectLen>0){
ofUTF8Erase(input, cursorPos, selectLen);
}
auto clipboard = ofGetClipboardString();
for(auto c: ofUTF8Iterator(clipboard)){
ofUTF8Insert(input, cursorPos, c);
cursorPos+=1;
}
moveCursor(cursorPos);
}else if(key == 'x' && args.hasModifier(OF_KEY_CONTROL)){
if(selectLen>0){
auto selection = ofUTF8Substring(input, first, selectLen);
ofSetClipboardString(selection);
ofUTF8Erase(input, first, selectLen);
moveCursor(first);
}
}else if(key == OF_KEY_END){
auto inputLength = ofUTF8Length(input);
if(args.hasModifier(OF_KEY_SHIFT)){
calculateSelectionArea(selectStartPos, inputLength);
}else{
moveCursor(inputLength);
}
}else if(key == OF_KEY_HOME){
if(args.hasModifier(OF_KEY_SHIFT)){
calculateSelectionArea(selectStartPos, 0);
}else{
moveCursor(0);
}
}
return true;
}
return false;
}
//-----------------------------------------------------------
template<typename Type>
int ofxInputField<Type>::insertKeystroke(uint32_t character){
auto first = std::min(selectStartPos, selectEndPos);
auto last = std::max(selectStartPos, selectEndPos);
auto selectLen = last - first;
if(hasSelectedText()){
ofUTF8Erase(input, first, selectLen);
}
ofUTF8Insert(input, first, character);
auto cursorPos = first + 1;
setNeedsRedraw();
return cursorPos;
}
//-----------------------------------------------------------
template<typename Type>
int ofxInputField<Type>::insertAlphabetic(uint32_t character){
if(character == 'x' || character == 'a' || character == 'b' || character=='c' || character=='d' || character=='e' || character=='f'){
return insertKeystroke(character);
}else{
return -1; //cursor or selection area stay the same
}
}
//-----------------------------------------------------------
template<>
int ofxInputField<string>::insertAlphabetic(uint32_t character){
return insertKeystroke(character);
}
//-----------------------------------------------------------
template<typename Type>
Type ofxInputField<Type>::operator=(Type v){
value = v;
return v;
}
//-----------------------------------------------------------
template<typename Type>
ofxInputField<Type>::operator const Type & (){
return value;
}
//-----------------------------------------------------------
template<typename Type>
bool ofxInputField<Type>::hasSelectedText(){
return selectStartPos != selectEndPos;
}
//-----------------------------------------------------------
template<typename Type>
void ofxInputField<Type>::generateDraw(){
bg.clear();
bg.append(rectangle(b, thisBackgroundColor));
if(bGuiActive){
if(hasSelectedText()){
ofRectangle selection(selectStartX, b.y+1, selectionWidth, b.height-2);
bg.append(rectangle(selection, thisFillColor));
}else if(!blinkingCursor){
ofRectangle cursor(selectStartX, b.y, 1, b.height);
bg.append(rectangle(cursor, thisTextColor));
}
}
auto input = visibleInput;
if(!bGuiActive && !containsValidValue()){
input = toString(value);
}
auto inputWidth = getTextBoundingBox(input,0,0).width;
auto yPos = getTextVCenteredInRect(b);
auto label = getTextBoundingBox(getName(), b.x + textPadding, yPos);
auto value = getTextBoundingBox(input, b.x + b.width - textPadding - inputWidth, yPos);
overlappingLabel = label.getMaxX() > value.x;
textMesh.clear();
if(!bGuiActive || showLabelWhileEditing){
if(!overlappingLabel || (!bMouseOver && !bGuiActive)){
textMesh.append(getTextMesh(getName(), b.x + textPadding, yPos) );
}
if((!bGuiActive && (bMouseOver || !overlappingLabel)) || bGuiActive){
textMesh.append(getTextMesh(input, b.x + b.width - textPadding - inputWidth, yPos));
}
}else{
textMesh.append(getTextMesh(input, b.x + textPadding, yPos));
}
textMesh.getColors().assign(textMesh.getVertices().size(), thisTextColor);
}
//-----------------------------------------------------------
template<typename Type>
void ofxInputField<Type>::render(){
bg.draw();
if(!insideSlider && errorTime > 0 && !containsValidValue()){
auto now = ofGetElapsedTimeMillis();
auto pct = (now - errorTime) * 0.5f;
if(pct<1){
for(size_t i=0;i<originalColors.size();i++){
bg.getColors()[i] = ofFloatColor::darkRed.getLerped(originalColors[i], pct);
}
}else{
bg.getColors() = originalColors;
errorTime = 0;
}
}
if(bGuiActive && !hasSelectedText() && blinkingCursor){
drawCursor();
}
ofBlendMode blendMode = ofGetStyle().blendingMode;
if(blendMode!=OF_BLENDMODE_ALPHA){
ofEnableAlphaBlending();
}
bindFontTexture();
textMesh.draw();
unbindFontTexture();
if(blendMode!=OF_BLENDMODE_ALPHA){
ofEnableBlendMode(blendMode);
}
}
//-----------------------------------------------------------
template<typename Type>
void ofxInputField<Type>::drawCursor(){
auto now = ofGetElapsedTimeMillis();
auto timeSinceLastCursorMove = now - lastCursorMoveTime;
if(!blinkingCursor || ((now % 2000) >= 1000) || (timeSinceLastCursorMove < 500)){
ofPushStyle();
ofSetColor(thisTextColor);
ofDrawLine( selectStartX, b.y, selectStartX, b.y+b.height );
ofPopStyle();
}
}
//-----------------------------------------------------------
template<typename Type>
void ofxInputField<Type>::setBlinkingCursor(bool blink){
blinkingCursor = blink;
setNeedsRedraw();
}
//-----------------------------------------------------------
template<typename Type>
void ofxInputField<Type>::setShowLabelWhileEditing(bool show){
showLabelWhileEditing = show;
setNeedsRedraw();
}
//-----------------------------------------------------------
template<typename Type>
bool ofxInputField<Type>::setValue(float mx, float my, bool bCheck){
return false;
}
//-----------------------------------------------------------
template<typename Type>
ofAbstractParameter & ofxInputField<Type>::getParameter(){
return value;
}
//-----------------------------------------------------------
template<typename Type>
void ofxInputField<Type>::parseInput(){
try{
Type tmpVal = fromString<Type>(input);
if(tmpVal < getMin()){
tmpVal = getMin();
}else if(tmpVal > getMax()){
tmpVal = getMax();
}
value = tmpVal;
validValue = true;
}catch(...){
if(!insideSlider){
originalColors = bg.getColors();
errorTime = ofGetElapsedTimeMillis();
}
validValue = false;
}
}
//-----------------------------------------------------------
template<>
void ofxInputField<string>::parseInput(){
value = input;
}
//-----------------------------------------------------------
template<typename Type>
void ofxInputField<Type>::valueChanged(Type & value){
visibleInput = input = toString(value);
if(bGuiActive){
moveCursor(ofUTF8Length(input));
}
setNeedsRedraw();
}
//-----------------------------------------------------------
template<typename Type>
void ofxInputField<Type>::leaveFocus(){
bGuiActive = false;
parseInput();
setNeedsRedraw();
leftFocus.notify(this);
}
//-----------------------------------------------------------
template<typename Type>
bool ofxInputField<Type>::containsValidValue() const{
return validValue;
}
//-----------------------------------------------------------
template class ofxInputField<int8_t>;
template class ofxInputField<uint8_t>;
template class ofxInputField<int16_t>;
template class ofxInputField<uint16_t>;
template class ofxInputField<int32_t>;
template class ofxInputField<uint32_t>;
template class ofxInputField<int64_t>;
template class ofxInputField<uint64_t>;
template class ofxInputField<float>;
template class ofxInputField<double>;
template class ofxInputField<std::string>;
//for some reason osx errors if this isn't defined
#ifdef TARGET_OSX
template class ofxInputField<typename std::conditional<std::is_same<uint32_t, size_t>::value || std::is_same<uint64_t, size_t>::value, bool, size_t>::type>;
#endif