Explorar o código

Merge pull request #144 from felzix/master

added focus event
Daniel Salvadori %!s(int64=4) %!d(string=hai) anos
pai
achega
9fc9a1ec88
Modificáronse 4 ficheiros con 46 adicións e 13 borrados
  1. 1 2
      core/dispatcher.go
  2. 21 0
      window/canvas.go
  3. 7 0
      window/glfw.go
  4. 17 11
      window/window.go

+ 1 - 2
core/dispatcher.go

@@ -49,10 +49,9 @@ func (d *Dispatcher) Subscribe(evname string, cb Callback) {
 	d.evmap[evname] = append(d.evmap[evname], subscription{nil, cb})
 }
 
-// SubscribeID subscribes a callback to events events with the given name.
+// SubscribeID subscribes a callback to events with the given name.
 // The user-provided unique id can be used to unsubscribe via UnsubscribeID.
 func (d *Dispatcher) SubscribeID(evname string, id interface{}, cb Callback) {
-
 	d.evmap[evname] = append(d.evmap[evname], subscription{id, cb})
 }
 

+ 21 - 0
window/canvas.go

@@ -326,6 +326,7 @@ type WebGlCanvas struct {
 	sizeEv   SizeEvent
 	cursorEv CursorEvent
 	scrollEv ScrollEvent
+	focusEv  FocusEvent
 
 	// Callbacks
 	onCtxMenu  js.Func
@@ -336,6 +337,8 @@ type WebGlCanvas struct {
 	mouseMove  js.Func
 	mouseWheel js.Func
 	winResize  js.Func
+	winFocus   js.Func
+	winBlur   js.Func
 }
 
 // Init initializes the WebGlCanvas singleton.
@@ -464,6 +467,20 @@ func Init(canvasId string) error {
 	})
 	js.Global().Get("window").Call("addEventListener", "resize", w.winResize)
 
+	// Set up window focus callback to dispatch event
+	w.winFocus = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
+		w.focusEv.Focused = true
+		w.Dispatch(OnWindowFocus, &w.focusEv)
+		return nil
+	})
+	w.winBlur = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
+		w.focusEv.Focused = false
+		w.Dispatch(OnWindowFocus, &w.focusEv)
+		return nil
+	})
+	js.Global().Get("window").Call("addEventListener", "onfocus", w.winFocus)
+	js.Global().Get("window").Call("addEventListener", "onblur", w.winBlur)
+
 	//// Set up char callback to dispatch event TODO
 	//w.SetCharModsCallback(func(x *glfw.Window, char rune, mods glfw.ModifierKey) {	//
 	//	w.charEv.Char = char
@@ -536,6 +553,8 @@ func (w *WebGlCanvas) Destroy() {
 	w.canvas.Call("removeEventListener", "mousemove", w.mouseMove)
 	w.canvas.Call("removeEventListener", "wheel", w.mouseWheel)
 	js.Global().Get("window").Call("removeEventListener", "resize", w.winResize)
+	js.Global().Get("window").Call("removeEventListener", "onfocus", w.winFocus)
+	js.Global().Get("window").Call("removeEventListener", "onfocus", w.winBlur)
 
 	// Release callbacks
 	w.onCtxMenu.Release()
@@ -546,6 +565,8 @@ func (w *WebGlCanvas) Destroy() {
 	w.mouseMove.Release()
 	w.mouseWheel.Release()
 	w.winResize.Release()
+	w.winFocus.Release()
+	w.winBlur.Release()
 }
 
 // GetFramebufferSize returns the framebuffer size.

+ 7 - 0
window/glfw.go

@@ -205,6 +205,7 @@ type GlfwWindow struct {
 	sizeEv   SizeEvent
 	cursorEv CursorEvent
 	scrollEv ScrollEvent
+	focusEv  FocusEvent
 
 	mods ModifierKey // Current modifier keys
 
@@ -345,6 +346,12 @@ func Init(width, height int, title string) error {
 		w.Dispatch(OnWindowPos, &w.posEv)
 	})
 
+	// Set up window focus callback to dispatch event
+	w.SetFocusCallback(func(x *glfw.Window, focused bool) {
+		w.focusEv.Focused = focused
+		w.Dispatch(OnWindowFocus, &w.focusEv)
+	})
+
 	// Set up window cursor position callback to dispatch event
 	w.SetCursorPosCallback(func(x *glfw.Window, xpos float64, ypos float64) {
 		w.cursorEv.Xpos = float32(xpos)

+ 17 - 11
window/window.go

@@ -76,17 +76,18 @@ const (
 )
 
 // Window event names. See availability per platform below ("x" indicates available).
-const ( //                             Desktop | Browser |
-	OnWindowPos  = "w.OnWindowPos"  //    x    |         |
-	OnWindowSize = "w.OnWindowSize" //    x    |         |
-	OnKeyUp      = "w.OnKeyUp"      //    x    |    x    |
-	OnKeyDown    = "w.OnKeyDown"    //    x    |    x    |
-	OnKeyRepeat  = "w.OnKeyRepeat"  //    x    |         |
-	OnChar       = "w.OnChar"       //    x    |    x    |
-	OnCursor     = "w.OnCursor"     //    x    |    x    |
-	OnMouseUp    = "w.OnMouseUp"    //    x    |    x    |
-	OnMouseDown  = "w.OnMouseDown"  //    x    |    x    |
-	OnScroll     = "w.OnScroll"     //    x    |    x    |
+const ( //                               Desktop | Browser |
+	OnWindowFocus = "w.OnWindowFocus" //    x    |    x    |
+	OnWindowPos   = "w.OnWindowPos"   //    x    |         |
+	OnWindowSize  = "w.OnWindowSize"  //    x    |         |
+	OnKeyUp       = "w.OnKeyUp"       //    x    |    x    |
+	OnKeyDown     = "w.OnKeyDown"     //    x    |    x    |
+	OnKeyRepeat   = "w.OnKeyRepeat"   //    x    |         |
+	OnChar        = "w.OnChar"        //    x    |    x    |
+	OnCursor      = "w.OnCursor"      //    x    |    x    |
+	OnMouseUp     = "w.OnMouseUp"     //    x    |    x    |
+	OnMouseDown   = "w.OnMouseDown"   //    x    |    x    |
+	OnScroll      = "w.OnScroll"      //    x    |    x    |
 )
 
 // PosEvent describes a windows position changed event
@@ -134,3 +135,8 @@ type ScrollEvent struct {
 	Yoffset float32
 	Mods    ModifierKey
 }
+
+// FocusEvent describes a focus event
+type FocusEvent struct {
+	Focused bool
+}