yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
palette.cc
Go to the documentation of this file.
1#include "cli/cli.h"
3
6#include "absl/flags/flag.h"
7#include "absl/flags/declare.h"
8
9ABSL_DECLARE_FLAG(std::string, rom);
10
11namespace yaze {
12namespace cli {
13
14// Forward declarations for legacy functions
15absl::Status HandlePaletteExportLegacy(const std::vector<std::string>& arg_vec);
16absl::Status HandlePaletteImportLegacy(const std::vector<std::string>& arg_vec);
17
18// Legacy Palette constructor removed - using new CommandHandler system
19
20// Legacy Palette class removed - using new CommandHandler system
21// This implementation should be moved to PaletteCommandHandler
22absl::Status HandlePaletteLegacy(const std::vector<std::string>& arg_vec) {
23 if (arg_vec.empty()) {
24 return absl::InvalidArgumentError("Usage: palette <export|import> [options]");
25 }
26
27 const std::string& action = arg_vec[0];
28 std::vector<std::string> new_args(arg_vec.begin() + 1, arg_vec.end());
29
30 if (action == "export") {
31 return HandlePaletteExportLegacy(new_args);
32 } else if (action == "import") {
33 return HandlePaletteImportLegacy(new_args);
34 }
35
36 return absl::InvalidArgumentError("Invalid action for palette command.");
37}
38
39// Legacy Palette TUI removed - using new CommandHandler system
40void HandlePaletteTUI(ftxui::ScreenInteractive& screen) {
41 // TODO: Implement palette editor TUI
42 (void)screen; // Suppress unused parameter warning
43}
44
45// Legacy PaletteExport class removed - using new CommandHandler system
46absl::Status HandlePaletteExportLegacy(const std::vector<std::string>& arg_vec) {
47 std::string group_name;
48 int palette_id = -1;
49 std::string output_file;
50
51 for (size_t i = 0; i < arg_vec.size(); ++i) {
52 const std::string& arg = arg_vec[i];
53 if ((arg == "--group") && i + 1 < arg_vec.size()) {
54 group_name = arg_vec[++i];
55 } else if ((arg == "--id") && i + 1 < arg_vec.size()) {
56 palette_id = std::stoi(arg_vec[++i]);
57 } else if ((arg == "--to") && i + 1 < arg_vec.size()) {
58 output_file = arg_vec[++i];
59 }
60 }
61
62 if (group_name.empty() || palette_id == -1 || output_file.empty()) {
63 return absl::InvalidArgumentError("Usage: palette export --group <group> --id <id> --to <file>");
64 }
65
66 std::string rom_file = absl::GetFlag(FLAGS_rom);
67 if (rom_file.empty()) {
68 return absl::InvalidArgumentError("ROM file must be provided via --rom flag.");
69 }
70
71 Rom rom;
72 rom.LoadFromFile(rom_file);
73 if (!rom.is_loaded()) {
74 return absl::AbortedError("Failed to load ROM.");
75 }
76
77 auto palette_group = rom.palette_group().get_group(group_name);
78 if (!palette_group) {
79 return absl::NotFoundError("Palette group not found.");
80 }
81
82 auto palette = palette_group->palette(palette_id);
83 if (palette.empty()) {
84 return absl::NotFoundError("Palette not found.");
85 }
86
87 std::vector<SDL_Color> sdl_palette;
88 for (const auto& color : palette) {
89 SDL_Color sdl_color;
90 sdl_color.r = color.rgb().x;
91 sdl_color.g = color.rgb().y;
92 sdl_color.b = color.rgb().z;
93 sdl_color.a = 255;
94 sdl_palette.push_back(sdl_color);
95 }
96
97 auto status = gfx::SaveCol(output_file, sdl_palette);
98 if (!status.ok()) {
99 return status;
100 }
101
102 std::cout << "Successfully exported palette " << palette_id << " from group " << group_name << " to " << output_file << std::endl;
103
104 return absl::OkStatus();
105}
106
107// Legacy PaletteImport class removed - using new CommandHandler system
108absl::Status HandlePaletteImportLegacy(const std::vector<std::string>& arg_vec) {
109 std::string group_name;
110 int palette_id = -1;
111 std::string input_file;
112
113 for (size_t i = 0; i < arg_vec.size(); ++i) {
114 const std::string& arg = arg_vec[i];
115 if ((arg == "--group") && i + 1 < arg_vec.size()) {
116 group_name = arg_vec[++i];
117 } else if ((arg == "--id") && i + 1 < arg_vec.size()) {
118 palette_id = std::stoi(arg_vec[++i]);
119 } else if ((arg == "--from") && i + 1 < arg_vec.size()) {
120 input_file = arg_vec[++i];
121 }
122 }
123
124 if (group_name.empty() || palette_id == -1 || input_file.empty()) {
125 return absl::InvalidArgumentError("Usage: palette import --group <group> --id <id> --from <file>");
126 }
127
128 std::string rom_file = absl::GetFlag(FLAGS_rom);
129 if (rom_file.empty()) {
130 return absl::InvalidArgumentError("ROM file must be provided via --rom flag.");
131 }
132
133 Rom rom;
134 rom.LoadFromFile(rom_file);
135 if (!rom.is_loaded()) {
136 return absl::AbortedError("Failed to load ROM.");
137 }
138
139 auto sdl_palette = gfx::DecodeColFile(input_file);
140 if (sdl_palette.empty()) {
141 return absl::AbortedError("Failed to load palette file.");
142 }
143
145 for (const auto& sdl_color : sdl_palette) {
146 snes_palette.AddColor(gfx::SnesColor(sdl_color.r, sdl_color.g, sdl_color.b));
147 }
148
149 auto palette_group = rom.palette_group().get_group(group_name);
150 if (!palette_group) {
151 return absl::NotFoundError("Palette group not found.");
152 }
153
154 // Replace the palette at the specified index
155 auto* pal = palette_group->mutable_palette(palette_id);
156 *pal = snes_palette;
157
158 // TODO: Implement saving the modified palette back to the ROM.
159 auto save_status = rom.SaveToFile({.save_new = false});
160 if (!save_status.ok()) {
161 return save_status;
162 }
163
164 std::cout << "Successfully imported palette " << palette_id << " to group " << group_name << " from " << input_file << std::endl;
165 std::cout << "✅ ROM saved to: " << rom.filename() << std::endl;
166
167 return absl::OkStatus();
168}
169
170} // namespace cli
171} // namespace yaze
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:71
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
Definition rom.cc:289
auto palette_group() const
Definition rom.h:213
auto filename() const
Definition rom.h:208
absl::Status SaveToFile(const SaveSettings &settings)
Definition rom.cc:536
bool is_loaded() const
Definition rom.h:197
SNES Color container.
Definition snes_color.h:38
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
void HandlePaletteTUI(ftxui::ScreenInteractive &screen)
Definition palette.cc:40
absl::Status HandlePaletteLegacy(const std::vector< std::string > &arg_vec)
Definition palette.cc:22
absl::Status HandlePaletteExportLegacy(const std::vector< std::string > &arg_vec)
Definition palette.cc:46
absl::Status HandlePaletteImportLegacy(const std::vector< std::string > &arg_vec)
Definition palette.cc:108
absl::Status SaveCol(std::string_view filename, const std::vector< SDL_Color > &palette)
Save Col file (palette data)
std::vector< SDL_Color > DecodeColFile(const std::string_view filename)
Decode color file.
Main namespace for the application.
ABSL_DECLARE_FLAG(std::string, rom)
SNES color palette.
Definition yaze.h:240