2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-07-06 20:29:05 +02:00
|
|
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
2021-06-24 10:02:04 +02:00
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
module gg
|
|
|
|
|
2020-04-26 11:42:44 +02:00
|
|
|
import gx
|
2020-06-04 16:05:12 +02:00
|
|
|
import sokol.sapp
|
|
|
|
import sokol.sgl
|
|
|
|
import sokol.gfx
|
2020-10-10 10:37:17 +02:00
|
|
|
import math
|
2019-06-22 20:20:28 +02:00
|
|
|
|
2021-06-24 10:02:04 +02: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-01 23:40:25 +02:00
|
|
|
|
2021-06-24 10:02:04 +02:00
|
|
|
pub type FNKeyDown = fn (c KeyCode, m Modifier, data voidptr)
|
2020-08-01 23:40:25 +02:00
|
|
|
|
2021-06-24 10:02:04 +02:00
|
|
|
pub type FNKeyUp = fn (c KeyCode, m Modifier, data voidptr)
|
2020-08-01 23:40:25 +02:00
|
|
|
|
2021-06-24 10:02:04 +02:00
|
|
|
pub type FNMove = fn (x f32, y f32, data voidptr)
|
2020-08-01 23:40:25 +02:00
|
|
|
|
2021-06-24 10:02:04 +02:00
|
|
|
pub type FNClick = fn (x f32, y f32, button MouseButton, data voidptr)
|
2020-09-21 02:42:28 +02:00
|
|
|
|
2021-06-24 10:02:04 +02:00
|
|
|
pub type FNUnClick = fn (x f32, y f32, button MouseButton, data voidptr)
|
2021-05-28 11:46:23 +02:00
|
|
|
|
2021-06-24 10:02:04 +02:00
|
|
|
pub type FNChar = fn (c u32, data voidptr)
|
2019-06-22 20:20:28 +02:00
|
|
|
|
2020-06-04 16:05:12 +02:00
|
|
|
pub struct Config {
|
2019-08-22 23:00:31 +02:00
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
width int
|
|
|
|
height int
|
2021-06-24 10:02:04 +02:00
|
|
|
use_ortho bool // unused, still here just for backwards compatibility
|
2021-01-12 04:38:43 +01:00
|
|
|
retina bool
|
|
|
|
resizable bool
|
|
|
|
user_data voidptr
|
|
|
|
font_size int
|
|
|
|
create_window bool
|
2020-06-04 16:05:12 +02:00
|
|
|
// window_user_ptr voidptr
|
2021-01-12 04:38:43 +01:00
|
|
|
window_title string
|
|
|
|
borderless_window bool
|
|
|
|
always_on_top bool
|
|
|
|
bg_color gx.Color
|
2021-06-24 10:02:04 +02: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 00:04:29 +01:00
|
|
|
// wait_events bool // set this to true for UIs, to save power
|
2021-09-25 14:06:56 +02:00
|
|
|
fullscreen bool
|
|
|
|
scale f32 = 1.0
|
|
|
|
sample_count int
|
|
|
|
swap_interval int = 1 // 1 = 60fps, 2 = 30fps etc. The preferred swap interval (ignored on some platforms)
|
2021-05-27 19:14:36 +02:00
|
|
|
// ved needs this
|
2020-08-01 23:40:25 +02:00
|
|
|
// init_text bool
|
2020-12-01 16:30:22 +01:00
|
|
|
font_path string
|
|
|
|
custom_bold_font_path string
|
|
|
|
ui_mode bool // refreshes only on events to save CPU usage
|
2021-01-21 22:07:47 +01:00
|
|
|
// font bytes for embedding
|
|
|
|
font_bytes_normal []byte
|
|
|
|
font_bytes_bold []byte
|
|
|
|
font_bytes_mono []byte
|
|
|
|
font_bytes_italic []byte
|
2021-01-23 10:25:40 +01:00
|
|
|
native_rendering bool // Cocoa on macOS/iOS, GDI+ on Windows
|
2021-09-01 08:21:27 +02:00
|
|
|
// drag&drop
|
|
|
|
enable_dragndrop bool // enable file dropping (drag'n'drop), default is false
|
|
|
|
max_dropped_files int = 1 // max number of dropped files to process (default: 1)
|
|
|
|
max_dropped_file_path_length int = 2048 // max length in bytes of a dropped UTF-8 file path (default: 2048)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 20:32:08 +02:00
|
|
|
pub struct PenConfig {
|
|
|
|
color gx.Color
|
|
|
|
line_type PenLineType = .solid
|
|
|
|
thickness int = 1
|
|
|
|
}
|
|
|
|
|
2020-08-01 23:40:25 +02:00
|
|
|
pub struct Size {
|
|
|
|
pub:
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
}
|
2020-06-04 19:57:13 +02:00
|
|
|
|
2020-06-04 16:05:12 +02:00
|
|
|
fn gg_frame_fn(user_data voidptr) {
|
2021-01-05 19:07:45 +01:00
|
|
|
mut ctx := unsafe { &Context(user_data) }
|
2021-06-24 10:02:04 +02:00
|
|
|
ctx.frame++
|
2020-11-20 14:50:06 +01:00
|
|
|
if ctx.config.frame_fn == voidptr(0) {
|
|
|
|
return
|
|
|
|
}
|
2021-01-23 10:25:40 +01:00
|
|
|
if ctx.native_rendering {
|
|
|
|
// return
|
|
|
|
}
|
2021-04-07 20:39:23 +02:00
|
|
|
|
2021-12-08 21:38:33 +01:00
|
|
|
ctx.record_frame()
|
|
|
|
|
2020-11-20 14:50:06 +01: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-22 23:00:31 +02:00
|
|
|
}
|
2021-11-06 17:24:19 +01:00
|
|
|
ctx.config.frame_fn(ctx.user_data)
|
2020-11-20 14:50:06 +01:00
|
|
|
ctx.needs_refresh = false
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut ctx Context) refresh_ui() {
|
|
|
|
ctx.needs_refresh = true
|
|
|
|
ctx.ticks = 0
|
2019-08-22 23:00:31 +02:00
|
|
|
}
|
2019-07-19 13:27:44 +02:00
|
|
|
|
2021-11-03 15:20:19 +01:00
|
|
|
fn gg_event_fn(ce voidptr, user_data voidptr) {
|
2021-02-17 06:44:01 +01:00
|
|
|
// e := unsafe { &sapp.Event(ce) }
|
2021-02-17 19:14:37 +01:00
|
|
|
mut e := unsafe { &Event(ce) }
|
2021-01-05 19:07:45 +01:00
|
|
|
mut g := unsafe { &Context(user_data) }
|
2021-03-27 11:03:15 +01:00
|
|
|
if g.ui_mode {
|
|
|
|
g.refresh_ui()
|
|
|
|
}
|
2021-02-17 19:14:37 +01:00
|
|
|
if e.typ == .mouse_down {
|
|
|
|
bitplace := int(e.mouse_button)
|
|
|
|
g.mbtn_mask |= byte(1 << bitplace)
|
2021-06-24 10:02:04 +02:00
|
|
|
g.mouse_buttons = MouseButtons(g.mbtn_mask)
|
2021-02-17 19:14:37 +01:00
|
|
|
}
|
|
|
|
if e.typ == .mouse_up {
|
|
|
|
bitplace := int(e.mouse_button)
|
|
|
|
g.mbtn_mask &= ~(byte(1 << bitplace))
|
2021-06-24 10:02:04 +02:00
|
|
|
g.mouse_buttons = MouseButtons(g.mbtn_mask)
|
2021-02-17 19:14:37 +01: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 10:02:04 +02: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
|
2021-07-26 17:39:59 +02:00
|
|
|
prev := g.pressed_keys[key_idx]
|
|
|
|
next := e.typ == .key_down
|
|
|
|
g.pressed_keys[key_idx] = next
|
|
|
|
g.pressed_keys_edge[key_idx] = prev != next
|
2021-06-24 10:02:04 +02:00
|
|
|
}
|
2020-06-04 16:05:12 +02:00
|
|
|
if g.config.event_fn != voidptr(0) {
|
|
|
|
g.config.event_fn(e, g.config.user_data)
|
2019-08-22 23:00:31 +02:00
|
|
|
}
|
2020-06-05 10:46:58 +02:00
|
|
|
match e.typ {
|
2021-06-24 10:02:04 +02: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 10:46:58 +02:00
|
|
|
.key_down {
|
|
|
|
if g.config.keydown_fn != voidptr(0) {
|
2021-06-24 10:02:04 +02: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 10:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.char {
|
|
|
|
if g.config.char_fn != voidptr(0) {
|
2021-06-24 10:02:04 +02:00
|
|
|
g.config.char_fn(e.char_code, g.config.user_data)
|
2020-06-05 10:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 10:02:04 +02:00
|
|
|
.resized {
|
|
|
|
if g.config.resized_fn != voidptr(0) {
|
|
|
|
g.config.resized_fn(e, g.config.user_data)
|
2020-09-21 02:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 10:02:04 +02:00
|
|
|
.quit_requested {
|
|
|
|
if g.config.quit_fn != voidptr(0) {
|
|
|
|
g.config.quit_fn(e, g.config.user_data)
|
2020-09-21 02:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 10:02:04 +02:00
|
|
|
else {
|
|
|
|
// dump(e)
|
|
|
|
}
|
2020-06-05 10:46:58 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-01 23:40:25 +02:00
|
|
|
fn gg_cleanup_fn(user_data voidptr) {
|
2021-01-05 19:07:45 +01:00
|
|
|
mut g := unsafe { &Context(user_data) }
|
2020-06-04 16:05:12 +02:00
|
|
|
if g.config.cleanup_fn != voidptr(0) {
|
|
|
|
g.config.cleanup_fn(g.config.user_data)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2021-11-11 16:38:45 +01:00
|
|
|
gfx.shutdown()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-04-04 17:05:01 +02:00
|
|
|
fn gg_fail_fn(msg &char, user_data voidptr) {
|
2021-01-05 19:07:45 +01:00
|
|
|
mut g := unsafe { &Context(user_data) }
|
2021-02-15 16:15:52 +01:00
|
|
|
vmsg := unsafe { tos3(msg) }
|
2020-06-04 16:05:12 +02:00
|
|
|
if g.config.fail_fn != voidptr(0) {
|
|
|
|
g.config.fail_fn(vmsg, g.config.user_data)
|
2020-08-01 23:40:25 +02:00
|
|
|
} else {
|
2020-06-04 16:05:12 +02:00
|
|
|
eprintln('gg error: $vmsg')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 21:38:33 +01:00
|
|
|
pub fn (ctx &Context) run() {
|
|
|
|
sapp.run(&ctx.window)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Prepares the context for drawing
|
|
|
|
pub fn (gg &Context) begin() {
|
|
|
|
if gg.render_text && gg.font_inited {
|
|
|
|
gg.ft.flush()
|
|
|
|
}
|
|
|
|
sgl.defaults()
|
|
|
|
sgl.matrix_mode_projection()
|
|
|
|
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finishes drawing for the context
|
|
|
|
pub fn (gg &Context) end() {
|
|
|
|
gfx.begin_default_pass(gg.clear_pass, sapp.width(), sapp.height())
|
|
|
|
sgl.draw()
|
|
|
|
gfx.end_pass()
|
|
|
|
gfx.commit()
|
|
|
|
/*
|
|
|
|
if gg.config.wait_events {
|
|
|
|
// println('gg: waiting')
|
|
|
|
wait_events()
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2021-05-27 16:56:18 +02: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 07:10:42 +02:00
|
|
|
pub fn (mut ctx Context) set_bg_color(c gx.Color) {
|
2020-10-10 10:37:17 +02: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 07:10:42 +02:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Sets a pixel
|
2021-01-27 21:39:11 +01:00
|
|
|
[inline]
|
|
|
|
pub fn (ctx &Context) set_pixel(x f32, y f32, c gx.Color) {
|
2021-10-07 09:46:57 +02:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
|
|
|
sgl.begin_points()
|
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.end()
|
2021-01-27 21:39:11 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Sets pixels from an array of points [x, y, x2, y2, etc...]
|
2021-10-07 09:46:57 +02:00
|
|
|
[direct_array_access; inline]
|
2021-08-20 00:14:25 +02:00
|
|
|
pub fn (ctx &Context) set_pixels(points []f32, c gx.Color) {
|
|
|
|
assert points.len % 2 == 0
|
|
|
|
len := points.len / 2
|
|
|
|
|
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2021-10-07 09:46:57 +02:00
|
|
|
sgl.begin_points()
|
2021-08-20 00:14:25 +02:00
|
|
|
for i in 0 .. len {
|
|
|
|
x, y := points[i * 2], points[i * 2 + 1]
|
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
|
|
|
}
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Draws a filled triangle
|
2020-10-18 08:48:13 +02: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 11:05:30 +02:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
2020-09-20 03:47:22 +02:00
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2021-10-07 02:45:23 +02:00
|
|
|
sgl.begin_triangles()
|
2020-09-20 03:47:22 +02:00
|
|
|
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 16:05:12 +02:00
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Draws the outline of a triangle
|
|
|
|
pub fn (ctx &Context) draw_empty_triangle(x f32, y f32, x2 f32, y2 f32, x3 f32, y3 f32, c gx.Color) {
|
2020-09-20 11:05:30 +02:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
2021-12-15 19:17:07 +01:00
|
|
|
|
2020-08-19 07:10:42 +02:00
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2020-06-04 16:05:12 +02:00
|
|
|
sgl.begin_line_strip()
|
2021-05-28 21:37:02 +02:00
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
2021-12-15 19:17:07 +01:00
|
|
|
sgl.v2f(x2 * ctx.scale, y2 * ctx.scale)
|
|
|
|
sgl.v2f(x3 * ctx.scale, y3 * ctx.scale)
|
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
2020-06-04 16:05:12 +02:00
|
|
|
sgl.end()
|
2020-01-15 22:17:40 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Draws a filled square
|
|
|
|
[inline]
|
|
|
|
pub fn (ctx &Context) draw_square(x f32, y f32, s f32, c gx.Color) {
|
|
|
|
ctx.draw_rect(x, y, s, s, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draws the outline of a square
|
2021-01-27 21:39:11 +01: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)
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Draws the outline of a rectangle
|
|
|
|
pub fn (ctx &Context) draw_empty_rect(x f32, y f32, w f32, 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.begin_line_strip()
|
|
|
|
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 - 1) * ctx.scale)
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draws a circle
|
2020-11-15 15:09:35 +01:00
|
|
|
pub fn (ctx &Context) draw_circle(x f32, y f32, r f32, c gx.Color) {
|
2021-05-28 21:37:02 +02:00
|
|
|
ctx.draw_circle_with_segments(x, y, r, 10, c)
|
2020-10-18 21:22:37 +02:00
|
|
|
}
|
2020-11-15 15:11:43 +01:00
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Draws a circle with a specific number of segments (affects how smooth/round the circle is)
|
2020-11-15 15:09:35 +01:00
|
|
|
pub fn (ctx &Context) draw_circle_with_segments(x f32, y f32, r f32, segments int, c gx.Color) {
|
2020-10-10 10:26:08 +02:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2021-05-28 21:37:02 +02:00
|
|
|
nx := x * ctx.scale
|
|
|
|
ny := y * ctx.scale
|
|
|
|
nr := r * ctx.scale
|
2020-10-10 10:26:08 +02:00
|
|
|
mut theta := f32(0)
|
|
|
|
mut xx := f32(0)
|
|
|
|
mut yy := f32(0)
|
|
|
|
sgl.begin_triangle_strip()
|
2020-10-10 10:37:17 +02:00
|
|
|
for i := 0; i < segments + 1; i++ {
|
2020-10-10 10:26:08 +02:00
|
|
|
theta = 2.0 * f32(math.pi) * f32(i) / f32(segments)
|
2021-05-28 21:37:02 +02:00
|
|
|
xx = nr * math.cosf(theta)
|
|
|
|
yy = nr * math.sinf(theta)
|
|
|
|
sgl.v2f(xx + nx, yy + ny)
|
|
|
|
sgl.v2f(nx, ny)
|
2020-10-10 10:26:08 +02:00
|
|
|
}
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Draws a circle slice/pie.
|
|
|
|
pub fn (ctx &Context) draw_slice(x f32, y f32, r f32, start_angle f32, arc_angle f32, segments int, c gx.Color) {
|
2020-10-10 10:26:08 +02:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2021-12-15 19:17:07 +01:00
|
|
|
nx := x * ctx.scale
|
|
|
|
ny := y * ctx.scale
|
2020-10-11 20:36:18 +02:00
|
|
|
theta := f32(arc_angle / f32(segments))
|
2020-10-10 10:26:08 +02:00
|
|
|
tan_factor := math.tanf(theta)
|
|
|
|
rad_factor := math.cosf(theta)
|
2021-12-15 19:17:07 +01:00
|
|
|
mut xx := r * math.cosf(start_angle)
|
|
|
|
mut yy := r * math.sinf(start_angle)
|
|
|
|
sgl.begin_triangle_strip()
|
2020-10-10 10:37:17 +02:00
|
|
|
for i := 0; i < segments + 1; i++ {
|
2021-05-28 21:37:02 +02:00
|
|
|
sgl.v2f(xx + nx, yy + ny)
|
2021-12-15 19:17:07 +01:00
|
|
|
sgl.v2f(nx, ny)
|
2020-10-10 10:26:08 +02:00
|
|
|
tx := -yy
|
|
|
|
ty := xx
|
|
|
|
xx += tx * tan_factor
|
|
|
|
yy += ty * tan_factor
|
|
|
|
xx *= rad_factor
|
|
|
|
yy *= rad_factor
|
|
|
|
}
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Draws the outline of a circle slice/pie.
|
|
|
|
pub fn (ctx &Context) draw_empty_slice(x f32, y f32, r f32, start_angle f32, arc_angle f32, segments int, c gx.Color) {
|
2020-10-10 10:26:08 +02:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2020-10-11 20:36:18 +02:00
|
|
|
theta := f32(arc_angle / f32(segments))
|
2020-10-10 10:26:08 +02:00
|
|
|
tan_factor := math.tanf(theta)
|
|
|
|
rad_factor := math.cosf(theta)
|
2021-12-15 19:17:07 +01:00
|
|
|
nx := x * ctx.scale
|
|
|
|
ny := y * ctx.scale
|
|
|
|
mut xx := r * math.cosf(start_angle)
|
|
|
|
mut yy := r * math.sinf(start_angle)
|
|
|
|
sgl.begin_line_strip()
|
2020-10-10 10:37:17 +02:00
|
|
|
for i := 0; i < segments + 1; i++ {
|
2021-05-28 21:37:02 +02:00
|
|
|
sgl.v2f(xx + nx, yy + ny)
|
2020-10-10 10:26:08 +02:00
|
|
|
tx := -yy
|
|
|
|
ty := xx
|
|
|
|
xx += tx * tan_factor
|
|
|
|
yy += ty * tan_factor
|
|
|
|
xx *= rad_factor
|
|
|
|
yy *= rad_factor
|
|
|
|
}
|
|
|
|
sgl.end()
|
2020-07-13 01:02:47 +02:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Resize the context's Window
|
2021-01-24 22:27:27 +01:00
|
|
|
pub fn (mut ctx Context) resize(width int, height int) {
|
|
|
|
ctx.width = width
|
|
|
|
ctx.height = height
|
2021-10-19 15:03:45 +02:00
|
|
|
// C.sapp_resize_window(width, height)
|
2021-01-24 22:27:27 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Draws a line between the points provided
|
2020-10-18 08:48:13 +02:00
|
|
|
pub fn (ctx &Context) draw_line(x f32, y f32, x2 f32, y2 f32, c gx.Color) {
|
2021-07-03 01:49:20 +02:00
|
|
|
$if macos {
|
|
|
|
if ctx.native_rendering {
|
|
|
|
// Make the line more clear on hi dpi screens: draw a rectangle
|
|
|
|
mut width := math.abs(x2 - x)
|
|
|
|
mut height := math.abs(y2 - y)
|
|
|
|
if width == 0 {
|
|
|
|
width = 1
|
|
|
|
} else if height == 0 {
|
|
|
|
height = 1
|
|
|
|
}
|
|
|
|
ctx.draw_rect(x, y, f32(width), f32(height), c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 11:05:30 +02:00
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
2021-06-21 19:30:03 +02: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 20:32:08 +02:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Draws a line between the points provided with the PenConfig
|
2021-06-19 20:32:08 +02:00
|
|
|
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 09:01:55 +02:00
|
|
|
}
|
2021-06-19 20:32:08 +02:00
|
|
|
|
|
|
|
available -= space
|
|
|
|
start_x += space
|
2020-08-23 04:57:12 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-19 20:32:08 +02:00
|
|
|
|
|
|
|
sgl.pop_matrix()
|
2020-08-01 23:40:25 +02:00
|
|
|
}
|
2020-06-05 15:43:44 +02:00
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Draws an arc
|
|
|
|
pub fn (ctx &Context) draw_arc(x f32, y f32, inner_r f32, outer_r f32, start_angle f32, end_angle f32, segments int, c gx.Color) {
|
2021-12-13 20:03:42 +01:00
|
|
|
if start_angle == end_angle || outer_r <= 0.0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mut r1 := inner_r
|
|
|
|
mut r2 := outer_r
|
|
|
|
mut a1 := start_angle
|
|
|
|
mut a2 := end_angle
|
|
|
|
|
|
|
|
// TODO: Maybe this does not make since inner_r and outer_r is actually integers?
|
|
|
|
if outer_r < inner_r {
|
|
|
|
r1, r2 = r2, r1
|
|
|
|
|
|
|
|
if r2 <= 0.0 {
|
|
|
|
r2 = 0.1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if a2 < a1 {
|
|
|
|
a1, a2 = a2, a1
|
|
|
|
}
|
|
|
|
|
|
|
|
if r1 <= 0.0 {
|
2021-12-15 19:17:07 +01:00
|
|
|
ctx.draw_slice(x, y, int(r2), a1, a2, segments, c)
|
2021-12-13 20:03:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mut step_length := (a2 - a1) / f32(segments)
|
|
|
|
mut angle := a1
|
|
|
|
|
|
|
|
sgl.begin_quads()
|
2021-12-15 19:17:07 +01:00
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2021-12-13 20:03:42 +01:00
|
|
|
for _ in 0 .. segments {
|
|
|
|
sgl.v2f(x + f32(math.sin(angle)) * r1, y + f32(math.cos(angle) * r1))
|
|
|
|
sgl.v2f(x + f32(math.sin(angle)) * r2, y + f32(math.cos(angle) * r2))
|
|
|
|
|
|
|
|
sgl.v2f(x + f32(math.sin(angle + step_length)) * r2, y + f32(math.cos(angle +
|
|
|
|
step_length) * r2))
|
|
|
|
sgl.v2f(x + f32(math.sin(angle + step_length)) * r1, y + f32(math.cos(angle +
|
|
|
|
step_length) * r1))
|
|
|
|
|
|
|
|
angle += step_length
|
|
|
|
}
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Draws a filled rounded rectangle
|
|
|
|
pub fn (ctx &Context) draw_rounded_rect(x f32, y f32, w f32, h f32, radius f32, c gx.Color) {
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2020-11-22 20:13:40 +01:00
|
|
|
sgl.begin_triangle_strip()
|
|
|
|
mut theta := f32(0)
|
|
|
|
mut xx := f32(0)
|
|
|
|
mut yy := f32(0)
|
2021-05-28 21:37:02 +02:00
|
|
|
r := radius * ctx.scale
|
|
|
|
nx := x * ctx.scale
|
|
|
|
ny := y * ctx.scale
|
|
|
|
width := w * ctx.scale
|
|
|
|
height := h * ctx.scale
|
2020-11-22 20:13:40 +01: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 09:58:57 +02:00
|
|
|
mut rx := nx + width - r
|
2020-11-22 20:13:40 +01: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 09:58:57 +02:00
|
|
|
mut rby := ny + height - r
|
2020-11-22 20:13:40 +01: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 09:58:57 +02:00
|
|
|
mut lby := ny + height - r
|
2020-11-22 20:13:40 +01: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 19:45:00 +02:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:17:07 +01:00
|
|
|
// Draws the outline of a rounded rectangle
|
|
|
|
pub fn (ctx &Context) draw_empty_rounded_rect(x f32, y f32, w f32, h f32, radius f32, c gx.Color) {
|
2020-11-21 15:40:02 +01:00
|
|
|
mut theta := f32(0)
|
|
|
|
mut xx := f32(0)
|
|
|
|
mut yy := f32(0)
|
2021-05-28 21:37:02 +02:00
|
|
|
r := radius * ctx.scale
|
|
|
|
nx := x * ctx.scale
|
|
|
|
ny := y * ctx.scale
|
|
|
|
width := w * ctx.scale
|
|
|
|
height := h * ctx.scale
|
2020-11-21 15:40:02 +01:00
|
|
|
segments := 2 * math.pi * r
|
|
|
|
segdiv := segments / 4
|
|
|
|
rb := 0
|
|
|
|
lb := int(rb + segdiv)
|
|
|
|
lt := int(lb + segdiv)
|
|
|
|
rt := int(lt + segdiv)
|
2021-12-15 19:17:07 +01:00
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
2020-11-21 15:40:02 +01:00
|
|
|
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 09:58:57 +02:00
|
|
|
mut rx := nx + width - r
|
2020-11-21 15:40:02 +01: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 09:58:57 +02:00
|
|
|
mut rby := ny + height - r
|
2020-11-21 15:40:02 +01: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 09:58:57 +02:00
|
|
|
mut lby := ny + height - r
|
2020-11-21 15:40:02 +01: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 15:20:32 +02:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:18:23 +01: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()
|
2021-08-23 12:38:29 +02:00
|
|
|
x0 := points[0] * ctx.scale
|
|
|
|
y0 := points[1] * ctx.scale
|
2021-02-01 17:18:23 +01:00
|
|
|
for i in 1 .. (len / 2 + 1) {
|
|
|
|
sgl.v2f(x0, y0)
|
2021-08-23 12:38:29 +02:00
|
|
|
sgl.v2f(points[i * 4 - 2] * ctx.scale, points[i * 4 - 1] * ctx.scale)
|
|
|
|
sgl.v2f(points[i * 4] * ctx.scale, points[i * 4 + 1] * ctx.scale)
|
2021-02-01 17:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len % 2 == 0 {
|
2021-08-23 12:38:29 +02:00
|
|
|
sgl.v2f(points[2 * len - 2] * ctx.scale, points[2 * len - 1] * ctx.scale)
|
2021-02-01 17:18:23 +01:00
|
|
|
}
|
|
|
|
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 {
|
2021-08-22 23:37:20 +02:00
|
|
|
sgl.v2f(points[2 * i] * ctx.scale, points[2 * i + 1] * ctx.scale)
|
2021-02-01 17:18:23 +01:00
|
|
|
}
|
2021-08-22 23:37:20 +02:00
|
|
|
sgl.v2f(points[0] * ctx.scale, points[1] * ctx.scale)
|
2021-02-01 17:18:23 +01:00
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2021-08-24 05:35:27 +02:00
|
|
|
// draw_cubic_bezier draws a cubic Bézier curve, also known as a spline, from four points.
|
2021-08-27 15:52:05 +02:00
|
|
|
// The four points is provided as one `points` array which contains a stream of point pairs (x and y coordinates).
|
|
|
|
// Thus a cubic Bézier could be declared as: `points := [x1, y1, control_x1, control_y1, control_x2, control_y2, x2, y2]`.
|
2021-08-24 05:35:27 +02:00
|
|
|
// Please see `draw_cubic_bezier_in_steps` to control the amount of steps (segments) used to draw the curve.
|
2021-08-27 15:52:05 +02:00
|
|
|
pub fn (ctx &Context) draw_cubic_bezier(points []f32, c gx.Color) {
|
|
|
|
ctx.draw_cubic_bezier_in_steps(points, u32(30 * ctx.scale), c)
|
2021-08-24 05:35:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw_cubic_bezier_in_steps draws a cubic Bézier curve, also known as a spline, from four points.
|
|
|
|
// The smoothness of the curve can be controlled with the `steps` parameter. `steps` determines how many iterations is
|
|
|
|
// taken to draw the curve.
|
2021-08-27 15:52:05 +02:00
|
|
|
// The four points is provided as one `points` array which contains a stream of point pairs (x and y coordinates).
|
|
|
|
// Thus a cubic Bézier could be declared as: `points := [x1, y1, control_x1, control_y1, control_x2, control_y2, x2, y2]`.
|
|
|
|
pub fn (ctx &Context) draw_cubic_bezier_in_steps(points []f32, steps u32, c gx.Color) {
|
2021-08-24 05:35:27 +02:00
|
|
|
assert steps > 0
|
2021-08-27 15:52:05 +02:00
|
|
|
assert points.len == 8
|
2021-08-24 05:35:27 +02:00
|
|
|
|
|
|
|
if c.a != 255 {
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
}
|
|
|
|
sgl.c4b(c.r, c.g, c.b, c.a)
|
|
|
|
|
|
|
|
sgl.begin_line_strip()
|
|
|
|
|
|
|
|
p1_x, p1_y := points[0], points[1]
|
2021-08-27 15:52:05 +02:00
|
|
|
p2_x, p2_y := points[6], points[7]
|
2021-08-24 05:35:27 +02:00
|
|
|
|
2021-08-27 15:52:05 +02:00
|
|
|
ctrl_p1_x, ctrl_p1_y := points[2], points[3]
|
|
|
|
ctrl_p2_x, ctrl_p2_y := points[4], points[5]
|
2021-08-24 05:35:27 +02:00
|
|
|
|
|
|
|
// The constant 3 is actually points.len() - 1;
|
|
|
|
|
|
|
|
step := f32(1.0) / steps
|
|
|
|
sgl.v2f(p1_x * ctx.scale, p1_y * ctx.scale)
|
|
|
|
for u := f32(0.0); u <= f32(1.0); u += step {
|
|
|
|
pow_2_u := u * u
|
|
|
|
pow_3_u := pow_2_u * u
|
|
|
|
|
|
|
|
x := pow_3_u * (p2_x + 3 * (ctrl_p1_x - ctrl_p2_x) - p1_x) +
|
|
|
|
3 * pow_2_u * (p1_x - 2 * ctrl_p1_x + ctrl_p2_x) + 3 * u * (ctrl_p1_x - p1_x) + p1_x
|
|
|
|
|
|
|
|
y := pow_3_u * (p2_y + 3 * (ctrl_p1_y - ctrl_p2_y) - p1_y) +
|
|
|
|
3 * pow_2_u * (p1_y - 2 * ctrl_p1_y + ctrl_p2_y) + 3 * u * (ctrl_p1_y - p1_y) + p1_y
|
|
|
|
|
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
|
|
|
}
|
|
|
|
sgl.v2f(p2_x * ctx.scale, p2_y * ctx.scale)
|
|
|
|
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:11:03 +01:00
|
|
|
// window_size returns the `Size` of the active window
|
|
|
|
pub fn window_size() Size {
|
2021-02-22 20:24:18 +01:00
|
|
|
s := dpi_scale()
|
2021-02-16 12:41:21 +01:00
|
|
|
return Size{int(sapp.width() / s), int(sapp.height() / s)}
|
2021-01-29 11:11:03 +01:00
|
|
|
}
|
|
|
|
|
2021-02-27 22:11:26 +01: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 06:44:01 +01:00
|
|
|
pub fn dpi_scale() f32 {
|
2021-02-22 20:24:18 +01: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 {
|
2021-08-25 13:39:37 +02:00
|
|
|
s = 1.0
|
2021-02-22 20:24:18 +01:00
|
|
|
}
|
|
|
|
return s
|
2021-02-17 06:44:01 +01:00
|
|
|
}
|