From adf01db4bfef0db9be19fa204a69ad83f3812c05 Mon Sep 17 00:00:00 2001 From: Niko Jochim <82joni1bif@hft-stuttgart.de> Date: Thu, 18 Jun 2020 22:28:49 +0200 Subject: [PATCH] FIX Slider performance * create hidden silderKnob --- GoldWars/GoldWars/Components/SliderNode.swift | 112 ++++++++++-------- .../GoldWars/Entities/EntityManager.swift | 8 +- GoldWars/GoldWars/Scenes/GameScene.swift | 1 + 3 files changed, 69 insertions(+), 52 deletions(-) diff --git a/GoldWars/GoldWars/Components/SliderNode.swift b/GoldWars/GoldWars/Components/SliderNode.swift index 2ae988e..78adda3 100644 --- a/GoldWars/GoldWars/Components/SliderNode.swift +++ b/GoldWars/GoldWars/Components/SliderNode.swift @@ -10,58 +10,72 @@ import SpriteKit class SliderNode :SKNode { - - var sliderLine :SKShapeNode - var sliderKnob :SliderKnob - var width: CGFloat - - var getValue: CGFloat{ - get{ - return ((sliderKnob.position.x.rounded() - sliderKnob.min) / width) - } - } - - init(width: CGFloat, position: CGPoint) { - self.width = width - sliderLine = SKShapeNode(rectOf: CGSize(width: width, height: 8)) - sliderLine.position = position - sliderLine.fillColor = SKColor.white + + var sliderLine :SKShapeNode + var hiddenKnob :SliderKnob + var silderKnob :SKSpriteNode + var width: CGFloat + + var getValue: CGFloat{ + get{ + silderKnob.position = hiddenKnob.position + return ((hiddenKnob.position.x.rounded() - hiddenKnob.min) / width) + } + } + + init(width: CGFloat, position: CGPoint) { + self.width = width + sliderLine = SKShapeNode(rectOf: CGSize(width: width, height: 8)) + sliderLine.position = position + sliderLine.fillColor = SKColor.white sliderLine.zPosition = 4 - sliderKnob = SliderKnob(circleOfRadius: 20) - sliderKnob.min = position.x - width / 2 - sliderKnob.max = position.x + width / 2 - sliderKnob.fillColor = SKColor.red - sliderKnob.zPosition = sliderLine.zPosition + 1 - sliderKnob.position = CGPoint(x: sliderLine.position.x, y: sliderLine.position.y + 1) - super.init() - - } - - required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } + + + hiddenKnob = SliderKnob(circleOfRadius: 60) + hiddenKnob.min = position.x - width / 2 + hiddenKnob.max = position.x + width / 2 + hiddenKnob.fillColor = SKColor.red + hiddenKnob.alpha = 0.001 + hiddenKnob.zPosition = sliderLine.zPosition + 1 + hiddenKnob.position = CGPoint(x: sliderLine.position.x, y: sliderLine.position.y + 1) + + silderKnob = SKSpriteNode(texture: SKTexture(imageNamed: "yellow_boxTick")) + silderKnob.position = hiddenKnob.position + silderKnob.zPosition = hiddenKnob.zPosition - 1 + silderKnob.size = CGSize(width: 50, height: 50) + + super.init() + self.name = "slider" + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + } class SliderKnob: SKShapeNode { - var min = CGFloat() - var max = CGFloat() - - override func touchesMoved(_ touches: Set, with event: UIEvent?) { - - for touch in touches { - let touchLocation = touch.location(in: self.scene!) - - if self.position.x >= min - 1 && self.position.x <= max + 1{ - self.position.x = touchLocation.x - } - - if(self.position.x <= min){ - self.position.x = min - } - if(self.position.x >= max){ - self.position.x = max - } - } - } + var min = CGFloat() + var max = CGFloat() + + override func touchesMoved(_ touches: Set, with event: UIEvent?) { + + for touch in touches { + let touchLocation = touch.location(in: self.scene!) + + if self.position.x >= min - 1 && self.position.x <= max + 1{ + self.position.x = touchLocation.x + } + + if(self.position.x <= min){ + self.position.x = min + } + if(self.position.x >= max){ + self.position.x = max + + } + } + } } diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index d256f8e..83e8b7e 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -88,8 +88,9 @@ class EntityManager { } } if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode { - scene.addChild(sliderNode.sliderKnob) + scene.addChild(sliderNode.hiddenKnob) scene.addChild(sliderNode.sliderLine) + scene.addChild(sliderNode.silderKnob) } if let labelNode = entity.component(ofType: LabelComponent.self)?.labelNode { scene.addChild(labelNode) @@ -102,8 +103,9 @@ class EntityManager { spriteNode.removeFromParent() } if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode { - sliderNode.sliderKnob.removeFromParent() + sliderNode.hiddenKnob.removeFromParent() sliderNode.sliderLine.removeFromParent() + sliderNode.silderKnob.removeFromParent() } if let modalButton = entity.component(ofType: ButtonComponent.self) { modalButton.buttonNode.removeFromParent() @@ -336,7 +338,7 @@ class EntityManager { if let slider = modal.component(ofType: SliderComponent.self) { slider.sliderNode.removeFromParent() - slider.sliderNode.sliderKnob.removeFromParent() + slider.sliderNode.hiddenKnob.removeFromParent() slider.sliderNode.sliderLine.removeFromParent() } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 8c7472d..5d1105b 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -81,6 +81,7 @@ class GameScene: SKScene{ if GameCenterManager.sharedInstance.gameEnded && !gameEndEffects { gameEnd() } + } func addAttackDetails(touchLocation: CGPoint){