MCPcopy Create free account
hub / github.com/ddnet/ddnet / ComputePointAlignments

Method ComputePointAlignments

src/game/editor/editor.cpp:911–1030  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

909}
910
911void CEditor::ComputePointAlignments(const std::shared_ptr<CLayerQuads> &pLayer, CQuad *pQuad, int QuadIndex, int PointIndex, ivec2 Offset, std::vector<SAlignmentInfo> &vAlignments, bool Append) const
912{
913 if(!Append)
914 vAlignments.clear();
915 if(!g_Config.m_EdAlignQuads)
916 return;
917
918 bool GridEnabled = MapView()->MapGrid()->IsEnabled() && !Input()->AltIsPressed();
919
920 // Perform computation from the original position of this point
921 int Threshold = f2fx(maximum(5.0f, 10.0f * m_MouseWorldScale));
922 CPoint OrigPoint = m_QuadDragOriginalPoints.at(QuadIndex)[PointIndex];
923 // Get the "current" point by applying the offset
924 CPoint Point = OrigPoint + Offset;
925
926 // Save smallest diff on both axis to only keep closest alignments
927 ivec2 SmallestDiff = ivec2(Threshold + 1, Threshold + 1);
928 // Store both axis alignments in separate vectors
929 std::vector<SAlignmentInfo> vAlignmentsX, vAlignmentsY;
930
931 // Check if we can align/snap to a specific point
932 auto &&CheckAlignment = [&](CPoint *pQuadPoint) {
933 ivec2 DirectedDiff = *pQuadPoint - Point;
934 ivec2 Diff = ivec2(absolute(DirectedDiff.x), absolute(DirectedDiff.y));
935
936 if(Diff.x <= Threshold && (!GridEnabled || Diff.x == 0))
937 {
938 // Only store alignments that have the smallest difference
939 if(Diff.x < SmallestDiff.x)
940 {
941 vAlignmentsX.clear();
942 SmallestDiff.x = Diff.x;
943 }
944
945 // We can have multiple alignments having the same difference/distance
946 if(Diff.x == SmallestDiff.x)
947 {
948 vAlignmentsX.push_back(SAlignmentInfo{
949 *pQuadPoint, // Aligned point
950 {OrigPoint.y}, // Value that can change (which is not snapped), original position
951 EAxis::Y, // The alignment axis
952 PointIndex, // The index of the point
953 DirectedDiff.x,
954 });
955 }
956 }
957
958 if(Diff.y <= Threshold && (!GridEnabled || Diff.y == 0))
959 {
960 // Only store alignments that have the smallest difference
961 if(Diff.y < SmallestDiff.y)
962 {
963 vAlignmentsY.clear();
964 SmallestDiff.y = Diff.y;
965 }
966
967 if(Diff.y == SmallestDiff.y)
968 {

Callers

nothing calls this directly

Calls 13

MapViewFunction · 0.85
InputFunction · 0.85
f2fxFunction · 0.85
maximumFunction · 0.85
absoluteFunction · 0.85
MapFunction · 0.85
MapGridMethod · 0.80
AltIsPressedMethod · 0.80
IsQuadSelectedMethod · 0.80
IsQuadCornerSelectedMethod · 0.80
endMethod · 0.80
beginMethod · 0.80

Tested by

no test coverage detected