Change clipboard system entirely, refactored anm2 serializing/deserializing, quick shell script for atlas update
This commit is contained in:
@@ -1,94 +1,101 @@
|
||||
#include "clipboard.h"
|
||||
|
||||
static void _clipboard_item_remove(ClipboardItem* self, Anm2* anm2)
|
||||
{
|
||||
switch (self->type)
|
||||
{
|
||||
case CLIPBOARD_FRAME:
|
||||
{
|
||||
Anm2FrameWithReference* frameWithReference = std::get_if<Anm2FrameWithReference>(&self->data);
|
||||
if (!frameWithReference) break;
|
||||
|
||||
anm2_frame_erase(anm2, &frameWithReference->reference);
|
||||
break;
|
||||
}
|
||||
case CLIPBOARD_ANIMATION:
|
||||
{
|
||||
Anm2AnimationWithID* animationWithID = std::get_if<Anm2AnimationWithID>(&self->data);
|
||||
if (!animationWithID) break;
|
||||
|
||||
for (auto & [id, animation] : anm2->animations)
|
||||
{
|
||||
if (id == animationWithID->id)
|
||||
{
|
||||
anm2->animations.erase(animationWithID->id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void _clipboard_item_paste(ClipboardItem* self, ClipboardLocation* location, Anm2* anm2)
|
||||
{
|
||||
switch (self->type)
|
||||
{
|
||||
case CLIPBOARD_FRAME:
|
||||
{
|
||||
Anm2FrameWithReference* frameWithReference = std::get_if<Anm2FrameWithReference>(&self->data);
|
||||
Anm2Reference* reference = std::get_if<Anm2Reference>(location);
|
||||
|
||||
if (!frameWithReference || !reference) break;
|
||||
if (frameWithReference->reference.itemType != reference->itemType) break;
|
||||
|
||||
Anm2Animation* animation = anm2_animation_from_reference(anm2, reference);
|
||||
Anm2Item* anm2Item = anm2_item_from_reference(anm2, reference);
|
||||
|
||||
if (!animation || !anm2Item) break;
|
||||
|
||||
anm2_frame_add(anm2, &frameWithReference->frame, reference, reference->frameIndex);
|
||||
|
||||
break;
|
||||
}
|
||||
case CLIPBOARD_ANIMATION:
|
||||
{
|
||||
Anm2AnimationWithID* animationWithID = std::get_if<Anm2AnimationWithID>(&self->data);
|
||||
if (!animationWithID) break;
|
||||
|
||||
s32 index = 0;
|
||||
|
||||
if (std::holds_alternative<s32>(*location))
|
||||
index = std::get<s32>(*location);
|
||||
else
|
||||
break;
|
||||
|
||||
index = std::clamp(index, 0, (s32)anm2->animations.size());
|
||||
|
||||
map_insert_shift(anm2->animations, index, animationWithID->animation);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void clipboard_copy(Clipboard* self)
|
||||
{
|
||||
self->item = self->hoveredItem;
|
||||
std::string clipboardText{};
|
||||
|
||||
auto clipboard_text_set = [&]()
|
||||
{
|
||||
if (!SDL_SetClipboardText(clipboardText.c_str()))
|
||||
log_warning(std::format(CLIPBOARD_TEXT_SET_WARNING, SDL_GetError()));
|
||||
};
|
||||
|
||||
switch (self->type)
|
||||
{
|
||||
case CLIPBOARD_FRAME:
|
||||
{
|
||||
Anm2Reference* reference = std::get_if<Anm2Reference>(&self->location);
|
||||
if (!reference) break;
|
||||
Anm2Frame* frame = anm2_frame_from_reference(self->anm2, reference);
|
||||
if (!frame) break;
|
||||
anm2_frame_serialize(frame, reference->itemType, nullptr, nullptr, &clipboardText);
|
||||
clipboard_text_set();
|
||||
break;
|
||||
}
|
||||
case CLIPBOARD_ANIMATION:
|
||||
{
|
||||
s32* id = std::get_if<s32>(&self->location);
|
||||
if (!id) break;
|
||||
Anm2Animation* animation = map_find(self->anm2->animations, *id);
|
||||
if (!animation) break;
|
||||
anm2_animation_serialize(self->anm2, animation, nullptr, nullptr, &clipboardText);
|
||||
clipboard_text_set();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void clipboard_cut(Clipboard* self)
|
||||
{
|
||||
self->item = self->hoveredItem;
|
||||
_clipboard_item_remove(&self->item, self->anm2);
|
||||
clipboard_copy(self);
|
||||
|
||||
switch (self->type)
|
||||
{
|
||||
case CLIPBOARD_FRAME:
|
||||
{
|
||||
Anm2Reference* reference = std::get_if<Anm2Reference>(&self->location);
|
||||
if (!reference) break;
|
||||
anm2_frame_remove(self->anm2, reference);
|
||||
break;
|
||||
}
|
||||
case CLIPBOARD_ANIMATION:
|
||||
{
|
||||
s32* id = std::get_if<s32>(&self->location);
|
||||
if (!id) break;
|
||||
anm2_animation_remove(self->anm2, *id);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void clipboard_paste(Clipboard* self)
|
||||
{
|
||||
_clipboard_item_paste(&self->item, &self->location, self->anm2);
|
||||
auto clipboard_string = [&]()
|
||||
{
|
||||
char* clipboardText = SDL_GetClipboardText();
|
||||
std::string clipboardString = std::string(clipboardText);
|
||||
SDL_free(clipboardText);
|
||||
return clipboardString;
|
||||
};
|
||||
|
||||
switch (self->type)
|
||||
{
|
||||
case CLIPBOARD_FRAME:
|
||||
{
|
||||
Anm2Reference* reference = std::get_if<Anm2Reference>(&self->location);
|
||||
if (!reference) break;
|
||||
Anm2Frame frame;
|
||||
if (anm2_frame_deserialize_from_xml(self->anm2, &frame, clipboard_string()))
|
||||
anm2_frame_add(self->anm2, &frame, reference);
|
||||
break;
|
||||
}
|
||||
case CLIPBOARD_ANIMATION:
|
||||
{
|
||||
s32* id = std::get_if<s32>(&self->location);
|
||||
if (!id) break;
|
||||
Anm2Animation animation;
|
||||
if (anm2_animation_deserialize_from_xml(self->anm2, &animation, clipboard_string()))
|
||||
anm2_animation_add(self->anm2, false, &animation, *id);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void clipboard_init(Clipboard* self, Anm2* anm2)
|
||||
@@ -96,3 +103,7 @@ void clipboard_init(Clipboard* self, Anm2* anm2)
|
||||
self->anm2 = anm2;
|
||||
}
|
||||
|
||||
bool clipboard_is_value(void)
|
||||
{
|
||||
return SDL_HasClipboardText();
|
||||
}
|
Reference in New Issue
Block a user