BizHawk/BizHawk.Client.EmuHawk/tools/Lua/LuaFunctionsForm.cs

192 lines
4.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk
{
public partial class LuaFunctionsForm : Form
{
private readonly LuaDocumentation _docs;
private readonly Sorting _columnSort = new Sorting();
2019-11-30 17:28:56 +00:00
private List<LibraryFunction> _functionList = new List<LibraryFunction>();
private List<LibraryFunction> _filteredList = new List<LibraryFunction>();
public LuaFunctionsForm(LuaDocumentation docs)
{
_docs = docs;
InitializeComponent();
FunctionView.RetrieveVirtualItem += FunctionView_QueryItemText;
}
private void GenerateFilteredList()
{
if (!string.IsNullOrWhiteSpace(FilterBox.Text))
{
2019-11-30 17:28:56 +00:00
_filteredList = _functionList
2019-03-18 14:06:37 +00:00
.Where(f => $"{f.Library}.{f.Name}".ToLowerInvariant().Contains(FilterBox.Text.ToLowerInvariant()))
.ToList();
}
else
{
2019-11-30 17:28:56 +00:00
_filteredList = _functionList.ToList();
}
}
private void LuaFunctionList_Load(object sender, EventArgs e)
{
_functionList = _docs
2017-05-19 16:05:21 +00:00
.OrderBy(l => l.Library)
.ThenBy(l => l.Name)
2014-06-03 01:21:49 +00:00
.ToList();
UpdateList();
FilterBox.Focus();
ToWikiMarkupButton.Visible = VersionInfo.DeveloperBuild;
}
private void FunctionView_QueryItemText(object sender, RetrieveVirtualItemEventArgs e)
{
var entry = _filteredList[e.ItemIndex];
e.Item = new ListViewItem(entry.ReturnType);
e.Item.SubItems.Add(entry.Library);
e.Item.SubItems.Add(entry.Name);
e.Item.SubItems.Add(entry.ParameterList);
e.Item.SubItems.Add(entry.Description);
}
private void OrderColumn(int column)
{
_columnSort.Column = column;
if (_columnSort.Descending)
{
switch (column)
{
case 0: // Return
2019-11-30 17:28:56 +00:00
_functionList = _functionList.OrderByDescending(x => x.ReturnType).ToList();
break;
case 1: // Library
2019-11-30 17:28:56 +00:00
_functionList = _functionList.OrderByDescending(x => x.Library).ToList();
break;
case 2: // Name
2019-11-30 17:28:56 +00:00
_functionList = _functionList.OrderByDescending(x => x.Name).ToList();
break;
case 3: // Parameters
2019-11-30 17:28:56 +00:00
_functionList = _functionList.OrderByDescending(x => x.ParameterList).ToList();
break;
case 4: // Description
2019-11-30 17:28:56 +00:00
_functionList = _functionList.OrderByDescending(x => x.Description).ToList();
break;
}
}
else
{
switch (column)
{
case 0: // Return
2019-11-30 17:28:56 +00:00
_functionList = _functionList.OrderBy(x => x.ReturnType).ToList();
break;
case 1: // Library
2019-11-30 17:28:56 +00:00
_functionList = _functionList.OrderBy(x => x.Library).ToList();
break;
case 2: // Name
2019-11-30 17:28:56 +00:00
_functionList = _functionList.OrderBy(x => x.Name).ToList();
break;
case 3: // Parameters
2019-11-30 17:28:56 +00:00
_functionList = _functionList.OrderBy(x => x.ParameterList).ToList();
break;
case 4: // Description
2019-11-30 17:28:56 +00:00
_functionList = _functionList.OrderBy(x => x.Description).ToList();
break;
}
}
UpdateList();
}
private void Ok_Click(object sender, EventArgs e)
{
Close();
}
private void FunctionView_ColumnClick(object sender, ColumnClickEventArgs e)
{
OrderColumn(e.Column);
}
2017-05-19 16:05:21 +00:00
private class Sorting
{
private int _column = 1;
public int Column
{
2019-10-29 13:23:40 +00:00
get => _column;
set
{
if (_column == value)
{
2019-10-29 13:23:40 +00:00
Descending ^= true;
}
_column = value;
}
}
2019-10-29 13:23:40 +00:00
public bool Descending { get; private set; }
}
private void FunctionView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.C && e.Control && !e.Alt && !e.Shift) // Copy
{
FunctionView_Copy(null, null);
}
}
private void FunctionView_Copy(object sender, EventArgs e)
{
if (FunctionView.SelectedIndices.Count == 0)
{
return;
}
var sb = new StringBuilder();
foreach (int index in FunctionView.SelectedIndices)
{
var itm = _filteredList[index];
sb.Append($"//{itm.Library}.{itm.Name}{itm.ParameterList}"); // comment style not an accident: the 'declaration' is not legal lua, so use of -- to comment it shouldn't suggest it. right?
if (itm.Example != null)
{
sb.AppendLine();
sb.AppendLine(itm.Example);
}
}
if (sb.Length > 0)
2019-10-29 13:23:40 +00:00
{
Clipboard.SetText(sb.ToString());
2019-10-29 13:23:40 +00:00
}
}
private void UpdateList()
{
GenerateFilteredList();
FunctionView.VirtualListSize = _filteredList.Count;
}
private void FilterBox_KeyUp(object sender, KeyEventArgs e)
{
UpdateList();
}
private void ToWikiMarkupButton_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(_docs.ToTASVideosWikiMarkup());
}
}
}