mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 13:07:26 +01:00
example/mapeditor: Add map tools (which doesn't work yet)
This commit is contained in:
parent
0c7716586a
commit
dee7b1a328
BIN
example/images/mapeditor/maptools.png
Normal file
BIN
example/images/mapeditor/maptools.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 336 B |
@ -19,6 +19,7 @@ import (
|
||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||
"github.com/hajimehoshi/ebiten/example/mapeditor/mapeditor"
|
||||
"image/color"
|
||||
_ "image/png"
|
||||
"log"
|
||||
)
|
||||
|
||||
|
@ -26,7 +26,7 @@ const (
|
||||
mapViewX = 16 + TileWidth*TileSetXNum + 16
|
||||
mapViewY = 16
|
||||
mapViewWidth = 720
|
||||
mapViewHeight = 720
|
||||
mapViewHeight = 736
|
||||
)
|
||||
|
||||
type MainEditor struct {
|
||||
|
@ -66,8 +66,8 @@ func (t *TileRects) Dst(i int) (x0, y0, x1, y1 int) {
|
||||
return x, y, x + TileLogicWidth, y + TileLogicHeight
|
||||
}
|
||||
|
||||
func (m *Map) Draw(i *ebiten.Image, tileSetImg *ebiten.Image, x, y int) error {
|
||||
i.Fill(color.RGBA{0x80, 0x80, 0x80, 0xff})
|
||||
func (m *Map) Draw(i *ebiten.Image, tileSetImg *ebiten.Image, x, y, width, height int) error {
|
||||
i.DrawFilledRect(x, y, width, height, color.RGBA{0x80, 0x80, 0x80, 0xff})
|
||||
|
||||
op := &ebiten.DrawImageOptions{
|
||||
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{
|
||||
ImageParts: &TileRects{m},
|
||||
}
|
||||
op.GeoM.Translate(float64(x), float64(y))
|
||||
op.GeoM.Scale(2, 2)
|
||||
op.GeoM.Translate(float64(x), float64(y))
|
||||
if err := i.DrawImage(tileSetImg, op); err != nil {
|
||||
return err
|
||||
}
|
||||
|
52
example/mapeditor/mapeditor/maptools.go
Normal file
52
example/mapeditor/mapeditor/maptools.go
Normal 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
|
||||
}
|
@ -21,26 +21,30 @@ import (
|
||||
|
||||
type MapView struct {
|
||||
m *Map
|
||||
mapTools *MapTools
|
||||
tileSet *TileSet
|
||||
cursorX int
|
||||
cursorY int
|
||||
dragging bool
|
||||
focused bool // TODO: FocusManager is needed?
|
||||
}
|
||||
|
||||
func NewMapView(m *Map) *MapView {
|
||||
return &MapView{
|
||||
m: m,
|
||||
mapTools: NewMapTools(),
|
||||
cursorX: 0,
|
||||
cursorY: 0,
|
||||
}
|
||||
}
|
||||
|
||||
const mapYOffset = 32
|
||||
|
||||
func (m *MapView) Update(input *Input, ox, oy, width, height int, tileSet *TileSet, selectedTile int) error {
|
||||
m.tileSet = tileSet
|
||||
|
||||
x, y := ebiten.CursorPosition()
|
||||
x -= ox
|
||||
y -= oy
|
||||
x += -ox
|
||||
y += -oy - mapYOffset
|
||||
if x < 0 || y < 0 || width <= x || height <= y {
|
||||
return nil
|
||||
}
|
||||
@ -50,31 +54,33 @@ func (m *MapView) Update(input *Input, ox, oy, width, height int, tileSet *TileS
|
||||
m.cursorX = x / TileWidth
|
||||
m.cursorY = y / TileHeight
|
||||
if input.MouseButtonState(ebiten.MouseButtonLeft) == 0 {
|
||||
m.dragging = false
|
||||
m.focused = false
|
||||
return nil
|
||||
}
|
||||
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)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if m.cursorX == -1 || m.cursorY == -1 {
|
||||
return nil
|
||||
}
|
||||
sx := x + m.cursorX*TileWidth
|
||||
sy := y + m.cursorY*TileHeight
|
||||
if m.cursorX != -1 && m.cursorY != -1 {
|
||||
sx := mapX + m.cursorX*TileWidth
|
||||
sy := mapY + m.cursorY*TileHeight
|
||||
i.DrawRect(sx, sy, TileWidth, TileHeight, color.Black)
|
||||
i.DrawRect(sx+1, sy+1, TileWidth-2, TileHeight-2, color.White)
|
||||
i.DrawRect(sx+2, sy+2, TileWidth-4, TileHeight-4, color.Black)
|
||||
}
|
||||
|
||||
m.mapTools.Draw(i, x, y, width, 32)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ import (
|
||||
type TileSetView struct {
|
||||
tileSet *TileSet
|
||||
selectedTile int
|
||||
dragging bool
|
||||
focused bool
|
||||
}
|
||||
|
||||
func NewTileSetView(tileSet *TileSet) *TileSetView {
|
||||
@ -42,13 +42,13 @@ func (t *TileSetView) Update(input *Input, ox, oy, width, height int) error {
|
||||
return nil
|
||||
}
|
||||
if input.MouseButtonState(ebiten.MouseButtonLeft) == 0 {
|
||||
t.dragging = false
|
||||
t.focused = false
|
||||
return nil
|
||||
}
|
||||
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 {
|
||||
tile, err := t.tileSet.TileAt(x, y)
|
||||
|
Loading…
Reference in New Issue
Block a user