Beispiel #1
0
func newRasterPlay() *rasterPlay {
	obj := new(rasterPlay)
	obj.points = make([]rpoint, 0, 100) // expansion later
	obj.moved = make(chan moveEvent)
	obj.raster.SetFill(&image.Uniform{image.AlphaMultiply(blue, 0x8000)})
	obj.c = canvas.NewCanvas(nil, cvs.Bbox())
	obj.colValue = values.NewValue(nil)
	obj.colValue.Set(image.Black)
	obj.HandlerItem = obj.c
	obj.c.AddItem(&obj.raster)
	go obj.listener()
	return obj
}
Beispiel #2
0
func main() {
	win, err := x11.NewWindow()
	if win == nil {
		log.Fatalf("no window: %v", err)
	}
	screen := win.Screen()

	bg := canvas.NewBackground(screen.(*image.RGBA), image.White, flushFunc(win))
	cvs = canvas.NewCanvas(nil, bg.Rect())
	bg.SetItem(cvs)

	item := canvas.Draggable(canvas.Moveable(
		canvas.NewText(
			image.ZP,
			0,
			"Hello, world",
			defaultFont(),
			30,
			nil)))
	item.SetCentre(image.Pt(cvs.Rect().Dx()/2, cvs.Rect().Dy()/3))
	cvs.AddItem(item)
	//	txtitem :=	canvas.NewText(
	//			image.Pt(100, 100),
	//			0,
	//			"Working?",
	//			defaultFont(),
	//			20,
	//			nil)

	//	img := canvas.ImageOf(txtitem)

	//	cvs.AddItem(canvas.NewImage(img, false, image.Pt(cvs.Width() / 2, cvs.Height()*2/3)))

	cvs.Flush()
	ec := win.EventChan()
	for {
		switch e := (<-ec).(type) {
		case nil:
			log.Fatal("quitting")
			return
		case draw.MouseEvent:
			if e.Buttons == 0 {
				break
			}
			cvs.HandleMouse(cvs, e, ec)
		}
	}
}
Beispiel #3
0
func main() {
	// Ticker that keeps the world ticking
	ticker = time.NewTicker(time.Nanosecond * 40000)
	// Initialize screen
	win, err := x11.NewWindow()
	if win == nil {
		log.Fatalf("no window: %v", err)
	}
	screen := win.Screen()

	bg := canvas.NewBackground(screen.(*image.RGBA), image.White, nil)
	cvs = canvas.NewCanvas(nil, bg.Rect())
	bg.SetItem(cvs)
	bbox := cvs.Bbox()
	height = uint32(bbox.Dy())
	width = uint32(bbox.Dx())

	// Set even handler for ui events
	ec := win.EventChan()
	go readKeyboardInput(ec)

	var last time.Time
	var now time.Time
	last = time.Now()

	// Parse scene
	sceneparser.ParseJsonFile(world, defaultScenePath)

	// Clear output buffer
	clear()

	renderer.Render(world, width, height, output)
	paint()
	win.FlushImage()

	// Main loop
	for {
		now = <-ticker.C
		paint()
		win.FlushImage()

		dt := now.Nanosecond() - last.Nanosecond()
		last = now
		dt = dt
	}
}
Beispiel #4
0
func main() {
	ctxt, err := x11.NewWindow()
	if ctxt == nil {
		log.Fatalf("no window: %v", err)
	}
	screen := ctxt.Screen()

	bg := canvas.NewBackground(screen.(*image.RGBA), image.White, flushFunc(ctxt))
	cvs = canvas.NewCanvas(nil, bg.Rect())
	bg.SetItem(cvs)
	ec := ctxt.EventChan()
	cvs.Flush()

	for {
		select {
		case e := <-ec:
			switch e := e.(type) {
			case nil:
				if closed(ec) {
					log.Fatal("quitting")
					return
				}
			case draw.MouseEvent:
				if e.Buttons == 0 {
					break
				}
				if cvs.HandleMouse(cvs, e, ec) {
					fmt.Printf("handled mouse\n")
					break
				}
				fmt.Printf("raster maker\n")
				rasterMaker(e, ec)
			}
		}
	}
}
Beispiel #5
0
func main() {
	flag.Parse()
	wctxt, err := x11.NewWindow()
	if wctxt == nil {
		log.Exitf("no window: %v", err)
	}
	screen := wctxt.Screen()

	ctxt := &context{}
	ctxt.iterations = *iterations

	bg := canvas.NewBackground(screen.(*image.RGBA), draw.White, flushFunc(wctxt))
	ctxt.cvs = canvas.NewCanvas(nil, bg.Rect())
	bg.SetItem(ctxt.cvs)

	ctxt.cache = NewTileTable()
	ctxt.setFractal(NewMandelbrot(topArea, ctxt.cvs.Rect(), false, 0, ctxt.iterations), draw.ZP)

	qc := wctxt.QuitChan()
	kc := wctxt.KeyboardChan()
	mc := wctxt.MouseChan()
	ctxt.cvs.Flush()
	for {
		select {
		case <-qc:
			log.Exit("quitting")
			return
		case m0 := <-mc:
			// b1 drag - drag mandel around.
			// double-b1 drag - zoom in to rectangle
			// douible-b1 click - zoom in a little
			// b2 click - julia
			// b2 drag - interactive julia
			// b3 zoom out
			if m0.Buttons == 0 {
				break
			}
			nclick := 0
			clicks, finalm := clicker(m0, mc)
			for _ = range clicks {
				nclick++
			}
			m := <-finalm
			dragging := m.Buttons != 0
			switch {
			case m0.Buttons&1 != 0:
				switch nclick {
				case 1:
					if dragging {
						ctxt.cvs.HandleMouse(ctxt.cvs, m, mc)
					}
				case 2:
					if dragging {
						ctxt.zoomRect(m, mc)
					} else {
						ctxt.zoomABit(m, mc)
					}
				}
			case m0.Buttons&2 != 0:
				switch nclick {
				case 1:
					if dragging {
						ctxt.interactiveJulia(m, mc)
					} else {
						ctxt.julia(m.Point)
					}
				}
			case m0.Buttons&4 != 0:
				ctxt.pop()
			}
		case <-kc:
		}
	}
}
Beispiel #6
0
func main() {
	rand.Seed(0)
	ctxt, err := x11.NewWindow()
	if ctxt == nil {
		log.Fatalf("no window: %v", err)
	}
	screen := ctxt.Screen()
	bg := canvas.NewBackground(screen.(*image.RGBA), image.White, flushFunc(ctxt))
	window = canvas.NewCanvas(nil, bg.Rect())
	bg.SetItem(window)
	nballs := 0
	ctxt.FlushImage()

	csz := window.Rect().Max

	// add edges of window
	addLine(image.Pt(-1, -1), image.Pt(csz.X, -1))
	addLine(image.Pt(csz.X, -1), image.Pt(csz.X, csz.Y))
	addLine(image.Pt(csz.X, csz.Y), image.Pt(-1, csz.Y))
	addLine(image.Pt(-1, csz.Y), image.Pt(-1, -1))

	go sliderProc()

	makeRect(image.Rect(30, 30, 200, 100), setAlpha(red, 128))
	makeRect(image.Rect(150, 90, 230, 230), setAlpha(blue, 128))

	window.Flush()

	mkball := make(chan ball)
	delball := make(chan bool)
	pause := make(chan bool)

	go monitor(mkball, delball, pause)

	for i := 0; i < nballs; i++ {
		mkball <- randBall()
	}
	ecc := make(chan (<-chan interface{}))
	ec := ctxt.EventChan()
	for {
		select {
		case e, ok := <-ec:
			if !ok {
				return
			}
			switch e := e.(type) {
			case ui.MouseEvent:
				if e.Buttons == 0 {
					break
				}
				if window.HandleMouse(window, e, ec) {
					break
				}
				switch {
				case e.Buttons&1 != 0:
					go handleMouse(e, ec, ecc, lineMaker)
					ec = nil
				case e.Buttons&2 != 0:
					go handleMouse(e, ec, ecc, func(m ui.MouseEvent, ec <-chan interface{}) {
						ballMaker(e, ec, mkball)
					})
					ec = nil
				case e.Buttons&4 != 0:
					delball <- true
				}
			case ui.KeyEvent:
				fmt.Printf("got key %c (%d)\n", e.Key, e.Key)
				switch e.Key {
				case ' ':
					pause <- true
				case 'd':
					delball <- true
				}
			default:
				fmt.Printf("unknown event %v\n", e)
			}
		case ec = <-ecc:
			break
		}
	}
}