Skip to content

Commit

Permalink
coordinate fixes, add dynamic features
Browse files Browse the repository at this point in the history
- add dynamics properties to layers and objects
- `SKTilemap.baseLayer` is ignored when querying layers
- add Data extension to check for compressed data
- fix coordinate error with negative tile coordinates
  • Loading branch information
mfessenden committed Nov 8, 2016
1 parent 5ea1064 commit df0cf76
Show file tree
Hide file tree
Showing 24 changed files with 329 additions and 205 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
Change Log
==========================
1.0.5
--------------------------

#### Changes
- add dynamics properties to layers and objects
- `SKTilemap.baseLayer` is ignored when querying layers
- add Data extension to check for compressed data
- fix coordinate error with negative tile coordinates

1.0.4
--------------------------
Expand Down
2 changes: 1 addition & 1 deletion Resources/ortho4-16x16.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
</layer>
<objectgroup name="Locations" visible="0">
<objectgroup name="Locations">
<properties>
<property name="lineWidth" type="float" value="1.5"/>
</properties>
Expand Down
19 changes: 14 additions & 5 deletions Resources/staggered-64x33.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="staggered" renderorder="right-down" width="15" height="35" tilewidth="64" tileheight="33" staggeraxis="y" staggerindex="odd" backgroundcolor="#000000" nextobjectid="4">
<map version="1.0" orientation="staggered" renderorder="right-down" width="15" height="35" tilewidth="64" tileheight="33" staggeraxis="y" staggerindex="odd" backgroundcolor="#000000" nextobjectid="12">
<properties>
<property name="autoResize" type="bool" value="true"/>
<property name="frameColor" type="color" value="#ff4f5051"/>
Expand Down Expand Up @@ -180,11 +180,20 @@
</data>
</layer>
<objectgroup name="Locations">
<object id="2" name="carpet" x="64" y="448">
<properties>
<property name="color" type="color" value="#ff55d633"/>
</properties>
<object id="2" name="carpet1" x="64" y="448">
<polygon points="32,16 288,-112 416,-48 160,80"/>
</object>
<object id="7" name="window1" x="612" y="45">
<polygon points="0,0 0,84 53.3333,112 53.3333,28"/>
</object>
<object id="8" name="window2" x="708" y="93">
<polygon points="0,0 0,84 53.3333,112 53.3333,28"/>
</object>
<object id="9" name="window3" x="802" y="139">
<polygon points="0,0 0,84 53.3333,112 53.3333,28"/>
</object>
<object id="10" name="window4" x="900" y="188">
<polygon points="0,0 0,84 53.3333,112 53.3333,28"/>
</object>
</objectgroup>
</map>
4 changes: 4 additions & 0 deletions SKTiled.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,9 @@
CODE_SIGN_IDENTITY = "Mac Developer";
COMBINE_HIDPI_IMAGES = YES;
DEBUG_INFORMATION_FORMAT = dwarf;
DEPLOYMENT_LOCATION = YES;
DEVELOPMENT_TEAM = 747QKN4G7U;
DSTROOT = /;
INFOPLIST_FILE = "$(SRCROOT)/macOS/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.11;
Expand All @@ -539,7 +541,9 @@
CLANG_WARN_SUSPICIOUS_MOVES = YES;
CODE_SIGN_IDENTITY = "Mac Developer";
COMBINE_HIDPI_IMAGES = YES;
DEPLOYMENT_LOCATION = YES;
DEVELOPMENT_TEAM = 747QKN4G7U;
DSTROOT = /;
INFOPLIST_FILE = "$(SRCROOT)/macOS/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.11;
Expand Down
28 changes: 26 additions & 2 deletions Shared/ButtonNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ open class ButtonNode: SKSpriteNode {

// textures
open var selectedTexture: SKTexture!
open var hoveredTexture: SKTexture!
open var defaultTexture: SKTexture! {
didSet {
defaultTexture.filteringMode = .linear
Expand All @@ -40,8 +41,9 @@ open class ButtonNode: SKSpriteNode {
}
}

// action to show highlight scaling
// actions to show highlight scaling & hover (OSX)
fileprivate let scaleAction: SKAction = SKAction.scale(by: 0.95, duration: 0.025)
fileprivate let hoverAction: SKAction = SKAction.colorize(with: SKColor.white, colorBlendFactor: 0.5, duration: 0.025)

public init(defaultImage: String, highlightImage: String, action: @escaping () -> ()) {
buttonAction = action
Expand Down Expand Up @@ -95,11 +97,21 @@ open class ButtonNode: SKSpriteNode {
didSet {
// Guard against repeating the same action.
guard oldValue != wasPressed else { return }
texture = wasPressed ? selectedTexture : defaultTexture
let action = wasPressed ? scaleAction : scaleAction.reversed()
run(action)
}
}

// swap textures when mouse hovers
open var mouseHover = false {
didSet {
// Guard against repeating the same action.
guard oldValue != mouseHover else { return }
texture = mouseHover ? selectedTexture : defaultTexture
let action = mouseHover ? hoverAction : hoverAction.reversed()
run(action)
}
}
}

#if os(iOS)
Expand Down Expand Up @@ -147,6 +159,18 @@ public extension ButtonNode {
#if os(OSX)
extension ButtonNode {

override open func mouseEntered(with event: NSEvent) {
if isUserInteractionEnabled {
if containsEvent(event){
mouseHover = true
}
}
}

override open func mouseExited(with event: NSEvent) {
mouseHover = false
}

override open func mouseDown(with event: NSEvent) {
if isUserInteractionEnabled {
wasPressed = true
Expand Down
96 changes: 52 additions & 44 deletions Shared/SKTiledDemoScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class SKTiledDemoScene: SKTiledScene {
/// global information label font size.
private let labelFontSize: CGFloat = 11

internal var selected: [TiledLayerObject] = []
internal var editMode: Bool = false

override public func didMove(to view: SKView) {
super.didMove(to: view)

Expand Down Expand Up @@ -73,10 +76,7 @@ public class SKTiledDemoScene: SKTiledScene {
if (showGridButton == nil){
showGridButton = ButtonNode(defaultImage: "grid-button-norm", highlightImage: "grid-button-pressed", action: {
guard let tilemap = self.tilemap else { return }
let debugState = !tilemap.baseLayer.showGrid
tilemap.baseLayer.showGrid = debugState

tilemap.baseLayer.drawBounds()
tilemap.debugDraw = !tilemap.debugDraw
})

cameraNode.overlay.addChild(showGridButton)
Expand Down Expand Up @@ -240,7 +240,6 @@ public class SKTiledDemoScene: SKTiledScene {
return true
}


/**
Update HUD elements when the view size changes.
*/
Expand Down Expand Up @@ -321,46 +320,32 @@ extension SKTiledDemoScene {

// get the position in the baseLayer
let positionInLayer = baseLayer.touchLocation(touch)


let coord = baseLayer.coordinateAtTouchLocation(touch)
// add a tile shape to the base layer where the user has clicked

// highlight the current coordinate
let _ = addTileAt(layer: baseLayer, Int(coord.x), Int(coord.y), duration: 5)

// update the tile information label
var coordStr = "Tile: \(coord.coordDescription), \(positionInLayer.roundTo())"
let coordStr = "Coord: \(coord.coordDescription), \(positionInLayer.roundTo())"
tileInformationLabel.isHidden = false
tileInformationLabel.text = coordStr

// tile properties output
var propertiesInfoString = "ID: ~"
if let tile = tilemap.firstTileAt(coord) {
propertiesInfoString = "ID: \(tile.tileData.id)"
propertiesInfoString += ", \(tile.tileData.propertiesString ?? "")"

var propertiesInfoString = ""
if let tile = tilemap.firstTileAt(coord: coord) {
propertiesInfoString = "Tile ID: \(tile.tileData.id)"
if tile.tileData.propertiesString != "" {
propertiesInfoString += "; \(tile.tileData.propertiesString)"
}
}

propertiesInformationLabel.isHidden = false
propertiesInformationLabel.text = propertiesInfoString
}
}

override open func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
// do something here
}
}

override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
// do something here
}
}

override open func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
// do something here
}
}
}
#endif

Expand All @@ -382,24 +367,31 @@ extension SKTiledDemoScene {

// get the position in the baseLayer
let positionInLayer = baseLayer.mouseLocation(event: event)
// get the coordinate at that position
let coord = baseLayer.coordinateAtMouseEvent(event: event)

if (tilemap.isPaused == false){
// highlight the current coordinate
let _ = addTileAt(layer: baseLayer, Int(coord.x), Int(coord.y), duration: 3)
let _ = addTileAt(layer: baseLayer, Int(floor(coord.x)), Int(floor(coord.y)), duration: 3)
}

// update the tile information label
let coordStr = "Tile: \(coord.coordDescription), \(positionInLayer.roundTo())"
let coordStr = "Coord: \(coord.coordDescription), \(positionInLayer.roundTo())"
tileInformationLabel.isHidden = false
tileInformationLabel.text = coordStr

// tile properties output
var propertiesInfoString = "ID: ~"
var propertiesInfoString = ""
if let tile = tilemap.firstTileAt(coord: coord) {
propertiesInfoString = "ID: \(tile.tileData.id)"
if tile.tileData.propertiesString != nil {
propertiesInfoString += "; \(tile.tileData.propertiesString!)"
propertiesInfoString = "Tile ID: \(tile.tileData.id)"
if tile.tileData.propertiesString != "" {
propertiesInfoString += "; \(tile.tileData.propertiesString)"
}

if let layer = tile.layer {
if !selected.contains(layer) {
selected.append(layer)
}
}
}
propertiesInformationLabel.isHidden = false
Expand All @@ -421,20 +413,22 @@ extension SKTiledDemoScene {
let coord = baseLayer.screenToTileCoords(positionInLayer)

tileInformationLabel?.isHidden = false
tileInformationLabel?.text = "Tile: \(coord.coordDescription), \(positionInLayer.roundTo())"
tileInformationLabel?.text = "Coord: \(coord.coordDescription), \(positionInLayer.roundTo())"

// tile properties output
var propertiesInfoString = "ID: ~"
var propertiesInfoString = ""
if let tile = tilemap.firstTileAt(coord: coord) {
propertiesInfoString = "ID: \(tile.tileData.id)"
//tile.highlightWithColor(tilemap.highlightColor)
propertiesInfoString = "Tile ID: \(tile.tileData.id)"
if tile.tileData.propertiesString != "" {
propertiesInfoString += "; \(tile.tileData.propertiesString)"
}
}

propertiesInformationLabel.isHidden = false
propertiesInformationLabel.text = propertiesInfoString
}

override open func mouseDragged(with event: NSEvent) {
guard let cameraNode = cameraNode else { return }
cameraNode.scenePositionChanged(event)
Expand All @@ -443,6 +437,7 @@ extension SKTiledDemoScene {
override open func mouseUp(with event: NSEvent) {
guard let cameraNode = cameraNode else { return }
cameraNode.mouseUp(with: event)
selected = []
}

override open func scrollWheel(with event: NSEvent) {
Expand Down Expand Up @@ -485,17 +480,25 @@ extension SKTiledDemoScene {
// 'G' shows the grid
if event.keyCode == 0x05 {
if let tilemap = tilemap {
tilemap.baseLayer.showGrid = !tilemap.baseLayer.showGrid
tilemap.debugDraw = !tilemap.debugDraw
}
}

// 'H' hides the HUD
if event.keyCode == 0x04 {
cameraNode.showOverlay = !cameraNode.showOverlay
let debugState = !cameraNode.showOverlay
cameraNode.showOverlay = debugState

if let view = self.view {
view.showsFPS = debugState
view.showsNodeCount = debugState
view.showsDrawCount = debugState
}
}

// 'A', '0' reset the camera to 100%
if event.keyCode == 0x00 || event.keyCode == 0x52 || event.keyCode == 0x1D {

// 'A' & '1' reset the camera to 100%
if event.keyCode == 0x12 || event.keyCode == 0x00 {
if let tilemap = tilemap {
cameraNode.resetCamera(toScale: tilemap.worldScale)
} else {
Expand All @@ -512,6 +515,11 @@ extension SKTiledDemoScene {
if event.keyCode == 0x7B {
self.loadPreviousScene()
}

// 'E' toggles edit mode
if event.keyCode == 0x0E {
editMode = !editMode
}
}

/**
Expand Down
Loading

0 comments on commit df0cf76

Please sign in to comment.