-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofxJFNumberBox.cpp
187 lines (120 loc) · 4.04 KB
/
ofxJFNumberBox.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
//
// ofxJFNumberBox.cpp
// jfGui
//
// Created by João Fonseca on 05/04/15.
//
//
#include "ofxJFNumberBox.h"
ofxJFNumberBox::ofxJFNumberBox(){
enableKeyEvents();
enableMouseEvents();
}
ofxJFNumberBox::ofxJFNumberBox(string _name, float _min, float _max, int _x, int _y, int _precision, int _width, int _height){
min=_min;
max=_max;
name=_name;
precision=_precision;
location.set(_x, _y);
size.set(_width,_height);
totalHeight=size.y;
enableKeyEvents();
enableMouseEvents();
value=ofRandom(1);
boxPositionX=location.x-size.x;
boxPositionY=location.y-(size.x/4);
addLabel(ofToString(value,precision), ofPoint(location.x,location.y));
ofRectangle bounds=getBoundingBox();
setEventArea(ofPoint(bounds.x,bounds.y), ofVec2f(bounds.width,bounds.height));
setAlignment(JF_RIGHT);
backgroundColor=SLIDER_BACKGROUND_COLOR;
sliderColor=SLIDER_COLOR;
handleColor=SLIDER_HANDLE_COLOR;
inputValue="";
widgetChangedSize=false;
updateValue=false;
updatedInputValue=false;
isMaximized=false;
initialLocation=location;
}
void ofxJFNumberBox::setLocation(ofPoint _location){
location=_location;
setLPosition(ofPoint(location.x,location.y));
ofRectangle bounds=getBoundingBox();
setEventArea(ofPoint(bounds.x,bounds.y), ofVec2f(bounds.width,bounds.height));
}
void ofxJFNumberBox::showLabel(bool status){
labelIsVisible=status;
}
void ofxJFNumberBox::update(){
updatedInputValue=false;
}
void ofxJFNumberBox::drawController(){
//ofRect =
ofPushStyle();
//ofSetColor(255,80);
//ofRect(getBoundingBox());
drawLabel();
ofPopStyle();
}
void ofxJFNumberBox::setValue(float _value){
value=_value;
setLabel(ofToString(value,precision));
}
float ofxJFNumberBox::getValue(){
return value;
}
void ofxJFNumberBox::mousePressed(int x, int y, int button){
mousePressedLocation.set(x,y);
//value=ofMap(ofClamp(x, location.x, location.x+size.x),location.x, location.x+size.x,0,1);
updateValue=true;
setLabel(inputValue);
}
void ofxJFNumberBox::mouseDragged(int x, int y, int button){
/*
updateValue=true;
if(updateValue){
float draggedDistance=ofClamp((mousePressedLocation.y-y),-200,200);//ofDist(x, y, mousePressedLocation.x, mousePressedLocation.y);
// value=ofMap(draggedDistance,-200,200,0,1);
//value=ofMap(ofClamp(x, location.x, location.x+size.x),location.x, location.x+size.x,0,1);
}
*/
}
void ofxJFNumberBox::mouseReleased(int x, int y, int button){
if(!hitTest(x, y) && updateValue){
updateValue=false;
if(inputValue!="")value=ofClamp(ofToFloat(inputValue),min,max);
setLabel(ofToString(value,precision));
inputValue="";
updatedInputValue=true;
ofRectangle bounds=getBoundingBox();
setEventArea(ofPoint(bounds.x,bounds.y), ofVec2f(bounds.width,bounds.height));
}
}
void ofxJFNumberBox::doubleClick(int x, int y, int button){
// updateValue=true;
// setLabel(inputValue);
}
void ofxJFNumberBox::keyPressed(int key){
if(updateValue){
if ((key>=48 && key<=57) || key == 46 ) {
// if(inputValue.size()<precision){
inputValue+=key;
setLabel(inputValue);
//}
}else if(key == 127 ){
if(inputValue.size())inputValue.resize(inputValue.size()-1);
setLabel(inputValue);
}else if( key== 13 ){
updateValue=false;
if(inputValue!="")value=ofClamp(ofToFloat(inputValue),min,max);
setLabel(ofToString(value,precision));
inputValue="";
updatedInputValue=true;
ofRectangle bounds=getBoundingBox();
setEventArea(ofPoint(bounds.x,bounds.y), ofVec2f(bounds.width,bounds.height));
}
}
}
void ofxJFNumberBox::keyReleased(int key){
}