Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
3.8 kB
1
Indexable
Never
////////////////ContentView

import SwiftUI

struct ContentView: View {
    @ObservedObject var viewModel: DrawingViewModel

    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Kształt")) {
                    Picker("Wybierz kształt", selection: $viewModel.selectedFigure) {
                        ForEach(FigureType.allCases, id: \.self) { figureType in
                            Label(figureType.figure().name, systemImage: figureType.figure().systemIconName)
                                .tag(figureType)
                        }
                    }
                }

                Section(header: Text("Kolor wypełnienia")) {
                    ColorPicker("Wybierz kolor", selection: $viewModel.fillColor)
                }

                Section(header: Text("Rozmiar")) {
                    TextField("Szerokość", text: $viewModel.widthText)
                        .keyboardType(.decimalPad)
                    TextField("Wysokość", text: $viewModel.heightText)
                        .keyboardType(.decimalPad)
                }

                Section {
                    Toggle(isOn: $viewModel.hasBorder) {
                        Text("Obramowanie")
                    }
                }
            }
            .navigationBarTitle("Rysowanie figur")
        }
    }
}

@main
struct DrawingApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView(viewModel: DrawingViewModel())
        }
    }
}


///////////////DrawingViewModel


import SwiftUI
import Combine

class DrawingViewModel: ObservableObject {
    @Published var selectedFigure: FigureType = .circle
    @Published var fillColor: Color = .blue
    @Published var widthText: String = ""
    @Published var heightText: String = ""
    @Published var hasBorder: Bool = false
    
    var width: CGFloat {
        CGFloat(Double(widthText) ?? 0)
    }
    
    var height: CGFloat {
        CGFloat(Double(heightText) ?? 0)
    }
}


//////////////FigureType

struct Figure {
    let name: String
    let systemIconName: String
}

enum FigureType: CaseIterable {
    case circle, ellipse, square

    func figure() -> Figure {
        switch self {
        case .circle:
            return Figure(name: "Koło", systemIconName: "circle")
        case .ellipse:
            return Figure(name: "Elipsa", systemIconName: "oval")
        case .square:
            return Figure(name: "Kwadrat", systemIconName: "square")
        }
    }
}








///////////////////FigureView


import SwiftUI

struct FigureView: View {
    @ObservedObject var viewModel: DrawingViewModel

    var body: some View {
        ZStack {
            if viewModel.selectedFigure == .circle {
                Circle()
                    .fill(viewModel.fillColor)
                    .frame(width: viewModel.width, height: viewModel.height)
                    .overlay(Circle().stroke(viewModel.hasBorder ? Color.black : Color.clear, lineWidth: 3))
            } else if viewModel.selectedFigure == .square {
                Rectangle()
                    .fill(viewModel.fillColor)
                    .frame(width: viewModel.width, height: viewModel.height)
                    .overlay(Rectangle().stroke(viewModel.hasBorder ? Color.black : Color.clear, lineWidth: 3))
            } else if viewModel.selectedFigure == .ellipse {
                Ellipse()
                    .fill(viewModel.fillColor)
                    .frame(width: viewModel.width, height: viewModel.height)
                    .overlay(Ellipse().stroke(viewModel.hasBorder ? Color.black : Color.clear, lineWidth: 3))
            }
        }
    }
}

struct Ellipse: Shape {
    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.addEllipse(in: rect)
        return path
    }
}


Leave a Comment