Ejemplo n.º 1
0
Archivo: main.go Proyecto: Luit/go.uik
func uiktest() {

	wbounds := geom.Rect{
		Max: geom.Coord{480, 320},
	}
	w, err := uik.NewWindow(nil, int(wbounds.Max.X), int(wbounds.Max.Y))
	if err != nil {
		fmt.Println(err)
		return
	}
	w.W.SetTitle("uiktest")

	// Create a button with the given size and label
	b := widgets.NewButton("Hi")
	// Here we get the button's label's data
	ld := b.Label.GetConfig()
	// we modify the copy for a special message to display
	ld.Text = "clicked!"

	l := widgets.NewLabel(geom.Coord{100, 50}, widgets.LabelConfig{"text", 14, color.Black})
	b2 := widgets.NewButton("there")
	ld2 := b2.Label.GetConfig()
	ld2.Text = "BAM"

	// the widget.Buttton has a special channels that sends out wde.Buttons
	// whenever its clicked. Here we set up something that changes the
	// label's text every time a click is received.
	clicker := make(widgets.Clicker)
	b.AddClicker <- clicker
	go func() {
		for _ = range clicker {
			b.Label.SetConfig(ld)
			l.SetConfig(widgets.LabelConfig{"ohnoes", 20, color.Black})
		}
	}()

	clicker2 := make(widgets.Clicker)
	b2.AddClicker <- clicker2
	go func() {
		for _ = range clicker2 {
			b.Label.SetConfig(ld2)
			b2.Label.SetConfig(ld)
			l.SetConfig(widgets.LabelConfig{"oops", 14, color.Black})
		}
	}()

	cb := widgets.NewCheckbox(geom.Coord{50, 50})

	kg := widgets.NewKeyGrab(geom.Coord{50, 50})

	ge := layouts.NewGridEngine(layouts.GridConfig{})
	g := layouts.NewLayouter(ge)

	l0_0 := widgets.NewLabel(geom.Coord{}, widgets.LabelConfig{"0, 0", 12, color.Black})
	l0_1 := widgets.NewLabel(geom.Coord{}, widgets.LabelConfig{"0, 1", 12, color.Black})
	l1_0 := widgets.NewLabel(geom.Coord{}, widgets.LabelConfig{"1, 0", 12, color.Black})
	l1_1 := widgets.NewLabel(geom.Coord{}, widgets.LabelConfig{"1, 1", 12, color.Black})

	ge.Add(&l0_0.Block, layouts.GridComponent{
		GridX: 0, GridY: 0,
	})
	ge.Add(&l0_1.Block, layouts.GridComponent{
		GridX: 0, GridY: 1,
	})
	ge.Add(&l1_0.Block, layouts.GridComponent{
		GridX: 1, GridY: 0,
		MinSize:      geom.Coord{60, 60},
		AnchorRight:  true,
		AnchorBottom: true,
	})
	ge.Add(&l1_1.Block, layouts.GridComponent{
		GridX: 1, GridY: 1,
		AnchorRight: true,
		AnchorTop:   true,
	})
	ge.Add(&widgets.NewButton("Spanner").Block, layouts.GridComponent{
		GridX: 0, GridY: 2,
		ExtraX:       1,
		AnchorLeft:   true,
		AnchorRight:  true,
		AnchorTop:    true,
		AnchorBottom: true,
	})
	ge.Add(&widgets.NewButton("Offset Spanner").Block, layouts.GridComponent{
		GridX: 1, GridY: 3,
		ExtraX:       1,
		AnchorLeft:   true,
		AnchorRight:  true,
		AnchorTop:    true,
		AnchorBottom: true,
	})

	if true {
		imgReader := bytes.NewReader(gordon_gif())
		gordonImage, gerr := gif.Decode(imgReader)
		if gerr == nil {
			im := widgets.NewImage(widgets.ImageConfig{
				Image: gordonImage,
			})
			ge.Add(&im.Block, layouts.GridComponent{
				GridX: 0, GridY: 4,
				ExtraX: 2,
			})
		} else {
			fmt.Println(gerr)
		}
	}

	clicker3 := make(widgets.Clicker)
	b.AddClicker <- clicker3
	go func() {
		for _ = range clicker3 {
			l0_0.SetConfig(widgets.LabelConfig{"Pow", 12, color.Black})
		}
	}()
	clicker4 := make(widgets.Clicker)
	b2.AddClicker <- clicker4
	go func() {
		for _ = range clicker4 {
			l0_0.SetConfig(widgets.LabelConfig{"gotcha", 12, color.Black})
		}
	}()

	e := widgets.NewEntry(geom.Coord{100, 30})

	_ = kg
	_ = cb
	_ = e

	// the HBox is a special type of grid that lines things up horizontally
	hb := layouts.HBox(layouts.GridConfig{},
		&b.Block,
		&l.Block,
		// &kg.Block,
		&b2.Block,
		&e.Block,
		// &e.Block,
		&g.Block,
	)

	// set this HBox to be the window pane
	w.SetPane(&hb.Block)

	w.Show()

	// Here we set up a subscription on the window's close events.
	done := make(chan interface{}, 1)
	isDone := func(e interface{}) (accept, done bool) {
		_, accept = e.(uik.CloseEvent)
		done = accept
		return
	}
	w.Block.Subscribe <- uik.Subscription{isDone, done}

	// once a close event comes in on the subscription, end the program
	<-done

	w.W.Close()

	wde.Stop()
}
Ejemplo n.º 2
0
func main() {
	gmd.SetAppName("go.uik")

	go func() {

		uik.WindowGenerator = func(parent wde.Window, width, height int) (window wde.Window, err error) {
			window, err = gmd.NewWindow()
			window.SetSize(width, height)
			return
		}

		wbounds := geom.Rect{
			Max: geom.Coord{480, 320},
		}
		w, err := uik.NewWindow(nil, int(wbounds.Max.X), int(wbounds.Max.Y))
		if err != nil {
			fmt.Println(err)
			return
		}
		w.W.SetTitle("go.uik")

		// Create a button with the given size and label
		b := widgets.NewButton("Hi")
		// Here we get the button's label's data
		ld := <-b.Label.GetConfig
		// we modify the copy for a special message to display
		ld.Text = "clicked!"

		l := widgets.NewLabel(geom.Coord{100, 50}, widgets.LabelData{"text", 14, color.Black})
		b2 := widgets.NewButton("there")
		ld2 := <-b2.Label.GetConfig
		ld2.Text = "BAM"

		// the widget.Buttton has a special channels that sends out wde.Buttons
		// whenever its clicked. Here we set up something that changes the
		// label's text every time a click is received.
		clicker := make(widgets.Clicker)
		b.AddClicker <- clicker
		go func() {
			for _ = range clicker {
				b.Label.SetConfig <- ld
				l.SetConfig <- widgets.LabelData{"ohnoes", 20, color.Black}
			}
		}()

		clicker2 := make(widgets.Clicker)
		b2.AddClicker <- clicker2
		go func() {
			for _ = range clicker2 {
				b.Label.SetConfig <- ld2
				b2.Label.SetConfig <- ld
				l.SetConfig <- widgets.LabelData{"oops", 14, color.Black}
			}
		}()

		cb := widgets.NewCheckbox(geom.Coord{50, 50})

		kg := widgets.NewKeyGrab(geom.Coord{50, 50})
		kg2 := widgets.NewKeyGrab(geom.Coord{50, 50})

		g := layouts.NewGrid(layouts.GridConfig{})

		l0_0 := widgets.NewLabel(geom.Coord{}, widgets.LabelData{"0, 0", 12, color.Black})
		l0_1 := widgets.NewLabel(geom.Coord{}, widgets.LabelData{"0, 1", 12, color.Black})
		l1_0 := widgets.NewLabel(geom.Coord{}, widgets.LabelData{"1, 0", 12, color.Black})
		l1_1 := widgets.NewLabel(geom.Coord{}, widgets.LabelData{"1, 1", 12, color.Black})

		g.Add <- layouts.BlockData{
			Block: &l0_0.Block,
			GridX: 0, GridY: 0,
		}
		g.Add <- layouts.BlockData{
			Block: &l0_1.Block,
			GridX: 0, GridY: 1,
		}
		g.Add <- layouts.BlockData{
			Block: &l1_0.Block,
			GridX: 1, GridY: 0,
		}
		g.Add <- layouts.BlockData{
			Block: &l1_1.Block,
			GridX: 1, GridY: 1,
		}

		clicker3 := make(widgets.Clicker)
		b.AddClicker <- clicker3
		go func() {
			for _ = range clicker3 {
				l0_0.SetConfig <- widgets.LabelData{"Pow", 12, color.Black}
			}
		}()
		clicker4 := make(widgets.Clicker)
		b2.AddClicker <- clicker4
		go func() {
			for _ = range clicker4 {
				l0_0.SetConfig <- widgets.LabelData{"gotcha", 12, color.Black}
			}
		}()

		e := widgets.NewEntry(geom.Coord{100, 30})

		// Here we create a flow layout, which just lines up its blocks from
		// left to right.
		fl := layouts.NewFlow()

		fl.Add <- &b.Block
		fl.Add <- &l.Block
		fl.Add <- &kg.Block
		fl.Add <- &b2.Block
		fl.Add <- &cb.Block
		fl.Add <- &kg2.Block
		fl.Add <- &g.Block
		fl.Add <- &e.Block
		// We add it to the window, taking up the entire space the window has.
		w.SetPane(&fl.Block)

		w.Show()

		// Here we set up a subscription on the window's close events.
		done := make(chan interface{}, 1)
		isDone := func(e interface{}) (accept, done bool) {
			_, accept = e.(uik.CloseEvent)
			done = accept
			return
		}
		w.Block.Subscribe <- uik.Subscription{isDone, done}

		// once a close event comes in on the subscription, end the program
		<-done

		w.W.Close()
		gmd.Stop()
	}()

	gmd.Run()
}