]> git.r.bdr.sh - rbdr/map/blame - Map/Logic/MapParser/Strategies/NoteParserStrategy.swift
Add some debouncing
[rbdr/map] / Map / Logic / MapParser / Strategies / NoteParserStrategy.swift
CommitLineData
be897af3 1// Copyright (C) 2024 Rubén Beltrán del Río
98f09799 2
be897af3
RBR
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
98f09799 7
be897af3
RBR
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
98f09799 12
be897af3
RBR
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see https://map.tranquil.systems.
77d0155b
RBR
15import Foundation
16
fdb4633d
RBR
17struct NoteParserStrategy: MapParserStrategy {
18 private let regex = MapParsingPatterns.note
77d0155b
RBR
19
20 func canHandle(line: String) -> Bool {
21 let range = NSRange(location: 0, length: line.utf16.count)
22 let matches = regex.matches(in: String(line), options: [], range: range)
fdb4633d 23 return matches.count > 0 && matches[0].numberOfRanges == 5
77d0155b
RBR
24 }
25
26 func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
27 let range = NSRange(location: 0, length: line.utf16.count)
28 let matches = regex.matches(in: String(line), options: [], range: range)
29
30 let match = matches[0]
fdb4633d 31 let text = String(line[Range(match.range(at: 4), in: line)!])
77d0155b
RBR
32 let xString = String(line[Range(match.range(at: 2), in: line)!])
33 let yString = String(line[Range(match.range(at: 3), in: line)!])
34 let x = CGFloat(truncating: NumberFormatter().number(from: xString) ?? 0.0)
35 let y = CGFloat(truncating: NumberFormatter().number(from: yString) ?? 0.0)
36
fdb4633d 37 let note = Note(
77d0155b 38 id: index,
fdb4633d
RBR
39 position: CGPoint(x: x, y: y),
40 text: text
77d0155b
RBR
41 )
42
fdb4633d 43 return (Note.self, note)
77d0155b
RBR
44 }
45}