This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,972 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/jacekolszak/pixiq/clear" | ||
"github.com/jacekolszak/pixiq/colornames" | ||
"github.com/jacekolszak/pixiq/glfw" | ||
"github.com/jacekolszak/pixiq/image" | ||
"github.com/jacekolszak/pixiq/loop" | ||
"github.com/jacekolszak/pixiq/mouse" | ||
) | ||
|
||
func main() { | ||
glfw.RunOrDie(func(openGL *glfw.OpenGL) { | ||
window, err := openGL.OpenWindow(80, 20, glfw.Title("Move mouse left and right"), glfw.Zoom(7)) | ||
if err != nil { | ||
log.Panicf("OpenWindow failed: %v", err) | ||
} | ||
// Create mouse instance for window. | ||
mouseState := mouse.New(window) | ||
x := 40 | ||
clearTool := clear.New() | ||
loop.Run(window, func(frame *loop.Frame) { | ||
screen := frame.Screen() | ||
clearTool.Clear(screen) | ||
// Poll mouse events | ||
mouseState.Update() | ||
x += mouseState.PositionChange().X() | ||
if x < 0 { | ||
x = 0 | ||
} | ||
if x >= screen.Width() { | ||
x = screen.Width() - 1 | ||
} | ||
drawVerticalLine(screen, x) | ||
if window.ShouldClose() { | ||
frame.StopLoopEventually() | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
func drawVerticalLine(screen image.Selection, x int) { | ||
for y := 0; y < screen.Height(); y++ { | ||
screen.SetColor(x, y, colornames.Azure) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/jacekolszak/pixiq/colornames" | ||
"github.com/jacekolszak/pixiq/glfw" | ||
"github.com/jacekolszak/pixiq/image" | ||
"github.com/jacekolszak/pixiq/loop" | ||
"github.com/jacekolszak/pixiq/mouse" | ||
) | ||
|
||
func main() { | ||
glfw.RunOrDie(func(openGL *glfw.OpenGL) { | ||
window, err := openGL.OpenWindow(80, 40, glfw.Title("Use left and right mouse buttons to draw"), glfw.Zoom(20)) | ||
if err != nil { | ||
log.Panicf("OpenWindow failed: %v", err) | ||
} | ||
// Create mouse instance for window. | ||
mouseState := mouse.New(window) | ||
loop.Run(window, func(frame *loop.Frame) { | ||
screen := frame.Screen() | ||
// Poll mouse events | ||
mouseState.Update() | ||
// Get cursor position | ||
pos := mouseState.Position() | ||
// Pressed returns true if given key is currently pressed. | ||
if mouseState.Pressed(mouse.Left) { | ||
// pos.X() and pos.Y() returns position in pixel dimensions | ||
drawSquare(screen, pos.X(), pos.Y(), colornames.White) | ||
} | ||
if mouseState.Pressed(mouse.Right) { | ||
drawSquare(screen, pos.X(), pos.Y(), colornames.Black) | ||
} | ||
if window.ShouldClose() { | ||
frame.StopLoopEventually() | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
func drawSquare(screen image.Selection, x int, y int, color image.Color) { | ||
for xOff := -1; xOff <= 1; xOff++ { | ||
for yOff := -1; yOff <= 1; yOff++ { | ||
screen.SetColor(x+xOff, y+yOff, color) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/jacekolszak/pixiq/colornames" | ||
"github.com/jacekolszak/pixiq/glfw" | ||
"github.com/jacekolszak/pixiq/loop" | ||
"github.com/jacekolszak/pixiq/mouse" | ||
) | ||
|
||
func main() { | ||
glfw.RunOrDie(func(openGL *glfw.OpenGL) { | ||
window, err := openGL.OpenWindow(80, 40, glfw.Title("Move mouse wheel in all possible directions"), glfw.Zoom(20)) | ||
if err != nil { | ||
log.Panicf("OpenWindow failed: %v", err) | ||
} | ||
x := 40 | ||
y := 20 | ||
// Create mouse instance for window. | ||
mouseState := mouse.New(window) | ||
loop.Run(window, func(frame *loop.Frame) { | ||
screen := frame.Screen() | ||
// Poll mouse events | ||
mouseState.Update() | ||
scroll := mouseState.Scroll() | ||
x += int(scroll.X()) | ||
y += int(scroll.Y()) | ||
screen.SetColor(x, y, colornames.Azure) | ||
if window.ShouldClose() { | ||
frame.StopLoopEventually() | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package internal | ||
|
||
import ( | ||
"github.com/go-gl/glfw/v3.3/glfw" | ||
|
||
"github.com/jacekolszak/pixiq/mouse" | ||
) | ||
|
||
// MouseEvents maps GLFW events to mouse.Event. Mapped events can be | ||
// polled using mouse.EventSource interface. | ||
type MouseEvents struct { | ||
buffer *mouse.EventBuffer | ||
window Window | ||
lastPosX, lastPosY float64 | ||
} | ||
|
||
// NewMouseEvents creates *MouseEvents using given buffer and window. Based on the | ||
// information returned by Window mouse move events are generated. | ||
func NewMouseEvents(buffer *mouse.EventBuffer, window Window) *MouseEvents { | ||
if buffer == nil { | ||
panic("nil buffer") | ||
} | ||
if window == nil { | ||
panic("nil window") | ||
} | ||
return &MouseEvents{buffer: buffer, window: window} | ||
} | ||
|
||
var mouseButtonMapping = map[glfw.MouseButton]mouse.Button{ | ||
glfw.MouseButtonLeft: mouse.Left, | ||
glfw.MouseButtonRight: mouse.Right, | ||
glfw.MouseButtonMiddle: mouse.Middle, | ||
glfw.MouseButton4: mouse.Button4, | ||
glfw.MouseButton5: mouse.Button5, | ||
glfw.MouseButton6: mouse.Button6, | ||
glfw.MouseButton7: mouse.Button7, | ||
glfw.MouseButton8: mouse.Button8, | ||
} | ||
|
||
// OnMouseButtonCallback passes GLFW mouse event | ||
func (e *MouseEvents) OnMouseButtonCallback(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, _ glfw.ModifierKey) { | ||
btn, ok := mouseButtonMapping[button] | ||
if !ok { | ||
return | ||
} | ||
switch action { | ||
case glfw.Press: | ||
e.buffer.Add(mouse.NewPressedEvent(btn)) | ||
case glfw.Release: | ||
e.buffer.Add(mouse.NewReleasedEvent(btn)) | ||
} | ||
} | ||
|
||
// OnScrollCallback passes GLFW mouse event | ||
func (e *MouseEvents) OnScrollCallback(_ *glfw.Window, xoff float64, yoff float64) { | ||
e.buffer.Add(mouse.NewScrolledEvent(-xoff, -yoff)) | ||
} | ||
|
||
// Window is an abstraction for getting information about cursor position, size and zoom. | ||
// It is needed for generating mouse move events | ||
type Window interface { | ||
CursorPosition() (float64, float64) | ||
Size() (int, int) | ||
Zoom() int | ||
} | ||
|
||
// Poll return next mapped event | ||
func (e *MouseEvents) Poll() (mouse.Event, bool) { | ||
event, ok := e.buffer.Poll() | ||
if ok { | ||
return event, ok | ||
} | ||
// generate move event, because GLFW does not provide move events for Linux | ||
// and Windows when cursor is outside window. | ||
realX, realY := e.window.CursorPosition() | ||
if e.lastPosX != realX || e.lastPosY != realY { | ||
w, h := e.window.Size() | ||
zoom := float64(e.window.Zoom()) | ||
insideWindow := true | ||
if int(realX) >= w || int(realY) >= h || realX < 0 || realY < 0 { | ||
insideWindow = false | ||
} | ||
e.lastPosX = realX | ||
e.lastPosY = realY | ||
return mouse.NewMovedEvent(int(realX/zoom), int(realY/zoom), realX, realY, insideWindow), true | ||
} | ||
return mouse.EmptyEvent, false | ||
} |
Oops, something went wrong.