Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glfw: export Drop callback #145

Merged
merged 6 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package imgui
// extern void afterRender();
// extern void afterCreateContext();
// extern void beforeDestoryContext();
// extern void dropCallback(void*, int, char**);
import "C"

import (
Expand Down Expand Up @@ -59,6 +60,20 @@ func beforeDestoryContext() {
}
}

type DropCallback func([]string)

//export dropCallback
func dropCallback(wnd unsafe.Pointer, count C.int, names **C.char) {
namesSlice := make([]string, int(count))
for i := 0; i < int(count); i++ {
var x *C.char
p := (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(names)) + uintptr(i)*unsafe.Sizeof(x)))
namesSlice[i] = C.GoString(*p)
}

currentBackend.dropCallback()(namesSlice)
}

// Backend is a special interface that implements all methods required
// to render imgui application.
type Backend interface {
Expand All @@ -80,6 +95,7 @@ type Backend interface {
CreateTexture(pixels unsafe.Pointer, width, Height int) TextureID
CreateTextureRgba(img *image.RGBA, width, height int) TextureID
DeleteTexture(id TextureID)
SetDropCallback(DropCallback)

// TODO: flags needs generic layer
CreateWindow(title string, width, height int, flags GLFWWindowFlags)
Expand All @@ -99,6 +115,7 @@ type Backend interface {
loopFunc() func()
afterRenderHook() func()
beforeDestroyHook() func()
dropCallback() DropCallback
}

func CreateBackend( /*TODO: backend type*/ ) Backend {
Expand Down
4 changes: 4 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,9 @@ func main() {

backend.CreateWindow("Hello from cimgui-go", 1200, 900, 0)

backend.SetDropCallback(func(p []string) {
fmt.Printf("drop triggered: %v", p)
})

backend.Run(loop)
}
4 changes: 4 additions & 0 deletions glfw_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,8 @@ void igGLFWWindow_SetShouldClose(GLFWwindow *window, int value){
glfwSetWindowShouldClose(window, value);
}

void igGLFWWindow_SetDropCallbackCB(GLFWwindow *wnd){
glfwSetDropCallback(wnd, (GLFWdropfun)dropCallback);
}

#endif
12 changes: 12 additions & 0 deletions glfw_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type GLFWBackend struct {
beforeRender voidCallbackFunc
afterRender voidCallbackFunc
beforeDestoryContext voidCallbackFunc
dropCB DropCallback
window uintptr
}

Expand Down Expand Up @@ -96,6 +97,10 @@ func (b *GLFWBackend) loopFunc() func() {
return b.loop
}

func (b *GLFWBackend) dropCallback() DropCallback {
return b.dropCB
}

func (b *GLFWBackend) SetWindowPos(x, y int) {
C.igGLFWWindow_SetWindowPos(b.handle(), C.int(x), C.int(y))
}
Expand Down Expand Up @@ -152,3 +157,10 @@ func (b *GLFWBackend) CreateTextureRgba(img *image.RGBA, width, height int) Text
func (b *GLFWBackend) DeleteTexture(id TextureID) {
C.igDeleteTexture(C.ImTextureID(id))
}

// SetDropCallback sets the drop callback which is called when an object
// is dropped over the window.
func (b *GLFWBackend) SetDropCallback(cbfun DropCallback) {
b.dropCB = cbfun
C.igGLFWWindow_SetDropCallbackCB(b.handle())
}
3 changes: 3 additions & 0 deletions glfw_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ extern void igRunLoop(GLFWwindow *window, VoidCallback loop, VoidCallback before
extern void igGLFWWindow_GetDisplaySize(GLFWwindow *window, int *width, int *height);
extern void igGLFWWindow_SetWindowPos(GLFWwindow *window, int x, int y);
extern void igGLFWWindow_SetShouldClose(GLFWwindow *window, int value);
extern void igGLFWWindow_SetDropCallbackCB(GLFWwindow *window);
extern void igRefresh();
extern ImTextureID igCreateTexture(unsigned char *pixels, int width, int height);
extern void igDeleteTexture(ImTextureID id);

extern void dropCallback(int, char**);

#ifdef __cplusplus
}
#endif