Minecraft-Console-Client/MinecraftClient/Tui/SlotViewModel.cs

158 lines
5.1 KiB
C#
Raw Normal View History

TUI for MCC (#2976) * feat: implement interactive TUI inventory viewer - Added InventoryTui command to open an interactive terminal user interface for inventory management. - Introduced InventoryApp and InventoryMainView classes for TUI layout and functionality. - Created InventoryViewModel and SlotViewModel to manage inventory data and display. - Integrated Consolonia for enhanced console UI experience. - Updated McClient to support console message handling during TUI operation. These changes enhance user interaction with inventory management in Minecraft Console Client. * feat: enhance debugging workflow with mcc-debug.sh and TUI support - Introduced mcc-debug.sh for streamlined one-step build, server start, and MCC launch. - Added TUI mode for improved user experience during debugging sessions. - Updated mcc-env.sh to include new debug helpers and TUI mode functionality. - Enhanced SKILL.md with detailed instructions for using the new debugging tools and console modes. These changes significantly improve the debugging process for Minecraft Console Client, making it more efficient and user-friendly. * feat: introduce TUI console backend and related enhancements - Added ClassicConsoleBackend and TuiConsoleBackend to support different console I/O modes. - Implemented IConsoleBackend interface for better abstraction of console operations. - Enhanced ConsoleIO to utilize the new backend structure for input and output handling. - Introduced MainTuiView and MccTuiApp for a full-screen TUI experience using Avalonia. - Updated InventoryTuiHost to manage TUI lifecycle and interactions. These changes significantly improve the console experience in Minecraft Console Client, providing a more flexible and user-friendly interface. * refactor: remove InventoryTui command implementation - Deleted the InventoryTui class, which provided an interactive terminal user interface for inventory management. - This change simplifies the command structure as part of ongoing improvements to the console experience in Minecraft Console Client. The removal of this command is aligned with recent enhancements to the console backend and user interface. * refactor: update InventoryMainView and MainTuiView for improved UI handling - Changed several fields from readonly to mutable in InventoryMainView to allow for dynamic updates. - Introduced a new McColorParser class for parsing Minecraft color codes and creating colored text blocks. - Enhanced MainTuiView to support formatted log lines and improved notification handling. - Updated chat scrolling behavior to ensure better user experience during text input and log display. These changes streamline the UI components and enhance the overall console experience in Minecraft Console Client. * feat: enhance command input handling in MainTuiView - Updated command input to use event routing for better key handling. - Added support for Ctrl key shortcuts to improve text manipulation (e.g., word deletion, caret movement). - Implemented text cleaning on input change to prevent newline characters. - Improved health and food status bar rendering with a new method for building bar text. These changes significantly enhance the user experience in the TUI by providing more intuitive command input functionality. * feat: enhance TUI command suggestion functionality - Introduced a new CommandSuggestion struct for backend-independent suggestion handling. - Updated ConsoleIO to streamline suggestion updates for both Classic and TUI backends. - Enhanced MainTuiView to display command suggestions with improved visibility and interaction. - Increased the maximum number of displayed suggestions from 6 to 10 for better user experience. These changes significantly improve the command input experience in the TUI, making it more intuitive and user-friendly. * feat: enhance offline command autocomplete functionality - Introduced an OfflineAutocompleteHandler to provide command suggestions for offline commands. - Added support for tab cycling through suggestions in the TUI. - Improved command input handling to clear suggestions when necessary and manage user input more effectively. - Updated TuiConsoleBackend to integrate with the new autocomplete feature. These changes significantly enhance the user experience by making command input more intuitive and responsive in offline scenarios. * feat: enhance localization and user feedback in TUI - Updated various TUI components to utilize localized strings for improved user experience. - Enhanced debug state output with translated labels for better clarity. - Improved inventory command help messages with localized text. - Added new translations for console mode descriptions and inventory viewer prompts. These changes significantly enhance the usability and accessibility of the TUI in Minecraft Console Client, making it more user-friendly and informative. * feat: enforce localization for user-facing strings in AGENTS.md - Added guidelines to ensure all user-facing text, including log messages and error messages, is managed through the translation system. - Specified the use of `Translations.resx` and `Translations.Designer.cs` for localization, emphasizing the importance of avoiding hardcoded strings in source code. These changes improve the consistency and accessibility of user-facing content across the application.
2026-03-26 02:08:18 +08:00
using System.ComponentModel;
using System.Runtime.CompilerServices;
using MinecraftClient.Inventory;
namespace MinecraftClient.Tui
{
public class SlotViewModel : INotifyPropertyChanged
{
private bool _isSelected;
private bool _isHovered;
public int SlotId { get; }
public string ItemDisplayText { get; private set; }
public string CountDisplay { get; private set; }
public string FullInfo { get; private set; }
public string ItemTypeName { get; private set; }
public bool IsEmpty { get; private set; }
public bool IsHotbar { get; }
public int HotbarIndex { get; }
public ItemType ItemType { get; private set; }
public int ItemCount { get; private set; }
public Item? RawItem { get; private set; }
public int NameMaxWidth { get; set; } = 9;
public int NameMaxLines { get; set; } = 1;
public bool IsSelected
{
get => _isSelected;
set { _isSelected = value; OnPropertyChanged(); }
}
public bool IsHovered
{
get => _isHovered;
set { _isHovered = value; OnPropertyChanged(); }
}
public SlotViewModel(int slotId, bool isHotbar = false, int hotbarIndex = -1)
{
SlotId = slotId;
IsHotbar = isHotbar;
HotbarIndex = hotbarIndex;
ItemDisplayText = "";
CountDisplay = "";
FullInfo = "";
ItemTypeName = "";
IsEmpty = true;
ItemType = ItemType.Air;
ItemCount = 0;
}
public void Update(Item? item)
{
RawItem = item;
if (item == null || item.IsEmpty)
{
ItemDisplayText = "";
CountDisplay = "";
FullInfo = "";
ItemTypeName = "";
IsEmpty = true;
ItemType = ItemType.Air;
ItemCount = 0;
}
else
{
ItemType = item.Type;
ItemCount = item.Count;
string typeName = item.GetTypeString();
ItemTypeName = typeName;
ItemDisplayText = FormatMultiLine(typeName, NameMaxWidth, NameMaxLines);
CountDisplay = item.Count > 1 ? $"x{item.Count}" : "";
FullInfo = item.ToFullString();
IsEmpty = false;
}
OnPropertyChanged(nameof(ItemDisplayText));
OnPropertyChanged(nameof(CountDisplay));
OnPropertyChanged(nameof(FullInfo));
OnPropertyChanged(nameof(IsEmpty));
OnPropertyChanged(nameof(ItemType));
OnPropertyChanged(nameof(ItemTypeName));
OnPropertyChanged(nameof(ItemCount));
}
/// <summary>
/// Format item name into multi-line display text that fits within
/// maxWidth columns and maxLines lines. Breaks at word boundaries.
/// </summary>
private static string FormatMultiLine(string name, int maxWidth, int maxLines)
{
if (string.IsNullOrEmpty(name))
return "";
int colonIdx = name.LastIndexOf(':');
if (colonIdx >= 0 && colonIdx < name.Length - 1)
name = name[(colonIdx + 1)..];
name = name.Replace("_", " ").Trim();
name = InsertCamelCaseSpaces(name);
if (maxLines <= 1 || name.Length <= maxWidth)
return name.Length <= maxWidth ? name : name[..maxWidth];
var lines = new System.Collections.Generic.List<string>();
string remaining = name;
for (int line = 0; line < maxLines && remaining.Length > 0; line++)
{
if (remaining.Length <= maxWidth)
{
lines.Add(remaining);
break;
}
int breakAt = -1;
for (int i = maxWidth; i >= 1; i--)
{
if (remaining[i] == ' ')
{
breakAt = i;
break;
}
}
if (breakAt < 0)
breakAt = maxWidth;
lines.Add(remaining[..breakAt].TrimEnd());
remaining = remaining[breakAt..].TrimStart();
}
return string.Join("\n", lines);
}
private static string InsertCamelCaseSpaces(string s)
{
if (s.Length < 2) return s;
var sb = new System.Text.StringBuilder(s.Length + 4);
sb.Append(s[0]);
for (int i = 1; i < s.Length; i++)
{
if (char.IsUpper(s[i]) && char.IsLower(s[i - 1]))
sb.Append(' ');
sb.Append(s[i]);
}
return sb.ToString();
}
public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string? name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}