1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-05-31 22:41:37 +00:00
6502bench/SourceGen/WpfGui/EditVisualizationSet.xaml.cs
Andy McFadden 125080dbda Finish basic visualization editor implementation
The Visualization and Visualization Set editors are now fully
functional.  You can create, edit, and rearrange sets, and they're
now stored in the project file.
2019-12-02 16:38:32 -08:00

217 lines
7.8 KiB
C#

/*
* Copyright 2019 faddenSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Asm65;
using PluginCommon;
using SourceGen.Sandbox;
namespace SourceGen.WpfGui {
/// <summary>
/// Visualization set editor.
/// </summary>
public partial class EditVisualizationSet : Window, INotifyPropertyChanged {
public VisualizationSet NewVisSet { get; private set; }
private DisasmProject mProject;
private Formatter mFormatter;
private VisualizationSet mOrigSet;
private int mOffset;
public ObservableCollection<Visualization> VisualizationList { get; private set; } =
new ObservableCollection<Visualization>();
/// <summary>
/// True if there are plugins that implement the visualization generation interface.
/// </summary>
public bool HasVisPlugins {
get { return mHasVisPlugins; }
set { mHasVisPlugins = value; OnPropertyChanged(); }
}
private bool mHasVisPlugins;
public bool IsEditEnabled {
get { return mIsEditEnabled; }
set { mIsEditEnabled = value; OnPropertyChanged(); }
}
private bool mIsEditEnabled;
public bool IsRemoveEnabled {
get { return mIsRemoveEnabled; }
set { mIsRemoveEnabled = value; OnPropertyChanged(); }
}
private bool mIsRemoveEnabled;
public bool IsUpEnabled {
get { return mIsUpEnabled; }
set { mIsUpEnabled = value; OnPropertyChanged(); }
}
private bool mIsUpEnabled;
public bool IsDownEnabled {
get { return mIsDownEnabled; }
set { mIsDownEnabled = value; OnPropertyChanged(); }
}
private bool mIsDownEnabled;
// INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public EditVisualizationSet(Window owner, DisasmProject project, Formatter formatter,
VisualizationSet curSet, int offset) {
InitializeComponent();
Owner = owner;
DataContext = this;
mProject = project;
mFormatter = formatter;
mOrigSet = curSet;
mOffset = offset;
if (curSet != null) {
// Populate the ItemsSource.
foreach (Visualization vis in curSet) {
VisualizationList.Add(vis);
}
}
// Check to see if we have any relevant plugins. If not, disable New/Edit.
List<IPlugin> plugins = project.GetActivePlugins();
foreach (IPlugin chkPlug in plugins) {
if (chkPlug is IPlugin_Visualizer) {
HasVisPlugins = true;
break;
}
}
}
private void OkButton_Click(object sender, RoutedEventArgs e) {
NewVisSet = MakeVisSet();
DialogResult = true;
}
private void Window_Closing(object sender, CancelEventArgs e) {
if (DialogResult == true) {
return;
}
// Check to see if changes have been made.
VisualizationSet newSet = MakeVisSet();
if (newSet != mOrigSet) {
string msg = (string)FindResource("str_ConfirmDiscardChanges");
string caption = (string)FindResource("str_ConfirmDiscardChangesCaption");
MessageBoxResult result = MessageBox.Show(msg, caption, MessageBoxButton.OKCancel,
MessageBoxImage.Question);
if (result == MessageBoxResult.Cancel) {
e.Cancel = true;
}
}
}
private VisualizationSet MakeVisSet() {
if (VisualizationList.Count == 0) {
return null;
}
VisualizationSet newSet = new VisualizationSet(VisualizationList.Count);
foreach (Visualization vis in VisualizationList) {
newSet.Add(vis);
}
return newSet;
}
private void VisualizationList_SelectionChanged(object sender,
SelectionChangedEventArgs e) {
bool isItemSelected = (visualizationGrid.SelectedItem != null);
IsEditEnabled = HasVisPlugins && isItemSelected;
IsRemoveEnabled = isItemSelected;
IsUpEnabled = isItemSelected && visualizationGrid.SelectedIndex != 0;
IsDownEnabled = isItemSelected &&
visualizationGrid.SelectedIndex != VisualizationList.Count - 1;
}
private void VisualizationList_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
EditSelectedItem();
}
private void NewButton_Click(object sender, RoutedEventArgs e) {
EditVisualization dlg = new EditVisualization(this, mProject, mFormatter, mOffset,
null);
if (dlg.ShowDialog() != true) {
return;
}
VisualizationList.Add(dlg.NewVis);
}
private void EditButton_Click(object sender, RoutedEventArgs e) {
EditSelectedItem();
}
private void EditSelectedItem() {
if (!IsEditEnabled) {
// can happen on a double-click
return;
}
Visualization item = (Visualization)visualizationGrid.SelectedItem;
EditVisualization dlg = new EditVisualization(this, mProject, mFormatter, mOffset,
item);
if (dlg.ShowDialog() != true) {
return;
}
int index = VisualizationList.IndexOf(item);
VisualizationList.Remove(item);
VisualizationList.Insert(index, dlg.NewVis);
}
private void RemoveButton_Click(object sender, RoutedEventArgs e) {
Visualization item = (Visualization)visualizationGrid.SelectedItem;
VisualizationList.Remove(item);
}
private void UpButton_Click(object sender, RoutedEventArgs e) {
Visualization item = (Visualization)visualizationGrid.SelectedItem;
int index = VisualizationList.IndexOf(item);
Debug.Assert(index > 0);
VisualizationList.Remove(item);
VisualizationList.Insert(index - 1, item);
visualizationGrid.SelectedIndex = index - 1;
}
private void DownButton_Click(object sender, RoutedEventArgs e) {
Visualization item = (Visualization)visualizationGrid.SelectedItem;
int index = VisualizationList.IndexOf(item);
Debug.Assert(index < VisualizationList.Count - 1);
VisualizationList.Remove(item);
VisualizationList.Insert(index + 1, item);
visualizationGrid.SelectedIndex = index + 1;
}
}
}