Pass content lists to the item templates as member pointers
The six one-line accessors go.
This commit is contained in:
1 parent
48661b4c2c
commit
69cb240a8d
5 files changed
+30
-57
No files matched your search
+15
-28
@@ -9,21 +9,6 @@
|
|||||||
constexpr int NULL_PROPERTIES_ROWS = 2;
|
constexpr int NULL_PROPERTIES_ROWS = 2;
|
||||||
constexpr int LAYER_PROPERTIES_ROWS = 6;
|
constexpr int LAYER_PROPERTIES_ROWS = 6;
|
||||||
|
|
||||||
static auto& layers_get(Model& model)
|
|
||||||
{
|
|
||||||
return model.content.layers;
|
|
||||||
}
|
|
||||||
|
|
||||||
static auto& nulls_get(Model& model)
|
|
||||||
{
|
|
||||||
return model.content.nulls;
|
|
||||||
}
|
|
||||||
|
|
||||||
static auto& events_get(Model& model)
|
|
||||||
{
|
|
||||||
return model.content.events;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void item_tooltip_draw(Resources& resources, const std::string& name, int id)
|
static void item_tooltip_draw(Resources& resources, const std::string& name, int id)
|
||||||
{
|
{
|
||||||
tooltip_name_draw(resources, name);
|
tooltip_name_draw(resources, name);
|
||||||
@@ -104,9 +89,10 @@ void layer_source_draw(Document& document, Manager::ItemEdit& layer)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The layer/null properties popup: adds a new item (id -1) or replaces the edited one, then selects it.
|
// The layer/null properties popup: adds a new item (id -1) or replaces the edited one, then selects it.
|
||||||
template <class Item, class ItemsGet, class FieldsDraw>
|
template <class Item, class FieldsDraw>
|
||||||
void item_properties_update(Panel& panel, int popupIndex, int rows, SelectionKind kind, StringType nameTooltip,
|
void item_properties_update(Panel& panel, int popupIndex, int rows, SelectionKind kind, StringType nameTooltip,
|
||||||
StringType addEdit, StringType propertiesEdit, ItemsGet items_get, FieldsDraw&& fields_draw)
|
StringType addEdit, StringType propertiesEdit, ContentList<Item> list,
|
||||||
|
FieldsDraw&& fields_draw)
|
||||||
{
|
{
|
||||||
auto& manager = panel.manager;
|
auto& manager = panel.manager;
|
||||||
auto& popup = manager.itemPropertiesPopups[popupIndex];
|
auto& popup = manager.itemPropertiesPopups[popupIndex];
|
||||||
@@ -133,11 +119,10 @@ void item_properties_update(Panel& panel, int popupIndex, int rows, SelectionKin
|
|||||||
else
|
else
|
||||||
item = item_edit_null_get(edit);
|
item = item_edit_null_get(edit);
|
||||||
auto& state = panel.state;
|
auto& state = panel.state;
|
||||||
command_push(panel,
|
command_push(panel, [&state, item, id = edit.id, kind, addEdit, propertiesEdit, list](Document& document) {
|
||||||
[&state, item, id = edit.id, kind, addEdit, propertiesEdit, items_get](Document& document) {
|
|
||||||
auto changedId = id;
|
auto changedId = id;
|
||||||
document.edit_apply(id == -1 ? addEdit : propertiesEdit, [&](Model& model) {
|
document.edit_apply(id == -1 ? addEdit : propertiesEdit, [&](Model& model) {
|
||||||
auto& items = items_get(model);
|
auto& items = (model.content.*list);
|
||||||
if (auto target = item_get(items, id))
|
if (auto target = item_get(items, id))
|
||||||
{
|
{
|
||||||
auto uid = target->uid;
|
auto uid = target->uid;
|
||||||
@@ -165,9 +150,10 @@ void item_properties_update(Panel& panel, int popupIndex, int rows, SelectionKin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Layers and nulls: properties popup, add, remove unused, copy and paste.
|
// Layers and nulls: properties popup, add, remove unused, copy and paste.
|
||||||
template <class Item, class ItemsGet>
|
template <class Item>
|
||||||
void track_items_actions_add(Actions& actions, Panel& panel, SelectionKind kind, ElementType type,
|
void track_items_actions_add(Actions& actions, Panel& panel, SelectionKind kind, ElementType type,
|
||||||
StringType addTooltip, StringType pasteEdit, StringType pasteFailToast, ItemsGet items_get)
|
StringType addTooltip, StringType pasteEdit, StringType pasteFailToast,
|
||||||
|
ContentList<Item> list)
|
||||||
{
|
{
|
||||||
auto selection = panel.document.selected_ids_get(kind);
|
auto selection = panel.document.selected_ids_get(kind);
|
||||||
auto& manager = panel.manager;
|
auto& manager = panel.manager;
|
||||||
@@ -176,7 +162,7 @@ void track_items_actions_add(Actions& actions, Panel& panel, SelectionKind kind,
|
|||||||
[&manager, type, selection]() { manager.item_properties_open(type, *selection.begin()); });
|
[&manager, type, selection]() { manager.item_properties_open(type, *selection.begin()); });
|
||||||
actions.add(ACTION_ADD, {}, [&manager, type]() { manager.item_properties_open(type, -1); }, addTooltip);
|
actions.add(ACTION_ADD, {}, [&manager, type]() { manager.item_properties_open(type, -1); }, addTooltip);
|
||||||
content_remove_action_add(actions, panel, type, kind);
|
content_remove_action_add(actions, panel, type, kind);
|
||||||
items_clipboard_actions_add<Item>(actions, panel, kind, pasteEdit, pasteFailToast, items_get);
|
items_clipboard_actions_add<Item>(actions, panel, kind, pasteEdit, pasteFailToast, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
void layers_update(Panel& panel)
|
void layers_update(Panel& panel)
|
||||||
@@ -188,7 +174,7 @@ void layers_update(Panel& panel)
|
|||||||
Actions actions = {};
|
Actions actions = {};
|
||||||
track_items_actions_add<Layer>(actions, panel, SelectionKind::LAYERS, ElementType::LAYER_ELEMENT,
|
track_items_actions_add<Layer>(actions, panel, SelectionKind::LAYERS, ElementType::LAYER_ELEMENT,
|
||||||
TOOLTIP_ADD_LAYER, EDIT_PASTE_LAYERS, TOAST_DESERIALIZE_LAYERS_FAILED,
|
TOOLTIP_ADD_LAYER, EDIT_PASTE_LAYERS, TOAST_DESERIALIZE_LAYERS_FAILED,
|
||||||
layers_get);
|
&Content::layers);
|
||||||
ListRows rows{.label_get = [&](int id) { return layer_label_get(*item_get(layers, id)); },
|
ListRows rows{.label_get = [&](int id) { return layer_label_get(*item_get(layers, id)); },
|
||||||
.is_invalid = [&](int id) { return document.is_layer_animation_missing(*item_get(layers, id)); },
|
.is_invalid = [&](int id) { return document.is_layer_animation_missing(*item_get(layers, id)); },
|
||||||
.tooltip_draw =
|
.tooltip_draw =
|
||||||
@@ -204,7 +190,8 @@ void layers_update(Panel& panel)
|
|||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
item_properties_update<Layer>(panel, 0, LAYER_PROPERTIES_ROWS, SelectionKind::LAYERS, TOOLTIP_ITEM_NAME,
|
item_properties_update<Layer>(panel, 0, LAYER_PROPERTIES_ROWS, SelectionKind::LAYERS, TOOLTIP_ITEM_NAME,
|
||||||
EDIT_ADD_LAYER, EDIT_SET_LAYER_PROPERTIES, layers_get, [&](Manager::ItemEdit& layer) {
|
EDIT_ADD_LAYER, EDIT_SET_LAYER_PROPERTIES, &Content::layers,
|
||||||
|
[&](Manager::ItemEdit& layer) {
|
||||||
ImGui::SeparatorText(localize.get(LABEL_LAYER_SOURCE));
|
ImGui::SeparatorText(localize.get(LABEL_LAYER_SOURCE));
|
||||||
layer_source_draw(document, layer);
|
layer_source_draw(document, layer);
|
||||||
});
|
});
|
||||||
@@ -218,7 +205,7 @@ void nulls_update(Panel& panel)
|
|||||||
{
|
{
|
||||||
Actions actions = {};
|
Actions actions = {};
|
||||||
track_items_actions_add<Null>(actions, panel, SelectionKind::NULLS, ElementType::NULL_ELEMENT, TOOLTIP_ADD_NULL,
|
track_items_actions_add<Null>(actions, panel, SelectionKind::NULLS, ElementType::NULL_ELEMENT, TOOLTIP_ADD_NULL,
|
||||||
EDIT_PASTE_NULLS, TOAST_DESERIALIZE_NULLS_FAILED, nulls_get);
|
EDIT_PASTE_NULLS, TOAST_DESERIALIZE_NULLS_FAILED, &Content::nulls);
|
||||||
ListRows rows{.label_get =
|
ListRows rows{.label_get =
|
||||||
[&](int id) {
|
[&](int id) {
|
||||||
auto name = item_get(nulls, id)->name;
|
auto name = item_get(nulls, id)->name;
|
||||||
@@ -233,7 +220,7 @@ void nulls_update(Panel& panel)
|
|||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
item_properties_update<Null>(panel, 1, NULL_PROPERTIES_ROWS, SelectionKind::NULLS, TOOLTIP_NULL_NAME, EDIT_ADD_NULL,
|
item_properties_update<Null>(panel, 1, NULL_PROPERTIES_ROWS, SelectionKind::NULLS, TOOLTIP_NULL_NAME, EDIT_ADD_NULL,
|
||||||
EDIT_SET_NULL_PROPERTIES, nulls_get, [](Manager::ItemEdit& null) {
|
EDIT_SET_NULL_PROPERTIES, &Content::nulls, [](Manager::ItemEdit& null) {
|
||||||
checkbox_draw(LABEL_RECT, null.isShowRect, TOOLTIP_NULL_RECT);
|
checkbox_draw(LABEL_RECT, null.isShowRect, TOOLTIP_NULL_RECT);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -268,7 +255,7 @@ void events_update(Panel& panel)
|
|||||||
TOOLTIP_ADD_EVENT);
|
TOOLTIP_ADD_EVENT);
|
||||||
content_remove_action_add(actions, panel, ElementType::EVENT_ELEMENT, SelectionKind::EVENTS);
|
content_remove_action_add(actions, panel, ElementType::EVENT_ELEMENT, SelectionKind::EVENTS);
|
||||||
items_clipboard_actions_add<Event>(actions, panel, SelectionKind::EVENTS, EDIT_PASTE_EVENTS,
|
items_clipboard_actions_add<Event>(actions, panel, SelectionKind::EVENTS, EDIT_PASTE_EVENTS,
|
||||||
TOAST_DESERIALIZE_EVENTS_FAILED, events_get);
|
TOAST_DESERIALIZE_EVENTS_FAILED, &Content::events);
|
||||||
ListRows rows{.label_get = [&](int id) { return item_get(events, id)->name; },
|
ListRows rows{.label_get = [&](int id) { return item_get(events, id)->name; },
|
||||||
.name_get = [&](int id) { return item_get(events, id)->name; },
|
.name_get = [&](int id) { return item_get(events, id)->name; },
|
||||||
.rename =
|
.rename =
|
||||||
|
|||||||
@@ -6,11 +6,6 @@
|
|||||||
|
|
||||||
constexpr int LIBRARY_CARD_LINES = 2;
|
constexpr int LIBRARY_CARD_LINES = 2;
|
||||||
|
|
||||||
static auto& libraries_get(Model& model)
|
|
||||||
{
|
|
||||||
return model.content.libraries;
|
|
||||||
}
|
|
||||||
|
|
||||||
void libraries_update(Panel& panel)
|
void libraries_update(Panel& panel)
|
||||||
{
|
{
|
||||||
auto& [manager, settings, resources, dialog, clipboard, document, state] = panel;
|
auto& [manager, settings, resources, dialog, clipboard, document, state] = panel;
|
||||||
@@ -63,7 +58,7 @@ void libraries_update(Panel& panel)
|
|||||||
},
|
},
|
||||||
TOOLTIP_RELOAD_LIBRARIES);
|
TOOLTIP_RELOAD_LIBRARIES);
|
||||||
items_clipboard_actions_add<Library>(actions, panel, SelectionKind::LIBRARIES, EDIT_PASTE_LIBRARIES,
|
items_clipboard_actions_add<Library>(actions, panel, SelectionKind::LIBRARIES, EDIT_PASTE_LIBRARIES,
|
||||||
TOAST_DESERIALIZE_LIBRARIES_FAILED, libraries_get);
|
TOAST_DESERIALIZE_LIBRARIES_FAILED, &Content::libraries);
|
||||||
|
|
||||||
ListRows rows{.label_get =
|
ListRows rows{.label_get =
|
||||||
[&](int id) {
|
[&](int id) {
|
||||||
|
|||||||
+12
-11
@@ -180,6 +180,9 @@ void onionskin_update(Manager&, Settings&);
|
|||||||
void tools_update(Manager&, Settings&, Resources&, PopupHelper&);
|
void tools_update(Manager&, Settings&, Resources&, PopupHelper&);
|
||||||
void welcome_update(Manager&, Resources&, Dialog&, Taskbar&, Documents&);
|
void welcome_update(Manager&, Resources&, Dialog&, Taskbar&, Documents&);
|
||||||
|
|
||||||
|
// One of a document's content lists, as a member of Content.
|
||||||
|
template <class Item> using ContentList = std::vector<Item> Content::*;
|
||||||
|
|
||||||
template <class Items> std::vector<int> item_ids_get(const Items& items)
|
template <class Items> std::vector<int> item_ids_get(const Items& items)
|
||||||
{
|
{
|
||||||
std::vector<int> ids;
|
std::vector<int> ids;
|
||||||
@@ -199,25 +202,25 @@ template <class Items> void items_copy(Clipboard& clipboard, const Items& items,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Pastes clipboard items into a content list with fresh ids and selects the last one.
|
// Pastes clipboard items into a content list with fresh ids and selects the last one.
|
||||||
template <class Item, class ItemsGet>
|
template <class Item>
|
||||||
void items_paste(const Panel& panel, SelectionKind kind, StringType edit, StringType failToast, ItemsGet items_get)
|
void items_paste(const Panel& panel, SelectionKind kind, StringType edit, StringType failToast, ContentList<Item> list)
|
||||||
{
|
{
|
||||||
auto text = panel.clipboard.get();
|
auto text = panel.clipboard.get();
|
||||||
auto& state = panel.state;
|
auto& state = panel.state;
|
||||||
command_push(panel, [&state, kind, edit, failToast, text, items_get](Document& document) {
|
command_push(panel, [&state, kind, edit, failToast, text, list](Document& document) {
|
||||||
std::string errorString;
|
std::string errorString;
|
||||||
auto pasted = items_from_string<Item>(text, &errorString);
|
auto pasted = items_from_string<Item>(text, &errorString);
|
||||||
if (pasted.empty())
|
if (pasted.empty())
|
||||||
return toast_log(Level::ERROR, failToast, errorString);
|
return toast_log(Level::ERROR, failToast, errorString);
|
||||||
document.edit_apply(edit, [&](Model& model) {
|
document.edit_apply(edit, [&](Model& model) {
|
||||||
auto& items = items_get(model);
|
auto& items = (model.content.*list);
|
||||||
for (auto& item : pasted)
|
for (auto& item : pasted)
|
||||||
{
|
{
|
||||||
item.id = item_next_id_get(items);
|
item.id = item_next_id_get(items);
|
||||||
items.push_back(std::move(item));
|
items.push_back(std::move(item));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
auto id = items_get(document.model).back().id;
|
auto id = (document.model.content.*list).back().id;
|
||||||
document.selected_ids_set(kind, {id});
|
document.selected_ids_set(kind, {id});
|
||||||
document.focused_id_set(kind, id);
|
document.focused_id_set(kind, id);
|
||||||
state.newId = id;
|
state.newId = id;
|
||||||
@@ -225,18 +228,16 @@ void items_paste(const Panel& panel, SelectionKind kind, StringType edit, String
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Copy and paste for a content list, after a separator.
|
// Copy and paste for a content list, after a separator.
|
||||||
template <class Item, class ItemsGet>
|
template <class Item>
|
||||||
void items_clipboard_actions_add(Actions& actions, Panel& panel, SelectionKind kind, StringType pasteEdit,
|
void items_clipboard_actions_add(Actions& actions, Panel& panel, SelectionKind kind, StringType pasteEdit,
|
||||||
StringType failToast, ItemsGet items_get)
|
StringType failToast, ContentList<Item> list)
|
||||||
{
|
{
|
||||||
auto selection = panel.document.selected_ids_get(kind);
|
auto selection = panel.document.selected_ids_get(kind);
|
||||||
actions.separator();
|
actions.separator();
|
||||||
actions.add(
|
actions.add(
|
||||||
ACTION_COPY, [selection]() { return !selection.empty(); },
|
ACTION_COPY, [selection]() { return !selection.empty(); },
|
||||||
[&panel, selection, items_get]() { items_copy(panel.clipboard, items_get(panel.document.model), selection); });
|
[&panel, selection, list]() { items_copy(panel.clipboard, (panel.document.model.content.*list), selection); });
|
||||||
actions.add(
|
actions.add(
|
||||||
ACTION_PASTE, [&panel]() { return !panel.clipboard.is_empty(); },
|
ACTION_PASTE, [&panel]() { return !panel.clipboard.is_empty(); },
|
||||||
[&panel, kind, pasteEdit, failToast, items_get]() {
|
[&panel, kind, pasteEdit, failToast, list]() { items_paste<Item>(panel, kind, pasteEdit, failToast, list); });
|
||||||
items_paste<Item>(panel, kind, pasteEdit, failToast, items_get);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
+1
-6
@@ -6,11 +6,6 @@
|
|||||||
|
|
||||||
constexpr int SOUND_CARD_LINES = 2;
|
constexpr int SOUND_CARD_LINES = 2;
|
||||||
|
|
||||||
static auto& sounds_get(Model& model)
|
|
||||||
{
|
|
||||||
return model.content.sounds;
|
|
||||||
}
|
|
||||||
|
|
||||||
void sounds_update(Panel& panel)
|
void sounds_update(Panel& panel)
|
||||||
{
|
{
|
||||||
auto& [manager, settings, resources, dialog, clipboard, document, state] = panel;
|
auto& [manager, settings, resources, dialog, clipboard, document, state] = panel;
|
||||||
@@ -78,7 +73,7 @@ void sounds_update(Panel& panel)
|
|||||||
TOOLTIP_RELOAD_SOUNDS);
|
TOOLTIP_RELOAD_SOUNDS);
|
||||||
actions.add(ACTION_REPLACE, isOne, [&]() { dialog.file_open(Dialog::SOUND_REPLACE); }, TOOLTIP_REPLACE_SOUND);
|
actions.add(ACTION_REPLACE, isOne, [&]() { dialog.file_open(Dialog::SOUND_REPLACE); }, TOOLTIP_REPLACE_SOUND);
|
||||||
items_clipboard_actions_add<Sound>(actions, panel, SelectionKind::SOUNDS, EDIT_PASTE_SOUNDS,
|
items_clipboard_actions_add<Sound>(actions, panel, SelectionKind::SOUNDS, EDIT_PASTE_SOUNDS,
|
||||||
TOAST_SOUNDS_DESERIALIZE_ERROR, sounds_get);
|
TOAST_SOUNDS_DESERIALIZE_ERROR, &Content::sounds);
|
||||||
|
|
||||||
ListRows rows{.label_get =
|
ListRows rows{.label_get =
|
||||||
[&](int id) {
|
[&](int id) {
|
||||||
|
|||||||
@@ -13,11 +13,6 @@ constexpr int PADDING_MAX = 100;
|
|||||||
// The merge region origin setting indexes this (top-left or center).
|
// The merge region origin setting indexes this (top-left or center).
|
||||||
constexpr Origin MERGE_REGION_ORIGINS[] = {Origin::TOP_LEFT, Origin::CENTER};
|
constexpr Origin MERGE_REGION_ORIGINS[] = {Origin::TOP_LEFT, Origin::CENTER};
|
||||||
|
|
||||||
static auto& spritesheets_get(Model& model)
|
|
||||||
{
|
|
||||||
return model.content.spritesheets;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const Spritesheet* spritesheet_get(Document& document, int id)
|
static const Spritesheet* spritesheet_get(Document& document, int id)
|
||||||
{
|
{
|
||||||
return item_get(document.model.content.spritesheets, id);
|
return item_get(document.model.content.spritesheets, id);
|
||||||
@@ -292,7 +287,7 @@ void spritesheets_update(Panel& panel, SpritesheetsPanel& spritesheets)
|
|||||||
},
|
},
|
||||||
TOOLTIP_SAVE_SPRITESHEETS);
|
TOOLTIP_SAVE_SPRITESHEETS);
|
||||||
items_clipboard_actions_add<Spritesheet>(actions, panel, SelectionKind::SPRITESHEETS, EDIT_PASTE_SPRITESHEETS,
|
items_clipboard_actions_add<Spritesheet>(actions, panel, SelectionKind::SPRITESHEETS, EDIT_PASTE_SPRITESHEETS,
|
||||||
TOAST_DESERIALIZE_SPRITESHEETS_FAILED, spritesheets_get);
|
TOAST_DESERIALIZE_SPRITESHEETS_FAILED, &Content::spritesheets);
|
||||||
|
|
||||||
auto image_get = [&](int id) {
|
auto image_get = [&](int id) {
|
||||||
auto texture = document.texture_get(id);
|
auto texture = document.texture_get(id);
|
||||||
|
|||||||
Reference in new issue
Block a user