mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
js,gg: more work on porting gg to JS backend (#12903)
This commit is contained in:
181
vlib/gg/gg.js.v
181
vlib/gg/gg.js.v
@ -1,5 +1,6 @@
|
||||
module gg
|
||||
|
||||
import gx
|
||||
import js.dom
|
||||
|
||||
pub enum DOMEventType {
|
||||
@ -55,37 +56,6 @@ pub mut:
|
||||
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
|
||||
@ -226,3 +196,152 @@ pub enum DOMKeyCode {
|
||||
right_super = 347
|
||||
menu = 348
|
||||
}
|
||||
|
||||
pub struct Config {
|
||||
pub:
|
||||
width int
|
||||
height int
|
||||
use_ortho bool // unused, still here just for backwards compatibility
|
||||
retina bool
|
||||
resizable bool
|
||||
user_data voidptr
|
||||
font_size int
|
||||
create_window bool
|
||||
// window_user_ptr voidptr
|
||||
window_title string
|
||||
borderless_window bool
|
||||
always_on_top bool
|
||||
bg_color gx.Color
|
||||
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)
|
||||
// wait_events bool // set this to true for UIs, to save power
|
||||
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)
|
||||
// ved needs this
|
||||
// init_text bool
|
||||
font_path string
|
||||
custom_bold_font_path string
|
||||
ui_mode bool // refreshes only on events to save CPU usage
|
||||
// font bytes for embedding
|
||||
font_bytes_normal []byte
|
||||
font_bytes_bold []byte
|
||||
font_bytes_mono []byte
|
||||
font_bytes_italic []byte
|
||||
native_rendering bool // Cocoa on macOS/iOS, GDI+ on Windows
|
||||
// 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)
|
||||
context JS.CanvasRenderingContext2D
|
||||
}
|
||||
|
||||
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 [noinit]
|
||||
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,
|
||||
canvas JS.CanvasRenderingContext2D [noinit]
|
||||
// *before* the current event was different
|
||||
}
|
||||
|
||||
pub fn new_context(cfg Config) &Context {
|
||||
mut g := &Context{}
|
||||
g.user_data = cfg.user_data
|
||||
g.width = cfg.width
|
||||
g.height = cfg.height
|
||||
g.ui_mode = cfg.ui_mode
|
||||
g.config = cfg
|
||||
if isnil(cfg.user_data) {
|
||||
g.user_data = g
|
||||
}
|
||||
g.window = dom.window()
|
||||
g.canvas = cfg.context
|
||||
|
||||
return g
|
||||
}
|
||||
|
||||
pub fn (mut ctx Context) run() {
|
||||
gg_animation_frame_fn(mut ctx)
|
||||
}
|
||||
|
||||
pub fn (mut ctx Context) begin() {
|
||||
// ctx.canvas.beginPath()
|
||||
}
|
||||
|
||||
pub fn (mut ctx Context) end() {
|
||||
// ctx.canvas.closePath()
|
||||
}
|
||||
|
||||
pub fn (mut ctx Context) draw_line(x1 f32, y1 f32, x2 f32, y2 f32, c gx.Color) {
|
||||
ctx.canvas.beginPath()
|
||||
ctx.canvas.strokeStyle = c.to_css_string().str
|
||||
ctx.canvas.moveTo(x1, y1)
|
||||
ctx.canvas.lineTo(x2, y2)
|
||||
ctx.canvas.stroke()
|
||||
ctx.canvas.closePath()
|
||||
}
|
||||
|
||||
pub fn (mut ctx Context) draw_rect(x f32, y f32, w f32, h f32, c gx.Color) {
|
||||
ctx.canvas.beginPath()
|
||||
ctx.canvas.fillStyle = c.to_css_string().str
|
||||
ctx.canvas.fillRect(x, y, w, h)
|
||||
ctx.canvas.closePath()
|
||||
}
|
||||
|
||||
fn gg_animation_frame_fn(mut g Context) {
|
||||
g.frame++
|
||||
g.canvas.clearRect(0, 0, g.config.width, g.config.height)
|
||||
// todo(playXE): handle events
|
||||
if !isnil(g.config.frame_fn) {
|
||||
f := g.config.frame_fn
|
||||
f(g.user_data)
|
||||
g.needs_refresh = false
|
||||
}
|
||||
|
||||
g.window.requestAnimationFrame(fn [mut g] (time JS.Number) {
|
||||
gg_animation_frame_fn(mut g)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user