00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <GLW/GLWImageList.h>
00022 #include <GLW/GLWTranslate.h>
00023 #include <image/ImageFactory.h>
00024 #include <GLEXT/GLState.h>
00025 #include <common/FileList.h>
00026 #include <string.h>
00027
00028 REGISTER_CLASS_SOURCE(GLWImageList);
00029
00030 GLWImageList::GLWImageList(float x, float y) :
00031 GLWidget(x, y, 32.0f, 32.0f), current_(0), enabled_(true)
00032 {
00033 }
00034
00035 GLWImageList::~GLWImageList()
00036 {
00037 while (!entries_.empty())
00038 {
00039 GLWImageListEntry* entry = entries_.front();
00040 entries_.pop_front();
00041 delete entry;
00042 }
00043 }
00044
00045 void GLWImageList::addDirectory(const std::string &directory)
00046 {
00047 if (directory.empty()) return;
00048
00049 FileList filelist(directory, "*.png");
00050 std::list<std::string>::iterator itor;
00051 for (itor = filelist.getFiles().begin();
00052 itor != filelist.getFiles().end();
00053 itor++)
00054 {
00055 const char *filename = (*itor).c_str();
00056 ImageHandle png = ImageFactory::loadImageHandle(filename);
00057 if (png.getBits() &&
00058 png.getWidth() == 32 &&
00059 png.getHeight() == 32)
00060 {
00061 GLWImageListEntry *entry = new GLWImageListEntry;
00062 if (entry->texture.create(png))
00063 {
00064 entry->longFileName = filename;
00065 const char *sep = strrchr(filename, '/');
00066 if (sep) sep++;
00067 else sep = filename;
00068 entry->shortFileName = sep;
00069 entries_.push_back(entry);
00070 }
00071 else delete entry;
00072 }
00073 }
00074
00075 setCurrentShortPath("");
00076 }
00077
00078 void GLWImageList::draw()
00079 {
00080 GLWidget::draw();
00081
00082 if (!current_) return;
00083 current_->texture.draw();
00084
00085 glTexParameteri(GL_TEXTURE_2D,
00086 GL_TEXTURE_WRAP_S, GL_CLAMP);
00087 glTexParameteri(GL_TEXTURE_2D,
00088 GL_TEXTURE_WRAP_T, GL_CLAMP);
00089
00090 GLState currentStateOff(GLState::TEXTURE_OFF);
00091 glColor3f(0.4f, 0.4f, 0.6f);
00092 glBegin(GL_LINE_LOOP);
00093 glVertex2f(x_ - 4.0f, y_ - 3.0f);
00094 glVertex2f(x_ + w_ + 3.0f, y_ - 3.0f);
00095 glVertex2f(x_ + w_ + 3.0f, y_ + h_ + 3.0f);
00096 glVertex2f(x_ - 4.0f, y_ + h_ + 3.0f);
00097 glEnd();
00098
00099 GLState currentStateOn(GLState::TEXTURE_ON);
00100 glColor3f(1.0f, 1.0f, 1.0f);
00101 glBegin(GL_QUADS);
00102 glTexCoord2f(0.0f, 0.0f);
00103 glVertex2f(x_, y_);
00104 glTexCoord2f(1.0f, 0.0f);
00105 glVertex2f(x_ + w_, y_);
00106 glTexCoord2f(1.0f, 1.0f);
00107 glVertex2f(x_ + w_, y_ + h_);
00108 glTexCoord2f(0.0f, 1.0f);
00109 glVertex2f(x_, y_ + h_);
00110 glEnd();
00111 }
00112
00113 bool GLWImageList::setCurrentShortPath(const char *current)
00114 {
00115 std::list<GLWImageListEntry*>::iterator itor;
00116 for (itor = entries_.begin();
00117 itor != entries_.end();
00118 itor++)
00119 {
00120 GLWImageListEntry *entry = (*itor);
00121 if (0 == strcmp(entry->shortFileName.c_str(), current))
00122 {
00123 current_ = entry;
00124 return true;
00125 }
00126 }
00127
00128 if (!current_ && !entries_.empty())
00129 {
00130 current_ = entries_.front();
00131 }
00132 return false;
00133 }
00134
00135 const char *GLWImageList::getCurrentLongPath()
00136 {
00137 if (current_) return current_->longFileName.c_str();
00138 return "";
00139 }
00140
00141 const char *GLWImageList::getCurrentShortPath()
00142 {
00143 if (current_) return current_->shortFileName.c_str();
00144 return "";
00145 }
00146
00147 void GLWImageList::simulate(float frameTime)
00148 {
00149 GLWidget::simulate(frameTime);
00150 }
00151
00152 void GLWImageList::mouseDown(int button, float x, float y,
00153 bool &skipRest)
00154 {
00155 GLWidget::mouseDown(button, x, y, skipRest);
00156
00157 if (inBox(x, y, x_, y_, w_, h_))
00158 {
00159 skipRest = true;
00160
00161 if (enabled_)
00162 {
00163 std::list<GLWSelectorEntry> entries;
00164 std::list<GLWImageListEntry*>::iterator itor;
00165 for (itor = entries_.begin();
00166 itor != entries_.end();
00167 itor++)
00168 {
00169 GLWImageListEntry *entry = (*itor);
00170 entries.push_back(
00171 GLWSelectorEntry(LANG_STRING(entry->shortFileName.c_str()),
00172 0, 0, &entry->texture, 0, entry->shortFileName));
00173 }
00174
00175 GLWSelector::instance()->showSelector(
00176 this,
00177 GLWTranslate::getPosX() + x,
00178 GLWTranslate::getPosY() + y,
00179 entries, 0, false);
00180 }
00181 }
00182 }
00183
00184 void GLWImageList::mouseUp(int button, float x, float y,
00185 bool &skipRest)
00186 {
00187 GLWidget::mouseUp(button, x, y, skipRest);
00188 }
00189
00190 void GLWImageList::mouseDrag(int button, float mx, float my,
00191 float x, float y, bool &skipRest)
00192 {
00193 GLWidget::mouseDrag(button, mx, my, x, y, skipRest);
00194 }
00195
00196 void GLWImageList::itemSelected(GLWSelectorEntry *entry, int position)
00197 {
00198 setCurrentShortPath(entry->getDataText());
00199 }
00200