yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
asset_browser.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GUI_ASSET_BROWSER_H
2#define YAZE_APP_GUI_ASSET_BROWSER_H
3
4#include <array>
5#include <string>
6
7#include "app/gfx/bitmap.h"
8#include "app/rom.h"
9#include "imgui/imgui.h"
10
11#define IM_MIN(A, B) (((A) < (B)) ? (A) : (B))
12#define IM_MAX(A, B) (((A) >= (B)) ? (A) : (B))
13#define IM_CLAMP(V, MN, MX) ((V) < (MN) ? (MN) : (V) > (MX) ? (MX) : (V))
14
15namespace yaze {
16namespace gui {
17
18// Extra functions to add deletion support to ImGuiSelectionBasicStorage
19struct ExampleSelectionWithDeletion : ImGuiSelectionBasicStorage {
20 // Find which item should be Focused after deletion.
21 // Call _before_ item submission. Retunr an index in the before-deletion item
22 // list, your item loop should call SetKeyboardFocusHere() on it. The
23 // subsequent ApplyDeletionPostLoop() code will use it to apply Selection.
24 // - We cannot provide this logic in core Dear ImGui because we don't have
25 // access to selection data.
26 // - We don't actually manipulate the ImVector<> here, only in
27 // ApplyDeletionPostLoop(), but using similar API for consistency and
28 // flexibility.
29 // - Important: Deletion only works if the underlying ImGuiID for your items
30 // are stable: aka not depend on their index, but on e.g. item id/ptr.
31 // FIXME-MULTISELECT: Doesn't take account of the possibility focus target
32 // will be moved during deletion. Need refocus or scroll offset.
33 int ApplyDeletionPreLoop(ImGuiMultiSelectIO* ms_io, int items_count) {
34 if (Size == 0) return -1;
35
36 // If focused item is not selected...
37 const int focused_idx =
38 (int)ms_io->NavIdItem; // Index of currently focused item
39 if (ms_io->NavIdSelected ==
40 false) // This is merely a shortcut, ==
41 // Contains(adapter->IndexToStorage(items, focused_idx))
42 {
43 ms_io->RangeSrcReset =
44 true; // Request to recover RangeSrc from NavId next frame. Would be
45 // ok to reset even when NavIdSelected==true, but it would take
46 // an extra frame to recover RangeSrc when deleting a selected
47 // item.
48 return focused_idx; // Request to focus same item after deletion.
49 }
50
51 // If focused item is selected: land on first unselected item after focused
52 // item.
53 for (int idx = focused_idx + 1; idx < items_count; idx++)
54 if (!Contains(GetStorageIdFromIndex(idx))) return idx;
55
56 // If focused item is selected: otherwise return last unselected item before
57 // focused item.
58 for (int idx = IM_MIN(focused_idx, items_count) - 1; idx >= 0; idx--)
59 if (!Contains(GetStorageIdFromIndex(idx))) return idx;
60
61 return -1;
62 }
63
64 // Rewrite item list (delete items) + update selection.
65 // - Call after EndMultiSelect()
66 // - We cannot provide this logic in core Dear ImGui because we don't have
67 // access to your items, nor to selection data.
68 template <typename ITEM_TYPE>
69 void ApplyDeletionPostLoop(ImGuiMultiSelectIO* ms_io,
70 ImVector<ITEM_TYPE>& items,
71 int item_curr_idx_to_select) {
72 // Rewrite item list (delete items) + convert old selection index (before
73 // deletion) to new selection index (after selection). If NavId was not part
74 // of selection, we will stay on same item.
75 ImVector<ITEM_TYPE> new_items;
76 new_items.reserve(items.Size - Size);
77 int item_next_idx_to_select = -1;
78 for (int idx = 0; idx < items.Size; idx++) {
79 if (!Contains(GetStorageIdFromIndex(idx)))
80 new_items.push_back(items[idx]);
81 if (item_curr_idx_to_select == idx)
82 item_next_idx_to_select = new_items.Size - 1;
83 }
84 items.swap(new_items);
85
86 // Update selection
87 Clear();
88 if (item_next_idx_to_select != -1 && ms_io->NavIdSelected)
89 SetItemSelected(GetStorageIdFromIndex(item_next_idx_to_select), true);
90 }
91};
92
94 ImGuiID ID;
95 int Type;
96
97 AssetObject(ImGuiID id, int type) {
98 ID = id;
99 Type = type;
100 }
101
102 static const ImGuiTableSortSpecs* s_current_sort_specs;
103
104 static void SortWithSortSpecs(ImGuiTableSortSpecs* sort_specs,
105 AssetObject* items, int items_count) {
106 // Store in variable accessible by the sort function.
107 s_current_sort_specs = sort_specs;
108 if (items_count > 1)
109 qsort(items, (size_t)items_count, sizeof(items[0]),
112 }
113
114 // Compare function to be used by qsort()
115 static int CompareWithSortSpecs(const void* lhs, const void* rhs) {
116 const AssetObject* a = (const AssetObject*)lhs;
117 const AssetObject* b = (const AssetObject*)rhs;
118 for (int n = 0; n < s_current_sort_specs->SpecsCount; n++) {
119 const ImGuiTableColumnSortSpecs* sort_spec =
120 &s_current_sort_specs->Specs[n];
121 int delta = 0;
122 if (sort_spec->ColumnIndex == 0)
123 delta = ((int)a->ID - (int)b->ID);
124 else if (sort_spec->ColumnIndex == 1)
125 delta = (a->Type - b->Type);
126 if (delta > 0)
127 return (sort_spec->SortDirection == ImGuiSortDirection_Ascending) ? +1
128 : -1;
129 if (delta < 0)
130 return (sort_spec->SortDirection == ImGuiSortDirection_Ascending) ? -1
131 : +1;
132 }
133 return ((int)a->ID - (int)b->ID);
134 }
135};
136
137struct UnsortedAsset : public AssetObject {
138 UnsortedAsset(ImGuiID id) : AssetObject(id, 0) {}
139};
140
141struct DungeonAsset : public AssetObject {
142 DungeonAsset(ImGuiID id) : AssetObject(id, 1) {}
143};
144
146 OverworldAsset(ImGuiID id) : AssetObject(id, 2) {}
147};
148
149struct SpriteAsset : public AssetObject {
150 SpriteAsset(ImGuiID id) : AssetObject(id, 3) {}
151};
152
154 // Options
155 bool ShowTypeOverlay = true;
156 bool AllowSorting = true;
158 bool AllowBoxSelect = true;
159 float IconSize = 32.0f;
160 int IconSpacing = 10;
161 // Increase hit-spacing if you want to make it possible to clear or
162 // box-select from gaps. Some spacing is required to able to amend
163 // with Shift+box-select. Value is small in Explorer.
165 bool StretchSpacing = true;
166
167 // State
168 ImVector<AssetObject> Items;
169
170 // (ImGuiSelectionBasicStorage + helper funcs to handle deletion)
172
173 ImGuiID NextItemId = 0; // Unique identifier when creating new items
174 bool RequestDelete = false; // Deferred deletion request
175 bool RequestSort = false; // Deferred sort request
176 // Mouse wheel accumulator to handle smooth wheels better
177 float ZoomWheelAccum = 0.0f;
178
179 // Calculated sizes for layout, output of UpdateLayoutSizes(). Could be locals
180 // but our code is simpler this way.
182 ImVec2 LayoutItemStep; // == LayoutItemSize + LayoutItemSpacing
183 float LayoutItemSpacing = 0.0f;
185 float LayoutOuterPadding = 0.0f;
188 bool Initialized = false;
189
190 void Initialize(const std::array<gfx::Bitmap, kNumGfxSheets>& bmp_manager) {
191 // Load the assets
192 for (int i = 0; i < kNumGfxSheets; i++) {
193 Items.push_back(UnsortedAsset(i));
194 }
195 Initialized = true;
196 }
197
198 void AddItems(int count) {
199 if (Items.Size == 0) NextItemId = 0;
200 Items.reserve(Items.Size + count);
201 for (int n = 0; n < count; n++, NextItemId++)
202 Items.push_back(AssetObject(NextItemId, (NextItemId % 20) < 15 ? 0
203 : (NextItemId % 20) < 18 ? 1
204 : 2));
205 RequestSort = true;
206 }
207 void ClearItems() {
208 Items.clear();
209 Selection.Clear();
210 }
211
212 // Logic would be written in the main code BeginChild() and outputing to local
213 // variables. We extracted it into a function so we can call it easily from
214 // multiple places.
215 void UpdateLayoutSizes(float avail_width) {
216 // Layout: when not stretching: allow extending into right-most spacing.
218 if (StretchSpacing == false)
219 avail_width += floorf(LayoutItemSpacing * 0.5f);
220
221 // Layout: calculate number of icon per line and number of lines
222 LayoutItemSize = ImVec2(floorf(IconSize * 4), floorf(IconSize));
224 IM_MAX((int)(avail_width / (LayoutItemSize.x + LayoutItemSpacing)), 1);
226
227 // Layout: when stretching: allocate remaining space to more spacing. Round
228 // before division, so item_spacing may be non-integer.
231 floorf(avail_width - LayoutItemSize.x * LayoutColumnCount) /
233
237 IM_MAX(floorf(LayoutItemSpacing) - IconHitSpacing, 0.0f);
238 LayoutOuterPadding = floorf(LayoutItemSpacing * 0.5f);
239 }
240
241 void Draw(const std::array<gfx::Bitmap, kNumGfxSheets>& bmp_manager);
242};
243
244} // namespace gui
245
246} // namespace yaze
247
248#endif // YAZE_APP_GUI_ASSET_BROWSER_H
#define IM_MAX(A, B)
#define IM_MIN(A, B)
Main namespace for the application.
Definition controller.cc:18
constexpr uint32_t kNumGfxSheets
Definition rom.h:30
static const ImGuiTableSortSpecs * s_current_sort_specs
static void SortWithSortSpecs(ImGuiTableSortSpecs *sort_specs, AssetObject *items, int items_count)
AssetObject(ImGuiID id, int type)
static int CompareWithSortSpecs(const void *lhs, const void *rhs)
int ApplyDeletionPreLoop(ImGuiMultiSelectIO *ms_io, int items_count)
void ApplyDeletionPostLoop(ImGuiMultiSelectIO *ms_io, ImVector< ITEM_TYPE > &items, int item_curr_idx_to_select)
void Draw(const std::array< gfx::Bitmap, kNumGfxSheets > &bmp_manager)
void UpdateLayoutSizes(float avail_width)
ExampleSelectionWithDeletion Selection
ImVector< AssetObject > Items
void Initialize(const std::array< gfx::Bitmap, kNumGfxSheets > &bmp_manager)