Пример #1
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)
		}
	}
}
Пример #2
0
func sliderProc() {
	val := values.NewValue(float64(0.0), nil)
	window.AddItem(canvas.NewSlider(image.Rect(10, 10, 100, 40), image.White, blue, val))
	window.AddItem(canvas.NewSlider(image.Rect(15, 35, 100, 70), image.White, setAlpha(red, 128), val))
	window.Flush()
	rval := values.Transform(val, values.UnitFloat64ToRangedFloat64(0.001e9, 0.1e9))
	timeText := canvas.NewText(
		image.Pt(10, 80), canvas.N|canvas.W, "", defaultFont(), 12, values.Transform(rval, values.Float64Multiply(1e-6).Combine(values.Float64ToString("%6.2gms", "%gms"))))
	window.AddItem(timeText)
	g := rval.Getter()
	for {
		x, ok := g.Get()
		if !ok {
			break
		}
		sleepTime = time.Duration(x.(float64))
	}
}
Пример #3
0
func monitor(mkball <-chan ball, delball <-chan bool, pause <-chan bool) {
	ballcountText := canvas.NewText(
		image.Pt(window.Rect().Max.X-5, 5), canvas.N|canvas.E, "0 balls", defaultFont(), 30, nil)
	window.AddItem(canvas.Draggable(ballcountText))
	ballcountText.SetFill(image.NewUniform(red))
	window.Flush()
	ctl := make(chan (chan<- bool))
	nballs := 0
	for {
		select {
		case b := <-mkball:
			go animateBall(ctl, b)
			nballs++
			ballcountText.SetText(fmt.Sprintf("%d balls", nballs))
			window.Flush()

		case <-pause:
			reply := make(chan bool)
			for i := 0; i < nballs; i++ {
				ctl <- reply
			}
			<-pause
			for i := 0; i < nballs; i++ {
				<-reply
			}
		case <-delball:
			// delete a random ball
			if nballs > 0 {
				ctl <- nil
				nballs--
				ballcountText.SetText(fmt.Sprintf("%d balls", nballs))
				window.Flush()
			}
		}
	}
}