Skip to content

Commit

Permalink
make the code noticably less stinky
Browse files Browse the repository at this point in the history
  • Loading branch information
counter185 committed Sep 16, 2024
1 parent c8449b9 commit da09536
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 224 deletions.
26 changes: 13 additions & 13 deletions freesprite/EditorColorPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ class EditorColorPicker : public Drawable, public EventCallbackListener

uint8_t currentR = 0, currentG = 0, currentB = 0;

UIHueSlider* hueSlider;
UISVPicker* satValSlider;
UITextField* colorTextField;
UISlider* sliderH;
UIHueSlider* hueSlider = NULL;
UISVPicker* satValSlider = NULL;
UITextField* colorTextField = NULL;
UISlider* sliderH = NULL;

UITextField* txtR, *txtG, *txtB;
UITextField* txtH, *txtS, *txtV;

UIColorSlider* sliderS;
UIColorSlider* sliderV;
UIColorSlider* sliderR;
UIColorSlider* sliderG;
UIColorSlider* sliderB;
UIColorSlider* sliderS = NULL;
UIColorSlider* sliderV = NULL;
UIColorSlider* sliderR = NULL;
UIColorSlider* sliderG = NULL;
UIColorSlider* sliderB = NULL;

TabbedView* colorModeTabs;
TabbedView* colorTabs;
TabbedView* colorModeTabs = NULL;
TabbedView* colorTabs = NULL;

UIButton* eraserButton;
UIButton* blendModeButton;
UIButton* eraserButton = NULL;
UIButton* blendModeButton = NULL;

std::vector<uint32_t> lastColors;
bool lastColorsChanged = true;
Expand Down
113 changes: 76 additions & 37 deletions freesprite/FileIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,48 +104,87 @@ struct FileExportFlatNPath {
};
};

inline std::vector<FileExportFlatNPath> g_fileExportersFlatNPaths = {
{
"PNG (libpng)", ".png", &writePNG, FORMAT_RGB | FORMAT_PALETTIZED
},
{
"RPG2000/2003 XYZ (voidsprite custom)", ".xyz", &writeXYZ, FORMAT_RGB | FORMAT_PALETTIZED
},
{
"BMP (EasyBMP)", ".bmp", &writeBMP
},
{
"TGA (voidsprite custom)", ".tga", &writeTGA
},
{
"CaveStory PBM (EasyBMP)", ".pbm", &writeCaveStoryPBM
},
{
"C Header (voidsprite custom)", ".h", &writeCHeader
},
{
"Python NumPy array (voidsprite custom)", ".py", &writePythonNPArray
},
{
"HTML Base64 image (base64)", ".html", &writeHTMLBase64
},
{
"Java Buffered Image (voidsprite custom)", ".java", &writeJavaBufferedImage
class FileExporter {

public:
static FileExporter* sessionExporter(std::string name, std::string extension, bool (*exportFunction)(PlatformNativePathString, MainEditor*), int formatflags = FORMAT_RGB, bool (*canExport)(MainEditor*) = NULL) {
FileExporter* ret = new FileExporter();
ret->_name = name;
ret->_extension = extension;
ret->_formatFlags = formatflags;
ret->_isSessionExporter = true;
ret->_sessionExportFunction = exportFunction;
ret->_sessionCheckExportFunction = canExport;
return ret;
}
};
inline std::vector<FileExportMultiLayerNPath> g_fileExportersMLNPaths = {
{
"voidsprite Session version 3", ".voidsn", &writeVOIDSNv3
},
{
"voidsprite Session version 2", ".voidsnv2", &writeVOIDSNv2
},
{
"OpenRaster", ".ora", &writeOpenRaster
static FileExporter* flatExporter(std::string name, std::string extension, bool (*exportFunction)(PlatformNativePathString, Layer*), int formatflags = FORMAT_RGB, bool (*canExport)(Layer*) = NULL) {
FileExporter* ret = new FileExporter();
ret->_name = name;
ret->_extension = extension;
ret->_formatFlags = formatflags;
ret->_isSessionExporter = false;
ret->_flatExportFunction = exportFunction;
ret->_flatCheckExportFunction = canExport;
return ret;
}

virtual int formatFlags() { return _formatFlags; }
virtual std::string name() { return _name; }
virtual std::string extension() { return _extension; }
virtual bool exportsWholeSession() { return _isSessionExporter; }
virtual bool canExport(void* data) {
if (exportsWholeSession()) {
return _sessionCheckExportFunction != NULL ? _sessionCheckExportFunction((MainEditor*)data) : true;
}
else {
return _flatCheckExportFunction != NULL ? _flatCheckExportFunction((Layer*)data) : true;
}
}
virtual bool exportData(PlatformNativePathString path, void* data) {
if (exportsWholeSession()) {
return _sessionExportFunction(path, (MainEditor*)data);
}
else {
return _flatExportFunction(path, (Layer*)data);
}
}
protected:
std::string _name = "File type (library)";
std::string _extension = "";
int _formatFlags = FORMAT_RGB;
bool _isSessionExporter = false;

bool (*_sessionExportFunction)(PlatformNativePathString, MainEditor*) = NULL;
bool (*_sessionCheckExportFunction)(MainEditor*) = NULL;

bool (*_flatExportFunction)(PlatformNativePathString, Layer*) = NULL;
bool (*_flatCheckExportFunction)(Layer*) = NULL;
};

inline std::vector<FileExporter*> g_fileExporters;
inline std::vector<FileExporter*> g_palettizedFileExporters;

inline void g_setupIO() {
g_fileExporters.push_back(FileExporter::sessionExporter("voidsprite Session version 3", ".voidsn", &writeVOIDSNv3));
g_fileExporters.push_back(FileExporter::sessionExporter("voidsprite Session version 2", ".voidsnv2", &writeVOIDSNv2));
g_fileExporters.push_back(FileExporter::sessionExporter("OpenRaster", ".ora", &writeOpenRaster));

g_fileExporters.push_back(FileExporter::flatExporter("PNG (libpng)", ".png", &writePNG, FORMAT_RGB | FORMAT_PALETTIZED));
g_fileExporters.push_back(FileExporter::flatExporter("RPG2000/2003 XYZ (voidsprite custom)", ".xyz", &writeXYZ, FORMAT_RGB | FORMAT_PALETTIZED));
g_fileExporters.push_back(FileExporter::flatExporter("BMP (EasyBMP)", ".bmp", &writeBMP));
g_fileExporters.push_back(FileExporter::flatExporter("TGA (voidsprite custom)", ".tga", &writeTGA));
g_fileExporters.push_back(FileExporter::flatExporter("CaveStory PBM (EasyBMP)", ".pbm", &writeCaveStoryPBM));
g_fileExporters.push_back(FileExporter::flatExporter("C Header (voidsprite custom)", ".h", &writeCHeader));
g_fileExporters.push_back(FileExporter::flatExporter("Python NumPy array (voidsprite custom)", ".py", &writePythonNPArray));
g_fileExporters.push_back(FileExporter::flatExporter("HTML Base64 image (base64)", ".html", &writeHTMLBase64));
g_fileExporters.push_back(FileExporter::flatExporter("Java Buffered Image (voidsprite custom)", ".java", &writeJavaBufferedImage));

for (auto& exporter : g_fileExporters) {
if (exporter->formatFlags() & FORMAT_PALETTIZED) {
g_palettizedFileExporters.push_back(exporter);
}
}
}

inline std::vector<FileSessionImportNPath> g_fileSessionImportersNPaths = {
{
Expand Down
55 changes: 14 additions & 41 deletions freesprite/MainEditorPalettized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,37 +67,21 @@ MainEditorPalettized::MainEditorPalettized(std::vector<LayerPalettized*> layers)
void MainEditorPalettized::eventFileSaved(int evt_id, PlatformNativePathString name, int exporterId)
{
if (evt_id == EVENT_PALETTIZEDEDITOR_SAVEFILE) {
//todo clean this shit up
std::vector<int> palettizedExporterIds;
std::vector<std::pair<std::string, std::string>> namesAndExtensions;
int cexporterID = 0;
for (auto& e : g_fileExportersMLNPaths) {
if ((e.exportFormats & FORMAT_PALETTIZED) != 0) {
palettizedExporterIds.push_back(cexporterID);
}
cexporterID++;
}
for (auto& e : g_fileExportersFlatNPaths) {
if ((e.exportFormats & FORMAT_PALETTIZED) != 0) {
palettizedExporterIds.push_back(cexporterID);
}
cexporterID++;
}

int actualExporterID = palettizedExporterIds[exporterId - 1];
exporterId--;

bool result = false;

if (actualExporterID >= g_fileExportersMLNPaths.size()) {
actualExporterID -= g_fileExportersMLNPaths.size();
auto exporter = g_fileExportersFlatNPaths[actualExporterID];
Layer* flat = flattenImageWithoutConvertingToRGB();
result = exporter.exportFunction(name, flat);
delete flat;
}
else {
auto exporter = g_fileExportersMLNPaths[actualExporterID];
result = exporter.exportFunction(name, this);
if (exporterId < g_palettizedFileExporters.size()) {
FileExporter* actualExporterID = g_palettizedFileExporters[exporterId];
if (actualExporterID->exportsWholeSession()) {
result = actualExporterID->exportData(name, this);
}
else {
Layer* flat = flattenImageWithoutConvertingToRGB();
result = actualExporterID->exportData(name, flat);
delete flat;
}
}

if (result) {
Expand Down Expand Up @@ -497,22 +481,11 @@ void MainEditorPalettized::trySavePalettizedImage()
void MainEditorPalettized::trySaveAsPalettizedImage()
{
lastWasSaveAs = true;
std::vector<int> palettizedExporterIds;
std::vector<std::pair<std::string, std::string>> namesAndExtensions;
int exporterID = 0;
for (auto& e : g_fileExportersMLNPaths) {
if ((e.exportFormats & FORMAT_PALETTIZED) != 0) {
namesAndExtensions.push_back({ e.extension, e.name });
palettizedExporterIds.push_back(exporterID);
}
exporterID++;
}
for (auto& e : g_fileExportersFlatNPaths) {
if ((e.exportFormats & FORMAT_PALETTIZED) != 0) {
namesAndExtensions.push_back({ e.extension, e.name });
palettizedExporterIds.push_back(exporterID);
for (auto& e : g_palettizedFileExporters) {
if ((e->formatFlags() & FORMAT_PALETTIZED) != 0) {
namesAndExtensions.push_back({ e->extension(), e->name()});
}
exporterID++;
}
platformTrySaveOtherFile(this, namesAndExtensions, "save palettized image", EVENT_PALETTIZEDEDITOR_SAVEFILE);
}
Expand Down
41 changes: 11 additions & 30 deletions freesprite/PopupQuickConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ PopupQuickConvert::PopupQuickConvert(std::string tt, std::string tx) {
wxsManager.addDrawable(nbutton);

std::vector<std::string> formats;
for (auto& fmt : g_fileExportersFlatNPaths) {
formats.push_back(fmt.name);
}
for (auto& fmt : g_fileExportersMLNPaths) {
formats.push_back(fmt.name);
for (auto& fmt : g_fileExporters) {
formats.push_back(fmt->name());
}

exporterIndex = 0;
Expand Down Expand Up @@ -72,47 +69,31 @@ void PopupQuickConvert::onDropFileEvent(SDL_Event evt)

MainEditor* session = loadAnyIntoSession(path);
if (session != NULL) {
if (exporterIndex >= g_fileExportersFlatNPaths.size()) {
int realExporterIndex = exporterIndex - g_fileExportersFlatNPaths.size();
FileExportMultiLayerNPath exporter = g_fileExportersMLNPaths[realExporterIndex];

/*Layer* l;
FileExporter* exporter = g_fileExporters[exporterIndex];

if (session->isPalettized) {
if ((exporter.exportFormats & FORMAT_PALETTIZED) != 0) {
MainEditorPalettized* upcastSession = (MainEditorPalettized*)session;
l = upcastSession->flattenImageWithoutConvertingToRGB();
}
else {
MainEditorPalettized* upcastSession = (MainEditorPalettized*)session;
l = upcastSession->flattenImageAndConvertToRGB();
}
}
else {
l = session->flattenImage();
}*/
if (session->isPalettized && ((exporter.exportFormats & FORMAT_PALETTIZED) == 0)) {
if (exporter->exportsWholeSession()) {

if (session->isPalettized && ((exporter->formatFlags() & FORMAT_PALETTIZED) == 0)) {
MainEditor* rgbConvEditor = ((MainEditorPalettized*)session)->toRGBSession();
delete session;
session = rgbConvEditor;
}

outPath += convertStringOnWin32(exporter.extension);
outPath += convertStringOnWin32(exporter->extension());

if (exporter.exportFunction(outPath, session)) {
if (exporter->exportData(outPath, session)) {
g_addNotification(Notification("Success", "Exported file", 4000, NULL, COLOR_INFO));
}
else {
g_addNotification(ErrorNotification("Error", "Failed to export file"));
}
}
else {
FileExportFlatNPath exporter = g_fileExportersFlatNPaths[exporterIndex];

Layer* l = NULL;

if (session->isPalettized) {
if ((exporter.exportFormats & FORMAT_PALETTIZED) != 0) {
if ((exporter->formatFlags() & FORMAT_PALETTIZED) != 0) {
MainEditorPalettized* upcastSession = (MainEditorPalettized*)session;
l = upcastSession->flattenImageWithoutConvertingToRGB();
}
Expand All @@ -125,9 +106,9 @@ void PopupQuickConvert::onDropFileEvent(SDL_Event evt)
l = session->flattenImage();
}

outPath += convertStringOnWin32(exporter.extension);
outPath += convertStringOnWin32(exporter->extension());

if (exporter.exportFunction(outPath, l)) {
if (exporter->exportData(outPath, l)) {
g_addNotification(Notification("Success", "Exported file", 4000, NULL, COLOR_INFO));
}
else {
Expand Down
3 changes: 3 additions & 0 deletions freesprite/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ToolRectSwap.h"
#include "ToolText.h"
#include "Gamepad.h"
#include "FileIO.h"

#include "ee_creature.h"

Expand Down Expand Up @@ -218,6 +219,8 @@ int main(int argc, char** argv)
g_gamepad = new Gamepad();
g_gamepad->TryCaptureGamepad();

g_setupIO();

//load brushes
g_brushes.push_back(new Brush1x1());
g_brushes.push_back(new Brush1x1ArcX());
Expand Down
28 changes: 14 additions & 14 deletions freesprite/maineditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,15 +850,18 @@ void MainEditor::eventFileSaved(int evt_id, PlatformNativePathString name, int e
printf("eventFileSaved: got file name %ls\n", name.c_str());

bool result = false;
if (exporterID >= g_fileExportersMLNPaths.size()) {
FileExportFlatNPath f = g_fileExportersFlatNPaths[exporterID - g_fileExportersMLNPaths.size()];
Layer* flat = flattenImage();
result = f.exportFunction(name, flat);
delete flat;
}
else {
FileExportMultiLayerNPath f = g_fileExportersMLNPaths[exporterID];
result = f.exportFunction(name, this);

if (exporterID < g_fileExporters.size()) {
FileExporter* exporter = g_fileExporters[exporterID];

if (exporter->exportsWholeSession()) {
result = exporter->exportData(name, this);
}
else {
Layer* flat = flattenImage();
result = exporter->exportData(name, flat);
delete flat;
}
}

if (result) {
Expand Down Expand Up @@ -978,11 +981,8 @@ void MainEditor::trySaveAsImage()
{
lastWasSaveAs = true;
std::vector<std::pair<std::string, std::string>> formats;
for (FileExportMultiLayerNPath f : g_fileExportersMLNPaths) {
formats.push_back({ f.extension, f.name });
}
for (FileExportFlatNPath f : g_fileExportersFlatNPaths) {
formats.push_back({ f.extension, f.name });
for (auto f : g_fileExporters) {
formats.push_back({ f->extension(), f->name()});
}
platformTrySaveOtherFile(this, formats, "save image", EVENT_MAINEDITOR_SAVEFILE);
}
Expand Down
Loading

0 comments on commit da09536

Please sign in to comment.