2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-07-06 21:29:05 +03:00
|
|
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
2021-06-24 11:02:04 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
module gg
|
|
|
|
|
2020-09-24 16:53:06 +03:00
|
|
|
import os
|
2020-04-26 12:42:44 +03:00
|
|
|
import gx
|
2020-06-04 17:05:12 +03:00
|
|
|
import sokol
|
|
|
|
import sokol.sapp
|
|
|
|
import sokol.sgl
|
|
|
|
import sokol.gfx
|
2020-10-10 11:37:17 +03:00
|
|
|
import math
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2021-06-24 11:02:04 +03:00
|
|
|
pub type FNCb = fn (data voidptr)
|
|
|
|
|
|
|
|
pub type FNEvent = fn (e &Event, data voidptr)
|
|
|
|
|
|
|
|
pub type FNFail = fn (msg string, data voidptr)
|
2020-08-02 00:40:25 +03:00
|
|
|
|
2021-06-24 11:02:04 +03:00
|
|
|
pub type FNKeyDown = fn (c KeyCode, m Modifier, data voidptr)
|
2020-08-02 00:40:25 +03:00
|
|
|
|
2021-06-24 11:02:04 +03:00
|
|
|
pub type FNKeyUp = fn (c KeyCode, m Modifier, data voidptr)
|
2020-08-02 00:40:25 +03:00
|
|
|
|
2021-06-24 11:02:04 +03:00
|
|
|
pub type FNMove = fn (x f32, y f32, data voidptr)
|
2020-08-02 00:40:25 +03:00
|
|
|
|
2021-06-24 11:02:04 +03:00
|
|
|
pub type FNClick = fn (x f32, y f32, button MouseButton, data voidptr)
|
2020-09-21 03:42:28 +03:00
|
|
|
|
2021-06-24 11:02:04 +03:00
|
|
|
pub type FNUnClick = fn (x f32, y f32, button MouseButton, data voidptr)
|
2021-05-28 12:46:23 +03:00
|
|
|
|
2021-06-24 11:02:04 +03:00
|
|
|
pub type FNChar = fn (c u32, data voidptr)
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2021-02-17 08:44:01 +03:00
|
|
|
pub struct Event {
|
2021-02-17 21:14:37 +03:00
|
|
|
pub mut:
|
2021-03-05 17:52:34 +03:00
|
|
|
frame_count u64
|
|
|
|
typ sapp.EventType
|
|
|
|
key_code KeyCode
|
|
|
|
char_code u32
|
|
|
|
key_repeat bool
|
|
|
|
modifiers u32
|
2021-05-28 12:46:23 +03:00
|
|
|
mouse_button MouseButton
|
2021-02-17 08:44:01 +03:00
|
|
|
mouse_x f32
|
|
|
|
mouse_y f32
|
|
|
|
mouse_dx f32
|
|
|
|
mouse_dy f32
|
|
|
|
scroll_x f32
|
|
|
|
scroll_y f32
|
|
|
|
num_touches int
|
|
|
|
touches [8]C.sapp_touchpoint
|
|
|
|
window_width int
|
|
|
|
window_height int
|
|
|
|
framebuffer_width int
|
|
|
|
framebuffer_height int
|
|
|
|
}
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
pub struct Config {
|
2019-08-23 00:00:31 +03:00
|
|
|
pub:
|
2021-01-12 06:38:43 +03:00
|
|
|
width int
|
|
|
|
height int
|
2021-06-24 11:02:04 +03:00
|
|
|
use_ortho bool // unused, still here just for backwards compatibility
|
2021-01-12 06:38:43 +03:00
|
|
|
retina bool
|
|
|
|
resizable bool
|
|
|
|
user_data voidptr
|
|
|
|
font_size int
|
|
|
|
create_window bool
|
2020-06-04 17:05:12 +03:00
|
|
|
// window_user_ptr voidptr
|
2021-01-12 06:38:43 +03:00
|
|
|
window_title string
|
|
|
|
borderless_window bool
|
|
|
|
always_on_top bool
|
|
|
|
bg_color gx.Color
|
2021-06-24 11:02:04 +03:00
|
|
|
init_fn FNCb = voidptr(0)
|
|
|
|
frame_fn FNCb = voidptr(0)
|
|
|
|
native_frame_fn FNCb = voidptr(0)
|
|
|
|
cleanup_fn FNCb = voidptr(0)
|
|
|
|
fail_fn FNFail = voidptr(0)
|
|
|
|
//
|
|
|
|
event_fn FNEvent = voidptr(0)
|
|
|
|
quit_fn FNEvent = voidptr(0)
|
|
|
|
//
|
|
|
|
keydown_fn FNKeyDown = voidptr(0)
|
|
|
|
keyup_fn FNKeyUp = voidptr(0)
|
|
|
|
char_fn FNChar = voidptr(0)
|
|
|
|
//
|
|
|
|
move_fn FNMove = voidptr(0)
|
|
|
|
click_fn FNClick = voidptr(0)
|
|
|
|
unclick_fn FNUnClick = voidptr(0)
|
|
|
|
leave_fn FNEvent = voidptr(0)
|
|
|
|
enter_fn FNEvent = voidptr(0)
|
|
|
|
resized_fn FNEvent = voidptr(0)
|
|
|
|
scroll_fn FNEvent = voidptr(0)
|
2020-11-21 02:04:29 +03:00
|
|
|
// wait_events bool // set this to true for UIs, to save power
|
2021-01-12 06:38:43 +03:00
|
|
|
fullscreen bool
|
|
|
|
scale f32 = 1.0
|
|
|
|
sample_count int
|
2021-05-27 20:14:36 +03:00
|
|
|
// ved needs this
|
2020-08-02 00:40:25 +03:00
|
|
|
// init_text bool
|
2020-12-01 18:30:22 +03:00
|
|
|
font_path string
|
|
|
|
custom_bold_font_path string
|
|
|
|
ui_mode bool // refreshes only on events to save CPU usage
|
2021-01-22 00:07:47 +03:00
|
|
|
// font bytes for embedding
|
|
|
|
font_bytes_normal []byte
|
|
|
|
font_bytes_bold []byte
|
|
|
|
font_bytes_mono []byte
|
|
|
|
font_bytes_italic []byte
|
2021-01-23 12:25:40 +03:00
|
|
|
native_rendering bool // Cocoa on macOS/iOS, GDI+ on Windows
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:32:08 +03:00
|
|
|
pub struct PenConfig {
|
|
|
|
color gx.Color
|
|
|
|
line_type PenLineType = .solid
|
|
|
|
thickness int = 1
|
|
|
|
}
|
|
|
|
|
2021-05-07 15:58:48 +03:00
|
|
|
[heap]
|
2020-06-04 21:26:18 +03:00
|
|
|
pub struct Context {
|
2020-08-05 17:00:24 +03:00
|
|
|
mut:
|
2021-06-02 16:00:14 +03:00
|
|
|
render_text bool = true
|
2020-08-05 17:00:24 +03:00
|
|
|
// a cache with all images created by the user. used for sokol image init and to save space
|
|
|
|
// (so that the user can store image ids, not entire Image objects)
|
2020-11-20 16:50:06 +03:00
|
|
|
image_cache []Image
|
|
|
|
needs_refresh bool = true
|
2021-03-27 13:03:15 +03:00
|
|
|
ticks int // for ui mode only
|
2021-01-23 12:25:40 +03:00
|
|
|
pub:
|
|
|
|
native_rendering bool
|
2019-08-23 00:00:31 +03:00
|
|
|
pub mut:
|
2021-01-12 06:38:43 +03:00
|
|
|
scale f32 = 1.0
|
2020-10-18 09:48:13 +03:00
|
|
|
// will get set to 2.0 for retina, will remain 1.0 for normal
|
2021-01-12 06:38:43 +03:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
clear_pass C.sg_pass_action
|
|
|
|
window C.sapp_desc
|
|
|
|
timage_pip C.sgl_pipeline
|
|
|
|
config Config
|
|
|
|
ft &FT
|
|
|
|
font_inited bool
|
|
|
|
ui_mode bool // do not redraw everything 60 times/second, but only when the user requests
|
2021-06-24 11:02:04 +03:00
|
|
|
frame u64 // the current frame counted from the start of the application; always increasing
|
|
|
|
//
|
|
|
|
mbtn_mask byte
|
|
|
|
mouse_buttons MouseButtons // typed version of mbtn_mask; easier to use for user programs
|
|
|
|
mouse_pos_x int
|
|
|
|
mouse_pos_y int
|
|
|
|
mouse_dx int
|
|
|
|
mouse_dy int
|
|
|
|
scroll_x int
|
|
|
|
scroll_y int
|
|
|
|
//
|
|
|
|
key_modifiers Modifier
|
|
|
|
key_repeat bool
|
|
|
|
pressed_keys [key_code_max]bool
|
2020-06-04 17:05:12 +03:00
|
|
|
}
|
|
|
|
|
2020-08-02 00:40:25 +03:00
|
|
|
pub struct Size {
|
|
|
|
pub:
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
}
|
2020-06-04 20:57:13 +03:00
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
fn gg_init_sokol_window(user_data voidptr) {
|
2021-01-05 21:07:45 +03:00
|
|
|
mut g := unsafe { &Context(user_data) }
|
2020-08-23 08:47:50 +03:00
|
|
|
desc := sapp.create_desc()
|
2020-08-23 08:25:30 +03:00
|
|
|
/*
|
2020-06-04 17:05:12 +03:00
|
|
|
desc := C.sg_desc{
|
|
|
|
mtl_device: sapp.metal_get_device()
|
|
|
|
mtl_renderpass_descriptor_cb: sapp.metal_get_renderpass_descriptor
|
|
|
|
mtl_drawable_cb: sapp.metal_get_drawable
|
|
|
|
d3d11_device: sapp.d3d11_get_device()
|
|
|
|
d3d11_device_context: sapp.d3d11_get_device_context()
|
|
|
|
d3d11_render_target_view_cb: sapp.d3d11_get_render_target_view
|
|
|
|
d3d11_depth_stencil_view_cb: sapp.d3d11_get_depth_stencil_view
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2020-08-23 08:25:30 +03:00
|
|
|
*/
|
2020-06-04 17:05:12 +03:00
|
|
|
gfx.setup(&desc)
|
|
|
|
sgl_desc := C.sgl_desc_t{}
|
|
|
|
sgl.setup(&sgl_desc)
|
2021-02-22 22:24:18 +03:00
|
|
|
g.scale = dpi_scale()
|
2020-08-02 00:40:25 +03:00
|
|
|
// 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 {
|
2020-09-25 13:06:20 +03:00
|
|
|
// `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 }
|
2021-06-02 16:00:14 +03:00
|
|
|
if g.config.font_path != '' && !exists {
|
|
|
|
g.render_text = false
|
|
|
|
} else if g.config.font_path != '' && exists {
|
2020-08-02 00:40:25 +03:00
|
|
|
// t := time.ticks()
|
2020-12-14 07:03:17 +03:00
|
|
|
g.ft = new_ft(
|
|
|
|
font_path: g.config.font_path
|
|
|
|
custom_bold_font_path: g.config.custom_bold_font_path
|
2021-02-22 22:24:18 +03:00
|
|
|
scale: dpi_scale()
|
2021-03-01 02:18:14 +03:00
|
|
|
) or { panic(err) }
|
2020-08-02 00:40:25 +03:00
|
|
|
// println('FT took ${time.ticks()-t} ms')
|
2020-07-06 20:45:00 +03:00
|
|
|
g.font_inited = true
|
2020-12-07 22:28:41 +03:00
|
|
|
} else {
|
2021-06-02 16:00:14 +03:00
|
|
|
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
|
|
|
|
scale: sapp.dpi_scale()
|
|
|
|
) or { panic(err) }
|
|
|
|
g.font_inited = true
|
|
|
|
} else {
|
|
|
|
sfont := system_font_path()
|
|
|
|
if g.config.font_path != '' {
|
|
|
|
eprintln('font file "$g.config.font_path" does not exist, the system font ($sfont) was used instead.')
|
2021-01-22 00:07:47 +03:00
|
|
|
}
|
2021-06-02 16:00:14 +03:00
|
|
|
|
|
|
|
g.ft = new_ft(
|
|
|
|
font_path: sfont
|
|
|
|
custom_bold_font_path: g.config.custom_bold_font_path
|
|
|
|
scale: sapp.dpi_scale()
|
|
|
|
) or { panic(err) }
|
|
|
|
g.font_inited = true
|
2020-12-07 22:28:41 +03:00
|
|
|
}
|
2020-07-06 20:45:00 +03:00
|
|
|
}
|
2020-08-03 00:30:08 +03:00
|
|
|
//
|
2021-04-27 08:18:48 +03:00
|
|
|
mut pipdesc := C.sg_pipeline_desc{
|
|
|
|
label: c'alpha_image'
|
|
|
|
}
|
2020-12-23 21:13:42 +03:00
|
|
|
unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) }
|
2021-04-07 21:39:23 +03:00
|
|
|
|
|
|
|
color_state := C.sg_color_state{
|
|
|
|
blend: C.sg_blend_state{
|
|
|
|
enabled: true
|
|
|
|
src_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA)
|
|
|
|
dst_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pipdesc.colors[0] = color_state
|
|
|
|
|
2020-08-03 00:30:08 +03:00
|
|
|
g.timage_pip = sgl.make_pipeline(&pipdesc)
|
|
|
|
//
|
2020-06-04 17:05:12 +03:00
|
|
|
if g.config.init_fn != voidptr(0) {
|
2020-07-06 20:45:00 +03:00
|
|
|
g.config.init_fn(g.config.user_data)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2020-08-05 17:00:24 +03:00
|
|
|
// Create images now that we can do that after sg is inited
|
2021-01-23 12:25:40 +03:00
|
|
|
if g.native_rendering {
|
|
|
|
return
|
|
|
|
}
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2020-10-10 11:37:17 +03:00
|
|
|
for i in 0 .. g.image_cache.len {
|
2021-06-24 17:45:14 +03:00
|
|
|
if g.image_cache[i].simg.id == 0 {
|
|
|
|
g.image_cache[i].init_sokol_image()
|
|
|
|
}
|
2020-08-05 17:00:24 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
fn gg_frame_fn(user_data voidptr) {
|
2021-01-05 21:07:45 +03:00
|
|
|
mut ctx := unsafe { &Context(user_data) }
|
2021-06-24 11:02:04 +03:00
|
|
|
ctx.frame++
|
2020-11-20 16:50:06 +03:00
|
|
|
if ctx.config.frame_fn == voidptr(0) {
|
|
|
|
return
|
|
|
|
}
|
2021-01-23 12:25:40 +03:00
|
|
|
if ctx.native_rendering {
|
|
|
|
// return
|
|
|
|
}
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2020-11-20 16:50:06 +03:00
|
|
|
if ctx.ui_mode && !ctx.needs_refresh {
|
|
|
|
// Draw 3 more frames after the "stop refresh" command
|
|
|
|
ctx.ticks++
|
|
|
|
if ctx.ticks > 3 {
|
|
|
|
return
|
|
|
|
}
|
2019-08-23 00:00:31 +03:00
|
|
|
}
|
2020-11-20 16:50:06 +03:00
|
|
|
ctx.config.frame_fn(ctx.config.user_data)
|
|
|
|
ctx.needs_refresh = false
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut ctx Context) refresh_ui() {
|
|
|
|
ctx.needs_refresh = true
|
|
|
|
ctx.ticks = 0
|
2019-08-23 00:00:31 +03:00
|
|
|
}
|
2019-07-19 14:27:44 +03:00
|
|
|
|
2020-08-02 00:40:25 +03:00
|
|
|
fn gg_event_fn(ce &C.sapp_event, user_data voidptr) {
|
2021-02-17 08:44:01 +03:00
|
|
|
// e := unsafe { &sapp.Event(ce) }
|
2021-02-17 21:14:37 +03:00
|
|
|
mut e := unsafe { &Event(ce) }
|
2021-01-05 21:07:45 +03:00
|
|
|
mut g := unsafe { &Context(user_data) }
|
2021-03-27 13:03:15 +03:00
|
|
|
if g.ui_mode {
|
|
|
|
g.refresh_ui()
|
|
|
|
}
|
2021-02-17 21:14:37 +03:00
|
|
|
if e.typ == .mouse_down {
|
|
|
|
bitplace := int(e.mouse_button)
|
|
|
|
g.mbtn_mask |= byte(1 << bitplace)
|
2021-06-24 11:02:04 +03:00
|
|
|
g.mouse_buttons = MouseButtons(g.mbtn_mask)
|
2021-02-17 21:14:37 +03:00
|
|
|
}
|
|
|
|
if e.typ == .mouse_up {
|
|
|
|
bitplace := int(e.mouse_button)
|
|
|
|
g.mbtn_mask &= ~(byte(1 << bitplace))
|
2021-06-24 11:02:04 +03:00
|
|
|
g.mouse_buttons = MouseButtons(g.mbtn_mask)
|
2021-02-17 21:14:37 +03:00
|
|
|
}
|
|
|
|
if e.typ == .mouse_move && e.mouse_button == .invalid {
|
|
|
|
if g.mbtn_mask & 0x01 > 0 {
|
|
|
|
e.mouse_button = .left
|
|
|
|
}
|
|
|
|
if g.mbtn_mask & 0x02 > 0 {
|
|
|
|
e.mouse_button = .right
|
|
|
|
}
|
|
|
|
if g.mbtn_mask & 0x04 > 0 {
|
|
|
|
e.mouse_button = .middle
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 11:02:04 +03:00
|
|
|
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
|
|
|
|
if e.typ in [.key_down, .key_up] {
|
|
|
|
key_idx := int(e.key_code) % key_code_max
|
|
|
|
g.pressed_keys[key_idx] = e.typ == .key_down
|
|
|
|
}
|
2020-06-04 17:05:12 +03:00
|
|
|
if g.config.event_fn != voidptr(0) {
|
|
|
|
g.config.event_fn(e, g.config.user_data)
|
2019-08-23 00:00:31 +03:00
|
|
|
}
|
2020-06-05 11:46:58 +03:00
|
|
|
match e.typ {
|
2021-06-24 11:02:04 +03:00
|
|
|
.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.mouse_leave {
|
|
|
|
if g.config.leave_fn != voidptr(0) {
|
|
|
|
g.config.leave_fn(e, g.config.user_data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.mouse_enter {
|
|
|
|
if g.config.enter_fn != voidptr(0) {
|
|
|
|
g.config.enter_fn(e, g.config.user_data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.mouse_scroll {
|
|
|
|
if g.config.scroll_fn != voidptr(0) {
|
|
|
|
g.config.scroll_fn(e, g.config.user_data)
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 11:46:58 +03:00
|
|
|
.key_down {
|
|
|
|
if g.config.keydown_fn != voidptr(0) {
|
2021-06-24 11:02:04 +03:00
|
|
|
g.config.keydown_fn(e.key_code, Modifier(e.modifiers), g.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)
|
2020-06-05 11:46:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.char {
|
|
|
|
if g.config.char_fn != voidptr(0) {
|
2021-06-24 11:02:04 +03:00
|
|
|
g.config.char_fn(e.char_code, g.config.user_data)
|
2020-06-05 11:46:58 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 11:02:04 +03:00
|
|
|
.resized {
|
|
|
|
if g.config.resized_fn != voidptr(0) {
|
|
|
|
g.config.resized_fn(e, g.config.user_data)
|
2020-09-21 03:42:28 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 11:02:04 +03:00
|
|
|
.quit_requested {
|
|
|
|
if g.config.quit_fn != voidptr(0) {
|
|
|
|
g.config.quit_fn(e, g.config.user_data)
|
2020-09-21 03:42:28 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 11:02:04 +03:00
|
|
|
else {
|
|
|
|
// dump(e)
|
|
|
|
}
|
2020-06-05 11:46:58 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-08-02 00:40:25 +03:00
|
|
|
fn gg_cleanup_fn(user_data voidptr) {
|
2021-01-05 21:07:45 +03:00
|
|
|
mut g := unsafe { &Context(user_data) }
|
2020-06-04 17:05:12 +03:00
|
|
|
if g.config.cleanup_fn != voidptr(0) {
|
|
|
|
g.config.cleanup_fn(g.config.user_data)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 18:05:01 +03:00
|
|
|
fn gg_fail_fn(msg &char, user_data voidptr) {
|
2021-01-05 21:07:45 +03:00
|
|
|
mut g := unsafe { &Context(user_data) }
|
2021-02-15 18:15:52 +03:00
|
|
|
vmsg := unsafe { tos3(msg) }
|
2020-06-04 17:05:12 +03:00
|
|
|
if g.config.fail_fn != voidptr(0) {
|
|
|
|
g.config.fail_fn(vmsg, g.config.user_data)
|
2020-08-02 00:40:25 +03:00
|
|
|
} else {
|
2020-06-04 17:05:12 +03:00
|
|
|
eprintln('gg error: $vmsg')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
//
|
2020-08-02 00:40:25 +03:00
|
|
|
pub fn new_context(cfg Config) &Context {
|
2020-06-04 21:26:18 +03:00
|
|
|
mut g := &Context{
|
2020-06-04 17:05:12 +03:00
|
|
|
width: cfg.width
|
|
|
|
height: cfg.height
|
|
|
|
config: cfg
|
2020-07-06 20:45:00 +03:00
|
|
|
ft: 0
|
2020-11-20 16:50:06 +03:00
|
|
|
ui_mode: cfg.ui_mode
|
2021-01-23 12:25:40 +03:00
|
|
|
native_rendering: cfg.native_rendering
|
2020-06-04 17:05:12 +03:00
|
|
|
}
|
2020-08-19 08:10:42 +03:00
|
|
|
g.set_bg_color(cfg.bg_color)
|
2020-08-02 00:40:25 +03:00
|
|
|
// C.printf('new_context() %p\n', cfg.user_data)
|
2020-06-04 17:05:12 +03:00
|
|
|
window := C.sapp_desc{
|
|
|
|
user_data: g
|
|
|
|
init_userdata_cb: gg_init_sokol_window
|
|
|
|
frame_userdata_cb: gg_frame_fn
|
|
|
|
event_userdata_cb: gg_event_fn
|
|
|
|
fail_userdata_cb: gg_fail_fn
|
|
|
|
cleanup_userdata_cb: gg_cleanup_fn
|
2021-04-19 15:38:48 +03:00
|
|
|
window_title: &char(cfg.window_title.str)
|
|
|
|
html5_canvas_name: &char(cfg.window_title.str)
|
2020-06-04 17:05:12 +03:00
|
|
|
width: cfg.width
|
|
|
|
height: cfg.height
|
2021-01-01 14:04:51 +03:00
|
|
|
sample_count: cfg.sample_count
|
2020-06-04 21:09:46 +03:00
|
|
|
high_dpi: true
|
2020-06-05 00:51:54 +03:00
|
|
|
fullscreen: cfg.fullscreen
|
2021-04-07 21:39:23 +03:00
|
|
|
__v_native_render: cfg.native_rendering
|
2020-06-04 17:05:12 +03:00
|
|
|
}
|
|
|
|
g.window = window
|
|
|
|
return g
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:26:18 +03:00
|
|
|
pub fn (gg &Context) run() {
|
2020-06-04 17:05:12 +03:00
|
|
|
sapp.run(&gg.window)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2021-05-27 17:56:18 +03:00
|
|
|
// quit closes the context window and exits the event loop for it
|
|
|
|
pub fn (ctx &Context) quit() {
|
|
|
|
sapp.request_quit() // does not require ctx right now, but sokol multi-window might in the future
|
|
|
|
}
|
|
|
|
|
2020-08-19 08:10:42 +03:00
|
|
|
pub fn (mut ctx Context) set_bg_color(c gx.Color) {
|
2020-10-10 11:37:17 +03:00
|
|
|
ctx.clear_pass = gfx.create_clear_pass(f32(c.r) / 255.0, f32(c.g) / 255.0, f32(c.b) / 255.0,
|
|
|
|
f32(c.a) / 255.0)
|
2020-08-19 08:10:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Fix alpha
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ctx &Context) draw_rect(x f32, y f32, w f32, h f32, c gx.Color) {
|
2021-01-23 12:25:40 +03:00
|
|
|
$if macos {
|
|
|
|
if ctx.native_rendering {
|
|
|
|
C.darwin_draw_rect(x, ctx.height - (y + h), w, h, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 12:05:30 +03:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
2020-08-19 08:10:42 +03:00
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2020-06-04 17:05:12 +03:00
|
|
|
sgl.begin_quads()
|
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f((x + w) * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f((x + w) * ctx.scale, (y + h) * ctx.scale)
|
|
|
|
sgl.v2f(x * ctx.scale, (y + h) * ctx.scale)
|
2020-09-20 04:47:22 +03:00
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2021-01-27 23:39:11 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (ctx &Context) draw_square(x f32, y f32, s f32, c gx.Color) {
|
|
|
|
ctx.draw_rect(x, y, s, s, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn (ctx &Context) set_pixel(x f32, y f32, c gx.Color) {
|
|
|
|
ctx.draw_square(x, y, 1, c)
|
|
|
|
}
|
|
|
|
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ctx &Context) draw_triangle(x f32, y f32, x2 f32, y2 f32, x3 f32, y3 f32, c gx.Color) {
|
2020-09-20 12:05:30 +03:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
2020-09-20 04:47:22 +03:00
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
|
|
|
sgl.begin_quads()
|
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f(x2 * ctx.scale, y2 * ctx.scale)
|
|
|
|
sgl.v2f(x3 * ctx.scale, y3 * ctx.scale)
|
2020-06-04 17:05:12 +03:00
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ctx &Context) draw_empty_rect(x f32, y f32, w f32, h f32, c gx.Color) {
|
2020-09-20 12:05:30 +03:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
2020-08-19 08:10:42 +03:00
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2020-06-04 17:05:12 +03:00
|
|
|
sgl.begin_line_strip()
|
2021-05-28 22:37:02 +03:00
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f((x + w) * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f((x + w) * ctx.scale, (y + h) * ctx.scale)
|
|
|
|
sgl.v2f(x * ctx.scale, (y + h) * ctx.scale)
|
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
2020-06-04 17:05:12 +03:00
|
|
|
sgl.end()
|
2020-01-16 00:17:40 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 23:39:11 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (ctx &Context) draw_empty_square(x f32, y f32, s f32, c gx.Color) {
|
|
|
|
ctx.draw_empty_rect(x, y, s, s, c)
|
|
|
|
}
|
|
|
|
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ctx &Context) draw_circle_line(x f32, y f32, r int, segments int, c gx.Color) {
|
2021-04-07 21:39:23 +03:00
|
|
|
$if macos {
|
|
|
|
if ctx.native_rendering {
|
|
|
|
C.darwin_draw_circle(x - r + 1, ctx.height - (y + r + 3), r, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-10-10 11:26:08 +03:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2021-06-19 09:07:34 +03:00
|
|
|
nx := x * ctx.scale
|
|
|
|
ny := y * ctx.scale
|
|
|
|
nr := r * ctx.scale
|
2020-10-10 11:26:08 +03:00
|
|
|
mut theta := f32(0)
|
|
|
|
mut xx := f32(0)
|
|
|
|
mut yy := f32(0)
|
|
|
|
sgl.begin_line_strip()
|
2020-10-10 11:37:17 +03:00
|
|
|
for i := 0; i < segments + 1; i++ {
|
2020-10-10 11:26:08 +03:00
|
|
|
theta = 2.0 * f32(math.pi) * f32(i) / f32(segments)
|
2021-06-19 09:07:34 +03:00
|
|
|
xx = nr * math.cosf(theta)
|
|
|
|
yy = nr * math.sinf(theta)
|
|
|
|
sgl.v2f(xx + nx, yy + ny)
|
2020-10-10 11:26:08 +03:00
|
|
|
}
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2020-11-15 17:09:35 +03:00
|
|
|
pub fn (ctx &Context) draw_circle(x f32, y f32, r f32, c gx.Color) {
|
2021-05-28 22:37:02 +03:00
|
|
|
ctx.draw_circle_with_segments(x, y, r, 10, c)
|
2020-10-18 22:22:37 +03:00
|
|
|
}
|
2020-11-15 17:11:43 +03:00
|
|
|
|
2020-11-15 17:09:35 +03:00
|
|
|
pub fn (ctx &Context) draw_circle_with_segments(x f32, y f32, r f32, segments int, c gx.Color) {
|
2020-10-10 11:26:08 +03:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2021-05-28 22:37:02 +03:00
|
|
|
nx := x * ctx.scale
|
|
|
|
ny := y * ctx.scale
|
|
|
|
nr := r * ctx.scale
|
2020-10-10 11:26:08 +03:00
|
|
|
mut theta := f32(0)
|
|
|
|
mut xx := f32(0)
|
|
|
|
mut yy := f32(0)
|
|
|
|
sgl.begin_triangle_strip()
|
2020-10-10 11:37:17 +03:00
|
|
|
for i := 0; i < segments + 1; i++ {
|
2020-10-10 11:26:08 +03:00
|
|
|
theta = 2.0 * f32(math.pi) * f32(i) / f32(segments)
|
2021-05-28 22:37:02 +03:00
|
|
|
xx = nr * math.cosf(theta)
|
|
|
|
yy = nr * math.sinf(theta)
|
|
|
|
sgl.v2f(xx + nx, yy + ny)
|
|
|
|
sgl.v2f(nx, ny)
|
2020-10-10 11:26:08 +03:00
|
|
|
}
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ctx &Context) draw_arc_line(x f32, y f32, r int, start_angle f32, arc_angle f32, segments int, c gx.Color) {
|
2020-10-10 11:26:08 +03:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2020-10-11 21:36:18 +03:00
|
|
|
theta := f32(arc_angle / f32(segments))
|
2020-10-10 11:26:08 +03:00
|
|
|
tan_factor := math.tanf(theta)
|
|
|
|
rad_factor := math.cosf(theta)
|
2021-05-28 22:37:02 +03:00
|
|
|
nx := x * ctx.scale
|
|
|
|
ny := y * ctx.scale
|
2020-10-10 11:26:08 +03:00
|
|
|
mut xx := f32(r * math.cosf(start_angle))
|
|
|
|
mut yy := f32(r * math.sinf(start_angle))
|
|
|
|
sgl.begin_line_strip()
|
2020-10-10 11:37:17 +03:00
|
|
|
for i := 0; i < segments + 1; i++ {
|
2021-05-28 22:37:02 +03:00
|
|
|
sgl.v2f(xx + nx, yy + ny)
|
2020-10-10 11:26:08 +03:00
|
|
|
tx := -yy
|
|
|
|
ty := xx
|
|
|
|
xx += tx * tan_factor
|
|
|
|
yy += ty * tan_factor
|
|
|
|
xx *= rad_factor
|
|
|
|
yy *= rad_factor
|
|
|
|
}
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ctx &Context) draw_arc(x f32, y f32, r int, start_angle f32, arc_angle f32, segments int, c gx.Color) {
|
2020-10-10 11:26:08 +03:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2021-05-28 22:37:02 +03:00
|
|
|
nx := x * ctx.scale
|
|
|
|
ny := y * ctx.scale
|
2020-10-11 21:36:18 +03:00
|
|
|
theta := f32(arc_angle / f32(segments))
|
2020-10-10 11:26:08 +03:00
|
|
|
tan_factor := math.tanf(theta)
|
|
|
|
rad_factor := math.cosf(theta)
|
|
|
|
mut xx := f32(r * math.cosf(start_angle))
|
|
|
|
mut yy := f32(r * math.sinf(start_angle))
|
|
|
|
sgl.begin_triangle_strip()
|
2020-10-10 11:37:17 +03:00
|
|
|
for i := 0; i < segments + 1; i++ {
|
2021-05-28 22:37:02 +03:00
|
|
|
sgl.v2f(xx + nx, yy + ny)
|
|
|
|
sgl.v2f(nx, ny)
|
2020-10-10 11:26:08 +03:00
|
|
|
tx := -yy
|
|
|
|
ty := xx
|
|
|
|
xx += tx * tan_factor
|
|
|
|
yy += ty * tan_factor
|
|
|
|
xx *= rad_factor
|
|
|
|
yy *= rad_factor
|
|
|
|
}
|
|
|
|
sgl.end()
|
2020-07-13 02:02:47 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:26:18 +03:00
|
|
|
pub fn (gg &Context) begin() {
|
2020-07-06 20:45:00 +03:00
|
|
|
if gg.render_text && gg.font_inited {
|
|
|
|
gg.ft.flush()
|
|
|
|
}
|
2020-06-04 17:05:12 +03:00
|
|
|
sgl.defaults()
|
|
|
|
sgl.matrix_mode_projection()
|
|
|
|
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
|
2020-01-27 22:42:32 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:26:18 +03:00
|
|
|
pub fn (gg &Context) end() {
|
2020-06-04 17:05:12 +03:00
|
|
|
gfx.begin_default_pass(gg.clear_pass, sapp.width(), sapp.height())
|
|
|
|
sgl.draw()
|
|
|
|
gfx.end_pass()
|
|
|
|
gfx.commit()
|
2020-11-21 01:49:52 +03:00
|
|
|
/*
|
2020-06-04 17:05:12 +03:00
|
|
|
if gg.config.wait_events {
|
2020-08-02 00:40:25 +03:00
|
|
|
// println('gg: waiting')
|
2020-06-04 17:05:12 +03:00
|
|
|
wait_events()
|
|
|
|
}
|
2020-11-21 01:49:52 +03:00
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-20 18:38:00 +03:00
|
|
|
|
2021-06-19 21:32:08 +03:00
|
|
|
// resize the context's Window
|
2021-01-25 00:27:27 +03:00
|
|
|
pub fn (mut ctx Context) resize(width int, height int) {
|
|
|
|
ctx.width = width
|
|
|
|
ctx.height = height
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:32:08 +03:00
|
|
|
// draw_line draws a line between the points provided
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ctx &Context) draw_line(x f32, y f32, x2 f32, y2 f32, c gx.Color) {
|
2020-09-20 12:05:30 +03:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
2021-06-19 21:32:08 +03:00
|
|
|
|
2021-06-21 20:30:03 +03:00
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
|
|
|
sgl.begin_line_strip()
|
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f(x2 * ctx.scale, y2 * ctx.scale)
|
|
|
|
sgl.end()
|
2021-06-19 21:32:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw_line_with_config draws a line between the points provided with the PenConfig
|
|
|
|
pub fn (ctx &Context) draw_line_with_config(x f32, y f32, x2 f32, y2 f32, config PenConfig) {
|
|
|
|
if config.color.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.thickness <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nx := x * ctx.scale
|
|
|
|
ny := y * ctx.scale
|
|
|
|
nx2 := x2 * ctx.scale
|
|
|
|
ny2 := y2 * ctx.scale
|
|
|
|
|
|
|
|
dx := nx2 - nx
|
|
|
|
dy := ny2 - ny
|
|
|
|
length := math.sqrtf(math.powf(x2 - x, 2) + math.powf(y2 - y, 2))
|
|
|
|
theta := f32(math.atan2(dy, dx))
|
|
|
|
|
|
|
|
sgl.push_matrix()
|
|
|
|
|
|
|
|
sgl.translate(nx, ny, 0)
|
|
|
|
sgl.rotate(theta, 0, 0, 1)
|
|
|
|
sgl.translate(-nx, -ny, 0)
|
|
|
|
|
|
|
|
if config.line_type == .solid {
|
|
|
|
ctx.draw_rect(x, y, length, config.thickness, config.color)
|
|
|
|
} else {
|
|
|
|
size := if config.line_type == .dotted { config.thickness } else { config.thickness * 3 }
|
|
|
|
space := if size == 1 { 2 } else { size }
|
|
|
|
|
|
|
|
mut available := length
|
|
|
|
mut start_x := x
|
|
|
|
|
|
|
|
for i := 0; available > 0; i++ {
|
|
|
|
if i % 2 == 0 {
|
|
|
|
ctx.draw_rect(start_x, y, size, config.thickness, config.color)
|
|
|
|
available -= size
|
|
|
|
start_x += size
|
|
|
|
continue
|
2021-04-26 10:01:55 +03:00
|
|
|
}
|
2021-06-19 21:32:08 +03:00
|
|
|
|
|
|
|
available -= space
|
|
|
|
start_x += space
|
2020-08-23 05:57:12 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-19 21:32:08 +03:00
|
|
|
|
|
|
|
sgl.pop_matrix()
|
2020-08-02 00:40:25 +03:00
|
|
|
}
|
2020-06-05 16:43:44 +03:00
|
|
|
|
2020-11-22 22:13:40 +03:00
|
|
|
pub fn (ctx &Context) draw_rounded_rect(x f32, y f32, w f32, h f32, radius f32, color gx.Color) {
|
|
|
|
sgl.c4b(color.r, color.g, color.b, color.a)
|
|
|
|
sgl.begin_triangle_strip()
|
|
|
|
mut theta := f32(0)
|
|
|
|
mut xx := f32(0)
|
|
|
|
mut yy := f32(0)
|
2021-05-28 22:37:02 +03:00
|
|
|
r := radius * ctx.scale
|
|
|
|
nx := x * ctx.scale
|
|
|
|
ny := y * ctx.scale
|
|
|
|
width := w * ctx.scale
|
|
|
|
height := h * ctx.scale
|
2020-11-22 22:13:40 +03:00
|
|
|
segments := 2 * math.pi * r
|
|
|
|
segdiv := segments / 4
|
|
|
|
rb := 0
|
|
|
|
lb := int(rb + segdiv)
|
|
|
|
lt := int(lb + segdiv)
|
|
|
|
rt := int(lt + segdiv)
|
|
|
|
// left top
|
|
|
|
lx := nx + r
|
|
|
|
ly := ny + r
|
|
|
|
for i in lt .. rt {
|
|
|
|
theta = 2 * f32(math.pi) * f32(i) / segments
|
|
|
|
xx = r * math.cosf(theta)
|
|
|
|
yy = r * math.sinf(theta)
|
|
|
|
sgl.v2f(xx + lx, yy + ly)
|
|
|
|
sgl.v2f(lx, ly)
|
|
|
|
}
|
|
|
|
// right top
|
2021-04-29 10:58:57 +03:00
|
|
|
mut rx := nx + width - r
|
2020-11-22 22:13:40 +03:00
|
|
|
mut ry := ny + r
|
|
|
|
for i in rt .. int(segments) {
|
|
|
|
theta = 2 * f32(math.pi) * f32(i) / segments
|
|
|
|
xx = r * math.cosf(theta)
|
|
|
|
yy = r * math.sinf(theta)
|
|
|
|
sgl.v2f(xx + rx, yy + ry)
|
|
|
|
sgl.v2f(rx, ry)
|
|
|
|
}
|
|
|
|
// right bottom
|
|
|
|
mut rbx := rx
|
2021-04-29 10:58:57 +03:00
|
|
|
mut rby := ny + height - r
|
2020-11-22 22:13:40 +03:00
|
|
|
for i in rb .. lb {
|
|
|
|
theta = 2 * f32(math.pi) * f32(i) / segments
|
|
|
|
xx = r * math.cosf(theta)
|
|
|
|
yy = r * math.sinf(theta)
|
|
|
|
sgl.v2f(xx + rbx, yy + rby)
|
|
|
|
sgl.v2f(rbx, rby)
|
|
|
|
}
|
|
|
|
// left bottom
|
|
|
|
mut lbx := lx
|
2021-04-29 10:58:57 +03:00
|
|
|
mut lby := ny + height - r
|
2020-11-22 22:13:40 +03:00
|
|
|
for i in lb .. lt {
|
|
|
|
theta = 2 * f32(math.pi) * f32(i) / segments
|
|
|
|
xx = r * math.cosf(theta)
|
|
|
|
yy = r * math.sinf(theta)
|
|
|
|
sgl.v2f(xx + lbx, yy + lby)
|
|
|
|
sgl.v2f(lbx, lby)
|
|
|
|
}
|
|
|
|
sgl.v2f(lx + xx, ly)
|
|
|
|
sgl.v2f(lx, ly)
|
|
|
|
sgl.end()
|
|
|
|
sgl.begin_quads()
|
|
|
|
sgl.v2f(lx, ly)
|
|
|
|
sgl.v2f(rx, ry)
|
|
|
|
sgl.v2f(rbx, rby)
|
|
|
|
sgl.v2f(lbx, lby)
|
|
|
|
sgl.end()
|
2020-07-06 20:45:00 +03:00
|
|
|
}
|
|
|
|
|
2020-11-21 17:40:02 +03:00
|
|
|
pub fn (ctx &Context) draw_empty_rounded_rect(x f32, y f32, w f32, h f32, radius f32, border_color gx.Color) {
|
|
|
|
mut theta := f32(0)
|
|
|
|
mut xx := f32(0)
|
|
|
|
mut yy := f32(0)
|
2021-05-28 22:37:02 +03:00
|
|
|
r := radius * ctx.scale
|
|
|
|
nx := x * ctx.scale
|
|
|
|
ny := y * ctx.scale
|
|
|
|
width := w * ctx.scale
|
|
|
|
height := h * ctx.scale
|
2020-11-21 17:40:02 +03:00
|
|
|
segments := 2 * math.pi * r
|
|
|
|
segdiv := segments / 4
|
|
|
|
rb := 0
|
|
|
|
lb := int(rb + segdiv)
|
|
|
|
lt := int(lb + segdiv)
|
|
|
|
rt := int(lt + segdiv)
|
|
|
|
sgl.c4b(border_color.r, border_color.g, border_color.b, border_color.a)
|
|
|
|
sgl.begin_line_strip()
|
|
|
|
// left top
|
|
|
|
lx := nx + r
|
|
|
|
ly := ny + r
|
|
|
|
for i in lt .. rt {
|
|
|
|
theta = 2 * f32(math.pi) * f32(i) / segments
|
|
|
|
xx = r * math.cosf(theta)
|
|
|
|
yy = r * math.sinf(theta)
|
|
|
|
sgl.v2f(xx + lx, yy + ly)
|
|
|
|
}
|
|
|
|
// right top
|
2021-04-29 10:58:57 +03:00
|
|
|
mut rx := nx + width - r
|
2020-11-21 17:40:02 +03:00
|
|
|
mut ry := ny + r
|
|
|
|
for i in rt .. int(segments) {
|
|
|
|
theta = 2 * f32(math.pi) * f32(i) / segments
|
|
|
|
xx = r * math.cosf(theta)
|
|
|
|
yy = r * math.sinf(theta)
|
|
|
|
sgl.v2f(xx + rx, yy + ry)
|
|
|
|
}
|
|
|
|
// right bottom
|
|
|
|
mut rbx := rx
|
2021-04-29 10:58:57 +03:00
|
|
|
mut rby := ny + height - r
|
2020-11-21 17:40:02 +03:00
|
|
|
for i in rb .. lb {
|
|
|
|
theta = 2 * f32(math.pi) * f32(i) / segments
|
|
|
|
xx = r * math.cosf(theta)
|
|
|
|
yy = r * math.sinf(theta)
|
|
|
|
sgl.v2f(xx + rbx, yy + rby)
|
|
|
|
}
|
|
|
|
// left bottom
|
|
|
|
mut lbx := lx
|
2021-04-29 10:58:57 +03:00
|
|
|
mut lby := ny + height - r
|
2020-11-21 17:40:02 +03:00
|
|
|
for i in lb .. lt {
|
|
|
|
theta = 2 * f32(math.pi) * f32(i) / segments
|
|
|
|
xx = r * math.cosf(theta)
|
|
|
|
yy = r * math.sinf(theta)
|
|
|
|
sgl.v2f(xx + lbx, yy + lby)
|
|
|
|
}
|
|
|
|
sgl.v2f(lx + xx, ly)
|
|
|
|
sgl.end()
|
2020-06-07 16:20:32 +03:00
|
|
|
}
|
|
|
|
|
2021-02-01 19:18:23 +03:00
|
|
|
// draw_convex_poly draws a convex polygon, given an array of points, and a color.
|
|
|
|
// Note that the points must be given in clockwise order.
|
|
|
|
pub fn (ctx &Context) draw_convex_poly(points []f32, c gx.Color) {
|
|
|
|
assert points.len % 2 == 0
|
|
|
|
len := points.len / 2
|
|
|
|
assert len >= 3
|
|
|
|
|
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
|
|
|
|
|
|
|
sgl.begin_triangle_strip()
|
|
|
|
x0 := points[0]
|
|
|
|
y0 := points[1]
|
|
|
|
for i in 1 .. (len / 2 + 1) {
|
|
|
|
sgl.v2f(x0, y0)
|
|
|
|
sgl.v2f(points[i * 4 - 2], points[i * 4 - 1])
|
|
|
|
sgl.v2f(points[i * 4], points[i * 4 + 1])
|
|
|
|
}
|
|
|
|
|
|
|
|
if len % 2 == 0 {
|
|
|
|
sgl.v2f(points[2 * len - 2], points[2 * len - 1])
|
|
|
|
}
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw_empty_poly - draws the borders of a polygon, given an array of points, and a color.
|
|
|
|
// Note that the points must be given in clockwise order.
|
|
|
|
pub fn (ctx &Context) draw_empty_poly(points []f32, c gx.Color) {
|
|
|
|
assert points.len % 2 == 0
|
|
|
|
len := points.len / 2
|
|
|
|
assert len >= 3
|
|
|
|
|
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
|
|
|
|
|
|
|
sgl.begin_line_strip()
|
|
|
|
for i in 0 .. len {
|
|
|
|
sgl.v2f(points[2 * i], points[2 * i + 1])
|
|
|
|
}
|
|
|
|
sgl.v2f(points[0], points[1])
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2020-12-04 01:17:00 +03:00
|
|
|
pub fn screen_size() Size {
|
|
|
|
$if macos {
|
|
|
|
return C.gg_get_screen_size()
|
|
|
|
}
|
|
|
|
// TODO windows, linux, etc
|
|
|
|
return Size{}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:11:03 +03:00
|
|
|
// window_size returns the `Size` of the active window
|
|
|
|
pub fn window_size() Size {
|
2021-02-22 22:24:18 +03:00
|
|
|
s := dpi_scale()
|
2021-02-16 14:41:21 +03:00
|
|
|
return Size{int(sapp.width() / s), int(sapp.height() / s)}
|
2021-01-29 13:11:03 +03:00
|
|
|
}
|
|
|
|
|
2021-02-28 00:11:26 +03:00
|
|
|
// window_size_real_pixels returns the `Size` of the active window without scale
|
|
|
|
pub fn window_size_real_pixels() Size {
|
|
|
|
return Size{sapp.width(), sapp.height()}
|
|
|
|
}
|
|
|
|
|
2021-02-17 08:44:01 +03:00
|
|
|
pub fn dpi_scale() f32 {
|
2021-02-22 22:24:18 +03:00
|
|
|
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
|
2021-02-17 08:44:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn high_dpi() bool {
|
|
|
|
return C.sapp_high_dpi()
|
|
|
|
}
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
fn C.WaitMessage()
|
2020-01-16 00:17:40 +03:00
|
|
|
|
2020-11-21 01:49:52 +03:00
|
|
|
/*
|
2020-06-04 17:05:12 +03:00
|
|
|
pub fn wait_events() {
|
|
|
|
unsafe {
|
2020-08-02 00:40:25 +03:00
|
|
|
$if macos {
|
|
|
|
#NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny
|
|
|
|
#untilDate:[NSDate distantFuture]
|
|
|
|
#inMode:NSDefaultRunLoopMode
|
|
|
|
#dequeue:YES];
|
|
|
|
#[NSApp sendEvent:event];
|
2020-06-04 17:05:12 +03:00
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
C.WaitMessage()
|
|
|
|
}
|
|
|
|
}
|
2020-02-03 06:01:39 +03:00
|
|
|
}
|
2020-11-21 01:49:52 +03:00
|
|
|
*/
|