Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and improvements of some names displaying. #954

Merged
merged 4 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions UndertaleModTool/Converters/StringTitleConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Data;

namespace UndertaleModTool
{
public class StringTitleConverter : IValueConverter
{
public static readonly Regex NewLineRegex = new(@"\r\n?|\n", RegexOptions.Compiled);

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is not string str)
return null;

if (str.Length > 256)
str = str[..256] + "...";
str = NewLineRegex.Replace(str, " ");

return str;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
6 changes: 4 additions & 2 deletions UndertaleModTool/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<local:CompareNumbersConverter x:Key="CompareNumbersConverter"/>
<BooleanToVisibilityConverter x:Key="BoolToVisConverter"/> <!-- (built-in converter) -->
<local:TabTitleConverter x:Key="TabTitleConverter"/>
<local:StringTitleConverter x:Key="StringTitleConverter"/>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="New" Executed="Command_New" />
Expand Down Expand Up @@ -345,7 +346,7 @@
<TreeViewItem Header="Strings" ItemsSource="{Binding Strings, Converter={StaticResource FilteredViewConverter}}" Visibility="{Binding Strings, Converter={StaticResource VisibleIfNotNull}}">
<TreeViewItem.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type undertale:UndertaleString}">
<TextBlock Text="{Binding Content}" />
<TextBlock Text="{Binding Content, Mode=OneWay, Converter={StaticResource StringTitleConverter}}" />
</HierarchicalDataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
Expand Down Expand Up @@ -428,7 +429,8 @@
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource TabTitleConverter}" Mode="OneWay">
<Binding Path="OpenedObject" Mode="OneTime"/>
<Binding Mode="OneTime"/>
<Binding Path="OpenedObject" Mode="OneWay"/>
<!-- for notification only -->
<Binding Path="OpenedObject.Name.Content" Mode="OneWay"/>
</MultiBinding>
Expand Down
31 changes: 27 additions & 4 deletions UndertaleModTool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,24 @@ public class Tab : INotifyPropertyChanged

public object OpenedObject { get; set; }
public string TabTitle { get; set; } = "Untitled";
public bool IsCustomTitle { get; set; }
public int TabIndex { get; set; }
public bool AutoClose { get; set; } = false;

public Tab(object obj, int tabIndex, string tabTitle = null)
{
OpenedObject = obj;
TabIndex = tabIndex;
TabTitle = tabTitle ?? GetTitleForObject(obj);
AutoClose = obj is DescriptionView;

IsCustomTitle = tabTitle is not null;
if (IsCustomTitle)
{
if (tabTitle.Length > 64)
TabTitle = tabTitle[..64] + "...";
else
TabTitle = tabTitle;
}
}

public static string GetTitleForObject(object obj)
Expand Down Expand Up @@ -128,9 +137,14 @@ public static string GetTitleForObject(object obj)
else
Debug.WriteLine($"Could not handle type {obj.GetType()}");
}
else if (obj is UndertaleString)
else if (obj is UndertaleString str)
{
title = "String - " + ((UndertaleString)obj).Content;
string stringFirstLine = str.Content;
int stringLength = StringTitleConverter.NewLineRegex.Match(stringFirstLine).Index;
if (stringLength != -1)
stringFirstLine = stringFirstLine[..stringLength] + " ...";

title = "String - " + stringFirstLine;
}
else if (obj is UndertaleChunkVARI)
{
Expand All @@ -153,6 +167,9 @@ public static string GetTitleForObject(object obj)
Debug.WriteLine($"Could not handle type {obj.GetType()}");
}

if (title.Length > 64)
title = title[..64] + "...";

return title;
}

Expand All @@ -166,7 +183,13 @@ public class TabTitleConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return Tab.GetTitleForObject(values[0]);
if (values[0] is not Tab tab)
return null;

if (!tab.IsCustomTitle)
tab.TabTitle = Tab.GetTitleForObject(values[1]);

return tab.TabTitle;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
Expand Down