root / trunk / tbeta / Windows / addons / ofxGui / ofxGuiKnob.cpp @ 80
View | Annotate | Download (7.4 KB)
| 1 | /*
|
|---|---|
| 2 | * ofxGuiKnob.cpp |
| 3 | * openFrameworks |
| 4 | * |
| 5 | * Created by Stefan Kirch on 18.06.08. |
| 6 | * Copyright 2008 alphakanal. All rights reserved. |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | // ----------------------------------------------------------------------------------------------------
|
| 11 | |
| 12 | #include "ofxGuiKnob.h" |
| 13 | |
| 14 | // ----------------------------------------------------------------------------------------------------
|
| 15 | |
| 16 | ofxGuiKnob::ofxGuiKnob() |
| 17 | {
|
| 18 | mParamType = kofxGui_Object_Knob; |
| 19 | } |
| 20 | |
| 21 | // ----------------------------------------------------------------------------------------------------
|
| 22 | |
| 23 | void ofxGuiKnob::init(int id, string name, int x, int y, int width, int height, float min, float max, float value, int display, int steps) |
| 24 | {
|
| 25 | int textHeight = (name == "") ? 0 : mGlobals->mParamFontHeight; |
| 26 | |
| 27 | mParamId = id; |
| 28 | mParamName = name; |
| 29 | |
| 30 | mObjX = x; |
| 31 | mObjY = y; |
| 32 | |
| 33 | mObjWidth = width; |
| 34 | mObjHeight = textHeight + height; |
| 35 | |
| 36 | mDisplay = display; |
| 37 | mSteps = steps; |
| 38 | |
| 39 | setRange(min, max); |
| 40 | setValue(value); |
| 41 | setControlRegion(0, textHeight, width, height);
|
| 42 | } |
| 43 | |
| 44 | // ----------------------------------------------------------------------------------------------------
|
| 45 | |
| 46 | void ofxGuiKnob::setValue(float value) |
| 47 | {
|
| 48 | if(mSteps > 1) |
| 49 | {
|
| 50 | float fraction = valueToFraction(value);
|
| 51 | float steps = (float)mSteps - 1; |
| 52 | float slice = roundInt(fraction * steps) / steps;
|
| 53 | |
| 54 | value = mMinVal + mValDlt * slice; |
| 55 | |
| 56 | if (mDisplay == kofxGui_Display_String && value != mValue)
|
| 57 | {
|
| 58 | int id = (int)value; |
| 59 | mGlobals->mListener->handleGui(mParamId, kofxGui_Get_String, &id, sizeof(int)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | mValue = value; |
| 64 | } |
| 65 | |
| 66 | // ----------------------------------------------------------------------------------------------------
|
| 67 | |
| 68 | void ofxGuiKnob::setRange(float min, float max) |
| 69 | {
|
| 70 | mMinVal = min; |
| 71 | mMaxVal = max; |
| 72 | mValDlt = mMaxVal - mMinVal; |
| 73 | } |
| 74 | |
| 75 | // ----------------------------------------------------------------------------------------------------
|
| 76 | |
| 77 | bool ofxGuiKnob::update(int id, int task, void* data, int length) |
| 78 | {
|
| 79 | bool handled = false; |
| 80 | |
| 81 | if(id == mParamId)
|
| 82 | {
|
| 83 | if(task == kofxGui_Set_Float)
|
| 84 | setValue(*(float*)data);
|
| 85 | else if(task == kofxGui_Set_String) |
| 86 | mDisplaySting = *(string*)data; |
| 87 | |
| 88 | handled = true;
|
| 89 | } |
| 90 | |
| 91 | return handled;
|
| 92 | } |
| 93 | |
| 94 | // ----------------------------------------------------------------------------------------------------
|
| 95 | |
| 96 | void ofxGuiKnob::draw()
|
| 97 | {
|
| 98 | glPushMatrix(); |
| 99 | |
| 100 | glTranslatef(mObjX, mObjY, 0.0); |
| 101 | |
| 102 | float w = mCtrWidth / 2; |
| 103 | float h = mCtrHeight / 2; |
| 104 | float x = mCtrX + w;
|
| 105 | float y = mCtrY + h;
|
| 106 | |
| 107 | float v1 = 130.0f; |
| 108 | float v2 = 280.0f; |
| 109 | |
| 110 | int resolution = 22; |
| 111 | |
| 112 | float angle, step, cosine, sine, xpos, ypos;
|
| 113 | |
| 114 | if(mParamName != "") |
| 115 | drawParamString(x, 0.0, mParamName, true); |
| 116 | |
| 117 | ofFill(); |
| 118 | |
| 119 | // background
|
| 120 | glColor4f(mGlobals->mCoverColor.r, mGlobals->mCoverColor.g, mGlobals->mCoverColor.b, mGlobals->mCoverColor.a); |
| 121 | ofEllipse(x, y, w, h); |
| 122 | |
| 123 | // action
|
| 124 | glColor4f(mGlobals->mSliderColor.r, mGlobals->mSliderColor.g, mGlobals->mSliderColor.b, mGlobals->mSliderColor.a); |
| 125 | |
| 126 | step = (DEG_TO_RAD * valueToFraction(mValue) * v2) / (resolution - 1);
|
| 127 | angle = DEG_TO_RAD * v1; |
| 128 | |
| 129 | glBegin(GL_TRIANGLE_STRIP); |
| 130 | for(int i = 0; i < resolution; i++) |
| 131 | {
|
| 132 | cosine = cos(angle); |
| 133 | sine = sin(angle); |
| 134 | |
| 135 | xpos = cosine * w; |
| 136 | ypos = sine * h; |
| 137 | |
| 138 | glVertex2f(x + xpos, y + ypos); |
| 139 | |
| 140 | xpos = cosine * (w - mGlobals->mKnobSize); |
| 141 | ypos = sine * (h - mGlobals->mKnobSize); |
| 142 | |
| 143 | glVertex2f(x + xpos, y + ypos); |
| 144 | |
| 145 | angle += step; |
| 146 | } |
| 147 | glEnd(); |
| 148 | |
| 149 | ofNoFill(); |
| 150 | |
| 151 | // handle
|
| 152 | glColor4f(mGlobals->mHandleColor.r, mGlobals->mHandleColor.g, mGlobals->mHandleColor.b, mGlobals->mHandleColor.a); |
| 153 | |
| 154 | angle = DEG_TO_RAD * v1; |
| 155 | cosine = cos(angle); |
| 156 | sine = sin(angle); |
| 157 | |
| 158 | ofLine(x + cosine * w, y + sine * h, x + cosine * (w - mGlobals->mKnobSize), y + sine * (h - mGlobals->mKnobSize)); |
| 159 | |
| 160 | angle = DEG_TO_RAD * (v1 + valueToFraction(mValue) * v2); |
| 161 | cosine = cos(angle); |
| 162 | sine = sin(angle); |
| 163 | |
| 164 | ofLine(x + cosine * w, y + sine * h, x + cosine * (w - mGlobals->mKnobSize), y + sine * (h - mGlobals->mKnobSize)); |
| 165 | |
| 166 | step = (DEG_TO_RAD * valueToFraction(mValue) * v2) / (resolution - 1);
|
| 167 | angle = DEG_TO_RAD * v1; |
| 168 | |
| 169 | glPushMatrix(); |
| 170 | glTranslatef(x, y, 0.0); |
| 171 | ofBeginShape(); |
| 172 | |
| 173 | for(int i = 0; i < resolution; i++) |
| 174 | {
|
| 175 | xpos = cos(angle) * (w - mGlobals->mKnobSize); |
| 176 | ypos = sin(angle) * (h - mGlobals->mKnobSize); |
| 177 | |
| 178 | ofVertex(xpos, ypos); |
| 179 | angle += step; |
| 180 | } |
| 181 | |
| 182 | ofEndShape(false);
|
| 183 | glPopMatrix(); |
| 184 | |
| 185 | // frame
|
| 186 | glColor4f(mGlobals->mFrameColor.r, mGlobals->mFrameColor.g, mGlobals->mFrameColor.b, mGlobals->mFrameColor.a); |
| 187 | ofEllipse(x, y, w, h); |
| 188 | |
| 189 | if (mDisplay == kofxGui_Display_String && mSteps > 1) |
| 190 | drawValueString(x, y, mDisplaySting); |
| 191 | else
|
| 192 | drawValueString(x, y, floatToString(mValue, mDisplay)); |
| 193 | |
| 194 | glPopMatrix(); |
| 195 | } |
| 196 | |
| 197 | // ----------------------------------------------------------------------------------------------------
|
| 198 | |
| 199 | bool ofxGuiKnob::mouseDragged(int x, int y, int button) |
| 200 | {
|
| 201 | if(mMouseIsDown)
|
| 202 | {
|
| 203 | ofxPoint2f point = mouseToLocal(x, y); |
| 204 | |
| 205 | float deltaX = point.x - mFirstHit.x;
|
| 206 | float deltaY = point.y - mFirstHit.y;
|
| 207 | float delta = deltaX - deltaY;
|
| 208 | float value = CLAMP(mValue + delta * (mValDlt / 100.0f), mMinVal, mMaxVal); |
| 209 | |
| 210 | if(value != mValue)
|
| 211 | {
|
| 212 | setValue(value); |
| 213 | mGlobals->mListener->handleGui(mParamId, kofxGui_Set_Float, &mValue, sizeof(float)); |
| 214 | |
| 215 | mFirstHit = point; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return mMouseIsDown;
|
| 220 | } |
| 221 | |
| 222 | // ----------------------------------------------------------------------------------------------------
|
| 223 | |
| 224 | bool ofxGuiKnob::mousePressed(int x, int y, int button) |
| 225 | {
|
| 226 | ofxPoint2f point = mouseToLocal(x, y); |
| 227 | mMouseIsDown = isPointInsideMe(point); |
| 228 | |
| 229 | if (mMouseIsDown)
|
| 230 | {
|
| 231 | mFirstHit = point; |
| 232 | mouseDragged(x, y, button); |
| 233 | } |
| 234 | |
| 235 | return mMouseIsDown;
|
| 236 | } |
| 237 | |
| 238 | // ----------------------------------------------------------------------------------------------------
|
| 239 | |
| 240 | bool ofxGuiKnob::mouseReleased(int x, int y, int button) |
| 241 | {
|
| 242 | bool handled = mMouseIsDown;
|
| 243 | |
| 244 | if(mMouseIsDown)
|
| 245 | mMouseIsDown = false;
|
| 246 | |
| 247 | return handled;
|
| 248 | } |
| 249 | |
| 250 | // ----------------------------------------------------------------------------------------------------
|
| 251 | |
| 252 | void ofxGuiKnob::buildFromXml()
|
| 253 | {
|
| 254 | mGlobals->mListener->handleGui(mParamId, kofxGui_Set_Float, &mValue, sizeof(float)); |
| 255 | } |
| 256 | |
| 257 | // ----------------------------------------------------------------------------------------------------
|
| 258 | |
| 259 | void ofxGuiKnob::saveToXml()
|
| 260 | {
|
| 261 | int id = saveObjectData();
|
| 262 | |
| 263 | mGlobals->mXml.setValue("OBJECT:MIN", mMinVal, id);
|
| 264 | mGlobals->mXml.setValue("OBJECT:MAX", mMaxVal, id);
|
| 265 | mGlobals->mXml.setValue("OBJECT:VALUE", mValue, id);
|
| 266 | } |
| 267 | |
| 268 | // ----------------------------------------------------------------------------------------------------
|
| 269 | |
| 270 | float ofxGuiKnob::valueToFraction(float value) |
| 271 | {
|
| 272 | return (value - mMinVal) / mValDlt;
|
| 273 | } |
| 274 | |
| 275 | // ----------------------------------------------------------------------------------------------------
|
| 276 | |
| 277 | float ofxGuiKnob::fractionToValue(float fraction) |
| 278 | {
|
| 279 | return (mValDlt * fraction) + mMinVal;
|
| 280 | } |
| 281 | |
| 282 | // ----------------------------------------------------------------------------------------------------
|
| 283 | |
| 284 | void ofxGuiKnob::drawValueString(float x, float y, string text) |
| 285 | {
|
| 286 | glColor4f(mGlobals->mTextColor.r, mGlobals->mTextColor.g, mGlobals->mTextColor.b, mGlobals->mTextColor.a); |
| 287 | |
| 288 | x -= roundInt(mGlobals->mParamFont.stringWidth(text) / 2.0f); |
| 289 | mGlobals->mParamFont.drawString(text, x, y); |
| 290 | |
| 291 | // debug rect to position font
|
| 292 | /*
|
| 293 | ofRectangle rect = mGlobals->mParamFont.getStringBoundingBox(text, x, y); |
| 294 | ofNoFill(); |
| 295 | glColor4f(1.0, 0.0, 0.0, 1.0); |
| 296 | ofRect(x, y, rect.width, mGlobals->mParamFontHeight); |
| 297 | */ |
| 298 | } |
| 299 | |
| 300 | // ----------------------------------------------------------------------------------------------------
|
