example/mapeditor: Add map tools (which doesn't work yet)

This commit is contained in:
Hajime Hoshi 2015-02-07 03:21:22 +09:00
parent 0c7716586a
commit dee7b1a328
7 changed files with 84 additions and 25 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 336 B

View File

@ -19,6 +19,7 @@ import (
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/example/mapeditor/mapeditor" "github.com/hajimehoshi/ebiten/example/mapeditor/mapeditor"
"image/color" "image/color"
_ "image/png"
"log" "log"
) )

View File

@ -26,7 +26,7 @@ const (
mapViewX = 16 + TileWidth*TileSetXNum + 16 mapViewX = 16 + TileWidth*TileSetXNum + 16
mapViewY = 16 mapViewY = 16
mapViewWidth = 720 mapViewWidth = 720
mapViewHeight = 720 mapViewHeight = 736
) )
type MainEditor struct { type MainEditor struct {

View File

@ -66,8 +66,8 @@ func (t *TileRects) Dst(i int) (x0, y0, x1, y1 int) {
return x, y, x + TileLogicWidth, y + TileLogicHeight return x, y, x + TileLogicWidth, y + TileLogicHeight
} }
func (m *Map) Draw(i *ebiten.Image, tileSetImg *ebiten.Image, x, y int) error { func (m *Map) Draw(i *ebiten.Image, tileSetImg *ebiten.Image, x, y, width, height int) error {
i.Fill(color.RGBA{0x80, 0x80, 0x80, 0xff}) i.DrawFilledRect(x, y, width, height, color.RGBA{0x80, 0x80, 0x80, 0xff})
op := &ebiten.DrawImageOptions{ op := &ebiten.DrawImageOptions{
ImageParts: &TilesBackgroundRects{m.width, m.height}, ImageParts: &TilesBackgroundRects{m.width, m.height},
@ -80,8 +80,8 @@ func (m *Map) Draw(i *ebiten.Image, tileSetImg *ebiten.Image, x, y int) error {
op = &ebiten.DrawImageOptions{ op = &ebiten.DrawImageOptions{
ImageParts: &TileRects{m}, ImageParts: &TileRects{m},
} }
op.GeoM.Translate(float64(x), float64(y))
op.GeoM.Scale(2, 2) op.GeoM.Scale(2, 2)
op.GeoM.Translate(float64(x), float64(y))
if err := i.DrawImage(tileSetImg, op); err != nil { if err := i.DrawImage(tileSetImg, op); err != nil {
return err return err
} }

View File

@ -0,0 +1,52 @@
// Copyright 2015 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mapeditor
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
_ "image/png"
)
var mapToolsImage *ebiten.Image
func init() {
var err error
mapToolsImage, _, err = ebitenutil.NewImageFromFile("images/mapeditor/maptools.png", ebiten.FilterNearest)
if err != nil {
panic(err)
}
}
type MapTools struct {
focused bool
}
func NewMapTools() *MapTools {
return &MapTools{}
}
func (m *MapTools) Update() {
}
func (m *MapTools) Draw(i *ebiten.Image, x, y, width, height int) error {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(x), float64(y))
op.GeoM.Scale(2, 2)
if err := i.DrawImage(mapToolsImage, op); err != nil {
return err
}
return nil
}

View File

@ -21,26 +21,30 @@ import (
type MapView struct { type MapView struct {
m *Map m *Map
mapTools *MapTools
tileSet *TileSet tileSet *TileSet
cursorX int cursorX int
cursorY int cursorY int
dragging bool focused bool // TODO: FocusManager is needed?
} }
func NewMapView(m *Map) *MapView { func NewMapView(m *Map) *MapView {
return &MapView{ return &MapView{
m: m, m: m,
mapTools: NewMapTools(),
cursorX: 0, cursorX: 0,
cursorY: 0, cursorY: 0,
} }
} }
const mapYOffset = 32
func (m *MapView) Update(input *Input, ox, oy, width, height int, tileSet *TileSet, selectedTile int) error { func (m *MapView) Update(input *Input, ox, oy, width, height int, tileSet *TileSet, selectedTile int) error {
m.tileSet = tileSet m.tileSet = tileSet
x, y := ebiten.CursorPosition() x, y := ebiten.CursorPosition()
x -= ox x += -ox
y -= oy y += -oy - mapYOffset
if x < 0 || y < 0 || width <= x || height <= y { if x < 0 || y < 0 || width <= x || height <= y {
return nil return nil
} }
@ -50,31 +54,33 @@ func (m *MapView) Update(input *Input, ox, oy, width, height int, tileSet *TileS
m.cursorX = x / TileWidth m.cursorX = x / TileWidth
m.cursorY = y / TileHeight m.cursorY = y / TileHeight
if input.MouseButtonState(ebiten.MouseButtonLeft) == 0 { if input.MouseButtonState(ebiten.MouseButtonLeft) == 0 {
m.dragging = false m.focused = false
return nil return nil
} }
if input.MouseButtonState(ebiten.MouseButtonLeft) == 1 { if input.MouseButtonState(ebiten.MouseButtonLeft) == 1 {
m.dragging = true m.focused = true
} }
if m.dragging && ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if m.focused && ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
m.m.SetTile(m.cursorX, m.cursorY, selectedTile) m.m.SetTile(m.cursorX, m.cursorY, selectedTile)
} }
return nil return nil
} }
func (m *MapView) Draw(i *ebiten.Image, x, y, width, height int) error { func (m *MapView) Draw(i *ebiten.Image, x, y, width, height int) error {
if err := m.m.Draw(i, m.tileSet.image, x, y); err != nil { mapX, mapY := x, y+mapYOffset
if err := m.m.Draw(i, m.tileSet.image, mapX, mapY, width, height-mapYOffset); err != nil {
return err return err
} }
if m.cursorX == -1 || m.cursorY == -1 { if m.cursorX != -1 && m.cursorY != -1 {
return nil sx := mapX + m.cursorX*TileWidth
} sy := mapY + m.cursorY*TileHeight
sx := x + m.cursorX*TileWidth
sy := y + m.cursorY*TileHeight
i.DrawRect(sx, sy, TileWidth, TileHeight, color.Black) i.DrawRect(sx, sy, TileWidth, TileHeight, color.Black)
i.DrawRect(sx+1, sy+1, TileWidth-2, TileHeight-2, color.White) i.DrawRect(sx+1, sy+1, TileWidth-2, TileHeight-2, color.White)
i.DrawRect(sx+2, sy+2, TileWidth-4, TileHeight-4, color.Black) i.DrawRect(sx+2, sy+2, TileWidth-4, TileHeight-4, color.Black)
}
m.mapTools.Draw(i, x, y, width, 32)
return nil return nil
} }

View File

@ -22,7 +22,7 @@ import (
type TileSetView struct { type TileSetView struct {
tileSet *TileSet tileSet *TileSet
selectedTile int selectedTile int
dragging bool focused bool
} }
func NewTileSetView(tileSet *TileSet) *TileSetView { func NewTileSetView(tileSet *TileSet) *TileSetView {
@ -42,13 +42,13 @@ func (t *TileSetView) Update(input *Input, ox, oy, width, height int) error {
return nil return nil
} }
if input.MouseButtonState(ebiten.MouseButtonLeft) == 0 { if input.MouseButtonState(ebiten.MouseButtonLeft) == 0 {
t.dragging = false t.focused = false
return nil return nil
} }
if input.MouseButtonState(ebiten.MouseButtonLeft) == 1 { if input.MouseButtonState(ebiten.MouseButtonLeft) == 1 {
t.dragging = true t.focused = true
} }
// TODO: Implement dragging to select multiple tiles. // TODO: Implement focused to select multiple tiles.
if input.MouseButtonState(ebiten.MouseButtonLeft) == 1 { if input.MouseButtonState(ebiten.MouseButtonLeft) == 1 {
tile, err := t.tileSet.TileAt(x, y) tile, err := t.tileSet.TileAt(x, y)