Skip to content

Commit c5bc3ba

Browse files
committed
DrawList: rework _SelectFringeTexture() for use by third-party extensions + rename to _SelectLineTexture().
1 parent 56a3017 commit c5bc3ba

2 files changed

Lines changed: 15 additions & 32 deletions

File tree

imgui.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3670,7 +3670,7 @@ struct ImDrawList
36703670
IMGUI_API void _AddRectBaked(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float r, float t, const ImVec4& tex_uvs, ImDrawFlags flags);
36713671
IMGUI_API void _AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float thickness, ImDrawFlags flags);
36723672
IMGUI_API void _AddRectTinyRounding(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, float thickness, ImDrawFlags flags);
3673-
IMGUI_API void _SelectFringeTexture(float screen_thickness, ImVec4* out_tex_uvs, float* out_fringe, ImDrawFlags flags);
3673+
IMGUI_API void _SelectLineTexture(float screen_thickness, ImVec2* out_uv0, ImVec2* out_uv1, float* out_fringe, ImDrawFlags flags);
36743674
IMGUI_API float _CalculateCenterBiasedOffset(float thickness);
36753675
IMGUI_API void _AddPolyline(const ImVec2* points, int num_points, ImU32 col, float thickness, ImDrawFlags flags, float max_inner_thickness);
36763676
};

imgui_draw.cpp

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ static ImU32 ImAlphaMultiply(ImU32 col, float alpha_mul)
909909
}
910910

911911
// Select texture and adjust fringe size for specified line thickness.
912-
void ImDrawList::_SelectFringeTexture(float screen_thickness, ImVec4* out_tex_uvs, float* out_fringe, ImDrawFlags flags)
912+
void ImDrawList::_SelectLineTexture(float screen_thickness, ImVec2* out_uv0, ImVec2* out_uv1, float* out_fringe, ImDrawFlags flags)
913913
{
914914
// We assume that the screen_thickness has been clamped to 1.0 by the caller.
915915
IM_ASSERT_PARANOID(screen_thickness >= 1.0f);
@@ -929,13 +929,15 @@ void ImDrawList::_SelectFringeTexture(float screen_thickness, ImVec4* out_tex_uv
929929

930930
// Scale the fringe so that the texture matches the line thickness.
931931
ImVec4 tex_uvs = _Data->TexUvLines[texture_idx];
932-
*out_tex_uvs = tex_uvs;
932+
out_uv0->x = tex_uvs.x;
933+
out_uv1->x = tex_uvs.y;
934+
out_uv0->y = out_uv1->y = tex_uvs.z;
933935
*out_fringe = _FringeScale * screen_thickness * tex_uvs.w; // tex_uvs.w = 1 / thickness
934936
}
935937
else
936938
{
937939
// For no anti-alias we use fully opaque texture, and no fringe expansion.
938-
*out_tex_uvs = ImVec4(_Data->TexUvWhitePixel.x, _Data->TexUvWhitePixel.x, _Data->TexUvWhitePixel.y, 0.0f); // u0, u1, v, 1/thickness (unused)
940+
*out_uv0 = *out_uv1 = _Data->TexUvWhitePixel;
939941
*out_fringe = 0.0f;
940942
}
941943
}
@@ -1003,9 +1005,9 @@ void ImDrawList::_AddPolyline(const ImVec2* points, const int points_count, ImU3
10031005
sqr_lengths[points_count - 1] = 0.0f;
10041006
}
10051007

1006-
ImVec4 tex_uvs;
1008+
ImVec2 uv0, uv1;
10071009
float fringe;
1008-
_SelectFringeTexture(screen_thickness, &tex_uvs, &fringe, flags);
1010+
_SelectLineTexture(screen_thickness, &uv0, &uv1, &fringe, flags);
10091011

10101012
const ImU32 col_trans = col & ~IM_COL32_A_MASK;
10111013

@@ -1043,13 +1045,7 @@ void ImDrawList::_AddPolyline(const ImVec2* points, const int points_count, ImU3
10431045
PrimReserve(idx_count, vtx_count);
10441046

10451047
const float ratio = thickness0 / (thickness0 + thickness1); // The points that use uv2 are placed directly on the path (offset = 0), calculate the texture position from stroke offsets.
1046-
ImVec2 uv0, uv1, uv2;
1047-
uv0.x = tex_uvs.x;
1048-
uv0.y = tex_uvs.z;
1049-
uv1.x = tex_uvs.y;
1050-
uv1.y = tex_uvs.z;
1051-
uv2.x = uv0.x + (uv1.x - uv0.x) * ratio;
1052-
uv2.y = tex_uvs.z;
1048+
const ImVec2 uv2(uv0.x + (uv1.x - uv0.x) * ratio, uv0.y);
10531049

10541050
// Clamp inner offset and adjust texture coordinates to match the change.
10551051
// This is used by some of the convex shape rendering to avoid rendering artifacts when thickness is about to fill the whole shape.
@@ -1878,9 +1874,9 @@ void ImDrawList::_AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float t
18781874
}
18791875

18801876
flags |= Flags;
1881-
ImVec4 tex_uvs;
1877+
ImVec2 uv0, uv1;
18821878
float fringe;
1883-
_SelectFringeTexture(screen_thickness, &tex_uvs, &fringe, flags);
1879+
_SelectLineTexture(screen_thickness, &uv0, &uv1, &fringe, flags);
18841880

18851881
float dir_x = p2.x - p1.x;
18861882
float dir_y = p2.y - p1.y;
@@ -1907,13 +1903,7 @@ void ImDrawList::_AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float t
19071903
float thickness1 = (thickness + fringe) - thickness0;
19081904

19091905
const float ratio = thickness0 / (thickness0 + thickness1); // The points using uv2 are placed on the path, calculate the position from stroke offets.
1910-
ImVec2 uv0, uv1, uv2;
1911-
uv0.x = tex_uvs.x;
1912-
uv0.y = tex_uvs.z;
1913-
uv1.x = tex_uvs.y;
1914-
uv1.y = tex_uvs.z;
1915-
uv2.x = uv0.x + (uv1.x - uv0.x) * ratio;
1916-
uv2.y = tex_uvs.z;
1906+
const ImVec2 uv2(uv0.x + (uv1.x - uv0.x) * ratio, uv0.y);
19171907

19181908
const float half_aa = _FringeScale * 0.5f; // Used for end caps, does not use "fringe" since end cap AA is not using the texture.
19191909
const float half_thickness = thickness * 0.5f;
@@ -1994,7 +1984,6 @@ void ImDrawList::_AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float t
19941984
// AA cap
19951985
IM_APPEND_TRI(base_idx + 0, base_idx + 2, base_idx + 3);
19961986
IM_APPEND_TRI(base_idx + 0, base_idx + 3, base_idx + 1);
1997-
19981987
}
19991988
}
20001989

@@ -2241,9 +2230,9 @@ void ImDrawList::_AddRectTinyRounding(const ImVec2& p_min, const ImVec2& p_max,
22412230
}
22422231

22432232
flags |= Flags;
2244-
ImVec4 tex_uvs;
2233+
ImVec2 outer_uv, inner_uv;
22452234
float fringe;
2246-
_SelectFringeTexture(screen_thickness, &tex_uvs, &fringe, flags);
2235+
_SelectLineTexture(screen_thickness, &outer_uv, &inner_uv, &fringe, flags);
22472236

22482237
const int auto_seg_count = _CalcCircleAutoSegmentCount(rounding);
22492238
int arc_step, arc_step_count;
@@ -2272,13 +2261,7 @@ void ImDrawList::_AddRectTinyRounding(const ImVec2& p_min, const ImVec2& p_max,
22722261

22732262
const float stem_offset = ImMin(rounding, thickness); // The function might get called with rounding slightly bigger than thickness, which is ok, but we should keep the stem inside the shape.
22742263
const float ratio = stem_offset / thickness;
2275-
ImVec2 inner_uv, outer_uv, stem_uv;
2276-
outer_uv.x = tex_uvs.x;
2277-
outer_uv.y = tex_uvs.z;
2278-
inner_uv.x = tex_uvs.y;
2279-
inner_uv.y = tex_uvs.z;
2280-
stem_uv.x = outer_uv.x + (inner_uv.x - outer_uv.x) * ratio;
2281-
stem_uv.y = tex_uvs.z;
2264+
const ImVec2 stem_uv(outer_uv.x + (inner_uv.x - outer_uv.x) * ratio, outer_uv.y);
22822265

22832266
// Previous vertices, copied from the last vertices.
22842267
int base_idx = (int)_VtxCurrentIdx;

0 commit comments

Comments
 (0)