Skip to content

Commit

Permalink
Merge pull request ddnet#6867 from Marmare314/point_selection
Browse files Browse the repository at this point in the history
Make point selection easier
  • Loading branch information
Robyt3 authored Jul 28, 2023
2 parents b57cd2e + 862c0ca commit 1f1cc80
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 49 deletions.
173 changes: 124 additions & 49 deletions src/game/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1645,11 +1645,6 @@ void CEditor::DoQuad(CQuad *pQuad, int Index)
float CenterX = fx2f(pQuad->m_aPoints[4].x);
float CenterY = fx2f(pQuad->m_aPoints[4].y);

float dx = (CenterX - wx) / m_MouseWScale;
float dy = (CenterY - wy) / m_MouseWScale;
if(dx * dx + dy * dy < 50)
UI()->SetHotItem(pID);

const bool IgnoreGrid = Input()->AltIsPressed();

// draw selection background
Expand Down Expand Up @@ -1936,11 +1931,6 @@ void CEditor::DoQuadPoint(CQuad *pQuad, int QuadIndex, int V)
float px = fx2f(pQuad->m_aPoints[V].x);
float py = fx2f(pQuad->m_aPoints[V].y);

float dx = (px - wx) / m_MouseWScale;
float dy = (py - wy) / m_MouseWScale;
if(dx * dx + dy * dy < 50)
UI()->SetHotItem(pID);

// draw selection background
if(IsQuadPointSelected(QuadIndex, V))
{
Expand Down Expand Up @@ -2485,23 +2475,14 @@ void CEditor::DoQuadEnvPoint(const CQuad *pQuad, int QIndex, int PIndex)
float wy = UI()->MouseWorldY();
CEnvelope *pEnvelope = m_Map.m_vpEnvelopes[pQuad->m_PosEnv];
void *pID = &pEnvelope->m_vPoints[PIndex];
static int s_CurQIndex = -1;

// get pivot
float CenterX = fx2f(pQuad->m_aPoints[4].x) + fx2f(pEnvelope->m_vPoints[PIndex].m_aValues[0]);
float CenterY = fx2f(pQuad->m_aPoints[4].y) + fx2f(pEnvelope->m_vPoints[PIndex].m_aValues[1]);

float dx = (CenterX - wx) / m_MouseWScale;
float dy = (CenterY - wy) / m_MouseWScale;
if(dx * dx + dy * dy < 50.0f && UI()->CheckActiveItem(nullptr))
{
UI()->SetHotItem(pID);
s_CurQIndex = QIndex;
}

const bool IgnoreGrid = Input()->AltIsPressed();

if(UI()->CheckActiveItem(pID) && s_CurQIndex == QIndex)
if(UI()->CheckActiveItem(pID) && m_CurrentQuadIndex == QIndex)
{
if(s_Operation == OP_MOVE)
{
Expand Down Expand Up @@ -2534,7 +2515,7 @@ void CEditor::DoQuadEnvPoint(const CQuad *pQuad, int QIndex, int PIndex)

Graphics()->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
}
else if(UI()->HotItem() == pID && s_CurQIndex == QIndex)
else if(UI()->HotItem() == pID && m_CurrentQuadIndex == QIndex)
{
ms_pUiGotContext = pID;

Expand Down Expand Up @@ -3068,6 +3049,8 @@ void CEditor::DoMapEditor(CUIRect View)
DoQuadKnife(m_vSelectedQuads[m_SelectedQuadIndex]);
else
{
SetHotQuadPoint(pLayer);

Graphics()->TextureClear();
Graphics()->QuadsBegin();
for(size_t i = 0; i < pLayer->m_vQuads.size(); i++)
Expand Down Expand Up @@ -3377,6 +3360,51 @@ void CEditor::DoMapEditor(CUIRect View)
UI()->MapScreen();
}

void CEditor::SetHotQuadPoint(CLayerQuads *pLayer)
{
float wx = UI()->MouseWorldX();
float wy = UI()->MouseWorldY();

float MinDist = 500.0f;
void *pMinPoint = nullptr;

auto UpdateMinimum = [&](float px, float py, void *pID) {
float dx = (px - wx) / m_MouseWScale;
float dy = (py - wy) / m_MouseWScale;

float CurrDist = dx * dx + dy * dy;
if(CurrDist < MinDist)
{
MinDist = CurrDist;
pMinPoint = pID;
return true;
}
return false;
};

for(size_t i = 0; i < pLayer->m_vQuads.size(); i++)
{
CQuad &Quad = pLayer->m_vQuads.at(i);

if(m_ShowTileInfo != SHOW_TILE_OFF && m_ShowEnvelopePreview != SHOWENV_NONE && Quad.m_PosEnv >= 0)
{
for(auto &EnvPoint : m_Map.m_vpEnvelopes[Quad.m_PosEnv]->m_vPoints)
{
float px = fx2f(Quad.m_aPoints[4].x) + fx2f(EnvPoint.m_aValues[0]);
float py = fx2f(Quad.m_aPoints[4].y) + fx2f(EnvPoint.m_aValues[1]);
if(UpdateMinimum(px, py, &EnvPoint))
m_CurrentQuadIndex = i;
}
}

for(auto &Point : Quad.m_aPoints)
UpdateMinimum(fx2f(Point.x), fx2f(Point.y), &Point);
}

if(pMinPoint != nullptr)
UI()->SetHotItem(pMinPoint);
}

int CEditor::DoProperties(CUIRect *pToolBox, CProperty *pProps, int *pIDs, int *pNewVal, ColorRGBA Color)
{
int Change = -1;
Expand Down Expand Up @@ -5933,6 +5961,57 @@ class CTimeStep
}
};

void CEditor::SetHotEnvelopePoint(const CUIRect &View, CEnvelope *pEnvelope)
{
if(!UI()->MouseInside(&View))
return;

float mx = UI()->MouseX();
float my = UI()->MouseY();

float MinDist = 200.0f;
int *pMinPoint = nullptr;

auto UpdateMinimum = [&](float px, float py, int *pID) {
float dx = px - mx;
float dy = py - my;

float CurrDist = dx * dx + dy * dy;
if(CurrDist < MinDist)
{
MinDist = CurrDist;
pMinPoint = pID;
}
};

for(size_t i = 0; i < pEnvelope->m_vPoints.size(); i++)
{
for(int c = 0; c < pEnvelope->GetChannels(); c++)
{
if(i > 0 && pEnvelope->m_vPoints[i - 1].m_Curvetype == CURVETYPE_BEZIER)
{
float px = EnvelopeToScreenX(View, fxt2f(pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c]));
float py = EnvelopeToScreenY(View, fx2f(pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c]));
UpdateMinimum(px, py, &pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c]);
}

if(pEnvelope->m_vPoints[i].m_Curvetype == CURVETYPE_BEZIER)
{
float px = EnvelopeToScreenX(View, fxt2f(pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c]));
float py = EnvelopeToScreenY(View, fx2f(pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c]));
UpdateMinimum(px, py, &pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c]);
}

float px = EnvelopeToScreenX(View, fxt2f(pEnvelope->m_vPoints[i].m_Time));
float py = EnvelopeToScreenY(View, fx2f(pEnvelope->m_vPoints[i].m_aValues[c]));
UpdateMinimum(px, py, &pEnvelope->m_vPoints[i].m_aValues[c]);
}
}

if(pMinPoint != nullptr)
UI()->SetHotItem(pMinPoint);
}

void CEditor::RenderEnvelopeEditor(CUIRect View)
{
if(m_SelectedEnvelope < 0)
Expand Down Expand Up @@ -6198,8 +6277,30 @@ void CEditor::RenderEnvelopeEditor(CUIRect View)
UI()->DoLabel(&Button, "Sync.", 10.0f, TEXTALIGN_ML);

if(UI()->MouseInside(&View))
{
UI()->SetHotItem(&s_EnvelopeEditorID);

if(UI()->MouseButton(2) || (UI()->MouseButton(0) && Input()->ModifierIsPressed()))
{
m_OffsetEnvelopeX += UI()->MouseDeltaX() / Graphics()->ScreenWidth() * UI()->Screen()->w / View.w;
m_OffsetEnvelopeY -= UI()->MouseDeltaY() / Graphics()->ScreenHeight() * UI()->Screen()->h / View.h;
}
if(Input()->ShiftIsPressed())
{
if(Input()->KeyPress(KEY_MOUSE_WHEEL_DOWN))
m_ZoomEnvelopeY.ChangeZoom(0.1f * m_ZoomEnvelopeY.GetZoom());
if(Input()->KeyPress(KEY_MOUSE_WHEEL_UP))
m_ZoomEnvelopeY.ChangeZoom(-0.1f * m_ZoomEnvelopeY.GetZoom());
}
else
{
if(Input()->KeyPress(KEY_MOUSE_WHEEL_DOWN))
m_ZoomEnvelopeX.ChangeZoom(0.1f * m_ZoomEnvelopeX.GetZoom());
if(Input()->KeyPress(KEY_MOUSE_WHEEL_UP))
m_ZoomEnvelopeX.ChangeZoom(-0.1f * m_ZoomEnvelopeX.GetZoom());
}
}

if(UI()->HotItem() == &s_EnvelopeEditorID)
{
// do stuff
Expand All @@ -6220,25 +6321,6 @@ void CEditor::RenderEnvelopeEditor(CUIRect View)
RemoveTimeOffsetEnvelope(pEnvelope);
m_Map.OnModify();
}
else if(UI()->MouseButton(2) || (UI()->MouseButton(0) && Input()->ModifierIsPressed()))
{
m_OffsetEnvelopeX += UI()->MouseDeltaX() / Graphics()->ScreenWidth() * UI()->Screen()->w / View.w;
m_OffsetEnvelopeY -= UI()->MouseDeltaY() / Graphics()->ScreenHeight() * UI()->Screen()->h / View.h;
}
if(Input()->ShiftIsPressed())
{
if(Input()->KeyPress(KEY_MOUSE_WHEEL_DOWN))
m_ZoomEnvelopeY.ChangeZoom(0.1f * m_ZoomEnvelopeY.GetZoom());
if(Input()->KeyPress(KEY_MOUSE_WHEEL_UP))
m_ZoomEnvelopeY.ChangeZoom(-0.1f * m_ZoomEnvelopeY.GetZoom());
}
else
{
if(Input()->KeyPress(KEY_MOUSE_WHEEL_DOWN))
m_ZoomEnvelopeX.ChangeZoom(0.1f * m_ZoomEnvelopeX.GetZoom());
if(Input()->KeyPress(KEY_MOUSE_WHEEL_UP))
m_ZoomEnvelopeX.ChangeZoom(-0.1f * m_ZoomEnvelopeX.GetZoom());
}

m_ShowEnvelopePreview = SHOWENV_SELECTED;
m_pTooltip = "Double-click to create a new point. Use shift to change the zoom axis.";
Expand Down Expand Up @@ -6505,6 +6587,8 @@ void CEditor::RenderEnvelopeEditor(CUIRect View)
}

{
SetHotEnvelopePoint(View, pEnvelope);

UI()->ClipEnable(&View);
Graphics()->TextureClear();
Graphics()->QuadsBegin();
Expand All @@ -6527,9 +6611,6 @@ void CEditor::RenderEnvelopeEditor(CUIRect View)

const void *pID = &pEnvelope->m_vPoints[i].m_aValues[c];

if(UI()->MouseInside(&Final) && UI()->MouseInside(&View))
UI()->SetHotItem(pID);

if(IsEnvPointSelected(i, c))
{
Graphics()->SetColor(1, 1, 1, 1);
Expand Down Expand Up @@ -6741,9 +6822,6 @@ void CEditor::RenderEnvelopeEditor(CUIRect View)
Graphics()->QuadsDrawFreeform(&FreeformItem, 1);
}

if(UI()->MouseInside(&Final) && UI()->MouseInside(&View))
UI()->SetHotItem(pID);

if(UI()->CheckActiveItem(pID))
{
m_ShowEnvelopePreview = SHOWENV_SELECTED;
Expand Down Expand Up @@ -6876,9 +6954,6 @@ void CEditor::RenderEnvelopeEditor(CUIRect View)
Graphics()->QuadsDrawFreeform(&FreeformItem, 1);
}

if(UI()->MouseInside(&Final) && UI()->MouseInside(&View))
UI()->SetHotItem(pID);

if(UI()->CheckActiveItem(pID))
{
m_ShowEnvelopePreview = SHOWENV_SELECTED;
Expand Down
7 changes: 7 additions & 0 deletions src/game/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -949,10 +949,12 @@ class CEditor : public IEditor

m_ShowEnvelopePreview = SHOWENV_NONE;
m_SelectedQuadEnvelope = -1;

m_vSelectedEnvelopePoints = {};
m_UpdateEnvPointInfo = false;
m_SelectedTangentInPoint = std::pair(-1, -1);
m_SelectedTangentOutPoint = std::pair(-1, -1);
m_CurrentQuadIndex = -1;

m_QuadKnifeActive = false;
m_QuadKnifeCount = 0;
Expand Down Expand Up @@ -1292,6 +1294,7 @@ class CEditor : public IEditor
int m_SelectedEnvelope;
std::vector<std::pair<int, int>> m_vSelectedEnvelopePoints;
int m_SelectedQuadEnvelope;
int m_CurrentQuadIndex;
int m_SelectedImage;
int m_SelectedSound;
int m_SelectedSource;
Expand Down Expand Up @@ -1407,6 +1410,7 @@ class CEditor : public IEditor
void DoQuadEnvelopes(const std::vector<CQuad> &vQuads, IGraphics::CTextureHandle Texture = IGraphics::CTextureHandle());
void DoQuadEnvPoint(const CQuad *pQuad, int QIndex, int pIndex);
void DoQuadPoint(CQuad *pQuad, int QuadIndex, int v);
void SetHotQuadPoint(CLayerQuads *pLayer);

float TriangleArea(vec2 A, vec2 B, vec2 C);
bool IsInTriangle(vec2 Point, vec2 A, vec2 B, vec2 C);
Expand Down Expand Up @@ -1442,8 +1446,11 @@ class CEditor : public IEditor

void RenderEnvelopeEditor(CUIRect View);
void RenderServerSettingsEditor(CUIRect View, bool ShowServerSettingsEditorLast);

void RenderExtraEditorDragBar(CUIRect View, CUIRect DragBar);

void SetHotEnvelopePoint(const CUIRect &View, CEnvelope *pEnvelope);

void RenderMenubar(CUIRect Menubar);
void RenderFileDialog();

Expand Down

0 comments on commit 1f1cc80

Please sign in to comment.