-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is something that the Pocketwatch team requested but I found myself needing it when doing a Unity test this weekend. It adds the ability to dynamically retrieve the dimensions of any Shape from C++. In Unity, it uses a similar API to GameKit where it exposes elements of the Artboard as "Components" and then you can call different methods that will only function on certain types but it hides needing to expose all different kinds of types to the end user. I also added the ability to change a run's value so I could check that the shape was resizing properly from Unity. @HayesGordon we really should land your text run api too, but we should modify it to use the same component logic (might require changing from the base TransformComponent to just Component in the C++ plugin code). The Unity Artboard Components are wrapped and track the owning Artboard. This is to make sure that if the C# code releases all references of Artboard while someone is still holding a Component, Artboard will still be available at the C++ level (so you can't accidentally create a race condition that causes a crash in native). I didn't need to do this for the Text Runs as I just have a set api that doesn't store the TextValueRun reference in Unity yet. Diffs= 5cb42a9b0 Unity compute bounds (#6649) Co-authored-by: Luigi Rosso <[email protected]>
- Loading branch information
1 parent
a040296
commit c6d407a
Showing
5 changed files
with
126 additions
and
1 deletion.
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 |
---|---|---|
@@ -1 +1 @@ | ||
b765280df32889e6df18690d840fe8f240871c09 | ||
5cb42a9b0033a1b9d2360292b7ef14bf67e3d45e |
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
Binary file not shown.
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,48 @@ | ||
#include "rive/file.hpp" | ||
#include "rive/node.hpp" | ||
#include "rive/shapes/shape.hpp" | ||
#include "rive/math/transform_components.hpp" | ||
#include "rive/text/text_value_run.hpp" | ||
#include "utils/no_op_renderer.hpp" | ||
#include "rive_file_reader.hpp" | ||
#include "rive_testing.hpp" | ||
#include <cstdio> | ||
|
||
TEST_CASE("compute bounds of background shape", "[bounds]") | ||
{ | ||
auto file = ReadRiveFile("../../test/assets/background_measure.riv"); | ||
|
||
auto artboard = file->artboard(); | ||
|
||
REQUIRE(artboard->find<rive::Shape>("background") != nullptr); | ||
auto background = artboard->find<rive::Shape>("background"); | ||
REQUIRE(artboard->find<rive::TextValueRun>("nameRun") != nullptr); | ||
auto name = artboard->find<rive::TextValueRun>("nameRun"); | ||
artboard->advance(0.0f); | ||
|
||
auto bounds = background->computeWorldBounds(); | ||
CHECK(bounds.width() == Approx(42.010925f)); | ||
CHECK(bounds.height() == Approx(29.995453f)); | ||
|
||
// Change the text and verify the bounds extended further. | ||
name->text("much much longer"); | ||
artboard->advance(0.0f); | ||
|
||
bounds = background->computeWorldBounds(); | ||
CHECK(bounds.width() == Approx(138.01093f)); | ||
CHECK(bounds.height() == Approx(29.995453f)); | ||
|
||
// Apply a transform to the whole artboard. | ||
rive::Mat2D& world = artboard->mutableWorldTransform(); | ||
world.scaleByValues(0.5f, 0.5f); | ||
artboard->markWorldTransformDirty(); | ||
artboard->advance(0.0f); | ||
|
||
bounds = background->computeWorldBounds(); | ||
CHECK(bounds.width() == Approx(138.01093f / 2.0f)); | ||
CHECK(bounds.height() == Approx(29.995453f / 2.0f)); | ||
|
||
bounds = background->computeLocalBounds(); | ||
CHECK(bounds.width() == Approx(138.01093f)); | ||
CHECK(bounds.height() == Approx(29.995453f)); | ||
} |