Skip to content

Commit

Permalink
add: add tile editor in tilemap
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelesaux committed Nov 10, 2024
1 parent cf9cc3e commit 6d6fdb4
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ require (
github.com/go-text/typesetting v0.2.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/jeromelesaux/fyne-io v0.0.0-20241108081021-cb87ec93652b
github.com/jeromelesaux/fyne-io v0.0.0-20241110145530-be12d459d08b
github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/muesli/clusters v0.0.0-20200529215643-2700303c1762 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ github.com/jeromelesaux/fyne-io v0.0.0-20241108075048-fa6baca4f329 h1:HYIikz06h9
github.com/jeromelesaux/fyne-io v0.0.0-20241108075048-fa6baca4f329/go.mod h1:CfylT5HbwoZiniHx+zg+9fpOM4P8cE6XQuyUOTJHVOo=
github.com/jeromelesaux/fyne-io v0.0.0-20241108081021-cb87ec93652b h1:NHMxRSYb4Dp2e6MnwbrZx1BgGJjf9VJlkRA42VUnVus=
github.com/jeromelesaux/fyne-io v0.0.0-20241108081021-cb87ec93652b/go.mod h1:CfylT5HbwoZiniHx+zg+9fpOM4P8cE6XQuyUOTJHVOo=
github.com/jeromelesaux/fyne-io v0.0.0-20241110113029-363f5be2ffa5 h1:RuAvrKFhOMVNGdW0W4L9WKM8jE+7PNMAg9BrDEfbPnA=
github.com/jeromelesaux/fyne-io v0.0.0-20241110113029-363f5be2ffa5/go.mod h1:CfylT5HbwoZiniHx+zg+9fpOM4P8cE6XQuyUOTJHVOo=
github.com/jeromelesaux/fyne-io v0.0.0-20241110113803-3ea975dab154 h1:Bi+j8nGn6CM8QjUPjZUYw6rnSKJOt0vllqa8g8hKcFs=
github.com/jeromelesaux/fyne-io v0.0.0-20241110113803-3ea975dab154/go.mod h1:CfylT5HbwoZiniHx+zg+9fpOM4P8cE6XQuyUOTJHVOo=
github.com/jeromelesaux/fyne-io v0.0.0-20241110145530-be12d459d08b h1:wiGRjtt9t2v2Ke3stNvgbi7h//y20NyQ0UggYsyOGnY=
github.com/jeromelesaux/fyne-io v0.0.0-20241110145530-be12d459d08b/go.mod h1:CfylT5HbwoZiniHx+zg+9fpOM4P8cE6XQuyUOTJHVOo=
github.com/jeromelesaux/m4client v0.0.0-20200309212559-efb59c22369c/go.mod h1:JO0ijl8YXO6FxogJoC3yAFbBjSAnkwFquc8W6C7k94E=
github.com/jeromelesaux/m4client v0.0.0-20230327092026-4e80fd2b1474 h1:CWtkpA4Q63pkhHWRrd/e98JD4YVZhk3SZu58bUkQsuI=
github.com/jeromelesaux/m4client v0.0.0-20230327092026-4e80fd2b1474/go.mod h1:Xc0HzC2TZ7fbDWyB8LosvziBQ5QvvwVt3xxAIEavWRc=
Expand Down
2 changes: 1 addition & 1 deletion ui/martine-ui/menu/sprite_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (s *SpriteMenu) OriginalBoard() *canvas.Image {
func NewSpriteMenu() *SpriteMenu {
return &SpriteMenu{
originalBoard: &canvas.Image{},
OriginalImages: w.NewEmptyImageTable(fyne.NewSize(SpriteSize, SpriteSize)),
OriginalImages: w.NewEmptyImageTable(fyne.NewSize(SpriteSize, SpriteSize), nil),
SpritesCollection: make([][]*image.NRGBA, 0),
SpritesData: make([][][]byte, 0),
originalPalette: &canvas.Image{},
Expand Down
32 changes: 28 additions & 4 deletions ui/martine-ui/menu/tilemap_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package menu

import (
"fmt"
"image"
"os"
"strconv"

Expand All @@ -20,18 +21,22 @@ type TilemapMenu struct {
TileImages *w.ImageTable
ExportZigzag bool
Historic *sprite.TilesHistorical
col int
row int
tileImage image.Image
}

func (tm *TilemapMenu) ResetExport() {
tm.Cfg.Reset()
}

func NewTilemapMenu() *TilemapMenu {
return &TilemapMenu{
ImageMenu: NewImageMenu(),
Result: &transformation.AnalyzeBoard{},
TileImages: w.NewEmptyImageTable(fyne.NewSize(TileSize, TileSize)),
t := &TilemapMenu{
ImageMenu: NewImageMenu(),
Result: &transformation.AnalyzeBoard{},
}
t.TileImages = w.NewEmptyImageTable(fyne.NewSize(TileSize, TileSize), t.TileSelected)
return t
}

func (i *TilemapMenu) CmdLine() string {
Expand Down Expand Up @@ -94,3 +99,22 @@ func (i *TilemapMenu) CmdLine() string {
i.CmdLineGenerate = exec
return exec
}

func (me *TilemapMenu) TileSelected(row, col int) {
if row < 0 || col < 0 {
return
}
me.row = row
me.col = col
if me.Result == nil {
return
}
if row < len(me.Result.Tiles) && col < len(me.Result.Tiles[0]) {
tile := me.Result.Tiles[row][col]
me.tileImage = tile
}
}

func (me *TilemapMenu) TileImage() image.Image {
return me.tileImage
}
8 changes: 4 additions & 4 deletions ui/martine-ui/sprite_tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (m *MartineUI) ApplySprite(s *menu.SpriteMenu) {
s.SpritesCollection = sprites
s.SpritesData = raw

icache := wgt.NewImageTableCache(s.SpriteColumns, s.SpriteRows, fyne.NewSize(50, 50))
icache := wgt.NewImageTableCache(s.SpriteColumns, s.SpriteRows, fyne.NewSize(50, 50), nil)

for x := 0; x < s.SpriteColumns; x++ {
for y := 0; y < s.SpriteRows; y++ {
Expand Down Expand Up @@ -369,7 +369,7 @@ func applySpriteBoardFromGif(s *menu.SpriteMenu, m *MartineUI) *widget.Button {
s.SpritesData[0] = raw
s.SpriteColumns = 1
s.SpriteRows = len(resized)
icache := wgt.NewImageTableCache(s.SpriteColumns, s.SpriteRows, fyne.NewSize(50, 50))
icache := wgt.NewImageTableCache(s.SpriteColumns, s.SpriteRows, fyne.NewSize(50, 50), nil)

for x := 0; x < s.SpriteColumns; x++ {
for y := 0; y < s.SpriteRows; y++ {
Expand Down Expand Up @@ -436,7 +436,7 @@ func ImportSpriteBoard(m *MartineUI) *widget.Button {
}
}

icache := wgt.NewImageTableCache(m.sprite.SpriteRows, m.sprite.SpriteColumns, fyne.NewSize(50, 50))
icache := wgt.NewImageTableCache(m.sprite.SpriteRows, m.sprite.SpriteColumns, fyne.NewSize(50, 50), nil)

for y := 0; y < m.sprite.SpriteColumns; y++ {
for x := 0; x < m.sprite.SpriteRows; x++ {
Expand Down Expand Up @@ -490,7 +490,7 @@ func ImportSpriteBoard(m *MartineUI) *widget.Button {
}
}

icache := wgt.NewImageTableCache(m.sprite.SpriteRows, m.sprite.SpriteColumns, fyne.NewSize(50, 50))
icache := wgt.NewImageTableCache(m.sprite.SpriteRows, m.sprite.SpriteColumns, fyne.NewSize(50, 50), nil)

for y := 0; y < m.sprite.SpriteColumns; y++ {
for x := 0; x < m.sprite.SpriteRows; x++ {
Expand Down
35 changes: 33 additions & 2 deletions ui/martine-ui/tilemap_tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
wgt "github.com/jeromelesaux/fyne-io/widget"
"github.com/jeromelesaux/fyne-io/widget/editor"
"github.com/jeromelesaux/martine/config"
"github.com/jeromelesaux/martine/constants"
impPalette "github.com/jeromelesaux/martine/export/impdraw/palette"
Expand Down Expand Up @@ -80,7 +81,7 @@ func (m *MartineUI) TilemapApply(me *menu.TilemapMenu) {

me.Result = analyze
me.SetPalette(palette)
tilesCanvas := wgt.NewImageTableCache(len(tiles), len(tiles[0]), fyne.NewSize(50, 50))
tilesCanvas := wgt.NewImageTableCache(len(tiles), len(tiles[0]), fyne.NewSize(50, 50), me.TileSelected)
for i, v := range tiles {
for i2, v2 := range v {
tilesCanvas.Set(i, i2, canvas.NewImageFromImage(v2))
Expand Down Expand Up @@ -121,6 +122,13 @@ func (m *MartineUI) newImageMenuExportButton(tm *menu.ImageMenu) *widget.Button
})
}

func (m *MartineUI) SetTileImage(i image.Image, p color.Palette) {
if m.tilemap.Result == nil {
return
}
// set the tile to replace
}

// nolint: funlen, gocognit
func (m *MartineUI) newTilemapTab(tm *menu.TilemapMenu) *fyne.Container {
tm.ImageMenu.SetWindow(m.window)
Expand Down Expand Up @@ -237,7 +245,29 @@ func (m *MartineUI) newTilemapTab(tm *menu.TilemapMenu) *fyne.Container {
heightLabel := widget.NewLabel("Height")
tm.Height().Validator = validation.NewRegexp("\\d+", "Must contain a number")

tm.TileImages = wgt.NewEmptyImageTable(fyne.NewSize(menu.TileSize, menu.TileSize))
tm.TileImages = wgt.NewEmptyImageTable(fyne.NewSize(menu.TileSize, menu.TileSize), tm.TileSelected)

editButton := widget.NewButtonWithIcon("Edit", theme.DocumentCreateIcon(), func() {
p := constants.CpcOldPalette
if tm.Cfg.ScrCfg.IsPlus {
p = constants.CpcPlusPalette
}
if tm.TileImage() == nil || tm.PaletteImage().Image == nil {
return
}
edit := editor.NewEditor(tm.TileImage(),
editor.MagnifyX8,
tm.Palette(),
p, m.SetTileImage,
m.window)

d := dialog.NewCustom("Editor Selected Tile", "Ok", edit.NewEditor(), m.window)
size := m.window.Content().Size()
size = fyne.Size{Width: size.Width, Height: size.Height}
d.Resize(size)
d.Show()
// after the me.CpcImage().Image must be used to export
})

return container.New(
layout.NewGridLayoutWithColumns(2),
Expand Down Expand Up @@ -322,6 +352,7 @@ func (m *MartineUI) newTilemapTab(tm *menu.TilemapMenu) *fyne.Container {
}),

applyButton,
editButton,
),
),
),
Expand Down

0 comments on commit 6d6fdb4

Please sign in to comment.