Skip to content

Commit

Permalink
ARView: implemented tapping to attach message, small bugs to be fixed…
Browse files Browse the repository at this point in the history
…; MessageView to be updated
  • Loading branch information
Lily Yu committed Dec 11, 2023
1 parent 05792e1 commit 6ad1f63
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 89 deletions.
Binary file not shown.
Binary file not shown.
47 changes: 0 additions & 47 deletions StickAroundUI_AR/StickAroundUI_AR.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@
EE0FB3E92B222E7700241EC8 /* ARView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE0FB3E82B222E7700241EC8 /* ARView.swift */; };
EE0FB3EB2B222EA700241EC8 /* EntityHelperMethods.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE0FB3EA2B222EA700241EC8 /* EntityHelperMethods.swift */; };
EE0FB3ED2B225E7600241EC8 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = EE0FB3EC2B225E7600241EC8 /* GoogleService-Info.plist */; };
EE0FB3F02B22618700241EC8 /* FirebaseAnalytics in Frameworks */ = {isa = PBXBuildFile; productRef = EE0FB3EF2B22618700241EC8 /* FirebaseAnalytics */; };
EE0FB3F22B22618700241EC8 /* FirebaseAnalyticsOnDeviceConversion in Frameworks */ = {isa = PBXBuildFile; productRef = EE0FB3F12B22618700241EC8 /* FirebaseAnalyticsOnDeviceConversion */; };
EE0FB3F42B22618700241EC8 /* FirebaseAnalyticsSwift in Frameworks */ = {isa = PBXBuildFile; productRef = EE0FB3F32B22618700241EC8 /* FirebaseAnalyticsSwift */; };
EE0FB3F62B22618700241EC8 /* FirebaseAnalyticsWithoutAdIdSupport in Frameworks */ = {isa = PBXBuildFile; productRef = EE0FB3F52B22618700241EC8 /* FirebaseAnalyticsWithoutAdIdSupport */; };
EE0FB3F82B22618700241EC8 /* FirebaseAppCheck in Frameworks */ = {isa = PBXBuildFile; productRef = EE0FB3F72B22618700241EC8 /* FirebaseAppCheck */; };
EE0FB3FA2B262EE900241EC8 /* TestMsgCanvasView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE0FB3F92B262EE900241EC8 /* TestMsgCanvasView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -45,19 +39,13 @@
EE0FB3E82B222E7700241EC8 /* ARView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ARView.swift; sourceTree = "<group>"; };
EE0FB3EA2B222EA700241EC8 /* EntityHelperMethods.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityHelperMethods.swift; sourceTree = "<group>"; };
EE0FB3EC2B225E7600241EC8 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
EE0FB3F92B262EE900241EC8 /* TestMsgCanvasView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestMsgCanvasView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
EE0FB3A82B22223A00241EC8 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
EE0FB3F42B22618700241EC8 /* FirebaseAnalyticsSwift in Frameworks */,
EE0FB3F02B22618700241EC8 /* FirebaseAnalytics in Frameworks */,
EE0FB3F22B22618700241EC8 /* FirebaseAnalyticsOnDeviceConversion in Frameworks */,
EE0FB3F82B22618700241EC8 /* FirebaseAppCheck in Frameworks */,
EE0FB3F62B22618700241EC8 /* FirebaseAnalyticsWithoutAdIdSupport in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -67,7 +55,6 @@
EE0FB3A22B22223A00241EC8 = {
isa = PBXGroup;
children = (
EE0FB3F92B262EE900241EC8 /* TestMsgCanvasView.swift */,
EE0FB3AD2B22223A00241EC8 /* StickAroundUI_AR */,
EE0FB3AC2B22223A00241EC8 /* Products */,
);
Expand Down Expand Up @@ -127,11 +114,6 @@
);
name = StickAroundUI_AR;
packageProductDependencies = (
EE0FB3EF2B22618700241EC8 /* FirebaseAnalytics */,
EE0FB3F12B22618700241EC8 /* FirebaseAnalyticsOnDeviceConversion */,
EE0FB3F32B22618700241EC8 /* FirebaseAnalyticsSwift */,
EE0FB3F52B22618700241EC8 /* FirebaseAnalyticsWithoutAdIdSupport */,
EE0FB3F72B22618700241EC8 /* FirebaseAppCheck */,
);
productName = StickAroundUI_AR;
productReference = EE0FB3AB2B22223A00241EC8 /* StickAroundUI_AR.app */;
Expand Down Expand Up @@ -199,7 +181,6 @@
EE0FB3BF2B22233D00241EC8 /* CanvasView.swift in Sources */,
EE0FB3CA2B22235700241EC8 /* ScribbleView.swift in Sources */,
EE0FB3AF2B22223A00241EC8 /* AppDelegate.swift in Sources */,
EE0FB3FA2B262EE900241EC8 /* TestMsgCanvasView.swift in Sources */,
EE0FB3EB2B222EA700241EC8 /* EntityHelperMethods.swift in Sources */,
EE0FB3C92B22235700241EC8 /* ViewModel.swift in Sources */,
EE0FB3C32B22235200241EC8 /* MessageView.swift in Sources */,
Expand Down Expand Up @@ -423,34 +404,6 @@
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
EE0FB3EF2B22618700241EC8 /* FirebaseAnalytics */ = {
isa = XCSwiftPackageProductDependency;
package = EE0FB3EE2B22618700241EC8 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */;
productName = FirebaseAnalytics;
};
EE0FB3F12B22618700241EC8 /* FirebaseAnalyticsOnDeviceConversion */ = {
isa = XCSwiftPackageProductDependency;
package = EE0FB3EE2B22618700241EC8 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */;
productName = FirebaseAnalyticsOnDeviceConversion;
};
EE0FB3F32B22618700241EC8 /* FirebaseAnalyticsSwift */ = {
isa = XCSwiftPackageProductDependency;
package = EE0FB3EE2B22618700241EC8 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */;
productName = FirebaseAnalyticsSwift;
};
EE0FB3F52B22618700241EC8 /* FirebaseAnalyticsWithoutAdIdSupport */ = {
isa = XCSwiftPackageProductDependency;
package = EE0FB3EE2B22618700241EC8 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */;
productName = FirebaseAnalyticsWithoutAdIdSupport;
};
EE0FB3F72B22618700241EC8 /* FirebaseAppCheck */ = {
isa = XCSwiftPackageProductDependency;
package = EE0FB3EE2B22618700241EC8 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */;
productName = FirebaseAppCheck;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = EE0FB3A32B22223A00241EC8 /* Project object */;
}
Binary file not shown.
57 changes: 57 additions & 0 deletions StickAroundUI_AR/StickAroundUI_AR/ARView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class SimpleARView: ARView {
// Dictionary for storing ARPlaneAnchor(s) with AnchorEntity(s)
var anchorEntityMap: [ARPlaneAnchor: AnchorEntity] = [:]

// to save the tapped plane entity
var tappedPlane: MessagePlaneEntity?

init(frame: CGRect, viewModel: ViewModel) {
self.viewModel = viewModel
super.init(frame: frame)
Expand All @@ -55,6 +58,7 @@ class SimpleARView: ARView {
self.setupARSession()
// self.setupScene()
self.setupSubscriptions()
self.setupTapGesture()
}

private func setupARSession() {
Expand Down Expand Up @@ -140,6 +144,56 @@ class SimpleARView: ARView {
self.resetScene()
}.store(in: &subscriptions)
}

private func setupTapGesture() {
print("tap gesture set up")
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
tapGesture.delegate = self
self.addGestureRecognizer(tapGesture)
}

@objc private func handleTap(_ gesture: UITapGestureRecognizer) {
let location = gesture.location(in: self)

print("Tapped at location: \(location)")

// Perform hit test to check if the tap intersects with any entities in the scene
if let tappedEntity = self.entity(at: location) {
print("Tapped entity: \(tappedEntity)")
print("Tapped parent: \(tappedEntity.parent)")

if let messagePlaneEntity = tappedEntity.parent as? MessagePlaneEntity {
// Check if the tapped entity is different from the currently selected one
if messagePlaneEntity != tappedPlane {

// Update the selected entity
tappedPlane = messagePlaneEntity

print("Set tapped plane scale to [1, 1, 1]")

// Update the size of the tapped entity
messagePlaneEntity.messageEntity.setScale([1, 1, 1], relativeTo: nil)
messagePlaneEntity.modelEntity.setScale([0, 0, 0], relativeTo: nil)

// Reset the size of all the other entities
self.anchorEntityMap.values.forEach { anchorEntity in
// let messageEntity = anchorEntity.findEntity(named: "message-plane") as? MessagePlaneEntity
let messageEntities: [MessagePlaneEntity] = anchorEntity.children.compactMap { entity in
return entity as? MessagePlaneEntity
}

messageEntities.forEach { entity in
if (entity != tappedPlane) {
entity.messageEntity.setScale([0, 0, 0], relativeTo: nil)
entity.modelEntity.setScale([0, 0, 0], relativeTo: nil)
}

}
}
}
}
}
}
}

// MARK: - Implement ARSessionDelegate protocol
Expand Down Expand Up @@ -182,6 +236,9 @@ extension SimpleARView: ARSessionDelegate {
self.messagePlaneEntity =
MessagePlaneEntity(message: viewModel.message, planeAnchor: $0)

// Hide the message entity when generated
self.messagePlaneEntity?.messageEntity.setScale([0, 0, 0], relativeTo: nil)

// Add message plane entity to anchor entity
anchorEntity.addChild(self.messagePlaneEntity!)

Expand Down
4 changes: 2 additions & 2 deletions StickAroundUI_AR/StickAroundUI_AR/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit
import SwiftUI
import FirebaseCore
//import FirebaseCore



Expand All @@ -30,7 +30,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
window.makeKeyAndVisible()

// configure firestore
FirebaseApp.configure()
// FirebaseApp.configure()

return true
}
Expand Down
39 changes: 26 additions & 13 deletions StickAroundUI_AR/StickAroundUI_AR/ComposeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct ComposeView: View {

let locations = ["Wall", "Window", "Ceiling", "Table", "Floor"]
@State private var locationSelection = "Wall"


// @State var canvasView = PKCanvasView()
// @State var toolPicker: PKToolPicker? = PKToolPicker()
Expand Down Expand Up @@ -52,14 +52,27 @@ struct ComposeView: View {
.background(Color.white.opacity(0.75).ignoresSafeArea())

// body

HStack {
Spacer()
MessageView(message: $viewModel.message)
.frame(width: geometry.size.width * 0.7, height: geometry.size.width * 0.7)
.scaleEffect(geometry.size.width / 2048 / 1.5) // TODO: magic number?
// .border(Color.black)
Spacer()
}
.padding()
.border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/)

List {
Section {
Section(/*header: Text("Write the note here").bold().foregroundStyle(Color.black).font(.body)*/) {
TextField("Name", text: $viewModel.message.sender) {
}
TextField("Leave your message here", text: $viewModel.message.text) {
}
.frame(height: 60, alignment: .top)
}
.headerProminence(.increased)

Section(header: Text("Where to stick the note?").bold().foregroundStyle(Color.black).font(.body)) {
Picker("", selection: $locationSelection) {
Expand Down Expand Up @@ -104,17 +117,17 @@ struct ComposeView: View {
.listRowBackground(Color.clear)
.listSectionSpacing(.compact)

HStack {
Spacer()
MessageView(message: $viewModel.message)
.frame(width: geometry.size.width * 0.7, height: geometry.size.width * 0.7)
.scaleEffect(geometry.size.width / 2048 / 1.5) // TODO: magic number?
// .border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/)
Spacer()
}
// .listRowBackground(Color.clear)
// .border(.blue)
.listRowSpacing(0)
// HStack {
// Spacer()
// MessageView(message: $viewModel.message)
// .frame(width: geometry.size.width * 0.7, height: geometry.size.width * 0.7)
// .scaleEffect(geometry.size.width / 2048 / 1.5) // TODO: magic number?
//// .border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/)
// Spacer()
// }
//// .listRowBackground(Color.clear)
//// .border(.blue)
// .listRowSpacing(0)

HStack {
Spacer()
Expand Down
9 changes: 7 additions & 2 deletions StickAroundUI_AR/StickAroundUI_AR/EntityHelperMethods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@ class MessagePlaneEntity: Entity {

self.planeMesh = .generatePlane(width: planeAnchor.planeExtent.width, depth: planeAnchor.planeExtent.height)

var transparentMaterial = SimpleMaterial(color: UIColor(.white).withAlphaComponent(0.0), isMetallic: false)
var transparentMaterial = SimpleMaterial(color: UIColor(.gray).withAlphaComponent(0.5), isMetallic: false)
transparentMaterial.tintColor = .white.withAlphaComponent(0.999)
// transparentMaterial.color = Color(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
self.modelEntity = ModelEntity(mesh: self.planeMesh, materials: [transparentMaterial])
// for debug
// self.modelEntity = ModelEntity(mesh: self.planeMesh, materials: [UnlitMaterial(color: .red)])

// generate collision shapes for tapping detection
self.modelEntity.generateCollisionShapes(recursive: true)

super.init()

self.addChild(self.messageEntity)

self.addChild(self.modelEntity)
self.messageEntity.position.y += 0.001

// Enable tapping detection on the entity

}

required init() {
Expand Down
2 changes: 1 addition & 1 deletion StickAroundUI_AR/StickAroundUI_AR/MessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct MessageView: View {
.background(RoundedRectangle(cornerRadius: 200).fill(message.backgroundColor.opacity(0.85)).shadow(radius: 100))
.border(Color.red, width: 4)

CanvasView(pkCanvas: pkCanvas, toolPicker: toolPicker).border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/, width: 10)
// CanvasView(pkCanvas: pkCanvas, toolPicker: toolPicker)
}
}

Expand Down
2 changes: 2 additions & 0 deletions StickAroundUI_AR/StickAroundUI_AR/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import PencilKit
class ViewModel: ObservableObject {

@Published var message: Message = Message(sender: "El", text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sodales posuere augue mollis posuere. Curabitur lectus massa, suscipit vitae malesuada in, ultrices quis nulla.", location: "Wall", time: "2023-10-25 15:22:42", backgroundColor: .yellow, fontColor: .white, fontSize: 200)
// @Published var message: Message = Message(sender: "", text: "", location: "Wall", time: "", backgroundColor: .yellow, fontColor: .white, fontSize: 200)


// Single pass through subject ("signal") for resetting the view.
@Published var resetSignal: PassthroughSubject<Void, Never> = PassthroughSubject<Void, Never>()
Expand Down
24 changes: 0 additions & 24 deletions StickAroundUI_AR/TestMsgCanvasView.swift

This file was deleted.

0 comments on commit 6ad1f63

Please sign in to comment.