From 56559f2a3b5d37905d3add74d6e70489ffe03512 Mon Sep 17 00:00:00 2001 From: Daylily-Zeleen <735170336@qq.com> Date: Sun, 28 Apr 2024 17:22:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A5utf8=E5=BD=A2=E5=BC=8F=E5=A4=84?= =?UTF-8?q?=E7=90=86DragonBones=E7=9B=B8=E5=85=B3=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build_version.py | 2 +- src/dragonbones.cpp | 16 +++++----- src/dragonbones.h | 2 +- src/dragonbones_armature.cpp | 62 ++++++++++++++++++------------------ src/dragonbones_armature.h | 4 +-- src/dragonbones_bone.cpp | 2 +- src/dragonbones_factory.cpp | 16 +++++----- src/dragonbones_slot.cpp | 4 +-- src/wrappers/GDDisplay.h | 10 +++++- 9 files changed, 63 insertions(+), 55 deletions(-) diff --git a/build_version.py b/build_version.py index a53ad2c..ae905a8 100644 --- a/build_version.py +++ b/build_version.py @@ -1,6 +1,6 @@ #!/usr/bin/env python import sys -version = "v0.2.1" +version = "v0.2.2-dev" print("BUILD_VERSION=" + version) diff --git a/src/dragonbones.cpp b/src/dragonbones.cpp index 0bb6ed8..5281495 100644 --- a/src/dragonbones.cpp +++ b/src/dragonbones.cpp @@ -48,8 +48,8 @@ void DragonBones::dispatch_sound_event(const String &_str_type, const dragonBone if (_str_type == EventObject::SOUND_EVENT) { DragonBonesArmature *armature_proxy = static_cast(_p_value->getArmature()->getDisplay()); - String anim_name = _p_value->animationState->name.c_str(); - String event_name = _p_value->name.c_str(); + String anim_name = to_gd_str(_p_value->animationState->name); + String event_name = to_gd_str(_p_value->name); Ref user_data{ memnew(DragonBonesUserData(_p_value->getData())) }; emit_signal("sound_event", armature_proxy, anim_name, event_name, user_data); } @@ -62,7 +62,7 @@ void DragonBones::dispatch_event(const String &_str_type, const dragonBones::Eve } DragonBonesArmature *armature_proxy = static_cast(_p_value->getArmature()->getDisplay()); - String anim_name = _p_value->animationState->name.c_str(); + String anim_name = to_gd_str(_p_value->animationState->name); if (_str_type == EventObject::START) { emit_signal("start", armature_proxy, anim_name); @@ -79,7 +79,7 @@ void DragonBones::dispatch_event(const String &_str_type, const dragonBones::Eve } else if (_str_type == EventObject::FADE_OUT_COMPLETE) { emit_signal("fade_out_completed", armature_proxy, anim_name); } else if (_str_type == EventObject::FRAME_EVENT) { - String event_name = _p_value->name.c_str(); + String event_name = to_gd_str(_p_value->name); Ref user_data{ memnew(DragonBonesUserData(_p_value->getData())) }; // TODO:: 是否需要包装 EventObj 与 ActionData? emit_signal("frame_event", armature_proxy, anim_name, event_name, user_data); @@ -409,7 +409,7 @@ void DragonBones::play_from_time(float _f_time) { WARN_DEPRECATED; play(); if (b_playing && p_armature->is_initialized()) { - p_armature->getAnimation()->gotoAndPlayByTime(str_curr_anim.ascii().get_data(), _f_time, c_loop); + p_armature->getAnimation()->gotoAndPlayByTime(to_std_str(str_curr_anim), _f_time, c_loop); } } @@ -417,7 +417,7 @@ void DragonBones::play_from_progress(float _f_progress) { WARN_DEPRECATED; play(); if (b_playing && p_armature->is_initialized()) { - p_armature->getAnimation()->gotoAndPlayByProgress(str_curr_anim.ascii().get_data(), CLAMP(_f_progress, 0, 1.f), c_loop); + p_armature->getAnimation()->gotoAndPlayByProgress(to_std_str(str_curr_anim), CLAMP(_f_progress, 0, 1.f), c_loop); } } @@ -792,7 +792,7 @@ PackedStringArray DragonBonesUserData::get_strings() const { if (user_data->strings.size()) { for (size_t i = 0; i < user_data->strings.size(); ++i) { - ret[i] = user_data->strings[i].c_str(); + ret[i] = to_gd_str(user_data->strings[i]); } } @@ -824,7 +824,7 @@ String DragonBonesUserData::get_string(DragonBonesUserData::v_size_t p_index) co return {}; } ERR_FAIL_INDEX_V(p_index, user_data->strings.size(), {}); - return user_data->strings[p_index].c_str(); + return to_gd_str(user_data->strings[p_index]); } DragonBonesUserData::v_size_t DragonBonesUserData::get_ints_size() const { diff --git a/src/dragonbones.h b/src/dragonbones.h index 4d221ad..f9ed815 100644 --- a/src/dragonbones.h +++ b/src/dragonbones.h @@ -17,7 +17,7 @@ class DragonBones : public GDOwnerNode, public dragonBones::IEventDispatcher { virtual bool hasDBEventListener(const std::string &type) const override { return true; } virtual void dispatchDBEvent(const std::string &type, dragonBones::EventObject *value) override { - dispatch_sound_event(String(type.c_str()), value); + dispatch_sound_event(to_gd_str(type), value); } private: diff --git a/src/dragonbones_armature.cpp b/src/dragonbones_armature.cpp index 00424bb..5a7c62b 100644 --- a/src/dragonbones_armature.cpp +++ b/src/dragonbones_armature.cpp @@ -210,7 +210,7 @@ bool DragonBonesArmature::has_animation(const String &_animation_name) const { return false; } - return getArmature()->getArmatureData()->getAnimation(_animation_name.ascii().get_data()) != nullptr; + return getArmature()->getArmatureData()->getAnimation(to_std_str(_animation_name)) != nullptr; } PackedStringArray DragonBonesArmature::get_animations() { @@ -219,7 +219,7 @@ PackedStringArray DragonBonesArmature::get_animations() { const ArmatureData *data = p_armature->getArmatureData(); for (std::string animation_name : data->getAnimationNames()) { - animations.push_back(String(animation_name.c_str())); + animations.push_back(to_gd_str(animation_name)); } return animations; @@ -263,7 +263,7 @@ String DragonBonesArmature::get_current_animation() const { if (!p_armature || !getAnimation()) { return {}; } - return getAnimation()->getLastAnimationName().c_str(); + return to_gd_str(getAnimation()->getLastAnimationName()); } String DragonBonesArmature::get_current_animation_on_layer(int _layer) const { @@ -275,7 +275,7 @@ String DragonBonesArmature::get_current_animation_on_layer(int _layer) const { for (AnimationState *state : states) { if (state->layer == _layer) { - return state->getName().c_str(); + return to_gd_str(state->getName()); } } @@ -289,8 +289,8 @@ String DragonBonesArmature::get_current_animation_in_group(const String &_group_ std::vector states = getAnimation()->getStates(); for (AnimationState *state : states) { - if (state->group == _group_name.ascii().get_data()) { - return state->getName().c_str(); + if (state->group == _group_name.utf8().get_data()) { + return to_gd_str(state->getName()); } } @@ -299,7 +299,7 @@ String DragonBonesArmature::get_current_animation_in_group(const String &_group_ float DragonBonesArmature::tell_animation(const String &_animation_name) const { if (has_animation(_animation_name)) { - AnimationState *animation_state = getAnimation()->getState(_animation_name.ascii().get_data()); + AnimationState *animation_state = getAnimation()->getState(to_std_str(_animation_name)); if (animation_state) { return animation_state->getCurrentTime() / animation_state->getTotalTime(); } @@ -314,7 +314,7 @@ void DragonBonesArmature::seek_animation(const String &_animation_name, float pr if (current_progress == 0 && progress != 0) { current_progress = 1.0f; } - p_armature->getAnimation()->gotoAndStopByProgress(_animation_name.ascii().get_data(), current_progress < 0 ? 1. + current_progress : current_progress); + p_armature->getAnimation()->gotoAndStopByProgress(to_std_str(_animation_name), current_progress < 0 ? 1. + current_progress : current_progress); } } @@ -324,7 +324,7 @@ bool DragonBonesArmature::is_playing() const { void DragonBonesArmature::play(const String &_animation_name, int loop) { if (has_animation(_animation_name)) { - getAnimation()->play(_animation_name.ascii().get_data(), loop); + getAnimation()->play(to_std_str(_animation_name), loop); _set_process(true); } @@ -334,20 +334,20 @@ void DragonBonesArmature::play(const String &_animation_name, int loop) { void DragonBonesArmature::play_from_time(const String &_animation_name, float _f_time, int loop) { if (has_animation(_animation_name)) { play(_animation_name, loop); - getAnimation()->gotoAndPlayByTime(_animation_name.ascii().get_data(), _f_time); + getAnimation()->gotoAndPlayByTime(to_std_str(_animation_name), _f_time); } } void DragonBonesArmature::play_from_progress(const String &_animation_name, float f_progress, int loop) { if (has_animation(_animation_name)) { play(_animation_name, loop); - getAnimation()->gotoAndPlayByProgress(_animation_name.ascii().get_data(), f_progress); + getAnimation()->gotoAndPlayByProgress(to_std_str(_animation_name), f_progress); } } void DragonBonesArmature::stop(const String &_animation_name, bool b_reset, bool p_recursively) { if (getAnimation()) { - getAnimation()->stop(_animation_name.ascii().get_data()); + getAnimation()->stop(to_std_str(_animation_name)); if (b_reset) { reset(); @@ -383,7 +383,7 @@ void DragonBonesArmature::stop_all_animations(bool b_reset, bool p_recursively) void DragonBonesArmature::fade_in(const String &_animation_name, float _time, int _loop, int _layer, const String &_group, AnimFadeOutMode _fade_out_mode) { if (has_animation(_animation_name)) { - getAnimation()->fadeIn(_animation_name.ascii().get_data(), _time, _loop, _layer, _group.ascii().get_data(), (AnimationFadeOutMode)_fade_out_mode); + getAnimation()->fadeIn(to_std_str(_animation_name), _time, _loop, _layer, to_std_str(_group), (AnimationFadeOutMode)_fade_out_mode); _set_process(true); } } @@ -401,21 +401,21 @@ void DragonBonesArmature::reset(bool p_recursively) { } bool DragonBonesArmature::has_slot(const String &_slot_name) const { - return getArmature()->getSlot(_slot_name.ascii().get_data()) != nullptr; + return getArmature()->getSlot(to_std_str(_slot_name)) != nullptr; } Dictionary DragonBonesArmature::get_slots() { Dictionary slots{}; for (auto &slot : _slots) { - slots[slot.first.c_str()] = slot.second; + slots[to_gd_str(slot.first)] = slot.second; } return slots; } Ref DragonBonesArmature::get_slot(const String &_slot_name) { - return _slots[_slot_name.ascii().get_data()]; + return _slots[to_std_str(_slot_name)]; } void DragonBonesArmature::set_slot_display_index(const String &_slot_name, int _index) { @@ -437,7 +437,7 @@ void DragonBonesArmature::set_slot_by_item_name(const String &_slot_name, const // we only want to update the slot if there's a choice if (rawData->size() > 1) { - const char *desired_item = _item_name.ascii().get_data(); + const char *desired_item = _item_name.utf8().get_data(); std::string NONE_STRING("none"); if (NONE_STRING.compare(desired_item) == 0) { @@ -462,7 +462,7 @@ void DragonBonesArmature::set_slot_by_item_name(const String &_slot_name, const void DragonBonesArmature::set_all_slots_by_item_name(const String &_item_name) { for (Slot *slot : getArmature()->getSlots()) { - set_slot_by_item_name(String(slot->getName().c_str()), _item_name); + set_slot_by_item_name(to_gd_str(slot->getName()), _item_name); } } @@ -471,7 +471,7 @@ int DragonBonesArmature::get_slot_display_index(const String &_slot_name) { WARN_PRINT("Slot " + _slot_name + " doesn't exist"); return -1; } - return p_armature->getSlot(_slot_name.ascii().get_data())->getDisplayIndex(); + return p_armature->getSlot(to_std_str(_slot_name))->getDisplayIndex(); } int DragonBonesArmature::get_total_items_in_slot(const String &_slot_name) { @@ -479,7 +479,7 @@ int DragonBonesArmature::get_total_items_in_slot(const String &_slot_name) { WARN_PRINT("Slot " + _slot_name + " doesn't exist"); return -1; } - return p_armature->getSlot(_slot_name.ascii().get_data())->getDisplayList().size(); + return p_armature->getSlot(to_std_str(_slot_name))->getDisplayList().size(); } void DragonBonesArmature::cycle_next_item_in_slot(const String &_slot_name) { @@ -511,7 +511,7 @@ Color DragonBonesArmature::get_slot_display_color_multiplier(const String &_slot WARN_PRINT("Slot " + _slot_name + " doesn't exist"); return Color(-1, -1, -1, -1); } - ColorTransform transform(p_armature->getSlot(_slot_name.ascii().get_data())->_colorTransform); + ColorTransform transform(p_armature->getSlot(to_std_str(_slot_name))->_colorTransform); Color return_color; return_color.r = transform.redMultiplier; @@ -533,7 +533,7 @@ void DragonBonesArmature::set_slot_display_color_multiplier(const String &_slot_ _new_color.blueMultiplier = _color.b; _new_color.alphaMultiplier = _color.a; - p_armature->getSlot(_slot_name.ascii().get_data())->_setColor(_new_color); + p_armature->getSlot(to_std_str(_slot_name))->_setColor(_new_color); } void DragonBonesArmature::set_flip_x(bool p_flip_x, bool p_recursively) { @@ -595,7 +595,7 @@ Dictionary DragonBonesArmature::get_ik_constraints() { Dictionary dict; for (auto &constraint : getArmature()->getArmatureData()->constraints) { - dict[String(constraint.first.c_str())] = Vector2(constraint.second->target->transform.x, constraint.second->target->transform.y); + dict[to_gd_str(constraint.first)] = Vector2(constraint.second->target->transform.x, constraint.second->target->transform.y); } return dict; @@ -603,7 +603,7 @@ Dictionary DragonBonesArmature::get_ik_constraints() { void DragonBonesArmature::set_ik_constraint(const String &name, Vector2 position) { for (dragonBones::Constraint *constraint : getArmature()->_constraints) { - if (constraint->getName() == name.ascii().get_data()) { + if (constraint->getName() == name.utf8().get_data()) { dragonBones::BoneData *target = const_cast(constraint->_constraintData->target); target->transform.x = position.x; target->transform.y = position.y; @@ -617,7 +617,7 @@ void DragonBonesArmature::set_ik_constraint(const String &name, Vector2 position void DragonBonesArmature::set_ik_constraint_bend_positive(const String &name, bool bend_positive) { for (dragonBones::Constraint *constraint : getArmature()->_constraints) { - if (constraint->getName() == name.ascii().get_data()) { + if (constraint->getName() == name.utf8().get_data()) { dragonBones::BoneData *target = const_cast(constraint->_constraintData->target); static_cast(constraint)->_bendPositive = bend_positive; @@ -631,14 +631,14 @@ Dictionary DragonBonesArmature::get_bones() { Dictionary bones{}; for (auto &bone : _bones) { - bones[bone.first.c_str()] = bone.second; + bones[to_gd_str(bone.first)] = bone.second; } return bones; } Ref DragonBonesArmature::get_bone(const String &name) { - return _bones[name.ascii().get_data()]; + return _bones[to_std_str(name)]; } Slot *DragonBonesArmature::getSlot(const std::string &name) const { @@ -943,7 +943,7 @@ void DragonBonesArmature::set_settings(const Dictionary &p_settings) { for (size_t j = 0; j < slot_names.size(); ++i) { const String &slot_name = slot_names[i]; const Dictionary &armature_settings = slot_settings[i]; - auto it = _slots.find(slot_name.ascii().get_data()); + auto it = _slots.find(to_std_str(slot_name)); if (it == _slots.end()) { continue; } @@ -979,7 +979,7 @@ Dictionary DragonBonesArmature::get_settings() const { } auto sub_armature = slot->get_child_armature(); if (sub_armature) { - sub_armatures_setting[slot_name.c_str()] = sub_armature->get_settings(); + sub_armatures_setting[to_gd_str(slot_name)] = sub_armature->get_settings(); } } @@ -1016,7 +1016,7 @@ bool DragonBonesArmatureProxy::_get(const StringName &p_name, Variant &r_val) co } if (p_name == SNAME("armature_name")) { - r_val = static_cast(armature_node)->getArmature()->getName().c_str(); + r_val = to_gd_str(static_cast(armature_node)->getArmature()->getName()); return true; } @@ -1041,7 +1041,7 @@ void DragonBonesArmatureProxy::_get_property_list(List *p_list) co String hint = "[none]"; for (const auto &anim : armature_node->getArmature()->getArmatureData()->getAnimationNames()) { hint += ","; - hint += anim.c_str(); + hint += to_gd_str(anim); } info.hint_string = hint; p_list->push_back(info); diff --git a/src/dragonbones_armature.h b/src/dragonbones_armature.h index 34ebbc6..ef5a583 100644 --- a/src/dragonbones_armature.h +++ b/src/dragonbones_armature.h @@ -76,7 +76,7 @@ class DragonBonesArmature : public GDDisplay, public dragonBones::IArmatureProxy virtual void addDBEventListener(const std::string &_type, const std::function &_listener) override {} virtual void removeDBEventListener(const std::string &_type, const std::function &_listener) override {} virtual void dispatchDBEvent(const std::string &_type, dragonBones::EventObject *_value) override { - this->dispatch_event(String(_type.c_str()), _value); + this->dispatch_event(to_gd_str(_type), _value); } void dbInit(dragonBones::Armature *_p_armature) override; @@ -94,7 +94,7 @@ class DragonBonesArmature : public GDDisplay, public dragonBones::IArmatureProxy void update_material_inheritance_recursively(bool p_inheritance); // - dragonBones::Slot *getSlot(const String &p_name) const { return p_armature->getSlot(p_name.ascii().get_data()); } + dragonBones::Slot *getSlot(const String &p_name) const { return p_armature->getSlot(to_std_str(p_name)); } template >::type *_dummy = nullptr> void for_each_armature(Func &&p_action) { diff --git a/src/dragonbones_bone.cpp b/src/dragonbones_bone.cpp index 67098df..df6fe3b 100644 --- a/src/dragonbones_bone.cpp +++ b/src/dragonbones_bone.cpp @@ -130,7 +130,7 @@ Vector2 DragonBonesBone::get_bone_offset_position() const { String DragonBonesBone::get_name() const { ERR_FAIL_NULL_V(boneData, {}); - return String(boneData->getName().c_str()); + return String::utf8(boneData->getName().c_str()); } Vector2 DragonBonesBone::get_bone_position() const { diff --git a/src/dragonbones_factory.cpp b/src/dragonbones_factory.cpp index 859d73c..f16abe0 100644 --- a/src/dragonbones_factory.cpp +++ b/src/dragonbones_factory.cpp @@ -86,7 +86,7 @@ Armature *DragonBonesFactory::_buildArmature(const BuildArmaturePackage &dataPac } armature->init(dataPackage.armature, armatureDisplay, armatureDisplay, _dragonBones); - armatureDisplay->set_name(armature->getName().c_str()); + armatureDisplay->set_name(to_gd_str(armature->getName())); return armature; } @@ -103,7 +103,7 @@ Slot *DragonBonesFactory::_buildSlot(const BuildArmaturePackage &dataPackage, co const auto proxy = static_cast(slot->getArmature()->getDisplay()); proxy->add_slot(slot->getName(), tree_slot); - wrapperDisplay->set_name(slot->getName().c_str()); + wrapperDisplay->set_name(to_gd_str(slot->getName())); return slot; } @@ -300,7 +300,7 @@ Error DragonBonesFactory::load_texture_atlas_json_file_list(PackedStringArray p_ ERR_CONTINUE_MSG(false, vformat("Unsupport texture atlas file: \"%s\", need \"imagePath\" field.", file_path)); } - data->init(file_path.get_base_dir().path_join(data->imagePath.c_str())); + data->init(file_path.get_base_dir().path_join(to_gd_str(data->imagePath))); } return err; } @@ -318,7 +318,7 @@ void DragonBonesFactory::set_texture_atlas_json_file_list(PackedStringArray p_fi PackedStringArray DragonBonesFactory::get_loaded_dragon_bones_data_name_list() const { PackedStringArray ret; for (auto kv : getAllDragonBonesData()) { - ret.push_back(kv.first.c_str()); + ret.push_back(to_gd_str(kv.first)); } return ret; } @@ -328,7 +328,7 @@ PackedStringArray DragonBonesFactory::get_loaded_dragon_bones_main_skin_name_lis DragonBonesData *dbdata = nullptr; if (!p_daragon_bones_data_name.is_empty()) { - dbdata = getDragonBonesData(p_daragon_bones_data_name.ascii().get_data()); + dbdata = getDragonBonesData(to_std_str(p_daragon_bones_data_name)); } else { if (getAllDragonBonesData().size() > 0) { dbdata = getAllDragonBonesData().begin()->second; @@ -346,7 +346,7 @@ PackedStringArray DragonBonesFactory::get_loaded_dragon_bones_main_skin_name_lis for (const auto &kv : armature_data->skins) { if (kv.second) { - ret.push_back(kv.second->name.c_str()); + ret.push_back(to_gd_str(kv.second->name)); } } return ret; @@ -365,7 +365,7 @@ dragonBones::DragonBones *DragonBonesFactory::create_dragon_bones( if (p_armature_data_name.is_empty()) { dragon_bones_data = dragon_bones_data_list.begin()->second; } else { - dragon_bones_data = getDragonBonesData(p_armature_data_name.ascii().get_data()); + dragon_bones_data = getDragonBonesData(to_std_str(p_armature_data_name)); } ERR_FAIL_NULL_V(dragon_bones_data, nullptr); @@ -377,7 +377,7 @@ dragonBones::DragonBones *DragonBonesFactory::create_dragon_bones( const auto armature_name = dragon_bones_data->getArmatureNames()[0]; building_main_armature = p_main_armature; - p_main_armature = buildArmatureDisplay(armature_name, dragon_bones_data->name, p_skin_name.ascii().get_data()); + p_main_armature = buildArmatureDisplay(armature_name, dragon_bones_data->name, to_std_str(p_skin_name)); building_main_armature = nullptr; return ret; diff --git a/src/dragonbones_slot.cpp b/src/dragonbones_slot.cpp index 0dedd48..1f005f3 100644 --- a/src/dragonbones_slot.cpp +++ b/src/dragonbones_slot.cpp @@ -403,7 +403,7 @@ void DragonBonesSlot::set_display_by_name(const String &_name) { // we only want to update the slot if there's a choice if (rawData->size() > 1) { - const char *desired_item = _name.ascii().get_data(); + const char *desired_item = _name.utf8().get_data(); std::string NONE_STRING("none"); if (NONE_STRING.compare(desired_item) == 0) { @@ -458,7 +458,7 @@ void DragonBonesSlot::previous_display() { String DragonBonesSlot::get_slot_name() { ERR_FAIL_NULL_V(slot, {}); - return slot->getName().c_str(); + return to_gd_str(slot->getName()); } DragonBonesArmature *DragonBonesSlot::get_child_armature() { diff --git a/src/wrappers/GDDisplay.h b/src/wrappers/GDDisplay.h index 28e5d51..94ffc82 100644 --- a/src/wrappers/GDDisplay.h +++ b/src/wrappers/GDDisplay.h @@ -5,10 +5,18 @@ #include "godot_cpp/classes/node2d.hpp" #include "dragonBones/DragonBonesHeaders.h" -DRAGONBONES_HEADERS_H // 无用代码,仅用于消除 IDE 警告 +DRAGONBONES_HEADERS_H; // 无用代码,仅用于消除 IDE 警告 namespace godot { +_FORCE_INLINE_ String to_gd_str(const std::string &p_std_str) { + return String::utf8(p_std_str.c_str()); +} + +_FORCE_INLINE_ std::string to_std_str(const String &p_gd_str) { + return p_gd_str.utf8().get_data(); +} + class GDOwnerNode : public Node2D { public: GDOwnerNode() = default;