Remove example/mapeditor

This commit is contained in:
Hajime Hoshi 2015-06-07 18:13:07 +09:00
parent 2e581abe2b
commit ab5a598d27
9 changed files with 0 additions and 670 deletions

View File

@ -1,62 +0,0 @@
// 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 main
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/example/mapeditor/mapeditor"
"image/color"
_ "image/png"
"log"
)
const (
ScreenWidth = 1024
ScreenHeight = 768
)
var editor *mapeditor.MainEditor
func init() {
tileSetImg, _, err := ebitenutil.NewImageFromFile("images/platform/tileset.png", ebiten.FilterNearest)
if err != nil {
panic(err)
}
tileSet := mapeditor.NewTileSet(tileSetImg)
m := mapeditor.NewMap(20, 15)
editor, err = mapeditor.NewMainEditor(tileSet, m)
if err != nil {
panic(err)
}
}
func update(screen *ebiten.Image) error {
if err := editor.Update(); err != nil {
return err
}
backgroundColor := color.RGBA{0xc0, 0xc0, 0xc0, 0xff}
screen.Fill(backgroundColor)
return editor.Draw(screen)
}
func main() {
if err := ebiten.Run(update, ScreenWidth, ScreenHeight, 1, "Map Editor (Ebiten Demo)"); err != nil {
log.Fatal(err)
}
}

View File

@ -1,37 +0,0 @@
// 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"
)
type Input struct {
mouseButtonStates [4]int
}
func (i *Input) Update() {
for b, _ := range i.mouseButtonStates {
if !ebiten.IsMouseButtonPressed(ebiten.MouseButton(b)) {
i.mouseButtonStates[b] = 0
continue
}
i.mouseButtonStates[b]++
}
}
func (i *Input) MouseButtonState(m ebiten.MouseButton) int {
return i.mouseButtonStates[m]
}

View File

@ -1,82 +0,0 @@
// 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"
)
const (
tileSetX = 16
tileSetY = 16
tileSetWidth = TileSetXNum * TileWidth
tileSetHeight = TileSetYNum * TileHeight
mapViewX = 16 + TileWidth*TileSetXNum + 16
mapViewY = 16
mapViewWidth = 720
mapViewHeight = 736
)
type MainEditor struct {
input Input
tileSetView *TileSetView
tileSetPanel *Panel
mapView *MapView
mapPanel *Panel
mapCursorX int
mapCursorY int
}
func NewMainEditor(tileSet *TileSet, m *Map) (*MainEditor, error) {
tileSetView := NewTileSetView(tileSet)
tileSetPanel, err := NewPanel(tileSetView, tileSetX, tileSetY, tileSetWidth, tileSetHeight)
if err != nil {
return nil, err
}
mapView := NewMapView(m)
mapPanel, err := NewPanel(mapView, mapViewX, mapViewY, mapViewWidth, mapViewHeight)
if err != nil {
return nil, err
}
return &MainEditor{
tileSetView: tileSetView,
tileSetPanel: tileSetPanel,
mapView: mapView,
mapPanel: mapPanel,
}, nil
}
func (m *MainEditor) Update() error {
m.input.Update()
if err := m.tileSetView.Update(&m.input, tileSetX, tileSetY, TileWidth*TileSetXNum, TileHeight*TileSetYNum); err != nil {
return err
}
tileSet := m.tileSetView.tileSet
if err := m.mapView.Update(&m.input, mapViewX, mapViewY, mapViewWidth, mapViewHeight, tileSet, m.tileSetView.selectedTile); err != nil {
return err
}
return nil
}
func (m *MainEditor) Draw(screen *ebiten.Image) error {
if err := m.tileSetPanel.Draw(screen); err != nil {
return err
}
if err := m.mapPanel.Draw(screen); err != nil {
return err
}
return nil
}

View File

@ -1,89 +0,0 @@
// 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"
"image/color"
)
type Map struct {
width int
height int
tiles []int
}
func NewMap(width, height int) *Map {
return &Map{
width: width,
height: height,
tiles: make([]int, width*height),
}
}
func (m *Map) TileAt(x, y int) (int, error) {
/*if x < 0 || y < 0 || TileWidth*TileSetXNum <= x || TileHeight*TileSetYNum <= y {
return 0, fmt.Errorf("out of range: (%d, %d)", x, y)
}*/
return x/TileWidth + y/TileHeight, nil
}
func (m *Map) SetTile(x, y int, tile int) {
i := x + y*m.width
m.tiles[i] = tile
}
type TileRects struct {
m *Map
}
func (t *TileRects) Len() int {
return t.m.width * t.m.height
}
func (t *TileRects) Src(i int) (x0, y0, x1, y1 int) {
tile := t.m.tiles[i]
x := tile % TileSetXNum * TileLogicWidth
y := tile / TileSetXNum * TileLogicHeight
return x, y, x + TileLogicWidth, y + TileLogicHeight
}
func (t *TileRects) Dst(i int) (x0, y0, x1, y1 int) {
x := i % t.m.width * TileLogicWidth
y := i / t.m.width * TileLogicHeight
return x, y, x + TileLogicWidth, y + TileLogicHeight
}
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},
}
op.GeoM.Translate(float64(x), float64(y))
if err := i.DrawImage(tilesBackground, op); err != nil {
return err
}
op = &ebiten.DrawImageOptions{
ImageParts: &TileRects{m},
}
op.GeoM.Scale(2, 2)
op.GeoM.Translate(float64(x), float64(y))
if err := i.DrawImage(tileSetImg, op); err != nil {
return err
}
return nil
}

View File

@ -1,68 +0,0 @@
// 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/color"
_ "image/png"
)
type MapTool int
const (
MapToolScroll MapTool = iota
MapToolPen
MapToolFill
)
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
current MapTool
}
func NewMapTools() *MapTools {
return &MapTools{}
}
func (m *MapTools) Update() {
}
func (m *MapTools) Current() MapTool {
return m.current
}
func (m *MapTools) Draw(i *ebiten.Image, x, y, width, height int) error {
i.DrawFilledRect(x+int(m.current)*32, y, 32, 32, color.White)
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

@ -1,90 +0,0 @@
// 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"
"image/color"
)
type MapView struct {
m *Map
mapTools *MapTools
tileSet *TileSet
cursorX int
cursorY int
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 - mapYOffset
if x < 0 || y < 0 || width <= x || height <= y {
return nil
}
if m.m.width*TileWidth <= x || m.m.height*TileHeight <= y {
return nil
}
m.cursorX = x / TileWidth
m.cursorY = y / TileHeight
if input.MouseButtonState(ebiten.MouseButtonLeft) == 0 {
m.focused = false
return nil
}
if input.MouseButtonState(ebiten.MouseButtonLeft) == 1 {
m.focused = true
}
switch m.mapTools.Current() {
case MapToolPen:
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 {
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.mapTools.Current() != MapToolScroll && 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
}

View File

@ -1,59 +0,0 @@
// 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"
)
type drawer interface {
Draw(target *ebiten.Image, x, y, width, height int) error
}
type Panel struct {
drawer drawer
panelX int
panelY int
regionX int
regionY int
width int
height int
offscreen *ebiten.Image
}
func NewPanel(drawer drawer, x, y, width, height int) (*Panel, error) {
offscreen, err := ebiten.NewImage(width, height, ebiten.FilterNearest)
if err != nil {
return nil, err
}
return &Panel{
drawer: drawer,
panelX: x,
panelY: y,
width: width,
height: height,
offscreen: offscreen,
}, nil
}
func (p *Panel) Draw(target *ebiten.Image) error {
if err := p.drawer.Draw(p.offscreen, p.regionX, p.regionY, p.width, p.height); err != nil {
return err
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(p.panelX), float64(p.panelY))
return target.DrawImage(p.offscreen, op)
}

View File

@ -1,109 +0,0 @@
// 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 (
"fmt"
"github.com/hajimehoshi/ebiten"
"image/color"
)
const (
TileLogicWidth = 16
TileLogicHeight = 16
TileWidth = 32
TileHeight = 32
TileSetXNum = 8
TileSetYNum = 16
)
var tilesBackground *ebiten.Image
const tilesBackgroundWidth = 32
const tilesBackgroundHeight = 32
func init() {
var err error
tilesBackground, err = ebiten.NewImage(tilesBackgroundWidth, tilesBackgroundHeight, ebiten.FilterNearest)
if err != nil {
panic(err)
}
blue := color.RGBA{0x00, 0x00, 0x80, 0xff}
black := color.Black
tilesBackground.DrawFilledRect(0, 0, 16, 16, blue)
tilesBackground.DrawFilledRect(16, 0, 16, 16, black)
tilesBackground.DrawFilledRect(0, 16, 16, 16, black)
tilesBackground.DrawFilledRect(16, 16, 16, 16, blue)
}
type TilesBackgroundRects struct {
xNum int
yNum int
}
func (t *TilesBackgroundRects) Len() int {
return t.xNum * t.yNum
}
func (t *TilesBackgroundRects) Src(i int) (x0, y0, x1, y1 int) {
return 0, 0, tilesBackgroundWidth, tilesBackgroundHeight
}
func (t *TilesBackgroundRects) Dst(i int) (x0, y0, x1, y1 int) {
x := i % t.xNum * tilesBackgroundWidth
y := i / t.xNum * tilesBackgroundHeight
return x, y, x + tilesBackgroundWidth, y + tilesBackgroundHeight
}
type TileSet struct {
image *ebiten.Image
}
func NewTileSet(img *ebiten.Image) *TileSet {
return &TileSet{
image: img,
}
}
func (t *TileSet) TileAt(x, y int) (int, error) {
if x < 0 || y < 0 || TileWidth*TileSetXNum <= x || TileHeight*TileSetYNum <= y {
return 0, fmt.Errorf("out of range: (%d, %d)", x, y)
}
return x/TileWidth + y/TileHeight*TileSetXNum, nil
}
func (t *TileSet) Draw(i *ebiten.Image, x, y, width, height int) error {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(x), float64(y))
op.ImageParts = &TilesBackgroundRects{8, 16}
if err := i.DrawImage(tilesBackground, op); err != nil {
return err
}
op = &ebiten.DrawImageOptions{}
op.GeoM.Scale(2, 2)
op.GeoM.Translate(float64(x), float64(y))
if err := i.DrawImage(t.image, op); err != nil {
return err
}
/*sx := x + s%TileSetXNum*TileWidth
sy := y + s/TileSetXNum*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)*/
return nil
}

View File

@ -1,74 +0,0 @@
// 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"
"image/color"
)
type TileSetView struct {
tileSet *TileSet
selectedTile int
focused bool
}
func NewTileSetView(tileSet *TileSet) *TileSetView {
return &TileSetView{
tileSet: tileSet,
}
}
func (t *TileSetView) Update(input *Input, ox, oy, width, height int) error {
x, y := ebiten.CursorPosition()
x -= ox
y -= oy
if x < 0 || y < 0 || width <= x || height <= y {
return nil
}
if TileWidth*TileSetXNum <= x && TileHeight*TileSetYNum <= y {
return nil
}
if input.MouseButtonState(ebiten.MouseButtonLeft) == 0 {
t.focused = false
return nil
}
if input.MouseButtonState(ebiten.MouseButtonLeft) == 1 {
t.focused = true
}
// TODO: Implement focused to select multiple tiles.
if input.MouseButtonState(ebiten.MouseButtonLeft) == 1 {
tile, err := t.tileSet.TileAt(x, y)
if err != nil {
return err
}
t.selectedTile = tile
}
return nil
}
func (t *TileSetView) Draw(i *ebiten.Image, x, y, width, height int) error {
t.tileSet.Draw(i, x, y, width, height)
s := t.selectedTile
sx := x + s%TileSetXNum*TileWidth
sy := y + s/TileSetXNum*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)
return nil
}