var (
N int
- display draw.Context
+ display draw.Window
screen draw.Image
screenr image.Rectangle
board [NY][NX]byte
DY int
DMOUSE int
lastmx int
- mouse draw.Mouse
+ mouse draw.MouseEvent
newscreen bool
timerc <-chan int64
suspc chan bool
- mousec chan draw.Mouse
- resizec <-chan bool
+ mousec chan draw.MouseEvent
+ resizec chan bool
kbdc chan int
suspended bool
tsleep int
func movemouse() int {
//mouse.image.Point = image.Pt(rboard.Min.X + rboard.Dx()/2, rboard.Min.Y + rboard.Dy()/2);
//moveto(mousectl, mouse.Xy);
- return mouse.X
+ return mouse.Loc.X
}
func warp(p image.Point, x int) int {
suspend(true)
} else if suspended && !s {
suspend(false)
- lastmx = warp(mouse.Point, lastmx)
+ lastmx = warp(mouse.Loc, lastmx)
}
case <-timerc:
if suspended {
setpiece(nil)
pause(1500)
choosepiece()
- lastmx = warp(mouse.Point, lastmx)
+ lastmx = warp(mouse.Loc, lastmx)
return false
}
func play() {
- var om draw.Mouse
+ var om draw.MouseEvent
dt = 64
lastmx = -1
lastmx = movemouse()
choosepiece()
- lastmx = warp(mouse.Point, lastmx)
+ lastmx = warp(mouse.Loc, lastmx)
for {
select {
case mouse = <-mousec:
break
}
if lastmx < 0 {
- lastmx = mouse.X
+ lastmx = mouse.Loc.X
}
- if mouse.X > lastmx+DMOUSE {
+ if mouse.Loc.X > lastmx+DMOUSE {
mright()
- lastmx = mouse.X
+ lastmx = mouse.Loc.X
}
- if mouse.X < lastmx-DMOUSE {
+ if mouse.Loc.X < lastmx-DMOUSE {
mleft()
- lastmx = mouse.X
+ lastmx = mouse.Loc.X
}
if mouse.Buttons&^om.Buttons&1 == 1 {
rleft()
suspend(true)
} else if suspended && !s {
suspend(false)
- lastmx = warp(mouse.Point, lastmx)
+ lastmx = warp(mouse.Loc, lastmx)
}
case <-resizec:
}
func suspproc() {
- mc := display.MouseChan()
- kc := display.KeyboardChan()
-
s := false
for {
select {
- case mouse = <-mc:
+ case mouse = <-mousec:
mousec <- mouse
- case r := <-kc:
+ case r := <-kbdc:
switch r {
case 'q', 'Q', 0x04, 0x7F:
os.Exit(0)
display.FlushImage()
}
-func quitter(c <-chan bool) {
- <-c
+func demuxEvents(w draw.Window) {
+ for event := range w.EventChan() {
+ switch e := event.(type) {
+ case draw.MouseEvent:
+ mousec <- e
+ case draw.ConfigEvent:
+ resizec <- true
+ case draw.KeyEvent:
+ kbdc <- e.Key
+ }
+ }
os.Exit(0)
}
-func Play(pp []Piece, ctxt draw.Context) {
+func Play(pp []Piece, ctxt draw.Window) {
display = ctxt
screen = ctxt.Screen()
screenr = screen.Bounds()
tsleep = 50
timerc = time.Tick(int64(tsleep/2) * 1e6)
suspc = make(chan bool)
- mousec = make(chan draw.Mouse)
- resizec = ctxt.ResizeChan()
+ mousec = make(chan draw.MouseEvent)
+ resizec = make(chan bool)
kbdc = make(chan int)
- go quitter(ctxt.QuitChan())
+ go demuxEvents(ctxt)
go suspproc()
points = 0
redraw(false)
type Window struct {
Embedded bool // running as part of a web page?
*Image // screen image
-
- mousec chan draw.Mouse
- kbdc chan int
- quitc chan bool
- resizec chan bool
+ eventc chan interface{}
}
-// *Window implements draw.Context
-var _ draw.Context = (*Window)(nil)
+// *Window implements draw.Window.
+var _ draw.Window = (*Window)(nil)
-func (w *Window) KeyboardChan() <-chan int { return w.kbdc }
+func (w *Window) EventChan() <-chan interface{} { return w.eventc }
-func (w *Window) MouseChan() <-chan draw.Mouse {
- return w.mousec
+func (w *Window) Close() os.Error {
+ // TODO(nigeltao): implement.
+ return nil
}
-func (w *Window) QuitChan() <-chan bool { return w.quitc }
-
-func (w *Window) ResizeChan() <-chan bool { return w.resizec }
-
func (w *Window) Screen() draw.Image { return w.Image }
// Init initializes the Native Client subsystems specified by subsys.
return nil, err
}
w.Image = newImage(dx, dy, bridge.pixel)
- w.resizec = make(chan bool, 64)
- w.kbdc = make(chan int, 64)
- w.mousec = make(chan draw.Mouse, 64)
- w.quitc = make(chan bool)
+ w.eventc = make(chan interface{}, 64)
}
if subsys&SubsystemAudio != 0 {
import (
"encoding/binary"
"exp/draw"
+ "image"
"log"
"os"
"time"
mbe *mouseButtonEvent
qe *quitEvent
)
- var m draw.Mouse
+ var m draw.MouseEvent
for {
if err := videoPollEvent(buf); err != nil {
if !clean {
- clean = w.resizec <- false
+ clean = w.eventc <- draw.ConfigEvent{image.Config{ColorModel, w.Image.Bounds().Dx(), w.Image.Bounds().Dy()}}
}
time.Sleep(10e6) // 10ms
continue
// log.Stdoutf("%#v\n", e);
switch buf[0] {
case eventExpose:
- w.resizec <- true
+ w.eventc <- draw.ConfigEvent{image.Config{ColorModel, w.Image.Bounds().Dx(), w.Image.Bounds().Dy()}}
case eventKeyDown:
- w.kbdc <- int(ke.Key)
+ w.eventc <- draw.KeyEvent{int(ke.Key)}
case eventKeyUp:
- w.kbdc <- -int(ke.Key)
+ w.eventc <- draw.KeyEvent{-int(ke.Key)}
case eventMouseMotion:
- m.X = int(mme.X)
- m.Y = int(mme.Y)
+ m.Loc.X = int(mme.X)
+ m.Loc.Y = int(mme.Y)
m.Buttons = int(mme.Buttons)
m.Nsec = time.Nanoseconds()
- _ = w.mousec <- m
+ _ = w.eventc <- m
case eventMouseButtonDown:
- m.X = int(mbe.X)
- m.Y = int(mbe.Y)
+ m.Loc.X = int(mbe.X)
+ m.Loc.Y = int(mbe.Y)
// TODO(rsc): Remove uint cast once 8g bug is fixed.
m.Buttons |= 1 << uint(mbe.Button-1)
m.Nsec = time.Nanoseconds()
- _ = w.mousec <- m
+ _ = w.eventc <- m
case eventMouseButtonUp:
- m.X = int(mbe.X)
- m.Y = int(mbe.Y)
+ m.Loc.X = int(mbe.X)
+ m.Loc.Y = int(mbe.Y)
// TODO(rsc): Remove uint cast once 8g bug is fixed.
m.Buttons &^= 1 << uint(mbe.Button-1)
m.Nsec = time.Nanoseconds()
- _ = w.mousec <- m
+ _ = w.eventc <- m
case eventQuit:
- w.quitc <- true
+ close(w.eventc)
}
}
}