Skip to content

Commit

Permalink
[共通] Camera2D::getGrabbedPos() #1242
Browse files Browse the repository at this point in the history
  • Loading branch information
Reputeless committed Jul 3, 2024
1 parent 5d0a497 commit 627c863
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
9 changes: 7 additions & 2 deletions Siv3D/include/Siv3D/Camera2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ namespace s3d
/// @param color UI の色
void draw(const ColorF& color = Palette::White) const;

/// @brief 右クリックによる移動の開始座標を返します。
/// @return 右クリックによる移動の開始座標。右クリックによる移動が開始されていない場合は none
[[nodiscard]]
const Optional<Vec2>& getGrabbedPos() const noexcept;

protected:

double m_targetScale = BasicCamera2D::m_scale;
Expand All @@ -92,9 +97,9 @@ namespace s3d

Vec2 m_positionChangeVelocity = Vec2::Zero();

Optional<Point> m_grabPos;
Optional<Vec2> m_grabbedPos;

Optional<std::pair<Point, Vec2>> m_pointedScale;
Optional<std::pair<Vec2, Vec2>> m_pointedScale;

Camera2DParameters m_parameters;

Expand Down
11 changes: 8 additions & 3 deletions Siv3D/include/Siv3D/detail/Camera2D.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace s3d

inline void Camera2D::setTargetCenter(const Vec2 targetCenter) noexcept
{
m_grabPos.reset();
m_grabbedPos.reset();
m_pointedScale.reset();
m_targetCenter = targetCenter;
}
Expand All @@ -44,7 +44,7 @@ namespace s3d

inline void Camera2D::setTargetScale(const double targetScale) noexcept
{
m_grabPos.reset();
m_grabbedPos.reset();
m_pointedScale.reset();
m_targetScale = targetScale;
}
Expand All @@ -56,11 +56,16 @@ namespace s3d

inline void Camera2D::jumpTo(const Vec2 center, const double scale) noexcept
{
m_grabPos.reset();
m_grabbedPos.reset();
m_pointedScale.reset();
m_targetCenter = m_center = center;
m_targetScale = m_scale = scale;
m_positionChangeVelocity = Vec2::Zero();
m_scaleChangeVelocity = 0.0;
}

inline const Optional<Vec2>& Camera2D::getGrabbedPos() const noexcept
{
return m_grabbedPos;
}
}
19 changes: 10 additions & 9 deletions Siv3D/src/Siv3D/Camera2D/SivCamera2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace s3d

void Camera2D::draw(const ColorF& color) const
{
if ((not m_grabPos)
if ((not m_grabbedPos)
|| m_pointedScale)
{
return;
Expand All @@ -60,11 +60,12 @@ namespace s3d
const auto t1 = Transformer2D{ Mat3x2::Identity(), TransformCursor::Yes, Transformer2D::Target::SetLocal };
const auto t2 = Transformer2D{ Mat3x2::Identity(), TransformCursor::Yes, Transformer2D::Target::SetCamera };

const Vec2 grabbedPos = *m_grabbedPos;
const double radius = 12.0;
const Point delta = (Cursor::Pos() - m_grabPos.value());
const Vec2 delta = (Cursor::PosF() - grabbedPos);
const double length = delta.length();

Circle{ m_grabPos.value(), radius }.drawFrame(4.0, 2.0, color);
Circle{ grabbedPos, radius }.drawFrame(4.0, 2.0, color);

if ((radius * 2) <= length)
{
Expand All @@ -77,7 +78,7 @@ namespace s3d
const Vec2 p2 = direction.withLength(radius * 1.8);
const Vec2 p3 = direction.withLength(radius * 1.2) + leftOffset;

Quad{ p1, p2, p3, p0 }.moveBy(m_grabPos.value()).draw(color);
Quad{ p1, p2, p3, p0 }.moveBy(grabbedPos).draw(color);
}
}

Expand Down Expand Up @@ -106,7 +107,7 @@ namespace s3d
const auto t1 = Transformer2D{ Mat3x2::Identity(), TransformCursor::Yes, Transformer2D::Target::SetLocal };
const auto t2 = Transformer2D{ Mat3x2::Identity(), TransformCursor::Yes, Transformer2D::Target::SetCamera };

const Point cursorPos = Cursor::Pos();
const Vec2 cursorPos = Cursor::PosF();
const Vec2 point = (m_center + (cursorPos - (sceneSize * 0.5)) / m_scale);
m_pointedScale.emplace(cursorPos, point);
}
Expand Down Expand Up @@ -172,17 +173,17 @@ namespace s3d

if (MouseR.down())
{
m_grabPos = Cursor::Pos();
m_grabbedPos = Cursor::PosF();
m_pointedScale.reset();
}
else if (m_grabPos)
else if (m_grabbedPos)
{
const Point delta = (Cursor::Pos() - m_grabPos.value());
const Vec2 delta = (Cursor::PosF() - *m_grabbedPos);
m_targetCenter += m_parameters.grabSpeedFactor * (deltaTime / 1.0) * (delta / m_targetScale);

if (MouseR.up())
{
m_grabPos = none;
m_grabbedPos = none;
}
}
}
Expand Down

0 comments on commit 627c863

Please sign in to comment.