example/mapeditor: Bug fix: dragging on the map view shouldn't affect the tileset view

This commit is contained in:
Hajime Hoshi 2015-02-05 11:02:31 +09:00
parent 30aece193b
commit 0c7716586a
4 changed files with 83 additions and 21 deletions

View File

@ -0,0 +1,37 @@
// 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

@ -30,6 +30,7 @@ const (
) )
type MainEditor struct { type MainEditor struct {
input Input
tileSetView *TileSetView tileSetView *TileSetView
tileSetPanel *Panel tileSetPanel *Panel
mapView *MapView mapView *MapView
@ -58,11 +59,13 @@ func NewMainEditor(tileSet *TileSet, m *Map) (*MainEditor, error) {
} }
func (m *MainEditor) Update() error { func (m *MainEditor) Update() error {
if err := m.tileSetView.Update(tileSetX, tileSetY); err != nil { m.input.Update()
if err := m.tileSetView.Update(&m.input, tileSetX, tileSetY, TileWidth*TileSetXNum, TileHeight*TileSetYNum); err != nil {
return err return err
} }
tileSet := m.tileSetView.tileSet tileSet := m.tileSetView.tileSet
if err := m.mapView.Update(mapViewX, mapViewY, mapViewWidth, mapViewHeight, tileSet, m.tileSetView.selectedTile); err != nil { if err := m.mapView.Update(&m.input, mapViewX, mapViewY, mapViewWidth, mapViewHeight, tileSet, m.tileSetView.selectedTile); err != nil {
return err return err
} }
return nil return nil

View File

@ -20,21 +20,22 @@ import (
) )
type MapView struct { type MapView struct {
m *Map m *Map
tileSet *TileSet tileSet *TileSet
cursorX int cursorX int
cursorY int cursorY int
dragging bool
} }
func NewMapView(m *Map) *MapView { func NewMapView(m *Map) *MapView {
return &MapView{ return &MapView{
m: m, m: m,
cursorX: -1, cursorX: 0,
cursorY: -1, cursorY: 0,
} }
} }
func (m *MapView) Update(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()
@ -48,7 +49,14 @@ func (m *MapView) Update(ox, oy, width, height int, tileSet *TileSet, selectedTi
} }
m.cursorX = x / TileWidth m.cursorX = x / TileWidth
m.cursorY = y / TileHeight m.cursorY = y / TileHeight
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if input.MouseButtonState(ebiten.MouseButtonLeft) == 0 {
m.dragging = false
return nil
}
if input.MouseButtonState(ebiten.MouseButtonLeft) == 1 {
m.dragging = true
}
if m.dragging && ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
m.m.SetTile(m.cursorX, m.cursorY, selectedTile) m.m.SetTile(m.cursorX, m.cursorY, selectedTile)
} }
return nil return nil

View File

@ -22,6 +22,7 @@ import (
type TileSetView struct { type TileSetView struct {
tileSet *TileSet tileSet *TileSet
selectedTile int selectedTile int
dragging bool
} }
func NewTileSetView(tileSet *TileSet) *TileSetView { func NewTileSetView(tileSet *TileSet) *TileSetView {
@ -30,18 +31,31 @@ func NewTileSetView(tileSet *TileSet) *TileSetView {
} }
} }
func (t *TileSetView) Update(ox, oy int) error { func (t *TileSetView) Update(input *Input, ox, oy, width, height int) error {
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { x, y := ebiten.CursorPosition()
x, y := ebiten.CursorPosition() x -= ox
x -= ox y -= oy
y -= oy if x < 0 || y < 0 || width <= x || height <= y {
if 0 <= x && 0 <= y && x < TileWidth*TileSetXNum && y < TileHeight*TileSetYNum { return nil
tile, err := t.tileSet.TileAt(x, y) }
if err != nil { if TileWidth*TileSetXNum <= x && TileHeight*TileSetYNum <= y {
return err return nil
} }
t.selectedTile = tile if input.MouseButtonState(ebiten.MouseButtonLeft) == 0 {
t.dragging = false
return nil
}
if input.MouseButtonState(ebiten.MouseButtonLeft) == 1 {
t.dragging = true
}
// TODO: Implement dragging 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 return nil
} }