[all] Add a ball on click; regenerate the field on right click

This commit is contained in:
Eryn Wells 2017-08-06 08:24:50 -07:00
parent 9297302a75
commit 357db9a248
2 changed files with 25 additions and 2 deletions

View file

@ -41,17 +41,36 @@ class ViewController: NSViewController, RendererDelegate {
super.viewWillAppear()
renderer.mtkView(metalView, drawableSizeWillChange: metalView.drawableSize)
for _ in 1...10 {
let r = Float(20 + arc4random_uniform(50))
field.add(ballWithRadius: r)
addBallWithRandomRadius()
}
}
override func mouseDown(with event: NSEvent) {
addBallWithRandomRadius()
}
override func rightMouseDown(with event: NSEvent) {
field.clear()
for _ in 1...10 {
addBallWithRandomRadius()
}
}
// MARK: - Private
private func newErrorView() -> NSView {
let view = NSView()
view.layer?.backgroundColor = NSColor.red.cgColor
return view
}
private func addBallWithRandomRadius() {
let base = UInt32(view.bounds.width * 0.05)
let variance = UInt32(base * 2)
let r = Float(base + arc4random_uniform(variance))
field.add(ballWithRadius: r)
}
// MARK: - RendererDelegate
var renderSize: CGSize {

View file

@ -110,6 +110,10 @@ public class Field {
populateBallBuffer()
}
public func clear() {
balls.removeAll(keepingCapacity: true)
}
// MARK: - Metal Configuration
private var device: MTLDevice?