Add support for alert tones

pull/1/head
php 1 year ago
parent 5511563423
commit 8d73e7d573

@ -0,0 +1,15 @@
<UserControl x:Class="WhackerLinkConsoleV2.Controls.AlertTone"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="70" Width="150"
MouseLeftButtonDown="AlertTone_MouseLeftButtonDown"
MouseMove="AlertTone_MouseMove"
MouseRightButtonDown="AlertTone_MouseRightButtonDown"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Border BorderBrush="Gray" BorderThickness="1" CornerRadius="5" Background="Gray">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="{Binding AlertFileName}" Margin="5" TextWrapping="Wrap" HorizontalAlignment="Center"/>
<Button Content="Play Alert" Background="Green" Click="PlayAlert_Click" Width="80" />
</StackPanel>
</Border>
</UserControl>

@ -0,0 +1,121 @@
/*
* WhackerLink - WhackerLinkConsoleV2
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2024 Caleb, K4PHP
*
*/
using System.Media;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace WhackerLinkConsoleV2.Controls
{
public partial class AlertTone : UserControl
{
public static readonly DependencyProperty AlertFileNameProperty =
DependencyProperty.Register("AlertFileName", typeof(string), typeof(AlertTone), new PropertyMetadata(string.Empty));
public string AlertFileName
{
get => (string)GetValue(AlertFileNameProperty);
set => SetValue(AlertFileNameProperty, value);
}
public string AlertFilePath { get; set; }
private Point _startPoint;
private bool _isDragging;
public bool IsEditMode { get; set; }
public AlertTone(string alertFilePath)
{
InitializeComponent();
AlertFilePath = alertFilePath;
AlertFileName = System.IO.Path.GetFileNameWithoutExtension(alertFilePath);
this.MouseLeftButtonDown += AlertTone_MouseLeftButtonDown;
this.MouseMove += AlertTone_MouseMove;
this.MouseRightButtonDown += AlertTone_MouseRightButtonDown;
}
private void PlayAlert_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(AlertFilePath) && System.IO.File.Exists(AlertFilePath))
{
try
{
using (var player = new SoundPlayer(AlertFilePath))
{
player.Play();
}
}
catch (Exception ex)
{
MessageBox.Show($"Failed to play alert: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
{
MessageBox.Show("Alert file not set or file not found.", "Alert", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void AlertTone_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!IsEditMode) return;
_startPoint = e.GetPosition(this);
_isDragging = true;
}
private void AlertTone_MouseMove(object sender, MouseEventArgs e)
{
if (_isDragging && IsEditMode)
{
var parentCanvas = VisualTreeHelper.GetParent(this) as Canvas;
if (parentCanvas != null)
{
Point mousePos = e.GetPosition(parentCanvas);
double newLeft = mousePos.X - _startPoint.X;
double newTop = mousePos.Y - _startPoint.Y;
Canvas.SetLeft(this, Math.Max(0, newLeft));
Canvas.SetTop(this, Math.Max(0, newTop));
}
}
}
private void AlertTone_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
if (!IsEditMode || !_isDragging) return;
_isDragging = false;
var parentCanvas = VisualTreeHelper.GetParent(this) as Canvas;
if (parentCanvas != null)
{
double x = Canvas.GetLeft(this);
double y = Canvas.GetTop(this);
}
ReleaseMouseCapture();
}
}
}

@ -12,42 +12,13 @@
<MenuItem Header="Enable Edit Mode" Click="ToggleEditMode_Click"/>
<MenuItem Header="Select Widgets to Display" Click="SelectWidgets_Click"/>
<MenuItem Header="Reset Settings" Click="ResetSettings_Click"/>
<MenuItem Header="Alerts">
<MenuItem Header="Add Alert Tone" Click="AddAlertTone_Click"/>
</MenuItem>
</MenuItem>
</Menu>
<Canvas x:Name="ChannelsCanvas" Margin="0,25,0,0" Background="#1e1e1e">
</Canvas>
<Grid VerticalAlignment="Top" Height="75" Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center">
<Button Content="ALERT 1" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 2" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 3" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 4" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 5" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 6" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 7" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 8" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 9" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 10" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 11" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 12" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 13" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 14" Height="47" Width="53" Click="Button_Click" />
<Button Content="ALERT 15" Height="47" Width="53" Click="Button_Click" />
</StackPanel>
</Grid>
</Grid>
</Window>

@ -105,8 +105,16 @@ namespace WhackerLinkConsoleV2
{
var systemStatusBox = new SystemStatusBox(system.Name, system.Address, system.Port);
if (_settingsManager.SystemStatusPositions.TryGetValue(system.Name, out var position))
{
Canvas.SetLeft(systemStatusBox, position.X);
Canvas.SetTop(systemStatusBox, position.Y);
}
else
{
Canvas.SetLeft(systemStatusBox, offsetX);
Canvas.SetTop(systemStatusBox, offsetY);
}
systemStatusBox.MouseLeftButtonDown += SystemStatusBox_MouseLeftButtonDown;
systemStatusBox.MouseMove += SystemStatusBox_MouseMove;
@ -156,6 +164,29 @@ namespace WhackerLinkConsoleV2
}
}
}
foreach (var alertPath in _settingsManager.AlertToneFilePaths)
{
var alertTone = new AlertTone(alertPath)
{
IsEditMode = isEditMode
};
if (_settingsManager.AlertTonePositions.TryGetValue(alertPath, out var position))
{
Canvas.SetLeft(alertTone, position.X);
Canvas.SetTop(alertTone, position.Y);
}
else
{
Canvas.SetLeft(alertTone, 20);
Canvas.SetTop(alertTone, 20);
}
alertTone.MouseRightButtonUp += AlertTone_MouseRightButtonUp;
ChannelsCanvas.Children.Add(alertTone);
}
}
private void SelectWidgets_Click(object sender, RoutedEventArgs e)
@ -212,13 +243,84 @@ namespace WhackerLinkConsoleV2
private void SystemStatusBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) => ChannelBox_MouseLeftButtonDown(sender, e);
private void SystemStatusBox_MouseMove(object sender, MouseEventArgs e) => ChannelBox_MouseMove(sender, e);
private void SystemStatusBox_MouseRightButtonDown(object sender, MouseButtonEventArgs e) => ChannelBox_MouseRightButtonDown(sender, e);
private void SystemStatusBox_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
if (!isEditMode) return;
if (sender is SystemStatusBox systemStatusBox)
{
double x = Canvas.GetLeft(systemStatusBox);
double y = Canvas.GetTop(systemStatusBox);
_settingsManager.SystemStatusPositions[systemStatusBox.SystemName] = new ChannelPosition { X = x, Y = y };
ChannelBox_MouseRightButtonDown(sender, e);
}
}
private void ToggleEditMode_Click(object sender, RoutedEventArgs e)
{
isEditMode = !isEditMode;
var menuItem = (MenuItem)sender;
menuItem.Header = isEditMode ? "Disable Edit Mode" : "Enable Edit Mode";
UpdateEditModeForWidgets();
}
private void UpdateEditModeForWidgets()
{
foreach (var child in ChannelsCanvas.Children)
{
if (child is AlertTone alertTone)
{
alertTone.IsEditMode = isEditMode;
}
}
}
private void AddAlertTone_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "WAV Files (*.wav)|*.wav|All Files (*.*)|*.*",
Title = "Select Alert Tone"
};
if (openFileDialog.ShowDialog() == true)
{
string alertFilePath = openFileDialog.FileName;
var alertTone = new AlertTone(alertFilePath)
{
IsEditMode = isEditMode
};
if (_settingsManager.AlertTonePositions.TryGetValue(alertFilePath, out var position))
{
Canvas.SetLeft(alertTone, position.X);
Canvas.SetTop(alertTone, position.Y);
}
else
{
Canvas.SetLeft(alertTone, 20);
Canvas.SetTop(alertTone, 20);
}
alertTone.MouseRightButtonUp += AlertTone_MouseRightButtonUp;
ChannelsCanvas.Children.Add(alertTone);
_settingsManager.UpdateAlertTonePaths(alertFilePath);
}
}
private void AlertTone_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
if (!isEditMode) return;
if (sender is AlertTone alertTone)
{
double x = Canvas.GetLeft(alertTone);
double y = Canvas.GetTop(alertTone);
_settingsManager.UpdateAlertTonePosition(alertTone.AlertFilePath, x, y);
}
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
@ -238,13 +340,5 @@ namespace WhackerLinkConsoleV2
GenerateChannelWidgets();
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
string buttonContent = button.Content.ToString();
MessageBox.Show($"Beep boop {buttonContent}");
}
}
}
}

@ -32,6 +32,9 @@ namespace WhackerLinkConsoleV2
public string LastCodeplugPath { get; set; } = null;
public Dictionary<string, ChannelPosition> ChannelPositions { get; set; } = new Dictionary<string, ChannelPosition>();
public Dictionary<string, ChannelPosition> SystemStatusPositions { get; set; } = new Dictionary<string, ChannelPosition>();
public List<string> AlertToneFilePaths { get; set; } = new List<string>();
public Dictionary<string, ChannelPosition> AlertTonePositions { get; set; } = new Dictionary<string, ChannelPosition>();
public void LoadSettings()
{
@ -48,6 +51,9 @@ namespace WhackerLinkConsoleV2
ShowChannels = loadedSettings.ShowChannels;
LastCodeplugPath = loadedSettings.LastCodeplugPath;
ChannelPositions = loadedSettings.ChannelPositions ?? new Dictionary<string, ChannelPosition>();
SystemStatusPositions = loadedSettings.SystemStatusPositions ?? new Dictionary<string, ChannelPosition>();
AlertToneFilePaths = loadedSettings.AlertToneFilePaths ?? new List<string>();
AlertTonePositions = loadedSettings.AlertTonePositions ?? new Dictionary<string, ChannelPosition>();
}
}
catch (Exception ex)
@ -56,29 +62,43 @@ namespace WhackerLinkConsoleV2
}
}
public void SaveSettings()
public void UpdateAlertTonePaths(string newFilePath)
{
try
if (!AlertToneFilePaths.Contains(newFilePath))
{
var json = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(SettingsFilePath, json);
AlertToneFilePaths.Add(newFilePath);
SaveSettings();
}
catch (Exception ex)
{
Console.WriteLine($"Error saving settings: {ex.Message}");
}
public void UpdateAlertTonePosition(string alertFileName, double x, double y)
{
AlertTonePositions[alertFileName] = new ChannelPosition { X = x, Y = y };
SaveSettings();
}
public void UpdateChannelPosition(string channelName, double x, double y)
{
if (ChannelPositions.ContainsKey(channelName))
ChannelPositions[channelName] = new ChannelPosition { X = x, Y = y };
SaveSettings();
}
public void UpdateSystemStatusPosition(string systemName, double x, double y)
{
ChannelPositions[channelName].X = x;
ChannelPositions[channelName].Y = y;
SystemStatusPositions[systemName] = new ChannelPosition { X = x, Y = y };
SaveSettings();
}
else
public void SaveSettings()
{
ChannelPositions[channelName] = new ChannelPosition { X = x, Y = y };
try
{
var json = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(SettingsFilePath, json);
}
catch (Exception ex)
{
Console.WriteLine($"Error saving settings: {ex.Message}");
}
}
}

Loading…
Cancel
Save

Powered by TurnKey Linux.