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#include "app/rom.h"
18
19namespace yaze {
20namespace gfx {
21
33
46
48 std::string group_name;
49 int palette_index = -1;
50 int color_index = -1;
51};
52
69 public:
70 using ChangeCallback = std::function<void(const PaletteChangeEvent&)>;
71
73 static PaletteManager& Get() {
74 static PaletteManager instance;
75 return instance;
76 }
77
78 // Delete copy/move constructors and assignment operators
83
84 // ========== Initialization ==========
85
90 void Initialize(Rom* rom);
91
95 bool IsInitialized() const { return rom_ != nullptr; }
96
97 // ========== Color Operations ==========
98
106 SnesColor GetColor(const std::string& group_name, int palette_index,
107 int color_index) const;
108
117 absl::Status SetColor(const std::string& group_name, int palette_index,
118 int color_index, const SnesColor& new_color);
119
123 absl::Status ResetColor(const std::string& group_name, int palette_index,
124 int color_index);
125
129 absl::Status ResetPalette(const std::string& group_name, int palette_index);
130
131 // ========== Dirty Tracking ==========
132
136 bool HasUnsavedChanges() const;
137
141 std::vector<std::string> GetModifiedGroups() const;
142
146 bool IsGroupModified(const std::string& group_name) const;
147
151 bool IsPaletteModified(const std::string& group_name,
152 int palette_index) const;
153
157 bool IsColorModified(const std::string& group_name, int palette_index,
158 int color_index) const;
159
163 size_t GetModifiedColorCount() const;
164
165 // ========== Persistence ==========
166
170 absl::Status SaveGroup(const std::string& group_name);
171
175 absl::Status SaveAllToRom();
176
180 void DiscardGroup(const std::string& group_name);
181
185 void DiscardAllChanges();
186
187 // ========== Undo/Redo ==========
188
192 void Undo();
193
197 void Redo();
198
202 bool CanUndo() const { return !undo_stack_.empty(); }
203
207 bool CanRedo() const { return !redo_stack_.empty(); }
208
212 size_t GetUndoStackSize() const { return undo_stack_.size(); }
213
217 size_t GetRedoStackSize() const { return redo_stack_.size(); }
218
222 void ClearHistory();
223
224 // ========== Change Notifications ==========
225
231
235 void UnregisterChangeListener(int callback_id);
236
237 // ========== Batch Operations ==========
238
243 void BeginBatch();
244
248 void EndBatch();
249
253 bool InBatch() const { return batch_depth_ > 0; }
254
255 private:
256 PaletteManager() = default;
257 ~PaletteManager() = default;
258
260 PaletteGroup* GetMutableGroup(const std::string& group_name);
261
263 const PaletteGroup* GetGroup(const std::string& group_name) const;
264
266 SnesColor GetOriginalColor(const std::string& group_name, int palette_index,
267 int color_index) const;
268
270 void RecordChange(const PaletteColorChange& change);
271
273 void NotifyListeners(const PaletteChangeEvent& event);
274
276 void MarkModified(const std::string& group_name, int palette_index,
277 int color_index);
278
280 void ClearModifiedFlags(const std::string& group_name);
281
282 // ========== Member Variables ==========
283
285 Rom* rom_ = nullptr;
286
289 std::unordered_map<std::string, std::vector<SnesPalette>>
291
294 std::unordered_map<std::string, std::unordered_set<int>>
296
299 std::unordered_map<std::string,
300 std::unordered_map<int, std::unordered_set<int>>>
302
304 std::deque<PaletteColorChange> undo_stack_;
305 std::deque<PaletteColorChange> redo_stack_;
306 static constexpr size_t kMaxUndoHistory = 500;
307
309 std::unordered_map<int, ChangeCallback> change_listeners_;
311
314 std::vector<PaletteColorChange> batch_changes_;
315};
316
317} // namespace gfx
318} // namespace yaze
319
320#endif // YAZE_APP_GFX_PALETTE_MANAGER_H
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:74
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)
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.
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.
void Initialize(Rom *rom)
Initialize the palette manager with ROM data.
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.
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.
void Redo()
Redo the most recently undone change.
SNES Color container.
Definition snes_color.h:109
Main namespace for the application.
Definition controller.cc:20
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.