Skip to content

Commit

Permalink
Merge pull request #199 from oakmound/feature/v4-eventskeys+examples
Browse files Browse the repository at this point in the history
Feature/v4 eventskeys+examples
  • Loading branch information
Implausiblyfun authored Apr 2, 2022
2 parents 382dded + 8ed5338 commit e27b187
Show file tree
Hide file tree
Showing 77 changed files with 1,224 additions and 888 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ jobs:
runs-on: [self-hosted, linux, ARM64]
steps:

- name: Set up Go 1.17
- name: Set up Go 1.18
uses: actions/setup-go@v1
with:
go-version: 1.17
go-version: 1.18
id: go

- name: Check out code into the Go module directory
Expand All @@ -27,10 +27,10 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.17
- name: Set up Go 1.18
uses: actions/setup-go@v1
with:
go-version: 1.17
go-version: 1.18
id: go

- name: Check out code into the Go module directory
Expand Down
4 changes: 2 additions & 2 deletions debugstream/scopeHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ func mouseDetails(w window.Window) func(*mouse.Event) event.Response {
if len(results) > 0 {
i := results[0].CID
if i > 0 && cm.HasEntity(i) {
e := cm.GetEntity(i)
e := cm.HasEntity(i)
fmt.Printf("%+v\n", e)
} else {
fmt.Println("No entity ", i)
}
}

return event.UnbindThis
return event.ResponseUnbindThisBinding
}
}

Expand Down
4 changes: 1 addition & 3 deletions debugtools/inputviz/joystick.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"math"
"time"

mkey "golang.org/x/mobile/event/key"

"github.com/oakmound/oak/v3/alg/floatgeom"
"github.com/oakmound/oak/v3/dlog"
"github.com/oakmound/oak/v3/event"
Expand Down Expand Up @@ -189,7 +187,7 @@ func (j *Joystick) RenderAndListen(ctx *scene.Context, joy *joystick.Joystick, l
})

// TODO: it is bad that you need to import two 'key' packages
b2 := event.Bind(ctx, key.Down(mkey.CodeSpacebar), j, func(j *Joystick, _ key.Event) event.Response {
b2 := event.Bind(ctx, key.Down(key.Spacebar), j, func(j *Joystick, _ key.Event) event.Response {
j.joy.Vibrate(math.MaxUint16, math.MaxUint16)
go func() {
time.Sleep(1 * time.Second)
Expand Down
50 changes: 24 additions & 26 deletions debugtools/inputviz/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
)

type KeyboardLayout interface {
KeyRect(k string) floatgeom.Rect2
KeyRect(k key.Code) floatgeom.Rect2
}

type LayoutKey interface {
Pos() LayoutPosition
}

type LayoutPosition struct {
Key string
Key key.Code
Gap bool
Width float64
Height float64
Expand All @@ -36,45 +36,45 @@ func (g gap) Pos() LayoutPosition {
}
}

type standardKey string
type standardKey key.Code

func (s standardKey) Pos() LayoutPosition {
return LayoutPosition{
Key: string(s),
Key: key.Code(s),
Width: 1,
Height: 1,
}
}

type wideKey struct {
k string
k key.Code
w float64
}

func (w wideKey) Pos() LayoutPosition {
return LayoutPosition{
Key: string(w.k),
Key: w.k,
Width: w.w,
Height: 1,
}
}

type tallKey struct {
k string
k key.Code
h float64
}

func (h tallKey) Pos() LayoutPosition {
return LayoutPosition{
Key: string(h.k),
Key: h.k,
Width: 1,
Height: h.h,
}
}

type LayoutQWERTY struct {
Bounds floatgeom.Rect2
layoutMap map[string]LayoutPosition
layoutMap map[key.Code]LayoutPosition
}

func (l *LayoutQWERTY) init() {
Expand All @@ -83,22 +83,22 @@ func (l *LayoutQWERTY) init() {
}
type sk = standardKey

l.layoutMap = make(map[string]LayoutPosition)
l.layoutMap = make(map[key.Code]LayoutPosition)
qwertyRows := [][]LayoutKey{
{sk(key.Escape), gap(1), sk(key.F1), sk(key.F2), sk(key.F3), sk(key.F4), gap(.5), sk(key.F5), sk(key.F6), sk(key.F7), sk(key.F8), gap(.5), sk(key.F9), sk(key.F10), sk(key.F11), sk(key.F12), gap(2.1), sk(key.Pause)},
{sk(key.GraveAccent), sk(key.One), sk(key.Two), sk(key.Three), sk(key.Four), sk(key.Five), sk(key.Six), sk(key.Seven), sk(key.Eight), sk(key.Nine), sk(key.Zero), sk(key.HyphenMinus), sk(key.EqualSign), wideKey{key.DeleteBackspace, 2.0}, gap(.1), sk(key.Insert), sk(key.Home), sk(key.PageUp), gap(.1), sk(key.KeypadNumLock), sk(key.KeypadSlash), sk(key.KeypadAsterisk), sk(key.KeypadHyphenMinus)},
{sk(key.GraveAccent), sk(key.Num1), sk(key.Num2), sk(key.Num3), sk(key.Num4), sk(key.Num5), sk(key.Num6), sk(key.Num7), sk(key.Num8), sk(key.Num9), sk(key.Num0), sk(key.HyphenMinus), sk(key.EqualSign), wideKey{key.DeleteBackspace, 2.0}, gap(.1), sk(key.Insert), sk(key.Home), sk(key.PageUp), gap(.1), sk(key.KeypadNumLock), sk(key.KeypadSlash), sk(key.KeypadAsterisk), sk(key.KeypadHyphenMinus)},
{wideKey{key.Tab, 1.5}, sk(key.Q), sk(key.W), sk(key.E), sk(key.R), sk(key.T), sk(key.Y), sk(key.U), sk(key.I), sk(key.O), sk(key.P), sk(key.LeftSquareBracket), sk(key.RightSquareBracket), wideKey{key.Backslash, 1.5}, gap(.1), sk(key.DeleteForward), sk(key.End), sk(key.PageDown), gap(.1), sk(key.Keypad7), sk(key.Keypad8), sk(key.Keypad9), tallKey{key.KeypadPlusSign, 2}},
{wideKey{key.CapsLock, 1.5}, sk(key.A), sk(key.S), sk(key.D), sk(key.F), sk(key.G), sk(key.H), sk(key.J), sk(key.K), sk(key.L), sk(key.Semicolon), sk(key.Apostrophe), wideKey{key.ReturnEnter, 2.5}, gap(3.2), sk(key.Keypad4), sk(key.Keypad5), sk(key.Keypad6)},
{wideKey{key.LeftShift, 2.0}, sk(key.Z), sk(key.X), sk(key.C), sk(key.V), sk(key.B), sk(key.N), sk(key.M), sk(key.Comma), sk(key.FullStop), sk(key.Slash), wideKey{key.RightShift, 3.0}, gap(1.1), sk(key.UpArrow), gap(1.1), sk(key.Keypad1), sk(key.Keypad2), sk(key.Keypad3), tallKey{key.KeypadEnter, 2.0}},
{wideKey{key.LeftControl, 1.5}, sk(key.LeftGUI), wideKey{key.LeftAlt, 1.5}, wideKey{key.Spacebar, 7.0}, wideKey{key.RightAlt, 1.5}, sk(key.RightGUI), wideKey{key.RightControl, 1.5}, gap(.1), sk(key.LeftArrow), sk(key.DownArrow), sk(key.RightArrow), gap(.1), wideKey{key.Keypad0, 2.0}, sk(key.KeypadPeriod)},
{wideKey{key.LeftControl, 1.5}, sk(key.LeftGUI), wideKey{key.LeftAlt, 1.5}, wideKey{key.Spacebar, 7.0}, wideKey{key.RightAlt, 1.5}, sk(key.RightGUI), wideKey{key.RightControl, 1.5}, gap(.1), sk(key.LeftArrow), sk(key.DownArrow), sk(key.RightArrow), gap(.1), wideKey{key.Keypad0, 2.0}, sk(key.KeypadFullStop)},
}
rowFloats := []float64{0.0, 1.1, 2.1, 3.1, 4.1, 5.1}
for row, cols := range qwertyRows {
rf := rowFloats[row]
cf := 0.0
for _, v := range cols {
ps := v.Pos()
if ps.Key != "" {
if ps.Key != 0 {
l.layoutMap[ps.Key] = LayoutPosition{
Row: rf,
Col: cf,
Expand All @@ -111,7 +111,7 @@ func (l *LayoutQWERTY) init() {
}
}

func (l *LayoutQWERTY) KeyRect(k string) floatgeom.Rect2 {
func (l *LayoutQWERTY) KeyRect(k key.Code) floatgeom.Rect2 {
l.init()

pos, ok := l.layoutMap[k]
Expand All @@ -137,12 +137,12 @@ func (l *LayoutQWERTY) KeyRect(k string) floatgeom.Rect2 {
return floatgeom.NewRect2WH(x, y, keyWidth, keyHeight)
}

var defaultColors = map[string]color.Color{}
var defaultColors = map[key.Code]color.Color{}

type Keyboard struct {
Rect floatgeom.Rect2
BaseLayer int
Colors map[string]color.Color
Colors map[key.Code]color.Color
KeyboardLayout

RenderCharacters bool
Expand All @@ -151,7 +151,7 @@ type Keyboard struct {
event.CallerID
ctx *scene.Context

rs map[string]*render.Switch
rs map[key.Code]*render.Switch

bindings []event.Binding
}
Expand Down Expand Up @@ -179,9 +179,9 @@ func (k *Keyboard) RenderAndListen(ctx *scene.Context, layer int) error {
k.Font = render.DefaultFont()
}

k.rs = make(map[string]*render.Switch)
k.rs = make(map[key.Code]*render.Switch)

for kv := range key.AllKeys {
for kv, kstr := range key.AllKeys {
rect := k.KeyboardLayout.KeyRect(kv)
if rect == (floatgeom.Rect2{}) {
continue
Expand All @@ -199,7 +199,7 @@ func (k *Keyboard) RenderAndListen(ctx *scene.Context, layer int) error {
k.rs[kv] = r
if k.RenderCharacters {
x, y := rect.Min.X(), rect.Min.Y()
txt := k.Font.NewText(kv, x, y)
txt := k.Font.NewText(kstr, x, y)
tw, th := txt.GetDims()
xBuffer := rect.W() - float64(tw)
yBuffer := rect.H() - float64(th)
Expand All @@ -222,19 +222,17 @@ func (k *Keyboard) RenderAndListen(ctx *scene.Context, layer int) error {
}

b1 := event.Bind(ctx, key.AnyDown, k, func(kb *Keyboard, ev key.Event) event.Response {
btn := ev.Code.String()[4:]
if kb.rs[btn] == nil {
if kb.rs[ev.Code] == nil {
return 0
}
kb.rs[btn].Set("pressed")
kb.rs[ev.Code].Set("pressed")
return 0
})
b2 := event.Bind(ctx, key.AnyUp, k, func(kb *Keyboard, ev key.Event) event.Response {
btn := ev.Code.String()[4:]
if kb.rs[btn] == nil {
if kb.rs[ev.Code] == nil {
return 0
}
kb.rs[btn].Set("released")
kb.rs[ev.Code].Set("released")
return 0
})
k.bindings = []event.Binding{b1, b2}
Expand Down
2 changes: 1 addition & 1 deletion debugtools/inputviz/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (m *Mouse) CID() event.CallerID {

func (m *Mouse) RenderAndListen(ctx *scene.Context, layer int) error {
m.ctx = ctx
m.CallerID = ctx.Handler.GetCallerMap().Register(m)
m.CallerID = ctx.Register(m)

if m.Rect.W() == 0 || m.Rect.H() == 0 {
m.Rect.Max = m.Rect.Min.Add(floatgeom.Point2{60, 100})
Expand Down
7 changes: 4 additions & 3 deletions debugtools/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package debugtools
import (
"github.com/oakmound/oak/v3/dlog"
"github.com/oakmound/oak/v3/event"
"github.com/oakmound/oak/v3/key"
"github.com/oakmound/oak/v3/mouse"
"github.com/oakmound/oak/v3/scene"
)

// DebugMouseRelease will print the position and button pressed of the mouse when the mouse is released, if the given
// key is held down at the time. If no key is given, it will always be printed
func DebugMouseRelease(ctx *scene.Context, k string) {
// key is held down at the time. If 0 is given, it will always be printed
func DebugMouseRelease(ctx *scene.Context, k key.Code) {
event.GlobalBind(ctx, mouse.Release, func(mev *mouse.Event) event.Response {
if k == "" || ctx.KeyState.IsDown(k) {
if k == 0 || ctx.KeyState.IsDown(k) {
dlog.Info(mev)
}
return 0
Expand Down
21 changes: 5 additions & 16 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/oakmound/oak/v3/alg/intgeom"
"github.com/oakmound/oak/v3/event"
"github.com/oakmound/oak/v3/key"
"github.com/oakmound/oak/v3/render"
"github.com/oakmound/oak/v3/scene"
)
Expand Down Expand Up @@ -37,27 +38,15 @@ func AddScene(name string, sc scene.Scene) error {
}

// IsDown calls IsDown on the default window.
func IsDown(key string) bool {
func IsDown(k key.Code) bool {
initDefaultWindow()
return defaultWindow.IsDown(key)
return defaultWindow.IsDown(k)
}

// IsHeld calls IsHeld on the default window.
func IsHeld(key string) (bool, time.Duration) {
func IsHeld(k key.Code) (bool, time.Duration) {
initDefaultWindow()
return defaultWindow.IsHeld(key)
}

// SetUp calls SetUp on the default window.
func SetUp(key string) {
initDefaultWindow()
defaultWindow.SetUp(key)
}

// SetDown calls SetDown on the default window.
func SetDown(key string) {
initDefaultWindow()
defaultWindow.SetDown(key)
return defaultWindow.IsHeld(k)
}

// SetViewportBounds calls SetViewportBounds on the default window.
Expand Down
11 changes: 9 additions & 2 deletions entities/doodad.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,24 @@ func (d *Doodad) Destroy() {
d.R.Undraw()
}
event.DefaultBus.UnbindAllFrom(d.CallerID)
event.DefaultCallerMap.DestroyEntity(d.CallerID)
event.DefaultCallerMap.RemoveEntity(d.CallerID)
}

// Overwrites

// SetPos both Sets logical position and renderable position
// The need for this sort of function is lessened with the introduction
// of vector attachement.
// of vector attachment.
func (d *Doodad) SetPos(x, y float64) {
d.SetLogicPos(x, y)
if d.R != nil {
d.R.SetPos(x, y)
}
}

// GetRenmderable retrieves the renderable.
// Mainly used to satisfy upper level interfaces.
// TODO: remove along with entity rework
func (d *Doodad) GetRenderable() render.Renderable {
return d.R
}
2 changes: 1 addition & 1 deletion entities/solid.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewSolid(x, y, w, h float64, r render.Renderable, tree *collision.Tree, cid
tree = collision.DefaultTree
}
s.Tree = tree
s.Space = collision.NewSpace(x, y, w, h, cid)
s.Space = collision.NewSpace(x, y, w, h, s.CallerID)
s.Tree.Add(s.Space)
return s
}
Expand Down
2 changes: 1 addition & 1 deletion entities/x/move/topdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Arrows(mvr Mover) {
}

// TopDown moves the given mover based on its speed as the given keys are pressed
func TopDown(mvr Mover, up, down, left, right string) {
func TopDown(mvr Mover, up, down, left, right key.Code) {
delta := mvr.GetDelta()
vec := mvr.Vec()
spd := mvr.GetSpeed()
Expand Down
4 changes: 2 additions & 2 deletions entities/x/stat/statistic.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (st *Statistics) TrackStats(no int, data interface{}) event.Response {
stat, ok := data.(stat)
if !ok {
dlog.Error("TrackStats called with a non-stat payload")
return event.UnbindThis
return event.ResponseUnbindThisBinding
}
st.trackStats(stat.name, stat.inc)
return 0
Expand All @@ -87,7 +87,7 @@ func (st *Statistics) TrackTimeStats(no int, data interface{}) event.Response {
timed, ok := data.(timedStat)
if !ok {
dlog.Error("TrackTimeStats called with a non-timedStat payload")
return event.UnbindThis
return event.ResponseUnbindThisBinding
}
if timed.on { //Turning on a thing to time track
st.statTimeLock.Lock()
Expand Down
Loading

0 comments on commit e27b187

Please sign in to comment.