From 94d7be35ff8b08b875a2654b8dc0b556cf699eb2 Mon Sep 17 00:00:00 2001 From: Niko <> Date: Sat, 2 May 2020 23:12:37 +0200 Subject: [PATCH] implemented ButtonEntity --- GoldWars/GoldWars/Components/ButtonNode.swift | 59 +++++++++++-------- GoldWars/GoldWars/Scenes/MenuScene.swift | 16 ++--- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/GoldWars/GoldWars/Components/ButtonNode.swift b/GoldWars/GoldWars/Components/ButtonNode.swift index be4176c..d1d6ad2 100644 --- a/GoldWars/GoldWars/Components/ButtonNode.swift +++ b/GoldWars/GoldWars/Components/ButtonNode.swift @@ -11,39 +11,48 @@ import SpriteKit class ButtonNode : SKSpriteNode { - var isEnabled:Bool + var isEnabled:Bool{ + didSet{ + if isEnabled { + self.alpha = 1 + self.childNode(withName: "label")?.alpha = 1 + } else { + self.alpha = 0.3 + self.childNode(withName: "label")?.alpha = 0.3 + } + } + } let onButtonPress: () -> () init(iconName: String, text: String,isEnabled:Bool, position: CGPoint, onButtonPress: @escaping () -> ()) { + self.onButtonPress = onButtonPress + self.isEnabled = isEnabled + let texture = SKTexture(imageNamed: "yellow_button04") + super.init(texture: texture, color: SKColor.white, size: texture.size()) + self.position = position - self.onButtonPress = onButtonPress - self.isEnabled = isEnabled - let texture = SKTexture(imageNamed: "yellow_button04") - super.init(texture: texture, color: SKColor.white, size: texture.size()) - self.position = position + let label = SKLabelNode(fontNamed: "Courier-Bold") - let label = SKLabelNode(fontNamed: "Courier-Bold") + label.fontSize = 30 + label.fontColor = SKColor.black - label.fontSize = 30 - label.fontColor = SKColor.black + label.zPosition = 1 + label.verticalAlignmentMode = .center + label.text = text + label.name = "label" - label.zPosition = 1 - label.verticalAlignmentMode = .center - label.text = text - label.name = "label" - - if !iconName.isEmpty { - label.position = CGPoint(x: size.width * 0.25, y: 0) - let icon = SKSpriteNode(imageNamed: iconName) - icon.position = CGPoint(x: -size.width * 0.25, y: 0) - icon.zPosition = 1 - self.addChild(icon) - } else { - label.position = CGPoint(x: 0, y: 0) - } - self.addChild(label) - isUserInteractionEnabled = true + if !iconName.isEmpty { + label.position = CGPoint(x: size.width * 0.25, y: 0) + let icon = SKSpriteNode(imageNamed: iconName) + icon.position = CGPoint(x: -size.width * 0.25, y: 0) + icon.zPosition = 1 + self.addChild(icon) + } else { + label.position = CGPoint(x: 0, y: 0) + } + self.addChild(label) + isUserInteractionEnabled = true } override func touchesBegan(_ touches: Set, with event: UIEvent?) { diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 46f3ae6..8557396 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -12,7 +12,7 @@ class MenuScene: SKScene { var entityManager: EntityManager! - override func sceneDidLoad() { + override func sceneDidLoad() { entityManager = EntityManager(scene: self) let midX = self.size.width / 2 let midY = self.size.height / 2 @@ -23,18 +23,14 @@ class MenuScene: SKScene { onButtonPress: { self.loadScene(scene: GameScene(size: self.size)) })) - entityManager.add(Button(name: "settingsButton", iconName: "", text: "Settings", position: CGPoint(x: midX, y: midY - 80 ), onButtonPress: { - - })) - - } - - + //TODO: create Settings Scene + })) + } func backgroundInit(){ for i in 0...2 { @@ -43,7 +39,6 @@ class MenuScene: SKScene { sky.zPosition = -1 sky.size = CGSize(width: self.size.width, height: self.size.height) sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (self.frame.size.height / 2)) - self.addChild(sky) } } @@ -61,7 +56,6 @@ class MenuScene: SKScene { node.position.x += (self.scene?.size.width)! * 3 } })) - - entityManager.getButtonByName(buttonName: "startGameButton").isEnabled = GameCenterHelper.isAuthenticated + entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated } }