gg: rename some variables (for consistency) (#13280)

pull/13285/head
kahsa 2022-01-26 19:26:12 +09:00 committed by GitHub
parent 0979723636
commit 867056dafb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 95 additions and 95 deletions

View File

@ -145,7 +145,7 @@ pub mut:
}
fn gg_init_sokol_window(user_data voidptr) {
mut g := unsafe { &Context(user_data) }
mut ctx := unsafe { &Context(user_data) }
desc := sapp.create_desc()
/*
desc := gfx.Desc{
@ -161,47 +161,47 @@ fn gg_init_sokol_window(user_data voidptr) {
gfx.setup(&desc)
sgl_desc := sgl.Desc{}
sgl.setup(&sgl_desc)
g.scale = dpi_scale()
ctx.scale = dpi_scale()
// is_high_dpi := sapp.high_dpi()
// fb_w := sapp.width()
// fb_h := sapp.height()
// println('g.scale=$g.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h')
// if g.config.init_text {
// println('ctx.scale=$ctx.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h')
// if ctx.config.init_text {
// `os.is_file()` won't work on Android if the font file is embedded into the APK
exists := $if !android { os.is_file(g.config.font_path) } $else { true }
if g.config.font_path != '' && !exists {
g.render_text = false
} else if g.config.font_path != '' && exists {
exists := $if !android { os.is_file(ctx.config.font_path) } $else { true }
if ctx.config.font_path != '' && !exists {
ctx.render_text = false
} else if ctx.config.font_path != '' && exists {
// t := time.ticks()
g.ft = new_ft(
font_path: g.config.font_path
custom_bold_font_path: g.config.custom_bold_font_path
ctx.ft = new_ft(
font_path: ctx.config.font_path
custom_bold_font_path: ctx.config.custom_bold_font_path
scale: dpi_scale()
) or { panic(err) }
// println('FT took ${time.ticks()-t} ms')
g.font_inited = true
ctx.font_inited = true
} else {
if g.config.font_bytes_normal.len > 0 {
g.ft = new_ft(
bytes_normal: g.config.font_bytes_normal
bytes_bold: g.config.font_bytes_bold
bytes_mono: g.config.font_bytes_mono
bytes_italic: g.config.font_bytes_italic
if ctx.config.font_bytes_normal.len > 0 {
ctx.ft = new_ft(
bytes_normal: ctx.config.font_bytes_normal
bytes_bold: ctx.config.font_bytes_bold
bytes_mono: ctx.config.font_bytes_mono
bytes_italic: ctx.config.font_bytes_italic
scale: sapp.dpi_scale()
) or { panic(err) }
g.font_inited = true
ctx.font_inited = true
} else {
sfont := font.default()
if g.config.font_path != '' {
eprintln('font file "$g.config.font_path" does not exist, the system font ($sfont) was used instead.')
if ctx.config.font_path != '' {
eprintln('font file "$ctx.config.font_path" does not exist, the system font ($sfont) was used instead.')
}
g.ft = new_ft(
ctx.ft = new_ft(
font_path: sfont
custom_bold_font_path: g.config.custom_bold_font_path
custom_bold_font_path: ctx.config.custom_bold_font_path
scale: sapp.dpi_scale()
) or { panic(err) }
g.font_inited = true
ctx.font_inited = true
}
}
//
@ -219,19 +219,19 @@ fn gg_init_sokol_window(user_data voidptr) {
}
pipdesc.colors[0] = color_state
g.timage_pip = sgl.make_pipeline(&pipdesc)
ctx.timage_pip = sgl.make_pipeline(&pipdesc)
//
if g.config.init_fn != voidptr(0) {
g.config.init_fn(g.user_data)
if ctx.config.init_fn != voidptr(0) {
ctx.config.init_fn(ctx.user_data)
}
// Create images now that we can do that after sg is inited
if g.native_rendering {
if ctx.native_rendering {
return
}
for i in 0 .. g.image_cache.len {
if g.image_cache[i].simg.id == 0 {
g.image_cache[i].init_sokol_image()
for i in 0 .. ctx.image_cache.len {
if ctx.image_cache[i].simg.id == 0 {
ctx.image_cache[i].init_sokol_image()
}
}
}
@ -262,105 +262,105 @@ fn gg_frame_fn(user_data voidptr) {
fn gg_event_fn(ce voidptr, user_data voidptr) {
// e := unsafe { &sapp.Event(ce) }
mut e := unsafe { &Event(ce) }
mut g := unsafe { &Context(user_data) }
if g.ui_mode {
g.refresh_ui()
mut ctx := unsafe { &Context(user_data) }
if ctx.ui_mode {
ctx.refresh_ui()
}
if e.typ == .mouse_down {
bitplace := int(e.mouse_button)
g.mbtn_mask |= byte(1 << bitplace)
g.mouse_buttons = MouseButtons(g.mbtn_mask)
ctx.mbtn_mask |= byte(1 << bitplace)
ctx.mouse_buttons = MouseButtons(ctx.mbtn_mask)
}
if e.typ == .mouse_up {
bitplace := int(e.mouse_button)
g.mbtn_mask &= ~(byte(1 << bitplace))
g.mouse_buttons = MouseButtons(g.mbtn_mask)
ctx.mbtn_mask &= ~(byte(1 << bitplace))
ctx.mouse_buttons = MouseButtons(ctx.mbtn_mask)
}
if e.typ == .mouse_move && e.mouse_button == .invalid {
if g.mbtn_mask & 0x01 > 0 {
if ctx.mbtn_mask & 0x01 > 0 {
e.mouse_button = .left
}
if g.mbtn_mask & 0x02 > 0 {
if ctx.mbtn_mask & 0x02 > 0 {
e.mouse_button = .right
}
if g.mbtn_mask & 0x04 > 0 {
if ctx.mbtn_mask & 0x04 > 0 {
e.mouse_button = .middle
}
}
g.mouse_pos_x = int(e.mouse_x / g.scale)
g.mouse_pos_y = int(e.mouse_y / g.scale)
g.mouse_dx = int(e.mouse_dx / g.scale)
g.mouse_dy = int(e.mouse_dy / g.scale)
g.scroll_x = int(e.scroll_x / g.scale)
g.scroll_y = int(e.scroll_y / g.scale)
g.key_modifiers = Modifier(e.modifiers)
g.key_repeat = e.key_repeat
ctx.mouse_pos_x = int(e.mouse_x / ctx.scale)
ctx.mouse_pos_y = int(e.mouse_y / ctx.scale)
ctx.mouse_dx = int(e.mouse_dx / ctx.scale)
ctx.mouse_dy = int(e.mouse_dy / ctx.scale)
ctx.scroll_x = int(e.scroll_x / ctx.scale)
ctx.scroll_y = int(e.scroll_y / ctx.scale)
ctx.key_modifiers = Modifier(e.modifiers)
ctx.key_repeat = e.key_repeat
if e.typ in [.key_down, .key_up] {
key_idx := int(e.key_code) % key_code_max
prev := g.pressed_keys[key_idx]
prev := ctx.pressed_keys[key_idx]
next := e.typ == .key_down
g.pressed_keys[key_idx] = next
g.pressed_keys_edge[key_idx] = prev != next
ctx.pressed_keys[key_idx] = next
ctx.pressed_keys_edge[key_idx] = prev != next
}
if g.config.event_fn != voidptr(0) {
g.config.event_fn(e, g.config.user_data)
if ctx.config.event_fn != voidptr(0) {
ctx.config.event_fn(e, ctx.config.user_data)
}
match e.typ {
.mouse_move {
if g.config.move_fn != voidptr(0) {
g.config.move_fn(e.mouse_x / g.scale, e.mouse_y / g.scale, g.config.user_data)
if ctx.config.move_fn != voidptr(0) {
ctx.config.move_fn(e.mouse_x / ctx.scale, e.mouse_y / ctx.scale, ctx.config.user_data)
}
}
.mouse_down {
if g.config.click_fn != voidptr(0) {
g.config.click_fn(e.mouse_x / g.scale, e.mouse_y / g.scale, e.mouse_button,
g.config.user_data)
if ctx.config.click_fn != voidptr(0) {
ctx.config.click_fn(e.mouse_x / ctx.scale, e.mouse_y / ctx.scale, e.mouse_button,
ctx.config.user_data)
}
}
.mouse_up {
if g.config.unclick_fn != voidptr(0) {
g.config.unclick_fn(e.mouse_x / g.scale, e.mouse_y / g.scale, e.mouse_button,
g.config.user_data)
if ctx.config.unclick_fn != voidptr(0) {
ctx.config.unclick_fn(e.mouse_x / ctx.scale, e.mouse_y / ctx.scale, e.mouse_button,
ctx.config.user_data)
}
}
.mouse_leave {
if g.config.leave_fn != voidptr(0) {
g.config.leave_fn(e, g.config.user_data)
if ctx.config.leave_fn != voidptr(0) {
ctx.config.leave_fn(e, ctx.config.user_data)
}
}
.mouse_enter {
if g.config.enter_fn != voidptr(0) {
g.config.enter_fn(e, g.config.user_data)
if ctx.config.enter_fn != voidptr(0) {
ctx.config.enter_fn(e, ctx.config.user_data)
}
}
.mouse_scroll {
if g.config.scroll_fn != voidptr(0) {
g.config.scroll_fn(e, g.config.user_data)
if ctx.config.scroll_fn != voidptr(0) {
ctx.config.scroll_fn(e, ctx.config.user_data)
}
}
.key_down {
if g.config.keydown_fn != voidptr(0) {
g.config.keydown_fn(e.key_code, Modifier(e.modifiers), g.config.user_data)
if ctx.config.keydown_fn != voidptr(0) {
ctx.config.keydown_fn(e.key_code, Modifier(e.modifiers), ctx.config.user_data)
}
}
.key_up {
if g.config.keyup_fn != voidptr(0) {
g.config.keyup_fn(e.key_code, Modifier(e.modifiers), g.config.user_data)
if ctx.config.keyup_fn != voidptr(0) {
ctx.config.keyup_fn(e.key_code, Modifier(e.modifiers), ctx.config.user_data)
}
}
.char {
if g.config.char_fn != voidptr(0) {
g.config.char_fn(e.char_code, g.config.user_data)
if ctx.config.char_fn != voidptr(0) {
ctx.config.char_fn(e.char_code, ctx.config.user_data)
}
}
.resized {
if g.config.resized_fn != voidptr(0) {
g.config.resized_fn(e, g.config.user_data)
if ctx.config.resized_fn != voidptr(0) {
ctx.config.resized_fn(e, ctx.config.user_data)
}
}
.quit_requested {
if g.config.quit_fn != voidptr(0) {
g.config.quit_fn(e, g.config.user_data)
if ctx.config.quit_fn != voidptr(0) {
ctx.config.quit_fn(e, ctx.config.user_data)
}
}
else {
@ -370,18 +370,18 @@ fn gg_event_fn(ce voidptr, user_data voidptr) {
}
fn gg_cleanup_fn(user_data voidptr) {
mut g := unsafe { &Context(user_data) }
if g.config.cleanup_fn != voidptr(0) {
g.config.cleanup_fn(g.config.user_data)
mut ctx := unsafe { &Context(user_data) }
if ctx.config.cleanup_fn != voidptr(0) {
ctx.config.cleanup_fn(ctx.config.user_data)
}
gfx.shutdown()
}
fn gg_fail_fn(msg &char, user_data voidptr) {
mut g := unsafe { &Context(user_data) }
mut ctx := unsafe { &Context(user_data) }
vmsg := unsafe { tos3(msg) }
if g.config.fail_fn != voidptr(0) {
g.config.fail_fn(vmsg, g.config.user_data)
if ctx.config.fail_fn != voidptr(0) {
ctx.config.fail_fn(vmsg, ctx.config.user_data)
} else {
eprintln('gg error: $vmsg')
}
@ -391,7 +391,7 @@ fn gg_fail_fn(msg &char, user_data voidptr) {
//
pub fn new_context(cfg Config) &Context {
mut g := &Context{
mut ctx := &Context{
user_data: cfg.user_data
width: cfg.width
height: cfg.height
@ -401,12 +401,12 @@ pub fn new_context(cfg Config) &Context {
native_rendering: cfg.native_rendering
}
if isnil(cfg.user_data) {
g.user_data = g
ctx.user_data = ctx
}
g.set_bg_color(cfg.bg_color)
ctx.set_bg_color(cfg.bg_color)
// C.printf('new_context() %p\n', cfg.user_data)
window := sapp.Desc{
user_data: g
user_data: ctx
init_userdata_cb: gg_init_sokol_window
frame_userdata_cb: gg_frame_fn
event_userdata_cb: gg_event_fn
@ -426,8 +426,8 @@ pub fn new_context(cfg Config) &Context {
max_dropped_file_path_length: cfg.max_dropped_file_path_length
swap_interval: cfg.swap_interval
}
g.window = window
return g
ctx.window = window
return ctx
}
pub fn (ctx &Context) run() {
@ -456,9 +456,9 @@ pub fn (mut ctx Context) refresh_ui() {
}
// Prepares the context for drawing
pub fn (gg &Context) begin() {
if gg.render_text && gg.font_inited {
gg.ft.flush()
pub fn (ctx &Context) begin() {
if ctx.render_text && ctx.font_inited {
ctx.ft.flush()
}
sgl.defaults()
sgl.matrix_mode_projection()
@ -466,8 +466,8 @@ pub fn (gg &Context) begin() {
}
// Finishes drawing for the context
pub fn (gg &Context) end() {
gfx.begin_default_pass(gg.clear_pass, sapp.width(), sapp.height())
pub fn (ctx &Context) end() {
gfx.begin_default_pass(ctx.clear_pass, sapp.width(), sapp.height())
sgl.draw()
gfx.end_pass()
gfx.commit()