yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
asset_browser.cc
Go to the documentation of this file.
1#include "asset_browser.h"
2
3#include "absl/strings/str_format.h"
4
5namespace yaze {
6namespace gui {
7
8using namespace ImGui;
9
10const ImGuiTableSortSpecs* AssetObject::s_current_sort_specs = NULL;
11
13 const std::array<gfx::Bitmap, kNumGfxSheets>& bmp_manager) {
14 PushItemWidth(GetFontSize() * 10);
15 SeparatorText("Contents");
16 Checkbox("Show Type Overlay", &ShowTypeOverlay);
17 SameLine();
18 Checkbox("Allow Sorting", &AllowSorting);
19 SameLine();
20 Checkbox("Stretch Spacing", &StretchSpacing);
21 SameLine();
22 Checkbox("Allow dragging unselected item", &AllowDragUnselected);
23 SameLine();
24 Checkbox("Allow box-selection", &AllowBoxSelect);
25 SameLine();
26 SliderFloat("Icon Size", &IconSize, 16.0f, 128.0f, "%.0f");
27 SameLine();
28 SliderInt("Icon Spacing", &IconSpacing, 0, 32);
29 SameLine();
30 SliderInt("Icon Hit Spacing", &IconHitSpacing, 0, 32);
31 PopItemWidth();
32
33 // Filter by types
34 static bool filter_type[4] = {true, true, true, true};
35 Text("Filter by type:");
36 SameLine();
37 Checkbox("Unsorted", &filter_type[0]);
38 SameLine();
39 Checkbox("Dungeon", &filter_type[1]);
40 SameLine();
41 Checkbox("Overworld", &filter_type[2]);
42 SameLine();
43 Checkbox("Sprite", &filter_type[3]);
44
45 // Show a table with ONLY one header row to showcase the idea/possibility of
46 // using this to provide a sorting UI
47 if (AllowSorting) {
48 PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
49 ImGuiTableFlags table_flags_for_sort_specs =
50 ImGuiTableFlags_Sortable | ImGuiTableFlags_SortMulti |
51 ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Borders;
52 if (BeginTable("for_sort_specs_only", 2, table_flags_for_sort_specs,
53 ImVec2(0.0f, GetFrameHeight()))) {
54 TableSetupColumn("Index");
55 TableSetupColumn("Type");
56 TableHeadersRow();
57 if (ImGuiTableSortSpecs* sort_specs = TableGetSortSpecs())
58 if (sort_specs->SpecsDirty || RequestSort) {
59 AssetObject::SortWithSortSpecs(sort_specs, Items.Data, Items.Size);
60 sort_specs->SpecsDirty = RequestSort = false;
61 }
62 EndTable();
63 }
64 PopStyleVar();
65 }
66
67 ImGuiIO& io = GetIO();
68 SetNextWindowContentSize(ImVec2(
69 0.0f, LayoutOuterPadding +
71 if (BeginChild("Assets", ImVec2(0.0f, -GetTextLineHeightWithSpacing()),
72 ImGuiChildFlags_Border, ImGuiWindowFlags_NoMove)) {
73 ImDrawList* draw_list = GetWindowDrawList();
74
75 const float avail_width = GetContentRegionAvail().x;
76 UpdateLayoutSizes(avail_width);
77
78 // Calculate and store start position.
79 ImVec2 start_pos = GetCursorScreenPos();
80 start_pos = ImVec2(start_pos.x + LayoutOuterPadding,
81 start_pos.y + LayoutOuterPadding);
82 SetCursorScreenPos(start_pos);
83
84 // Multi-select
85 ImGuiMultiSelectFlags ms_flags = ImGuiMultiSelectFlags_ClearOnEscape |
86 ImGuiMultiSelectFlags_ClearOnClickVoid;
87
88 // - Enable box-select (in 2D mode, so that changing box-select rectangle
89 // X1/X2 boundaries will affect clipped items)
90 if (AllowBoxSelect) ms_flags |= ImGuiMultiSelectFlags_BoxSelect2d;
91
92 // - This feature allows dragging an unselected item without selecting it
93 // (rarely used)
95 ms_flags |= ImGuiMultiSelectFlags_SelectOnClickRelease;
96
97 // - Enable keyboard wrapping on X axis
98 // (FIXME-MULTISELECT: We haven't designed/exposed a general nav wrapping
99 // api yet, so this flag is provided as a courtesy to avoid doing:
100 // NavMoveRequestTryWrapping(GetCurrentWindow(),
101 // ImGuiNavMoveFlags_WrapX);
102 // When we finish implementing a more general API for this, we will
103 // obsolete this flag in favor of the new system)
104 ms_flags |= ImGuiMultiSelectFlags_NavWrapX;
105
106 ImGuiMultiSelectIO* ms_io =
107 BeginMultiSelect(ms_flags, Selection.Size, Items.Size);
108
109 // Use custom selection adapter: store ID in selection (recommended)
110 Selection.UserData = this;
111 Selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self_,
112 int idx) {
113 GfxSheetAssetBrowser* self = (GfxSheetAssetBrowser*)self_->UserData;
114 return self->Items[idx].ID;
115 };
116 Selection.ApplyRequests(ms_io);
117
118 const bool want_delete =
119 (Shortcut(ImGuiKey_Delete, ImGuiInputFlags_Repeat) &&
120 (Selection.Size > 0)) ||
122 const int item_curr_idx_to_focus =
123 want_delete ? Selection.ApplyDeletionPreLoop(ms_io, Items.Size) : -1;
124 RequestDelete = false;
125
126 // Push LayoutSelectableSpacing (which is LayoutItemSpacing minus
127 // hit-spacing, if we decide to have hit gaps between items) Altering
128 // style ItemSpacing may seem unnecessary as we position every items using
129 // SetCursorScreenPos()... But it is necessary for two reasons:
130 // - Selectables uses it by default to visually fill the space between two
131 // items.
132 // - The vertical spacing would be measured by Clipper to calculate line
133 // height if we didn't provide it explicitly (here we do).
134 PushStyleVar(ImGuiStyleVar_ItemSpacing,
136
137 // Rendering parameters
138 const ImU32 icon_type_overlay_colors[5] = {
139 0, IM_COL32(200, 70, 70, 255), IM_COL32(70, 170, 70, 255),
140 IM_COL32(70, 70, 200, 255), IM_COL32(200, 200, 200, 255)};
141 const ImU32 icon_bg_color = GetColorU32(ImGuiCol_MenuBarBg);
142 const ImVec2 icon_type_overlay_size = ImVec2(5.0f, 5.0f);
143 const bool display_label = (LayoutItemSize.x >= CalcTextSize("999").x);
144
145 const int column_count = LayoutColumnCount;
146 ImGuiListClipper clipper;
147 clipper.Begin(LayoutLineCount, LayoutItemStep.y);
148
149 // Ensure focused item line is not clipped.
150 if (item_curr_idx_to_focus != -1)
151 clipper.IncludeItemByIndex(item_curr_idx_to_focus / column_count);
152
153 // Ensure RangeSrc item line is not clipped.
154 if (ms_io->RangeSrcItem != -1)
155 clipper.IncludeItemByIndex((int)ms_io->RangeSrcItem / column_count);
156
157 while (clipper.Step()) {
158 for (int line_idx = clipper.DisplayStart; line_idx < clipper.DisplayEnd;
159 line_idx++) {
160 const int item_min_idx_for_current_line = line_idx * column_count;
161 const int item_max_idx_for_current_line =
162 IM_MIN((line_idx + 1) * column_count, Items.Size);
163 for (int item_idx = item_min_idx_for_current_line;
164 item_idx < item_max_idx_for_current_line; ++item_idx) {
165 AssetObject* item_data = &Items[item_idx];
166 PushID((int)item_data->ID);
167
168 // Position item
169 ImVec2 pos =
170 ImVec2(start_pos.x + (item_idx % column_count) * LayoutItemStep.x,
171 start_pos.y + line_idx * LayoutItemStep.y);
172 SetCursorScreenPos(pos);
173
174 SetNextItemSelectionUserData(item_idx);
175 bool item_is_selected = Selection.Contains((ImGuiID)item_data->ID);
176 bool item_is_visible = IsRectVisible(LayoutItemSize);
177 Selectable("", item_is_selected, ImGuiSelectableFlags_None,
179
180 // Update our selection state immediately (without waiting for
181 // EndMultiSelect() requests) because we use this to alter the color
182 // of our text/icon.
183 if (IsItemToggledSelection()) item_is_selected = !item_is_selected;
184
185 // Focus (for after deletion)
186 if (item_curr_idx_to_focus == item_idx) SetKeyboardFocusHere(-1);
187
188 // Drag and drop
189 if (BeginDragDropSource()) {
190 // Create payload with full selection OR single unselected item.
191 // (the later is only possible when using
192 // ImGuiMultiSelectFlags_SelectOnClickRelease)
193 if (GetDragDropPayload() == NULL) {
194 ImVector<ImGuiID> payload_items;
195 void* it = NULL;
196 ImGuiID id = 0;
197 if (!item_is_selected)
198 payload_items.push_back(item_data->ID);
199 else
200 while (Selection.GetNextSelectedItem(&it, &id))
201 payload_items.push_back(id);
202 SetDragDropPayload("ASSETS_BROWSER_ITEMS", payload_items.Data,
203 (size_t)payload_items.size_in_bytes());
204 }
205
206 // Display payload content in tooltip, by extracting it from the
207 // payload data (we could read from selection, but it is more
208 // correct and reusable to read from payload)
209 const ImGuiPayload* payload = GetDragDropPayload();
210 const int payload_count =
211 (int)payload->DataSize / (int)sizeof(ImGuiID);
212 Text("%d assets", payload_count);
213
214 EndDragDropSource();
215 }
216
217 // Render icon (a real app would likely display an image/thumbnail
218 // here) Because we use ImGuiMultiSelectFlags_BoxSelect2d, clipping
219 // vertical may occasionally be larger, so we coarse-clip our
220 // rendering as well.
221 if (item_is_visible) {
222 ImVec2 box_min(pos.x - 1, pos.y - 1);
223 ImVec2 box_max(box_min.x + LayoutItemSize.x + 2,
224 box_min.y + LayoutItemSize.y + 2); // Dubious
225 draw_list->AddRectFilled(box_min, box_max,
226 icon_bg_color); // Background color
227
228 if (display_label) {
229 ImU32 label_col = GetColorU32(
230 item_is_selected ? ImGuiCol_Text : ImGuiCol_TextDisabled);
231 draw_list->AddImage(
232 (ImTextureID)(intptr_t)bmp_manager[item_data->ID].texture(),
233 box_min, box_max, ImVec2(0, 0), ImVec2(1, 1),
234 GetColorU32(ImVec4(1, 1, 1, 1)));
235 draw_list->AddText(ImVec2(box_min.x, box_max.y - GetFontSize()),
236 label_col,
237 absl::StrFormat("%X", item_data->ID).c_str());
238 }
239 if (ShowTypeOverlay && item_data->Type != 0) {
240 ImU32 type_col = icon_type_overlay_colors
241 [item_data->Type % IM_ARRAYSIZE(icon_type_overlay_colors)];
242 draw_list->AddRectFilled(
243 ImVec2(box_max.x - 2 - icon_type_overlay_size.x,
244 box_min.y + 2),
245 ImVec2(box_max.x - 2,
246 box_min.y + 2 + icon_type_overlay_size.y),
247 type_col);
248 }
249 }
250
251 PopID();
252 }
253 }
254 }
255 clipper.End();
256 PopStyleVar(); // ImGuiStyleVar_ItemSpacing
257
258 // Context menu
259 if (BeginPopupContextWindow()) {
260 Text("Selection: %d items", Selection.Size);
261 Separator();
262 if (BeginMenu("Set Type")) {
263 if (MenuItem("Unsorted")) {
264 void* it = NULL;
265 ImGuiID id = 0;
266 while (Selection.GetNextSelectedItem(&it, &id)) Items[id].Type = 0;
267 }
268 if (MenuItem("Dungeon")) {
269 void* it = NULL;
270 ImGuiID id = 0;
271 while (Selection.GetNextSelectedItem(&it, &id)) Items[id].Type = 1;
272 }
273 if (MenuItem("Overworld")) {
274 void* it = NULL;
275 ImGuiID id = 0;
276 while (Selection.GetNextSelectedItem(&it, &id)) Items[id].Type = 2;
277 }
278 if (MenuItem("Sprite")) {
279 void* it = NULL;
280 ImGuiID id = 0;
281 while (Selection.GetNextSelectedItem(&it, &id)) Items[id].Type = 3;
282 }
283 EndMenu();
284 }
285 Separator();
286 if (MenuItem("Delete", "Del", false, Selection.Size > 0))
287 RequestDelete = true;
288 EndPopup();
289 }
290
291 ms_io = EndMultiSelect();
292 Selection.ApplyRequests(ms_io);
293 if (want_delete)
294 Selection.ApplyDeletionPostLoop(ms_io, Items, item_curr_idx_to_focus);
295
296 // Zooming with CTRL+Wheel
297 if (IsWindowAppearing()) ZoomWheelAccum = 0.0f;
298 if (IsWindowHovered() && io.MouseWheel != 0.0f &&
299 IsKeyDown(ImGuiMod_Ctrl) && IsAnyItemActive() == false) {
300 ZoomWheelAccum += io.MouseWheel;
301 if (fabsf(ZoomWheelAccum) >= 1.0f) {
302 // Calculate hovered item index from mouse location
303 // FIXME: Locking aiming on 'hovered_item_idx' (with a cool-down
304 // timer) would ensure zoom keeps on it.
305 const float hovered_item_nx =
306 (io.MousePos.x - start_pos.x + LayoutItemSpacing * 0.5f) /
308 const float hovered_item_ny =
309 (io.MousePos.y - start_pos.y + LayoutItemSpacing * 0.5f) /
311 const int hovered_item_idx =
312 ((int)hovered_item_ny * LayoutColumnCount) + (int)hovered_item_nx;
313 // SetTooltip("%f,%f -> item %d", hovered_item_nx,
314 // hovered_item_ny, hovered_item_idx); // Move those 4 lines in block
315 // above for easy debugging
316
317 // Zoom
318 IconSize *= powf(1.1f, (float)(int)ZoomWheelAccum);
319 IconSize = IM_CLAMP(IconSize, 16.0f, 128.0f);
321 UpdateLayoutSizes(avail_width);
322
323 // Manipulate scroll to that we will land at the same Y location of
324 // currently hovered item.
325 // - Calculate next frame position of item under mouse
326 // - Set new scroll position to be used in next BeginChild()
327 // call.
328 float hovered_item_rel_pos_y =
329 ((float)(hovered_item_idx / LayoutColumnCount) +
330 fmodf(hovered_item_ny, 1.0f)) *
332 hovered_item_rel_pos_y += GetStyle().WindowPadding.y;
333 float mouse_local_y = io.MousePos.y - GetWindowPos().y;
334 SetScrollY(hovered_item_rel_pos_y - mouse_local_y);
335 }
336 }
337 }
338 EndChild();
339
340 Text("Selected: %d/%d items", Selection.Size, Items.Size);
341}
342
343} // namespace gui
344
345} // namespace yaze
#define IM_CLAMP(V, MN, MX)
#define IM_MIN(A, B)
Graphical User Interface (GUI) components for the application.
Definition canvas.cc:15
Main namespace for the application.
Definition controller.cc:18
static const ImGuiTableSortSpecs * s_current_sort_specs
static void SortWithSortSpecs(ImGuiTableSortSpecs *sort_specs, AssetObject *items, int items_count)
void Draw(const std::array< gfx::Bitmap, kNumGfxSheets > &bmp_manager)
void UpdateLayoutSizes(float avail_width)
ExampleSelectionWithDeletion Selection
ImVector< AssetObject > Items