/* * Erzeugt die ganzen Events für das drawing area mittels lambdas. * * Parameter: * drawingarea - Die drawing area auf der gezeichnet werden soll * * Zu den Schnittstellenkommentaren: Leider gibts das anscheinend nicht direkt * in go (jedenfall kein @param etc.) :/ */ func createEvents(drawingarea *gtk.DrawingArea) { // Wir brauchen ne pixmap in der wir die Pixeldaten speichern var pixmap *gdk.Pixmap // gdk.GC ist einfach eine Sammlung an Eigenschaften und Einstellungen zum zeichnen var gc *gdk.GC p := point{x: -1, y: -1} drawingarea.Connect("configure-event", func() { // wenns schon ne pixmap gibt, lösche diese if pixmap != nil { pixmap.Unref() } // hole pixmap und stuff allocation := drawingarea.GetAllocation() pixmap = gdk.NewPixmap(drawingarea.GetWindow().GetDrawable(), allocation.Width, allocation.Height, 24) // weißen Hintergrund zeichnen: gc = gdk.NewGC(pixmap.GetDrawable()) gc.SetRgbFgColor(gdk.NewColor("white")) pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1) // Vorder- und Hintergrundfarbe setzen gc.SetRgbFgColor(gdk.NewColor("black")) gc.SetRgbBgColor(gdk.NewColor("red")) }) // dieses event wird ausgeführt wenn auf das widget geklickt wurde drawingarea.Connect("button-press-event", func(ctx *glib.CallbackContext) { // Argumente holen arg := ctx.Args(0) // irgend son pointer auf ein objekt holen in dem mehr Infos stehen mev := *(**gdk.EventMotion)(unsafe.Pointer(&arg)) // Position ausgeben fmt.Println("Geklickte Position: ", p) // hier ist meine eigene Logik: // Wenn noch kein Klick registriert wurde ist p.x == p.y == -1 und da soll noch nichts gezeichnet werden // erst wenn der zweite klick kommt soll zwischen diesem und dem alten eine Linie gezeichnet werden p_neu := point{x: int(mev.X), y: int(mev.Y)} if p.x != -1 && p.y != -1 { pixmap.GetDrawable().DrawLine(gc, p.x, p.y, p_neu.x, p_neu.y) drawingarea.GetWindow().Invalidate(nil, false) } // Position des neuen klicks speichern, es ist also der neue startpunkt der nächsten linie p = p_neu }) // dieses event wird ausgeführt wenn drawingarea.GetWindow().Invalidate(nil, false) aufgerufen wird drawingarea.Connect("expose-event", func() { if pixmap != nil { drawingarea.GetWindow().GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1) } }) // Events müssen manuell spezifiziert werden. // Hier werden immer MASKs übergeben, hier also eben die BUTTON_PRESS_MASK, // welche erst in einen int gecastet werden muss drawingarea.SetEvents(int(gdk.BUTTON_PRESS_MASK)) }
func NewGraph() *Graph { graph := &Graph{*gtk.NewDrawingArea(), nil, nil, -20.0, 20.0, -2.0, 2.0, 1, 10.0, 0.1, 1.0, nil, nil} graph.Connect("expose_event", func() { if graph.pixmap != nil { graph.GetWindow().GetDrawable().DrawDrawable(graph.gc, graph.pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1) } }) graph.Connect("configure_event", func(ctx *glib.CallbackContext) { if graph.pixmap != nil { graph.pixmap.Unref() } allocation := graph.GetAllocation() graph.pixmap = gdk.NewPixmap(graph.GetWindow().GetDrawable(), allocation.Width, allocation.Height, -1) graph.gc = gdk.NewGC(graph.pixmap.GetDrawable()) graph.setScale() graph.plot() }) graph.SetEvents(int(gdk.EXPOSE | gdk.CONFIGURE)) return graph }
func (rw *RegionWrapper) SetBiomeAt(x, z int, bio mcmap.Biome) { cx, cz, bx, bz := mcmap.BlockToChunk(x, z) pos := XZPos{cx, cz} chunk, err := rw.region.Chunk(cx, cz) switch err { case nil: case mcmap.NotAvailable: return default: rw.guicbs.reportFail(fmt.Sprintf("Error while getting chunk %d, %d: %s", cx, cz, err)) return } chunk.SetBiome(bx, bz, bio) var newcol *gdk.Color if rw.fixSnowIce { newcol = rw.fixWeather(bio, bx, bz, chunk) } chunk.MarkModified() // Update cache if bc, ok := rw.bioCache[pos]; ok { bc[bz*mcmap.ChunkSizeXZ+bx] = bio } // Update tile if biotile, ok := rw.Biotiles[pos]; ok { gdk.ThreadsEnter() drawable := biotile.GetDrawable() gc := gdk.NewGC(drawable) gc.SetRgbFgColor(rw.bioLookup.Color(bio)) drawable.DrawRectangle(gc, true, bx*zoom, bz*zoom, zoom, zoom) if newcol != nil { drawable = rw.Maptiles[pos].GetDrawable() gc = gdk.NewGC(drawable) gc.SetRgbFgColor(newcol) drawable.DrawRectangle(gc, true, bx*zoom, bz*zoom, zoom, zoom) } gdk.ThreadsLeave() } }
func configure_board(vbox *gtk.VBox) { drawingarea = gtk.NewDrawingArea() drawingarea.Connect("configure-event", func() { if pixmap != nil { pixmap.Unref() } var allocation gtk.Allocation drawingarea.GetAllocation(&allocation) pixmap = gdk.NewPixmap(drawingarea.GetWindow().GetDrawable(), allocation.Width, allocation.Height, 24) gc = gdk.NewGC(pixmap.GetDrawable()) display_init_grid(gc, pixmap) }) drawingarea.Connect("button-press-event", func(ctx *glib.CallbackContext) { // Check if the game is running and if player click in the goban if stopGame == true || stopClick == true { return } if gdkwin == nil { gdkwin = drawingarea.GetWindow() } arg := ctx.Args(0) mev := *(**gdk.EventMotion)(unsafe.Pointer(&arg)) var mt gdk.ModifierType var x, y int if mev.IsHint != 0 { gdkwin.GetPointer(&x, &y, &mt) } else { x, y = int(mev.X), int(mev.Y) } x = ((x - INTER/2) / INTER) y = ((y - INTER/2) / INTER) if x < 0 || x >= 19 || y < 0 || y >= 19 { return } // end check good, vic := event_play(x, y) if good && iamode && stopGame == false && stopClick == false && vic == 0 { calc_ai() } if good && !iamode && stopGame == false && stopClick == false && hint { calcHint <- true } }) drawingarea.Connect("expose-event", func() { if pixmap != nil { drawingarea.GetWindow().GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1) } }) drawingarea.SetEvents(int(gdk.POINTER_MOTION_MASK | gdk.POINTER_MOTION_HINT_MASK | gdk.BUTTON_PRESS_MASK)) vbox.Add(drawingarea) }
func (rw *RegionWrapper) renderTile(chunk *mcmap.Chunk) (maptile, biotile *gdk.Pixmap, biocache []mcmap.Biome) { maptile = emptyPixmap(tileSize, tileSize, 24) mtDrawable := maptile.GetDrawable() mtGC := gdk.NewGC(mtDrawable) biotile = emptyPixmap(tileSize, tileSize, 24) btDrawable := biotile.GetDrawable() btGC := gdk.NewGC(btDrawable) biocache = make([]mcmap.Biome, mcmap.ChunkRectXZ) i := 0 for z := 0; z < mcmap.ChunkSizeXZ; z++ { scanX: for x := 0; x < mcmap.ChunkSizeXZ; x++ { bio := chunk.Biome(x, z) btGC.SetRgbFgColor(rw.bioLookup.Color(bio)) btDrawable.DrawRectangle(btGC, true, x*zoom, z*zoom, zoom, zoom) biocache[i] = bio i++ for y := chunk.Height(x, z); y >= 0; y-- { if col, ok := blockColors[chunk.Block(x, y, z).ID]; ok { mtGC.SetRgbFgColor(col) mtDrawable.DrawRectangle(mtGC, true, x*zoom, z*zoom, zoom, zoom) continue scanX } } mtGC.SetRgbFgColor(gdk.NewColor("#ffffff")) mtDrawable.DrawRectangle(mtGC, true, x*zoom, z*zoom, zoom, zoom) } } return }
func (mw *MapWidget) configure() { if mw.pixmap != nil { mw.pixmap.Unref() } alloc := mw.dArea.GetAllocation() mw.w = alloc.Width mw.h = alloc.Height if !mw.isInit { mw.offX = -(mw.w / 2) mw.offZ = -(mw.h / 2) mw.isInit = true } mw.updateChunkBounds() mw.pixmap = gdk.NewPixmap(mw.dArea.GetWindow().GetDrawable(), mw.w, mw.h, 24) mw.pixmapGC = gdk.NewGC(mw.pixmap.GetDrawable()) mw.drawBg() mw.updateGUI() }
func (mw *MapWidget) drawBg() { if mw.bg != nil { mw.bg.Unref() } mw.bg = emptyPixmap(mw.w, mw.h, 24) drawable := mw.bg.GetDrawable() gc := gdk.NewGC(drawable) w := int(math.Ceil(float64(mw.w) / 32)) h := int(math.Ceil(float64(mw.h) / 32)) for y := 0; y < h; y++ { for x := 0; x < w; x++ { if (x % 2) == (y % 2) { gc.SetRgbFgColor(checker1) } else { gc.SetRgbFgColor(checker2) } drawable.DrawRectangle(gc, true, x*32, y*32, 32, 32) } } }
func colorBox(c *gdk.Color) *gtk.DrawingArea { dArea := gtk.NewDrawingArea() var pixmap *gdk.Pixmap var gc *gdk.GC dArea.Connect("configure-event", func() { if pixmap != nil { pixmap.Unref() } alloc := dArea.GetAllocation() pixmap = gdk.NewPixmap(dArea.GetWindow().GetDrawable(), alloc.Width, alloc.Height, 24) gc = gdk.NewGC(pixmap.GetDrawable()) gc.SetRgbFgColor(c) pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1) }) dArea.Connect("expose-event", func() { if pixmap != nil { dArea.GetWindow().GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1) } }) return dArea }
func Init(title string) { gtk.Init(nil) window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) window.SetPosition(gtk.WIN_POS_CENTER) window.SetTitle(title) window.SetIconName("gtk-dialog-info") window.Connect("destroy", func(ctx *glib.CallbackContext) { fmt.Println("got destroy!", ctx.Data().(string)) gtk.MainQuit() }, "foo") //-------------------------------------------------------- // GtkVBox //-------------------------------------------------------- vbox := gtk.NewVBox(false, 1) //-------------------------------------------------------- // GtkMenuBar //-------------------------------------------------------- menubar := gtk.NewMenuBar() vbox.PackStart(menubar, false, false, 0) //-------------------------------------------------------- // GtkDrawable //-------------------------------------------------------- drawingarea := gtk.NewDrawingArea() //var gdkwin *gdk.Window var pixmap *gdk.Pixmap var gc *gdk.GC drawingarea.Connect("configure-event", func() { println("Configuring drawingArea!") if pixmap != nil { pixmap.Unref() } allocation := drawingarea.GetAllocation() pixmap = gdk.NewPixmap(drawingarea.GetWindow().GetDrawable(), allocation.Width, allocation.Height, 24) gc = gdk.NewGC(pixmap.GetDrawable()) gc.SetRgbFgColor(gdk.NewColor("white")) pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1) gc.SetRgbFgColor(gdk.NewColor("black")) gc.SetRgbBgColor(gdk.NewColor("white")) pixmap.GetDrawable().DrawRectangle(gc, false, 0, 0, 10, 10) }) drawingarea.Connect("expose-event", func() { println("Exposing DrawingArea!") if pixmap != nil { drawingarea.GetWindow().GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1) } }) vbox.Add(drawingarea) //-------------------------------------------------------- // GtkScale //-------------------------------------------------------- scale := gtk.NewHScaleWithRange(0, 100, 1) scale.Connect("value-changed", func() { //fmt.Println("scale:", int(scale.GetValue())) }) vbox.Add(scale) window.Add(vbox) window.SetSizeRequest(600, 600) window.ShowAll() gtk.Main() }
func main() { gtk.Init(&os.Args) window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) window.SetTitle("GTK DrawingArea") window.Connect("destroy", gtk.MainQuit) vbox := gtk.NewVBox(true, 0) vbox.SetBorderWidth(5) drawingarea := gtk.NewDrawingArea() var p1, p2 point var gdkwin *gdk.Window var pixmap *gdk.Pixmap var gc *gdk.GC p1.x = -1 p1.y = -1 colors := []string{ "black", "gray", "blue", "purple", "red", "orange", "yellow", "green", "darkgreen", } drawingarea.Connect("configure-event", func() { if pixmap != nil { pixmap.Unref() } allocation := drawingarea.GetAllocation() pixmap = gdk.NewPixmap(drawingarea.GetWindow().GetDrawable(), allocation.Width, allocation.Height, 24) gc = gdk.NewGC(pixmap.GetDrawable()) gc.SetRgbFgColor(gdk.NewColor("white")) pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1) gc.SetRgbFgColor(gdk.NewColor(colors[0])) gc.SetRgbBgColor(gdk.NewColor("white")) }) drawingarea.Connect("motion-notify-event", func(ctx *glib.CallbackContext) { arg := ctx.Args(0) mev := *(**gdk.EventMotion)(unsafe.Pointer(&arg)) var mt gdk.ModifierType if mev.IsHint != 0 { gdkwin.GetPointer(&p2.x, &p2.y, &mt) } else { p2.x, p2.y = int(mev.X), int(mev.Y) } if p1.x != -1 && p2.x != -1 && (gdk.EventMask(mt)&gdk.BUTTON_PRESS_MASK) != 0 { pixmap.GetDrawable().DrawLine(gc, p1.x, p1.y, p2.x, p2.y) gdkwin.Invalidate(nil, false) } colors = append(colors[1:], colors[0]) gc.SetRgbFgColor(gdk.NewColor(colors[0])) p1 = p2 }) drawingarea.Connect("expose-event", func() { if pixmap == nil { return } gdkwin.GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1) }) drawingarea.SetEvents(int(gdk.POINTER_MOTION_MASK | gdk.POINTER_MOTION_HINT_MASK | gdk.BUTTON_PRESS_MASK)) vbox.Add(drawingarea) window.Add(vbox) window.SetSizeRequest(400, 400) window.ShowAll() gdkwin = drawingarea.GetWindow() gtk.Main() }
func main() { var autoupdating bool = false var autoticker *time.Ticker var entries [][]*gtk.Entry = make([][]*gtk.Entry, entitylimit) for i := 0; i < entitylimit; i++ { entries[i] = make([]*gtk.Entry, entityfields) } var entities []*physics.Entity = initentities(entries) // Initialize gtk gtk.Init(nil) // WINDOW window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) window.SetPosition(gtk.WIN_POS_CENTER) window.SetTitle("Gravity Visualization") // Connect top window closing to gtk main loop closing window.Connect("destroy", func(ctx *glib.CallbackContext) { gtk.MainQuit() }) // TOP VERTICAL BOX topvbox := gtk.NewVBox(false, 1) // NOTEBOOK FOR TABS notebook := gtk.NewNotebook() // DRAWING AREA VERTICAL BOX davbox := gtk.NewVBox(false, 1) // DRAWING AREA drawingarea = gtk.NewDrawingArea() drawingarea.SetSizeRequest(width, height) drawingarea.ModifyBG(gtk.STATE_NORMAL, gdk.NewColor("white")) drawingarea.Connect("expose_event", func() { drawentities(entities) }) davbox.PackStart(drawingarea, true, true, 0) // TICK SPEED SLIDER ticksliderhbox := gtk.NewHBox(false, 1) ticksliderlabel := gtk.NewLabel("Time between ticks (ms)") ticksliderhbox.Add(ticksliderlabel) tickslider := gtk.NewHScaleWithRange(1, 1000, 100) // Default value of 10 ms tickslider.SetValue(10) ticksliderhbox.Add(tickslider) davbox.Add(ticksliderhbox) // BUTTONS buttons := gtk.NewHBox(false, 1) // RESET MENU ITEM resetbutton := gtk.NewButtonWithLabel("Reset") resetbutton.Clicked(func() { entities = initentities(entries) drawingarea.QueueDraw() }) buttons.Add(resetbutton) // TICK MENU ITEM tickbutton := gtk.NewButtonWithLabel("Tick") tickbutton.Clicked(func() { updateentities(entities) }) buttons.Add(tickbutton) // AUTOUPDATE MENU ITEM autotickbutton := gtk.NewToggleButtonWithLabel("AutoUpdate") autotickbutton.Clicked(func() { // Stop the previous ticker if it exists if autoticker != nil { autoticker.Stop() } if autoupdating { // Toggle autoupdating state autoupdating = false } else { // Start the ticker autoticker = time.NewTicker(time.Duration(tickslider.GetValue()) * time.Millisecond) // Spawn a goroutine that will run update entities every tick go func() { for _ = range autoticker.C { updateentities(entities) } }() // Toggle autoupdating state autoupdating = true } }) buttons.Add(autotickbutton) davbox.Add(buttons) notebook.AppendPage(davbox, gtk.NewLabel("Simulation")) // INITIALIZE PANEL entitiesvbox := gtk.NewVBox(false, 1) // INITIALIZE LABELS FOR TABLE titles := gtk.NewHBox(false, 1) titles.Add(gtk.NewLabel("Mass")) titles.Add(gtk.NewLabel("X-Pos")) titles.Add(gtk.NewLabel("Y-Pos")) titles.Add(gtk.NewLabel("X-Vel")) titles.Add(gtk.NewLabel("Y-Vel")) titles.Add(gtk.NewLabel("X-Acc")) titles.Add(gtk.NewLabel("Y-Acc")) entitiesvbox.Add(titles) // INITIALIZE ENTRIES IN ROWS FOR TABLE for row := 0; row < entitylimit; row++ { rowbox := gtk.NewHBox(false, 1) for col := 0; col < entityfields; col++ { textfield := gtk.NewEntry() // Hold reference to text field in entries 2d array entries[row][col] = textfield rowbox.Add(textfield) } entitiesvbox.Add(rowbox) } // CLEAR ENTITIES BUTTON clearentitiesbutton := gtk.NewButtonWithLabel("Clear Entries") clearentitiesbutton.Clicked(func() { for row := 0; row < entitylimit; row++ { for col := 0; col < entityfields; col++ { entries[row][col].SetText("") } } }) entitiesvbox.Add(clearentitiesbutton) // Limit the size of the entitiesvbox and add to notebook entitiesvbox.SetSizeRequest(width, height) notebook.AppendPage(entitiesvbox, gtk.NewLabel("Entities")) // FINISH PACKING COMPONENTS topvbox.PackStart(notebook, false, false, 0) // FINISH PACKING WINDOW window.Add(topvbox) // Show the GUI window.ShowAll() // Grab the drawable and initialize graphics context now that they are initialized drawable = drawingarea.GetWindow().GetDrawable() blackgc = gdk.NewGC(drawable) redgc = gdk.NewGC(drawable) redgc.SetRgbFgColor(gdk.NewColorRGB(255, 0, 0)) bluegc = gdk.NewGC(drawable) bluegc.SetRgbFgColor(gdk.NewColorRGB(0, 0, 255)) gtk.Main() }