rt300@0
|
1 //
|
rt300@0
|
2 // ButtronSlider.cpp
|
rt300@0
|
3 // emptyExample
|
rt300@0
|
4 //
|
rt300@0
|
5 // Created by Robert Tubb on 22/05/2013.
|
rt300@0
|
6 //
|
rt300@0
|
7 //
|
rt300@0
|
8
|
rt300@0
|
9 #include "ButtronSlider.h"
|
rt300@0
|
10
|
rt300@0
|
11
|
rt300@0
|
12 ButtronSlider::ButtronSlider(float ax,
|
rt300@0
|
13 float ay,
|
rt300@0
|
14 float awidth,
|
rt300@0
|
15 float aheight,
|
rt300@0
|
16 float athickness,
|
rt300@0
|
17 float aradius,
|
rt300@0
|
18 ofColor aforegroundHi,
|
rt300@0
|
19 ofColor abackgroundHi,
|
rt300@0
|
20 ofColor aforegroundLo,
|
rt300@0
|
21 ofColor abackgroundLo,
|
rt300@0
|
22 SliderType type) :
|
rt300@0
|
23 Buttron(ax,ay,awidth,aheight,athickness,aradius,aforegroundHi,abackgroundHi,aforegroundLo,abackgroundLo),
|
rt300@0
|
24 sliderType(type)
|
rt300@0
|
25
|
rt300@0
|
26 {
|
rt300@0
|
27
|
rt300@0
|
28 init();
|
rt300@0
|
29 }
|
rt300@0
|
30 ButtronSlider::ButtronSlider(float ax,
|
rt300@0
|
31 float ay,
|
rt300@0
|
32 float awidth,
|
rt300@0
|
33 float aheight,
|
rt300@0
|
34 SliderType type,
|
rt300@0
|
35 const UIProps& props) :
|
rt300@0
|
36 Buttron(ax,ay,awidth,aheight,props),
|
rt300@0
|
37 sliderType(type)
|
rt300@0
|
38
|
rt300@0
|
39 {
|
rt300@0
|
40 init();
|
rt300@0
|
41 }
|
rt300@0
|
42 void ButtronSlider::init(){
|
rt300@0
|
43 myType = SLIDER;
|
rt300@0
|
44 minVal = 0.;
|
rt300@0
|
45 maxVal = 127.;
|
rt300@0
|
46 value = 0.6;
|
rt300@0
|
47 hintShowing = false;
|
rt300@37
|
48
|
rt300@37
|
49 string fname = ofFilePath::getAbsolutePath(ofToDataPath("buttron.png"));
|
rt300@37
|
50 defaultImage.loadImage(fname);
|
rt300@37
|
51 setHandTexture(&defaultImage);
|
rt300@38
|
52 showScoreForFrames = 0;
|
rt300@38
|
53 hintPosAnimIncr = 0;
|
rt300@41
|
54 posAnimIncr = 0;
|
rt300@41
|
55 onlyOneTouchAllowed = true;
|
rt300@44
|
56 indicatorShowing = true;
|
rt300@0
|
57 }
|
rt300@37
|
58 //
|
rt300@0
|
59 //---------------------------------------------------------------------
|
rt300@0
|
60 void ButtronSlider::drawIndicator(double proportion){
|
rt300@27
|
61 if(!indicatorShowing) return;
|
rt300@0
|
62 if(on){
|
rt300@0
|
63 ofSetColor(foregroundHi);
|
rt300@0
|
64 }else{
|
rt300@0
|
65 ofSetColor(foregroundLo);
|
rt300@0
|
66
|
rt300@0
|
67 }
|
rt300@0
|
68 if(inactive){
|
rt300@0
|
69 ofSetColor(fgInactive);
|
rt300@0
|
70 }
|
rt300@37
|
71 if(showScoreForFrames > 0){
|
rt300@37
|
72 ofSetColor(flashColor);
|
rt300@37
|
73 showScoreForFrames--;
|
rt300@37
|
74 }
|
rt300@0
|
75 if(sliderType == FILL){
|
rt300@0
|
76
|
rt300@0
|
77 double maxH = height - 2 * thickness; //
|
rt300@0
|
78 double w = width - 2 * thickness;//
|
rt300@0
|
79 double barheight = value*maxH;
|
rt300@0
|
80 double top = y + height - thickness - barheight;
|
rt300@0
|
81 ofRect(x+thickness, top, w, barheight);
|
rt300@38
|
82
|
rt300@37
|
83 }else if(sliderType == TEXTURE_FILL){
|
rt300@38
|
84
|
rt300@37
|
85 double maxH = height - 2 * thickness; //
|
rt300@37
|
86 double w = width - 2 * thickness;//
|
rt300@37
|
87 double barheight = value*maxH;
|
rt300@37
|
88 double top = y + height - thickness - barheight;
|
rt300@37
|
89 //ofRect(x+thickness, top, w, barheight);
|
rt300@37
|
90 ofSetColor(255,255, 255);
|
rt300@37
|
91 (*handTextureRef).draw(x+thickness, top, w, barheight);
|
rt300@38
|
92
|
rt300@0
|
93 }else if(sliderType == LINE){
|
rt300@38
|
94
|
rt300@0
|
95 double loc = y + thickness + (1 -value)*(height-3*thickness);
|
rt300@0
|
96 ofRect(x+thickness,loc, width-2*thickness,thickness);
|
rt300@0
|
97 }
|
rt300@0
|
98
|
rt300@0
|
99
|
rt300@0
|
100 }
|
rt300@0
|
101
|
rt300@0
|
102 void ButtronSlider::drawHintIndicator(){
|
rt300@0
|
103
|
rt300@38
|
104 //ofSetColor(hintColor);
|
rt300@38
|
105 float hthick = 16;
|
rt300@38
|
106 double loc = y + thickness + (1 - hintValue)*(height-3*thickness);
|
rt300@38
|
107 //ofRect(x-thickness,loc, width+2*thickness,hthick);
|
rt300@38
|
108 ofSetColor(255, 255, 255);
|
rt300@44
|
109 (*handTextureRef).draw(x+thickness,loc-hthick/2, width-4*thickness,hthick);
|
rt300@0
|
110 }
|
rt300@0
|
111
|
rt300@29
|
112 void ButtronSlider::animateHintToNewValue(int newVal, float timeToTake){
|
rt300@31
|
113 hintTargVal = (newVal - minVal)/(maxVal - minVal);
|
rt300@29
|
114
|
rt300@29
|
115 float amtPerFrame = 1000./(ofGetFrameRate() * timeToTake);
|
rt300@31
|
116 hintPosAnimIncr = (hintTargVal - hintValue )*amtPerFrame;
|
rt300@29
|
117 animating = true;
|
rt300@29
|
118 }
|
rt300@31
|
119 void ButtronSlider::animateToNewValue(int newVal, float timeToTake){
|
rt300@31
|
120 targVal = (newVal - minVal)/(maxVal - minVal);
|
rt300@31
|
121
|
rt300@31
|
122 float amtPerFrame = 1000./(ofGetFrameRate() * timeToTake);
|
rt300@31
|
123 posAnimIncr = (targVal - value )*amtPerFrame;
|
rt300@47
|
124
|
rt300@47
|
125 animFrac = pow( 0.05, 1000./(ofGetFrameRate() * timeToTake));
|
rt300@31
|
126 animating = true;
|
rt300@31
|
127 }
|
rt300@29
|
128 void ButtronSlider::update(){
|
rt300@29
|
129 if (!animating) return;
|
rt300@47
|
130 float mov = (1-animFrac)*(targVal - value);
|
rt300@47
|
131 value += mov;
|
rt300@38
|
132 //hintValue += hintPosAnimIncr;
|
rt300@31
|
133
|
rt300@47
|
134 // when we've hit the target stop.
|
rt300@47
|
135 if (abs(targVal - value) < 0.01){
|
rt300@47
|
136 animating = false;
|
rt300@47
|
137 }
|
rt300@47
|
138 if (abs(hintTargVal - hintValue) < 0.01){
|
rt300@47
|
139 animating = false;
|
rt300@47
|
140 }
|
rt300@29
|
141 }
|
rt300@0
|
142 //---------------------------------------------------------------------
|
rt300@0
|
143
|
rt300@0
|
144 bool ButtronSlider::handleMyTouch(int tx, int ty, touchType ttype, int touchID){
|
rt300@0
|
145
|
rt300@0
|
146 double ly = ty - y - thickness - radius;
|
rt300@0
|
147
|
rt300@0
|
148 double prop;
|
rt300@0
|
149
|
rt300@0
|
150 prop = 1 - ly/(height - 2 * (thickness + radius));
|
rt300@0
|
151
|
rt300@0
|
152 if(prop > 1.) prop = 1.;
|
rt300@0
|
153 if(prop < 0.) prop = 0.;
|
rt300@0
|
154
|
rt300@0
|
155 setValue(prop);
|
rt300@0
|
156 int scaleVal = int((maxVal - minVal)*prop + minVal);
|
rt300@0
|
157
|
rt300@0
|
158 vector<int> pass;
|
rt300@0
|
159 if(callback) callback(myParamID,scaleVal);
|
rt300@0
|
160
|
rt300@0
|
161 return true;
|
rt300@29
|
162 }
|
rt300@37
|
163 //--------------------------------------------------------------------
|
rt300@39
|
164 void ButtronSlider::flashScore(int score, ofColor col, int howLong){
|
rt300@39
|
165 showScoreForFrames = howLong;
|
rt300@37
|
166 flashColor = col;
|
rt300@37
|
167 }
|