gg: move code using C types to c.v files, add js.v files (#12873)

pull/12885/head
playX 2021-12-17 16:22:09 +03:00 committed by GitHub
parent 66070ec63e
commit b482da74e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1173 additions and 930 deletions

View File

@ -286,3 +286,746 @@ pub fn (ctx &Context) draw_rect(x f32, y f32, w f32, h f32, c gx.Color) {
sgl.v2f(x * ctx.scale, (y + h) * ctx.scale)
sgl.end()
}
fn gg_frame_fn(user_data voidptr) {
mut ctx := unsafe { &Context(user_data) }
ctx.frame++
if ctx.config.frame_fn == voidptr(0) {
return
}
if ctx.native_rendering {
// return
}
ctx.record_frame()
if ctx.ui_mode && !ctx.needs_refresh {
// Draw 3 more frames after the "stop refresh" command
ctx.ticks++
if ctx.ticks > 3 {
return
}
}
ctx.config.frame_fn(ctx.user_data)
ctx.needs_refresh = false
}
pub fn (mut ctx Context) refresh_ui() {
ctx.needs_refresh = true
ctx.ticks = 0
}
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()
}
if e.typ == .mouse_down {
bitplace := int(e.mouse_button)
g.mbtn_mask |= byte(1 << bitplace)
g.mouse_buttons = MouseButtons(g.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)
}
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
}
}
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
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
}
if g.config.event_fn != voidptr(0) {
g.config.event_fn(e, g.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)
}
}
.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)
}
}
.key_down {
if g.config.keydown_fn != voidptr(0) {
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)
}
}
.char {
if g.config.char_fn != voidptr(0) {
g.config.char_fn(e.char_code, g.config.user_data)
}
}
.resized {
if g.config.resized_fn != voidptr(0) {
g.config.resized_fn(e, g.config.user_data)
}
}
.quit_requested {
if g.config.quit_fn != voidptr(0) {
g.config.quit_fn(e, g.config.user_data)
}
}
else {
// dump(e)
}
}
}
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)
}
gfx.shutdown()
}
fn gg_fail_fn(msg &char, user_data voidptr) {
mut g := unsafe { &Context(user_data) }
vmsg := unsafe { tos3(msg) }
if g.config.fail_fn != voidptr(0) {
g.config.fail_fn(vmsg, g.config.user_data)
} else {
eprintln('gg error: $vmsg')
}
}
pub fn (ctx &Context) run() {
sapp.run(&ctx.window)
}
// 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()
}
*/
}
// 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
}
pub fn (mut ctx Context) set_bg_color(c gx.Color) {
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)
}
// Sets a pixel
[inline]
pub fn (ctx &Context) set_pixel(x f32, y 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_points()
sgl.v2f(x * ctx.scale, y * ctx.scale)
sgl.end()
}
// Sets pixels from an array of points [x, y, x2, y2, etc...]
[direct_array_access; inline]
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)
sgl.begin_points()
for i in 0 .. len {
x, y := points[i * 2], points[i * 2 + 1]
sgl.v2f(x * ctx.scale, y * ctx.scale)
}
sgl.end()
}
// Draws a filled triangle
pub fn (ctx &Context) draw_triangle(x f32, y f32, x2 f32, y2 f32, x3 f32, y3 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_triangles()
sgl.v2f(x * ctx.scale, y * ctx.scale)
sgl.v2f(x2 * ctx.scale, y2 * ctx.scale)
sgl.v2f(x3 * ctx.scale, y3 * ctx.scale)
sgl.end()
}
// 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) {
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(x2 * ctx.scale, y2 * ctx.scale)
sgl.v2f(x3 * ctx.scale, y3 * ctx.scale)
sgl.v2f(x * ctx.scale, y * ctx.scale)
sgl.end()
}
// 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
[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)
}
// 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
pub fn (ctx &Context) draw_circle(x f32, y f32, r f32, c gx.Color) {
ctx.draw_circle_with_segments(x, y, r, 10, c)
}
// Draws a circle with a specific number of segments (affects how smooth/round the circle is)
pub fn (ctx &Context) draw_circle_with_segments(x f32, y f32, r f32, segments int, c gx.Color) {
if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
sgl.c4b(c.r, c.g, c.b, c.a)
nx := x * ctx.scale
ny := y * ctx.scale
nr := r * ctx.scale
mut theta := f32(0)
mut xx := f32(0)
mut yy := f32(0)
sgl.begin_triangle_strip()
for i := 0; i < segments + 1; i++ {
theta = 2.0 * f32(math.pi) * f32(i) / f32(segments)
xx = nr * math.cosf(theta)
yy = nr * math.sinf(theta)
sgl.v2f(xx + nx, yy + ny)
sgl.v2f(nx, ny)
}
sgl.end()
}
// 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) {
if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
sgl.c4b(c.r, c.g, c.b, c.a)
nx := x * ctx.scale
ny := y * ctx.scale
theta := f32(arc_angle / f32(segments))
tan_factor := math.tanf(theta)
rad_factor := math.cosf(theta)
mut xx := r * math.cosf(start_angle)
mut yy := r * math.sinf(start_angle)
sgl.begin_triangle_strip()
for i := 0; i < segments + 1; i++ {
sgl.v2f(xx + nx, yy + ny)
sgl.v2f(nx, ny)
tx := -yy
ty := xx
xx += tx * tan_factor
yy += ty * tan_factor
xx *= rad_factor
yy *= rad_factor
}
sgl.end()
}
// 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) {
if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
sgl.c4b(c.r, c.g, c.b, c.a)
theta := f32(arc_angle / f32(segments))
tan_factor := math.tanf(theta)
rad_factor := math.cosf(theta)
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()
for i := 0; i < segments + 1; i++ {
sgl.v2f(xx + nx, yy + ny)
tx := -yy
ty := xx
xx += tx * tan_factor
yy += ty * tan_factor
xx *= rad_factor
yy *= rad_factor
}
sgl.end()
}
// Resize the context's Window
pub fn (mut ctx Context) resize(width int, height int) {
ctx.width = width
ctx.height = height
// C.sapp_resize_window(width, height)
}
// Draws a line between the points provided
pub fn (ctx &Context) draw_line(x f32, y f32, x2 f32, y2 f32, c gx.Color) {
$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
}
}
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(x2 * ctx.scale, y2 * ctx.scale)
sgl.end()
}
// 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
}
available -= space
start_x += space
}
}
sgl.pop_matrix()
}
// 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) {
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 {
ctx.draw_slice(x, y, int(r2), a1, a2, segments, c)
return
}
mut step_length := (a2 - a1) / f32(segments)
mut angle := a1
sgl.begin_quads()
sgl.c4b(c.r, c.g, c.b, c.a)
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()
}
// 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)
sgl.begin_triangle_strip()
mut theta := f32(0)
mut xx := f32(0)
mut yy := f32(0)
r := radius * ctx.scale
nx := x * ctx.scale
ny := y * ctx.scale
width := w * ctx.scale
height := h * ctx.scale
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
mut rx := nx + width - r
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
mut rby := ny + height - r
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
mut lby := ny + height - r
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()
}
// 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) {
mut theta := f32(0)
mut xx := f32(0)
mut yy := f32(0)
r := radius * ctx.scale
nx := x * ctx.scale
ny := y * ctx.scale
width := w * ctx.scale
height := h * ctx.scale
segments := 2 * math.pi * r
segdiv := segments / 4
rb := 0
lb := int(rb + segdiv)
lt := int(lb + segdiv)
rt := int(lt + segdiv)
sgl.c4b(c.r, c.g, c.b, c.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
mut rx := nx + width - r
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
mut rby := ny + height - r
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
mut lby := ny + height - r
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()
}
// 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] * ctx.scale
y0 := points[1] * ctx.scale
for i in 1 .. (len / 2 + 1) {
sgl.v2f(x0, y0)
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)
}
if len % 2 == 0 {
sgl.v2f(points[2 * len - 2] * ctx.scale, points[2 * len - 1] * ctx.scale)
}
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] * ctx.scale, points[2 * i + 1] * ctx.scale)
}
sgl.v2f(points[0] * ctx.scale, points[1] * ctx.scale)
sgl.end()
}
// draw_cubic_bezier draws a cubic Bézier curve, also known as a spline, from four points.
// 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]`.
// Please see `draw_cubic_bezier_in_steps` to control the amount of steps (segments) used to draw the curve.
pub fn (ctx &Context) draw_cubic_bezier(points []f32, c gx.Color) {
ctx.draw_cubic_bezier_in_steps(points, u32(30 * ctx.scale), c)
}
// 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.
// 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) {
assert steps > 0
assert points.len == 8
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]
p2_x, p2_y := points[6], points[7]
ctrl_p1_x, ctrl_p1_y := points[2], points[3]
ctrl_p2_x, ctrl_p2_y := points[4], points[5]
// 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()
}
// window_size returns the `Size` of the active window
pub fn window_size() Size {
s := dpi_scale()
return Size{int(sapp.width() / s), int(sapp.height() / s)}
}
// 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()}
}
pub fn dpi_scale() f32 {
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.0
}
return s
}

228
vlib/gg/gg.js.v 100644
View File

@ -0,0 +1,228 @@
module gg
import js.dom
pub enum DOMEventType {
invalid
key_down
key_up
char
mouse_down
mouse_up
mouse_scroll
mouse_move
mouse_enter
mouse_leave
touches_began
touches_moved
touches_ended
touches_cancelled
resized
iconified
restored
focused
unfocused
suspended
resumed
update_cursor
quit_requested
clipboard_pasted
files_droped
num
}
pub struct Event {
pub mut:
frame_count u64
typ DOMEventType
key_code DOMKeyCode
char_code u32
key_repeat bool
modifiers u32
mouse_button DOMMouseButton
mouse_x f32
mouse_y f32
mouse_dx f32
mouse_dy f32
scroll_x f32
scroll_y f32
// todo(playX): add touches API support in js.dom
// num_touches int
// touches [8]C.sapp_touchpoint
window_width int
window_height int
framebuffer_width int
framebuffer_height int
}
pub struct Context {
mut:
render_text bool = true
image_cache []Image
needs_refresh bool = true
ticks int
pub mut:
scale f32 = 1.0
width int
height int
window JS.Window
config Config
user_data voidptr
ui_mode bool
frame u64
mbtn_mask byte
mouse_buttons MouseButtons
mouse_pos_x int
mouse_pos_y int
mouse_dx int
mouse_dy int
scroll_x int
scroll_y int
//
key_modifiers Modifier // the current key modifiers
key_repeat bool // whether the pressed key was an autorepeated one
pressed_keys [key_code_max]bool // an array representing all currently pressed keys
pressed_keys_edge [key_code_max]bool // true when the previous state of pressed_keys,
// *before* the current event was different
}
pub enum DOMMouseButton {
invalid = -1
left = 0
right = 1
middle = 2
}
pub enum DOMModifier {
shift = 1 //(1<<0)
ctrl = 2 //(1<<1)
alt = 4 //(1<<2)
super = 8 //(1<<3)
lmb = 0x100
rmb = 0x200
mmb = 0x400
}
pub enum DOMKeyCode {
invalid = 0
space = 32
apostrophe = 39 //'
comma = 44 //,
minus = 45 //-
period = 46 //.
slash = 47 ///
_0 = 48
_1 = 49
_2 = 50
_3 = 51
_4 = 52
_5 = 53
_6 = 54
_7 = 55
_8 = 56
_9 = 57
semicolon = 59 //;
equal = 61 //=
a = 65
b = 66
c = 67
d = 68
e = 69
f = 70
g = 71
h = 72
i = 73
j = 74
k = 75
l = 76
m = 77
n = 78
o = 79
p = 80
q = 81
r = 82
s = 83
t = 84
u = 85
v = 86
w = 87
x = 88
y = 89
z = 90
left_bracket = 91 //[
backslash = 92 //\
right_bracket = 93 //]
grave_accent = 96 //`
world_1 = 161 // non-us #1
world_2 = 162 // non-us #2
escape = 256
enter = 257
tab = 258
backspace = 259
insert = 260
delete = 261
right = 262
left = 263
down = 264
up = 265
page_up = 266
page_down = 267
home = 268
end = 269
caps_lock = 280
scroll_lock = 281
num_lock = 282
print_screen = 283
pause = 284
f1 = 290
f2 = 291
f3 = 292
f4 = 293
f5 = 294
f6 = 295
f7 = 296
f8 = 297
f9 = 298
f10 = 299
f11 = 300
f12 = 301
f13 = 302
f14 = 303
f15 = 304
f16 = 305
f17 = 306
f18 = 307
f19 = 308
f20 = 309
f21 = 310
f22 = 311
f23 = 312
f24 = 313
f25 = 314
kp_0 = 320
kp_1 = 321
kp_2 = 322
kp_3 = 323
kp_4 = 324
kp_5 = 325
kp_6 = 326
kp_7 = 327
kp_8 = 328
kp_9 = 329
kp_decimal = 330
kp_divide = 331
kp_multiply = 332
kp_subtract = 333
kp_add = 334
kp_enter = 335
kp_equal = 336
left_shift = 340
left_control = 341
left_alt = 342
left_super = 343
right_shift = 344
right_control = 345
right_alt = 346
right_super = 347
menu = 348
}

View File

@ -4,10 +4,6 @@
module gg
import gx
import sokol.sapp
import sokol.sgl
import sokol.gfx
import math
pub type FNCb = fn (data voidptr)
@ -95,746 +91,3 @@ pub:
width int
height int
}
fn gg_frame_fn(user_data voidptr) {
mut ctx := unsafe { &Context(user_data) }
ctx.frame++
if ctx.config.frame_fn == voidptr(0) {
return
}
if ctx.native_rendering {
// return
}
ctx.record_frame()
if ctx.ui_mode && !ctx.needs_refresh {
// Draw 3 more frames after the "stop refresh" command
ctx.ticks++
if ctx.ticks > 3 {
return
}
}
ctx.config.frame_fn(ctx.user_data)
ctx.needs_refresh = false
}
pub fn (mut ctx Context) refresh_ui() {
ctx.needs_refresh = true
ctx.ticks = 0
}
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()
}
if e.typ == .mouse_down {
bitplace := int(e.mouse_button)
g.mbtn_mask |= byte(1 << bitplace)
g.mouse_buttons = MouseButtons(g.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)
}
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
}
}
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
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
}
if g.config.event_fn != voidptr(0) {
g.config.event_fn(e, g.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)
}
}
.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)
}
}
.key_down {
if g.config.keydown_fn != voidptr(0) {
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)
}
}
.char {
if g.config.char_fn != voidptr(0) {
g.config.char_fn(e.char_code, g.config.user_data)
}
}
.resized {
if g.config.resized_fn != voidptr(0) {
g.config.resized_fn(e, g.config.user_data)
}
}
.quit_requested {
if g.config.quit_fn != voidptr(0) {
g.config.quit_fn(e, g.config.user_data)
}
}
else {
// dump(e)
}
}
}
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)
}
gfx.shutdown()
}
fn gg_fail_fn(msg &char, user_data voidptr) {
mut g := unsafe { &Context(user_data) }
vmsg := unsafe { tos3(msg) }
if g.config.fail_fn != voidptr(0) {
g.config.fail_fn(vmsg, g.config.user_data)
} else {
eprintln('gg error: $vmsg')
}
}
pub fn (ctx &Context) run() {
sapp.run(&ctx.window)
}
// 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()
}
*/
}
// 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
}
pub fn (mut ctx Context) set_bg_color(c gx.Color) {
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)
}
// Sets a pixel
[inline]
pub fn (ctx &Context) set_pixel(x f32, y 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_points()
sgl.v2f(x * ctx.scale, y * ctx.scale)
sgl.end()
}
// Sets pixels from an array of points [x, y, x2, y2, etc...]
[direct_array_access; inline]
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)
sgl.begin_points()
for i in 0 .. len {
x, y := points[i * 2], points[i * 2 + 1]
sgl.v2f(x * ctx.scale, y * ctx.scale)
}
sgl.end()
}
// Draws a filled triangle
pub fn (ctx &Context) draw_triangle(x f32, y f32, x2 f32, y2 f32, x3 f32, y3 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_triangles()
sgl.v2f(x * ctx.scale, y * ctx.scale)
sgl.v2f(x2 * ctx.scale, y2 * ctx.scale)
sgl.v2f(x3 * ctx.scale, y3 * ctx.scale)
sgl.end()
}
// 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) {
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(x2 * ctx.scale, y2 * ctx.scale)
sgl.v2f(x3 * ctx.scale, y3 * ctx.scale)
sgl.v2f(x * ctx.scale, y * ctx.scale)
sgl.end()
}
// 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
[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)
}
// 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
pub fn (ctx &Context) draw_circle(x f32, y f32, r f32, c gx.Color) {
ctx.draw_circle_with_segments(x, y, r, 10, c)
}
// Draws a circle with a specific number of segments (affects how smooth/round the circle is)
pub fn (ctx &Context) draw_circle_with_segments(x f32, y f32, r f32, segments int, c gx.Color) {
if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
sgl.c4b(c.r, c.g, c.b, c.a)
nx := x * ctx.scale
ny := y * ctx.scale
nr := r * ctx.scale
mut theta := f32(0)
mut xx := f32(0)
mut yy := f32(0)
sgl.begin_triangle_strip()
for i := 0; i < segments + 1; i++ {
theta = 2.0 * f32(math.pi) * f32(i) / f32(segments)
xx = nr * math.cosf(theta)
yy = nr * math.sinf(theta)
sgl.v2f(xx + nx, yy + ny)
sgl.v2f(nx, ny)
}
sgl.end()
}
// 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) {
if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
sgl.c4b(c.r, c.g, c.b, c.a)
nx := x * ctx.scale
ny := y * ctx.scale
theta := f32(arc_angle / f32(segments))
tan_factor := math.tanf(theta)
rad_factor := math.cosf(theta)
mut xx := r * math.cosf(start_angle)
mut yy := r * math.sinf(start_angle)
sgl.begin_triangle_strip()
for i := 0; i < segments + 1; i++ {
sgl.v2f(xx + nx, yy + ny)
sgl.v2f(nx, ny)
tx := -yy
ty := xx
xx += tx * tan_factor
yy += ty * tan_factor
xx *= rad_factor
yy *= rad_factor
}
sgl.end()
}
// 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) {
if c.a != 255 {
sgl.load_pipeline(ctx.timage_pip)
}
sgl.c4b(c.r, c.g, c.b, c.a)
theta := f32(arc_angle / f32(segments))
tan_factor := math.tanf(theta)
rad_factor := math.cosf(theta)
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()
for i := 0; i < segments + 1; i++ {
sgl.v2f(xx + nx, yy + ny)
tx := -yy
ty := xx
xx += tx * tan_factor
yy += ty * tan_factor
xx *= rad_factor
yy *= rad_factor
}
sgl.end()
}
// Resize the context's Window
pub fn (mut ctx Context) resize(width int, height int) {
ctx.width = width
ctx.height = height
// C.sapp_resize_window(width, height)
}
// Draws a line between the points provided
pub fn (ctx &Context) draw_line(x f32, y f32, x2 f32, y2 f32, c gx.Color) {
$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
}
}
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(x2 * ctx.scale, y2 * ctx.scale)
sgl.end()
}
// 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
}
available -= space
start_x += space
}
}
sgl.pop_matrix()
}
// 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) {
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 {
ctx.draw_slice(x, y, int(r2), a1, a2, segments, c)
return
}
mut step_length := (a2 - a1) / f32(segments)
mut angle := a1
sgl.begin_quads()
sgl.c4b(c.r, c.g, c.b, c.a)
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()
}
// 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)
sgl.begin_triangle_strip()
mut theta := f32(0)
mut xx := f32(0)
mut yy := f32(0)
r := radius * ctx.scale
nx := x * ctx.scale
ny := y * ctx.scale
width := w * ctx.scale
height := h * ctx.scale
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
mut rx := nx + width - r
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
mut rby := ny + height - r
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
mut lby := ny + height - r
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()
}
// 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) {
mut theta := f32(0)
mut xx := f32(0)
mut yy := f32(0)
r := radius * ctx.scale
nx := x * ctx.scale
ny := y * ctx.scale
width := w * ctx.scale
height := h * ctx.scale
segments := 2 * math.pi * r
segdiv := segments / 4
rb := 0
lb := int(rb + segdiv)
lt := int(lb + segdiv)
rt := int(lt + segdiv)
sgl.c4b(c.r, c.g, c.b, c.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
mut rx := nx + width - r
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
mut rby := ny + height - r
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
mut lby := ny + height - r
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()
}
// 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] * ctx.scale
y0 := points[1] * ctx.scale
for i in 1 .. (len / 2 + 1) {
sgl.v2f(x0, y0)
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)
}
if len % 2 == 0 {
sgl.v2f(points[2 * len - 2] * ctx.scale, points[2 * len - 1] * ctx.scale)
}
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] * ctx.scale, points[2 * i + 1] * ctx.scale)
}
sgl.v2f(points[0] * ctx.scale, points[1] * ctx.scale)
sgl.end()
}
// draw_cubic_bezier draws a cubic Bézier curve, also known as a spline, from four points.
// 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]`.
// Please see `draw_cubic_bezier_in_steps` to control the amount of steps (segments) used to draw the curve.
pub fn (ctx &Context) draw_cubic_bezier(points []f32, c gx.Color) {
ctx.draw_cubic_bezier_in_steps(points, u32(30 * ctx.scale), c)
}
// 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.
// 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) {
assert steps > 0
assert points.len == 8
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]
p2_x, p2_y := points[6], points[7]
ctrl_p1_x, ctrl_p1_y := points[2], points[3]
ctrl_p2_x, ctrl_p2_y := points[4], points[5]
// 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()
}
// window_size returns the `Size` of the active window
pub fn window_size() Size {
s := dpi_scale()
return Size{int(sapp.width() / s), int(sapp.height() / s)}
}
// 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()}
}
pub fn dpi_scale() f32 {
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.0
}
return s
}

View File

@ -5,6 +5,22 @@ module gg
import os
import stbi
import sokol.gfx
import sokol.sgl
[heap]
pub struct Image {
pub mut:
id int
width int
height int
nr_channels int
ok bool
data voidptr
ext string
simg_ok bool
simg C.sg_image
path string
}
// TODO return ?Image
pub fn (mut ctx Context) create_image(file string) Image {
@ -165,3 +181,126 @@ pub fn (mut ctx Context) create_image_with_size(file string, width int, height i
ctx.image_cache << img
return img
}
// TODO remove this
fn create_image(file string) Image {
if !os.exists(file) {
println('gg.create_image(): file not found: $file')
return Image{} // none
}
stb_img := stbi.load(file) or { return Image{} }
mut img := Image{
width: stb_img.width
height: stb_img.height
nr_channels: stb_img.nr_channels
ok: stb_img.ok
data: stb_img.data
ext: stb_img.ext
path: file
}
img.init_sokol_image()
return img
}
pub fn (mut ctx Context) create_image_from_memory(buf &byte, bufsize int) Image {
stb_img := stbi.load_from_memory(buf, bufsize) or { return Image{} }
mut img := Image{
width: stb_img.width
height: stb_img.height
nr_channels: stb_img.nr_channels
ok: stb_img.ok
data: stb_img.data
ext: stb_img.ext
id: ctx.image_cache.len
}
ctx.image_cache << img
return img
}
pub fn (mut ctx Context) create_image_from_byte_array(b []byte) Image {
return ctx.create_image_from_memory(b.data, b.len)
}
pub struct StreamingImageConfig {
pixel_format gfx.PixelFormat = .rgba8
wrap_u gfx.Wrap = .clamp_to_edge
wrap_v gfx.Wrap = .clamp_to_edge
min_filter gfx.Filter = .linear
mag_filter gfx.Filter = .linear
num_mipmaps int = 1
num_slices int = 1
}
// draw_image_with_config takes in a config that details how the
// provided image should be drawn onto the screen
pub fn (ctx &Context) draw_image_with_config(config DrawImageConfig) {
id := if !isnil(config.img) { config.img.id } else { config.img_id }
if id >= ctx.image_cache.len {
eprintln('gg: draw_image() bad img id $id (img cache len = $ctx.image_cache.len)')
return
}
img := &ctx.image_cache[id]
if !img.simg_ok {
return
}
mut img_rect := config.img_rect
if img_rect.width == 0 && img_rect.height == 0 {
img_rect = Rect{img_rect.x, img_rect.y, img.width, img.height}
}
mut part_rect := config.part_rect
if part_rect.width == 0 && part_rect.height == 0 {
part_rect = Rect{part_rect.x, part_rect.y, img.width, img.height}
}
u0 := part_rect.x / img.width
v0 := part_rect.y / img.height
u1 := (part_rect.x + part_rect.width) / img.width
v1 := (part_rect.y + part_rect.height) / img.height
x0 := img_rect.x * ctx.scale
y0 := img_rect.y * ctx.scale
x1 := (img_rect.x + img_rect.width) * ctx.scale
mut y1 := (img_rect.y + img_rect.height) * ctx.scale
if img_rect.height == 0 {
scale := f32(img.width) / f32(img_rect.width)
y1 = f32(img_rect.y + int(f32(img.height) / scale)) * ctx.scale
}
flip_x := config.flip_x
flip_y := config.flip_y
mut u0f := if !flip_x { u0 } else { u1 }
mut u1f := if !flip_x { u1 } else { u0 }
mut v0f := if !flip_y { v0 } else { v1 }
mut v1f := if !flip_y { v1 } else { v0 }
sgl.load_pipeline(ctx.timage_pip)
sgl.enable_texture()
sgl.texture(img.simg)
if config.rotate != 0 {
width := img_rect.width * ctx.scale
height := (if img_rect.height > 0 { img_rect.height } else { img.height }) * ctx.scale
sgl.push_matrix()
sgl.translate(x0 + (width / 2), y0 + (height / 2), 0)
sgl.rotate(sgl.rad(-config.rotate), 0, 0, 1)
sgl.translate(-x0 - (width / 2), -y0 - (height / 2), 0)
}
sgl.begin_quads()
sgl.c4b(config.color.r, config.color.g, config.color.b, config.color.a)
sgl.v3f_t2f(x0, y0, config.z, u0f, v0f)
sgl.v3f_t2f(x1, y0, config.z, u1f, v0f)
sgl.v3f_t2f(x1, y1, config.z, u1f, v1f)
sgl.v3f_t2f(x0, y1, config.z, u0f, v1f)
sgl.end()
if config.rotate != 0 {
sgl.pop_matrix()
}
sgl.disable_texture()
}

18
vlib/gg/image.js.v 100644
View File

@ -0,0 +1,18 @@
module gg
[heap]
pub struct Image {
pub mut:
id int
width int
height int
nr_channels int
ok bool
data voidptr
ext string
path string
}
pub fn (ctx &Context) draw_image_with_config(config DrawImageConfig) {
}

View File

@ -2,28 +2,7 @@
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module gg
// import sokol.sapp
import gx
import sokol.gfx
import os
import sokol
import sokol.sgl
import stbi
[heap]
pub struct Image {
pub mut:
id int
width int
height int
nr_channels int
ok bool
data voidptr
ext string
simg_ok bool
simg C.sg_image
path string
}
// DrawImageConfig struct defines the various options
// that can be used to draw an image onto the screen
@ -48,45 +27,6 @@ pub:
height f32
}
// TODO remove this
fn create_image(file string) Image {
if !os.exists(file) {
println('gg.create_image(): file not found: $file')
return Image{} // none
}
stb_img := stbi.load(file) or { return Image{} }
mut img := Image{
width: stb_img.width
height: stb_img.height
nr_channels: stb_img.nr_channels
ok: stb_img.ok
data: stb_img.data
ext: stb_img.ext
path: file
}
img.init_sokol_image()
return img
}
pub fn (mut ctx Context) create_image_from_memory(buf &byte, bufsize int) Image {
stb_img := stbi.load_from_memory(buf, bufsize) or { return Image{} }
mut img := Image{
width: stb_img.width
height: stb_img.height
nr_channels: stb_img.nr_channels
ok: stb_img.ok
data: stb_img.data
ext: stb_img.ext
id: ctx.image_cache.len
}
ctx.image_cache << img
return img
}
pub fn (mut ctx Context) create_image_from_byte_array(b []byte) Image {
return ctx.create_image_from_memory(b.data, b.len)
}
pub fn (mut ctx Context) cache_image(img Image) int {
ctx.image_cache << img
image_idx := ctx.image_cache.len - 1
@ -98,90 +38,6 @@ pub fn (mut ctx Context) get_cached_image_by_idx(image_idx int) &Image {
return &ctx.image_cache[image_idx]
}
pub struct StreamingImageConfig {
pixel_format gfx.PixelFormat = .rgba8
wrap_u gfx.Wrap = .clamp_to_edge
wrap_v gfx.Wrap = .clamp_to_edge
min_filter gfx.Filter = .linear
mag_filter gfx.Filter = .linear
num_mipmaps int = 1
num_slices int = 1
}
// draw_image_with_config takes in a config that details how the
// provided image should be drawn onto the screen
pub fn (ctx &Context) draw_image_with_config(config DrawImageConfig) {
id := if !isnil(config.img) { config.img.id } else { config.img_id }
if id >= ctx.image_cache.len {
eprintln('gg: draw_image() bad img id $id (img cache len = $ctx.image_cache.len)')
return
}
img := &ctx.image_cache[id]
if !img.simg_ok {
return
}
mut img_rect := config.img_rect
if img_rect.width == 0 && img_rect.height == 0 {
img_rect = Rect{img_rect.x, img_rect.y, img.width, img.height}
}
mut part_rect := config.part_rect
if part_rect.width == 0 && part_rect.height == 0 {
part_rect = Rect{part_rect.x, part_rect.y, img.width, img.height}
}
u0 := part_rect.x / img.width
v0 := part_rect.y / img.height
u1 := (part_rect.x + part_rect.width) / img.width
v1 := (part_rect.y + part_rect.height) / img.height
x0 := img_rect.x * ctx.scale
y0 := img_rect.y * ctx.scale
x1 := (img_rect.x + img_rect.width) * ctx.scale
mut y1 := (img_rect.y + img_rect.height) * ctx.scale
if img_rect.height == 0 {
scale := f32(img.width) / f32(img_rect.width)
y1 = f32(img_rect.y + int(f32(img.height) / scale)) * ctx.scale
}
flip_x := config.flip_x
flip_y := config.flip_y
mut u0f := if !flip_x { u0 } else { u1 }
mut u1f := if !flip_x { u1 } else { u0 }
mut v0f := if !flip_y { v0 } else { v1 }
mut v1f := if !flip_y { v1 } else { v0 }
sgl.load_pipeline(ctx.timage_pip)
sgl.enable_texture()
sgl.texture(img.simg)
if config.rotate != 0 {
width := img_rect.width * ctx.scale
height := (if img_rect.height > 0 { img_rect.height } else { img.height }) * ctx.scale
sgl.push_matrix()
sgl.translate(x0 + (width / 2), y0 + (height / 2), 0)
sgl.rotate(sgl.rad(-config.rotate), 0, 0, 1)
sgl.translate(-x0 - (width / 2), -y0 - (height / 2), 0)
}
sgl.begin_quads()
sgl.c4b(config.color.r, config.color.g, config.color.b, config.color.a)
sgl.v3f_t2f(x0, y0, config.z, u0f, v0f)
sgl.v3f_t2f(x1, y0, config.z, u1f, v0f)
sgl.v3f_t2f(x1, y1, config.z, u1f, v1f)
sgl.v3f_t2f(x0, y1, config.z, u0f, v1f)
sgl.end()
if config.rotate != 0 {
sgl.pop_matrix()
}
sgl.disable_texture()
}
// Draw part of an image using uv coordinates
// img_rect is the size and position (in pixels on screen) of the displayed rectangle (ie the draw_image args)
// part_rect is the size and position (in absolute pixels in the image) of the wanted part

View File

@ -0,0 +1,41 @@
module gg
import sokol.sapp
import os
[if gg_record ?]
pub fn (mut ctx Context) record_frame() {
if ctx.frame in gg.recorder_settings.screenshot_frames {
screenshot_file_path := '$gg.recorder_settings.screenshot_prefix${ctx.frame}.png'
$if gg_record_trace ? {
eprintln('>>> screenshoting $screenshot_file_path')
}
sapp.screenshot_png(screenshot_file_path) or { panic(err) }
}
if ctx.frame == gg.recorder_settings.stop_at_frame {
$if gg_record_trace ? {
eprintln('>>> exiting at frame $ctx.frame')
}
exit(0)
}
}
fn new_gg_recorder_settings() &SSRecorderSettings {
$if gg_record ? {
stop_frame := os.getenv_opt('VGG_STOP_AT_FRAME') or { '-1' }.i64()
frames := os.getenv('VGG_SCREENSHOT_FRAMES').split_any(',').map(it.u64())
folder := os.getenv('VGG_SCREENSHOT_FOLDER')
prefix := os.join_path_single(folder, os.file_name(os.executable()).all_before('.') + '_')
return &SSRecorderSettings{
stop_at_frame: stop_frame
screenshot_frames: frames
screenshot_folder: folder
screenshot_prefix: prefix
}
} $else {
return &SSRecorderSettings{}
}
}
const recorder_settings = new_gg_recorder_settings()

View File

@ -0,0 +1,4 @@
module gg
[if gg_record ?]
pub fn (mut ctx Context) record_frame() {}

View File

@ -1,8 +1,5 @@
module gg
import os
import sokol.sapp
[heap]
pub struct SSRecorderSettings {
pub mut:
@ -11,39 +8,3 @@ pub mut:
screenshot_folder string
screenshot_prefix string
}
const recorder_settings = new_gg_recorder_settings()
fn new_gg_recorder_settings() &SSRecorderSettings {
$if gg_record ? {
stop_frame := os.getenv_opt('VGG_STOP_AT_FRAME') or { '-1' }.i64()
frames := os.getenv('VGG_SCREENSHOT_FRAMES').split_any(',').map(it.u64())
folder := os.getenv('VGG_SCREENSHOT_FOLDER')
prefix := os.join_path_single(folder, os.file_name(os.executable()).all_before('.') + '_')
return &SSRecorderSettings{
stop_at_frame: stop_frame
screenshot_frames: frames
screenshot_folder: folder
screenshot_prefix: prefix
}
} $else {
return &SSRecorderSettings{}
}
}
[if gg_record ?]
pub fn (mut ctx Context) record_frame() {
if ctx.frame in gg.recorder_settings.screenshot_frames {
screenshot_file_path := '$gg.recorder_settings.screenshot_prefix${ctx.frame}.png'
$if gg_record_trace ? {
eprintln('>>> screenshoting $screenshot_file_path')
}
sapp.screenshot_png(screenshot_file_path) or { panic(err) }
}
if ctx.frame == gg.recorder_settings.stop_at_frame {
$if gg_record_trace ? {
eprintln('>>> exiting at frame $ctx.frame')
}
exit(0)
}
}