-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀️ Merge branch 'hugo/feature/Add-DnDInOrder-in-new-GEK' into develop
- Loading branch information
Showing
13 changed files
with
655 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
.../GameEngineKit/Sources/_NewSystem/Views/DnD/OneToOne/BaseScene/DnDOneToOneBaseScene.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Leka - iOS Monorepo | ||
// Copyright APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import ContentKit | ||
import SpriteKit | ||
import SwiftUI | ||
|
||
class DnDOneToOneBaseScene: SKScene { | ||
// MARK: Lifecycle | ||
|
||
init(viewModel: DnDOneToOneViewModel) { | ||
self.viewModel = viewModel | ||
super.init(size: CGSize.zero) | ||
} | ||
|
||
@available(*, unavailable) | ||
required init?(coder _: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
// MARK: Internal | ||
|
||
override func didMove(to _: SKView) { | ||
self.reset() | ||
} | ||
|
||
// MARK: - Touch Interaction | ||
|
||
override func touchesBegan(_ touches: Set<UITouch>, with _: UIEvent?) { | ||
guard let touch = touches.first else { | ||
return | ||
} | ||
let location = touch.location(in: self) | ||
if let node = atPoint(location) as? DnDAnswerNode { | ||
for choice in self.viewModel.choices where node.id == choice.id && node.isDraggable { | ||
selectedNodes[touch] = node | ||
self.viewModel.onTouch(.began, choice: node) | ||
} | ||
} | ||
} | ||
|
||
override func touchesMoved(_ touches: Set<UITouch>, with _: UIEvent?) { | ||
guard let touch = touches.first else { | ||
return | ||
} | ||
let location = touch.location(in: self) | ||
if let node = selectedNodes[touch] { | ||
node.position = location | ||
} | ||
} | ||
|
||
override func touchesEnded(_ touches: Set<UITouch>, with _: UIEvent?) { | ||
guard let touch = touches.first, | ||
let playedNode = self.selectedNodes[touch] | ||
else { | ||
return | ||
} | ||
|
||
self.viewModel.onTouch(.ended, choice: playedNode, destination: self.dropZonesNodes.first(where: { | ||
$0.frame.contains(touch.location(in: self)) | ||
})) | ||
} | ||
|
||
func reset() { | ||
backgroundColor = .clear | ||
removeAllChildren() | ||
removeAllActions() | ||
|
||
self.spacer = size.width / CGFloat(self.viewModel.choices.count + 1) | ||
|
||
self.layoutChoices() | ||
self.layoutDropzones() | ||
self.viewModel.setAlreadyOrderedNodes() | ||
} | ||
|
||
func exerciseCompletedBehavior() { | ||
for node in self.answerNodes { | ||
node.isDraggable = false | ||
} | ||
} | ||
|
||
func layoutChoices() { | ||
for (index, choice) in self.viewModel.choices.enumerated() { | ||
choice.initialPosition = self.setInitialPosition(index) | ||
choice.position = choice.initialPosition! | ||
self.answerNodes.append(choice) | ||
|
||
let shadowChoice = DnDShadowNode(node: choice) | ||
addChild(shadowChoice) | ||
addChild(choice) | ||
} | ||
} | ||
|
||
func layoutDropzones() { | ||
for (index, dropzone) in self.viewModel.dropzones.enumerated() { | ||
dropzone.position = self.setInitialDropZonePosition(index) | ||
self.dropZonesNodes.append(dropzone) | ||
addChild(dropzone) | ||
} | ||
} | ||
|
||
func setInitialPosition(_ index: Int) -> CGPoint { | ||
CGPoint(x: self.spacer * CGFloat(index + 1), y: size.height - self.viewModel.choices[0].size.height * 0.8) | ||
} | ||
|
||
func setInitialDropZonePosition(_ index: Int) -> CGPoint { | ||
CGPoint(x: self.spacer * CGFloat(index + 1), y: self.viewModel.choices[0].size.height * 0.8) | ||
} | ||
|
||
// MARK: Private | ||
|
||
private var spacer: CGFloat = .zero | ||
private var viewModel: DnDOneToOneViewModel | ||
private var expectedItemsNodes: [String: [SKSpriteNode]] = [:] | ||
private var selectedNodes: [UITouch: DnDAnswerNode] = [:] | ||
private var dropZonesNodes: [DnDDropZoneNode] = [] | ||
private var answerNodes: [DnDAnswerNode] = [] | ||
private var playedNode: DnDAnswerNode? | ||
} |
Oops, something went wrong.