Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PixelArtKit: Problema de inicialización en PixelDrawingView #9

Open
cHuMaRi opened this issue May 17, 2022 · 0 comments
Open

PixelArtKit: Problema de inicialización en PixelDrawingView #9

cHuMaRi opened this issue May 17, 2022 · 0 comments

Comments

@cHuMaRi
Copy link

cHuMaRi commented May 17, 2022

Al parecer, como en otros lenguajes, se puede acceder al setter de la propiedad con el guión bajo... si no se queja Xcode al asignar el valor.
Con State() se pueden inicializar las variables @State, y ya no es necesario poner el ! en la declaración de la variable pixelArt

También se pueden inicializar con la siguiente sintaxis, pero me parece un poco feote:

self._pixelArt = /*State<PixelArt>*/.init(initialValue: ....

Así es como yo lo tengo:

import SwiftUI

public struct PixelDrawingView: View {
    
    static let numberOfColumns: Int = 10
    @State var pixelArt: PixelArt
    let columns: [GridItem] = generateColumns(numberOfColumns, columnWidht: 22)
    
    @State private var color: Color = .blue
    private let pixelSquareSize: CGFloat = 20
    
    public init(pixelArt: PixelArt? = nil) {
        
        self._pixelArt = State(initialValue: pixelArt ?? PixelArt(pixels: PixelArt.generatePixelArtPattern(size: PixelDrawingView.numberOfColumns),
                                             name: "",
                                             dateCreation: Date(),
                                             dateLastModification: Date()))
         
    }
    
    public var body: some View {
        VStack{
            Text("Awesome Pixel Painter 🎨")
                .padding()
            
            PixelArtView(pixelArt: pixelArt, colums: columns, pixelSquareSize: pixelSquareSize) { (pixel: Pixel) in
                print("Touch on row: \(pixel.row) col: \(pixel.col)")
                
                pixelArt = PixelArt.from(pixelArt: pixelArt, row: pixel.row, col: pixel.col, color: color, numberOfColumns: PixelDrawingView.numberOfColumns)
            }
            
            PixelArtView(pixelArt: pixelArt,
                         colums: PixelDrawingView.generateColumns(PixelDrawingView.numberOfColumns, columnWidht: 7),
                         pixelSquareSize: 5.0)
            
            ColorPicker("Pick color", selection: $color)
                .padding()
            
            HStack{
                Button("Clear") {
                    pixelArt = PixelArt.from(pixelArt: pixelArt,
                                             newPixels: PixelArt.generatePixelArtPattern(size: 10, color: .black))
                }
                
                Button("Random") {
                    pixelArt = PixelArt.from(pixelArt: pixelArt,
                                             newPixels: PixelArt.generatePixelArtPattern(size: 10))
                }
                
                Button("Fill") {
                    pixelArt = PixelArt.from(pixelArt: pixelArt,
                                             newPixels: PixelArt.generatePixelArtPattern(size: 10, color: color))
                }
            }
#if targetEnvironment(macCatalyst)
            .frame(maxHeight: 100)
#else
            .frame(maxHeight: 50)
#endif
            .padding()
        }
    }
}

extension PixelDrawingView {
    
    static func generateColumns(_ numCols: Int, columnWidht: CGFloat) -> [GridItem] {
        var columns: [GridItem] = []
        
        for _ in 0..<numCols {
            columns.append(GridItem(.fixed(columnWidht), spacing: 0, alignment: .center))
        }
        
        return columns
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        PixelDrawingView()
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant