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