mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
example/mapeditor: Bug fix: dragging on the map view shouldn't affect the tileset view
This commit is contained in:
parent
30aece193b
commit
0c7716586a
37
example/mapeditor/mapeditor/input.go
Normal file
37
example/mapeditor/mapeditor/input.go
Normal 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]
|
||||
}
|
@ -30,6 +30,7 @@ const (
|
||||
)
|
||||
|
||||
type MainEditor struct {
|
||||
input Input
|
||||
tileSetView *TileSetView
|
||||
tileSetPanel *Panel
|
||||
mapView *MapView
|
||||
@ -58,11 +59,13 @@ func NewMainEditor(tileSet *TileSet, m *Map) (*MainEditor, 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
|
||||
}
|
||||
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 nil
|
||||
|
@ -20,21 +20,22 @@ import (
|
||||
)
|
||||
|
||||
type MapView struct {
|
||||
m *Map
|
||||
tileSet *TileSet
|
||||
cursorX int
|
||||
cursorY int
|
||||
m *Map
|
||||
tileSet *TileSet
|
||||
cursorX int
|
||||
cursorY int
|
||||
dragging bool
|
||||
}
|
||||
|
||||
func NewMapView(m *Map) *MapView {
|
||||
return &MapView{
|
||||
m: m,
|
||||
cursorX: -1,
|
||||
cursorY: -1,
|
||||
cursorX: 0,
|
||||
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
|
||||
|
||||
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.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)
|
||||
}
|
||||
return nil
|
||||
|
@ -22,6 +22,7 @@ import (
|
||||
type TileSetView struct {
|
||||
tileSet *TileSet
|
||||
selectedTile int
|
||||
dragging bool
|
||||
}
|
||||
|
||||
func NewTileSetView(tileSet *TileSet) *TileSetView {
|
||||
@ -30,18 +31,31 @@ func NewTileSetView(tileSet *TileSet) *TileSetView {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TileSetView) Update(ox, oy int) error {
|
||||
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
||||
x, y := ebiten.CursorPosition()
|
||||
x -= ox
|
||||
y -= oy
|
||||
if 0 <= x && 0 <= y && x < TileWidth*TileSetXNum && y < TileHeight*TileSetYNum {
|
||||
tile, err := t.tileSet.TileAt(x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.selectedTile = tile
|
||||
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.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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user