Community Core Vision 1.4
Cross platform, user friendly computer vision.
C:/CCV/1.4/addons/ofxNCore/src/Controls/ofxGuiScope.cpp
Go to the documentation of this file.
00001 /*
00002  *  ofxGuiScope.cpp
00003  *  openFrameworks
00004  *
00005  *  Created by Stefan Kirch on 18.06.08.
00006  *  Copyright 2008 alphakanal. All rights reserved.
00007  *
00008  */
00009 
00010 //      ----------------------------------------------------------------------------------------------------
00011 
00012 #include "ofxGuiScope.h"
00013 
00014 //      ----------------------------------------------------------------------------------------------------
00015 
00016 ofxGuiScope::ofxGuiScope()
00017 {
00018         mParamType              = kofxGui_Object_Scope;
00019         mBuffer                 = NULL;
00020         mBufferLength   = 0;
00021 }
00022 
00023 //      ----------------------------------------------------------------------------------------------------
00024 
00025 ofxGuiScope::~ofxGuiScope()
00026 {
00027         if(mBuffer)
00028                 delete[] mBuffer;
00029 }
00030 
00031 //      ----------------------------------------------------------------------------------------------------
00032 
00033 void ofxGuiScope::init(int id, string name, int x, int y, int width, int height, int length, ofxPoint2f value, int display)
00034 {
00035         int     textHeight      = (name == "") ? 0 : mGlobals->mParamFontHeight;
00036         
00037         mParamId                = id;
00038         mParamName              = name;
00039         
00040         mObjX                   = x; 
00041         mObjY                   = y;
00042         
00043         mObjWidth               = width;
00044         mObjHeight              = textHeight + height;
00045                 
00046         mDisplay                = display;
00047         
00048         setValue(value);
00049         setControlRegion(0, textHeight, width, height);
00050 
00051         if(mBuffer)
00052                 delete[] mBuffer;
00053         
00054         mBufferLength   = length;
00055         mBuffer                 = new float[mBufferLength];
00056         
00057         memset(mBuffer, 0, sizeof(float) * mBufferLength);
00058 
00059         mGlobals->mListener->handleGui(mParamId, kofxGui_Set_FloatArray, mBuffer, mBufferLength);
00060 }
00061 
00062 //      ----------------------------------------------------------------------------------------------------
00063 
00064 void ofxGuiScope::setValue(ofxPoint2f value)
00065 {
00066         //      todo:   steps
00067         
00068         mValue = value;
00069 }
00070 
00071 //      ----------------------------------------------------------------------------------------------------
00072 
00073 bool ofxGuiScope::update(int id, int task, void* data, int length)
00074 {
00075         bool handled = false;
00076         
00077         if(id == mParamId)
00078         {
00079                 if(task == kofxGui_Set_Float)
00080                         setValue(ofxPoint2f(CLAMP(*(float*)data, 0.0, 1.0) * mBufferLength, mValue.y));
00081                 else if(task == kofxGui_Set_Point)
00082                         setValue(*(ofxPoint2f*)data);
00083                 
00084                 handled = true;
00085         }
00086         
00087         return handled;
00088 }
00089 
00090 //      ----------------------------------------------------------------------------------------------------
00091 
00092 void ofxGuiScope::draw()
00093 {
00094         glPushMatrix();
00095         
00096         glTranslatef(mObjX, mObjY, 0.0);
00097         
00098         if(mParamName != "")
00099                 drawParamString(0.0, 0.0, mParamName + 
00100                                                 ": sample " + floatToString(mValue.x, kofxGui_Display_Int) + 
00101                                                 " value " + floatToString(mValue.y, kofxGui_Display_Float4), false);
00102         
00103         ofFill();
00104         
00105         //      background
00106         glColor4f(mGlobals->mCoverColor.r, mGlobals->mCoverColor.g, mGlobals->mCoverColor.b, mGlobals->mCoverColor.a);
00107         ofRect(mCtrX, mCtrY, mCtrWidth, mCtrHeight);
00108         
00109         if(mMouseIsDown)
00110         {
00111                 ofxPoint2f p = fractionToLocal(valueToFraction(mValue));
00112 
00113                 //      x-bar
00114                 glColor4f(mGlobals->mAxisColor.r, mGlobals->mAxisColor.g, mGlobals->mAxisColor.b, mGlobals-> mAxisColor.a);
00115                 ofLine(p.x + 0.5, mCtrY, p.x + 0.5, mCtrBottom);
00116                 
00117                 //      y-bar
00118                 glColor4f(mGlobals->mAxisColor.r, mGlobals->mAxisColor.g, mGlobals->mAxisColor.b, mGlobals->mAxisColor.a);
00119                 ofLine(mCtrX, p.y + 0.5, mCtrRight, p.y + 0.5);
00120         }
00121         
00122         ofNoFill();
00123 
00124         if(mBuffer && mBufferLength > 0)
00125         {
00126                 //      scope
00127                 glColor4f(mGlobals->mScopeColor.r, mGlobals->mScopeColor.g, mGlobals->mScopeColor.b, mGlobals->mScopeColor.a);
00128                 
00129                 float stretch           = mCtrWidth / (float)(mBufferLength - 1);
00130                 float zeroOffset        = mCtrHeight / 2.0;
00131                 
00132                 glPushMatrix(); 
00133                         glTranslatef(mCtrX, mCtrY + zeroOffset, 0.0);
00134                         ofBeginShape();
00135                         
00136                                 for (int i = 0; i < mBufferLength - 1; i++)
00137                                 {
00138                                         ofVertex(i * stretch, mBuffer[i] * zeroOffset);
00139                                         ofVertex((i + 1) * stretch, mBuffer[i + 1] * zeroOffset);
00140                                 }
00141                         
00142                         ofEndShape(false);
00143                 glPopMatrix();
00144         }
00145         
00146         //      frame
00147         glColor4f(mGlobals->mFrameColor.r, mGlobals->mFrameColor.g, mGlobals->mFrameColor.b, mGlobals->mFrameColor.a);
00148         ofRect(mCtrX, mCtrY, mCtrWidth, mCtrHeight);
00149         
00150         glPopMatrix();
00151 }
00152 
00153 //      ----------------------------------------------------------------------------------------------------
00154 
00155 bool ofxGuiScope::mouseDragged(int x, int y, int button)
00156 {
00157         if(mMouseIsDown)
00158         {
00159                 ofxPoint2f value = fractionToValue(mouseToFraction(mouseToLocal(x, y)));
00160                 
00161                 if(value != mValue)
00162                 {
00163                         setValue(value);
00164                         mGlobals->mListener->handleGui(mParamId, kofxGui_Set_Point, &mValue, sizeof(ofxPoint2f));
00165                 }
00166         }
00167 
00168         return mMouseIsDown;
00169 }
00170 
00171 //      ----------------------------------------------------------------------------------------------------
00172 
00173 bool ofxGuiScope::mousePressed(int x, int y, int button)
00174 {
00175         mMouseIsDown = isPointInsideMe(mouseToLocal(x, y));
00176                 
00177         if(mMouseIsDown)
00178                 mouseDragged(x, y, button);
00179 
00180         return mMouseIsDown;
00181 }
00182 
00183 //      ----------------------------------------------------------------------------------------------------
00184 
00185 bool ofxGuiScope::mouseReleased(int x, int y, int button)
00186 {
00187         bool handled = mMouseIsDown;
00188         
00189         if(mMouseIsDown)
00190                 mMouseIsDown = false;
00191         
00192         return handled;
00193 }
00194 
00195 //      ----------------------------------------------------------------------------------------------------
00196 
00197 void ofxGuiScope::buildFromXml()
00198 {
00199         //      load sampledata embeded within xml?
00200 
00201         mGlobals->mListener->handleGui(mParamId, kofxGui_Set_Point, &mValue, sizeof(ofxPoint2f));
00202 }
00203 
00204 //      ----------------------------------------------------------------------------------------------------
00205 
00206 void ofxGuiScope::saveToXml()
00207 {
00208         int id = saveObjectData();
00209 
00210         mGlobals->mXml.setValue("OBJECT:LENGTH", mBufferLength, id);
00211         mGlobals->mXml.setValue("OBJECT:VALUE_X", mValue.x, id);
00212         mGlobals->mXml.setValue("OBJECT:VALUE_Y", mValue.y, id);
00213 }
00214 
00215 //      ----------------------------------------------------------------------------------------------------
00216 
00217 ofxPoint2f ofxGuiScope::valueToFraction(ofxPoint2f value)
00218 {
00219         return ofxPoint2f(value.x / mBufferLength, value.y);
00220 }
00221 
00222 //      ----------------------------------------------------------------------------------------------------
00223 
00224 ofxPoint2f ofxGuiScope::fractionToValue(ofxPoint2f fraction)
00225 {
00226         return ofxPoint2f(fraction.x * mBufferLength, fraction.y);
00227 }
00228 
00229 //      ----------------------------------------------------------------------------------------------------
00230 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines