aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNigel Tao <nigeltao@golang.org>2010-09-22 12:20:56 +1000
committerNigel Tao <nigeltao@golang.org>2010-09-22 12:20:56 +1000
commit58795ea31a4cc7241082b6c8c729b1a00e1a5f62 (patch)
treea5ba04cfae833e680f63e2fcb53ef6402c3d8fcb
parent0e73fffe36a917ff2e8e41c4a91b6824a6446f2b (diff)
downloadgo-58795ea31a4cc7241082b6c8c729b1a00e1a5f62.tar.gz
go-58795ea31a4cc7241082b6c8c729b1a00e1a5f62.zip
exp/4s, exp/nacl/av: sync to recent exp/draw changes.
R=rsc CC=golang-dev https://golang.org/cl/2257042
-rw-r--r--src/pkg/exp/4s/xs.go58
-rw-r--r--src/pkg/exp/nacl/av/av.go26
-rw-r--r--src/pkg/exp/nacl/av/event.go31
3 files changed, 56 insertions, 59 deletions
diff --git a/src/pkg/exp/4s/xs.go b/src/pkg/exp/4s/xs.go
index 0332a3a94b..bc5c524836 100644
--- a/src/pkg/exp/4s/xs.go
+++ b/src/pkg/exp/4s/xs.go
@@ -51,7 +51,7 @@ const (
var (
N int
- display draw.Context
+ display draw.Window
screen draw.Image
screenr image.Rectangle
board [NY][NX]byte
@@ -69,12 +69,12 @@ var (
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
@@ -160,7 +160,7 @@ var txpix = [NCOL]image.ColorImage{
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 {
@@ -408,7 +408,7 @@ func pause(t int) {
suspend(true)
} else if suspended && !s {
suspend(false)
- lastmx = warp(mouse.Point, lastmx)
+ lastmx = warp(mouse.Loc, lastmx)
}
case <-timerc:
if suspended {
@@ -534,17 +534,17 @@ func drop(f bool) bool {
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:
@@ -553,15 +553,15 @@ func play() {
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()
@@ -581,7 +581,7 @@ func play() {
suspend(true)
} else if suspended && !s {
suspend(false)
- lastmx = warp(mouse.Point, lastmx)
+ lastmx = warp(mouse.Loc, lastmx)
}
case <-resizec:
@@ -637,15 +637,12 @@ func play() {
}
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)
@@ -716,12 +713,21 @@ func redraw(new bool) {
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()
@@ -733,10 +739,10 @@ func Play(pp []Piece, ctxt draw.Context) {
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)
diff --git a/src/pkg/exp/nacl/av/av.go b/src/pkg/exp/nacl/av/av.go
index 5c8728292d..93486f38b8 100644
--- a/src/pkg/exp/nacl/av/av.go
+++ b/src/pkg/exp/nacl/av/av.go
@@ -43,26 +43,19 @@ const (
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.
@@ -98,10 +91,7 @@ func Init(subsys int, dx, dy int) (*Window, os.Error) {
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 {
diff --git a/src/pkg/exp/nacl/av/event.go b/src/pkg/exp/nacl/av/event.go
index 11405c9808..4b0c3f5805 100644
--- a/src/pkg/exp/nacl/av/event.go
+++ b/src/pkg/exp/nacl/av/event.go
@@ -12,6 +12,7 @@ package av
import (
"encoding/binary"
"exp/draw"
+ "image"
"log"
"os"
"time"
@@ -398,11 +399,11 @@ func (w *Window) readEvents() {
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
@@ -440,33 +441,33 @@ func (w *Window) readEvents() {
// 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)
}
}
}