mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
gg: improve drawing effect and logic of draw_rounded_rect_empty (#25062)
Some checks failed
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
Workflow Lint / lint-yml-workflows (push) Has been cancelled
Some checks failed
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
Workflow Lint / lint-yml-workflows (push) Has been cancelled
This commit is contained in:
parent
1f9fd73d0d
commit
a2f65c6977
1 changed files with 58 additions and 63 deletions
121
vlib/gg/draw.c.v
121
vlib/gg/draw.c.v
|
@ -290,89 +290,84 @@ pub fn (ctx &Context) draw_rounded_rect_empty(x f32, y f32, w f32, h f32, radius
|
||||||
sgl.c4b(c.r, c.g, c.b, c.a)
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
||||||
|
|
||||||
mut new_radius := radius
|
mut new_radius := radius
|
||||||
|
if radius < 1 {
|
||||||
|
new_radius = 0
|
||||||
|
}
|
||||||
if w >= h && radius > h / 2 {
|
if w >= h && radius > h / 2 {
|
||||||
new_radius = h / 2
|
new_radius = h / 2
|
||||||
} else if radius > w / 2 {
|
} else if radius > w / 2 {
|
||||||
new_radius = w / 2
|
new_radius = w / 2
|
||||||
}
|
}
|
||||||
|
|
||||||
r := new_radius * ctx.scale
|
r := new_radius * ctx.scale
|
||||||
sx := x * ctx.scale // start point x
|
sx := x * ctx.scale // start point x
|
||||||
sy := y * ctx.scale
|
sy := y * ctx.scale
|
||||||
width := w * ctx.scale
|
width := w * ctx.scale
|
||||||
height := h * ctx.scale
|
height := h * ctx.scale
|
||||||
|
|
||||||
|
align_pixel := fn (v f32) f32 {
|
||||||
|
return math.floorf(v) + 0.5
|
||||||
|
}
|
||||||
// circle center coordinates
|
// circle center coordinates
|
||||||
ltx := sx + r
|
ltx := align_pixel(sx + r)
|
||||||
lty := sy + r
|
lty := align_pixel(sy + r)
|
||||||
rtx := sx + width - r
|
rtx := align_pixel(sx + width - r)
|
||||||
rty := lty
|
rty := align_pixel(sy + r)
|
||||||
rbx := rtx
|
rbx := align_pixel(sx + width - r)
|
||||||
rby := sy + height - r
|
rby := align_pixel(sy + height - r)
|
||||||
lbx := ltx
|
lbx := align_pixel(sx + r)
|
||||||
lby := rby
|
lby := align_pixel(sy + height - r)
|
||||||
|
|
||||||
mut rad := f32(0)
|
mut rad := f32(0)
|
||||||
mut dx := f32(0)
|
mut dx := f32(0)
|
||||||
mut dy := f32(0)
|
mut dy := f32(0)
|
||||||
|
|
||||||
if r != 0 {
|
if r == 0 {
|
||||||
// left top quarter
|
|
||||||
sgl.begin_line_strip()
|
sgl.begin_line_strip()
|
||||||
for i in 0 .. 31 {
|
// top
|
||||||
rad = f32(math.radians(i * 3))
|
sgl.v2f(ltx, lty)
|
||||||
dx = r * math.cosf(rad)
|
sgl.v2f(rtx, rty)
|
||||||
dy = r * math.sinf(rad)
|
// right
|
||||||
sgl.v2f(ltx - dx, lty - dy)
|
sgl.v2f(rbx, rby)
|
||||||
}
|
// bottom
|
||||||
sgl.end()
|
sgl.v2f(lbx, lby)
|
||||||
|
// left
|
||||||
// right top quarter
|
sgl.v2f(ltx, lty)
|
||||||
sgl.begin_line_strip()
|
|
||||||
for i in 0 .. 31 {
|
|
||||||
rad = f32(math.radians(i * 3))
|
|
||||||
dx = r * math.cosf(rad)
|
|
||||||
dy = r * math.sinf(rad)
|
|
||||||
sgl.v2f(rtx + dx, rty - dy)
|
|
||||||
}
|
|
||||||
sgl.end()
|
|
||||||
|
|
||||||
// right bottom quarter
|
|
||||||
sgl.begin_line_strip()
|
|
||||||
for i in 0 .. 31 {
|
|
||||||
rad = f32(math.radians(i * 3))
|
|
||||||
dx = r * math.cosf(rad)
|
|
||||||
dy = r * math.sinf(rad)
|
|
||||||
sgl.v2f(rbx + dx, rby + dy)
|
|
||||||
}
|
|
||||||
sgl.end()
|
|
||||||
|
|
||||||
// left bottom quarter
|
|
||||||
sgl.begin_line_strip()
|
|
||||||
for i in 0 .. 31 {
|
|
||||||
rad = f32(math.radians(i * 3))
|
|
||||||
dx = r * math.cosf(rad)
|
|
||||||
dy = r * math.sinf(rad)
|
|
||||||
sgl.v2f(lbx - dx, lby + dy)
|
|
||||||
}
|
|
||||||
sgl.end()
|
sgl.end()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Currently don't use 'gg.draw_line()' directly, it will repeatedly execute '*ctx.scale'.
|
sgl.begin_line_strip()
|
||||||
sgl.begin_lines()
|
// left top quarter
|
||||||
// top
|
for i in 0 .. 31 {
|
||||||
sgl.v2f(ltx, sy)
|
rad = f32(math.radians(i * 3))
|
||||||
sgl.v2f(rtx, sy)
|
dx = r * math.cosf(rad)
|
||||||
// right
|
dy = r * math.sinf(rad)
|
||||||
sgl.v2f(rtx + r, rty)
|
sgl.v2f(ltx - dx, lty - dy)
|
||||||
sgl.v2f(rtx + r, rby)
|
}
|
||||||
// bottom
|
// right top quarter
|
||||||
// Note: test on native windows, macos, and linux if you need to change the offset literal here,
|
for i in 0 .. 31 {
|
||||||
// with `v run vlib/gg/testdata/draw_rounded_rect_empty.vv` . Using 1 here, looks good on windows,
|
rad = f32(math.radians(i * 3))
|
||||||
// and on linux with LIBGL_ALWAYS_SOFTWARE=true, but misaligned on native macos and linux.
|
dx = r * math.sinf(rad)
|
||||||
sgl.v2f(lbx, lby + r - 0.5)
|
dy = r * math.cosf(rad)
|
||||||
sgl.v2f(rbx, rby + r - 0.5)
|
sgl.v2f(rtx + dx, rty - dy)
|
||||||
// left
|
}
|
||||||
sgl.v2f(sx + 1, lty)
|
// right bottom quarter
|
||||||
sgl.v2f(sx + 1, lby)
|
for i in 0 .. 31 {
|
||||||
|
rad = f32(math.radians(i * 3))
|
||||||
|
dx = r * math.cosf(rad)
|
||||||
|
dy = r * math.sinf(rad)
|
||||||
|
sgl.v2f(rbx + dx, rby + dy)
|
||||||
|
}
|
||||||
|
// left bottom quarter
|
||||||
|
for i in 0 .. 31 {
|
||||||
|
rad = f32(math.radians(i * 3))
|
||||||
|
dx = r * math.sinf(rad)
|
||||||
|
dy = r * math.cosf(rad)
|
||||||
|
sgl.v2f(lbx - dx, lby + dy)
|
||||||
|
}
|
||||||
|
// close
|
||||||
|
sgl.v2f(ltx - r, lty)
|
||||||
sgl.end()
|
sgl.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue