Skip to content

Commit

Permalink
Traktor: Improved translate modifier a bit; improved visual and also …
Browse files Browse the repository at this point in the history
…implemented a view plane translation anchor.
  • Loading branch information
apistol78 committed Oct 17, 2023
1 parent 7a45681 commit 4052fe9
Showing 1 changed file with 127 additions and 83 deletions.
210 changes: 127 additions & 83 deletions code/Scene/Editor/Modifiers/TranslateModifier.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2023 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <numeric>
#include "Core/Log/Log.h"
#include "Core/Math/Line2.h"
#include "Core/Math/Winding2.h"
#include "Render/PrimitiveRenderer.h"
Expand Down Expand Up @@ -84,8 +85,8 @@ IModifier::CursorMovedResult TranslateModifier::cursorMoved(
const Scalar distance = (m_center - eye).xyz0().length();

const float axisLength = (distance / 4.0f) * m_context->getGuideSize();
const float arrowLength = axisLength / 8.0f;
const float squareLength = axisLength / 3.0f;
const float arrowLength = axisLength / 7.0f;
const float squareLength = axisLength / 2.5f;

TransformChain tc = transformChain;
tc.pushWorld(translate(m_center));
Expand Down Expand Up @@ -117,47 +118,47 @@ IModifier::CursorMovedResult TranslateModifier::cursorMoved(

// Check drag circles.
{
// \FIXME
const float d = (center - cursorPosition).length();
if (d < 0.015f)
m_axisHot |= 1 | 2 | 4;
}

// Check combo squares.
{
Winding2 w(4);

// XY
w[0] = center;
w[1] = square[0];
w[2] = square[3];
w[3] = square[1];
if (w.inside(cursorPosition))
if (m_axisHot == 0)
{
m_axisHot |= 1 | 2;
goto hit;
w[0] = center;
w[1] = square[0];
w[2] = square[3];
w[3] = square[1];
if (w.inside(cursorPosition))
m_axisHot |= 1 | 2;
}

// XZ
w[0] = center;
w[1] = square[0];
w[2] = square[4];
w[3] = square[2];
if (w.inside(cursorPosition))
if (m_axisHot == 0)
{
m_axisHot |= 1 | 4;
goto hit;
w[0] = center;
w[1] = square[0];
w[2] = square[4];
w[3] = square[2];
if (w.inside(cursorPosition))
m_axisHot |= 1 | 4;
}

// YZ
w[0] = center;
w[1] = square[1];
w[2] = square[5];
w[3] = square[2];
if (w.inside(cursorPosition))
if (m_axisHot == 0)
{
m_axisHot |= 2 | 4;
goto hit;
w[0] = center;
w[1] = square[1];
w[2] = square[5];
w[3] = square[2];
if (w.inside(cursorPosition))
m_axisHot |= 2 | 4;
}

hit:;
}

// Check each line.
Expand Down Expand Up @@ -247,31 +248,49 @@ void TranslateModifier::apply(
{
const Vector4 cp = transformChain.worldToClip(m_center);

Vector4 worldDelta = transformChain.getView().inverse() * viewDelta * cp.w();
if (!(m_axisEnable & 1))
worldDelta *= Vector4(0.0f, 1.0f, 1.0f);
if (!(m_axisEnable & 2))
worldDelta *= Vector4(1.0f, 0.0f, 1.0f);
if (!(m_axisEnable & 4))
worldDelta *= Vector4(1.0f, 1.0f, 0.0f);
m_center += worldDelta;

Vector4 baseDelta = snap(m_center, 1 | 2 | 4, snapOverrideEnable) - m_center0;
if (!(m_axisEnable & 1))
baseDelta *= Vector4(0.0f, 1.0f, 1.0f);
if (!(m_axisEnable & 2))
baseDelta *= Vector4(1.0f, 0.0f, 1.0f);
if (!(m_axisEnable & 4))
baseDelta *= Vector4(1.0f, 1.0f, 0.0f);

for (uint32_t i = 0; i < m_entityAdapters.size(); ++i)
if (m_axisEnable != (1 | 2 | 4))
{
Vector4 worldDelta = transformChain.getView().inverse() * viewDelta * cp.w();
if (!(m_axisEnable & 1))
worldDelta *= Vector4(0.0f, 1.0f, 1.0f);
if (!(m_axisEnable & 2))
worldDelta *= Vector4(1.0f, 0.0f, 1.0f);
if (!(m_axisEnable & 4))
worldDelta *= Vector4(1.0f, 1.0f, 0.0f);
m_center += worldDelta;

Vector4 baseDelta = snap(m_center, 1 | 2 | 4, snapOverrideEnable) - m_center0;
if (!(m_axisEnable & 1))
baseDelta *= Vector4(0.0f, 1.0f, 1.0f);
if (!(m_axisEnable & 2))
baseDelta *= Vector4(1.0f, 0.0f, 1.0f);
if (!(m_axisEnable & 4))
baseDelta *= Vector4(1.0f, 1.0f, 0.0f);

for (uint32_t i = 0; i < m_entityAdapters.size(); ++i)
{
const Transform T = m_entityAdapters[i]->getTransform();
m_entityAdapters[i]->setTransform(Transform(
snap(m_baseTranslations[i] + baseDelta, m_axisEnable, snapOverrideEnable),
T.rotation()
));
}
}
else
{
const Transform T = m_entityAdapters[i]->getTransform();
m_entityAdapters[i]->setTransform(Transform(
//m_baseTranslations[i] + baseDelta, << Snap in object space
snap(m_baseTranslations[i] + baseDelta, m_axisEnable, snapOverrideEnable), //< Snap in world space.
T.rotation()
));
Vector4 worldDelta = transformChain.getView().inverse() * viewDelta * cp.w();
m_center += worldDelta;

Vector4 baseDelta = m_center - m_center0;

for (uint32_t i = 0; i < m_entityAdapters.size(); ++i)
{
const Transform T = m_entityAdapters[i]->getTransform();
m_entityAdapters[i]->setTransform(Transform(
snap(m_baseTranslations[i] + baseDelta, 1 | 2 | 4, snapOverrideEnable),
T.rotation()
));
}
}

m_axisHot = m_axisEnable;
Expand All @@ -292,8 +311,8 @@ void TranslateModifier::draw(render::PrimitiveRenderer* primitiveRenderer) const
const Scalar distance = (m_center - eye).xyz0().length();

const float axisLength = (distance / 4.0f) * m_context->getGuideSize();
const float arrowLength = axisLength / 8.0f;
const float squareLength = axisLength / 3.0f;
const float arrowLength = axisLength / 7.0f;
const float squareLength = axisLength / 2.5f;

primitiveRenderer->pushWorld(translate(m_center));

Expand Down Expand Up @@ -354,45 +373,63 @@ void TranslateModifier::draw(render::PrimitiveRenderer* primitiveRenderer) const

// Guide fill squares.
// XY
primitiveRenderer->drawSolidQuad(
Vector4(0.0f, 0.0f, 0.0f, 1.0f),
Vector4(sx * squareLength, 0.0f, 0.0f, 1.0f),
Vector4(sx * squareLength, sy * squareLength, 0.0f, 1.0f),
Vector4(0.0f, sy * squareLength, 0.0f, 1.0f),
Color4ub(255, 255, 0, m_axisHot == (1 | 2) ? 90 : 70)
);
if (m_axisEnable == 0 || m_axisEnable == (1 | 2))
{
primitiveRenderer->drawSolidQuad(
Vector4(0.0f, 0.0f, 0.0f, 1.0f),
Vector4(sx * squareLength, 0.0f, 0.0f, 1.0f),
Vector4(sx * squareLength, sy * squareLength, 0.0f, 1.0f),
Vector4(0.0f, sy * squareLength, 0.0f, 1.0f),
Color4ub(255, 255, 0, m_axisHot == (1 | 2) ? 90 : 70)
);
}
// XZ
primitiveRenderer->drawSolidQuad(
Vector4(0.0f, 0.0f, 0.0f, 1.0f),
Vector4(sx * squareLength, 0.0f, 0.0f, 1.0f),
Vector4(sx * squareLength, 0.0f, sz * squareLength, 1.0f),
Vector4(0.0f, 0.0f, sz * squareLength, 1.0f),
Color4ub(255, 255, 0, m_axisHot == (1 | 4) ? 90 : 70)
);
if (m_axisEnable == 0 || m_axisEnable == (1 | 4))
{
primitiveRenderer->drawSolidQuad(
Vector4(0.0f, 0.0f, 0.0f, 1.0f),
Vector4(sx * squareLength, 0.0f, 0.0f, 1.0f),
Vector4(sx * squareLength, 0.0f, sz * squareLength, 1.0f),
Vector4(0.0f, 0.0f, sz * squareLength, 1.0f),
Color4ub(255, 255, 0, m_axisHot == (1 | 4) ? 90 : 70)
);
}
// YZ
primitiveRenderer->drawSolidQuad(
Vector4(0.0f, 0.0f, 0.0f, 1.0f),
Vector4(0.0f, sy * squareLength, 0.0f, 1.0f),
Vector4(0.0f, sy * squareLength, sz * squareLength, 1.0f),
Vector4(0.0f, 0.0f, sz * squareLength, 1.0f),
Color4ub(255, 255, 0, m_axisHot == (2 | 4) ? 90 : 70)
);
if (m_axisEnable == 0 || m_axisEnable == (2 | 4))
{
primitiveRenderer->drawSolidQuad(
Vector4(0.0f, 0.0f, 0.0f, 1.0f),
Vector4(0.0f, sy * squareLength, 0.0f, 1.0f),
Vector4(0.0f, sy * squareLength, sz * squareLength, 1.0f),
Vector4(0.0f, 0.0f, sz * squareLength, 1.0f),
Color4ub(255, 255, 0, m_axisHot == (2 | 4) ? 90 : 70)
);
}

// Guide square lines.
// XY
const float awxy = (float)(((m_axisHot & (1 | 2)) == (1 | 2)) ? 3 : 1);
primitiveRenderer->drawLine(Vector4(sx * squareLength, 0.0f, 0.0f, 1.0f), Vector4(sx * squareLength, sy * squareLength, 0.0f, 1.0f), awxy, Color4ub(255, 0, 0, 255));
primitiveRenderer->drawLine(Vector4(0.0f, sy * squareLength, 0.0f, 1.0f), Vector4(sx * squareLength, sy * squareLength, 0.0f, 1.0f), awxy, Color4ub(0, 255, 0, 255));
if (m_axisEnable == 0 || m_axisEnable == (1 | 2))
{
const float awxy = (float)(((m_axisHot & (1 | 2)) == (1 | 2)) ? 3 : 1);
primitiveRenderer->drawLine(Vector4(sx * squareLength, 0.0f, 0.0f, 1.0f), Vector4(sx * squareLength, sy * squareLength, 0.0f, 1.0f), awxy, Color4ub(255, 0, 0, 255));
primitiveRenderer->drawLine(Vector4(0.0f, sy * squareLength, 0.0f, 1.0f), Vector4(sx * squareLength, sy * squareLength, 0.0f, 1.0f), awxy, Color4ub(0, 255, 0, 255));
}

// XZ
const float awxz = (float)(((m_axisHot & (1 | 4)) == (1 | 4)) ? 3 : 1);
primitiveRenderer->drawLine(Vector4(sx * squareLength, 0.0f, 0.0f, 1.0f), Vector4(sx * squareLength, 0.0f, sz * squareLength, 1.0f), awxz, Color4ub(255, 0, 0, 255));
primitiveRenderer->drawLine(Vector4(0.0f, 0.0f, sz * squareLength, 1.0f), Vector4(sx * squareLength, 0.0f, sz * squareLength, 1.0f), awxz, Color4ub(0, 0, 255, 255));
if (m_axisEnable == 0 || m_axisEnable == (1 | 4))
{
const float awxz = (float)(((m_axisHot & (1 | 4)) == (1 | 4)) ? 3 : 1);
primitiveRenderer->drawLine(Vector4(sx * squareLength, 0.0f, 0.0f, 1.0f), Vector4(sx * squareLength, 0.0f, sz * squareLength, 1.0f), awxz, Color4ub(255, 0, 0, 255));
primitiveRenderer->drawLine(Vector4(0.0f, 0.0f, sz * squareLength, 1.0f), Vector4(sx * squareLength, 0.0f, sz * squareLength, 1.0f), awxz, Color4ub(0, 0, 255, 255));
}

// YZ
const float awyz = (float)(((m_axisHot & (2 | 4)) == (2 | 4)) ? 3 : 1);
primitiveRenderer->drawLine(Vector4(0.0f, sy * squareLength, 0.0f, 1.0f), Vector4(0.0f, sy * squareLength, sz * squareLength, 1.0f), awyz, Color4ub(0, 255, 0, 255));
primitiveRenderer->drawLine(Vector4(0.0f, 0.0f, sz * squareLength, 1.0f), Vector4(0.0f, sy * squareLength, sz * squareLength, 1.0f), awyz, Color4ub(0, 0, 255, 255));
if (m_axisEnable == 0 || m_axisEnable == (2 | 4))
{
const float awyz = (float)(((m_axisHot & (2 | 4)) == (2 | 4)) ? 3 : 1);
primitiveRenderer->drawLine(Vector4(0.0f, sy * squareLength, 0.0f, 1.0f), Vector4(0.0f, sy * squareLength, sz * squareLength, 1.0f), awyz, Color4ub(0, 255, 0, 255));
primitiveRenderer->drawLine(Vector4(0.0f, 0.0f, sz * squareLength, 1.0f), Vector4(0.0f, sy * squareLength, sz * squareLength, 1.0f), awyz, Color4ub(0, 0, 255, 255));
}

// Guide axis lines.
primitiveRenderer->drawLine(
Expand Down Expand Up @@ -434,6 +471,13 @@ void TranslateModifier::draw(render::PrimitiveRenderer* primitiveRenderer) const
Color4ub(0, 0, 255, 255)
);

// Center point.
primitiveRenderer->drawSolidPoint(
Vector4::origo(),
10.0f,
(m_axisHot != (1 | 2 | 4)) ? Color4ub(255, 255, 255, 180) : Color4ub(255, 255, 255, 255)
);

primitiveRenderer->popDepthState();

primitiveRenderer->popWorld();
Expand Down

0 comments on commit 4052fe9

Please sign in to comment.