Community Core Vision 1.4
Cross platform, user friendly computer vision.
C:/CCV/1.4/addons/ofxNCore/src/Controls/ofxGuiFiles.cpp
Go to the documentation of this file.
00001 /*
00002  *  ofxGuiFiles.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 "ofxGuiFiles.h"
00013 
00014 //      ----------------------------------------------------------------------------------------------------
00015 
00016 ofxGuiFiles::ofxGuiFiles()
00017 {
00018         mParamType = kofxGui_Object_Files;
00019 }
00020 
00021 //      ----------------------------------------------------------------------------------------------------
00022 
00023 void ofxGuiFiles::init(int id, string name, int x, int y, int width, int height, string value, string path, string suffix)
00024 {
00025         int     textHeight      = (name == "") ? 0 : mGlobals->mParamFontHeight;
00026         
00027         mParamId                = id;
00028         mParamName              = name;
00029         
00030         mObjX                   = x; 
00031         mObjY                   = y;
00032         
00033         mPath                   = path;
00034         mSuffix                 = suffix;
00035 
00036         int maxWidth    = getFileList();
00037         
00038         if(maxWidth > width)
00039                 width = maxWidth;
00040         
00041         mObjWidth               = width;
00042         mObjHeight              = textHeight + height;
00043                         
00044         setValue(value);
00045         setControlRegion(0, textHeight, width, height);
00046 }
00047 
00048 //      ----------------------------------------------------------------------------------------------------
00049 
00050 void ofxGuiFiles::setValue(string value)
00051 {
00052         mSelected = 0;
00053         
00054         for(int i = 0; i < mFilelist.size(); i++)
00055         {
00056                 string file = mFilelist.at(i);
00057                 
00058                 if(value == file)
00059                 {
00060                         mSelected       = i;
00061                         mValue          = value;
00062                         break;
00063                 }
00064         }
00065         
00066         if(mSelected == 0 && mFilelist.size() > 0)
00067                 mValue = mFilelist.at(mSelected);
00068 }
00069 
00070 //      ----------------------------------------------------------------------------------------------------
00071 
00072 bool ofxGuiFiles::update(int id, int task, void* data, int length)
00073 {
00074         bool handled = false;
00075         
00076         if(id == mParamId)
00077         {
00078                 if(task == kofxGui_Set_Bool)
00079                 {
00080                         if((*(bool*)data))
00081                                 getFileList();
00082 
00083                 }
00084                 else if(task == kofxGui_Set_String)
00085                 {
00086                         setValue((*(string*)data));
00087                 }
00088 
00089                 handled = true;
00090         }
00091         
00092         return handled;
00093 }
00094 
00095 //      ----------------------------------------------------------------------------------------------------
00096 
00097 void ofxGuiFiles::draw()
00098 {
00099         glPushMatrix();
00100         
00101         glTranslatef(mObjX, mObjY, 0.0f);
00102         
00103                 if(mParamName != "")
00104                         drawParamString(0.0, 0.0, mParamName + ": " + ofToString(mNumberOfFiles, 0) + "/" + ofToString(mSelected + 1, 0), false);
00105 
00106                 ofFill();
00107                 
00108                 //      background
00109                 glColor4f(mGlobals->mCoverColor.r, mGlobals->mCoverColor.g, mGlobals->mCoverColor.b, mGlobals->mCoverColor.a);
00110                 ofRect(mCtrX, mCtrY, mCtrWidth, mCtrHeight);
00111 
00112                 if(mValue != "")
00113                         drawParamString(mCtrX + mGlobals->mFilesXText, mCtrY + mGlobals->mFilesYText, mValue, false);   
00114 
00115                 ofNoFill();
00116                 
00117                 //      frame
00118                 glColor4f(mGlobals->mFrameColor.r, mGlobals->mFrameColor.g, mGlobals->mFrameColor.b, mGlobals->mFrameColor.a);
00119                 ofRect(mCtrX, mCtrY, mCtrWidth, mCtrHeight);
00120         
00121         glPopMatrix();
00122 }
00123 
00124 //      ----------------------------------------------------------------------------------------------------
00125 
00126 bool ofxGuiFiles::mouseDragged(int x, int y, int button)
00127 {
00128         if(mMouseIsDown && mNumberOfFiles > 0)
00129         {
00130                 mSelected       = roundInt(fractionToValue(mouseToFraction(mouseToLocal(x, y)).x));
00131                 mValue          = mFilelist.at(mSelected);
00132         }
00133                 
00134         return mMouseIsDown;
00135 }
00136 
00137 //      ----------------------------------------------------------------------------------------------------
00138 
00139 bool ofxGuiFiles::mousePressed(int x, int y, int button)
00140 {
00141         mMouseIsDown = isPointInsideMe(mouseToLocal(x, y));
00142         
00143         if(mMouseIsDown)
00144         {
00145                 mMemory = mValue;
00146                 mouseDragged(x, y, button);
00147         }
00148         
00149         return mMouseIsDown;
00150 }
00151 
00152 //      ----------------------------------------------------------------------------------------------------
00153 
00154 bool ofxGuiFiles::mouseReleased(int x, int y, int button)
00155 {
00156         bool handled = mMouseIsDown;
00157 
00158         if(mMouseIsDown)
00159         {
00160                 if(isPointInsideMe(mouseToLocal(x, y)))
00161                    mGlobals->mListener->handleGui(mParamId, kofxGui_Set_String, &mValue, sizeof(string));
00162                 else
00163                    mValue = mMemory;
00164                    
00165                 mMouseIsDown = false;
00166         }
00167         
00168         return handled;
00169 }
00170 
00171 //      ----------------------------------------------------------------------------------------------------
00172 
00173 void ofxGuiFiles::buildFromXml()
00174 {
00175         mGlobals->mListener->handleGui(mParamId, kofxGui_Set_String, &mValue, sizeof(string));
00176 }
00177 
00178 //      ----------------------------------------------------------------------------------------------------
00179 
00180 void ofxGuiFiles::saveToXml()
00181 {
00182         int id = saveObjectData();
00183         
00184         mGlobals->mXml.setValue("OBJECT:VALUE", mValue, id);
00185         mGlobals->mXml.setValue("OBJECT:SUBPATH", mPath, id);
00186         mGlobals->mXml.setValue("OBJECT:SUFFIX", mSuffix, id);
00187 }
00188 
00189 //      ----------------------------------------------------------------------------------------------------
00190 
00191 float ofxGuiFiles::valueToFraction(float value)
00192 {
00193         return value / (mNumberOfFiles - 1);
00194 }
00195 
00196 //      ----------------------------------------------------------------------------------------------------
00197 
00198 float ofxGuiFiles::fractionToValue(float fraction)
00199 {
00200         return fraction * (mNumberOfFiles - 1);
00201 }
00202 
00203 //      ----------------------------------------------------------------------------------------------------
00204 
00205 int ofxGuiFiles::getFileList()
00206 {       
00207         mFilelist.clear();
00208         
00209         mGlobals->mDir.reset();
00210         mGlobals->mDir.allowExt(mSuffix);
00211         
00212         mNumberOfFiles  = mGlobals->mDir.listDir(mPath);
00213         int maxWidth    = 0;
00214         
00215         for(int i = 0; i < mNumberOfFiles; i++)
00216         {
00217                 string file = mGlobals->mDir.getName(i);
00218                 mFilelist.push_back(file);
00219                 
00220                 int width = roundInt(mGlobals->mParamFont.stringWidth(file));
00221                 
00222                 if(width > maxWidth)
00223                         maxWidth = width;
00224         }
00225         
00226         return maxWidth;
00227 }
00228 
00229 //      ----------------------------------------------------------------------------------------------------
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines