root / Community Core Vision / Windows / addons / ofxNCore / src / Controls / ofxGuiSlider.cpp @ 8
View | Annotate | Download (5 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 | |
| 57 | mValue = value; |
| 58 | } |
| 59 | |
| 60 | // ----------------------------------------------------------------------------------------------------
|
| 61 | |
| 62 | void ofxGuiSlider::setRange(float min, float max) |
| 63 | {
|
| 64 | mMinVal = min; |
| 65 | mMaxVal = max; |
| 66 | mValDlt = mMaxVal - mMinVal; |
| 67 | } |
| 68 | |
| 69 | // ----------------------------------------------------------------------------------------------------
|
| 70 | |
| 71 | bool ofxGuiSlider::update(int id, int task, void* data, int length) |
| 72 | {
|
| 73 | bool handled = false; |
| 74 | |
| 75 | if(id == mParamId)
|
| 76 | {
|
| 77 | if(task == kofxGui_Set_Float)
|
| 78 | {
|
| 79 | setValue((float)*(int*)data); // This hack is only for CCV original was *(float*)data |
| 80 | } |
| 81 | handled = true;
|
| 82 | } |
| 83 | |
| 84 | return handled;
|
| 85 | } |
| 86 | |
| 87 | // ----------------------------------------------------------------------------------------------------
|
| 88 | |
| 89 | void ofxGuiSlider::draw()
|
| 90 | {
|
| 91 | glPushMatrix(); |
| 92 | |
| 93 | glTranslatef(mObjX, mObjY, 0.0); |
| 94 | |
| 95 | if(mParamName != "") |
| 96 | drawParamString(0.0, 0.0, mParamName + ": " + floatToString(mValue, mDisplay), false); |
| 97 | |
| 98 | float x = (mCtrWidth * valueToFraction(mValue));
|
| 99 | |
| 100 | ofFill(); |
| 101 | |
| 102 | // background
|
| 103 | glColor4f(mGlobals->mCoverColor.r, mGlobals->mCoverColor.g, mGlobals->mCoverColor.b, mGlobals->mCoverColor.a); |
| 104 | ofRect(mCtrX, mCtrY, mCtrWidth, mCtrHeight); |
| 105 | |
| 106 | // action
|
| 107 | glColor4f(mGlobals->mSliderColor.r, mGlobals->mSliderColor.g, mGlobals->mSliderColor.b, mGlobals->mSliderColor.a); |
| 108 | ofRect(mCtrX, mCtrY, x, mCtrHeight); |
| 109 | |
| 110 | // handle
|
| 111 | glColor4f(mGlobals->mHandleColor.r, mGlobals->mHandleColor.g, mGlobals->mHandleColor.b, mGlobals->mHandleColor.a); |
| 112 | ofRect(x, mCtrY, 1.0, mCtrHeight); |
| 113 | |
| 114 | ofNoFill(); |
| 115 | |
| 116 | // frame
|
| 117 | glColor4f(mGlobals->mFrameColor.r, mGlobals->mFrameColor.g, mGlobals->mFrameColor.b, mGlobals->mFrameColor.a); |
| 118 | ofRect(mCtrX, mCtrY, mCtrWidth, mCtrHeight); |
| 119 | |
| 120 | glPopMatrix(); |
| 121 | } |
| 122 | |
| 123 | // ----------------------------------------------------------------------------------------------------
|
| 124 | |
| 125 | bool ofxGuiSlider::mouseDragged(int x, int y, int button) |
| 126 | {
|
| 127 | if(mMouseIsDown)
|
| 128 | {
|
| 129 | float value = fractionToValue(mouseToFraction(mouseToLocal(x, y)).x);
|
| 130 | |
| 131 | if(value != mValue)
|
| 132 | {
|
| 133 | setValue(value); |
| 134 | mGlobals->mListener->handleGui(mParamId, kofxGui_Set_Float, &mValue, sizeof(float)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return mMouseIsDown;
|
| 139 | } |
| 140 | |
| 141 | // ----------------------------------------------------------------------------------------------------
|
| 142 | |
| 143 | bool ofxGuiSlider::mousePressed(int x, int y, int button) |
| 144 | {
|
| 145 | mMouseIsDown = isPointInsideMe(mouseToLocal(x, y)); |
| 146 | |
| 147 | if (mMouseIsDown)
|
| 148 | mouseDragged(x, y, button); |
| 149 | |
| 150 | return mMouseIsDown;
|
| 151 | } |
| 152 | |
| 153 | // ----------------------------------------------------------------------------------------------------
|
| 154 | |
| 155 | bool ofxGuiSlider::mouseReleased(int x, int y, int button) |
| 156 | {
|
| 157 | bool handled = mMouseIsDown;
|
| 158 | |
| 159 | if(mMouseIsDown)
|
| 160 | mMouseIsDown = false;
|
| 161 | |
| 162 | return handled;
|
| 163 | } |
| 164 | |
| 165 | // ----------------------------------------------------------------------------------------------------
|
| 166 | |
| 167 | void ofxGuiSlider::buildFromXml()
|
| 168 | {
|
| 169 | mGlobals->mListener->handleGui(mParamId, kofxGui_Set_Float, &mValue, sizeof(float)); |
| 170 | } |
| 171 | |
| 172 | // ----------------------------------------------------------------------------------------------------
|
| 173 | |
| 174 | void ofxGuiSlider::saveToXml()
|
| 175 | {
|
| 176 | int id = saveObjectData();
|
| 177 | |
| 178 | mGlobals->mXml.setValue("OBJECT:MIN", mMinVal, id);
|
| 179 | mGlobals->mXml.setValue("OBJECT:MAX", mMaxVal, id);
|
| 180 | mGlobals->mXml.setValue("OBJECT:VALUE", mValue, id);
|
| 181 | } |
| 182 | |
| 183 | // ----------------------------------------------------------------------------------------------------
|
| 184 | |
| 185 | float ofxGuiSlider::valueToFraction(float value) |
| 186 | {
|
| 187 | return (value - mMinVal) / mValDlt;
|
| 188 | } |
| 189 | |
| 190 | // ----------------------------------------------------------------------------------------------------
|
| 191 | |
| 192 | float ofxGuiSlider::fractionToValue(float fraction) |
| 193 | {
|
| 194 | return (mValDlt * fraction) + mMinVal;
|
| 195 | } |
| 196 | |
| 197 | // ----------------------------------------------------------------------------------------------------
|
