Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/Folium-iOS/Classes/ControllerView/Latest/LatestControllerView.swift
2 views
//
//  LatestControllerView.swift
//  Folium-iOS
//
//  Created by Jarrod Norwell on 3/9/2025.
//

import Foundation
import SwiftUI
import UIKit

@available(iOS 26, *)
struct LatestButton {
    enum `Type` : String {
        case a, b, x, y
        case up, down, left, right
        
        case l, r, zl, zr
        
        case select, start
    }
    
    let position: CGPoint
    let size: CGSize
    let type: `Type`
}

@available(iOS 26, *)
struct LatestOrientation {
    var buttons: [LatestControllerButton.Model]
}

@available(iOS 26, *)
struct LatestControllerViewModel {
    struct Delegates {
        var button: ControllerButtonDelegate? = nil
        var thumbstick: ControllerThumbstickDelegate? = nil
    }
    
    let orientation: LatestOrientation
}

@available(iOS 26, *)
class LatestControllerButton : UIView {
    struct Model {
        let button: LatestButton
        var delegate: ControllerButtonDelegate? = nil
    }
    
    var model: Model
    init(model: Model) {
        self.model = model
        super.init(frame: .zero)
        clipsToBounds = true
        layer.cornerCurve = .continuous
        
        let effect: UIGlassEffect = .init(style: .regular)
        effect.isInteractive = true
        
        let visualEffectView: UIVisualEffectView = .init(effect: effect)
        visualEffectView.translatesAutoresizingMaskIntoConstraints = false
        insertSubview(visualEffectView, belowSubview: self)
        
        visualEffectView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        visualEffectView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        visualEffectView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        visualEffectView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        layer.cornerRadius = frame.height / 2
    }
}

@available(iOS 26, *)
class LatestControllerView : UIView {
    var controllerModel: LatestControllerViewModel
    init(controllerModel: LatestControllerViewModel) {
        self.controllerModel = controllerModel
        super.init(frame: .zero)
        
        controllerModel.orientation.buttons.forEach { model in
            let controllerButton: LatestControllerButton = .init(model: model)
            controllerButton.frame = .init(origin: model.button.position, size: model.button.size)
            addSubview(controllerButton)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

@available(iOS 26, *)
class LatestControllerViewController : UIViewController {
    override func loadView() {
        view = UIHostingController(rootView: MeshGradientView(colours: Color.vibrantRainbow)).view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let v = LatestControllerView(controllerModel: .init(orientation: .init(buttons: [
            .init(button: .init(position: .init(x: 100, y: 100), size: .init(width: 50, height: 50), type: .a))
        ])))
        v.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(v)
        
        v.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        v.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        v.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        v.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    }
}