コード例 #1
0
ファイル: widgets.go プロジェクト: rmoreira/go-ui
func (p *MainWindow) createMyTab() *ui.Widget {
	w := ui.NewWidget()
	vbox := ui.NewVBoxLayout()
	hbox := ui.NewHBoxLayout()
	my := new(MyWidget).Init()
	lbl := ui.NewLabel()
	lbl.SetText("this is custome widget - draw lines")
	btn := ui.NewButton()
	btn.SetText("Clear")
	btn.OnClicked(func() {
		my.Clear()
	})
	hbox.AddWidget(lbl)
	hbox.AddWidgetWith(btn, 0, ui.AlignRight)
	vbox.AddLayout(hbox)
	vbox.AddWidgetWith(my, 1, 0)
	w.SetLayout(vbox)
	return w
}
コード例 #2
0
ファイル: numpla.go プロジェクト: titoi2/NumberPlace
func ui_main() {
	w := ui.NewWidget()
	defer w.Close()
	w.SetWindowTitle("Number Place")

	place := ui.NewWidget()
	place.SetMinimumSize(ui.Sz(CELL_WIDTH*10, CELL_WIDTH*10))

	img := ui.NewImageWithSize(CELL_WIDTH*9, CELL_WIDTH*9)
	defer img.Close()

	imgPainter := ui.NewPainterWithImage(img)
	imgPainter.InitFrom(place)
	defer imgPainter.Close()

	place.OnPaintEvent(func(ev *ui.PaintEvent) {
		drawPlace(ev, place)
	})

	vbox := ui.NewVBoxLayout()

	hbox0 := ui.NewHBoxLayout()
	hbox := ui.NewHBoxLayout()
	hboxSelNum := ui.NewHBoxLayout()
	hbox7 := ui.NewHBoxLayout()
	hbox4 := ui.NewHBoxLayout()
	hbox1 := ui.NewHBoxLayout()

	vbox.AddWidget(place)
	vbox.AddLayout(hboxSelNum)
	vbox.AddLayout(hbox7)
	vbox.AddLayout(hbox4)
	vbox.AddLayout(hbox1)
	vbox.AddLayout(hbox0)
	vbox.AddLayout(hbox)

	lblSelectedNum := ui.NewLabelWithText(LBL_SELECTED)
	hboxSelNum.AddWidget(lblSelectedNum)

	Btn1 := ui.NewButtonWithText("1")
	Btn2 := ui.NewButtonWithText("2")
	Btn3 := ui.NewButtonWithText("3")
	Btn4 := ui.NewButtonWithText("4")
	Btn5 := ui.NewButtonWithText("5")
	Btn6 := ui.NewButtonWithText("6")
	Btn7 := ui.NewButtonWithText("7")
	Btn8 := ui.NewButtonWithText("8")
	Btn9 := ui.NewButtonWithText("9")
	BtnCLR := ui.NewButtonWithText("CLR")
	BtnReset := ui.NewButtonWithText("Reset")
	exitBtn := ui.NewButtonWithText("Exit")
	hbox7.AddWidget(Btn7)
	hbox7.AddWidget(Btn8)
	hbox7.AddWidget(Btn9)
	hbox7.AddStretch(0)
	hbox4.AddWidget(Btn4)
	hbox4.AddWidget(Btn5)
	hbox4.AddWidget(Btn6)
	hbox4.AddStretch(0)
	hbox1.AddWidget(Btn1)
	hbox1.AddWidget(Btn2)
	hbox1.AddWidget(Btn3)
	hbox1.AddStretch(0)

	hbox.AddSpacing(10)

	hbox0.AddWidget(BtnCLR)
	hbox0.AddStretch(0)
	hbox.AddStretch(0)
	hbox.AddWidget(BtnReset)
	hbox.AddWidget(exitBtn)

	selectdNumUpdate := func() {
		s := ""
		if selectedNum > 0 {
			s = strconv.Itoa(selectedNum)
		}
		lblSelectedNum.SetText(LBL_SELECTED + s)
	}

	place.OnMousePressEvent(func(ev *ui.MouseEvent) {
		pos := ev.Pos()
		fmt.Println("pos x", pos.X)
		fmt.Println("pos y", pos.Y)
		var ix int = pos.X / CELL_WIDTH
		var iy int = pos.Y / CELL_WIDTH
		if ix < 9 && iy < 9 {
			cursorx = ix
			cursory = iy
			if placeCells[iy][ix].kind != CELL_KIND_FIXED && placeCells[iy][ix].num == 0 {
				placeCells[iy][ix].num = selectedNum
			} else {
				selectedNum = placeCells[iy][ix].num
			}
			placeCheck()
			place.Update()
			selectdNumUpdate()
		}
	})

	numBtnFunc := func(n int) {
		if cursory == -1 {
			return
		}
		if placeCells[cursory][cursorx].kind != CELL_KIND_FIXED {
			placeCells[cursory][cursorx].num = n
			selectedNum = n
			placeCheck()
			place.Update()
			selectdNumUpdate()
		}
	}

	Btn1.OnClicked(func() {
		numBtnFunc(1)
	})
	Btn2.OnClicked(func() {
		numBtnFunc(2)
	})
	Btn3.OnClicked(func() {
		numBtnFunc(3)
	})
	Btn4.OnClicked(func() {
		numBtnFunc(4)
	})
	Btn5.OnClicked(func() {
		numBtnFunc(5)
	})
	Btn6.OnClicked(func() {
		numBtnFunc(6)
	})
	Btn7.OnClicked(func() {
		numBtnFunc(7)
	})
	Btn8.OnClicked(func() {
		numBtnFunc(8)
	})
	Btn9.OnClicked(func() {
		numBtnFunc(9)
	})

	BtnCLR.OnClicked(func() {
		numBtnFunc(0)
	})

	BtnReset.OnClicked(func() {
		selectedNum = 0
		selectdNumUpdate()
		for iy := 0; iy < 9; iy++ {
			for ix := 0; ix < 9; ix++ {
				if placeCells[iy][ix].kind != CELL_KIND_FIXED {
					placeCells[iy][ix].kind = CELL_KIND_NORMAL
					placeCells[iy][ix].num = 0
				}
			}
		}
		place.Update()
	})

	exitBtn.OnClicked(func() {
		w.Close()
	})

	w.SetLayout(vbox)
	w.Show()

	w.OnCloseEvent(func(e *ui.CloseEvent) {
	})

	ui.Run()
}