Skip to content

Commit

Permalink
Mouse input fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhonnyg committed Nov 8, 2024
1 parent 56fd328 commit 051d7f9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 24 deletions.
27 changes: 13 additions & 14 deletions defold-rive/src/comp_rive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,13 @@ namespace dmRive
rive::Mat2D transform;
Mat4ToMat2D(c->m_World, transform);

rive::Mat2D centerAdjustment = rive::Mat2D::fromTranslate(-bounds.width() / 2.0f, -bounds.height() / 2.0f);
rive::Mat2D scaleDpi = rive::Mat2D::fromScale(1,-1);
rive::Mat2D invertAdjustment = rive::Mat2D::fromScaleAndTranslation(g_DisplayFactor, -g_DisplayFactor, 0, window_height);
rive::Mat2D centerAdjustment = rive::Mat2D::fromTranslate(-bounds.width() / 2.0f, -bounds.height() / 2.0f);
rive::Mat2D scaleDpi = rive::Mat2D::fromScale(1,-1);
rive::Mat2D invertAdjustment = rive::Mat2D::fromScaleAndTranslation(g_DisplayFactor, -g_DisplayFactor, 0, window_height);
rive::Mat2D rendererTransform = invertAdjustment * transform * scaleDpi * centerAdjustment;

renderer->transform(invertAdjustment * transform * scaleDpi * centerAdjustment);
renderer->transform(rendererTransform);
c->m_InverseRendererTransform = rendererTransform.invertOrIdentity();
}
else if (ddf->m_CoordinateSystem == dmRiveDDF::RiveModelDesc::COORDINATE_SYSTEM_RIVE)
{
Expand All @@ -530,7 +532,9 @@ namespace dmRive
c->m_ArtboardInstance->height(height);
}

renderer->align(rive_fit, rive_align, rive::AABB(0, 0, width, height), bounds);
rive::Mat2D rendererTransform = rive::computeAlignment(rive_fit, rive_align, rive::AABB(0, 0, width, height), bounds);
renderer->transform(rendererTransform);
c->m_InverseRendererTransform = rendererTransform.invertOrIdentity();
}

if (c->m_StateMachineInstance) {
Expand Down Expand Up @@ -1514,15 +1518,10 @@ namespace dmRive

static rive::Vec2D WorldToLocal(RiveComponent* component, float x, float y)
{
float scale = g_DisplayFactor;

rive::AABB bounds = component->m_ArtboardInstance->bounds();
Matrix4 world_inv = dmVMath::Inverse(component->m_World);
Vector4 local = (world_inv * Point3(x*scale, y*scale, 0));
float bounds_width_half = bounds.width() * 0.5f;
float bounds_height_half = bounds.height() * 0.5f;
rive::Vec2D p((local.getX() + bounds.width()) / scale, (bounds.height() - local.getY()) / scale);
return p;
dmGraphics::HContext graphics_context = dmGraphics::GetInstalledContext();
uint32_t window_height = dmGraphics::GetWindowHeight(graphics_context);
rive::Vec2D p_local = component->m_InverseRendererTransform * rive::Vec2D(x, window_height - y);
return p_local;
}

void CompRivePointerMove(RiveComponent* component, float x, float y)
Expand Down
1 change: 1 addition & 0 deletions defold-rive/src/comp_rive.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace dmRive
dmRender::HMaterial m_Material;
dmScript::LuaCallbackInfo* m_Callback;
uint32_t m_CallbackId;
rive::Mat2D m_InverseRendererTransform;

std::unique_ptr<rive::ArtboardInstance> m_ArtboardInstance;
std::unique_ptr<rive::LinearAnimationInstance> m_AnimationInstance;
Expand Down
1 change: 1 addition & 0 deletions game.project
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ shared_state = 1
width = 960
height = 540
high_dpi = 1
samples = 4

[android]
input_method = HiddenInputField
Expand Down
9 changes: 2 additions & 7 deletions main/loader.collection
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,8 @@ embedded_instances {
"}\n"
""
position {
x: 128.0
y: 32.0
x: 72.423355
y: 19.258038
z: 1.0
}
scale3 {
x: 1.86
y: 1.86
z: 1.86
}
}
6 changes: 3 additions & 3 deletions main/sophiahud/sophiahud.script
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ end
function on_input(self, action_id, action)
if not action_id or action_id == hash("touch") then
if action.pressed then
rive.pointer_down("#hud", action.x, action.y)
rive.pointer_down("#hud", action.screen_x, action.screen_y)
elseif action.released then
rive.pointer_up("#hud", action.x, action.y)
rive.pointer_up("#hud", action.screen_x, action.screen_y)
else
rive.pointer_move("#hud", action.x, action.y)
rive.pointer_move("#hud", action.screen_x, action.screen_y)
end
end
end

0 comments on commit 051d7f9

Please sign in to comment.