Community Core Vision 1.4
Cross platform, user friendly computer vision.
C:/CCV/1.4/addons/ofxNCore/src/Controls/ofxGuiMatrix.cpp
Go to the documentation of this file.
00001 /*
00002  *  ofxGuiMatrix.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 "ofxGuiMatrix.h"
00013 
00014 //      ----------------------------------------------------------------------------------------------------
00015 
00016 ofxGuiMatrix::ofxGuiMatrix()
00017 {
00018         mParamType              = kofxGui_Object_Matrix;
00019         
00020         mBuffer                 = NULL;
00021         mBufferLength   = 0;
00022 }
00023 
00024 //      ----------------------------------------------------------------------------------------------------
00025 
00026 ofxGuiMatrix::~ofxGuiMatrix()
00027 {
00028         if(mBuffer)
00029                 delete[] mBuffer;
00030 }
00031 
00032 //      ----------------------------------------------------------------------------------------------------
00033 
00034 void ofxGuiMatrix::init(int id, string name, int x, int y, int width, int height, int xGrid, int yGrid, int value, int display, int spacing)
00035 {
00036         int     textHeight      = (name == "") ? 0 : mGlobals->mParamFontHeight;
00037         
00038         mParamId                = id;
00039         mParamName              = name;
00040         
00041         mObjX                   = x; 
00042         mObjY                   = y;
00043         
00044         mObjWidth               = width;
00045         mObjHeight              = textHeight + height;
00046         
00047         mXGrid                  = xGrid;
00048         mYGrid                  = yGrid;
00049         
00050         mDisplay                        = display;
00051         mSpacing                = spacing;
00052         
00053         setValue(value);
00054         setControlRegion(0, textHeight, width, height);
00055 
00056         if(mBuffer)
00057                 delete[] mBuffer;
00058         
00059         mBufferLength   = xGrid * yGrid;
00060         mBuffer                 = new int[mBufferLength];
00061         
00062         memset(mBuffer, 0, sizeof(int) * mBufferLength);
00063 
00064         mGlobals->mListener->handleGui(mParamId, kofxGui_Set_IntArray, mBuffer, mBufferLength);
00065 }
00066 
00067 //      ----------------------------------------------------------------------------------------------------
00068 
00069 void ofxGuiMatrix::setValue(int value)
00070 {
00071         mValue = value;
00072 }
00073 
00074 //      ----------------------------------------------------------------------------------------------------
00075 
00076 bool ofxGuiMatrix::update(int id, int task, void* data, int length)
00077 {
00078         bool    handled = false;
00079         int             value   = mValue;
00080         
00081         if(id == mParamId)
00082         {       
00083                 if(task == kofxGui_Set_Float)
00084                         setValue(CLAMP(*(float*)data, 0.0f, 1.0f) * mBufferLength - 1);
00085                 else if(task == kofxGui_Set_Int)
00086                         setValue(CLAMP(*(int*)data, 0, mBufferLength - 1));
00087                 
00088                 handled = true;
00089         }
00090         
00091         if(value != mValue)
00092         {
00093                 ofCell outVal = ofCell(mValue, mBuffer[mValue]);
00094                 mGlobals->mListener->handleGui(mParamId, kofxGui_Set_Cell, &outVal, sizeof(ofCell));
00095         }
00096         
00097         return handled;
00098 }
00099 
00100 //      ----------------------------------------------------------------------------------------------------
00101 
00102 void ofxGuiMatrix::draw()
00103 {
00104         glPushMatrix();
00105         
00106         glTranslatef(mObjX, mObjY, 0.0f);
00107         
00108         if(mParamName != "")
00109                 drawParamString(0.0, 0.0, mParamName + ": " + ofToString(mValue), false);
00110         
00111         ofFill();
00112         
00113         //      background
00114         glColor4f(mGlobals->mCoverColor.r, mGlobals->mCoverColor.g, mGlobals->mCoverColor.b, mGlobals->mCoverColor.a);
00115         ofRect(mCtrX, mCtrY, mCtrWidth, mCtrHeight);
00116         
00117         ofNoFill();
00118 
00119         //      pads
00120         glColor4f(mGlobals->mFrameColor.r, mGlobals->mFrameColor.g, mGlobals->mFrameColor.b, mGlobals->mFrameColor.a);
00121         
00122         int             s       = mSpacing / 2;
00123         float   x       = mCtrX + s;
00124         float   y       = mCtrY + s;
00125         float   w       = (mCtrWidth - mSpacing) / mXGrid;
00126         float   h       = (mCtrHeight - mSpacing) / mYGrid;
00127         
00128         for(int i = 0; i < mYGrid; i++)
00129         {
00130                 for(int j = 0; j < mXGrid; j++)
00131                 {
00132                         int index = j + (i * mXGrid);
00133                         
00134                         if((mBuffer[index] & kofxGui_Matrix_Set) == kofxGui_Matrix_Set)
00135                         {
00136                                 ofFill();
00137                                 glColor4f(mGlobals->mButtonColor.r, mGlobals->mButtonColor.g, mGlobals->mButtonColor.b, mGlobals->mButtonColor.a);
00138                                 ofRect(x + s, y + s, w - mSpacing, h - mSpacing);
00139                         }
00140                         
00141                         if((mBuffer[index] & kofxGui_Matrix_Selected) == kofxGui_Matrix_Selected)
00142                         {
00143                                 ofNoFill();
00144                                 glColor4f(mGlobals->mMatrixColor.r, mGlobals->mMatrixColor.g, mGlobals->mMatrixColor.b, mGlobals->mMatrixColor.a);
00145                                 ofRect(x + s, y + s, w - mSpacing, h - mSpacing);
00146                         }
00147                         else if(mValue == index)
00148                         {
00149                                 ofNoFill();
00150                                 glColor4f(mGlobals->mAxisColor.r, mGlobals->mAxisColor.g, mGlobals->mAxisColor.b, mGlobals->mAxisColor.a);
00151                                 ofRect(x + s, y + s, w - mSpacing, h - mSpacing);
00152                         }
00153                         else
00154                         {
00155                                 ofNoFill();
00156                                 glColor4f(mGlobals->mFrameColor.r, mGlobals->mFrameColor.g, mGlobals->mFrameColor.b, mGlobals->mFrameColor.a);
00157                                 ofRect(x + s, y + s, w - mSpacing, h - mSpacing);
00158                         }
00159                         
00160                         x += w;
00161                 }
00162                 
00163                 y += h;
00164                 x  = mCtrX + s;
00165         }
00166                 
00167         //      frame
00168         glColor4f(mGlobals->mFrameColor.r, mGlobals->mFrameColor.g, mGlobals->mFrameColor.b, mGlobals->mFrameColor.a);
00169         ofRect(mCtrX, mCtrY, mCtrWidth, mCtrHeight);
00170         
00171         glPopMatrix();
00172 }
00173 
00174 //      ----------------------------------------------------------------------------------------------------
00175 
00176 bool ofxGuiMatrix::mouseDragged(int x, int y, int button)
00177 {
00178         return mMouseIsDown;
00179 }
00180 
00181 //      ----------------------------------------------------------------------------------------------------
00182 
00183 bool ofxGuiMatrix::mousePressed(int x, int y, int button)
00184 {
00185         ofxPoint2f inside = mouseToLocal(x, y);
00186         mMouseIsDown = isPointInsideMe(inside);
00187         
00188         if(mMouseIsDown)
00189         {
00190                 int id = mouseToPadId(mouseToFraction(inside));
00191                 
00192                 if (mDisplay == kofxGui_Button_Trigger)
00193                         mBuffer[id]     |= kofxGui_Matrix_Set;  // = 1;
00194                 else
00195                         mBuffer[id]     ^= kofxGui_Matrix_Set;  // !mBuffer[id];
00196                 
00197                 setValue(id);
00198                 
00199                 ofCell outVal = ofCell(mValue, mBuffer[mValue]);
00200                 mGlobals->mListener->handleGui(mParamId, kofxGui_Set_Cell, &outVal, sizeof(ofCell));
00201         }
00202         
00203         return mMouseIsDown;
00204 }
00205 
00206 //      ----------------------------------------------------------------------------------------------------
00207 
00208 bool ofxGuiMatrix::mouseReleased(int x, int y, int button)
00209 {
00210         bool handled = mMouseIsDown;
00211         
00212         if(mMouseIsDown)
00213         {
00214                 if(mDisplay == kofxGui_Button_Trigger)
00215                 {
00216                         mBuffer[mValue] ^= kofxGui_Matrix_Set;  //      = 0;
00217                         mGlobals->mListener->handleGui(mParamId, kofxGui_Set_Int, &mValue, sizeof(int));
00218                 }
00219                 
00220                 mMouseIsDown = false;
00221         }
00222         
00223         return handled;
00224 }
00225 
00226 //      ----------------------------------------------------------------------------------------------------
00227 
00228 void ofxGuiMatrix::buildFromXml()
00229 {
00230         int numberOfTags = mGlobals->mXml.getNumTags("PAD");
00231         
00232         if(numberOfTags > 0)
00233         {
00234                 memset(mBuffer, 0, sizeof(int) * mBufferLength);
00235                 
00236                 for(int i = 0; i < numberOfTags; i++)
00237                 {
00238                         mGlobals->mXml.pushTag("PAD", i);
00239                         
00240                         int     j       = mGlobals->mXml.getValue("INDEX", 0);
00241                         int     v       = mGlobals->mXml.getValue("VALUE", 0);
00242                         
00243                         if(j < mBufferLength)
00244                            mBuffer[j] = v;
00245                         
00246                         mGlobals->mXml.popTag();
00247                 }
00248         }
00249 
00250         mGlobals->mListener->handleGui(mParamId, kofxGui_Set_Int, &mValue, sizeof(int));
00251 }
00252 
00253 //      ----------------------------------------------------------------------------------------------------
00254 
00255 void ofxGuiMatrix::saveToXml()
00256 {
00257         int id = saveObjectData();
00258         
00259         mGlobals->mXml.setValue("OBJECT:XGRID", mXGrid, id);
00260         mGlobals->mXml.setValue("OBJECT:YGRID", mYGrid, id);
00261         mGlobals->mXml.setValue("OBJECT:VALUE", mValue, id);
00262         mGlobals->mXml.setValue("OBJECT:SPACING", mSpacing, id);
00263 
00264         mGlobals->mXml.pushTag("OBJECT", id);
00265         
00266         for(int i = 0; i < mBufferLength; i++)
00267         {
00268                 int v = mBuffer[i];
00269                 
00270                 if(v == 0)
00271                         continue;
00272                 
00273                 id = mGlobals->mXml.addTag("PAD");
00274                 
00275                 mGlobals->mXml.setValue("PAD:INDEX", i, id);
00276                 mGlobals->mXml.setValue("PAD:VALUE", v, id);
00277         }
00278         
00279         mGlobals->mXml.popTag();
00280 }
00281 
00282 //      ----------------------------------------------------------------------------------------------------
00283 
00284 int ofxGuiMatrix::mouseToPadId(ofxPoint2f point)
00285 {
00286         float x = (int)(point.x * mXGrid);
00287         float y = (int)(point.y * mYGrid);
00288 
00289         return x + (y * mXGrid); 
00290 }
00291 
00292 //      ----------------------------------------------------------------------------------------------------
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines