yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
dungeon_object_selector.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_DUNGEON_DUNGEON_OBJECT_SELECTOR_H
2#define YAZE_APP_EDITOR_DUNGEON_DUNGEON_OBJECT_SELECTOR_H
3
4#include <array>
5#include <cstdint>
6#include <functional>
7#include <map>
8#include <memory>
9#include <set>
10#include <string>
11#include <utility>
12
13#include "absl/status/status.h"
15#include "app/editor/editor.h"
18#include "rom/rom.h"
19#include "zelda3/dungeon/room.h"
21#include "zelda3/game_data.h"
22// object_renderer.h removed - using ObjectDrawer for production rendering
23#include "imgui/imgui.h"
26
27namespace yaze {
28namespace editor {
29
30class ObjectTileEditorPanel;
31struct DungeonObjectSelectorTestAccess;
32
34 int columns = 1;
35 float item_size = 1.0f;
36 float leading_inset = 0.0f;
37};
38
40 bool valid = false;
41 float x = 0.0f;
42 float y = 0.0f;
43 float width = 0.0f;
44 float height = 0.0f;
45};
46
47// Pure responsive-layout helpers shared by the selector and its unit tests.
49 float available_width, float preferred_item_size, float item_spacing,
50 float min_item_size = 32.0f);
52 float source_height,
53 float box_width,
54 float box_height);
55bool MatchesDungeonObjectStreamFilter(int object_id, int selected_filter);
56bool IsDungeonCustomObjectRuntimeSlot(int object_id, int subtype);
57bool IsMinecartGraphicsRuntimeSlot(int object_id, int subtype);
58std::string GetDungeonCustomObjectSlotName(int object_id, int subtype);
59
64 public:
65 explicit DungeonObjectSelector(Rom* rom = nullptr) : rom_(rom) {}
67
68 // Unified context setter (preferred)
70 rom_ = ctx.rom;
72 }
73 EditorContext context() const { return {rom_, game_data_}; }
74
75 // Individual setters for compatibility
76 void SetRom(Rom* rom) { rom_ = rom; }
77 Rom* rom() const { return rom_; }
80
81 // Room data access
82 void set_rooms(DungeonRoomStore* rooms) { rooms_ = rooms; }
84 void set_current_room_id(int room_id) { current_room_id_ = room_id; }
85
86 // Palette access
87 // Replace the active palette group used by preview rendering. The preview
88 // cache is keyed on object identity plus room blockset, palette, and floor
89 // graphics, none of which capture the *contents* of the palette group: switching
90 // dungeons between two palette banks that happen to use the same numeric
91 // slot value will keep cache hits valid by key but stale by color. Since
92 // the cache rebuilds in well under a frame, we conservatively invalidate
93 // on every palette-group swap rather than fingerprint the group.
94 void SetCurrentPaletteGroup(const gfx::PaletteGroup& palette_group) {
95 current_palette_group_ = palette_group;
97 }
98 // Object selection callbacks
100 std::function<void(const zelda3::RoomObject&)> callback) {
101 object_selected_callback_ = callback;
102 }
103 void SetPlacementInvalidatedCallback(std::function<void()> callback) {
104 placement_invalidated_callback_ = std::move(callback);
105 }
106
107 // Get current preview object for placement
109 bool IsObjectLoaded() const { return object_loaded_; }
110
111 // AssetBrowser-style object selection
112 void DrawObjectAssetBrowser(float minimum_grid_height = 1.0f);
113
114 // Rail palette shares the room-context thumbnail cache and placement path.
115 bool DrawMinecartPalette();
116
117 // Programmatic selection
118 void SelectObject(int obj_id, int subtype = -1);
119
121 std::function<absl::Status(int, const zelda3::RoomObject&)> callback) {
122 edit_object_tiles_callback_ = std::move(callback);
123 }
124
125 // Fixed-slot custom-object management actions.
129 void SetOpenTileEditorWindowCallback(std::function<bool()> callback) {
130 open_tile_editor_window_callback_ = std::move(callback);
131 }
132 void SetOpenMinecartEditorWindowCallback(std::function<bool()> callback) {
133 open_minecart_editor_window_callback_ = std::move(callback);
134 }
135
136 // Invalidate preview and layout caches (e.g., after new custom object added)
139
140 // Break references to one DungeonEditorV2 session without mutating the
141 // process-wide custom-object manager used by other sessions.
143
144 // Test-only inspection. Increments every time InvalidatePreviewCache runs
145 // so unit tests can pin behavioral contracts (e.g. SetCurrentPaletteGroup
146 // must invalidate) without needing a full rendering pipeline.
154 bool matches_object_filter_for_testing(int obj_id, int filter_type) {
155 return MatchesObjectFilter(obj_id, filter_type);
156 }
157 std::string object_type_symbol_for_testing(int object_id) {
158 return GetObjectTypeSymbol(object_id);
159 }
160 static bool IsRepresentableChestObjectId(int object_id);
161
162 private:
164 enum class BrowserMode {
165 kObjects,
167 };
168
169 bool MatchesObjectFilter(int obj_id, int filter_type);
170 bool MatchesRoomUsage(int obj_id) const;
171 bool MatchesObjectSearch(int obj_id, const std::string& name,
172 int subtype = -1) const;
173 void CalculateObjectDimensions(const zelda3::RoomObject& object, int& width,
174 int& height);
175 bool DrawObjectPreview(const zelda3::RoomObject& object, ImVec2 top_left,
176 ImVec2 box_size);
177 zelda3::RoomObject MakePreviewObject(int obj_id) const;
179 ImU32 GetObjectTypeColor(int object_id);
180 std::string GetObjectTypeSymbol(int object_id);
181 absl::Status GetCustomObjectAssetStatus(int object_id, int subtype);
183 absl::Status OpenExistingCustomObjectEditor(int16_t object_id, int subtype,
184 int room_id);
185
189
190 // References for custom-object management.
191 std::function<absl::Status(int, const zelda3::RoomObject&)>
193 void DrawObjectContextMenu(int object_id, int subtype = -1);
198
199 Rom* rom_ = nullptr;
203 std::map<uint32_t, absl::Status> custom_asset_status_cache_;
204
205 // Room data
208
209 // Palette data
211
213
214 // Object preview system
216 bool object_loaded_ = false;
218
219 // Callback for object selection
221 std::function<void()> placement_invalidated_callback_;
222
223 // Object selection state
225
226 // UI state for object browser filter
228 int object_stream_filter_ = 0; // 0=All, 1=Type1, 2=Type2, 3=Type3
229 bool used_here_only_ = false;
230 int object_grid_density_ = 0; // 0=Compact, 1=Medium, 2=Large
231 char object_search_buffer_[64] = {0};
232
233 // Registry initialization flag
235
236 // Performance: enable/disable graphical preview rendering
238
239 // Preview cache for object selector grid, keyed by object/subtype and the
240 // room graphics context that can change its rendered tiles.
241 // Value: BackgroundBuffer with rendered preview
242 std::map<uint64_t, std::unique_ptr<gfx::BackgroundBuffer>> preview_cache_;
243 std::set<uint64_t> failed_preview_keys_; // cleared with preview_cache_
251
252 std::map<uint32_t, zelda3::ObjectTileLayout> layout_cache_;
253
254 // Bumped by InvalidatePreviewCache() to give tests a way to assert the
255 // cache invalidation contract without poking at the cache directly.
257
258 void RetirePreviewCache();
260 static uint32_t MakeLayoutCacheKey(int object_id, uint8_t preview_size,
261 const zelda3::Room* room);
262 bool GetOrCreatePreview(const zelda3::RoomObject& object,
264};
265
266} // namespace editor
267} // namespace yaze
268
269#endif
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:49
Handles object selection, preview, and editing UI.
void SynchronizePreviewCacheRoomContext(const zelda3::Room &room)
std::function< void()> placement_invalidated_callback_
std::function< bool()> open_minecart_editor_window_callback_
std::string object_type_symbol_for_testing(int object_id)
absl::Status OpenExistingCustomObjectEditor(int16_t object_id, int subtype, int room_id)
std::map< uint32_t, absl::Status > custom_asset_status_cache_
void DrawObjectContextMenu(int object_id, int subtype=-1)
void CalculateObjectDimensions(const zelda3::RoomObject &object, int &width, int &height)
zelda3::DungeonObjectRegistry object_registry_
void SetGameData(zelda3::GameData *game_data)
bool MatchesObjectSearch(int obj_id, const std::string &name, int subtype=-1) const
void set_rooms(DungeonRoomStore *rooms)
std::function< absl::Status(int, const zelda3::RoomObject &) edit_object_tiles_callback_)
void DrawObjectAssetBrowser(float minimum_grid_height=1.0f)
void SetCurrentPaletteGroup(const gfx::PaletteGroup &palette_group)
bool matches_object_filter_for_testing(int obj_id, int filter_type)
std::size_t preview_cache_invalidations_for_testing() const
static bool IsRepresentableChestObjectId(int object_id)
absl::Status GetCustomObjectAssetStatus(int object_id, int subtype)
void SetOpenMinecartEditorWindowCallback(std::function< bool()> callback)
void SelectObject(int obj_id, int subtype=-1)
const zelda3::RoomObject & GetPreviewObject() const
std::function< bool()> open_tile_editor_window_callback_
std::function< void(const zelda3::RoomObject &) object_selected_callback_)
std::map< uint64_t, std::unique_ptr< gfx::BackgroundBuffer > > preview_cache_
void SetOpenTileEditorWindowCallback(std::function< bool()> callback)
void SetPlacementInvalidatedCallback(std::function< void()> callback)
void SetTileEditorPanel(ObjectTileEditorPanel *panel)
zelda3::RoomObject MakePreviewObject(int obj_id) const
std::map< uint32_t, zelda3::ObjectTileLayout > layout_cache_
bool GetOrCreatePreview(const zelda3::RoomObject &object, gfx::BackgroundBuffer **out)
static uint32_t MakeLayoutCacheKey(int object_id, uint8_t preview_size, const zelda3::Room *room)
bool MatchesObjectFilter(int obj_id, int filter_type)
void SetObjectSelectedCallback(std::function< void(const zelda3::RoomObject &)> callback)
bool DrawObjectPreview(const zelda3::RoomObject &object, ImVec2 top_left, ImVec2 box_size)
void SetEditObjectTilesCallback(std::function< absl::Status(int, const zelda3::RoomObject &)> callback)
Panel for editing the tile8 composition of dungeon objects.
Minimal registry for dungeon objects (vanilla or custom).
bool MatchesDungeonObjectStreamFilter(int object_id, int selected_filter)
bool IsMinecartGraphicsRuntimeSlot(int object_id, int subtype)
bool IsDungeonCustomObjectRuntimeSlot(int object_id, int subtype)
DungeonObjectPreviewFit ResolveDungeonObjectPreviewFit(float source_width, float source_height, float box_width, float box_height)
std::string GetDungeonCustomObjectSlotName(int object_id, int subtype)
DungeonObjectSelectorGridLayout ResolveDungeonObjectSelectorGridLayout(float available_width, float preferred_item_size, float item_spacing, float min_item_size)
Lightweight view into the essential runtime context (Rom + GameData)
Definition editor.h:73
zelda3::GameData * game_data
Definition editor.h:75
Represents a group of palettes.