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

gg: handle Android's own native dpi scaling factor (#8908)

This commit is contained in:
spaceface 2021-02-22 20:24:18 +01:00 committed by GitHub
parent 79edff5c6c
commit b4dc6c83cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 16 deletions

View File

@ -255,7 +255,7 @@ fn (b Board) hmirror() Board {
// GCC optimization bug; inlining fails when compiled with -prod // GCC optimization bug; inlining fails when compiled with -prod
[no_inline] [no_inline]
fn (t TileLine) to_left() TileLine { fn (t TileLine) to_left() TileLine {
right_border_idx := 5 right_border_idx := 4
mut res := t mut res := t
mut zeros := 0 mut zeros := 0
mut nonzeros := 0 mut nonzeros := 0
@ -729,11 +729,11 @@ fn (mut app App) handle_tap() {
avgx, avgy := avg(s.pos.x, e.pos.x), avg(s.pos.y, e.pos.y) avgx, avgy := avg(s.pos.x, e.pos.x), avg(s.pos.y, e.pos.y)
// TODO: Replace "touch spots" with actual buttons // TODO: Replace "touch spots" with actual buttons
// bottom left -> change theme // bottom left -> change theme
if avgx < 200 && h - avgy < 200 { if avgx < 50 && h - avgy < 50 {
app.next_theme() app.next_theme()
} }
// bottom right -> change tile format // bottom right -> change tile format
if w - avgx < 200 && h - avgy < 200 { if w - avgx < 50 && h - avgy < 50 {
app.next_tile_format() app.next_tile_format()
} }
if app.state == .victory { if app.state == .victory {
@ -767,7 +767,7 @@ fn (mut app App) handle_swipe() {
dmax := if max(adx, ady) > 0 { max(adx, ady) } else { 1 } dmax := if max(adx, ady) > 0 { max(adx, ady) } else { 1 }
tdiff := int(e.time.unix_time_milli() - s.time.unix_time_milli()) tdiff := int(e.time.unix_time_milli() - s.time.unix_time_milli())
// TODO: make this calculation more accurate (don't use arbitrary numbers) // TODO: make this calculation more accurate (don't use arbitrary numbers)
min_swipe_distance := int(math.sqrt(min(w, h) * tdiff / 60)) + 20 min_swipe_distance := int(math.sqrt(min(w, h) * tdiff / 100)) + 20
if dmax < min_swipe_distance { if dmax < min_swipe_distance {
return return
} }

View File

@ -148,12 +148,7 @@ fn gg_init_sokol_window(user_data voidptr) {
gfx.setup(&desc) gfx.setup(&desc)
sgl_desc := C.sgl_desc_t{} sgl_desc := C.sgl_desc_t{}
sgl.setup(&sgl_desc) sgl.setup(&sgl_desc)
g.scale = sapp.dpi_scale() g.scale = dpi_scale()
// NB: on older X11, `Xft.dpi` from ~/.Xresources, that sokol uses,
// may not be set which leads to sapp.dpi_scale reporting incorrectly 0.0
if g.scale < 0.1 {
g.scale = 1.0
}
// is_high_dpi := sapp.high_dpi() // is_high_dpi := sapp.high_dpi()
// fb_w := sapp.width() // fb_w := sapp.width()
// fb_h := sapp.height() // fb_h := sapp.height()
@ -166,7 +161,7 @@ fn gg_init_sokol_window(user_data voidptr) {
g.ft = new_ft( g.ft = new_ft(
font_path: g.config.font_path font_path: g.config.font_path
custom_bold_font_path: g.config.custom_bold_font_path custom_bold_font_path: g.config.custom_bold_font_path
scale: sapp.dpi_scale() scale: dpi_scale()
) or { panic(err) } ) or { panic(err) }
// println('FT took ${time.ticks()-t} ms') // println('FT took ${time.ticks()-t} ms')
g.font_inited = true g.font_inited = true
@ -761,15 +756,21 @@ pub fn screen_size() Size {
// window_size returns the `Size` of the active window // window_size returns the `Size` of the active window
pub fn window_size() Size { pub fn window_size() Size {
mut s := sapp.dpi_scale() s := dpi_scale()
if s == 0 {
s = 1.
}
return Size{int(sapp.width() / s), int(sapp.height() / s)} return Size{int(sapp.width() / s), int(sapp.height() / s)}
} }
pub fn dpi_scale() f32 { pub fn dpi_scale() f32 {
return sapp.dpi_scale() mut s := sapp.dpi_scale()
$if android {
s *= android_dpi_scale()
}
// NB: on older X11, `Xft.dpi` from ~/.Xresources, that sokol uses,
// may not be set which leads to sapp.dpi_scale reporting incorrectly 0.0
if s < 0.1 {
s = 1.
}
return s
} }
pub fn high_dpi() bool { pub fn high_dpi() bool {

25
vlib/gg/gg_android.c.v Normal file
View File

@ -0,0 +1,25 @@
module gg
import sokol.sapp
#include <android/configuration.h>
#include <android/native_activity.h>
fn C.AConfiguration_new() voidptr
fn C.AConfiguration_fromAssetManager(voidptr, voidptr)
fn C.AConfiguration_getDensity(voidptr) u32
fn C.AConfiguration_delete(voidptr)
struct C.AAssetManager {}
struct C.ANativeActivity {
assetManager voidptr
}
pub fn android_dpi_scale() f32 {
config := C.AConfiguration_new()
activity := &C.ANativeActivity(sapp.android_get_native_activity())
C.AConfiguration_fromAssetManager(config, activity.assetManager)
density := C.AConfiguration_getDensity(config)
C.AConfiguration_delete(config)
return f32(density) / 160
}