root / trunk / Linux / addons / ofxNCore / src / Controls / ofxGuiFiles.cpp @ 59

View | Annotate | Download (5.4 KB)

1
/*
2
 *  ofxGuiFiles.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 "ofxGuiFiles.h"
13
14
//        ----------------------------------------------------------------------------------------------------
15
16
ofxGuiFiles::ofxGuiFiles()
17
{
18
        mParamType = kofxGui_Object_Files;
19
}
20
21
//        ----------------------------------------------------------------------------------------------------
22
23
void ofxGuiFiles::init(int id, string name, int x, int y, int width, int height, string value, string path, string suffix)
24
{
25
        int        textHeight        = (name == "") ? 0 : mGlobals->mParamFontHeight;
26
        
27
        mParamId                = id;
28
        mParamName                = name;
29
        
30
        mObjX                        = x; 
31
        mObjY                        = y;
32
        
33
        mPath                        = path;
34
        mSuffix                        = suffix;
35
36
        int maxWidth        = getFileList();
37
        
38
        if(maxWidth > width)
39
                width = maxWidth;
40
        
41
        mObjWidth                = width;
42
        mObjHeight                = textHeight + height;
43
                        
44
        setValue(value);
45
        setControlRegion(0, textHeight, width, height);
46
}
47
48
//        ----------------------------------------------------------------------------------------------------
49
50
void ofxGuiFiles::setValue(string value)
51
{
52
        mSelected = 0;
53
        
54
        for(int i = 0; i < mFilelist.size(); i++)
55
        {
56
                string file = mFilelist.at(i);
57
                
58
                if(value == file)
59
                {
60
                        mSelected        = i;
61
                        mValue                = value;
62
                        break;
63
                }
64
        }
65
        
66
        if(mSelected == 0 && mFilelist.size() > 0)
67
                mValue = mFilelist.at(mSelected);
68
}
69
70
//        ----------------------------------------------------------------------------------------------------
71
72
bool ofxGuiFiles::update(int id, int task, void* data, int length)
73
{
74
        bool handled = false;
75
        
76
        if(id == mParamId)
77
        {
78
                if(task == kofxGui_Set_Bool)
79
                {
80
                        if((*(bool*)data))
81
                                getFileList();
82
83
                }
84
                else if(task == kofxGui_Set_String)
85
                {
86
                        setValue((*(string*)data));
87
                }
88
89
                handled = true;
90
        }
91
        
92
        return handled;
93
}
94
95
//        ----------------------------------------------------------------------------------------------------
96
97
void ofxGuiFiles::draw()
98
{
99
        glPushMatrix();
100
        
101
        glTranslatef(mObjX, mObjY, 0.0f);
102
        
103
                if(mParamName != "")
104
                        drawParamString(0.0, 0.0, mParamName + ": " + ofToString(mNumberOfFiles, 0) + "/" + ofToString(mSelected + 1, 0), false);
105
106
                ofFill();
107
                
108
                //        background
109
                glColor4f(mGlobals->mCoverColor.r, mGlobals->mCoverColor.g, mGlobals->mCoverColor.b, mGlobals->mCoverColor.a);
110
                ofRect(mCtrX, mCtrY, mCtrWidth, mCtrHeight);
111
112
                if(mValue != "")
113
                        drawParamString(mCtrX + mGlobals->mFilesXText, mCtrY + mGlobals->mFilesYText, mValue, false);        
114
115
                ofNoFill();
116
                
117
                //        frame
118
                glColor4f(mGlobals->mFrameColor.r, mGlobals->mFrameColor.g, mGlobals->mFrameColor.b, mGlobals->mFrameColor.a);
119
                ofRect(mCtrX, mCtrY, mCtrWidth, mCtrHeight);
120
        
121
        glPopMatrix();
122
}
123
124
//        ----------------------------------------------------------------------------------------------------
125
126
bool ofxGuiFiles::mouseDragged(int x, int y, int button)
127
{
128
        if(mMouseIsDown && mNumberOfFiles > 0)
129
        {
130
                mSelected        = roundInt(fractionToValue(mouseToFraction(mouseToLocal(x, y)).x));
131
                mValue                = mFilelist.at(mSelected);
132
        }
133
                
134
        return mMouseIsDown;
135
}
136
137
//        ----------------------------------------------------------------------------------------------------
138
139
bool ofxGuiFiles::mousePressed(int x, int y, int button)
140
{
141
        mMouseIsDown = isPointInsideMe(mouseToLocal(x, y));
142
        
143
        if(mMouseIsDown)
144
        {
145
                mMemory = mValue;
146
                mouseDragged(x, y, button);
147
        }
148
        
149
        return mMouseIsDown;
150
}
151
152
//        ----------------------------------------------------------------------------------------------------
153
154
bool ofxGuiFiles::mouseReleased(int x, int y, int button)
155
{
156
        bool handled = mMouseIsDown;
157
158
        if(mMouseIsDown)
159
        {
160
                if(isPointInsideMe(mouseToLocal(x, y)))
161
                   mGlobals->mListener->handleGui(mParamId, kofxGui_Set_String, &mValue, sizeof(string));
162
                else
163
                   mValue = mMemory;
164
                   
165
                mMouseIsDown = false;
166
        }
167
        
168
        return handled;
169
}
170
171
//        ----------------------------------------------------------------------------------------------------
172
173
void ofxGuiFiles::buildFromXml()
174
{
175
        mGlobals->mListener->handleGui(mParamId, kofxGui_Set_String, &mValue, sizeof(string));
176
}
177
178
//        ----------------------------------------------------------------------------------------------------
179
180
void ofxGuiFiles::saveToXml()
181
{
182
        int id = saveObjectData();
183
        
184
        mGlobals->mXml.setValue("OBJECT:VALUE", mValue, id);
185
        mGlobals->mXml.setValue("OBJECT:SUBPATH", mPath, id);
186
        mGlobals->mXml.setValue("OBJECT:SUFFIX", mSuffix, id);
187
}
188
189
//        ----------------------------------------------------------------------------------------------------
190
191
float ofxGuiFiles::valueToFraction(float value)
192
{
193
        return value / (mNumberOfFiles - 1);
194
}
195
196
//        ----------------------------------------------------------------------------------------------------
197
198
float ofxGuiFiles::fractionToValue(float fraction)
199
{
200
        return fraction * (mNumberOfFiles - 1);
201
}
202
203
//        ----------------------------------------------------------------------------------------------------
204
205
int ofxGuiFiles::getFileList()
206
{        
207
        mFilelist.clear();
208
        
209
        mGlobals->mDir.reset();
210
        mGlobals->mDir.allowExt(mSuffix);
211
        
212
        mNumberOfFiles        = mGlobals->mDir.listDir(mPath);
213
        int maxWidth        = 0;
214
        
215
        for(int i = 0; i < mNumberOfFiles; i++)
216
        {
217
                string file = mGlobals->mDir.getName(i);
218
                mFilelist.push_back(file);
219
                
220
                int width = roundInt(mGlobals->mParamFont.stringWidth(file));
221
                
222
                if(width > maxWidth)
223
                        maxWidth = width;
224
        }
225
        
226
        return maxWidth;
227
}
228
229
//        ----------------------------------------------------------------------------------------------------