From 9c0e3dccbda5f8a6cf4a462e15522bc9dcc956b7 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 27 Oct 2018 08:53:21 -0700 Subject: [PATCH] Allow resizing the grid cells via [ and ] --- Metaballs-macOS/ViewController.swift | 15 ++++++++++++++- MetaballsKit/MarchingSquares.swift | 9 ++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Metaballs-macOS/ViewController.swift b/Metaballs-macOS/ViewController.swift index 5d5e65c..1d6fd10 100644 --- a/Metaballs-macOS/ViewController.swift +++ b/Metaballs-macOS/ViewController.swift @@ -97,7 +97,20 @@ class ViewController: NSViewController, RendererDelegate { } override func keyDown(with event: NSEvent) { - print("key down: \(event)") + switch event.charactersIgnoringModifiers { + case .some("["): + let size = marchingSquares.sampleGridSize + if size.x > 2 { + marchingSquares.sampleGridSize = Size(size.x / 2) + } + case .some("]"): + let size = marchingSquares.sampleGridSize + if size.x < 32 { + marchingSquares.sampleGridSize = Size(size.x * 2) + } + default: + print("key down: \(event)") + } } override var acceptsFirstResponder: Bool { diff --git a/MetaballsKit/MarchingSquares.swift b/MetaballsKit/MarchingSquares.swift index 2f11d6e..8eefbb9 100644 --- a/MetaballsKit/MarchingSquares.swift +++ b/MetaballsKit/MarchingSquares.swift @@ -12,11 +12,18 @@ import simd class MarchingSquares { private var field: Field - private var sampleGridSize = Size(16) + + var sampleGridSize = Size(16) { + didSet { + fieldDidResize() + } + } private var semaphore: DispatchSemaphore + /// Compute pipeline for sampling the field. private var samplingPipeline: MTLComputePipelineState? + /// Compute pipeline for calculating the contours based on a grid of samples. private var contouringPipeline: MTLComputePipelineState? private var parametersBuffer: MTLBuffer?