diff --git a/draw.go b/draw.go index 1f12319..a0eecae 100644 --- a/draw.go +++ b/draw.go @@ -2791,6 +2791,7 @@ type setting int const ( setKeys setting = iota invertLOS + toggleTiles ) func (s setting) String() (text string) { @@ -2799,6 +2800,8 @@ func (s setting) String() (text string) { text = "Change key bindings" case invertLOS: text = "Toggle dark/light LOS" + case toggleTiles: + text = "Toggle Tiles/Ascii display" } return text } @@ -2860,6 +2863,8 @@ func (ui *termui) HandleSettingAction(g *game) error { } else { ApplyLightLOS() } + case toggleTiles: + ui.ApplyToggleTiles() } return nil } diff --git a/encoding.go b/encoding.go index 8109394..822cfac 100644 --- a/encoding.go +++ b/encoding.go @@ -30,6 +30,7 @@ type config struct { RuneNormalModeKeys map[rune]keyAction RuneTargetModeKeys map[rune]keyAction DarkLOS bool + Tiles bool Version string } diff --git a/js.go b/js.go index 72b2803..cdfeddf 100644 --- a/js.go +++ b/js.go @@ -14,7 +14,6 @@ import ( "unicode/utf8" "github.com/gopherjs/gopherwasm/js" - //"github.com/gopherjs/gopherjs/js" ) func main() { @@ -26,6 +25,7 @@ func main() { defer tui.Close() ApplyDefaultKeyBindings() + gameConfig.Tiles = true tui.PostInit() LinkColors() @@ -201,10 +201,6 @@ func (ui *termui) Init() error { ch <- jsInput{mouse: true, mouseX: x, mouseY: y, button: -1} } })) - //js.Global().Get("document").Call("addEventListener", "mousemove", func(e js.Value) { - //x, y := ui.GetMousePos(e) - //ui.mouse = position{x, y} - //}) ui.ResetCells() ui.backBuffer = make([]UICell, UIWidth*UIHeight) ui.InitElements() @@ -333,6 +329,7 @@ func (ui *termui) PostInit() { ui.HideCursor() //MenuCols[MenuOther] = MenuCols[MenuView] //MenuCols[MenuView] = [2]int{-1, -1} + settingsActions = append(settingsActions, toggleTiles) } func (ui *termui) Clear() { diff --git a/jscanvas.go b/jscanvas.go index 7448db7..da93cd4 100644 --- a/jscanvas.go +++ b/jscanvas.go @@ -24,8 +24,6 @@ type termui struct { mousepos position } -var Tiles bool = true - func (ui *termui) InitElements() error { canvas := js.Global().Get("document").Call("getElementById", "gamecanvas") canvas.Call("addEventListener", "contextmenu", js.NewEventCallback(js.PreventDefault, func(e js.Value) { @@ -33,33 +31,26 @@ func (ui *termui) InitElements() error { canvas.Call("setAttribute", "tabindex", "1") ui.ctx = canvas.Call("getContext", "2d") ui.ctx.Set("imageSmoothingEnabled", false) - //if Tiles { - //ui.ctx.Set("font", "22px monospace") - //} else { - //ui.ctx.Set("font", "18px monospace") - //} - //if Tiles { ui.width = 16 ui.height = 24 canvas.Set("height", 24*UIHeight) canvas.Set("width", 16*UIWidth) - //} else { - //ui.height = 22 - //mesure := ui.ctx.Call("measureText", "W") - //ui.width = mesure.Get("width").Int() + 1 - //canvas.Set("height", ui.height*UIHeight) - //canvas.Set("width", ui.width*UIWidth) - //} - // seems to be needed again - //if Tiles { - //ui.ctx.Set("font", "22px monospace") - //} else { - //ui.ctx.Set("font", "18px monospace") - //} ui.cache = make(map[UICell]js.Value) return nil } +func (ui *termui) ApplyToggleTiles() { + gameConfig.Tiles = !gameConfig.Tiles + for c, _ := range ui.cache { + if c.inMap { + delete(ui.cache, c) + } + } + for i := 0; i < len(ui.backBuffer); i++ { + ui.backBuffer[i] = UICell{} + } +} + var TileImgs map[string][]byte var MapNames = map[rune]string{ @@ -152,7 +143,7 @@ var LetterNames = map[rune]string{ func getImage(cell UICell) []byte { var pngImg []byte - if cell.inMap && Tiles { + if cell.inMap && gameConfig.Tiles { pngImg = TileImgs["map-notile"] if im, ok := TileImgs["map-"+string(cell.r)]; ok { pngImg = im @@ -200,7 +191,7 @@ func (ui *termui) Draw(cell UICell, x, y int) { canvas = cv } else { canvas = js.Global().Get("document").Call("createElement", "canvas") - //if Tiles { + //if gameConfig.Tiles { canvas.Set("width", 16) canvas.Set("height", 24) ctx := canvas.Call("getContext", "2d") @@ -211,21 +202,6 @@ func (ui *termui) Draw(cell UICell, x, y int) { imgdata := js.Global().Get("ImageData").New(ca, 16, 24) ctx.Call("putImageData", imgdata, 0, 0) ta.Release() - //} else { - //canvas.Set("width", ui.width) - //canvas.Set("height", ui.height) - //ctx := canvas.Call("getContext", "2d") - //ctx.Set("imageSmoothingEnabled", false) - //ctx.Set("font", ui.ctx.Get("font")) - //ctx.Set("fillStyle", cell.bg.String()) - //ctx.Call("fillRect", 0, 0, ui.width, ui.height) - //ctx.Set("fillStyle", cell.fg.String()) - ////if Tiles { - ////ctx.Call("fillText", string(cell.r), 0, 18) - ////} else { - //ctx.Call("fillText", string(cell.r), 0, 18) - ////} - //} ui.cache[cell] = canvas } ui.ctx.Call("drawImage", canvas, x*ui.width, ui.height*y) diff --git a/save.go b/save.go index 4405877..34cddc4 100644 --- a/save.go +++ b/save.go @@ -144,3 +144,6 @@ func (g *game) RemoveDataFile(file string) error { } return nil } + +func (ui *termui) ApplyToggleTiles() { +}