mirror of
https://github.com/rive-app/rive-cpp.git
synced 2026-01-18 21:21:17 +01:00
Scripting in Core Runtime (#11235) 0a5325e474
rev to luau 0.702 (#11259) b50983c49d Co-authored-by: Luigi Rosso <luigi-rosso@users.noreply.github.com> Co-authored-by: Philip Chung <philterdesign@gmail.com>
This commit is contained in:
BIN
tests/unit_tests/assets/script_artboard_test.riv
Normal file
BIN
tests/unit_tests/assets/script_artboard_test.riv
Normal file
Binary file not shown.
BIN
tests/unit_tests/assets/script_dependency_test.riv
Normal file
BIN
tests/unit_tests/assets/script_dependency_test.riv
Normal file
Binary file not shown.
BIN
tests/unit_tests/assets/script_inputs_test_1.riv
Normal file
BIN
tests/unit_tests/assets/script_inputs_test_1.riv
Normal file
Binary file not shown.
BIN
tests/unit_tests/assets/script_layout_test.riv
Normal file
BIN
tests/unit_tests/assets/script_layout_test.riv
Normal file
Binary file not shown.
BIN
tests/unit_tests/assets/script_path_effects_test.riv
Normal file
BIN
tests/unit_tests/assets/script_path_effects_test.riv
Normal file
Binary file not shown.
BIN
tests/unit_tests/assets/script_paths_test.riv
Normal file
BIN
tests/unit_tests/assets/script_paths_test.riv
Normal file
Binary file not shown.
BIN
tests/unit_tests/assets/script_string_converter_test.riv
Normal file
BIN
tests/unit_tests/assets/script_string_converter_test.riv
Normal file
Binary file not shown.
@@ -1,8 +1,11 @@
|
||||
|
||||
#include "catch.hpp"
|
||||
#include "scripting_test_utilities.hpp"
|
||||
#include "rive/animation/state_machine_instance.hpp"
|
||||
#include "rive/lua/rive_lua_libs.hpp"
|
||||
#include "rive/math/path_types.hpp"
|
||||
#include "rive_file_reader.hpp"
|
||||
#include "rive_testing.hpp"
|
||||
|
||||
using namespace rive;
|
||||
|
||||
@@ -367,3 +370,57 @@ TEST_CASE("path measure extract across multiple contours", "[scripting]")
|
||||
// Should extract from both contours
|
||||
CHECK(destPath->rawPath.verbs().count() > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("path drawing examples", "[silver]")
|
||||
{
|
||||
rive::SerializingFactory silver;
|
||||
auto file = ReadRiveFile("assets/script_paths_test.riv", &silver);
|
||||
|
||||
auto artboard = file->artboardNamed("PathsScript");
|
||||
|
||||
silver.frameSize(artboard->width(), artboard->height());
|
||||
|
||||
REQUIRE(artboard != nullptr);
|
||||
auto stateMachine = artboard->stateMachineAt(0);
|
||||
stateMachine->advanceAndApply(0.1f);
|
||||
|
||||
auto renderer = silver.makeRenderer();
|
||||
artboard->draw(renderer.get());
|
||||
|
||||
int frames = 60;
|
||||
for (int i = 0; i < frames; i++)
|
||||
{
|
||||
silver.addFrame();
|
||||
stateMachine->advanceAndApply(0.016f);
|
||||
artboard->draw(renderer.get());
|
||||
}
|
||||
|
||||
CHECK(silver.matches("script_paths"));
|
||||
}
|
||||
|
||||
TEST_CASE("path effects examples", "[silver]")
|
||||
{
|
||||
rive::SerializingFactory silver;
|
||||
auto file = ReadRiveFile("assets/script_path_effects_test.riv", &silver);
|
||||
|
||||
auto artboard = file->artboardNamed("PathEffects");
|
||||
|
||||
silver.frameSize(artboard->width(), artboard->height());
|
||||
|
||||
REQUIRE(artboard != nullptr);
|
||||
auto stateMachine = artboard->stateMachineAt(0);
|
||||
stateMachine->advanceAndApply(0.1f);
|
||||
|
||||
auto renderer = silver.makeRenderer();
|
||||
artboard->draw(renderer.get());
|
||||
|
||||
int frames = 60;
|
||||
for (int i = 0; i < frames; i++)
|
||||
{
|
||||
silver.addFrame();
|
||||
stateMachine->advanceAndApply(0.016f);
|
||||
artboard->draw(renderer.get());
|
||||
}
|
||||
|
||||
CHECK(silver.matches("script_path_effects"));
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
|
||||
#include "catch.hpp"
|
||||
#include "scripting_test_utilities.hpp"
|
||||
#include "rive/animation/state_machine_instance.hpp"
|
||||
#include "rive/lua/rive_lua_libs.hpp"
|
||||
#include "rive_file_reader.hpp"
|
||||
|
||||
@@ -282,4 +283,31 @@ TEST_CASE("can add artboard to path", "[scripting]")
|
||||
lua_getglobal(L, "addToPath");
|
||||
lua_pushvalue(L, -2);
|
||||
CHECK(lua_pcall(L, 1, 1, 0) == LUA_OK);
|
||||
}
|
||||
|
||||
TEST_CASE("script instances Artboard input", "[silver]")
|
||||
{
|
||||
rive::SerializingFactory silver;
|
||||
auto file = ReadRiveFile("assets/script_artboard_test.riv", &silver);
|
||||
|
||||
auto artboard = file->artboardNamed("Artboard");
|
||||
|
||||
silver.frameSize(artboard->width(), artboard->height());
|
||||
|
||||
REQUIRE(artboard != nullptr);
|
||||
auto stateMachine = artboard->stateMachineAt(0);
|
||||
stateMachine->advanceAndApply(0.1f);
|
||||
|
||||
auto renderer = silver.makeRenderer();
|
||||
artboard->draw(renderer.get());
|
||||
|
||||
int frames = 60;
|
||||
for (int i = 0; i < frames; i++)
|
||||
{
|
||||
silver.addFrame();
|
||||
stateMachine->advanceAndApply(0.016f);
|
||||
artboard->draw(renderer.get());
|
||||
}
|
||||
|
||||
CHECK(silver.matches("script_artboards"));
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
|
||||
#include "catch.hpp"
|
||||
#include "scripting_test_utilities.hpp"
|
||||
#include "rive/animation/state_machine_instance.hpp"
|
||||
#include "rive/lua/rive_lua_libs.hpp"
|
||||
#include "rive/viewmodel/viewmodel_instance_number.hpp"
|
||||
#include "rive/viewmodel/viewmodel_instance_string.hpp"
|
||||
#include "rive_file_reader.hpp"
|
||||
|
||||
using namespace rive;
|
||||
|
||||
@@ -342,3 +346,101 @@ end
|
||||
CHECK(top == lua_gettop(L));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("scripted string converter", "[silver]")
|
||||
{
|
||||
rive::SerializingFactory silver;
|
||||
auto file =
|
||||
ReadRiveFile("assets/script_string_converter_test.riv", &silver);
|
||||
auto artboard = file->artboardNamed("Converter");
|
||||
|
||||
silver.frameSize(artboard->width(), artboard->height());
|
||||
REQUIRE(artboard != nullptr);
|
||||
auto stateMachine = artboard->stateMachineAt(0);
|
||||
int viewModelId = artboard.get()->viewModelId();
|
||||
|
||||
auto vmi = viewModelId == -1
|
||||
? file->createViewModelInstance(artboard.get())
|
||||
: file->createViewModelInstance(viewModelId, 0);
|
||||
stateMachine->bindViewModelInstance(vmi);
|
||||
stateMachine->advanceAndApply(0.1f);
|
||||
|
||||
auto renderer = silver.makeRenderer();
|
||||
artboard->draw(renderer.get());
|
||||
|
||||
auto field1 =
|
||||
vmi->propertyValue("Field1")->as<rive::ViewModelInstanceString>();
|
||||
REQUIRE(field1 != nullptr);
|
||||
field1->propertyValue("H#e%l&l*o");
|
||||
|
||||
silver.addFrame();
|
||||
stateMachine->advanceAndApply(0.016f);
|
||||
artboard->draw(renderer.get());
|
||||
|
||||
auto field2 =
|
||||
vmi->propertyValue("Field2")->as<rive::ViewModelInstanceString>();
|
||||
REQUIRE(field2 != nullptr);
|
||||
field2->propertyValue("____one two three___");
|
||||
|
||||
silver.addFrame();
|
||||
stateMachine->advanceAndApply(0.016f);
|
||||
artboard->draw(renderer.get());
|
||||
|
||||
auto field3 =
|
||||
vmi->propertyValue("Field3")->as<rive::ViewModelInstanceString>();
|
||||
REQUIRE(field3 != nullptr);
|
||||
field3->propertyValue(" **This uses a string converter@@. ");
|
||||
|
||||
silver.addFrame();
|
||||
stateMachine->advanceAndApply(0.016f);
|
||||
artboard->draw(renderer.get());
|
||||
|
||||
auto field4 =
|
||||
vmi->propertyValue("Field4")->as<rive::ViewModelInstanceString>();
|
||||
REQUIRE(field4 != nullptr);
|
||||
field4->propertyValue("It strips special characters like *&^%$#@!)()");
|
||||
|
||||
silver.addFrame();
|
||||
stateMachine->advanceAndApply(0.016f);
|
||||
artboard->draw(renderer.get());
|
||||
|
||||
CHECK(silver.matches("script_string_converter"));
|
||||
}
|
||||
|
||||
TEST_CASE("scripted data converter using multi chain requires", "[silver]")
|
||||
{
|
||||
rive::SerializingFactory silver;
|
||||
auto file = ReadRiveFile("assets/script_dependency_test.riv", &silver);
|
||||
auto artboard = file->artboardNamed("Artboard");
|
||||
|
||||
silver.frameSize(artboard->width(), artboard->height());
|
||||
REQUIRE(artboard != nullptr);
|
||||
auto stateMachine = artboard->stateMachineAt(0);
|
||||
int viewModelId = artboard.get()->viewModelId();
|
||||
|
||||
auto vmi = viewModelId == -1
|
||||
? file->createViewModelInstance(artboard.get())
|
||||
: file->createViewModelInstance(viewModelId, 0);
|
||||
stateMachine->bindViewModelInstance(vmi);
|
||||
stateMachine->advanceAndApply(0.1f);
|
||||
|
||||
auto renderer = silver.makeRenderer();
|
||||
artboard->draw(renderer.get());
|
||||
|
||||
rive::ViewModelInstanceNumber* num =
|
||||
vmi->propertyValue("InputValue1")->as<rive::ViewModelInstanceNumber>();
|
||||
REQUIRE(num != nullptr);
|
||||
|
||||
int counter = 0;
|
||||
int frames = 30;
|
||||
for (int i = 0; i < frames; i++)
|
||||
{
|
||||
num->propertyValue(counter);
|
||||
silver.addFrame();
|
||||
stateMachine->advanceAndApply(0.016f);
|
||||
artboard->draw(renderer.get());
|
||||
counter += 5;
|
||||
}
|
||||
|
||||
CHECK(silver.matches("script_converter_with_dependency"));
|
||||
}
|
||||
@@ -8,6 +8,9 @@
|
||||
#include "utils/no_op_renderer.hpp"
|
||||
#include "rive/layout/layout_enums.hpp"
|
||||
#include "rive/data_bind/data_values/data_value.hpp"
|
||||
#include "rive/viewmodel/viewmodel_instance_color.hpp"
|
||||
#include "rive/viewmodel/viewmodel_instance_trigger.hpp"
|
||||
#include "rive_file_reader.hpp"
|
||||
#include <memory>
|
||||
|
||||
using namespace rive;
|
||||
@@ -758,3 +761,39 @@ end
|
||||
// Cleanup - must happen before luaState goes out of scope
|
||||
converter->scriptDispose();
|
||||
}
|
||||
|
||||
TEST_CASE("scripted input color and trigger test", "[silver]")
|
||||
{
|
||||
rive::SerializingFactory silver;
|
||||
auto file = ReadRiveFile("assets/script_inputs_test_1.riv", &silver);
|
||||
auto artboard = file->artboardNamed("Artboard");
|
||||
|
||||
silver.frameSize(artboard->width(), artboard->height());
|
||||
REQUIRE(artboard != nullptr);
|
||||
auto stateMachine = artboard->stateMachineAt(0);
|
||||
int viewModelId = artboard.get()->viewModelId();
|
||||
|
||||
auto vmi = viewModelId == -1
|
||||
? file->createViewModelInstance(artboard.get())
|
||||
: file->createViewModelInstance(viewModelId, 0);
|
||||
stateMachine->bindViewModelInstance(vmi);
|
||||
stateMachine->advanceAndApply(0.1f);
|
||||
|
||||
auto renderer = silver.makeRenderer();
|
||||
artboard->draw(renderer.get());
|
||||
|
||||
rive::ViewModelInstanceTrigger* trigger =
|
||||
vmi->propertyValue("Trigger")->as<rive::ViewModelInstanceTrigger>();
|
||||
REQUIRE(trigger != nullptr);
|
||||
|
||||
int frames = 30;
|
||||
for (int i = 0; i < frames; i++)
|
||||
{
|
||||
trigger->trigger();
|
||||
silver.addFrame();
|
||||
stateMachine->advanceAndApply(0.016f);
|
||||
artboard->draw(renderer.get());
|
||||
}
|
||||
|
||||
CHECK(silver.matches("script_input_color_trigger"));
|
||||
}
|
||||
@@ -4,10 +4,13 @@
|
||||
|
||||
#include "catch.hpp"
|
||||
#include "scripting_test_utilities.hpp"
|
||||
#include "rive/animation/state_machine_instance.hpp"
|
||||
#include "rive/lua/rive_lua_libs.hpp"
|
||||
#include "rive/scripted/scripted_layout.hpp"
|
||||
#include "rive/layout/layout_enums.hpp"
|
||||
#include "rive/math/vec2d.hpp"
|
||||
#include "rive/viewmodel/viewmodel_instance_number.hpp"
|
||||
#include "rive_file_reader.hpp"
|
||||
#include "utils/no_op_renderer.hpp"
|
||||
|
||||
using namespace rive;
|
||||
@@ -287,3 +290,55 @@ end
|
||||
CHECK(top == lua_gettop(L));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("layout grid script", "[silver]")
|
||||
{
|
||||
rive::SerializingFactory silver;
|
||||
auto file = ReadRiveFile("assets/script_layout_test.riv", &silver);
|
||||
auto artboard = file->artboardNamed("LayoutScript");
|
||||
|
||||
silver.frameSize(artboard->width(), artboard->height());
|
||||
REQUIRE(artboard != nullptr);
|
||||
auto stateMachine = artboard->stateMachineAt(0);
|
||||
int viewModelId = artboard.get()->viewModelId();
|
||||
|
||||
auto vmi = viewModelId == -1
|
||||
? file->createViewModelInstance(artboard.get())
|
||||
: file->createViewModelInstance(viewModelId, 0);
|
||||
stateMachine->bindViewModelInstance(vmi);
|
||||
auto rows = vmi->propertyValue("Rows")->as<rive::ViewModelInstanceNumber>();
|
||||
REQUIRE(rows != nullptr);
|
||||
auto rowsValue = rows->propertyValue();
|
||||
REQUIRE(rowsValue == 5);
|
||||
auto cols =
|
||||
vmi->propertyValue("Columns")->as<rive::ViewModelInstanceNumber>();
|
||||
REQUIRE(cols != nullptr);
|
||||
auto colsValue = cols->propertyValue();
|
||||
REQUIRE(colsValue == 5);
|
||||
stateMachine->advanceAndApply(0.1f);
|
||||
|
||||
auto renderer = silver.makeRenderer();
|
||||
artboard->draw(renderer.get());
|
||||
|
||||
int frames = 20;
|
||||
for (int i = 0; i < frames; i++)
|
||||
{
|
||||
silver.addFrame();
|
||||
stateMachine->advanceAndApply(0.016f);
|
||||
artboard->draw(renderer.get());
|
||||
}
|
||||
|
||||
rows->propertyValue(8);
|
||||
rowsValue = rows->propertyValue();
|
||||
REQUIRE(rowsValue == 8);
|
||||
cols->propertyValue(7);
|
||||
colsValue = cols->propertyValue();
|
||||
REQUIRE(colsValue == 7);
|
||||
for (int i = 0; i < frames; i++)
|
||||
{
|
||||
silver.addFrame();
|
||||
stateMachine->advanceAndApply(0.016f);
|
||||
artboard->draw(renderer.get());
|
||||
}
|
||||
CHECK(silver.matches("script_layout_grid"));
|
||||
}
|
||||
BIN
tests/unit_tests/silvers/script_artboards.sriv
Normal file
BIN
tests/unit_tests/silvers/script_artboards.sriv
Normal file
Binary file not shown.
BIN
tests/unit_tests/silvers/script_converter_with_dependency.sriv
Normal file
BIN
tests/unit_tests/silvers/script_converter_with_dependency.sriv
Normal file
Binary file not shown.
BIN
tests/unit_tests/silvers/script_input_color_trigger.sriv
Normal file
BIN
tests/unit_tests/silvers/script_input_color_trigger.sriv
Normal file
Binary file not shown.
BIN
tests/unit_tests/silvers/script_layout_grid.sriv
Normal file
BIN
tests/unit_tests/silvers/script_layout_grid.sriv
Normal file
Binary file not shown.
BIN
tests/unit_tests/silvers/script_path_effects.sriv
Normal file
BIN
tests/unit_tests/silvers/script_path_effects.sriv
Normal file
Binary file not shown.
BIN
tests/unit_tests/silvers/script_paths.sriv
Normal file
BIN
tests/unit_tests/silvers/script_paths.sriv
Normal file
Binary file not shown.
BIN
tests/unit_tests/silvers/script_string_converter.sriv
Normal file
BIN
tests/unit_tests/silvers/script_string_converter.sriv
Normal file
Binary file not shown.
Reference in New Issue
Block a user