Skip to content

Commit

Permalink
以utf8形式处理DragonBones相关的字符串
Browse files Browse the repository at this point in the history
  • Loading branch information
Daylily-Zeleen committed Apr 28, 2024
1 parent 8467612 commit 56559f2
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 55 deletions.
2 changes: 1 addition & 1 deletion build_version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
import sys

version = "v0.2.1"
version = "v0.2.2-dev"

print("BUILD_VERSION=" + version)
16 changes: 8 additions & 8 deletions src/dragonbones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DragonBonesArmature *>(_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<DragonBonesUserData> user_data{ memnew(DragonBonesUserData(_p_value->getData())) };
emit_signal("sound_event", armature_proxy, anim_name, event_name, user_data);
}
Expand All @@ -62,7 +62,7 @@ void DragonBones::dispatch_event(const String &_str_type, const dragonBones::Eve
}

DragonBonesArmature *armature_proxy = static_cast<DragonBonesArmature *>(_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);
Expand All @@ -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<DragonBonesUserData> user_data{ memnew(DragonBonesUserData(_p_value->getData())) };
// TODO:: 是否需要包装 EventObj 与 ActionData?
emit_signal("frame_event", armature_proxy, anim_name, event_name, user_data);
Expand Down Expand Up @@ -409,15 +409,15 @@ 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);
}
}

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);
}
}

Expand Down Expand Up @@ -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]);
}
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/dragonbones.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
62 changes: 31 additions & 31 deletions src/dragonbones_armature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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());
}
}

Expand All @@ -289,8 +289,8 @@ String DragonBonesArmature::get_current_animation_in_group(const String &_group_
std::vector<AnimationState *> 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());
}
}

Expand All @@ -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();
}
Expand All @@ -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);
}
}

Expand All @@ -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);
}
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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<DragonBonesSlot> 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) {
Expand All @@ -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) {
Expand All @@ -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);
}
}

Expand All @@ -471,15 +471,15 @@ 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) {
if (!has_slot(_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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -595,15 +595,15 @@ 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;
}

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<BoneData *>(constraint->_constraintData->target);
target->transform.x = position.x;
target->transform.y = position.y;
Expand All @@ -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<BoneData *>(constraint->_constraintData->target);

static_cast<IKConstraint *>(constraint)->_bendPositive = bend_positive;
Expand All @@ -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<DragonBonesBone> 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 {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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<DragonBonesArmature *>(armature_node)->getArmature()->getName().c_str();
r_val = to_gd_str(static_cast<DragonBonesArmature *>(armature_node)->getArmature()->getName());
return true;
}

Expand All @@ -1041,7 +1041,7 @@ void DragonBonesArmatureProxy::_get_property_list(List<PropertyInfo> *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);
Expand Down
4 changes: 2 additions & 2 deletions src/dragonbones_armature.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class DragonBonesArmature : public GDDisplay, public dragonBones::IArmatureProxy
virtual void addDBEventListener(const std::string &_type, const std::function<void(dragonBones::EventObject *)> &_listener) override {}
virtual void removeDBEventListener(const std::string &_type, const std::function<void(dragonBones::EventObject *)> &_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;
Expand All @@ -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 <typename Func, typename std::enable_if<std::is_invocable_v<Func, DragonBonesArmature *>>::type *_dummy = nullptr>
void for_each_armature(Func &&p_action) {
Expand Down
2 changes: 1 addition & 1 deletion src/dragonbones_bone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 56559f2

Please sign in to comment.