yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
palette_manager.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_PALETTE_MANAGER_H
2#define YAZE_APP_GFX_PALETTE_MANAGER_H
3
4#include <cstdint>
5#include <deque>
6#include <functional>
7#include <memory>
8#include <string>
9#include <unordered_map>
10#include <unordered_set>
11#include <vector>
12
13#include "absl/status/status.h"
14#include "absl/status/statusor.h"
17
18namespace yaze {
19
20// Forward declarations
21class Rom;
22namespace zelda3 {
23struct GameData;
24}
25
26namespace gfx {
27
39
52
54 std::string group_name;
55 int palette_index = -1;
56 int color_index = -1;
57};
58
75 public:
76 using ChangeCallback = std::function<void(const PaletteChangeEvent&)>;
77
79 static PaletteManager& Get() {
80 static PaletteManager instance;
81 return instance;
82 }
83
84 // Delete copy/move constructors and assignment operators
89
90 // ========== Initialization ==========
91
96 void Initialize(zelda3::GameData* game_data);
97
102 void Initialize(Rom* rom);
103
107 bool IsInitialized() const { return game_data_ != nullptr || rom_ != nullptr; }
108
109 // ========== Color Operations ==========
110
118 SnesColor GetColor(const std::string& group_name, int palette_index,
119 int color_index) const;
120
129 absl::Status SetColor(const std::string& group_name, int palette_index,
130 int color_index, const SnesColor& new_color);
131
135 absl::Status ResetColor(const std::string& group_name, int palette_index,
136 int color_index);
137
141 absl::Status ResetPalette(const std::string& group_name, int palette_index);
142
143 // ========== Dirty Tracking ==========
144
148 bool HasUnsavedChanges() const;
149
153 std::vector<std::string> GetModifiedGroups() const;
154
158 bool IsGroupModified(const std::string& group_name) const;
159
163 bool IsPaletteModified(const std::string& group_name,
164 int palette_index) const;
165
169 bool IsColorModified(const std::string& group_name, int palette_index,
170 int color_index) const;
171
175 size_t GetModifiedColorCount() const;
176
177 // ========== Persistence ==========
178
182 absl::Status SaveGroup(const std::string& group_name);
183
187 absl::Status SaveAllToRom();
188
192 void DiscardGroup(const std::string& group_name);
193
197 void DiscardAllChanges();
198
199 // ========== Preview Mode ==========
200
208 absl::Status ApplyPreviewChanges();
209
210 // ========== Undo/Redo ==========
211
215 void Undo();
216
220 void Redo();
221
225 bool CanUndo() const { return !undo_stack_.empty(); }
226
230 bool CanRedo() const { return !redo_stack_.empty(); }
231
235 size_t GetUndoStackSize() const { return undo_stack_.size(); }
236
240 size_t GetRedoStackSize() const { return redo_stack_.size(); }
241
245 void ClearHistory();
246
247 // ========== Change Notifications ==========
248
254
258 void UnregisterChangeListener(int callback_id);
259
260 // ========== Batch Operations ==========
261
266 void BeginBatch();
267
271 void EndBatch();
272
276 bool InBatch() const { return batch_depth_ > 0; }
277
278 private:
279 PaletteManager() = default;
280 ~PaletteManager() = default;
281
283 PaletteGroup* GetMutableGroup(const std::string& group_name);
284
286 const PaletteGroup* GetGroup(const std::string& group_name) const;
287
289 SnesColor GetOriginalColor(const std::string& group_name, int palette_index,
290 int color_index) const;
291
293 void RecordChange(const PaletteColorChange& change);
294
296 void NotifyListeners(const PaletteChangeEvent& event);
297
299 void MarkModified(const std::string& group_name, int palette_index,
300 int color_index);
301
303 void ClearModifiedFlags(const std::string& group_name);
304
305 // ========== Member Variables ==========
306
309
311 Rom* rom_ = nullptr;
312
315 std::unordered_map<std::string, std::vector<SnesPalette>> original_palettes_;
316
319 std::unordered_map<std::string, std::unordered_set<int>> modified_palettes_;
320
323 std::unordered_map<std::string,
324 std::unordered_map<int, std::unordered_set<int>>>
326
328 std::deque<PaletteColorChange> undo_stack_;
329 std::deque<PaletteColorChange> redo_stack_;
330 static constexpr size_t kMaxUndoHistory = 500;
331
333 std::unordered_map<int, ChangeCallback> change_listeners_;
335
338 std::vector<PaletteColorChange> batch_changes_;
339};
340
341} // namespace gfx
342} // namespace yaze
343
344#endif // YAZE_APP_GFX_PALETTE_MANAGER_H
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:24
Centralized palette management system.
void RecordChange(const PaletteColorChange &change)
Helper: Record a change for undo.
absl::Status SetColor(const std::string &group_name, int palette_index, int color_index, const SnesColor &new_color)
Set a color in a palette (records change for undo)
std::vector< std::string > GetModifiedGroups() const
Get list of modified palette group names.
static constexpr size_t kMaxUndoHistory
bool HasUnsavedChanges() const
Check if there are ANY unsaved changes.
bool IsGroupModified(const std::string &group_name) const
Check if a specific palette group has modifications.
absl::Status SaveGroup(const std::string &group_name)
Save a specific palette group to ROM.
void BeginBatch()
Begin a batch operation (groups multiple changes into one undo step)
PaletteGroup * GetMutableGroup(const std::string &group_name)
Helper: Get mutable palette group.
size_t GetUndoStackSize() const
Get size of undo stack.
void Undo()
Undo the most recent change.
size_t GetRedoStackSize() const
Get size of redo stack.
void ClearHistory()
Clear undo/redo history.
std::unordered_map< int, ChangeCallback > change_listeners_
Change listeners.
int batch_depth_
Batch operation support.
std::deque< PaletteColorChange > redo_stack_
std::unordered_map< std::string, std::unordered_set< int > > modified_palettes_
void UnregisterChangeListener(int callback_id)
Unregister a change listener.
Rom * rom_
ROM instance (not owned) - legacy, used when game_data_ is null.
std::unordered_map< std::string, std::unordered_map< int, std::unordered_set< int > > > modified_colors_
bool CanRedo() const
Check if redo is available.
bool InBatch() const
Check if currently in a batch operation.
bool CanUndo() const
Check if undo is available.
void Initialize(zelda3::GameData *game_data)
Initialize the palette manager with GameData.
absl::Status ResetColor(const std::string &group_name, int palette_index, int color_index)
Reset a single color to its original ROM value.
void NotifyListeners(const PaletteChangeEvent &event)
Helper: Notify all listeners of an event.
void ClearModifiedFlags(const std::string &group_name)
Helper: Clear modified flags for a group.
void EndBatch()
End a batch operation.
int RegisterChangeListener(ChangeCallback callback)
Register a callback for palette change events.
PaletteManager(PaletteManager &&)=delete
std::deque< PaletteColorChange > undo_stack_
Undo/redo stacks.
absl::Status ResetPalette(const std::string &group_name, int palette_index)
Reset an entire palette to original ROM values.
SnesColor GetOriginalColor(const std::string &group_name, int palette_index, int color_index) const
Helper: Get original color from snapshot.
PaletteManager & operator=(const PaletteManager &)=delete
bool IsColorModified(const std::string &group_name, int palette_index, int color_index) const
Check if a specific color is modified.
void MarkModified(const std::string &group_name, int palette_index, int color_index)
Helper: Mark a color as modified.
void DiscardGroup(const std::string &group_name)
Discard changes for a specific group.
bool IsInitialized() const
Check if manager is initialized.
zelda3::GameData * game_data_
GameData instance (not owned) - preferred.
void DiscardAllChanges()
Discard ALL unsaved changes.
size_t GetModifiedColorCount() const
Get count of modified colors across all groups.
std::unordered_map< std::string, std::vector< SnesPalette > > original_palettes_
PaletteManager & operator=(PaletteManager &&)=delete
std::vector< PaletteColorChange > batch_changes_
static PaletteManager & Get()
Get the singleton instance.
PaletteManager(const PaletteManager &)=delete
const PaletteGroup * GetGroup(const std::string &group_name) const
Helper: Get const palette group.
SnesColor GetColor(const std::string &group_name, int palette_index, int color_index) const
Get a color from a palette.
std::function< void(const PaletteChangeEvent &)> ChangeCallback
absl::Status SaveAllToRom()
Save ALL modified palettes to ROM.
bool IsPaletteModified(const std::string &group_name, int palette_index) const
Check if a specific palette is modified.
absl::Status ApplyPreviewChanges()
Apply preview changes to other editors without saving to ROM.
void Redo()
Redo the most recently undone change.
SNES Color container.
Definition snes_color.h:110
Event notification for palette changes.
@ kColorChanged
Single color was modified.
@ kPaletteReset
Entire palette was reset.
@ kAllDiscarded
All changes discarded.
@ kGroupDiscarded
Palette group changes were discarded.
@ kAllSaved
All changes saved to ROM.
@ kGroupSaved
Palette group was saved to ROM.
Represents a single color change operation.
SnesColor new_color
New color after change.
int color_index
Index of color within palette.
std::string group_name
Palette group name (e.g., "ow_main")
int palette_index
Index of palette within group.
uint64_t timestamp_ms
Timestamp in milliseconds.
SnesColor original_color
Original color before change.
Represents a group of palettes.