Пример #1
0
func DrawAreaInit(client DrawAreaClient) (v DrawArea, err error) {
	var area *gtk.DrawingArea
	area, err = gtk.DrawingAreaNew()
	if err != nil {
		return
	}
	v = DrawArea{area, 0, 0, 0, 0}
	v.shDraw, err = v.Connect("draw", drawCallback, client)
	if err != nil {
		return
	}
	v.shMotion, err = v.Connect("motion-notify-event", motionCallback, client)
	if err != nil {
		return
	}
	v.shButtonPress, err = v.Connect("button-press-event", buttonCallback, client)
	if err != nil {
		return
	}
	v.shButtonRelease, err = v.Connect("button-release-event", buttonCallback, client)
	if err != nil {
		return
	}
	v.SetEvents(int(gdk.POINTER_MOTION_MASK | gdk.BUTTON_PRESS_MASK | gdk.BUTTON_RELEASE_MASK))
	return
}
Пример #2
0
func addGraphicalActions() {
	r := state.v2result
	side := r.SquareSide
	cells := make([]int, 1+side*side)
	areaSize := 4 + side*w.cellSize.GetValueAsInt()
	tail := w.fieldBuf.GetEndIter()
	for i := 0; i < len(r.Actions); i++ {
		for k, cell := range cells {
			if cell != cellEmpty && cell != cellOccupied {
				cells[k]--
			}
		}

		cells[r.Actions[i]] = cellCurrent

		if i+1 < len(r.Actions) && r.Actions[i+1] < 0 {
			i++
			cells[-r.Actions[i]] = cellMerged
		}

		area, err := gtk.DrawingAreaNew()
		e.Exit(err)
		area.SetSizeRequest(areaSize, areaSize)
		setStyle(area, "borderSolid")
		areaCells := make([]int, len(cells))
		copy(areaCells, cells)
		area.Connect("draw", func(area *gtk.DrawingArea, cr *cairo.Context) {
			drawTable(area, cr, side, areaCells)
		})
		w.field.AddChildAtAnchor(area, w.fieldBuf.CreateChildAnchor(tail))
	}
}
Пример #3
0
func main() {
	gtk.Init(nil)

	// gui boilerplate
	win, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
	da, _ := gtk.DrawingAreaNew()
	win.Add(da)
	win.SetTitle("Arrow keys")
	win.Connect("destroy", gtk.MainQuit)
	win.ShowAll()

	// Data
	unitSize := 20.0
	x := 0.0
	y := 0.0
	keyMap := map[uint]func(){
		KEY_LEFT:  func() { x-- },
		KEY_UP:    func() { y-- },
		KEY_RIGHT: func() { x++ },
		KEY_DOWN:  func() { y++ },
	}

	// Event handlers
	da.Connect("draw", func(da *gtk.DrawingArea, cr *cairo.Context) {
		cr.SetSourceRGB(0, 0, 0)
		cr.Rectangle(x*unitSize, y*unitSize, unitSize, unitSize)
		cr.Fill()
	})
	win.Connect("key-press-event", func(win *gtk.Window, ev *gdk.Event) {
		keyEvent := &gdk.EventKey{ev}
		if move, found := keyMap[keyEvent.KeyVal()]; found {
			move()
			win.QueueDraw()
		}
	})

	gtk.Main()
}
Пример #4
0
// Initialize the game widgets
func (self *MainWindow) InitializeWidgets() {

	self.BoardView, self.err = gtk.DrawingAreaNew()
	self.MainArea, self.err = gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 1)
	self.ScoreArea, self.err = gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 2)
	self.BoardEventBox, self.err = gtk.EventBoxNew()
	self.Scores, self.err = gtk.LabelNew("Scores:")
	self.BlacksScore, self.err = gtk.LabelNew("Black pieces:")
	self.RedsScore, self.err = gtk.LabelNew("Black pieces:")
	self.BoardWidth = 640
	self.BoardHeight = 480
	self.BoardEventBox.Add(self.BoardView)
	if self.err != nil {
		log.Fatal("Failed to draw board")
	}

	self.BoardEventBox.AddEvents(int(gdk.BUTTON_PRESS_MASK))
	self.BoardEventBox.Connect("button_press_event", self.interactBoard)
	self.MainArea.PackStart(self.BoardEventBox, true, true, 10)
	self.MainArea.Add(self.ScoreArea)

	self.Scores.SetMarkup("<strong>%s</strong>")
	self.Scores.SetJustify(gtk.JUSTIFY_CENTER)

	self.ScoreArea.Add(self.Scores)
	self.BlacksScore.SetJustify(gtk.JUSTIFY_LEFT)
	self.RedsScore.SetJustify(gtk.JUSTIFY_LEFT)
	self.ScoreArea.Add(self.BlacksScore)
	self.ScoreArea.Add(self.RedsScore)

	self.Window.Add(self.MainArea)
	self.setBoardSize(8)
	self.BoardView.Connect("draw", self.drawBoard)
	self.Board.RenderText()

	self.Window.SetTitle("Checkers")
}