Example #1
0
func (ctxt *context) interactiveJulia(m draw.Mouse, mc <-chan draw.Mouse) {
	var i canvas.ImageItem
	i.IsOpaque = true
	i.R = draw.Rect(0, 0, ThumbSize, ThumbSize).Add(ctxt.cvs.Rect().Max).Sub(draw.Pt(ThumbSize, ThumbSize))
	i.Image = image.NewRGBA(image.Rect(0, 0, ThumbSize, ThumbSize))
	ctxt.cvs.AddItem(&i)
	defer func() {
		ctxt.cvs.Delete(&i)
		ctxt.cvs.Flush()
	}()
	delta := ctxt.mouseDelta()
	for {
		f := ctxt.f.Associated(m.Point.Add(delta))
		if f == nil {
			for m.Buttons != 0 {
				m = <-mc
			}
			return
		}
		r := draw.Rect(0, 0, ThumbSize, ThumbSize)
		f = f.Resize(r)
		ctxt.cvs.Atomically(func(flush canvas.FlushFunc) {
			NewTile(r, f, i.Image.(*image.RGBA), true)
			flush(i.Bbox(), nil)
		})
		ctxt.cvs.Flush()
		if m.Buttons == 0 {
			ctxt.julia(m.Point)
			return
		}
		m = <-mc
	}
}
Example #2
0
func redraw(new bool) {
	//	if new && getwindow(display, Refmesg) < 0 {
	//		sysfatal("can't reattach to window");
	//	}
	r := draw.Rect(0, 0, screen.Width(), screen.Height())
	pos.X = (pos.X - rboard.Min.X) / pcsz
	pos.Y = (pos.Y - rboard.Min.Y) / pcsz
	dx := r.Max.X - r.Min.X
	dy := r.Max.Y - r.Min.Y - 2*32
	DY = dx / NX
	if DY > dy/NY {
		DY = dy / NY
	}
	DY /= 8
	if DY > 4 {
		DY = 4
	}
	pcsz = DY * 8
	DMOUSE = pcsz / 3
	if pcsz < 8 {
		log.Exitf("screen too small: %d", pcsz)
	}
	rboard = screenr
	rboard.Min.X += (dx - pcsz*NX) / 2
	rboard.Min.Y += (dy-pcsz*NY)/2 + 32
	rboard.Max.X = rboard.Min.X + NX*pcsz
	rboard.Max.Y = rboard.Min.Y + NY*pcsz
	pscore.X = rboard.Min.X + 8
	pscore.Y = rboard.Min.Y - 32
	//	scoresz = stringsize(font, "000000");
	pos.X = pos.X*pcsz + rboard.Min.X
	pos.Y = pos.Y*pcsz + rboard.Min.Y
	bbr = draw.Rect(0, 0, N*pcsz, N*pcsz)
	bb = image.NewRGBA(bbr.Max.X, bbr.Max.Y)
	bbmask = image.NewRGBA(bbr.Max.X, bbr.Max.Y) // actually just a bitmap
	bb2r = draw.Rect(0, 0, N*pcsz, N*pcsz+DY)
	bb2 = image.NewRGBA(bb2r.Dx(), bb2r.Dy())
	bb2mask = image.NewRGBA(bb2r.Dx(), bb2r.Dy()) // actually just a bitmap
	draw.Draw(screen, screenr, draw.White, nil, draw.ZP)
	drawboard()
	setpiece(piece)
	if piece != nil {
		drawpiece()
	}
	lastmx = movemouse()
	newscreen = true
	display.FlushImage()
}
Example #3
0
func (t *Tiler) Draw(dst draw.Image, clipr draw.Rectangle) {
	if t.current == nil {
		panic("no current")
	}
	min := draw.Point{
		roundDown(clipr.Min.X, t.tileSize),
		roundDown(clipr.Min.Y, t.tileSize),
	}
	var p draw.Point
	for p.Y = min.Y; p.Y < clipr.Max.Y; p.Y += t.tileSize {
		for p.X = min.X; p.X < clipr.Max.X; p.X += t.tileSize {
			tile := t.all.Get(p.X, p.Y, t.calc)
			if tile == nil {
				tile = NewTile(
					draw.Rect(p.X, p.Y, p.X+t.tileSize, p.Y+t.tileSize),
					t.calc,
					nil,
					false,
				)
				t.all.Set(p.X, p.Y, t.calc, tile)
				t.current[tile] = true
				tile.Go(t.updatec)
			} else if !t.current[tile] {
				tile.Go(t.updatec)
			}
			tile.Draw(dst, clipr)
		}
	}
}
Example #4
0
func drawboard() {
	draw.Border(screen, rboard.Inset(-2), 2, draw.Black, draw.ZP)
	draw.Draw(screen, draw.Rect(rboard.Min.X, rboard.Min.Y-2, rboard.Max.X, rboard.Min.Y),
		draw.White, nil, draw.ZP)
	for i := 0; i < NY; i++ {
		for j := 0; j < NX; j++ {
			if board[i][j] != 0 {
				drawsq(screen, draw.Pt(rboard.Min.X+j*pcsz, rboard.Min.Y+i*pcsz), int(board[i][j]-16))
			}
		}
	}
	score(0)
	if suspended {
		draw.Draw(screen, screenr, draw.White, whitemask, draw.ZP)
	}
}
Example #5
0
func setpiece(p *Piece) {
	draw.Draw(bb, bbr, draw.White, nil, draw.ZP)
	draw.Draw(bbmask, bbr, draw.Transparent, nil, draw.ZP)
	br = draw.Rect(0, 0, 0, 0)
	br2 = br
	piece = p
	if p == nil {
		return
	}
	var op draw.Point
	var r draw.Rectangle
	r.Min = bbr.Min
	for i, pt := range p.d {
		r.Min.X += pt.X * pcsz
		r.Min.Y += pt.Y * pcsz
		r.Max.X = r.Min.X + pcsz
		r.Max.Y = r.Min.Y + pcsz
		if i == 0 {
			draw.Draw(bb, r, draw.Black, nil, draw.ZP)
			draw.Draw(bb, r.Inset(1), txpix[piece.tx], nil, draw.ZP)
			draw.Draw(bbmask, r, draw.Opaque, nil, draw.ZP)
			op = r.Min
		} else {
			draw.Draw(bb, r, bb, nil, op)
			draw.Draw(bbmask, r, bbmask, nil, op)
		}
		if br.Max.X < r.Max.X {
			br.Max.X = r.Max.X
		}
		if br.Max.Y < r.Max.Y {
			br.Max.Y = r.Max.Y
		}
	}
	br.Max = br.Max.Sub(bbr.Min)
	delta := draw.Pt(0, DY)
	br2.Max = br.Max.Add(delta)
	r = br.Add(bb2r.Min)
	r2 := br2.Add(bb2r.Min)
	draw.Draw(bb2, r2, draw.White, nil, draw.ZP)
	draw.Draw(bb2, r.Add(delta), bb, nil, bbr.Min)
	draw.Draw(bb2mask, r2, draw.Transparent, nil, draw.ZP)
	draw.Draw(bb2mask, r, draw.Opaque, bbmask, bbr.Min)
	draw.Draw(bb2mask, r.Add(delta), draw.Opaque, bbmask, bbr.Min)
}
Example #6
0
func NewTile(r draw.Rectangle, calc Fractal, img *image.RGBA, wait bool) *Tile {
	t := new(Tile)
	t.r = r
	t.nrows = 0
	if img == nil {
		img = image.NewRGBA(image.Rect(0, 0, r.Dx(), r.Dy()))
	}
	t.calc = calc
	t.image = img
	if wait {
		t.calculate(nil, nil)
		t.nrows = img.Height()
	} else {
		// choose some vaguely appropriate colour
		col := calc.At(centre(r))
		draw.Draw(t.image, draw.Rect(0, 0, r.Dx(), r.Dy()), image.Uniform{col}, draw.ZP)
	}
	return t
}
Example #7
0
func Play(pp []Piece, ctxt draw.Context) {
	display = ctxt
	screen = ctxt.Screen()
	screenr = draw.Rect(0, 0, screen.Width(), screen.Height())
	pieces = pp
	N = len(pieces[0].d)
	initPieces()
	rand.Seed(int32(time.Nanoseconds() % (1e9 - 1)))
	whitemask = draw.White.SetAlpha(0x7F)
	tsleep = 50
	timerc = time.Tick(int64(tsleep/2) * 1e6)
	suspc = make(chan bool)
	mousec = make(chan draw.Mouse)
	resizec = ctxt.ResizeChan()
	kbdc = make(chan int)
	go quitter(ctxt.QuitChan())
	go suspproc()
	points = 0
	redraw(false)
	play()
}
Example #8
0
func app(inContext draw.Context)
{
    vl_screen := inContext.Screen();

    screenr := draw.Rect(0, 0, vl_screen.Width(), vl_screen.Height());
    draw.Draw(vl_screen, screenr, draw.White, nil, draw.ZP);

    squareSize := 30;

    var cell *SMazeCell = new(SMazeCell);
    cell.draw(inContext, squareSize);

    inContext.FlushImage();

    fmt.Printf("Press the any key to exit.\n");
    for
    {
        select
        {
            case r := <- inContext.KeyboardChan():
                switch r
                {
                    case 'q', 'Q', 0x04, 0x7F, 32 :
                        fmt.Printf("Exiting because of keyboard event %d\n", r);
                        os.Exit(0);
                    default :
                        fmt.Printf("Exiting because of keyboard event %d\n", r);
                }
            case <- inContext.MouseChan():
                // No-op.
            case <- inContext.ResizeChan():
                // No-op.
            case <- inContext.QuitChan():
                fmt.Printf("Exiting because of QuitChan\n");
                return;
        }
    }
}
Example #9
0
func (t *Tile) calculate(updatec chan<- draw.Rectangle, stop <-chan (chan<- int)) {
	y0 := t.nrows + t.r.Min.Y
	for y := t.r.Min.Y + t.nrows; y < t.r.Max.Y; {
		row := t.image.Pixel[y-t.r.Min.Y]
		for i := range row {
			row[i] = t.calc.At(draw.Point{i + t.r.Min.X, y})
		}
		y++
		if updatec != nil && y&3 == 0 {
			select {
			case updatec <- draw.Rect(t.r.Min.X, y0, t.r.Max.X, y):
				y0 = y
			case done := <-stop:
				done <- y - t.r.Min.Y
				return
			}
		}
	}
	if updatec != nil {
		updatec <- t.r
		<-stop <- t.image.Height()
	}
}
Example #10
0
func (t *Tiler) Bbox() draw.Rectangle {
	return draw.Rect(-100000000, -100000000, 100000000, 100000000)
}
Example #11
0
func (inpMazeCell *SMazeCell) draw(inContext draw.Context, inSquareSize int)
{
    vl_screen := inContext.Screen();

    upXLeft, upYLeft := 100, 100;
    squareSize := inSquareSize;

    fullCell := draw.Rect(upXLeft, upYLeft, squareSize + upXLeft, squareSize + upYLeft);
    draw.Draw(vl_screen, fullCell, draw.Black, nil, draw.ZP);

    // inset set the color of the inside
    draw.Draw(vl_screen, fullCell.Inset(1), draw.White, nil, draw.ZP);

    //hide a wall with a rectangle... POOR SOLUTION !!
    if inpMazeCell.northOpen
    {
        // up wall
        hiderup := draw.Rect(upXLeft, upYLeft,
                             upXLeft + squareSize, upYLeft + 1);
        draw.Draw(vl_screen, hiderup, draw.White, nil, draw.ZP);
    }
    else
    {
        // do not hide
    }

    if inpMazeCell.westOpen
    {
        // right wall
        hiderright := draw.Rect(upXLeft + squareSize - 1, upYLeft,
                                upXLeft + squareSize, upYLeft + squareSize);
        draw.Draw(vl_screen, hiderright, draw.White, nil, draw.ZP);
    }
    else
    {
        // do not hide
    }

    if inpMazeCell.southOpen
    {
        // down wall
        hiderdown := draw.Rect(upXLeft, upYLeft + squareSize - 1,
                              upXLeft + squareSize, upYLeft + squareSize);
        draw.Draw(vl_screen, hiderdown, draw.White, nil, draw.ZP);
    }
    else
    {
        // do not hide
    }

    if inpMazeCell.eastOpen
    {
        // left wall
        hiderleft := draw.Rect(upXLeft, upYLeft,
                               upXLeft + 1, upYLeft + squareSize);
        draw.Draw(vl_screen, hiderleft, draw.White, nil, draw.ZP);
    }
    else
    {
        // do not hide
    }
}