Skip to content

Commit 75fabb0

Browse files
committed
formatpicker when saving palettes, fix remove fav.
1 parent 9d51afc commit 75fabb0

11 files changed

Lines changed: 105 additions & 59 deletions

freesprite/FileIO.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,8 +3153,8 @@ void g_setupIO() {
31533153
PaletteExporter
31543154
* exVOIDPLT,
31553155
* exHexPLT;
3156-
g_paletteExporters.push_back(exVOIDPLT = PaletteExporter::paletteExporter("voidsprite palette", ".voidplt", &writePltVOIDPLT));
3157-
g_paletteExporters.push_back(exHexPLT = PaletteExporter::paletteExporter("Hex palette", ".hex", &writePltHEX));
3156+
g_paletteExporters.push_back(exVOIDPLT = PaletteExporter::paletteExporter("voidsprite palette", ".voidplt", TL("vsp.export.palette.voidplt"), &writePltVOIDPLT));
3157+
g_paletteExporters.push_back(exHexPLT = PaletteExporter::paletteExporter("Hex palette", ".hex", TL("vsp.export.palette.hex"), &writePltHEX));
31583158

31593159
g_paletteImporters.push_back(PaletteImporter::paletteImporter("voidsprite palette", ".voidplt", &readPltVOIDPLT, NULL, exVOIDPLT));
31603160
g_paletteImporters.push_back(PaletteImporter::paletteImporter("Hex palette", ".hex", &readPltHEX, NULL, exHexPLT));

freesprite/FileIO.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,12 @@ class FileImporter : public FileOperation {
318318

319319
class PaletteImporter : public FileOperation {
320320
public:
321-
static PaletteImporter* paletteImporter(std::string name, std::string extension,
321+
static PaletteImporter* paletteImporter(
322+
std::string name,
323+
std::string extension,
322324
std::function<std::pair<bool, std::vector<uint32_t>>(PlatformNativePathString)> importFunction,
323-
std::function<bool(PlatformNativePathString)> canImport = NULL, PaletteExporter* reverse = NULL) {
325+
std::function<bool(PlatformNativePathString)> canImport = NULL, PaletteExporter* reverse = NULL)
326+
{
324327

325328
PaletteImporter* ret = new PaletteImporter();
326329
ret->_name = name;
@@ -348,10 +351,16 @@ class PaletteImporter : public FileOperation {
348351
class PaletteExporter : public FileOperation {
349352

350353
public:
351-
static PaletteExporter* paletteExporter(std::string name, std::string extension, std::function<bool(PlatformNativePathString, std::vector<u32>)> exportFunction) {
354+
static PaletteExporter* paletteExporter(
355+
std::string name,
356+
std::string extension,
357+
std::string description,
358+
std::function<bool(PlatformNativePathString, std::vector<u32>)> exportFunction)
359+
{
352360
PaletteExporter* ret = new PaletteExporter();
353361
ret->_name = name;
354362
ret->_extension = extension;
363+
ret->description = description;
355364
ret->_exportFunction = exportFunction;
356365
return ret;
357366
}

freesprite/PalettizedEditorColorPicker.cpp

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "TabbedView.h"
99
#include "UILabel.h"
1010
#include "MainEditorPalettized.h"
11+
#include "PopupChooseFormat.h"
1112

1213
PalettizedEditorColorPicker::PalettizedEditorColorPicker(MainEditorPalettized* c)
1314
{
@@ -48,7 +49,15 @@ PalettizedEditorColorPicker::PalettizedEditorColorPicker(MainEditorPalettized* c
4849
buttonSavePalette->wxWidth = 120;
4950
buttonSavePalette->wxHeight = 30;
5051
buttonSavePalette->onClickCallback = [this](UIButton*) {
51-
platformTrySaveOtherFile(this, { {".voidplt", "voidsprite palette"} }, "save palette", EVENT_PALETTECOLORPICKER_SAVEPALETTE);
52+
PopupChooseFormat* popup = PopupChooseFormat::withDefaultPaletteExportFormats("Choose format", "");
53+
popup->chooseFormatAndDoFileSavePrompt("save palette", [this](FormatDef* f, PlatformNativePathString path) {
54+
if (((PaletteExporter*)f->udata)->exportData(path, &caller->palette)) {
55+
g_addNotification(SuccessNotification("Success", "Palette file saved"));
56+
}
57+
else {
58+
g_addNotification(ErrorNotification(TL("vsp.cmn.error"), "Could not save palette file"));
59+
}
60+
});
5261
};
5362
colorPaletteTabs->tabs[1].wxs.addDrawable(buttonSavePalette);
5463

@@ -151,28 +160,6 @@ void PalettizedEditorColorPicker::eventDropdownItemSelected(int evt_id, int inde
151160
}
152161
}
153162

154-
void PalettizedEditorColorPicker::eventFileSaved(int evt_id, PlatformNativePathString name, int exporterIndex)
155-
{
156-
if (evt_id == EVENT_PALETTECOLORPICKER_SAVEPALETTE) {
157-
FILE* f = platformOpenFile(name, PlatformFileModeWB);
158-
if (f != NULL) {
159-
fwrite("VOIDPLT", 7, 1, f);
160-
uint8_t fileversion = 1;
161-
fwrite(&fileversion, 1, 1, f);
162-
uint32_t count = caller->palette.size();
163-
fwrite(&count, 1, 4, f);
164-
for (uint32_t col : caller->palette) {
165-
fwrite(&col, 1, 4, f);
166-
}
167-
fclose(f);
168-
g_addNotification(SuccessNotification("Success", "Palette file saved"));
169-
}
170-
else {
171-
g_addNotification(ErrorNotification("Error", "Could not save palette file"));
172-
}
173-
}
174-
}
175-
176163
void PalettizedEditorColorPicker::eventFileOpen(int evt_id, PlatformNativePathString name, int importerIndex)
177164
{
178165
if (evt_id == EVENT_PALETTECOLORPICKER_LOADPALETTE) {

freesprite/PalettizedEditorColorPicker.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class PalettizedEditorColorPicker :
4343
void renderAfterBG(XY position) override;
4444

4545
void eventDropdownItemSelected(int evt_id, int index, std::string name) override;
46-
void eventFileSaved(int evt_id, PlatformNativePathString name, int importerIndex = -1) override;
4746
void eventFileOpen(int evt_id, PlatformNativePathString name, int importerIndex = -1) override;
4847

4948
void updateForcedColorPaletteButtons();

freesprite/PopupChooseFormat.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ void PopupChooseFormat::buildFormatList() {
2424
bool isFav = stringStartsWithIgnoreCase(f.name, "\xE2\x98\x85");
2525

2626
std::string name = f.name;
27+
std::string originalName = f.originalName;
2728

2829
UIButton* btn = new UIButton(name);
2930
btn->position = { 0,0 };
@@ -44,13 +45,18 @@ void PopupChooseFormat::buildFormatList() {
4445
}
4546

4647
if (isFav) {
47-
btn->onRightClickCallback = [this, name](...) {
48+
btn->onRightClickCallback = [this, originalName](...) {
4849
g_addPopup(new PopupContextMenu({
49-
{"Remove from favourites", [this, name]() {
50-
auto indexAt = std::find(g_config.favExportFormats.begin(), g_config.favExportFormats.end(), name);
51-
g_config.favExportFormats.erase(indexAt);
52-
g_saveConfig();
53-
filterList(filterQuery);
50+
{"Remove from favourites", [this, originalName]() {
51+
auto indexAt = std::find(g_config.favExportFormats.begin(), g_config.favExportFormats.end(), originalName);
52+
if (indexAt != g_config.favExportFormats.end()) {
53+
g_config.favExportFormats.erase(indexAt);
54+
g_saveConfig();
55+
filterList(filterQuery);
56+
}
57+
else {
58+
logerr("failed to remove fav format");
59+
}
5460
}}
5561
}));
5662
};
@@ -59,10 +65,10 @@ void PopupChooseFormat::buildFormatList() {
5965
priority1Buttons.push_back(newButton);
6066
}
6167
else {
62-
btn->onRightClickCallback = [this, name](...) {
68+
btn->onRightClickCallback = [this, originalName](...) {
6369
g_addPopup(new PopupContextMenu({
64-
{"Add to favourites", [this, name]() {
65-
g_config.favExportFormats.push_back(name);
70+
{"Add to favourites", [this, originalName]() {
71+
g_config.favExportFormats.push_back(originalName);
6672
g_saveConfig();
6773
filterList(filterQuery);
6874
}}
@@ -148,9 +154,28 @@ PopupChooseFormat* PopupChooseFormat::withDefaultIndexedExportFormats(std::strin
148154
return withDefaultExportFormats(tt, tx, FORMAT_PALETTIZED);
149155
}
150156

157+
PopupChooseFormat* PopupChooseFormat::withDefaultPaletteExportFormats(std::string tt, std::string tx)
158+
{
159+
std::vector<FormatDef> ff;
160+
for (PaletteExporter* f : g_paletteExporters) {
161+
ff.push_back({
162+
.name = f->name(),
163+
.extension = f->extension(),
164+
.description = f->description,
165+
.udata = (void*)f
166+
});
167+
}
168+
std::vector<FormatDef> ff2 = processFavouriteFormats(ff);
169+
PopupChooseFormat* ret = new PopupChooseFormat(tt, tx, ff2);
170+
ret->hasDefaultFormats = true;
171+
ret->defaultFormats = ff;
172+
return ret;
173+
}
174+
151175
std::vector<FormatDef> PopupChooseFormat::processFavouriteFormats(std::vector<FormatDef> srcList) {
152176
std::vector<FormatDef> ret;
153177
for (auto& f : srcList) {
178+
f.originalName = f.name;
154179
if (std::find(g_config.favExportFormats.begin(), g_config.favExportFormats.end(), f.name) != g_config.favExportFormats.end()) {
155180
f.name = "\xE2\x98\x85 " + f.name;
156181
ret.insert(ret.begin(), f);

freesprite/PopupChooseFormat.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ struct FormatDef {
77
std::string extension;
88
std::string description;
99
void* udata;
10+
11+
std::string originalName = "";
1012
};
1113

1214
class PopupChooseFormat : public BasePopup, EventCallbackListener
@@ -35,6 +37,8 @@ class PopupChooseFormat : public BasePopup, EventCallbackListener
3537
static PopupChooseFormat* withDefaultRGBExportFormats(std::string tt, std::string tx);
3638
static PopupChooseFormat* withDefaultIndexedExportFormats(std::string tt, std::string tx);
3739

40+
static PopupChooseFormat* withDefaultPaletteExportFormats(std::string tt, std::string tx);
41+
3842
static std::vector<FormatDef> processFavouriteFormats(std::vector<FormatDef> srcList);
3943

4044
void filterList(std::string search);

freesprite/UIColorPicker.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "UIHueWheel.h"
1313
#include "UIColorInputField.h"
1414
#include "PopupContextMenu.h"
15+
#include "PopupChooseFormat.h"
1516
#include "io/io_voidsprite.h"
1617

1718
#if VSP_PLATFORM == VSP_PLATFORM_WIN32
@@ -573,25 +574,7 @@ void UIColorPicker::reloadColorLists()
573574
newPaletteButton->position = XY{ 5, 60 };
574575
newPaletteButton->wxWidth = 140;
575576
newPaletteButton->onClickCallback = [this](UIButton*) {
576-
PopupTextBox* popup = new PopupTextBox(TL("vsp.maineditor.panel.colorpicker.tab.palettes.newpalette.popup.title"),
577-
TL("vsp.maineditor.panel.colorpicker.tab.palettes.newpalette.popup.desc"), "");
578-
popup->onTextInputConfirmedCallback = [this](PopupTextBox*, std::string name) {
579-
if (name.find_first_of("/\\") == std::string::npos) {
580-
PlatformNativePathString newPalettePath = platformEnsureDirAndGetConfigFilePath() + convertStringOnWin32("/palettes/" + name + ".voidplt");
581-
if (writePltVOIDPLT(newPalettePath, {})) {
582-
g_reloadColorMap();
583-
reloadColorLists();
584-
}
585-
else {
586-
g_addNotification(ErrorNotification(TL("vsp.cmn.error"), TL("vsp.cmn.error.exportfail")));
587-
}
588-
}
589-
else {
590-
//invalid characters in name
591-
g_addNotification(ErrorNotification(TL("vsp.cmn.error"), TL("vsp.cmn.error.exportfail")));
592-
}
593-
};
594-
g_addPopup(popup);
577+
g_promptCreateNewPalette({}, [this]() { reloadColorLists(); });
595578
};
596579
otherButtons->subWidgets.addDrawable(newPaletteButton);
597580

freesprite/colors.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "colors.h"
66
#include "background_operation.h"
77
#include "Notification.h"
8+
#include "PopupTextBox.h"
9+
#include "PopupChooseFormat.h"
810
#include "io/io_voidsprite.h"
911

1012
std::vector<NamedColorPalette> g_baseNamedColorMap = {
@@ -520,6 +522,34 @@ void g_downloadAndInstallPaletteFromLospec(std::string url)
520522
}
521523
}
522524

525+
void g_promptCreateNewPalette(std::vector<u32> newPalette, std::function<void()> onSuccessCallback)
526+
{
527+
PopupTextBox* popup = new PopupTextBox(TL("vsp.maineditor.panel.colorpicker.tab.palettes.newpalette.popup.title"),
528+
TL("vsp.maineditor.panel.colorpicker.tab.palettes.newpalette.popup.desc"), "");
529+
popup->onTextInputConfirmedCallback = [newPalette, onSuccessCallback](PopupTextBox*, std::string name) {
530+
if (name.find_first_of("/\\") == std::string::npos) {
531+
532+
PopupChooseFormat* popup2 = PopupChooseFormat::withDefaultPaletteExportFormats("Choose format", "");
533+
popup2->onFormatChosenCallback = [newPalette, name, onSuccessCallback](FormatDef* f) {
534+
PlatformNativePathString newPalettePath = platformEnsureDirAndGetConfigFilePath() + convertStringOnWin32("/palettes/" + name + f->extension);
535+
if (((PaletteExporter*)f->udata)->exportData(newPalettePath, (void*)&newPalette)) {
536+
g_reloadColorMap();
537+
onSuccessCallback();
538+
}
539+
else {
540+
g_addNotification(ErrorNotification(TL("vsp.cmn.error"), TL("vsp.cmn.error.exportfail")));
541+
}
542+
};
543+
g_addPopup(popup2);
544+
}
545+
else {
546+
//invalid characters in name
547+
g_addNotification(ErrorNotification(TL("vsp.cmn.error"), "Invalid characters in name"));
548+
}
549+
};
550+
g_addPopup(popup);
551+
}
552+
523553
bool NamedColorPalette::save() {
524554
if (correspondingExporter != NULL && !path.empty()) {
525555
std::vector<u32> colors;

freesprite/colors.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,5 @@ inline std::vector<NamedColorPalette> g_namedColorMap = {};
8888
void g_generateColorMap();
8989
void g_reloadColorMap();
9090
IPalette* g_paletteByName(std::string name);
91-
void g_downloadAndInstallPaletteFromLospec(std::string url);
91+
void g_downloadAndInstallPaletteFromLospec(std::string url);
92+
void g_promptCreateNewPalette(std::vector<u32> newPalette, std::function<void()> onSuccessCallback);

freesprite/io/io_palettes.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,13 @@ std::pair<bool, std::vector<uint32_t>> readPltHEX(PlatformNativePathString name)
7878
std::ifstream f(std::filesystem::path(name), std::ios::in);
7979
if (f.is_open()) {
8080
std::pair<bool, std::vector<uint32_t>> ret;
81+
bool anyNonEmptyLine = false;
8182
while (!f.eof()) {
8283
std::string line;
8384
std::getline(f, line);
85+
if (line.size() > 0) {
86+
anyNonEmptyLine = true;
87+
}
8488
if (line.size() == 6 || line.size() == 8) {
8589
std::string r = line.substr(0, 2);
8690
std::string g = line.substr(2, 2);
@@ -94,7 +98,8 @@ std::pair<bool, std::vector<uint32_t>> readPltHEX(PlatformNativePathString name)
9498
}
9599
}
96100
f.close();
97-
ret.first = ret.second.size() > 0;
101+
//allow empty files so that creating a new .hex palette works
102+
ret.first = !anyNonEmptyLine || ret.second.size() > 0;
98103
return ret;
99104
}
100105
return { false,{} };

0 commit comments

Comments
 (0)