gg: improve some loops in draw_rounded_rect* methods (#14195)

master
tzSharing 2022-04-28 03:57:48 +08:00 committed by GitHub
parent f53b9b4f12
commit eb03fad934
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 70 additions and 90 deletions

View File

@ -250,57 +250,47 @@ pub fn (ctx &Context) draw_rounded_rect_empty(x f32, y f32, w f32, h f32, radius
mut dx := f32(0) mut dx := f32(0)
mut dy := f32(0) mut dy := f32(0)
// left top quarter if r != 0 {
sgl.begin_line_strip() // left top quarter
for i in 0 .. 31 { sgl.begin_line_strip()
if r == 0 { for i in 0 .. 31 {
break rad = f32(math.radians(i * 3))
dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(ltx - dx, lty - dy)
} }
rad = f32(math.radians(i * 3)) sgl.end()
dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(ltx - dx, lty - dy)
}
sgl.end()
// right top quarter // right top quarter
sgl.begin_line_strip() sgl.begin_line_strip()
for i in 0 .. 31 { for i in 0 .. 31 {
if r == 0 { rad = f32(math.radians(i * 3))
break dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(rtx + dx, rty - dy)
} }
rad = f32(math.radians(i * 3)) sgl.end()
dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(rtx + dx, rty - dy)
}
sgl.end()
// right bottom quarter // right bottom quarter
sgl.begin_line_strip() sgl.begin_line_strip()
for i in 0 .. 31 { for i in 0 .. 31 {
if r == 0 { rad = f32(math.radians(i * 3))
break dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(rbx + dx, rby + dy)
} }
rad = f32(math.radians(i * 3)) sgl.end()
dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(rbx + dx, rby + dy)
}
sgl.end()
// left bottom quarter // left bottom quarter
sgl.begin_line_strip() sgl.begin_line_strip()
for i in 0 .. 31 { for i in 0 .. 31 {
if r == 0 { rad = f32(math.radians(i * 3))
break dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(lbx - dx, lby + dy)
} }
rad = f32(math.radians(i * 3)) sgl.end()
dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(lbx - dx, lby + dy)
} }
sgl.end()
// Currently don't use 'gg.draw_line()' directly, it will repeatedly execute '*ctx.scale'. // Currently don't use 'gg.draw_line()' directly, it will repeatedly execute '*ctx.scale'.
sgl.begin_lines() sgl.begin_lines()
@ -358,61 +348,51 @@ pub fn (ctx &Context) draw_rounded_rect_filled(x f32, y f32, w f32, h f32, radiu
mut dx := f32(0) mut dx := f32(0)
mut dy := f32(0) mut dy := f32(0)
// left top quarter if r != 0 {
sgl.begin_triangle_strip() // left top quarter
for i in 0 .. 31 { sgl.begin_triangle_strip()
if r == 0 { for i in 0 .. 31 {
break rad = f32(math.radians(i * 3))
dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(ltx - dx, lty - dy)
sgl.v2f(ltx, lty)
} }
rad = f32(math.radians(i * 3)) sgl.end()
dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(ltx - dx, lty - dy)
sgl.v2f(ltx, lty)
}
sgl.end()
// right top quarter // right top quarter
sgl.begin_triangle_strip() sgl.begin_triangle_strip()
for i in 0 .. 31 { for i in 0 .. 31 {
if r == 0 { rad = f32(math.radians(i * 3))
break dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(rtx + dx, rty - dy)
sgl.v2f(rtx, rty)
} }
rad = f32(math.radians(i * 3)) sgl.end()
dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(rtx + dx, rty - dy)
sgl.v2f(rtx, rty)
}
sgl.end()
// right bottom quarter // right bottom quarter
sgl.begin_triangle_strip() sgl.begin_triangle_strip()
for i in 0 .. 31 { for i in 0 .. 31 {
if r == 0 { rad = f32(math.radians(i * 3))
break dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(rbx + dx, rby + dy)
sgl.v2f(rbx, rby)
} }
rad = f32(math.radians(i * 3)) sgl.end()
dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(rbx + dx, rby + dy)
sgl.v2f(rbx, rby)
}
sgl.end()
// left bottom quarter // left bottom quarter
sgl.begin_triangle_strip() sgl.begin_triangle_strip()
for i in 0 .. 31 { for i in 0 .. 31 {
if r == 0 { rad = f32(math.radians(i * 3))
break dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(lbx - dx, lby + dy)
sgl.v2f(lbx, lby)
} }
rad = f32(math.radians(i * 3)) sgl.end()
dx = r * math.cosf(rad)
dy = r * math.sinf(rad)
sgl.v2f(lbx - dx, lby + dy)
sgl.v2f(lbx, lby)
} }
sgl.end()
// Separate drawing is to prevent transparent color overlap // Separate drawing is to prevent transparent color overlap
// top rectangle // top rectangle