Skip to content

Commit

Permalink
renamed functions + smoke test
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohce committed Sep 12, 2024
1 parent d9c3e54 commit 3e51224
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
18 changes: 9 additions & 9 deletions highgui.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import (
"unsafe"
)

type MouseCallbackFunc func(event int, x int, y int, flags int, userdata interface{})
type MouseHandlerFunc func(event int, x int, y int, flags int, userdata interface{})

type mouseCallbackInfo struct {
type mouseHandlerInfo struct {
c_name_ptr *C.char
fn MouseCallbackFunc
fn MouseHandlerFunc
userdata interface{}
}

var (
onMouseCallbacks = map[string]mouseCallbackInfo{}
onMouseHandlers = map[string]mouseHandlerInfo{}
)

// Window is a wrapper around OpenCV's "HighGUI" named windows.
Expand Down Expand Up @@ -61,12 +61,12 @@ func (w *Window) Close() error {
cName := C.CString(w.name)
defer C.free(unsafe.Pointer(cName))

mcbInfo, exists := onMouseCallbacks[w.name]
mcbInfo, exists := onMouseHandlers[w.name]
if exists {
mcbInfo.fn = nil
mcbInfo.userdata = nil
C.free(unsafe.Pointer(mcbInfo.c_name_ptr))
delete(onMouseCallbacks, w.name)
delete(onMouseHandlers, w.name)
}

C.Window_Close(cName)
Expand Down Expand Up @@ -397,17 +397,17 @@ func go_onmouse_dispatcher(event C.int, x C.int, y C.int, flags C.int, userdata

c_winname := (*C.char)(unsafe.Pointer(userdata))
winName := C.GoString(c_winname)
info, exists := onMouseCallbacks[winName]
info, exists := onMouseHandlers[winName]
if !exists {
return
}
info.fn(int(event), int(x), int(y), int(flags), info.userdata)
}

func (w *Window) SetMouseCallback(onMOuse MouseCallbackFunc, userdata interface{}) {
func (w *Window) SetMouseHandler(onMOuse MouseHandlerFunc, userdata interface{}) {
c_winname := C.CString(w.name)

onMouseCallbacks[w.name] = mouseCallbackInfo{
onMouseHandlers[w.name] = mouseHandlerInfo{
c_name_ptr: c_winname,
fn: onMOuse,
userdata: userdata,
Expand Down
36 changes: 12 additions & 24 deletions highgui_onmouse_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
package gocv

import (
"fmt"
"testing"
"unsafe"
)

func mcb(event int, x int, y int, flags int, userdata interface{}) {
name := *(userdata.(*string))
fmt.Println(name, event, x, y, flags)
type mouseHandlerUserData struct {
name string
}

func TestMouseCallback(t *testing.T) {
//t.Skip("TODO: figure out how to implement a test that can exercise the GUI")
func mouseHandler(event int, x int, y int, flags int, userdata interface{}) {}

// Comment'd out just for the sake of testing
// changes on this feature or until we find a
// proper way to run this test.
func TestMouseHandler(t *testing.T) {
windowName := "mouse"

w := NewWindow("mouse")
w := NewWindow(windowName)
defer w.Close()

name := "gocv"

w.SetMouseCallback(mcb, &name)

m := IMRead("images/face-detect.jpg", IMReadColor)
defer m.Close()

outer_for:
for {
w.IMShow(m)
switch w.WaitKey(5) {
case 'q':
break outer_for
}
udata := mouseHandlerUserData{
name: "gocv",
}

w.SetMouseHandler(mouseHandler, &udata)
go_onmouse_dispatcher(1, 2, 3, 4, unsafe.Pointer(&windowName))

}

0 comments on commit 3e51224

Please sign in to comment.