Created
May 21, 2013 08:30
-
-
Save satoruhiga/5618332 to your computer and use it in GitHub Desktop.
Tile layouted image preview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
class PixelPreview | |
{ | |
struct Panel | |
{ | |
typedef ofPtr<Panel> Ref; | |
string name; | |
ofTexture tex; | |
}; | |
public: | |
template <typename T> | |
void setImage(const string& name, ofPixels_<T> &pix) | |
{ | |
Panel::Ref img; | |
map<string, Panel::Ref>::iterator it = images.find(name); | |
if (it == images.end()) | |
{ | |
names.push_back(name); | |
img = Panel::Ref(new Panel); | |
img->name = name; | |
images[name] = img; | |
img->tex.allocate(pix); | |
} | |
else | |
{ | |
img = it->second; | |
} | |
img->tex.loadData(pix); | |
} | |
template <typename T> | |
void setImage(const string& name, ofImage_<T> &pix) | |
{ | |
setImage(name, pix.getPixelsRef()); | |
} | |
void clear() | |
{ | |
images.clear(); | |
names.clear(); | |
} | |
void draw(int panel_width, int panel_height) | |
{ | |
glPushAttrib(GL_ALL_ATTRIB_BITS); | |
ofPushStyle(); | |
ofSetRectMode(OF_RECTMODE_CORNER); | |
ofNoFill(); | |
ofEnableAlphaBlending(); | |
glDisable(GL_DEPTH_TEST); | |
int offsetX = 0; | |
int offsetY = 0; | |
for (int i = 0; i < names.size(); i++) | |
{ | |
Panel::Ref o = images[names[i]]; | |
glPushMatrix(); | |
glTranslatef(offsetX, offsetY, 0); | |
ofSetColor(ofColor::white); | |
o->tex.draw(0, 0, panel_width, panel_height); | |
ofSetColor(ofColor::yellow); | |
ofRect(0, 0, panel_width, panel_height); | |
ofDrawBitmapStringHighlight(names[i], 5, 16, ofColor::yellow, ofColor::black); | |
glPopMatrix(); | |
if (offsetX + panel_width * 2 > ofGetWidth()) | |
{ | |
offsetX = 0; | |
offsetY += panel_height; | |
} | |
else | |
{ | |
offsetX += panel_width; | |
} | |
} | |
ofPopStyle(); | |
glPopAttrib(); | |
} | |
protected: | |
vector<string> names; | |
map<string, Panel::Ref> images; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment