From ab5a598d278d8dca1d6b650350c357437c0b0e19 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 7 Jun 2015 18:13:07 +0900 Subject: [PATCH] Remove example/mapeditor --- example/mapeditor/main.go | 62 ------------ example/mapeditor/mapeditor/input.go | 37 ------- example/mapeditor/mapeditor/maineditor.go | 82 ---------------- example/mapeditor/mapeditor/map.go | 89 ----------------- example/mapeditor/mapeditor/maptools.go | 68 ------------- example/mapeditor/mapeditor/mapview.go | 90 ----------------- example/mapeditor/mapeditor/panel.go | 59 ----------- example/mapeditor/mapeditor/tileset.go | 109 --------------------- example/mapeditor/mapeditor/tilesetview.go | 74 -------------- 9 files changed, 670 deletions(-) delete mode 100644 example/mapeditor/main.go delete mode 100644 example/mapeditor/mapeditor/input.go delete mode 100644 example/mapeditor/mapeditor/maineditor.go delete mode 100644 example/mapeditor/mapeditor/map.go delete mode 100644 example/mapeditor/mapeditor/maptools.go delete mode 100644 example/mapeditor/mapeditor/mapview.go delete mode 100644 example/mapeditor/mapeditor/panel.go delete mode 100644 example/mapeditor/mapeditor/tileset.go delete mode 100644 example/mapeditor/mapeditor/tilesetview.go diff --git a/example/mapeditor/main.go b/example/mapeditor/main.go deleted file mode 100644 index 2bb4dc599..000000000 --- a/example/mapeditor/main.go +++ /dev/null @@ -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) - } -} diff --git a/example/mapeditor/mapeditor/input.go b/example/mapeditor/mapeditor/input.go deleted file mode 100644 index 5fc8c12b5..000000000 --- a/example/mapeditor/mapeditor/input.go +++ /dev/null @@ -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] -} diff --git a/example/mapeditor/mapeditor/maineditor.go b/example/mapeditor/mapeditor/maineditor.go deleted file mode 100644 index 406addcd0..000000000 --- a/example/mapeditor/mapeditor/maineditor.go +++ /dev/null @@ -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 -} diff --git a/example/mapeditor/mapeditor/map.go b/example/mapeditor/mapeditor/map.go deleted file mode 100644 index 0d0c14c7a..000000000 --- a/example/mapeditor/mapeditor/map.go +++ /dev/null @@ -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 -} diff --git a/example/mapeditor/mapeditor/maptools.go b/example/mapeditor/mapeditor/maptools.go deleted file mode 100644 index 584f97c42..000000000 --- a/example/mapeditor/mapeditor/maptools.go +++ /dev/null @@ -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 -} diff --git a/example/mapeditor/mapeditor/mapview.go b/example/mapeditor/mapeditor/mapview.go deleted file mode 100644 index 8ee2618db..000000000 --- a/example/mapeditor/mapeditor/mapview.go +++ /dev/null @@ -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 -} diff --git a/example/mapeditor/mapeditor/panel.go b/example/mapeditor/mapeditor/panel.go deleted file mode 100644 index d79d4ad60..000000000 --- a/example/mapeditor/mapeditor/panel.go +++ /dev/null @@ -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) -} diff --git a/example/mapeditor/mapeditor/tileset.go b/example/mapeditor/mapeditor/tileset.go deleted file mode 100644 index db452c2fa..000000000 --- a/example/mapeditor/mapeditor/tileset.go +++ /dev/null @@ -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 -} diff --git a/example/mapeditor/mapeditor/tilesetview.go b/example/mapeditor/mapeditor/tilesetview.go deleted file mode 100644 index 3a272c778..000000000 --- a/example/mapeditor/mapeditor/tilesetview.go +++ /dev/null @@ -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 -}