Skip to content
This repository was archived by the owner on Sep 14, 2025. It is now read-only.

Commit 1e287bc

Browse files
committed
Initial commit
0 parents  commit 1e287bc

17 files changed

+956
-0
lines changed

.github/screenshot.png

6.12 KB
Loading

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Haptics Presets OSC/obj/
2+
Haptics Presets OSC/bin/
3+
.vs
4+
.cr

Haptics Presets OSC.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.10.35027.167
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Haptics Presets OSC", "Haptics Presets OSC\Haptics Presets OSC.csproj", "{ED62B70F-1318-4470-AC11-211BBFBF4C5C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Release|Any CPU = Release|Any CPU
13+
Release|x64 = Release|x64
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{ED62B70F-1318-4470-AC11-211BBFBF4C5C}.Debug|Any CPU.ActiveCfg = Debug|x64
17+
{ED62B70F-1318-4470-AC11-211BBFBF4C5C}.Debug|Any CPU.Build.0 = Debug|x64
18+
{ED62B70F-1318-4470-AC11-211BBFBF4C5C}.Debug|x64.ActiveCfg = Debug|x64
19+
{ED62B70F-1318-4470-AC11-211BBFBF4C5C}.Debug|x64.Build.0 = Debug|x64
20+
{ED62B70F-1318-4470-AC11-211BBFBF4C5C}.Release|Any CPU.ActiveCfg = Release|x64
21+
{ED62B70F-1318-4470-AC11-211BBFBF4C5C}.Release|Any CPU.Build.0 = Release|x64
22+
{ED62B70F-1318-4470-AC11-211BBFBF4C5C}.Release|x64.ActiveCfg = Release|x64
23+
{ED62B70F-1318-4470-AC11-211BBFBF4C5C}.Release|x64.Build.0 = Release|x64
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {9ABDF590-86B6-4584-A105-64EAD9D7B1A0}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Haptics_Presets_OSC.Classes
8+
{
9+
public class ConnectionSettings
10+
{
11+
public string TargetIP { get; set; } = "127.0.0.1";
12+
public int SenderPort { get; set; } = 9009;
13+
public int ReceiverPort { get; set; } = 9008;
14+
}
15+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Haptics_Presets_OSC.Classes
4+
{
5+
public class HapticPattern
6+
{
7+
public bool Enabled { get; set; } = true;
8+
public string Name { get; }
9+
public readonly Uri PatternFile;
10+
11+
[JsonIgnore]
12+
public string Path
13+
{
14+
get
15+
{
16+
return PatternFile.LocalPath;
17+
}
18+
}
19+
public string Parameter { get; set; }
20+
21+
public HapticPattern(string name, Uri path, string parameter)
22+
{
23+
Name = name;
24+
PatternFile = path;
25+
Parameter = parameter;
26+
}
27+
28+
[JsonConstructor]
29+
public HapticPattern(bool enabled, string name, Uri patternFile, string parameter)
30+
{
31+
Enabled = enabled;
32+
Name = name;
33+
PatternFile = patternFile;
34+
Parameter = parameter;
35+
}
36+
}
37+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using BuildSoft.OscCore;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Haptics_Presets_OSC.Classes
9+
{
10+
public class OSCMessageEventArgs
11+
{
12+
public readonly HapticPattern Pattern;
13+
public readonly OscMessageValues Values;
14+
15+
public OSCMessageEventArgs(HapticPattern pattern, OscMessageValues values)
16+
{
17+
Pattern = pattern;
18+
Values = values;
19+
}
20+
}
21+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using BuildSoft.OscCore;
2+
3+
namespace Haptics_Presets_OSC.Classes
4+
{
5+
public class OSCServerHelper: IDisposable
6+
{
7+
private readonly OscClient client;
8+
private readonly OscServer server;
9+
private bool disposed;
10+
public bool IsRunning
11+
{
12+
get
13+
{
14+
return !disposed;
15+
}
16+
}
17+
18+
public EventHandler<OSCMessageEventArgs>? OnMessageReceived;
19+
20+
public OSCServerHelper(ConnectionSettings connectionSettings)
21+
{
22+
server = new OscServer(connectionSettings.ReceiverPort);
23+
client = new OscClient(connectionSettings.TargetIP, connectionSettings.SenderPort);
24+
}
25+
26+
public void Start(IEnumerable<HapticPattern> patterns)
27+
{
28+
// Start the OSC server
29+
foreach (HapticPattern pattern in patterns)
30+
{
31+
server.TryAddMethod(
32+
pattern.Parameter,
33+
(OscMessageValues values) =>
34+
{
35+
OnMessageReceived?.Invoke(this, new OSCMessageEventArgs(pattern, values));
36+
client.Send(pattern.Parameter, false);
37+
}
38+
);
39+
}
40+
41+
server.Start();
42+
}
43+
44+
protected virtual void Dispose(bool disposing)
45+
{
46+
if (disposing)
47+
{
48+
// free managed resources
49+
client.Dispose();
50+
server.Dispose();
51+
disposed = true;
52+
}
53+
}
54+
55+
public void Dispose()
56+
{
57+
Dispose(true);
58+
System.GC.SuppressFinalize(this);
59+
}
60+
}
61+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<RootNamespace>Haptics_Presets_OSC</RootNamespace>
6+
<Nullable>enable</Nullable>
7+
<UseWindowsForms>true</UseWindowsForms>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
<SupportedOSPlatformVersion>10.0.22621.0</SupportedOSPlatformVersion>
10+
<StartupObject>Haptics_Presets_OSC.Program</StartupObject>
11+
<PlatformTarget>x64</PlatformTarget>
12+
<Platforms>AnyCPU;x64</Platforms>
13+
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="bHapticsLib" Version="1.0.6" />
18+
<PackageReference Include="BuildSoft.OscCore" Version="1.2.1.1" />
19+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
20+
<PackageReference Include="System.Net.Http" Version="4.3.4" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<_LastSelectedProfileId>E:\Projects\Programmierung\Repos\Haptic_Presets_OSC\bHaptics Presets OSC\Haptics Presets OSC\Properties\PublishProfiles\FolderProfile.pubxml</_LastSelectedProfileId>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Update="PresetsForm.cs">
8+
<SubType>Form</SubType>
9+
</Compile>
10+
</ItemGroup>
11+
</Project>

0 commit comments

Comments
 (0)