1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

ui: fixes so that the ui demo works on linux too

* Implement os.resource_abs_path/1 .
* Implement glfw.get_window_size and glfw.get_framebuffer_size .
This commit is contained in:
Delyan Angelov 2020-01-12 20:35:06 +02:00 committed by Alexander Medvednikov
parent 607656d616
commit f317b65808
3 changed files with 65 additions and 20 deletions

View File

@ -101,7 +101,7 @@ pub fn init_glfw() {
C.glfwWindowHint(C.GLFW_OPENGL_PROFILE, C.GLFW_OPENGL_CORE_PROFILE)
}
pub fn (w &Window) destroy() {
pub fn (w &glfw.Window) destroy() {
C.glfwDestroyWindow(w.data)
}
@ -118,7 +118,7 @@ pub fn window_hint(key, val int) {
C.glfwWindowHint(key, val)
}
pub fn create_window(c WinCfg) &Window {
pub fn create_window(c WinCfg) &glfw.Window {
if c.borderless {
window_hint(C.GLFW_RESIZABLE, 0)
window_hint(C.GLFW_DECORATED, 0)
@ -133,18 +133,18 @@ pub fn create_window(c WinCfg) &Window {
}
println('create window wnd=$cwindow ptr==$c.ptr')
C.glfwSetWindowUserPointer(cwindow, c.ptr)
window := &Window {
window := &glfw.Window {
data: cwindow,
title: c.title,
}
return window
}
pub fn (w &Window) set_title(title string) {
pub fn (w &glfw.Window) set_title(title string) {
C.glfwSetWindowTitle(w.data, title.str)
}
pub fn (w &Window) make_context_current() {
pub fn (w &glfw.Window) make_context_current() {
C.glfwMakeContextCurrent(w.data)
}
@ -164,31 +164,31 @@ pub fn set_should_close(w voidptr, close bool) {
C.glfwSetWindowShouldClose(w, close)
}
pub fn (w &Window) should_close() bool {
pub fn (w &glfw.Window) should_close() bool {
return C.glfwWindowShouldClose(w.data)
}
pub fn (w &Window) swap_buffers() {
pub fn (w &glfw.Window) swap_buffers() {
C.glfwSwapBuffers(w.data)
}
pub fn (w mut Window) onmousemove(cb voidptr) {
pub fn (w mut glfw.Window) onmousemove(cb voidptr) {
C.glfwSetCursorPosCallback(w.data, cb)
}
pub fn (w mut Window) set_mouse_button_callback(cb voidptr) {
pub fn (w mut glfw.Window) set_mouse_button_callback(cb voidptr) {
C.glfwSetMouseButtonCallback(w.data, cb)
}
pub fn (w mut Window) on_click(cb voidptr) {
pub fn (w mut glfw.Window) on_click(cb voidptr) {
C.glfwSetMouseButtonCallback(w.data, cb)
}
pub fn (w &Window) set_scroll_callback(cb voidptr) {
pub fn (w &glfw.Window) set_scroll_callback(cb voidptr) {
C.glfwSetScrollCallback(w.data, cb)
}
pub fn (w &Window) on_scroll(cb voidptr) {
pub fn (w &glfw.Window) on_scroll(cb voidptr) {
C.glfwSetScrollCallback(w.data, cb)
}
@ -196,11 +196,11 @@ pub fn post_empty_event() {
C.glfwPostEmptyEvent()
}
pub fn (w mut Window) onkeydown(cb voidptr) {
pub fn (w mut glfw.Window) onkeydown(cb voidptr) {
C.glfwSetKeyCallback(w.data, cb)
}
pub fn (w mut Window) onchar(cb voidptr) {
pub fn (w mut glfw.Window) onchar(cb voidptr) {
C.glfwSetCharModsCallback(w.data, cb)
}
@ -212,11 +212,11 @@ pub fn key_pressed(wnd voidptr, key int) bool {
return int(C.glfwGetKey(wnd, key)) == C.GLFW_PRESS
}
pub fn (w &Window) get_clipboard_text() string {
pub fn (w &glfw.Window) get_clipboard_text() string {
return string(byteptr(C.glfwGetClipboardString(w.data)))
}
pub fn (w &Window) set_clipboard_text(s string) {
pub fn (w &glfw.Window) set_clipboard_text(s string) {
C.glfwSetClipboardString(w.data, s.str)
}
@ -227,7 +227,7 @@ pub fn get_cursor_pos(glfw_window voidptr) (f64, f64) {
return x,y
}
pub fn (w &Window) get_cursor_pos() Pos {
pub fn (w &glfw.Window) get_cursor_pos() Pos {
x := f64(0)
y := f64(0)
C.glfwGetCursorPos(w.data, &x, &y)
@ -247,16 +247,16 @@ pub fn set_cursor(c Cursor) {
C.glfwSetCursor(0, C.GLFW_IBEAM_CURSOR)
}
pub fn (w &Window) set_cursor(c Cursor) {
pub fn (w &glfw.Window) set_cursor(c Cursor) {
C.glfwSetCursor(w.data, C.GLFW_IBEAM_CURSOR)
}
pub fn (w &Window) user_ptr() voidptr {
pub fn (w &glfw.Window) user_ptr() voidptr {
return C.glfwGetWindowUserPointer(w.data)
}
pub fn (w &Window) set_user_ptr(ptr voidptr) {
pub fn (w &glfw.Window) set_user_ptr(ptr voidptr) {
C.glfwSetWindowUserPointer(w.data, ptr)
}
@ -273,6 +273,23 @@ pub fn get_monitor_size() Size {
return Size{mode.width, mode.height}
}
fn C.glfwGetWindowSize(window &glfw.Window, width &int, height &int) // screen coordinates
fn C.glfwGetFramebufferSize(window &glfw.Window, width &int, height &int) // pixels
// get_window_size in screen coordinates
pub fn (w &glfw.Window) get_window_size() Size {
res := Size{ 0, 0 }
C.glfwGetWindowSize( w.data, &res.width, &res.height )
return res
}
// get_framebuffer_size in pixels
pub fn (w &glfw.Window) get_framebuffer_size() Size {
res := Size{ 0, 0 }
C.glfwGetFramebufferSize( w.data, &res.width, &res.height )
return res
}
pub fn (size Size) str() string {
return '{$size.width, $size.height}'
}

View File

@ -1115,3 +1115,17 @@ pub fn chmod(path string, mode int) {
pub const (
wd_at_startup = getwd()
)
// resource_abs_path returns an absolute path, for the given `path`
// (the path is expected to be relative to the executable program)
// See https://discordapp.com/channels/592103645835821068/592294828432424960/630806741373943808
// It gives a convenient way to access program resources like images, fonts, sounds and so on,
// *no matter* how the program was started, and what is the current working directory.
pub fn resource_abs_path(path string) string {
mut base_path := os.realpath(filepath.dir(os.executable()))
vresource := os.getenv('V_RESOURCE_PATH')
if vresource.len != 0 {
base_path = vresource
}
return os.realpath( filepath.join( base_path, path ) )
}

View File

@ -71,6 +71,7 @@ pub fn is_dir(path string) bool {
pub fn open(path string) ?File {
mut file := File{}
/*
$if linux {
$if !android {
fd := C.syscall(sys_open, path.str, 511)
@ -83,6 +84,7 @@ pub fn open(path string) ?File {
}
}
}
*/
cpath := path.str
file = File{
cfile: C.fopen(charptr(cpath), 'rb')
@ -98,6 +100,7 @@ pub fn open(path string) ?File {
pub fn create(path string) ?File {
mut fd := 0
mut file := File{}
/*
// NB: android/termux/bionic is also a kind of linux,
// but linux syscalls there sometimes fail,
// while the libc version should work.
@ -119,6 +122,7 @@ pub fn create(path string) ?File {
return file
}
}
*/
file = File{
cfile: C.fopen(charptr(path.str), 'wb')
opened: true
@ -139,12 +143,14 @@ pub fn (f mut File) write(s string) {
if !f.opened {
return
}
/*
$if linux {
$if !android {
C.syscall(sys_write, f.fd, s.str, s.len)
return
}
}
*/
C.fputs(s.str, f.cfile)
// C.fwrite(s.str, 1, s.len, f.cfile)
}
@ -153,6 +159,7 @@ pub fn (f mut File) writeln(s string) {
if !f.opened {
return
}
/*
$if linux {
$if !android {
snl := s + '\n'
@ -160,6 +167,7 @@ pub fn (f mut File) writeln(s string) {
return
}
}
*/
// C.fwrite(s.str, 1, s.len, f.cfile)
// ss := s.clone()
// TODO perf
@ -174,6 +182,7 @@ pub fn mkdir(path string) ?bool {
return true
}
apath := os.realpath(path)
/*
$if linux {
$if !android {
ret := C.syscall(sys_mkdir, apath.str, 511)
@ -183,6 +192,7 @@ pub fn mkdir(path string) ?bool {
return true
}
}
*/
r := C.mkdir(apath.str, 511)
if r == -1 {
return error(get_error_msg(C.errno))
@ -228,12 +238,14 @@ pub fn symlink(origin, target string) ?bool {
// for example if we have write(7, 4), "07 00 00 00" gets written
// write(0x1234, 2) => "34 12"
pub fn (f mut File) write_bytes(data voidptr, size int) {
/*
$if linux {
$if !android {
C.syscall(sys_write, f.fd, data, 1)
return
}
}
*/
C.fwrite(data, 1, size, f.cfile)
}
@ -242,12 +254,14 @@ pub fn (f mut File) close() {
return
}
f.opened = false
/*
$if linux {
$if !android {
C.syscall(sys_close, f.fd)
return
}
}
*/
C.fflush(f.cfile)
C.fclose(f.cfile)
}