root / branches / tbeta / ccv_ofx_0061 / Windows / addons / ofxNCore / src / Controls / ofxGuiSlider.cpp @ 214
View | Annotate | Download (5.4 KB)
| 1 | /*
|
|---|---|
| 2 | * ofxGuiSlider.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 "ofxGuiSlider.h" |
| 13 | |
| 14 | // ----------------------------------------------------------------------------------------------------
|
| 15 | |
| 16 | ofxGuiSlider::ofxGuiSlider() |
| 17 | {
|
| 18 | mParamType = kofxGui_Object_Slider; |
| 19 | } |
| 20 | |
| 21 | // ----------------------------------------------------------------------------------------------------
|
| 22 | |
| 23 | void ofxGuiSlider::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 ofxGuiSlider::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 ofxGuiSlider::setRange(float min, float max) |
| 69 | {
|
| 70 | mMinVal = min; |
| 71 | mMaxVal = max; |
| 72 | mValDlt = mMaxVal - mMinVal; |
| 73 | } |
| 74 | |
| 75 | // ----------------------------------------------------------------------------------------------------
|
| 76 | |
| 77 | bool ofxGuiSlider::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 ofxGuiSlider::draw()
|
| 97 | {
|
| 98 | glPushMatrix(); |
| 99 | |
| 100 | glTranslatef(mObjX, mObjY, 0.0); |
| 101 | |
| 102 | if(mParamName != "") |
| 103 | {
|
| 104 | if (mDisplay == kofxGui_Display_String && mSteps > 1) |
| 105 | drawParamString(0.0, 0.0, mParamName + ": " + mDisplaySting, false); |
| 106 | else
|
| 107 | drawParamString(0.0, 0.0, mParamName + ": " + floatToString(mValue, mDisplay), false); |
| 108 | } |
| 109 | |
| 110 | float x = (mCtrWidth * valueToFraction(mValue));
|
| 111 | |
| 112 | ofFill(); |
| 113 | |
| 114 | // background
|
| 115 | glColor4f(mGlobals->mCoverColor.r, mGlobals->mCoverColor.g, mGlobals->mCoverColor.b, mGlobals->mCoverColor.a); |
| 116 | ofRect(mCtrX, mCtrY, mCtrWidth, mCtrHeight); |
| 117 | |
| 118 | // action
|
| 119 | glColor4f(mGlobals->mSliderColor.r, mGlobals->mSliderColor.g, mGlobals->mSliderColor.b, mGlobals->mSliderColor.a); |
| 120 | ofRect(mCtrX, mCtrY, x, mCtrHeight); |
| 121 | |
| 122 | // handle
|
| 123 | glColor4f(mGlobals->mHandleColor.r, mGlobals->mHandleColor.g, mGlobals->mHandleColor.b, mGlobals->mHandleColor.a); |
| 124 | ofRect(x, mCtrY, 1.0, mCtrHeight); |
| 125 | |
| 126 | ofNoFill(); |
| 127 | |
| 128 | // frame
|
| 129 | glColor4f(mGlobals->mFrameColor.r, mGlobals->mFrameColor.g, mGlobals->mFrameColor.b, mGlobals->mFrameColor.a); |
| 130 | ofRect(mCtrX, mCtrY, mCtrWidth, mCtrHeight); |
| 131 | |
| 132 | glPopMatrix(); |
| 133 | } |
| 134 | |
| 135 | // ----------------------------------------------------------------------------------------------------
|
| 136 | |
| 137 | bool ofxGuiSlider::mouseDragged(int x, int y, int button) |
| 138 | {
|
| 139 | if(mMouseIsDown)
|
| 140 | {
|
| 141 | float value = fractionToValue(mouseToFraction(mouseToLocal(x, y)).x);
|
| 142 | |
| 143 | if(value != mValue)
|
| 144 | {
|
| 145 | setValue(value); |
| 146 | mGlobals->mListener->handleGui(mParamId, kofxGui_Set_Float, &mValue, sizeof(float)); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return mMouseIsDown;
|
| 151 | } |
| 152 | |
| 153 | // ----------------------------------------------------------------------------------------------------
|
| 154 | |
| 155 | bool ofxGuiSlider::mousePressed(int x, int y, int button) |
| 156 | {
|
| 157 | mMouseIsDown = isPointInsideMe(mouseToLocal(x, y)); |
| 158 | |
| 159 | if (mMouseIsDown)
|
| 160 | mouseDragged(x, y, button); |
| 161 | |
| 162 | return mMouseIsDown;
|
| 163 | } |
| 164 | |
| 165 | // ----------------------------------------------------------------------------------------------------
|
| 166 | |
| 167 | bool ofxGuiSlider::mouseReleased(int x, int y, int button) |
| 168 | {
|
| 169 | bool handled = mMouseIsDown;
|
| 170 | |
| 171 | if(mMouseIsDown)
|
| 172 | mMouseIsDown = false;
|
| 173 | |
| 174 | return handled;
|
| 175 | } |
| 176 | |
| 177 | // ----------------------------------------------------------------------------------------------------
|
| 178 | |
| 179 | void ofxGuiSlider::buildFromXml()
|
| 180 | {
|
| 181 | mGlobals->mListener->handleGui(mParamId, kofxGui_Set_Float, &mValue, sizeof(float)); |
| 182 | } |
| 183 | |
| 184 | // ----------------------------------------------------------------------------------------------------
|
| 185 | |
| 186 | void ofxGuiSlider::saveToXml()
|
| 187 | {
|
| 188 | int id = saveObjectData();
|
| 189 | |
| 190 | mGlobals->mXml.setValue("OBJECT:MIN", mMinVal, id);
|
| 191 | mGlobals->mXml.setValue("OBJECT:MAX", mMaxVal, id);
|
| 192 | mGlobals->mXml.setValue("OBJECT:VALUE", mValue, id);
|
| 193 | } |
| 194 | |
| 195 | // ----------------------------------------------------------------------------------------------------
|
| 196 | |
| 197 | float ofxGuiSlider::valueToFraction(float value) |
| 198 | {
|
| 199 | return (value - mMinVal) / mValDlt;
|
| 200 | } |
| 201 | |
| 202 | // ----------------------------------------------------------------------------------------------------
|
| 203 | |
| 204 | float ofxGuiSlider::fractionToValue(float fraction) |
| 205 | {
|
| 206 | return (mValDlt * fraction) + mMinVal;
|
| 207 | } |
| 208 | |
| 209 | // ----------------------------------------------------------------------------------------------------
|
