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

Implement func setMouseCallback() #513

Open
SilentGopherLnx opened this issue Sep 24, 2019 · 2 comments
Open

Implement func setMouseCallback() #513

SilentGopherLnx opened this issue Sep 24, 2019 · 2 comments

Comments

@SilentGopherLnx
Copy link

SilentGopherLnx commented Sep 24, 2019

Trying to implement setMouseCallback()
I have done version for one window. Can you fix it for any windows and commit?

1)highgui_gocv.h

typedef void (*mouse_callback)(int event, int x, int y, int flags, void* param);
void Window_SetMouseCallback(const char* winname, mouse_callback callback);

2)highgui.cpp

void Window_SetMouseCallback(const char* winname, mouse_callback callback) {
   cv::setMouseCallback(winname, callback, NULL);
}

3)NEW go-file

package gocv

/*
#include <stdlib.h>
#include "highgui_gocv.h"

extern void for_cgo_mouse_callback(int event, int x, int y, int flags);

static void InsideCGO_MouseCallback(char* winname) {
	void callback(int event, int x, int y, int flags, void* param) {
		for_cgo_mouse_callback(event, x, y, flags);
	};
	Window_SetMouseCallback(winname, callback);
}
*/
import "C"

type MouseCallback = func(event int, x int, y int, flags int)
var callb MouseCallback

func (w *Window) SetMouseCallback(callback MouseCallback) {
	cName := C.CString(w.name)
	C.InsideCGO_MouseCallback(cName)
	callb = callback
}

//export for_cgo_mouse_callback
func for_cgo_mouse_callback(event C.int, x C.int, y C.int, flags C.int) { // cName *C.char win C.int,
	callb(int(event), int(x), int(y), int(flags))
}

//MouseEventTypes
const EVENT_MOUSEMOVE = 0
const EVENT_LBUTTONDOWN = 1
const EVENT_RBUTTONDOWN = 2
const EVENT_MBUTTONDOWN = 3
const EVENT_LBUTTONUP = 4
const EVENT_RBUTTONUP = 5
const EVENT_MBUTTONUP = 6
const EVENT_LBUTTONDBLCLK = 7
const EVENT_RBUTTONDBLCLK = 8
const EVENT_MBUTTONDBLCLK = 9
const EVENT_MOUSEWHEEL = 10
const EVENT_MOUSEHWHEEL = 11

//MouseEventFlags
const EVENT_FLAG_LBUTTON = 1
const EVENT_FLAG_RBUTTON = 2
const EVENT_FLAG_MBUTTON = 4
const EVENT_FLAG_CTRLKEY = 8
const EVENT_FLAG_SHIFTKEY = 16
const EVENT_FLAG_ALTKEY = 32

Usage Example

w := gocv.NewWindow("test")
mouse_callback := func(event int, x int, y int, flags int) {
	if event == cv.EVENT_LBUTTONDOWN {
		Prln("mouse click at: " + I2S(x) + "," + I2S(y))
	}
}
w.SetMouseCallback(mouse_callback)
@netbrain
Copy link
Contributor

netbrain commented Feb 5, 2020

relates to #603

@amlwwalker
Copy link

and then to be able to implement polygon area of interest like this https://www.programmersought.com/article/3449903953/

This was referenced Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants