Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
add setting to toggle between ascii and tiles display
Browse files Browse the repository at this point in the history
  • Loading branch information
anaseto committed Jul 23, 2018
1 parent 3753f34 commit d206732
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 43 deletions.
5 changes: 5 additions & 0 deletions draw.go
Expand Up @@ -2791,6 +2791,7 @@ type setting int
const (
setKeys setting = iota
invertLOS
toggleTiles
)

func (s setting) String() (text string) {
Expand All @@ -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
}
Expand Down Expand Up @@ -2860,6 +2863,8 @@ func (ui *termui) HandleSettingAction(g *game) error {
} else {
ApplyLightLOS()
}
case toggleTiles:
ui.ApplyToggleTiles()
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions encoding.go
Expand Up @@ -30,6 +30,7 @@ type config struct {
RuneNormalModeKeys map[rune]keyAction
RuneTargetModeKeys map[rune]keyAction
DarkLOS bool
Tiles bool
Version string
}

Expand Down
7 changes: 2 additions & 5 deletions js.go
Expand Up @@ -14,7 +14,6 @@ import (
"unicode/utf8"

"github.com/gopherjs/gopherwasm/js"
//"github.com/gopherjs/gopherjs/js"
)

func main() {
Expand All @@ -26,6 +25,7 @@ func main() {
defer tui.Close()

ApplyDefaultKeyBindings()
gameConfig.Tiles = true
tui.PostInit()
LinkColors()

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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() {
Expand Down
52 changes: 14 additions & 38 deletions jscanvas.go
Expand Up @@ -24,42 +24,33 @@ 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) {
}), false)
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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions save.go
Expand Up @@ -144,3 +144,6 @@ func (g *game) RemoveDataFile(file string) error {
}
return nil
}

func (ui *termui) ApplyToggleTiles() {
}

0 comments on commit d206732

Please sign in to comment.