From 0c7716586a0123a22b1a5395bcadb10b928815f0 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 5 Feb 2015 11:02:31 +0900 Subject: [PATCH] example/mapeditor: Bug fix: dragging on the map view shouldn't affect the tileset view --- example/mapeditor/mapeditor/input.go | 37 ++++++++++++++++++++++ example/mapeditor/mapeditor/maineditor.go | 7 ++-- example/mapeditor/mapeditor/mapview.go | 24 +++++++++----- example/mapeditor/mapeditor/tilesetview.go | 36 ++++++++++++++------- 4 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 example/mapeditor/mapeditor/input.go diff --git a/example/mapeditor/mapeditor/input.go b/example/mapeditor/mapeditor/input.go new file mode 100644 index 000000000..5fc8c12b5 --- /dev/null +++ b/example/mapeditor/mapeditor/input.go @@ -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] +} diff --git a/example/mapeditor/mapeditor/maineditor.go b/example/mapeditor/mapeditor/maineditor.go index 6a954baa7..1970ac82d 100644 --- a/example/mapeditor/mapeditor/maineditor.go +++ b/example/mapeditor/mapeditor/maineditor.go @@ -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 diff --git a/example/mapeditor/mapeditor/mapview.go b/example/mapeditor/mapeditor/mapview.go index 6342c947a..c24d38aa5 100644 --- a/example/mapeditor/mapeditor/mapview.go +++ b/example/mapeditor/mapeditor/mapview.go @@ -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 diff --git a/example/mapeditor/mapeditor/tilesetview.go b/example/mapeditor/mapeditor/tilesetview.go index 3773159cd..a1ca282d3 100644 --- a/example/mapeditor/mapeditor/tilesetview.go +++ b/example/mapeditor/mapeditor/tilesetview.go @@ -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 }