Merge pull request #7956 from archimede67/editor-fix-quads

Editor: fix several quads related issues
This commit is contained in:
Dennis Felsing 2024-02-09 23:13:30 +00:00 committed by GitHub
commit a7bbcf00a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 35 deletions

View file

@ -319,7 +319,7 @@ int CEditor::DoButton_DraggableEx(const void *pID, const char *pText, int Checke
return UI()->DoDraggableButtonLogic(pID, Checked, pRect, pClicked, pAbrupted); return UI()->DoDraggableButtonLogic(pID, Checked, pRect, pClicked, pAbrupted);
} }
void CEditor::RenderBackground(CUIRect View, IGraphics::CTextureHandle Texture, float Size, float Brightness) void CEditor::RenderBackground(CUIRect View, IGraphics::CTextureHandle Texture, float Size, float Brightness) const
{ {
Graphics()->TextureSet(Texture); Graphics()->TextureSet(Texture);
Graphics()->BlendNormal(); Graphics()->BlendNormal();
@ -1547,7 +1547,7 @@ void CEditor::DoPointDrag(const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQu
pQuad->m_aPoints[PointIndex].y = m_QuadDragOriginalPoints[QuadIndex][PointIndex].y + OffsetY; pQuad->m_aPoints[PointIndex].y = m_QuadDragOriginalPoints[QuadIndex][PointIndex].y + OffsetY;
} }
CEditor::EAxis CEditor::GetDragAxis(int OffsetX, int OffsetY) CEditor::EAxis CEditor::GetDragAxis(int OffsetX, int OffsetY) const
{ {
if(Input()->ShiftIsPressed()) if(Input()->ShiftIsPressed())
if(absolute(OffsetX) < absolute(OffsetY)) if(absolute(OffsetX) < absolute(OffsetY))
@ -1558,7 +1558,7 @@ CEditor::EAxis CEditor::GetDragAxis(int OffsetX, int OffsetY)
return EAxis::AXIS_NONE; return EAxis::AXIS_NONE;
} }
void CEditor::DrawAxis(EAxis Axis, CPoint &OriginalPoint, CPoint &Point) void CEditor::DrawAxis(EAxis Axis, CPoint &OriginalPoint, CPoint &Point) const
{ {
if(Axis == EAxis::AXIS_NONE) if(Axis == EAxis::AXIS_NONE)
return; return;
@ -1587,6 +1587,8 @@ void CEditor::ComputePointAlignments(const std::shared_ptr<CLayerQuads> &pLayer,
if(!g_Config.m_EdAlignQuads) if(!g_Config.m_EdAlignQuads)
return; return;
bool IgnoreGrid = Input()->AltIsPressed();
// Perform computation from the original position of this point // Perform computation from the original position of this point
int Threshold = f2fx(maximum(10.0f, 10.0f * m_MouseWScale)); int Threshold = f2fx(maximum(10.0f, 10.0f * m_MouseWScale));
CPoint OrigPoint = m_QuadDragOriginalPoints.at(QuadIndex)[PointIndex]; CPoint OrigPoint = m_QuadDragOriginalPoints.at(QuadIndex)[PointIndex];
@ -1605,8 +1607,14 @@ void CEditor::ComputePointAlignments(const std::shared_ptr<CLayerQuads> &pLayer,
int DiffX = absolute(DX); int DiffX = absolute(DX);
int DiffY = absolute(DY); int DiffY = absolute(DY);
// Check the X axis const float PX = fx2f(pQuadPoint->x);
if(DiffX <= Threshold) const float PY = fx2f(pQuadPoint->y);
float SnappedPX = PX;
float SnappedPY = PY;
if(MapView()->MapGrid()->IsEnabled() && !IgnoreGrid)
MapView()->MapGrid()->SnapToGrid(SnappedPX, SnappedPY);
if(DiffX <= Threshold && (SnappedPX == PX || DiffX == 0))
{ {
// Only store alignments that have the smallest difference // Only store alignments that have the smallest difference
if(DiffX < SmallestDiffX) if(DiffX < SmallestDiffX)
@ -1627,7 +1635,8 @@ void CEditor::ComputePointAlignments(const std::shared_ptr<CLayerQuads> &pLayer,
}); });
} }
} }
if(DiffY <= Threshold)
if(DiffY <= Threshold && (SnappedPY == PY || DiffY == 0))
{ {
// Only store alignments that have the smallest difference // Only store alignments that have the smallest difference
if(DiffY < SmallestDiffY) if(DiffY < SmallestDiffY)
@ -1777,6 +1786,8 @@ void CEditor::ComputeAABBAlignments(const std::shared_ptr<CLayerQuads> &pLayer,
int SmallestDiffX = Threshold + 1, SmallestDiffY = Threshold + 1; int SmallestDiffX = Threshold + 1, SmallestDiffY = Threshold + 1;
std::vector<SAlignmentInfo> vAlignmentsX, vAlignmentsY; std::vector<SAlignmentInfo> vAlignmentsX, vAlignmentsY;
bool IgnoreGrid = Input()->AltIsPressed();
auto &&CheckAlignment = [&](CPoint &Aligned, int Point) { auto &&CheckAlignment = [&](CPoint &Aligned, int Point) {
CPoint ToCheck = AABB.m_aPoints[Point] + ivec2(OffsetX, OffsetY); CPoint ToCheck = AABB.m_aPoints[Point] + ivec2(OffsetX, OffsetY);
int DX = Aligned.x - ToCheck.x; int DX = Aligned.x - ToCheck.x;
@ -1784,7 +1795,14 @@ void CEditor::ComputeAABBAlignments(const std::shared_ptr<CLayerQuads> &pLayer,
int DiffX = absolute(DX); int DiffX = absolute(DX);
int DiffY = absolute(DY); int DiffY = absolute(DY);
if(DiffX <= Threshold) const float PX = fx2f(Aligned.x);
const float PY = fx2f(Aligned.y);
float SnappedPX = PX;
float SnappedPY = PY;
if(MapView()->MapGrid()->IsEnabled() && !IgnoreGrid)
MapView()->MapGrid()->SnapToGrid(SnappedPX, SnappedPY);
if(DiffX <= Threshold && (SnappedPX == PX || DiffX == 0))
{ {
if(DiffX < SmallestDiffX) if(DiffX < SmallestDiffX)
{ {
@ -1803,7 +1821,8 @@ void CEditor::ComputeAABBAlignments(const std::shared_ptr<CLayerQuads> &pLayer,
}); });
} }
} }
if(DiffY <= Threshold)
if(DiffY <= Threshold && (SnappedPY == PY || DiffY == 0))
{ {
if(DiffY < SmallestDiffY) if(DiffY < SmallestDiffY)
{ {
@ -1873,7 +1892,7 @@ void CEditor::ComputeAABBAlignments(const std::shared_ptr<CLayerQuads> &pLayer,
vAlignments.insert(vAlignments.end(), vAlignmentsY.begin(), vAlignmentsY.end()); vAlignments.insert(vAlignments.end(), vAlignmentsY.begin(), vAlignmentsY.end());
} }
void CEditor::DrawPointAlignments(const std::vector<SAlignmentInfo> &vAlignments, int OffsetX, int OffsetY) void CEditor::DrawPointAlignments(const std::vector<SAlignmentInfo> &vAlignments, int OffsetX, int OffsetY) const
{ {
if(!g_Config.m_EdAlignQuads) if(!g_Config.m_EdAlignQuads)
return; return;
@ -1897,7 +1916,7 @@ void CEditor::DrawPointAlignments(const std::vector<SAlignmentInfo> &vAlignments
} }
} }
void CEditor::DrawAABB(const SAxisAlignedBoundingBox &AABB, int OffsetX, int OffsetY) void CEditor::DrawAABB(const SAxisAlignedBoundingBox &AABB, int OffsetX, int OffsetY) const
{ {
// Drawing an AABB is simply converting the points from fixed to float // Drawing an AABB is simply converting the points from fixed to float
// Then making lines out of quads and drawing them // Then making lines out of quads and drawing them
@ -1935,12 +1954,13 @@ void CEditor::QuadSelectionAABB(const std::shared_ptr<CLayerQuads> &pLayer, SAxi
for(int Selected : m_vSelectedQuads) for(int Selected : m_vSelectedQuads)
{ {
CQuad *pQuad = &pLayer->m_vQuads[Selected]; CQuad *pQuad = &pLayer->m_vQuads[Selected];
for(auto &Point : pQuad->m_aPoints) for(int i = 0; i < 4; i++)
{ {
Min.x = minimum(Min.x, Point.x); auto *pPoint = &pQuad->m_aPoints[i];
Min.y = minimum(Min.y, Point.y); Min.x = minimum(Min.x, pPoint->x);
Max.x = maximum(Max.x, Point.x); Min.y = minimum(Min.y, pPoint->y);
Max.y = maximum(Max.y, Point.y); Max.x = maximum(Max.x, pPoint->x);
Max.y = maximum(Max.y, pPoint->y);
} }
} }
CPoint Center = (Min + Max) / 2.0f; CPoint Center = (Min + Max) / 2.0f;
@ -1986,7 +2006,7 @@ void CEditor::ApplyAlignments(const std::vector<SAlignmentInfo> &vAlignments, in
OffsetY += AdjustY; OffsetY += AdjustY;
} }
void CEditor::ApplyAxisAlignment(int &OffsetX, int &OffsetY) void CEditor::ApplyAxisAlignment(int &OffsetX, int &OffsetY) const
{ {
// This is used to preserve axis alignment when pressing `Shift` // This is used to preserve axis alignment when pressing `Shift`
// Should be called before any other computation // Should be called before any other computation
@ -3363,7 +3383,7 @@ void CEditor::DoMapEditor(CUIRect View)
m_pBrush->Clear(); m_pBrush->Clear();
} }
if(UI()->MouseButton(0) && s_Operation == OP_NONE && !m_QuadKnifeActive) if(!Input()->ModifierIsPressed() && UI()->MouseButton(0) && s_Operation == OP_NONE && !m_QuadKnifeActive)
{ {
UI()->SetActiveItem(s_pEditorID); UI()->SetActiveItem(s_pEditorID);
@ -8341,7 +8361,7 @@ void CEditor::Reset(bool CreateDefault)
m_MapSettingsCommandContext.Reset(); m_MapSettingsCommandContext.Reset();
} }
int CEditor::GetTextureUsageFlag() int CEditor::GetTextureUsageFlag() const
{ {
return Graphics()->Uses2DTextureArrays() ? IGraphics::TEXLOAD_TO_2D_ARRAY_TEXTURE : IGraphics::TEXLOAD_TO_3D_TEXTURE; return Graphics()->Uses2DTextureArrays() ? IGraphics::TEXLOAD_TO_2D_ARRAY_TEXTURE : IGraphics::TEXLOAD_TO_3D_TEXTURE;
} }

View file

@ -288,7 +288,7 @@ class CEditor : public IEditor
IGraphics::CTextureHandle m_SwitchTexture; IGraphics::CTextureHandle m_SwitchTexture;
IGraphics::CTextureHandle m_TuneTexture; IGraphics::CTextureHandle m_TuneTexture;
int GetTextureUsageFlag(); int GetTextureUsageFlag() const;
enum EPreviewState enum EPreviewState
{ {
@ -301,16 +301,16 @@ class CEditor : public IEditor
static const ColorRGBA ms_DefaultPropColor; static const ColorRGBA ms_DefaultPropColor;
public: public:
class IInput *Input() { return m_pInput; } class IInput *Input() const { return m_pInput; }
class IClient *Client() { return m_pClient; } class IClient *Client() const { return m_pClient; }
class IConfigManager *ConfigManager() { return m_pConfigManager; } class IConfigManager *ConfigManager() const { return m_pConfigManager; }
class CConfig *Config() { return m_pConfig; } class CConfig *Config() const { return m_pConfig; }
class IConsole *Console() { return m_pConsole; } class IConsole *Console() const { return m_pConsole; }
class IEngine *Engine() { return m_pEngine; } class IEngine *Engine() const { return m_pEngine; }
class IGraphics *Graphics() { return m_pGraphics; } class IGraphics *Graphics() const { return m_pGraphics; }
class ISound *Sound() { return m_pSound; } class ISound *Sound() const { return m_pSound; }
class ITextRender *TextRender() { return m_pTextRender; } class ITextRender *TextRender() const { return m_pTextRender; }
class IStorage *Storage() { return m_pStorage; } class IStorage *Storage() const { return m_pStorage; }
CUI *UI() { return &m_UI; } CUI *UI() { return &m_UI; }
CRenderTools *RenderTools() { return &m_RenderTools; } CRenderTools *RenderTools() { return &m_RenderTools; }
@ -823,7 +823,7 @@ public:
template<typename T> template<typename T>
int RenderEditBoxDropdown(SEditBoxDropdownContext *pDropdown, CUIRect View, CLineInput *pLineInput, int x, float MaxHeight, bool AutoWidth, const std::vector<T> &vData, const FDropdownRenderCallback<T> &fnMatchCallback); int RenderEditBoxDropdown(SEditBoxDropdownContext *pDropdown, CUIRect View, CLineInput *pLineInput, int x, float MaxHeight, bool AutoWidth, const std::vector<T> &vData, const FDropdownRenderCallback<T> &fnMatchCallback);
void RenderBackground(CUIRect View, IGraphics::CTextureHandle Texture, float Size, float Brightness); void RenderBackground(CUIRect View, IGraphics::CTextureHandle Texture, float Size, float Brightness) const;
SEditResult<int> UiDoValueSelector(void *pID, CUIRect *pRect, const char *pLabel, int Current, int Min, int Max, int Step, float Scale, const char *pToolTip, bool IsDegree = false, bool IsHex = false, int corners = IGraphics::CORNER_ALL, const ColorRGBA *pColor = nullptr, bool ShowValue = true); SEditResult<int> UiDoValueSelector(void *pID, CUIRect *pRect, const char *pLabel, int Current, int Min, int Max, int Step, float Scale, const char *pToolTip, bool IsDegree = false, bool IsHex = false, int corners = IGraphics::CORNER_ALL, const ColorRGBA *pColor = nullptr, bool ShowValue = true);
@ -919,9 +919,9 @@ public:
void DoQuad(int LayerIndex, const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int Index); void DoQuad(int LayerIndex, const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int Index);
void PreparePointDrag(const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int PointIndex); void PreparePointDrag(const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int PointIndex);
void DoPointDrag(const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int PointIndex, int OffsetX, int OffsetY); void DoPointDrag(const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int PointIndex, int OffsetX, int OffsetY);
EAxis GetDragAxis(int OffsetX, int OffsetY); EAxis GetDragAxis(int OffsetX, int OffsetY) const;
void DrawAxis(EAxis Axis, CPoint &OriginalPoint, CPoint &Point); void DrawAxis(EAxis Axis, CPoint &OriginalPoint, CPoint &Point) const;
void DrawAABB(const SAxisAlignedBoundingBox &AABB, int OffsetX = 0, int OffsetY = 0); void DrawAABB(const SAxisAlignedBoundingBox &AABB, int OffsetX = 0, int OffsetY = 0) const;
ColorRGBA GetButtonColor(const void *pID, int Checked); ColorRGBA GetButtonColor(const void *pID, int Checked);
// Alignment methods // Alignment methods
@ -945,10 +945,10 @@ public:
void ComputePointAlignments(const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int PointIndex, int OffsetX, int OffsetY, std::vector<SAlignmentInfo> &vAlignments, bool Append = false) const; void ComputePointAlignments(const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int PointIndex, int OffsetX, int OffsetY, std::vector<SAlignmentInfo> &vAlignments, bool Append = false) const;
void ComputePointsAlignments(const std::shared_ptr<CLayerQuads> &pLayer, bool Pivot, int OffsetX, int OffsetY, std::vector<SAlignmentInfo> &vAlignments) const; void ComputePointsAlignments(const std::shared_ptr<CLayerQuads> &pLayer, bool Pivot, int OffsetX, int OffsetY, std::vector<SAlignmentInfo> &vAlignments) const;
void ComputeAABBAlignments(const std::shared_ptr<CLayerQuads> &pLayer, const SAxisAlignedBoundingBox &AABB, int OffsetX, int OffsetY, std::vector<SAlignmentInfo> &vAlignments) const; void ComputeAABBAlignments(const std::shared_ptr<CLayerQuads> &pLayer, const SAxisAlignedBoundingBox &AABB, int OffsetX, int OffsetY, std::vector<SAlignmentInfo> &vAlignments) const;
void DrawPointAlignments(const std::vector<SAlignmentInfo> &vAlignments, int OffsetX, int OffsetY); void DrawPointAlignments(const std::vector<SAlignmentInfo> &vAlignments, int OffsetX, int OffsetY) const;
void QuadSelectionAABB(const std::shared_ptr<CLayerQuads> &pLayer, SAxisAlignedBoundingBox &OutAABB); void QuadSelectionAABB(const std::shared_ptr<CLayerQuads> &pLayer, SAxisAlignedBoundingBox &OutAABB);
void ApplyAlignments(const std::vector<SAlignmentInfo> &vAlignments, int &OffsetX, int &OffsetY); void ApplyAlignments(const std::vector<SAlignmentInfo> &vAlignments, int &OffsetX, int &OffsetY);
void ApplyAxisAlignment(int &OffsetX, int &OffsetY); void ApplyAxisAlignment(int &OffsetX, int &OffsetY) const;
bool ReplaceImage(const char *pFilename, int StorageType, bool CheckDuplicate); bool ReplaceImage(const char *pFilename, int StorageType, bool CheckDuplicate);
static bool ReplaceImageCallback(const char *pFilename, int StorageType, void *pUser); static bool ReplaceImageCallback(const char *pFilename, int StorageType, void *pUser);