示例#1
0
文件: main.go 项目: nelsam/gxui
func appMain(driver gxui.Driver) {
	theme := flags.CreateTheme(driver)

	label := theme.CreateLabel()
	label.SetText("This is a progress bar:")

	progressBar := theme.CreateProgressBar()
	progressBar.SetDesiredSize(math.Size{W: 400, H: 20})
	progressBar.SetTarget(100)

	layout := theme.CreateLinearLayout()
	layout.AddChild(label)
	layout.AddChild(progressBar)
	layout.SetHorizontalAlignment(gxui.AlignCenter)

	window := theme.CreateWindow(800, 600, "Progress bar")
	window.SetScale(flags.DefaultScaleFactor)
	window.AddChild(layout)
	window.OnClose(driver.Terminate)

	progress := 0
	pause := time.Millisecond * 500
	var timer *time.Timer
	timer = time.AfterFunc(pause, func() {
		driver.Call(func() {
			progress = (progress + 3) % progressBar.Target()
			progressBar.SetProgress(progress)
			timer.Reset(pause)
		})
	})
}
示例#2
0
文件: main.go 项目: nelsam/gxui
func appMain(driver gxui.Driver) {
	theme := flags.CreateTheme(driver)

	// ┌───────┐║┌───────┐
	// │       │║│       │
	// │   A   │║│   B   │
	// │       │║│       │
	// └───────┘║└───────┘
	// ═══════════════════
	// ┌───────┐║┌───────┐
	// │       │║│       │
	// │   C   │║│   D   │
	// │       │║│       │
	// └───────┘║└───────┘

	splitterAB := theme.CreateSplitterLayout()
	splitterAB.SetOrientation(gxui.Horizontal)
	splitterAB.AddChild(panelHolder("A", theme))
	splitterAB.AddChild(panelHolder("B", theme))

	splitterCD := theme.CreateSplitterLayout()
	splitterCD.SetOrientation(gxui.Horizontal)
	splitterCD.AddChild(panelHolder("C", theme))
	splitterCD.AddChild(panelHolder("D", theme))

	vSplitter := theme.CreateSplitterLayout()
	vSplitter.SetOrientation(gxui.Vertical)
	vSplitter.AddChild(splitterAB)
	vSplitter.AddChild(splitterCD)

	window := theme.CreateWindow(800, 600, "Panels")
	window.SetScale(flags.DefaultScaleFactor)
	window.AddChild(vSplitter)
	window.OnClose(driver.Terminate)
}
示例#3
0
文件: main.go 项目: nelsam/gxui
func appMain(driver gxui.Driver) {
	theme := flags.CreateTheme(driver)

	overlay := theme.CreateBubbleOverlay()

	holder := theme.CreatePanelHolder()
	holder.AddPanel(numberPicker(theme, overlay), "Default adapter")
	holder.AddPanel(colorPicker(theme), "Custom adapter")

	window := theme.CreateWindow(800, 600, "Lists")
	window.SetScale(flags.DefaultScaleFactor)
	window.AddChild(holder)
	window.AddChild(overlay)
	window.OnClose(driver.Terminate)
	window.SetPadding(math.Spacing{L: 10, T: 10, R: 10, B: 10})
}
示例#4
0
文件: main.go 项目: nelsam/gxui
func appMain(driver gxui.Driver) {
	theme := flags.CreateTheme(driver)

	label1 := theme.CreateLabel()
	label1.SetColor(gxui.White)
	label1.SetText("1x1")

	cell1x1 := theme.CreateLinearLayout()
	cell1x1.SetBackgroundBrush(gxui.CreateBrush(gxui.Blue40))
	cell1x1.SetHorizontalAlignment(gxui.AlignCenter)
	cell1x1.AddChild(label1)

	label2 := theme.CreateLabel()
	label2.SetColor(gxui.White)
	label2.SetText("2x1")

	cell2x1 := theme.CreateLinearLayout()
	cell2x1.SetBackgroundBrush(gxui.CreateBrush(gxui.Green40))
	cell2x1.SetHorizontalAlignment(gxui.AlignCenter)
	cell2x1.AddChild(label2)

	label3 := theme.CreateLabel()
	label3.SetColor(gxui.White)
	label3.SetText("1x2")

	cell1x2 := theme.CreateLinearLayout()
	cell1x2.SetBackgroundBrush(gxui.CreateBrush(gxui.Red40))
	cell1x2.SetHorizontalAlignment(gxui.AlignCenter)
	cell1x2.AddChild(label3)

	table := theme.CreateTableLayout()
	table.SetGrid(3, 2) // columns, rows

	// row, column, horizontal span, vertical span
	table.SetChildAt(0, 0, 1, 1, cell1x1)
	table.SetChildAt(0, 1, 2, 1, cell2x1)
	table.SetChildAt(2, 0, 1, 2, cell1x2)

	window := theme.CreateWindow(800, 600, "Table")
	window.AddChild(table)
	window.OnClose(driver.Terminate)
}
示例#5
0
文件: main.go 项目: nelsam/gxui
func appMain(driver gxui.Driver) {
	theme := flags.CreateTheme(driver)

	layout := theme.CreateLinearLayout()
	layout.SetDirection(gxui.TopToBottom)

	adapter := &adapter{}

	// hook up node changed function to the adapter OnDataChanged event.
	adapter.changed = func() { adapter.DataChanged(false) }

	// add all the species to the 'Animals' root node.
	items := addSpecies(adapter.add("Animals"))

	tree := theme.CreateTree()
	tree.SetAdapter(adapter)
	tree.Select(items["Doves"])
	tree.Show(tree.Selected())

	layout.AddChild(tree)

	row := theme.CreateLinearLayout()
	row.SetDirection(gxui.LeftToRight)
	layout.AddChild(row)

	expandAll := theme.CreateButton()
	expandAll.SetText("Expand All")
	expandAll.OnClick(func(gxui.MouseEvent) { tree.ExpandAll() })
	row.AddChild(expandAll)

	collapseAll := theme.CreateButton()
	collapseAll.SetText("Collapse All")
	collapseAll.OnClick(func(gxui.MouseEvent) { tree.CollapseAll() })
	row.AddChild(collapseAll)

	window := theme.CreateWindow(800, 600, "Tree view")
	window.SetScale(flags.DefaultScaleFactor)
	window.AddChild(layout)
	window.OnClose(driver.Terminate)
	window.SetPadding(math.Spacing{L: 10, T: 10, R: 10, B: 10})
}
示例#6
0
文件: main.go 项目: nelsam/gxui
func appMain(driver gxui.Driver) {
	theme := flags.CreateTheme(driver)
	window := theme.CreateWindow(800, 600, "Polygon")
	window.SetScale(flags.DefaultScaleFactor)

	canvas := driver.CreateCanvas(math.Size{W: 1000, H: 1000})
	drawStar(canvas, math.Point{X: 100, Y: 100}, 50, 0.2, 6)
	drawStar(canvas, math.Point{X: 650, Y: 170}, 70, 0.5, 7)
	drawStar(canvas, math.Point{X: 40, Y: 300}, 20, 0, 5)
	drawStar(canvas, math.Point{X: 410, Y: 320}, 25, 0.9, 5)
	drawStar(canvas, math.Point{X: 220, Y: 520}, 45, 0, 6)

	drawMoon(canvas, math.Point{X: 400, Y: 300}, 200)
	canvas.Complete()

	image := theme.CreateImage()
	image.SetCanvas(canvas)
	window.AddChild(image)

	window.OnClose(driver.Terminate)
}
示例#7
0
文件: main.go 项目: nelsam/gxui
func appMain(driver gxui.Driver) {
	theme := flags.CreateTheme(driver)

	window := theme.CreateWindow(200, 150, "Window")
	window.OnClose(driver.Terminate)
	window.SetScale(flags.DefaultScaleFactor)
	window.SetPadding(math.Spacing{L: 10, R: 10, T: 10, B: 10})
	button := theme.CreateButton()
	button.SetHorizontalAlignment(gxui.AlignCenter)
	button.SetSizeMode(gxui.Fill)
	toggle := func() {
		fullscreen := !window.Fullscreen()
		window.SetFullscreen(fullscreen)
		if fullscreen {
			button.SetText("Make windowed")
		} else {
			button.SetText("Make fullscreen")
		}
	}
	button.SetText("Make fullscreen")
	button.OnClick(func(gxui.MouseEvent) { toggle() })
	window.AddChild(button)
}
示例#8
0
文件: main.go 项目: nelsam/gxui
func appMain(driver gxui.Driver) {
	theme := flags.CreateTheme(driver)

	font, err := driver.CreateFont(gxfont.Default, 75)
	if err != nil {
		panic(err)
	}

	window := theme.CreateWindow(380, 100, "Hi")
	window.SetBackgroundBrush(gxui.CreateBrush(gxui.Gray50))

	label := theme.CreateLabel()
	label.SetFont(font)
	label.SetText("Hello world")

	window.AddChild(label)

	ticker := time.NewTicker(time.Millisecond * 30)
	go func() {
		phase := float32(0)
		for _ = range ticker.C {
			c := gxui.Color{
				R: 0.75 + 0.25*math.Cosf((phase+0.000)*math.TwoPi),
				G: 0.75 + 0.25*math.Cosf((phase+0.333)*math.TwoPi),
				B: 0.75 + 0.25*math.Cosf((phase+0.666)*math.TwoPi),
				A: 0.50 + 0.50*math.Cosf(phase*10),
			}
			phase += 0.01
			driver.Call(func() {
				label.SetColor(c)
			})
		}
	}()

	window.OnClose(ticker.Stop)
	window.OnClose(driver.Terminate)
}
示例#9
0
文件: main.go 项目: nelsam/gxui
func appMain(driver gxui.Driver) {
	args := flag.Args()
	if len(args) != 1 {
		fmt.Print("usage: image_viewer image-path\n")
		os.Exit(1)
	}

	file := args[0]
	f, err := os.Open(file)
	if err != nil {
		fmt.Printf("Failed to open image '%s': %v\n", file, err)
		os.Exit(1)
	}

	source, _, err := image.Decode(f)
	if err != nil {
		fmt.Printf("Failed to read image '%s': %v\n", file, err)
		os.Exit(1)
	}

	theme := flags.CreateTheme(driver)
	img := theme.CreateImage()

	mx := source.Bounds().Max
	window := theme.CreateWindow(mx.X, mx.Y, "Image viewer")
	window.SetScale(flags.DefaultScaleFactor)
	window.AddChild(img)

	// Copy the image to a RGBA format before handing to a gxui.Texture
	rgba := image.NewRGBA(source.Bounds())
	draw.Draw(rgba, source.Bounds(), source, image.ZP, draw.Src)
	texture := driver.CreateTexture(rgba, 1)
	img.SetTexture(texture)

	window.OnClose(driver.Terminate)
}
示例#10
0
文件: main.go 项目: nelsam/gxui
func appMain(driver gxui.Driver) {
	theme := flags.CreateTheme(driver)

	window := theme.CreateWindow(800, 600, "Open file...")
	window.SetScale(flags.DefaultScaleFactor)

	// fullpath is the textbox at the top of the window holding the current
	// selection's absolute file path.
	fullpath := theme.CreateTextBox()
	fullpath.SetDesiredWidth(math.MaxSize.W)

	// directories is the Tree of directories on the left of the window.
	// It uses the directoryAdapter to show the entire system's directory
	// hierarchy.
	directories := theme.CreateTree()
	directories.SetAdapter(&directoryAdapter{
		directory: directory{
			subdirs: roots.Roots(),
		},
	})

	// filesAdapter is the adapter used to show the currently selected directory's
	// content. The adapter has its data changed whenever the selected directory
	// changes.
	filesAdapter := &filesAdapter{}

	// files is the List of files in the selected directory to the right of the
	// window.
	files := theme.CreateList()
	files.SetAdapter(filesAdapter)

	open := theme.CreateButton()
	open.SetText("Open...")
	open.OnClick(func(gxui.MouseEvent) {
		fmt.Printf("File '%s' selected!\n", files.Selected())
		window.Close()
	})

	// If the user hits the enter key while the fullpath control has focus,
	// attempt to select the directory.
	fullpath.OnKeyDown(func(ev gxui.KeyboardEvent) {
		if ev.Key == gxui.KeyEnter || ev.Key == gxui.KeyKpEnter {
			path := fullpath.Text()
			if directories.Select(path) {
				directories.Show(path)
			}
		}
	})

	// When the directory selection changes, update the files list
	directories.OnSelectionChanged(func(item gxui.AdapterItem) {
		dir := item.(string)
		filesAdapter.SetFiles(filesAt(dir))
		fullpath.SetText(dir)
	})

	// When the file selection changes, update the fullpath text
	files.OnSelectionChanged(func(item gxui.AdapterItem) {
		fullpath.SetText(item.(string))
	})

	// When the user double-clicks a directory in the file list, select it in the
	// directories tree view.
	files.OnDoubleClick(func(gxui.MouseEvent) {
		if path, ok := files.Selected().(string); ok {
			if fi, err := os.Stat(path); err == nil && fi.IsDir() {
				if directories.Select(path) {
					directories.Show(path)
				}
			} else {
				fmt.Printf("File '%s' selected!\n", path)
				window.Close()
			}
		}
	})

	// Start with the CWD selected and visible.
	if cwd, err := os.Getwd(); err == nil {
		if directories.Select(cwd) {
			directories.Show(directories.Selected())
		}
	}

	splitter := theme.CreateSplitterLayout()
	splitter.SetOrientation(gxui.Horizontal)
	splitter.AddChild(directories)
	splitter.AddChild(files)

	topLayout := theme.CreateLinearLayout()
	topLayout.SetDirection(gxui.TopToBottom)
	topLayout.AddChild(fullpath)
	topLayout.AddChild(splitter)

	btmLayout := theme.CreateLinearLayout()
	btmLayout.SetDirection(gxui.BottomToTop)
	btmLayout.SetHorizontalAlignment(gxui.AlignRight)
	btmLayout.AddChild(open)
	btmLayout.AddChild(topLayout)

	window.AddChild(btmLayout)
	window.OnClose(driver.Terminate)
	window.SetPadding(math.Spacing{L: 10, T: 10, R: 10, B: 10})
}
示例#11
0
文件: main.go 项目: nelsam/gxui
func appMain(driver gxui.Driver) {
	theme := flags.CreateTheme(driver)
	layout := theme.CreateLinearLayout()
	layout.SetSizeMode(gxui.Fill)

	buttonState := map[gxui.Button]func() bool{}
	update := func() {
		for button, f := range buttonState {
			button.SetChecked(f())
		}
	}

	button := func(name string, action func(), isSelected func() bool) gxui.Button {
		b := theme.CreateButton()
		b.SetText(name)
		b.OnClick(func(gxui.MouseEvent) { action(); update() })
		layout.AddChild(b)
		buttonState[b] = isSelected
		return b
	}

	button("TopToBottom",
		func() { layout.SetDirection(gxui.TopToBottom) },
		func() bool { return layout.Direction().TopToBottom() },
	)
	button("LeftToRight",
		func() { layout.SetDirection(gxui.LeftToRight) },
		func() bool { return layout.Direction().LeftToRight() },
	)
	button("BottomToTop",
		func() { layout.SetDirection(gxui.BottomToTop) },
		func() bool { return layout.Direction().BottomToTop() },
	)
	button("RightToLeft",
		func() { layout.SetDirection(gxui.RightToLeft) },
		func() bool { return layout.Direction().RightToLeft() },
	)

	button("AlignLeft",
		func() { layout.SetHorizontalAlignment(gxui.AlignLeft) },
		func() bool { return layout.HorizontalAlignment().AlignLeft() },
	)
	button("AlignCenter",
		func() { layout.SetHorizontalAlignment(gxui.AlignCenter) },
		func() bool { return layout.HorizontalAlignment().AlignCenter() },
	)
	button("AlignRight",
		func() { layout.SetHorizontalAlignment(gxui.AlignRight) },
		func() bool { return layout.HorizontalAlignment().AlignRight() },
	)

	button("AlignTop",
		func() { layout.SetVerticalAlignment(gxui.AlignTop) },
		func() bool { return layout.VerticalAlignment().AlignTop() },
	)
	button("AlignMiddle",
		func() { layout.SetVerticalAlignment(gxui.AlignMiddle) },
		func() bool { return layout.VerticalAlignment().AlignMiddle() },
	)
	button("AlignBottom",
		func() { layout.SetVerticalAlignment(gxui.AlignBottom) },
		func() bool { return layout.VerticalAlignment().AlignBottom() },
	)

	update()

	window := theme.CreateWindow(800, 600, "Linear layout")
	window.SetScale(flags.DefaultScaleFactor)
	window.AddChild(layout)
	window.OnClose(driver.Terminate)
	window.SetPadding(math.Spacing{L: 10, T: 10, R: 10, B: 10})
}