gg: support transparency for rect/triangle/etc primitives too

pull/6431/head
Delyan Angelov 2020-09-20 12:05:30 +03:00
parent 4ae88c69ac
commit d4fbf422b3
3 changed files with 18 additions and 2 deletions

View File

@ -87,7 +87,7 @@ const (
] ]
background_color = gx.white background_color = gx.white
ui_color = gx.red ui_color = gx.rgba(255,0,0, 210)
) )
// TODO: type Tetro [tetro_size]struct{ x, y int } // TODO: type Tetro [tetro_size]struct{ x, y int }

View File

@ -226,6 +226,9 @@ pub fn (mut ctx Context) set_bg_color(c gx.Color) {
// TODO: Fix alpha // TODO: Fix alpha
pub fn (ctx &Context) draw_rect(x, y, w, h f32, c gx.Color) { pub fn (ctx &Context) draw_rect(x, y, w, h f32, c gx.Color) {
if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
sgl.c4b(c.r, c.g, c.b, c.a) sgl.c4b(c.r, c.g, c.b, c.a)
sgl.begin_quads() sgl.begin_quads()
sgl.v2f(x * ctx.scale, y * ctx.scale) sgl.v2f(x * ctx.scale, y * ctx.scale)
@ -236,6 +239,9 @@ pub fn (ctx &Context) draw_rect(x, y, w, h f32, c gx.Color) {
} }
pub fn (ctx &Context) draw_triangle(x, y, x2, y2, x3, y3 f32, c gx.Color) { pub fn (ctx &Context) draw_triangle(x, y, x2, y2, x3, y3 f32, c gx.Color) {
if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
sgl.c4b(c.r, c.g, c.b, c.a) sgl.c4b(c.r, c.g, c.b, c.a)
sgl.begin_quads() sgl.begin_quads()
sgl.v2f(x * ctx.scale, y * ctx.scale) sgl.v2f(x * ctx.scale, y * ctx.scale)
@ -245,6 +251,9 @@ pub fn (ctx &Context) draw_triangle(x, y, x2, y2, x3, y3 f32, c gx.Color) {
} }
pub fn (ctx &Context) draw_empty_rect(x, y, w, h f32, c gx.Color) { pub fn (ctx &Context) draw_empty_rect(x, y, w, h f32, c gx.Color) {
if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
sgl.c4b(c.r, c.g, c.b, c.a) sgl.c4b(c.r, c.g, c.b, c.a)
sgl.begin_line_strip() sgl.begin_line_strip()
if ctx.scale == 1 { if ctx.scale == 1 {
@ -295,6 +304,9 @@ fn abs(a f32) f32 {
pub fn (ctx &Context) draw_line(x, y, x2, y2 f32, c gx.Color) { pub fn (ctx &Context) draw_line(x, y, x2, y2 f32, c gx.Color) {
if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
if ctx.scale > 1 { if ctx.scale > 1 {
// Make the line more clear on hi dpi screens: draw a rectangle // Make the line more clear on hi dpi screens: draw a rectangle
mut width := abs(x2 - x) mut width := abs(x2 - x)

View File

@ -3,6 +3,7 @@
module gg module gg
import sokol.sfons import sokol.sfons
import sokol.sgl
import gx import gx
import os import os
@ -94,7 +95,10 @@ fn (ctx &Context) set_cfg(cfg gx.TextCfg) {
size := if cfg.mono { cfg.size - 2 } else { cfg.size } size := if cfg.mono { cfg.size - 2 } else { cfg.size }
ctx.ft.fons.set_size(scale * f32(size)) ctx.ft.fons.set_size(scale * f32(size))
C.fonsSetAlign(ctx.ft.fons, int(cfg.align) | int(cfg.vertical_align)) C.fonsSetAlign(ctx.ft.fons, int(cfg.align) | int(cfg.vertical_align))
color := C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, 255) color := C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, cfg.color.a)
if cfg.color.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
C.fonsSetColor(ctx.ft.fons, color) C.fonsSetColor(ctx.ft.fons, color)
ascender := f32(0.0) ascender := f32(0.0)
descender := f32(0.0) descender := f32(0.0)