diff options
-rw-r--r-- | biome_info_editor.go | 415 | ||||
-rw-r--r-- | biome_infos.go | 365 | ||||
-rw-r--r-- | colbuf.go | 17 | ||||
-rw-r--r-- | main.go | 92 | ||||
-rw-r--r-- | mapwidget.go | 20 | ||||
-rw-r--r-- | region_wrapper.go | 24 |
6 files changed, 692 insertions, 241 deletions
diff --git a/biome_info_editor.go b/biome_info_editor.go new file mode 100644 index 0000000..61a1d7e --- /dev/null +++ b/biome_info_editor.go @@ -0,0 +1,415 @@ +package main + +import ( + "fmt" + "github.com/kch42/gomcmap/mcmap" + "github.com/mattn/go-gtk/gdk" + "github.com/mattn/go-gtk/glib" + "github.com/mattn/go-gtk/gtk" + "os" + "strconv" +) + +type biomeEditFrame struct { + *gtk.Frame + applyBtn *gtk.Button + idInput, snowLineInput, nameInput *gtk.Entry + colorInput *gtk.ColorButton + bList *biomeList +} + +func newBiomeEditFrame() *biomeEditFrame { + frm := &biomeEditFrame{ + Frame: gtk.NewFrame("Edit Biome"), + applyBtn: gtk.NewButtonWithLabel("Apply"), + idInput: gtk.NewEntry(), + snowLineInput: gtk.NewEntry(), + nameInput: gtk.NewEntry(), + colorInput: gtk.NewColorButton(), + } + + frm.idInput.SetSizeRequest(40, -1) + frm.snowLineInput.SetSizeRequest(40, -1) + + frm.idInput.Connect("changed", frm.unlockApply) + frm.nameInput.Connect("changed", frm.unlockApply) + frm.snowLineInput.Connect("changed", frm.unlockApply) + frm.applyBtn.SetSensitive(false) + + vbox := gtk.NewVBox(false, 0) + hbox := gtk.NewHBox(false, 0) + + frm.idInput.SetTooltipText("The data value of the Biome [0-255]") + frm.snowLineInput.SetTooltipText(fmt.Sprintf("Height (Y coordinate) at which snowfall starts (-1 or %d for no snowfall, 0 for always snowy)", mcmap.ChunkSizeY)) + + hbox.PackStart(gtk.NewLabel("Color:"), false, false, 0) + hbox.PackStart(frm.colorInput, false, false, 3) + hbox.PackStart(gtk.NewLabel("ID:"), false, false, 0) + hbox.PackStart(frm.idInput, false, false, 3) + hbox.PackStart(gtk.NewLabel("Snowline:"), false, false, 0) + hbox.PackStart(frm.snowLineInput, false, false, 3) + hbox.PackStart(gtk.NewLabel("Name:"), false, false, 0) + hbox.PackStart(frm.nameInput, true, true, 3) + + vbox.PackStart(hbox, false, false, 0) + vbox.PackStart(frm.applyBtn, false, false, 3) + frm.Add(vbox) + + frm.applyBtn.Connect("clicked", frm.doApply) + + return frm +} + +func (frm *biomeEditFrame) setBiomeInfo(info BiomeInfo) { + frm.colorInput.SetColor(gdk.NewColor(info.Color)) + frm.idInput.SetText(strconv.FormatInt(int64(info.ID), 10)) + frm.snowLineInput.SetText(strconv.FormatInt(int64(info.SnowLine), 10)) + frm.nameInput.SetText(info.Name) +} + +func (frm *biomeEditFrame) doApply() { + biome, ok := frm.getBiomeInfo() + if !ok { + return + } + + frm.bList.setCurrentBiome(biome) +} + +func (frm *biomeEditFrame) getBiomeInfo() (BiomeInfo, bool) { + id, err := strconv.ParseUint(frm.idInput.GetText(), 10, 8) + if err != nil { + return BiomeInfo{}, false + } + + snow, err := strconv.ParseInt(frm.snowLineInput.GetText(), 10, 32) + if err != nil { + return BiomeInfo{}, false + } + if (snow > mcmap.ChunkSizeY) || (snow < 0) { + snow = mcmap.ChunkSizeY + } + + name := frm.nameInput.GetText() + if name == "" { + return BiomeInfo{}, false + } + + col := frm.colorInput.GetColor() + + return BiomeInfo{ + ID: mcmap.Biome(id), + SnowLine: int(snow), + Name: name, + Color: fmt.Sprintf("#%02x%02x%02x", col.Red()>>8, col.Green()>>8, col.Blue()>>8), + }, true +} + +func (frm *biomeEditFrame) checkOK() bool { + _, ok := frm.getBiomeInfo() + return ok +} + +func (frm *biomeEditFrame) unlockApply() { + frm.applyBtn.SetSensitive(frm.checkOK()) +} + +type biomeList struct { + *gtk.HBox + treeview *gtk.TreeView + lStore *gtk.ListStore + biomes []BiomeInfo + editfrm *biomeEditFrame + addBtn, delBtn, upBtn, downBtn *gtk.Button +} + +func newBiomeList() *biomeList { + bl := &biomeList{ + HBox: gtk.NewHBox(false, 0), + treeview: gtk.NewTreeView(), + lStore: gtk.NewListStore(glib.G_TYPE_STRING, glib.G_TYPE_STRING, glib.G_TYPE_STRING, glib.G_TYPE_STRING), + addBtn: gtk.NewButton(), + delBtn: gtk.NewButton(), + upBtn: gtk.NewButton(), + downBtn: gtk.NewButton(), + } + + scroll := gtk.NewScrolledWindow(nil, nil) + scroll.SetPolicy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) + scroll.Add(bl.treeview) + bl.PackStart(scroll, true, true, 3) + + bl.treeview.SetModel(bl.lStore) + bl.treeview.AppendColumn(gtk.NewTreeViewColumnWithAttributes("Color", gtk.NewCellRendererText(), "background", 0)) + bl.treeview.AppendColumn(gtk.NewTreeViewColumnWithAttributes("ID", gtk.NewCellRendererText(), "text", 1)) + bl.treeview.AppendColumn(gtk.NewTreeViewColumnWithAttributes("Snowline", gtk.NewCellRendererText(), "text", 2)) + bl.treeview.AppendColumn(gtk.NewTreeViewColumnWithAttributes("Name", gtk.NewCellRendererText(), "text", 3)) + + bl.treeview.GetSelection().SetMode(gtk.SELECTION_SINGLE) + bl.treeview.Connect("cursor-changed", bl.onCursorChanged) + + vbox := gtk.NewVBox(false, 0) + + bl.addBtn.Add(gtk.NewImageFromStock(gtk.STOCK_ADD, gtk.ICON_SIZE_SMALL_TOOLBAR)) + bl.delBtn.Add(gtk.NewImageFromStock(gtk.STOCK_DELETE, gtk.ICON_SIZE_SMALL_TOOLBAR)) + bl.upBtn.Add(gtk.NewImageFromStock(gtk.STOCK_GO_UP, gtk.ICON_SIZE_SMALL_TOOLBAR)) + bl.downBtn.Add(gtk.NewImageFromStock(gtk.STOCK_GO_DOWN, gtk.ICON_SIZE_SMALL_TOOLBAR)) + + bl.addBtn.Connect("clicked", bl.onAdd) + bl.delBtn.Connect("clicked", bl.onDel) + bl.upBtn.Connect("clicked", bl.onUp) + bl.downBtn.Connect("clicked", bl.onDown) + + bl.delBtn.SetSensitive(false) + bl.upBtn.SetSensitive(false) + bl.downBtn.SetSensitive(false) + + vbox.PackStart(bl.addBtn, false, false, 3) + vbox.PackStart(bl.delBtn, false, false, 3) + vbox.PackStart(bl.upBtn, false, false, 3) + vbox.PackStart(bl.downBtn, false, false, 3) + + bl.PackStart(vbox, false, false, 0) + + return bl +} + +func (bl *biomeList) setBiome(iter *gtk.TreeIter, biome BiomeInfo) { + bl.lStore.Set(iter, biome.Color, strconv.FormatInt(int64(biome.ID), 10), strconv.FormatInt(int64(biome.SnowLine), 10), biome.Name) +} + +func (bl *biomeList) setCurrentBiome(biome BiomeInfo) { + idx, iter, _ := bl.treeviewIdx() + if idx < 0 { + return + } + bl.biomes[idx] = biome + bl.setBiome(iter, biome) +} + +func (bl *biomeList) SetBiomes(biomes []BiomeInfo) { + bl.biomes = biomes + + bl.lStore.Clear() + var iter gtk.TreeIter + for _, bio := range biomes { + bl.lStore.Append(&iter) + bl.setBiome(&iter, bio) + } +} + +func (bl *biomeList) Biomes() []BiomeInfo { return bl.biomes } + +func (bl *biomeList) treeviewIdx() (int, *gtk.TreeIter, *gtk.TreePath) { + var path *gtk.TreePath + var column *gtk.TreeViewColumn + bl.treeview.GetCursor(&path, &column) + + idxs := path.GetIndices() + if len(idxs) != 1 { + return -1, nil, nil + } + var iter gtk.TreeIter + bl.lStore.GetIter(&iter, path) + + return idxs[0], &iter, path +} + +func (bl *biomeList) onCursorChanged() { + idx, _, _ := bl.treeviewIdx() + + bl.delBtn.SetSensitive(idx >= 0) + bl.upBtn.SetSensitive(idx >= 1) + bl.downBtn.SetSensitive((idx >= 0) && (idx < len(bl.biomes)-1)) + + if idx >= 0 { + bl.editfrm.setBiomeInfo(bl.biomes[idx]) + } +} + +func (bl *biomeList) onAdd() { + bio := BiomeInfo{ + Color: "#000000", + ID: 0, + SnowLine: 255, + Name: "(new)", + } + bl.biomes = append(bl.biomes, bio) + + var iter gtk.TreeIter + bl.lStore.Append(&iter) + bl.setBiome(&iter, bio) + path := gtk.NewTreePath() + path.AppendIndex(len(bl.biomes) - 1) + bl.treeview.SetCursor(path, nil, false) +} + +func (bl *biomeList) onDel() { + idx, iter, path := bl.treeviewIdx() + if idx < 0 { + return + } + + copy(bl.biomes[idx:], bl.biomes[idx+1:]) + bl.biomes = bl.biomes[:len(bl.biomes)-1] + + bl.lStore.Remove(iter) + if len(bl.biomes) > 0 { + bl.treeview.SetCursor(path, nil, false) + } +} +func (bl *biomeList) onUp() { + idx, iter, path := bl.treeviewIdx() + if idx <= 0 { + return + } + + path.Prev() + var iter2 gtk.TreeIter + bl.lStore.GetIter(&iter2, path) + + bl.lStore.MoveBefore(iter, &iter2) + + bl.biomes[idx], bl.biomes[idx-1] = bl.biomes[idx-1], bl.biomes[idx] +} + +func (bl *biomeList) onDown() { + idx, iter, path := bl.treeviewIdx() + if (idx < 0) || (idx >= len(bl.biomes)-1) { + return + } + + path.Next() + var iter2 gtk.TreeIter + bl.lStore.GetIter(&iter2, path) + + bl.lStore.MoveAfter(iter, &iter2) + + bl.biomes[idx], bl.biomes[idx+1] = bl.biomes[idx+1], bl.biomes[idx] +} + +func connectBiomeListEditFrame(bl *biomeList, frm *biomeEditFrame) { + bl.editfrm = frm + frm.bList = bl +} + +type BiomeInfoEditor struct { + *gtk.Dialog + biolist *biomeList +} + +func NewBiomeInfoEditor(biomes []BiomeInfo) *BiomeInfoEditor { + ed := &BiomeInfoEditor{ + Dialog: gtk.NewDialog(), + biolist: newBiomeList(), + } + + ed.SetModal(true) + + vbox := ed.GetVBox() + + btnHBox := gtk.NewHBox(true, 0) + + resetBtn := gtk.NewButtonWithLabel("Reset to defaults") + resetBtn.Connect("clicked", ed.reset) + loadBtn := gtk.NewButtonWithLabel("Load from file ...") + loadBtn.Connect("clicked", ed.load) + saveBtn := gtk.NewButtonWithLabel("Save to file ...") + saveBtn.Connect("clicked", ed.save) + + btnHBox.PackStart(resetBtn, true, true, 3) + btnHBox.PackStart(loadBtn, true, true, 3) + btnHBox.PackStart(saveBtn, true, true, 3) + vbox.PackStart(btnHBox, false, false, 3) + + ed.biolist.SetBiomes(biomes) + vbox.PackStart(ed.biolist, true, true, 3) + + editFrame := newBiomeEditFrame() + connectBiomeListEditFrame(ed.biolist, editFrame) + vbox.PackStart(editFrame, false, false, 3) + + ed.AddButton("Cancel", gtk.RESPONSE_CANCEL) + ed.AddButton("OK", gtk.RESPONSE_OK) + ed.ShowAll() + return ed +} + +func (ed *BiomeInfoEditor) reset() { + ed.biolist.SetBiomes(ReadDefaultBiomes()) +} + +func mkBioFFilters() (*gtk.FileFilter, *gtk.FileFilter) { + f1 := gtk.NewFileFilter() + f1.AddPattern("*.biomes") + f1.SetName("Biome Infos (.biomes)") + + f2 := gtk.NewFileFilter() + f2.AddPattern("*") + f2.SetName("All files") + + return f1, f2 +} + +func errdlg(msg string, params ...interface{}) { + dlg := gtk.NewMessageDialog(nil, gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, msg, params...) + dlg.Run() + dlg.Destroy() +} + +func (ed *BiomeInfoEditor) load() { + dlg := gtk.NewFileChooserDialog("Load", nil, gtk.FILE_CHOOSER_ACTION_OPEN, "OK", gtk.RESPONSE_OK, "Cancel", gtk.RESPONSE_CANCEL) + defer dlg.Destroy() +askFilename: + if dlg.Run() == gtk.RESPONSE_OK { + path := dlg.GetFilename() + + f, err := os.Open(path) + if err != nil { + errdlg("Could not load biome infos %s:\n%s", path, err.Error()) + goto askFilename + } + defer f.Close() + + infos, err := ReadBiomeInfos(f) + if err != nil { + errdlg("Could not load biome infos %s:\n%s", path, err.Error()) + goto askFilename + } + + ed.biolist.SetBiomes(infos) + } +} + +func (ed *BiomeInfoEditor) save() { + dlg := gtk.NewFileChooserDialog("Save", nil, gtk.FILE_CHOOSER_ACTION_SAVE, "OK", gtk.RESPONSE_OK, "Cancel", gtk.RESPONSE_CANCEL) + defer dlg.Destroy() +askFilename: + if dlg.Run() == gtk.RESPONSE_OK { + path := dlg.GetFilename() + + if _, err := os.Stat(path); err == nil { + qdlg := gtk.NewMessageDialog(nil, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, "File %s already exists. Overwrite?", path) + resp := qdlg.Run() + qdlg.Destroy() + + if resp != gtk.RESPONSE_YES { + goto askFilename + } + } + + f, err := os.Create(path) + if err != nil { + errdlg("Could not save biome infos %s:\n%s", path, err.Error()) + goto askFilename + } + defer f.Close() + + if err := WriteBiomeInfos(ed.biolist.Biomes(), f); err != nil { + errdlg("Could not save biome infos %s:\n%s", path, err.Error()) + goto askFilename + } + } +} + +func (ed *BiomeInfoEditor) Biomes() []BiomeInfo { return ed.biolist.Biomes() } diff --git a/biome_infos.go b/biome_infos.go index c45294c..4c137af 100644 --- a/biome_infos.go +++ b/biome_infos.go @@ -1,206 +1,183 @@ package main import ( + "bufio" + "fmt" "github.com/kch42/gomcmap/mcmap" "github.com/mattn/go-gtk/gdk" + "io" + "strconv" + "strings" ) -var bioList = []mcmap.Biome{ - mcmap.BioOcean, - mcmap.BioPlains, - mcmap.BioDesert, - mcmap.BioExtremeHills, - mcmap.BioForest, - mcmap.BioTaiga, - mcmap.BioSwampland, - mcmap.BioRiver, - mcmap.BioHell, - mcmap.BioSky, - mcmap.BioFrozenOcean, - mcmap.BioFrozenRiver, - mcmap.BioIcePlains, - mcmap.BioIceMountains, - mcmap.BioMushroomIsland, - mcmap.BioMushroomIslandShore, - mcmap.BioBeach, - mcmap.BioDesertHills, - mcmap.BioForestHills, - mcmap.BioTaigaHills, - mcmap.BioExtremeHillsEdge, - mcmap.BioJungle, - mcmap.BioJungleHills, - mcmap.BioJungleEdge, - mcmap.BioDeepOcean, - mcmap.BioStoneBeach, - mcmap.BioColdBeach, - mcmap.BioBirchForest, - mcmap.BioBirchForestHills, - mcmap.BioRoofedForest, - mcmap.BioColdTaiga, - mcmap.BioColdTaigaHills, - mcmap.BioMegaTaiga, - mcmap.BioMegaTaigaHills, - mcmap.BioExtremeHillsPlus, - mcmap.BioSavanna, - mcmap.BioSavannaPlateau, - mcmap.BioMesa, - mcmap.BioMesaPlateauF, - mcmap.BioMesaPlateau, - mcmap.BioSunflowerPlains, - mcmap.BioDesertM, - mcmap.BioExtremeHillsM, - mcmap.BioFlowerForest, - mcmap.BioTaigaM, - mcmap.BioSwamplandM, - mcmap.BioIcePlainsSpikes, - mcmap.BioIceMountainsSpikes, - mcmap.BioJungleM, - mcmap.BioJungleEdgeM, - mcmap.BioBirchForestM, - mcmap.BioBirchForestHillsM, - mcmap.BioRoofedForestM, - mcmap.BioColdTaigaM, - mcmap.BioMegaSpruceTaiga, - mcmap.BioMegaSpruceTaigaHills, - mcmap.BioExtremeHillsPlusM, - mcmap.BioSavannaM, - mcmap.BioSavannaPlateauM, - mcmap.BioMesaBryce, - mcmap.BioMesaPlateauFM, - mcmap.BioMesaPlateauM, - mcmap.BioUncalculated, +type BiomeInfo struct { + ID mcmap.Biome + SnowLine int + Color string + Name string } -var bioColors = map[mcmap.Biome]*gdk.Color{ - mcmap.BioOcean: gdk.NewColor("#0000ff"), - mcmap.BioPlains: gdk.NewColor("#9fe804"), - mcmap.BioDesert: gdk.NewColor("#f5ff58"), - mcmap.BioExtremeHills: gdk.NewColor("#a75300"), - mcmap.BioForest: gdk.NewColor("#006f2a"), - mcmap.BioTaiga: gdk.NewColor("#05795a"), - mcmap.BioSwampland: gdk.NewColor("#6a7905"), - mcmap.BioRiver: gdk.NewColor("#196eff"), - mcmap.BioHell: gdk.NewColor("#d71900"), - mcmap.BioSky: gdk.NewColor("#871eb3"), - mcmap.BioFrozenOcean: gdk.NewColor("#d6f0ff"), - mcmap.BioFrozenRiver: gdk.NewColor("#8fb6cd"), - mcmap.BioIcePlains: gdk.NewColor("#fbfbfb"), - mcmap.BioIceMountains: gdk.NewColor("#c6bfb1"), - mcmap.BioMushroomIsland: gdk.NewColor("#9776a4"), - mcmap.BioMushroomIslandShore: gdk.NewColor("#9e8ebc"), - mcmap.BioBeach: gdk.NewColor("#fffdc9"), - mcmap.BioDesertHills: gdk.NewColor("#adb354"), - mcmap.BioForestHills: gdk.NewColor("#40694f"), - mcmap.BioTaigaHills: gdk.NewColor("#5b8578"), - mcmap.BioExtremeHillsEdge: gdk.NewColor("#a77748"), - mcmap.BioJungle: gdk.NewColor("#22db04"), - mcmap.BioJungleHills: gdk.NewColor("#63bf54"), - mcmap.BioJungleEdge: gdk.NewColor("#40ba2c"), - mcmap.BioDeepOcean: gdk.NewColor("#0000b3"), - mcmap.BioStoneBeach: gdk.NewColor("#9292a6"), - mcmap.BioColdBeach: gdk.NewColor("#c7c7e8"), - mcmap.BioBirchForest: gdk.NewColor("#1d964b"), - mcmap.BioBirchForestHills: gdk.NewColor("#498045"), - mcmap.BioRoofedForest: gdk.NewColor("#075a26"), - mcmap.BioColdTaiga: gdk.NewColor("#1b948e"), - mcmap.BioColdTaigaHills: gdk.NewColor("#1d7a76"), - mcmap.BioMegaTaiga: gdk.NewColor("#1f8f68"), - mcmap.BioMegaTaigaHills: gdk.NewColor("#217a5c"), - mcmap.BioExtremeHillsPlus: gdk.NewColor("#d76a00"), - mcmap.BioSavanna: gdk.NewColor("#b2bc0f"), - mcmap.BioSavannaPlateau: gdk.NewColor("#aba60e"), - mcmap.BioMesa: gdk.NewColor("#ff6c00"), - mcmap.BioMesaPlateauF: gdk.NewColor("#d9691e"), - mcmap.BioMesaPlateau: gdk.NewColor("#d95b07"), - mcmap.BioSunflowerPlains: gdk.NewColor("#ffd504"), - mcmap.BioDesertM: gdk.NewColor("#f4ff3f"), - mcmap.BioExtremeHillsM: gdk.NewColor("#8c4500"), - mcmap.BioFlowerForest: gdk.NewColor("#e02f4a"), - mcmap.BioTaigaM: gdk.NewColor("#0a6148"), - mcmap.BioSwamplandM: gdk.NewColor("#58630e"), - mcmap.BioIcePlainsSpikes: gdk.NewColor("#ace8e8"), - mcmap.BioIceMountainsSpikes: gdk.NewColor("#91cccc"), - mcmap.BioJungleM: gdk.NewColor("#30ba07"), - mcmap.BioJungleEdgeM: gdk.NewColor("#3e9130"), - mcmap.BioBirchForestM: gdk.NewColor("#228548"), - mcmap.BioBirchForestHillsM: gdk.NewColor("#2b7547"), - mcmap.BioRoofedForestM: gdk.NewColor("#1a5428"), - mcmap.BioColdTaigaM: gdk.NewColor("#0f706b"), - mcmap.BioMegaSpruceTaiga: gdk.NewColor("#198058"), - mcmap.BioMegaSpruceTaigaHills: gdk.NewColor("#156e4c"), - mcmap.BioExtremeHillsPlusM: gdk.NewColor("#ba5c00"), - mcmap.BioSavannaM: gdk.NewColor("#858111"), - mcmap.BioSavannaPlateauM: gdk.NewColor("#87830b"), - mcmap.BioMesaBryce: gdk.NewColor("#ff5100"), - mcmap.BioMesaPlateauFM: gdk.NewColor("#ba5a1a"), - mcmap.BioMesaPlateauM: gdk.NewColor("#ba4e06"), - mcmap.BioUncalculated: gdk.NewColor("#333333"), +func ReadBiomeInfos(r io.Reader) ([]BiomeInfo, error) { + var biomes []BiomeInfo + + sc := bufio.NewScanner(r) + for i := 1; sc.Scan(); i++ { + line := sc.Text() + if line == "" { + continue + } + parts := strings.SplitN(line, "\t", 4) + + if len(parts) != 4 { + return nil, fmt.Errorf("Line %d corrupted: Not enough parts", i) + } + + id, err := strconv.ParseUint(parts[0], 10, 8) + if err != nil { + return nil, fmt.Errorf("Line %d corrupted: %s", i, err) + } + + snow, err := strconv.ParseInt(parts[1], 10, 32) + if err != nil { + return nil, fmt.Errorf("Line %d corrupted: %s", i, err) + } + if (snow >= mcmap.ChunkSizeY) || (snow < 0) { + snow = mcmap.ChunkSizeY + } + + info := BiomeInfo{ + ID: mcmap.Biome(id), + SnowLine: int(snow), + Color: parts[2], + Name: parts[3], + } + biomes = append(biomes, info) + } + + if err := sc.Err(); err != nil { + return nil, err + } + + return biomes, nil +} + +func WriteBiomeInfos(biomes []BiomeInfo, w io.Writer) error { + for _, bio := range biomes { + if _, err := fmt.Fprintf(w, "%d\t%d\t%s\t%s\n", bio.ID, bio.SnowLine, bio.Color, bio.Name); err != nil { + return err + } + } + + return nil +} + +type BiomeLookup map[mcmap.Biome]BiomeInfo + +var colBlack = gdk.NewColor("#000000") +var colBuf = make(ColorBuffer) + +func (bl BiomeLookup) Color(bio mcmap.Biome) *gdk.Color { + if info, ok := bl[bio]; ok { + return colBuf.Color(info.Color) + } + + return colBlack +} + +func (bl BiomeLookup) SnowLine(bio mcmap.Biome) int { + if info, ok := bl[bio]; ok { + return info.SnowLine + } + + return mcmap.ChunkSizeY } -// snowLines maps a biome to the lowest y coordinate that can have snow naturally. -// TODO: Get the actual values that Minecraft uses (could not find them). -var snowLines = map[mcmap.Biome]int{ - mcmap.BioOcean: mcmap.ChunkSizeY, - mcmap.BioPlains: mcmap.ChunkSizeY, - mcmap.BioDesert: mcmap.ChunkSizeY, - mcmap.BioExtremeHills: 95, - mcmap.BioForest: mcmap.ChunkSizeY, - mcmap.BioTaiga: mcmap.ChunkSizeY, - mcmap.BioSwampland: mcmap.ChunkSizeY, - mcmap.BioRiver: mcmap.ChunkSizeY, - mcmap.BioHell: mcmap.ChunkSizeY, - mcmap.BioSky: mcmap.ChunkSizeY, - mcmap.BioFrozenOcean: 0, - mcmap.BioFrozenRiver: 0, - mcmap.BioIcePlains: 0, - mcmap.BioIceMountains: 0, - mcmap.BioMushroomIsland: mcmap.ChunkSizeY, - mcmap.BioMushroomIslandShore: mcmap.ChunkSizeY, - mcmap.BioBeach: mcmap.ChunkSizeY, - mcmap.BioDesertHills: mcmap.ChunkSizeY, - mcmap.BioForestHills: mcmap.ChunkSizeY, - mcmap.BioTaigaHills: 0, - mcmap.BioExtremeHillsEdge: 95, - mcmap.BioJungle: mcmap.ChunkSizeY, - mcmap.BioJungleHills: mcmap.ChunkSizeY, - mcmap.BioJungleEdge: mcmap.ChunkSizeY, - mcmap.BioDeepOcean: mcmap.ChunkSizeY, - mcmap.BioStoneBeach: mcmap.ChunkSizeY, - mcmap.BioColdBeach: 0, - mcmap.BioBirchForest: mcmap.ChunkSizeY, - mcmap.BioBirchForestHills: mcmap.ChunkSizeY, - mcmap.BioRoofedForest: mcmap.ChunkSizeY, - mcmap.BioColdTaiga: 0, - mcmap.BioColdTaigaHills: 0, - mcmap.BioMegaTaiga: mcmap.ChunkSizeY, - mcmap.BioMegaTaigaHills: mcmap.ChunkSizeY, - mcmap.BioExtremeHillsPlus: 95, - mcmap.BioSavanna: mcmap.ChunkSizeY, - mcmap.BioSavannaPlateau: mcmap.ChunkSizeY, - mcmap.BioMesa: mcmap.ChunkSizeY, - mcmap.BioMesaPlateauF: mcmap.ChunkSizeY, - mcmap.BioMesaPlateau: mcmap.ChunkSizeY, - mcmap.BioSunflowerPlains: mcmap.ChunkSizeY, - mcmap.BioDesertM: mcmap.ChunkSizeY, - mcmap.BioExtremeHillsM: 95, - mcmap.BioFlowerForest: mcmap.ChunkSizeY, - mcmap.BioTaigaM: mcmap.ChunkSizeY, - mcmap.BioSwamplandM: mcmap.ChunkSizeY, - mcmap.BioIcePlainsSpikes: 0, - mcmap.BioIceMountainsSpikes: 0, - mcmap.BioJungleM: mcmap.ChunkSizeY, - mcmap.BioJungleEdgeM: mcmap.ChunkSizeY, - mcmap.BioBirchForestM: mcmap.ChunkSizeY, - mcmap.BioBirchForestHillsM: mcmap.ChunkSizeY, - mcmap.BioRoofedForestM: mcmap.ChunkSizeY, - mcmap.BioColdTaigaM: 0, - mcmap.BioMegaSpruceTaiga: mcmap.ChunkSizeY, - mcmap.BioMegaSpruceTaigaHills: mcmap.ChunkSizeY, - mcmap.BioExtremeHillsPlusM: 95, - mcmap.BioSavannaM: mcmap.ChunkSizeY, - mcmap.BioSavannaPlateauM: mcmap.ChunkSizeY, - mcmap.BioMesaBryce: mcmap.ChunkSizeY, - mcmap.BioMesaPlateauFM: mcmap.ChunkSizeY, - mcmap.BioMesaPlateauM: mcmap.ChunkSizeY, - mcmap.BioUncalculated: mcmap.ChunkSizeY, +func (bl BiomeLookup) Name(bio mcmap.Biome) string { + if info, ok := bl[bio]; ok { + return info.Name + } + + return "?" +} + +func MkBiomeLookup(biomes []BiomeInfo) BiomeLookup { + lookup := make(BiomeLookup) + for _, biome := range biomes { + lookup[biome.ID] = biome + } + return lookup +} + +var defaultBiomes = `0 -1 #0000ff Ocean +1 -1 #9fe804 Plains +2 -1 #f5ff58 Desert +3 95 #a75300 Extreme Hills +4 -1 #006f2a Forest +5 -1 #05795a Taiga +6 -1 #6a7905 Swampland +7 -1 #196eff River +8 -1 #d71900 Hell +9 -1 #871eb3 Sky +10 0 #d6f0ff Frozen Ocean +11 0 #8fb6cd Frozen River +12 0 #fbfbfb Ice Plains +13 0 #c6bfb1 Ice Mountains +14 -1 #9776a4 Mushroom Island +15 -1 #9e8ebc Mushroom Island Shore +16 -1 #fffdc9 Beach +17 -1 #adb354 Desert Hills +18 -1 #40694f Forest Hills +19 0 #5b8578 Taiga Hills +20 95 #a77748 Extreme Hills Edge +21 -1 #22db04 Jungle +22 -1 #63bf54 Jungle Hills +23 -1 #40ba2c Jungle Edge +24 -1 #0000b3 Deep Ocean +25 -1 #9292a6 Stone Beach +26 0 #c7c7e8 Cold Beach +27 -1 #1d964b Birch Forest +28 -1 #498045 Birch Forest Hills +29 -1 #075a26 Roofed Forest +30 0 #1b948e Cold Taiga +31 0 #1d7a76 Cold Taiga Hills +32 -1 #1f8f68 Mega Taiga +33 -1 #217a5c Mega Taiga Hills +34 95 #d76a00 Extreme Hills+ +35 -1 #b2bc0f Savanna +36 -1 #aba60e Savanna Plateau +37 -1 #ff6c00 Mesa +38 -1 #d9691e Mesa Plateau F +39 -1 #d95b07 Mesa Plateau +40 -1 #ffd504 Sunflower Plains +41 -1 #f4ff3f Desert M +42 95 #8c4500 Extreme Hills M +43 -1 #e02f4a Flower Forest +44 -1 #0a6148 Taiga M +45 -1 #58630e Swampland M +46 0 #ace8e8 Ice Plains Spikes +47 0 #91cccc Ice Mountains Spikes +48 -1 #30ba07 Jungle M +49 -1 #3e9130 JungleEdge M +50 -1 #228548 Birch Forest M +51 -1 #2b7547 Birch Forest Hills M +52 -1 #1a5428 Roofed Forest M +53 0 #0f706b Cold Taiga M +54 -1 #198058 Mega Spruce Taiga +55 -1 #156e4c Mega Spruce Taiga Hills +56 95 #ba5c00 Extreme Hills+ M +57 -1 #858111 Savanna M +58 -1 #87830b Savanna Plateau M +59 -1 #ff5100 Mesa (Bryce) +60 -1 #ba5a1a Mesa Plateau F M +61 -1 #ba4e06 Mesa Plateau M +255 -1 #333333 (Uncalculated) +` + +func ReadDefaultBiomes() []BiomeInfo { + r := strings.NewReader(defaultBiomes) + biomes, err := ReadBiomeInfos(r) + if err != nil { + panic(err) + } + return biomes } diff --git a/colbuf.go b/colbuf.go new file mode 100644 index 0000000..429bf6c --- /dev/null +++ b/colbuf.go @@ -0,0 +1,17 @@ +package main + +import ( + "github.com/mattn/go-gtk/gdk" +) + +type ColorBuffer map[string]*gdk.Color + +func (cb ColorBuffer) Color(name string) *gdk.Color { + if col, ok := cb[name]; ok { + return col + } + + col := gdk.NewColor(name) + cb[name] = col + return col +} @@ -21,6 +21,10 @@ type GUI struct { statusContext uint lastStatus string + biomes []BiomeInfo + bioVBox *gtk.VBox + bioVBoxWrap *gtk.VBox + mapw *MapWidget } @@ -75,6 +79,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. dlg.Destroy() } +func (g *GUI) biomeEditor() { + ed := NewBiomeInfoEditor(g.biomes) + rv := ed.Run() + ed.Destroy() + if rv == gtk.RESPONSE_OK { + g.biomes = ed.Biomes() + g.updateBiomeInfo() + } +} + func (g *GUI) mkMenuBar() *gtk.MenuBar { menubar := gtk.NewMenuBar() @@ -103,16 +117,6 @@ func (g *GUI) mkMenuBar() *gtk.MenuBar { fileMenuItem.SetSubmenu(fileMenu) menubar.Append(fileMenuItem) - /*editMenu := gtk.NewMenu() - - undo := gtk.NewMenuItemWithLabel("Undo") - undo.Connect("activate", g.undo) - editMenu.Append(undo) - - editMenuItem := gtk.NewMenuItemWithLabel("Edit") - editMenuItem.SetSubmenu(editMenu) - menubar.Append(editMenuItem)*/ - helpMenu := gtk.NewMenu() controls := gtk.NewMenuItemWithLabel("Controls") @@ -174,19 +178,19 @@ func labelCustomFont(text, font string) *gtk.Label { } func (g *GUI) mkSidebar() *gtk.ScrolledWindow { - vbox := gtk.NewVBox(false, 0) + sbVBox := gtk.NewVBox(false, 0) - vbox.PackStart(labelCustomFont("Tools", "Sans Bold 14"), false, false, 3) + sbVBox.PackStart(labelCustomFont("Tools", "Sans Bold 14"), false, false, 3) g.showbiomes = gtk.NewCheckButtonWithLabel("Show Biomes") g.showbiomes.SetActive(true) g.showbiomes.Connect("toggled", g.showbiomesToggled) - vbox.PackStart(g.showbiomes, false, false, 3) + sbVBox.PackStart(g.showbiomes, false, false, 3) g.fixSnowIce = gtk.NewCheckButtonWithLabel("Fix Snow/Ice") g.fixSnowIce.SetTooltipText("Add Snow/Ice for Taiga/Ice Plains. Remove Snow/Ice for other biomes.") g.fixSnowIce.Connect("toggled", g.fixSnowIceToggled) - vbox.PackStart(g.fixSnowIce, false, false, 3) + sbVBox.PackStart(g.fixSnowIce, false, false, 3) fill := gtk.NewRadioButtonWithLabel(nil, "Fill") fill.SetActive(true) @@ -200,32 +204,58 @@ func (g *GUI) mkSidebar() *gtk.ScrolledWindow { drawHBox.PackEnd(drawRadius, false, false, 3) draw.Connect("toggled", g.mkUpdateToolFx(draw, NewDrawTool(func() int { return drawRadius.GetValueAsInt() }))) - vbox.PackStart(fill, false, false, 3) - vbox.PackStart(drawHBox, false, false, 3) + sbVBox.PackStart(fill, false, false, 3) + sbVBox.PackStart(drawHBox, false, false, 3) + + sbVBox.PackStart(gtk.NewHSeparator(), false, false, 3) + bioHeaderHBox := gtk.NewHBox(false, 0) + bioHeaderHBox.PackStart(labelCustomFont("Biomes", "Sans Bold 14"), true, false, 0) + editBiomesBtn := gtk.NewButton() + editBiomesBtn.Add(gtk.NewImageFromStock(gtk.STOCK_EDIT, gtk.ICON_SIZE_SMALL_TOOLBAR)) + editBiomesBtn.Connect("clicked", g.biomeEditor) + editBiomesBtn.SetTooltipText("Configure Biomes") + bioHeaderHBox.PackStart(editBiomesBtn, false, false, 0) + sbVBox.PackStart(bioHeaderHBox, false, false, 3) + + g.bioVBoxWrap = gtk.NewVBox(false, 0) + g.bioVBox = gtk.NewVBox(false, 0) + g.bioVBoxWrap.PackStart(g.bioVBox, false, false, 0) + sbVBox.PackStart(g.bioVBoxWrap, false, false, 3) + g.updateBiomeInfo() - vbox.PackStart(gtk.NewHSeparator(), false, false, 3) - vbox.PackStart(labelCustomFont("Biomes", "Sans Bold 14"), false, false, 3) + scrolled := gtk.NewScrolledWindow(nil, nil) + scrolled.SetPolicy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) + scrolled.AddWithViewPort(sbVBox) + return scrolled +} +func (g *GUI) updateBiomeInfo() { + vbox := gtk.NewVBox(false, 0) var grp *glib.SList - for _, bio := range bioList { + + for _, biome := range g.biomes { biohbox := gtk.NewHBox(false, 0) - cbox := colorBox(bioColors[bio]) + cbox := colorBox(gdk.NewColor(biome.Color)) cbox.SetSizeRequest(20, 20) biohbox.PackStart(cbox, false, false, 3) - rbutton := gtk.NewRadioButtonWithLabel(grp, bio.String()) + rbutton := gtk.NewRadioButtonWithLabel(grp, biome.Name) grp = rbutton.GetGroup() - rbutton.Connect("toggled", g.mkUpdateBiomeFx(rbutton, bio)) + rbutton.Connect("toggled", g.mkUpdateBiomeFx(rbutton, biome.ID)) biohbox.PackEnd(rbutton, true, true, 3) vbox.PackStart(biohbox, false, false, 3) } - scrolled := gtk.NewScrolledWindow(nil, nil) - scrolled.SetPolicy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) - scrolled.AddWithViewPort(vbox) - return scrolled + g.bioVBoxWrap.Remove(g.bioVBox) + g.bioVBoxWrap.PackStart(vbox, false, false, 3) + vbox.ShowAll() + g.bioVBox = vbox + + g.mapw.updateBioLookup(MkBiomeLookup(g.biomes)) } func (g *GUI) Init() { + g.biomes = ReadDefaultBiomes() + g.window = gtk.NewWindow(gtk.WINDOW_TOPLEVEL) g.window.SetTitle("biomed") @@ -235,7 +265,7 @@ func (g *GUI) Init() { hbox := gtk.NewHBox(false, 0) - g.mapw = NewMapWidget(GUICallbacks{g.reportError, g.updateInfo, g.setBusy}) + g.mapw = NewMapWidget(GUICallbacks{g.reportError, g.updateInfo, g.setBusy}, MkBiomeLookup(g.biomes)) hbox.PackStart(g.mapw.DArea(), true, true, 3) sidebar := g.mkSidebar() @@ -273,8 +303,8 @@ func (g *GUI) reportError(msg string) { os.Exit(1) } -func (g *GUI) updateInfo(x, z int, bio mcmap.Biome) { - g.lastStatus = fmt.Sprintf("X:%d, Z:%d, Biome:%s", x, z, bio) +func (g *GUI) updateInfo(x, z int, bio mcmap.Biome, name string) { + g.lastStatus = fmt.Sprintf("X:%d, Z:%d, Biome: %s(%d)", x, z, name, bio) g.statusbar.Pop(g.statusContext) g.statusbar.Push(g.statusContext, g.lastStatus) } @@ -311,10 +341,6 @@ func (g *GUI) fixSnowIceToggled() { g.mapw.SetFixSnowIce(g.fixSnowIce.GetActive()) } -/*func (g *GUI) undo() { - fmt.Println("Undo") -}*/ - func (g *GUI) Show() { g.window.ShowAll() g.betaWarning() diff --git a/mapwidget.go b/mapwidget.go index cb8c874..e04b5eb 100644 --- a/mapwidget.go +++ b/mapwidget.go @@ -16,7 +16,7 @@ const ( type GUICallbacks struct { reportFail func(msg string) - updateInfo func(x, z int, bio mcmap.Biome) + updateInfo func(x, z int, bio mcmap.Biome, name string) setBusy func(bool) } @@ -41,6 +41,8 @@ type MapWidget struct { bg *gdk.Pixmap regWrap *RegionWrapper + + bioLookup BiomeLookup } func (mw *MapWidget) calcChunkRect() { @@ -89,7 +91,7 @@ func (mw *MapWidget) movement(ctx *glib.CallbackContext) { if _bio, ok := mw.regWrap.GetBiomeAt(x, z); ok { bio = _bio } - mw.guicbs.updateInfo(x, z, bio) + mw.guicbs.updateInfo(x, z, bio, mw.bioLookup.Name(bio)) if mw.panning { if (mw.mx1 != -1) && (mw.my1 != -1) { @@ -231,7 +233,17 @@ func (mw *MapWidget) updateGUI() { mw.dArea.GetWindow().Invalidate(nil, false) } -func NewMapWidget(guicbs GUICallbacks) *MapWidget { +func (mw *MapWidget) updateBioLookup(lookup BiomeLookup) { + mw.bioLookup = lookup + mw.regWrap.bioLookup = lookup + + if mw.regWrap.RegionLoaded() { + mw.regWrap.FlushTiles() + mw.regWrap.UpdateTiles() + } +} + +func NewMapWidget(guicbs GUICallbacks, bioLookup BiomeLookup) *MapWidget { dArea := gtk.NewDrawingArea() mw := &MapWidget{ @@ -240,9 +252,11 @@ func NewMapWidget(guicbs GUICallbacks) *MapWidget { showBiomes: true, mx1: -1, my1: -1, + bioLookup: bioLookup, } mw.regWrap = NewRegionWrapper(mw.updateGUI, guicbs) + mw.regWrap.bioLookup = bioLookup dArea.Connect("configure-event", mw.configure) dArea.Connect("expose-event", mw.expose) diff --git a/region_wrapper.go b/region_wrapper.go index bf23913..d27d1ad 100644 --- a/region_wrapper.go +++ b/region_wrapper.go @@ -28,9 +28,11 @@ type RegionWrapper struct { bio mcmap.Biome startX, startZ, endX, endZ int + + bioLookup BiomeLookup } -func renderTile(chunk *mcmap.Chunk) (maptile, biotile *gdk.Pixmap, biocache []mcmap.Biome) { +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) @@ -46,7 +48,7 @@ func renderTile(chunk *mcmap.Chunk) (maptile, biotile *gdk.Pixmap, biocache []mc scanX: for x := 0; x < mcmap.ChunkSizeXZ; x++ { bio := chunk.Biome(x, z) - btGC.SetRgbFgColor(bioColors[bio]) + btGC.SetRgbFgColor(rw.bioLookup.Color(bio)) btDrawable.DrawRectangle(btGC, true, x*zoom, z*zoom, zoom, zoom) biocache[i] = bio @@ -115,7 +117,7 @@ func (rw *RegionWrapper) tileUpdater() { return } - rw.Maptiles[pos], rw.Biotiles[pos], rw.bioCache[pos] = renderTile(chunk) + rw.Maptiles[pos], rw.Biotiles[pos], rw.bioCache[pos] = rw.renderTile(chunk) chunk.MarkUnused() rw.redraw() @@ -129,11 +131,11 @@ func (rw *RegionWrapper) tileUpdater() { func (rw *RegionWrapper) SetRegion(region *mcmap.Region) { if rw.RegionLoaded() { - rw.flushTiles() + rw.FlushTiles() } rw.region = NewCachedRegion(region, cacheSize) - rw.tileUpdates <- true + rw.UpdateTiles() } func (rw *RegionWrapper) SetChunkBounds(startX, startZ, endX, endZ int) { @@ -150,7 +152,7 @@ func (rw *RegionWrapper) SetFixSnowIce(b bool) { rw.fixSnowIce = b } func (rw *RegionWrapper) RegionLoaded() bool { return rw.region != nil } func (rw *RegionWrapper) ToolSingleClick() bool { return rw.tool.SingleClick() } -func (rw *RegionWrapper) flushTiles() { +func (rw *RegionWrapper) FlushTiles() { if err := rw.region.Flush(); err != nil { rw.guicbs.reportFail(fmt.Sprintf("Error while flushing cache: %s", err)) return @@ -169,7 +171,7 @@ func (rw *RegionWrapper) flushTiles() { } func (rw *RegionWrapper) Save() { - rw.flushTiles() + rw.FlushTiles() if err := rw.region.Flush(); err != nil { rw.guicbs.reportFail(fmt.Sprintf("Error while flushing cache: %s", err)) @@ -242,8 +244,8 @@ func (rw *RegionWrapper) GetBiomeAt(x, z int) (mcmap.Biome, bool) { return chunk.Biome(bx, bz), true } -func fixWeather(bio mcmap.Biome, bx, bz int, chunk *mcmap.Chunk) (newcol *gdk.Color) { - snowLine := snowLines[bio] +func (rw *RegionWrapper) fixWeather(bio mcmap.Biome, bx, bz int, chunk *mcmap.Chunk) (newcol *gdk.Color) { + snowLine := rw.bioLookup.SnowLine(bio) for y := mcmap.ChunkSizeY; y >= 0; y-- { blk := chunk.Block(bx, y, bz) @@ -306,7 +308,7 @@ func (rw *RegionWrapper) SetBiomeAt(x, z int, bio mcmap.Biome) { var newcol *gdk.Color if rw.fixSnowIce { - newcol = fixWeather(bio, bx, bz, chunk) + newcol = rw.fixWeather(bio, bx, bz, chunk) } chunk.MarkModified() @@ -322,7 +324,7 @@ func (rw *RegionWrapper) SetBiomeAt(x, z int, bio mcmap.Biome) { drawable := biotile.GetDrawable() gc := gdk.NewGC(drawable) - gc.SetRgbFgColor(bioColors[bio]) + gc.SetRgbFgColor(rw.bioLookup.Color(bio)) drawable.DrawRectangle(gc, true, bx*zoom, bz*zoom, zoom, zoom) if newcol != nil { |