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