-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #381 from Spartan322/extract/StyleBoxWithSound-script
Move StyleBoxWithSound to C++ extension
- Loading branch information
Showing
7 changed files
with
182 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<class name="StyleBoxWithSound" inherits="StyleBox" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd"> | ||
<brief_description> | ||
</brief_description> | ||
<description> | ||
</description> | ||
<tutorials> | ||
</tutorials> | ||
<members> | ||
<member name="audio_bus" type="StringName" setter="set_audio_bus" getter="get_audio_bus" default="&"Master""> | ||
</member> | ||
<member name="audio_stream" type="AudioStream" setter="set_audio_stream" getter="get_audio_stream"> | ||
</member> | ||
<member name="style_box" type="StyleBox" setter="set_style_box" getter="get_style_box"> | ||
</member> | ||
</members> | ||
</class> |
109 changes: 109 additions & 0 deletions
109
extension/src/openvic-extension/classes/resources/StyleBoxWithSound.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#include "StyleBoxWithSound.hpp" | ||
|
||
#include <godot_cpp/classes/audio_stream_player.hpp> | ||
#include <godot_cpp/classes/engine.hpp> | ||
#include <godot_cpp/classes/global_constants.hpp> | ||
#include <godot_cpp/classes/main_loop.hpp> | ||
#include <godot_cpp/classes/scene_tree.hpp> | ||
#include <godot_cpp/classes/window.hpp> | ||
#include <godot_cpp/variant/callable_method_pointer.hpp> | ||
#include <godot_cpp/variant/string.hpp> | ||
#include <godot_cpp/variant/utility_functions.hpp> | ||
|
||
#include "openvic-extension/utility/ClassBindings.hpp" | ||
|
||
using namespace OpenVic; | ||
using namespace godot; | ||
|
||
void StyleBoxWithSound::_bind_methods() { | ||
OV_BIND_METHOD(StyleBoxWithSound::get_style_box); | ||
OV_BIND_METHOD(StyleBoxWithSound::set_style_box, { "style" }); | ||
OV_BIND_METHOD(StyleBoxWithSound::get_audio_bus); | ||
OV_BIND_METHOD(StyleBoxWithSound::set_audio_bus, { "bus" }); | ||
OV_BIND_METHOD(StyleBoxWithSound::get_audio_stream); | ||
OV_BIND_METHOD(StyleBoxWithSound::set_audio_stream, { "stream" }); | ||
|
||
ADD_PROPERTY( | ||
PropertyInfo(Variant::OBJECT, "style_box", PROPERTY_HINT_RESOURCE_TYPE, "StyleBox"), "set_style_box", "get_style_box" | ||
); | ||
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "audio_bus", PROPERTY_HINT_ENUM, ""), "set_audio_bus", "get_audio_bus"); | ||
ADD_PROPERTY( | ||
PropertyInfo(Variant::OBJECT, "audio_stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), // | ||
"set_audio_stream", "get_audio_stream" | ||
); | ||
} | ||
|
||
StyleBoxWithSound::StyleBoxWithSound() { | ||
audio_bus = "Master"; | ||
} | ||
|
||
Ref<StyleBox> StyleBoxWithSound::get_style_box() const { | ||
return style_box; | ||
} | ||
|
||
void StyleBoxWithSound::set_style_box(Ref<StyleBox> const& style) { | ||
style_box = style; | ||
emit_changed(); | ||
} | ||
|
||
StringName StyleBoxWithSound::get_audio_bus() const { | ||
return audio_bus; | ||
} | ||
|
||
void StyleBoxWithSound::set_audio_bus(String const& bus) { | ||
audio_bus = bus; | ||
emit_changed(); | ||
} | ||
|
||
Ref<AudioStream> StyleBoxWithSound::get_audio_stream() const { | ||
return audio_stream; | ||
} | ||
|
||
void StyleBoxWithSound::set_audio_stream(Ref<AudioStream> const& stream) { | ||
audio_stream = stream; | ||
emit_changed(); | ||
} | ||
|
||
godot::Rect2 StyleBoxWithSound::_get_draw_rect(Rect2 const& rect) const { | ||
if (style_box.is_null()) { | ||
return Rect2(); | ||
} | ||
return style_box->_get_draw_rect(rect); | ||
} | ||
|
||
void StyleBoxWithSound::_on_audio_finished(AudioStreamPlayer* player) { | ||
player->queue_free(); | ||
} | ||
|
||
void StyleBoxWithSound::_draw(RID const& canvas_item, Rect2 const& rect) const { | ||
SceneTree* tree = Object::cast_to<SceneTree>(Engine::get_singleton()->get_main_loop()); | ||
if (tree != nullptr) { | ||
if (audio_stream.is_valid()) { | ||
AudioStreamPlayer* asp = memnew(AudioStreamPlayer); | ||
asp->connect("finished", callable_mp_static(&StyleBoxWithSound::_on_audio_finished).bind(asp)); | ||
asp->set_bus(audio_bus); | ||
asp->set_stream(audio_stream); | ||
asp->set_autoplay(true); | ||
tree->get_root()->add_child(asp, false, Node::INTERNAL_MODE_BACK); | ||
} | ||
} | ||
if (style_box.is_valid()) { | ||
style_box->_draw(canvas_item, rect); | ||
} | ||
} | ||
|
||
void StyleBoxWithSound::_validate_property(PropertyInfo& property) const { | ||
static StringName audio_bus_name = "audio_bus"; | ||
if (property.name == audio_bus_name) { | ||
String options; | ||
for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { | ||
if (i > 0) { | ||
options += ","; | ||
} | ||
String name = AudioServer::get_singleton()->get_bus_name(i); | ||
options += name; | ||
} | ||
|
||
property.hint_string = options; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
extension/src/openvic-extension/classes/resources/StyleBoxWithSound.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <godot_cpp/classes/audio_stream.hpp> | ||
#include <godot_cpp/classes/audio_stream_player.hpp> | ||
#include <godot_cpp/classes/style_box.hpp> | ||
#include <godot_cpp/variant/rect2.hpp> | ||
#include <godot_cpp/variant/rid.hpp> | ||
#include <godot_cpp/variant/string.hpp> | ||
|
||
#include <openvic-simulation/utility/Getters.hpp> | ||
|
||
namespace OpenVic { | ||
class StyleBoxWithSound : public godot::StyleBox { | ||
GDCLASS(StyleBoxWithSound, godot::StyleBox) | ||
|
||
godot::Ref<godot::StyleBox> style_box; | ||
godot::StringName audio_bus; | ||
godot::Ref<godot::AudioStream> audio_stream; | ||
|
||
static void _on_audio_finished(godot::AudioStreamPlayer* player); | ||
|
||
protected: | ||
static void _bind_methods(); | ||
|
||
void _validate_property(godot::PropertyInfo& property) const; | ||
|
||
public: | ||
StyleBoxWithSound(); | ||
|
||
godot::Ref<godot::StyleBox> get_style_box() const; | ||
void set_style_box(godot::Ref<godot::StyleBox> const& style); | ||
godot::StringName get_audio_bus() const; | ||
void set_audio_bus(godot::String const& bus); | ||
godot::Ref<godot::AudioStream> get_audio_stream() const; | ||
void set_audio_stream(godot::Ref<godot::AudioStream> const& stream); | ||
|
||
virtual godot::Rect2 _get_draw_rect(godot::Rect2 const& rect) const override; | ||
virtual void _draw(godot::RID const& canvas_item, godot::Rect2 const& rect) const override; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.