Browse Source

Cleanup window package

Daniel Salvadori 6 years ago
parent
commit
20c3cb0c9e
3 changed files with 28 additions and 70 deletions
  1. 10 31
      window/canvas.go
  2. 3 17
      window/glfw.go
  3. 15 22
      window/window.go

+ 10 - 31
window/canvas.go

@@ -297,13 +297,6 @@ const (
 	MouseButtonMiddle = MouseButton(1)
 )
 
-// Actions
-const (
-	Release = Action(iota) // Release indicates that the key or mouse button was released
-	Press                  // Press indicates that the key or mouse button was pressed
-	Repeat                 // Repeat indicates that the key was held down until it repeated
-)
-
 // Input modes
 const (
 	CursorInputMode             = InputMode(iota) // See Cursor mode values
@@ -393,7 +386,6 @@ func Init(canvasId string) error {
 		event := args[0]
 		eventCode := event.Get("code").String()
 		w.keyEv.Keycode = Key(keyMap[eventCode])
-		w.keyEv.Action = Action(Press)
 		w.keyEv.Mods = getModifiers(event)
 		w.Dispatch(OnKeyDown, &w.keyEv)
 		return nil
@@ -404,7 +396,6 @@ func Init(canvasId string) error {
 		event := args[0]
 		eventCode := event.Get("code").String()
 		w.keyEv.Keycode = Key(keyMap[eventCode])
-		w.keyEv.Action = Action(Release)
 		w.keyEv.Mods = getModifiers(event)
 		w.Dispatch(OnKeyUp, &w.keyEv)
 		return nil
@@ -415,14 +406,10 @@ func Init(canvasId string) error {
 
 	w.mouseDown = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
 		event := args[0]
-		button := MouseButton(event.Get("button").Int())
-		xpos := event.Get("offsetX").Int()
-		ypos := event.Get("offsetY").Int()
-		w.mouseEv.Button = MouseButton(button)
-		w.mouseEv.Action = Action(Press)
+		w.mouseEv.Button = MouseButton(event.Get("button").Int())
+		w.mouseEv.Xpos = float32(event.Get("offsetX").Int()) //* float32(w.scaleX) TODO
+		w.mouseEv.Ypos = float32(event.Get("offsetY").Int()) //* float32(w.scaleY)
 		w.mouseEv.Mods = getModifiers(event)
-		w.mouseEv.Xpos = float32(xpos) //* float32(w.scaleX) TODO
-		w.mouseEv.Ypos = float32(ypos) //* float32(w.scaleY)
 		w.Dispatch(OnMouseDown, &w.mouseEv)
 		return nil
 	})
@@ -430,14 +417,10 @@ func Init(canvasId string) error {
 
 	w.mouseUp = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
 		event := args[0]
-		button := MouseButton(event.Get("button").Int())
-		xpos := event.Get("offsetX").Float()
-		ypos := event.Get("offsetY").Float()
-		w.mouseEv.Button = MouseButton(button)
-		w.mouseEv.Action = Action(Release)
+		w.mouseEv.Button = MouseButton(event.Get("button").Int())
+		w.mouseEv.Xpos = float32(event.Get("offsetX").Float()) //* float32(w.scaleX) TODO
+		w.mouseEv.Ypos = float32(event.Get("offsetY").Float()) //* float32(w.scaleY)
 		w.mouseEv.Mods = getModifiers(event)
-		w.mouseEv.Xpos = float32(xpos) //* float32(w.scaleX) TODO
-		w.mouseEv.Ypos = float32(ypos) //* float32(w.scaleY)
 		w.Dispatch(OnMouseUp, &w.mouseEv)
 		return nil
 	})
@@ -445,10 +428,8 @@ func Init(canvasId string) error {
 
 	w.mouseMove = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
 		event := args[0]
-		xpos := event.Get("offsetX").Float()
-		ypos := event.Get("offsetY").Float()
-		w.cursorEv.Xpos = float32(xpos) //* float32(w.scaleX) TODO
-		w.cursorEv.Ypos = float32(ypos) //* float32(w.scaleY)
+		w.cursorEv.Xpos = float32(event.Get("offsetX").Float()) //* float32(w.scaleX) TODO
+		w.cursorEv.Ypos = float32(event.Get("offsetY").Float()) //* float32(w.scaleY)
 		w.cursorEv.Mods = getModifiers(event)
 		w.Dispatch(OnCursor, &w.cursorEv)
 		return nil
@@ -458,10 +439,8 @@ func Init(canvasId string) error {
 	w.mouseWheel = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
 		event := args[0]
 		event.Call("preventDefault")
-		xoff := event.Get("deltaX").Float()
-		yoff := event.Get("deltaY").Float()
-		w.scrollEv.Xoffset = -float32(xoff) / 100.0
-		w.scrollEv.Yoffset = -float32(yoff) / 100.0
+		w.scrollEv.Xoffset = -float32(event.Get("deltaX").Float()) / 100.0
+		w.scrollEv.Yoffset = -float32(event.Get("deltaY").Float()) / 100.0
 		w.scrollEv.Mods = getModifiers(event)
 		w.Dispatch(OnScroll, &w.scrollEv)
 		return nil

+ 3 - 17
window/glfw.go

@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !wasm
+
 package window
 
 import (
@@ -168,16 +170,6 @@ const (
 	MouseButtonMiddle = MouseButton(glfw.MouseButtonMiddle)
 )
 
-// Actions
-const (
-	// Release indicates that key or mouse button was released
-	Release = Action(glfw.Release)
-	// Press indicates that key or mouse button was pressed
-	Press = Action(glfw.Press)
-	// Repeat indicates that key was held down until it repeated
-	Repeat = Action(glfw.Repeat)
-)
-
 // Input modes
 const (
 	CursorInputMode             = InputMode(glfw.CursorMode)             // See Cursor mode values
@@ -303,18 +295,14 @@ func Init(width, height int, title string) error {
 
 	// Set key callback to dispatch event
 	w.SetKeyCallback(func(x *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
-		w.keyEv.Keycode = Key(key)
-		w.keyEv.Action = Action(action)
+		w.keyEv.Key = Key(key)
 		w.keyEv.Mods = ModifierKey(mods)
 		w.mods = w.keyEv.Mods
 		if action == glfw.Press {
-			fmt.Println("GLFW: OnKeyDown")
 			w.Dispatch(OnKeyDown, &w.keyEv)
 		} else if action == glfw.Release {
-			fmt.Println("GLFW: OnKeyUp")
 			w.Dispatch(OnKeyUp, &w.keyEv)
 		} else if action == glfw.Repeat {
-			fmt.Println("GLFW: OnKeyRepeat")
 			w.Dispatch(OnKeyRepeat, &w.keyEv)
 		}
 	})
@@ -330,7 +318,6 @@ func Init(width, height int, title string) error {
 	w.SetMouseButtonCallback(func(x *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
 		xpos, ypos := x.GetCursorPos()
 		w.mouseEv.Button = MouseButton(button)
-		w.mouseEv.Action = Action(action)
 		w.mouseEv.Mods = ModifierKey(mods)
 		w.mouseEv.Xpos = float32(xpos * w.scaleX)
 		w.mouseEv.Ypos = float32(ypos * w.scaleY)
@@ -344,7 +331,6 @@ func Init(width, height int, title string) error {
 	// Set window size callback to dispatch event
 	w.SetSizeCallback(func(x *glfw.Window, width int, height int) {
 		fbw, fbh := x.GetFramebufferSize()
-		w.sizeEv.W = w
 		w.sizeEv.Width = width
 		w.sizeEv.Height = height
 		w.scaleX = float64(fbw) / float64(width)

+ 15 - 22
window/window.go

@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// Package window abstracts a system window.
+// Package window abstracts a platform-specific window.
 // Depending on the build tags it can be a GLFW desktop window or a browser WebGlCanvas.
 package window
 
@@ -46,9 +46,6 @@ type ModifierKey int
 // MouseButton corresponds to a mouse button.
 type MouseButton int
 
-// Action corresponds to a key or button action.
-type Action int
-
 // InputMode corresponds to an input mode.
 type InputMode int
 
@@ -71,20 +68,18 @@ const (
 	CursorLast = DiagResize2Cursor
 )
 
-//
-// Window event names used for dispatch and subscribe
-//
-const (
-	OnWindowPos  = "w.OnWindowPos"
-	OnWindowSize = "w.OnWindowSize"
-	OnKeyUp      = "w.OnKeyUp"
-	OnKeyDown    = "w.OnKeyDown"
-	OnKeyRepeat  = "w.OnKeyRepeat"
-	OnChar       = "w.OnChar"
-	OnCursor     = "w.OnCursor"
-	OnMouseUp    = "w.OnMouseUp"
-	OnMouseDown  = "w.OnMouseDown"
-	OnScroll     = "w.OnScroll"
+// 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    |
 )
 
 // PosEvent describes a windows position changed event
@@ -101,9 +96,8 @@ type SizeEvent struct {
 
 // KeyEvent describes a window key event
 type KeyEvent struct {
-	Keycode Key
-	Action  Action
-	Mods    ModifierKey
+	Key  Key
+	Mods ModifierKey
 }
 
 // CharEvent describes a window char event
@@ -117,7 +111,6 @@ type MouseEvent struct {
 	Xpos   float32
 	Ypos   float32
 	Button MouseButton
-	Action Action
 	Mods   ModifierKey
 }