1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

gg: merge ft module

This commit is contained in:
Alexander Medvednikov 2020-07-06 20:29:05 +02:00
parent 43c8726c37
commit 0ed8199da2
2 changed files with 23 additions and 41 deletions

View File

@ -1,6 +1,5 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license that can be found in the LICENSE file.
// that can be found in the LICENSE file.
module gg module gg
import gx import gx
@ -9,7 +8,6 @@ import sokol
import sokol.sapp import sokol.sapp
import sokol.sgl import sokol.sgl
import sokol.gfx import sokol.gfx
import gg.ft
pub type FNCb fn(x voidptr) pub type FNCb fn(x voidptr)
pub type FNEvent fn(e voidptr, x voidptr) pub type FNEvent fn(e voidptr, x voidptr)
@ -55,7 +53,7 @@ pub mut:
clear_pass C.sg_pass_action clear_pass C.sg_pass_action
window C.sapp_desc window C.sapp_desc
config Config config Config
ft &ft.FT ft &FT
font_inited bool font_inited bool
} }
@ -87,7 +85,7 @@ fn gg_init_sokol_window(user_data voidptr) {
//println('g.scale=$g.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h') //println('g.scale=$g.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h')
//if g.config.init_text { //if g.config.init_text {
if g.config.font_path != '' { if g.config.font_path != '' {
g.ft = ft.new({ font_path: g.config.font_path, scale: sapp.dpi_scale() }) or {panic(err)} g.ft = new_ft({ font_path: g.config.font_path, scale: sapp.dpi_scale() }) or {panic(err)}
g.font_inited = true g.font_inited = true
} }
if g.config.init_fn != voidptr(0) { if g.config.init_fn != voidptr(0) {
@ -272,18 +270,6 @@ pub fn (ctx &Context) draw_rounded_rect(x, y, width, height, radius f32, color g
pub fn (ctx &Context) draw_empty_rounded_rect(x, y, width, height, radius f32, border_color gx.Color) { pub fn (ctx &Context) draw_empty_rounded_rect(x, y, width, height, radius f32, border_color gx.Color) {
} }
pub fn (ctx &Context) draw_text(x, y int, text string, cfg gx.TextCfg) {
if !ctx.font_inited {
return
}
ctx.ft.draw_text(x, y, text, cfg)
}
pub fn (ctx &Context) draw_text_def(x, y int, text string) {
ctx.ft.draw_text_def(x, y, text)
}
fn C.WaitMessage() fn C.WaitMessage()
pub fn wait_events() { pub fn wait_events() {

View File

@ -1,4 +1,6 @@
module ft // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module gg
import sokol.sfons import sokol.sfons
import gx import gx
@ -7,13 +9,6 @@ import os
const ( const (
default_font_size = 20 default_font_size = 20
) )
// TODO remove globals
/*
__global g_fons &C.FONScontext
__global g_font_normal int
__global g_font_path string
*/
pub struct FT { pub struct FT {
pub: pub:
@ -23,13 +18,13 @@ pub struct FT {
scale f32 = 1.0 scale f32 = 1.0
} }
pub struct Config { pub struct FTConfig {
font_path string font_path string
scale f32 = 1.0 scale f32 = 1.0
font_size int font_size int
} }
pub fn new(c Config) ?&FT{ fn new_ft(c Config) ?&FT{
if c.font_path == '' { if c.font_path == '' {
// Load default font // Load default font
} }
@ -50,38 +45,40 @@ pub fn new(c Config) ?&FT{
} }
pub fn (ft &FT) draw_text(x, y int, text string, cfg gx.TextCfg) { pub fn (ctx &Context) draw_text(x, y int, text string, cfg gx.TextCfg) {
ft.fons.set_font(ft.font_normal) if !ctx.font_inited {
ft.fons.set_size(2.0 * ft.scale * f32(cfg.size)) return
}
ctx.ft.fons.set_font(ctx.ft.font_normal)
ctx.ft.fons.set_size(2.0 * ctx.ft.scale * f32(cfg.size))
if cfg.align == gx.align_right { if cfg.align == gx.align_right {
C.fonsSetAlign(ft.fons, C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_TOP) C.fonsSetAlign(ctx.ft.fons, C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_TOP)
} }
else { else {
C.fonsSetAlign(ft.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP) C.fonsSetAlign(ctx.ft.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP)
} }
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, 255)
C.fonsSetColor(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)
lh := f32(0.0) lh := f32(0.0)
ft.fons.vert_metrics(&ascender, &descender, &lh) ctx.ft.fons.vert_metrics(&ascender, &descender, &lh)
C.fonsDrawText(ft.fons, x*ft.scale, y*ft.scale, text.str, 0) // TODO: check offsets/alignment C.fonsDrawText(ctx.ft.fons, x*ctx.ft.scale, y*ctx.ft.scale, text.str, 0) // TODO: check offsets/alignment
} }
pub fn (ft &FT) draw_text_def(x, y int, text string) { pub fn (ctx &Context) draw_text_def(x, y int, text string) {
cfg := gx.TextCfg { cfg := gx.TextCfg {
color: gx.black color: gx.black
size: default_font_size size: default_font_size
align: gx.align_left align: gx.align_left
} }
ft.draw_text(x, y, text, cfg) ctx.draw_text(x, y, text, cfg)
} }
/*
pub fn (mut gg FT) init_font() { pub fn (mut gg FT) init_font() {
// TODO
////gg.fons =g_fons
//gg.font_normal=g_font_normal
} }
*/
pub fn (ft &FT) flush(){ pub fn (ft &FT) flush(){
sfons.flush(ft.fons) sfons.flush(ft.fons)
@ -93,7 +90,6 @@ pub fn (ft &FT) text_width(s string) int {
pub fn (ft &FT) text_height(s string) int { pub fn (ft &FT) text_height(s string) int {
return 0 return 0
} }
pub fn (ft &FT) text_size(s string) (int, int) { pub fn (ft &FT) text_size(s string) (int, int) {