Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/rive/viewmodel/runtime/viewmodel_runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct PropertyData
DataType type;
std::string name;
std::string enumName;
std::string viewModelName;
};

class ViewModelRuntime : public RefCnt<ViewModelRuntime>
Expand All @@ -34,8 +35,7 @@ class ViewModelRuntime : public RefCnt<ViewModelRuntime>
rcp<ViewModelInstanceRuntime> createDefaultInstance() const;
rcp<ViewModelInstanceRuntime> createInstance() const;
std::vector<PropertyData> properties();
static std::vector<PropertyData> buildPropertiesData(
std::vector<rive::ViewModelProperty*>& properties);
static std::vector<PropertyData> buildPropertiesData(ViewModel* viewModel);
std::vector<std::string> instanceNames() const;

private:
Expand Down
4 changes: 1 addition & 3 deletions include/rive/viewmodel/viewmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class ViewModel : public ViewModelBase, public RefCnt<ViewModel>
rcp<ViewModelInstance> createInstance();
rcp<ViewModelInstance> createFromInstance(const std::string& instanceName);
void file(File* value) { m_file = value; };
#ifdef WITH_RIVE_TOOLS
File* file() { return m_file; };
#endif
File* file() const { return m_file; };
ViewModelInstance* defaultInstance();
size_t instanceCount() const;
std::vector<ViewModelProperty*> properties() { return m_Properties; }
Expand Down
5 changes: 2 additions & 3 deletions src/viewmodel/runtime/viewmodel_instance_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ bool ViewModelInstanceRuntime::replaceViewModelByName(

std::vector<PropertyData> ViewModelInstanceRuntime::properties() const
{
std::vector<PropertyData> props;
auto properties = m_viewModelInstance->viewModel()->properties();
return ViewModelRuntime::buildPropertiesData(properties);
return ViewModelRuntime::buildPropertiesData(
m_viewModelInstance->viewModel());
}
23 changes: 18 additions & 5 deletions src/viewmodel/runtime/viewmodel_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ const std::string& ViewModelRuntime::name() const
}

std::vector<PropertyData> ViewModelRuntime::buildPropertiesData(
std::vector<rive::ViewModelProperty*>& properties)
ViewModel* viewModel)
{
auto* file = viewModel->file();
std::vector<PropertyData> props;
for (auto property : properties)
for (auto property : viewModel->properties())
{
DataType type = DataType::none;
std::string enumName;
std::string viewModelName;
switch (property->coreType())
{
case ViewModelPropertyString::typeKey:
Expand Down Expand Up @@ -82,8 +84,20 @@ std::vector<PropertyData> ViewModelRuntime::buildPropertiesData(
type = DataType::trigger;
break;
case ViewModelPropertyViewModelBase::typeKey:
{
type = DataType::viewModel;
if (file != nullptr)
{
auto* referencedViewModel = file->viewModel(
static_cast<ViewModelPropertyViewModel*>(property)
->viewModelReferenceId());
if (referencedViewModel != nullptr)
{
viewModelName = referencedViewModel->name();
}
}
break;
}
case ViewModelPropertySymbolListIndex::typeKey:
type = DataType::symbolListIndex;
break;
Expand All @@ -99,15 +113,14 @@ std::vector<PropertyData> ViewModelRuntime::buildPropertiesData(
default:
break;
}
props.push_back({type, property->name(), enumName});
props.push_back({type, property->name(), enumName, viewModelName});
}
return props;
}

std::vector<PropertyData> ViewModelRuntime::properties()
{
auto props = m_viewModel->properties();
return buildPropertiesData(props);
return buildPropertiesData(m_viewModel);
}

std::vector<std::string> ViewModelRuntime::instanceNames() const
Expand Down
17 changes: 17 additions & 0 deletions tests/unit_tests/runtime/data_binding_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,23 @@ TEST_CASE("View model runtime properties", "[data binding]")
REQUIRE(numData != properties.end());
REQUIRE(numData->type == rive::DataType::number);
REQUIRE(numData->enumName.empty());
REQUIRE(numData->viewModelName.empty());

// View model properties expose the name of the view model they reference,
// both from an instance and from the view model runtime itself.
auto chiData = findProperty("chi");
REQUIRE(chiData != properties.end());
REQUIRE(chiData->type == rive::DataType::viewModel);
REQUIRE(chiData->viewModelName == "child");

auto viewModelProperties = vm->properties();
auto chiViewModelData = std::find_if(viewModelProperties.begin(),
viewModelProperties.end(),
[](const rive::PropertyData& data) {
return data.name == "chi";
});
REQUIRE(chiViewModelData != viewModelProperties.end());
REQUIRE(chiViewModelData->viewModelName == "child");
}

TEST_CASE("Trigger fires single change on listener", "[data binding]")
Expand Down