|
| 1 | +using Newtonsoft.Json; |
| 2 | + |
| 3 | +namespace Haptics_Presets_OSC.Classes |
| 4 | +{ |
| 5 | + public class HapticsManager |
| 6 | + { |
| 7 | + private const string PatternDirectory = ".Patterns"; |
| 8 | + private const string ParameterBasePath = "/avatar/parameters/Haptics/Patterns/"; |
| 9 | + public readonly Dictionary<string, HapticPattern> Patterns = []; |
| 10 | + |
| 11 | + private Uri CreatePatternDirectory() |
| 12 | + { |
| 13 | + Uri patternDirectoryPath = new(Path.Combine(Directory.GetCurrentDirectory(), PatternDirectory)); |
| 14 | + if (!Directory.Exists(patternDirectoryPath.LocalPath)) |
| 15 | + { |
| 16 | + Directory.CreateDirectory(patternDirectoryPath.LocalPath); |
| 17 | + } |
| 18 | + |
| 19 | + return patternDirectoryPath; |
| 20 | + } |
| 21 | + |
| 22 | + public void SavePatterns() |
| 23 | + { |
| 24 | + Uri patternDirectoryPath = CreatePatternDirectory(); |
| 25 | + |
| 26 | + List<HapticPattern> patterns = [.. Patterns.Values]; |
| 27 | + |
| 28 | + string patternsJson = JsonConvert.SerializeObject(patterns, Formatting.Indented); |
| 29 | + |
| 30 | + string patternsJsonPath = Path.Combine(patternDirectoryPath.LocalPath, "patterns.json"); |
| 31 | + File.WriteAllText(patternsJsonPath, patternsJson); |
| 32 | + } |
| 33 | + |
| 34 | + public void LoadPatterns() |
| 35 | + { |
| 36 | + Uri patternDirectoryPath = CreatePatternDirectory(); |
| 37 | + |
| 38 | + string patternsJsonPath = Path.Combine(patternDirectoryPath.LocalPath, "patterns.json"); |
| 39 | + |
| 40 | + if (!File.Exists(patternsJsonPath)) |
| 41 | + { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + string patternsJson = File.ReadAllText(patternsJsonPath); |
| 46 | + |
| 47 | + List<HapticPattern>? patterns = JsonConvert.DeserializeObject<List<HapticPattern>>(patternsJson); |
| 48 | + |
| 49 | + if (patterns == null) |
| 50 | + { |
| 51 | + MessageBox.Show("Unable to load saved patterns"); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + foreach (HapticPattern pattern in patterns) |
| 56 | + { |
| 57 | + Patterns.Add(pattern.Name, pattern); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public void ImportPattern(Uri patternFile) |
| 62 | + { |
| 63 | + Uri patternDirectoryPath = CreatePatternDirectory(); |
| 64 | + |
| 65 | + string patternName = Path.GetFileNameWithoutExtension(patternFile.LocalPath); |
| 66 | + string patternFileName = Path.GetFileName(patternFile.LocalPath); |
| 67 | + string patternPath = Path.Combine(patternDirectoryPath.LocalPath, patternFileName); |
| 68 | + |
| 69 | + // Check if the pattern already exists |
| 70 | + if (Patterns.ContainsKey(patternName)) |
| 71 | + { |
| 72 | + MessageBox.Show("Pattern already exists"); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // Check if the pattern file already exists |
| 77 | + if (File.Exists(patternPath)) |
| 78 | + { |
| 79 | + DialogResult result = MessageBox.Show($"Pattern already exists.{Environment.NewLine}Do you want to replace the existing file?", "File exists", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); |
| 80 | + if (result != DialogResult.Yes) |
| 81 | + { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + File.Delete(patternPath); |
| 86 | + } |
| 87 | + |
| 88 | + // Copy pattern to the pattern directory |
| 89 | + File.Copy(patternFile.LocalPath, patternPath); |
| 90 | + |
| 91 | + // Save pattern info |
| 92 | + Patterns.Add(patternName, new HapticPattern(patternName, new Uri(patternPath), ParameterBasePath + patternName)); |
| 93 | + |
| 94 | + SavePatterns(); |
| 95 | + } |
| 96 | + |
| 97 | + public void RemovePattern(string patternName) |
| 98 | + { |
| 99 | + if (!Patterns.ContainsKey(patternName)) |
| 100 | + { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + HapticPattern pattern = Patterns[patternName]; |
| 105 | + Patterns.Remove(patternName); |
| 106 | + |
| 107 | + File.Delete(pattern.Path); |
| 108 | + |
| 109 | + SavePatterns(); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments