scripting to Linear Array

This commit is contained in:
LarsBrubaker 2021-06-14 08:00:14 -07:00
parent 3396326200
commit 47d2d66089
14 changed files with 269 additions and 218 deletions

View file

@ -37,181 +37,179 @@ using MatterHackers.Localizations;
namespace MatterHackers.MatterControl.DesignTools
{
public class SheetEditor : IObject3DEditor
public class SheetEditorWidget : FlowLayoutWidget
{
public class SheetEditorWidget : FlowLayoutWidget
private SheetData sheetData;
Point2D selectedCell = new Point2D(-1, -1);
Dictionary<(int, int), GuiWidget> CellWidgetsByLocation = new Dictionary<(int, int), GuiWidget>();
private ThemeConfig theme;
private MHTextEditWidget editSelectedName;
private MHTextEditWidget editSelectedExpression;
public SheetEditorWidget(SheetData sheetData, UndoBuffer undoBuffer, ThemeConfig theme)
: base(FlowDirection.TopToBottom)
{
private SheetObject3D sheetObject;
private SheetData sheetData;
Point2D selectedCell = new Point2D(-1, -1);
Dictionary<(int, int), GuiWidget> CellWidgetsByLocation = new Dictionary<(int, int), GuiWidget>();
private ThemeConfig theme;
private MHTextEditWidget editSelectedName;
private MHTextEditWidget editSelectedExpression;
this.theme = theme;
HAnchor = HAnchor.MaxFitOrStretch;
public SheetEditorWidget(IObject3D item, UndoBuffer undoBuffer, ThemeConfig theme)
: base(FlowDirection.TopToBottom)
this.sheetData = sheetData;
var countWidth = 10 * GuiWidget.DeviceScale;
var cellWidth = 50 * GuiWidget.DeviceScale;
// put in the edit row
var editSelectionGroup = this.AddChild(new FlowLayoutWidget()
{
this.theme = theme;
HAnchor = HAnchor.MaxFitOrStretch;
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
});
sheetObject = item as SheetObject3D;
sheetData = sheetObject.SheetData;
var countWidth = 10 * GuiWidget.DeviceScale;
var cellWidth = 50 * GuiWidget.DeviceScale;
editSelectedName = new MHTextEditWidget("", theme, cellWidth, messageWhenEmptyAndNotSelected: "Name".Localize())
{
HAnchor = HAnchor.Absolute,
SelectAllOnFocus = true,
};
editSelectedName.ActualTextEditWidget.EditComplete += SelectedName_EditComplete;
editSelectionGroup.AddChild(editSelectedName);
editSelectedExpression = new MHTextEditWidget("", theme, messageWhenEmptyAndNotSelected: "Select cell to edit".Localize())
{
HAnchor = HAnchor.Stretch,
SelectAllOnFocus = true,
};
editSelectionGroup.AddChild(editSelectedExpression);
editSelectedExpression.ActualTextEditWidget.EditComplete += ActualTextEditWidget_EditComplete1;
// put in the edit row
var editSelectionGroup = this.AddChild(new FlowLayoutWidget()
// put in the header row
var topRow = this.AddChild(new FlowLayoutWidget()
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
});
topRow.AddChild(new GuiWidget(cellWidth, 1));
for (int x = 0; x < sheetData.Width; x++)
{
topRow.AddChild(new TextWidget(((char)('A' + x)).ToString())
{
HAnchor = HAnchor.Stretch,
TextColor = theme.TextColor
});
}
for (int y = 0; y < sheetData.Height; y++)
{
var row = new FlowLayoutWidget()
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
};
this.AddChild(row);
// add row count
row.AddChild(new TextWidget((y + 1).ToString())
{
TextColor = theme.TextColor,
});
editSelectedName = new MHTextEditWidget("", theme, cellWidth, messageWhenEmptyAndNotSelected: "Name".Localize())
{
HAnchor = HAnchor.Absolute,
SelectAllOnFocus = true,
};
editSelectedName.ActualTextEditWidget.EditComplete += SelectedName_EditComplete;
editSelectionGroup.AddChild(editSelectedName);
editSelectedExpression = new MHTextEditWidget("", theme, messageWhenEmptyAndNotSelected: "Select cell to edit".Localize())
{
HAnchor = HAnchor.Stretch,
SelectAllOnFocus = true,
};
editSelectionGroup.AddChild(editSelectedExpression);
editSelectedExpression.ActualTextEditWidget.EditComplete += ActualTextEditWidget_EditComplete1;
// put in the header row
var topRow = this.AddChild(new FlowLayoutWidget()
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
});
topRow.AddChild(new GuiWidget(cellWidth, 1));
for (int x = 0; x < sheetData.Width; x++)
{
topRow.AddChild(new TextWidget(((char)('A' + x)).ToString())
{
HAnchor = HAnchor.Stretch,
TextColor = theme.TextColor
});
}
var capturedX = x;
var capturedY = y;
for (int y = 0; y < sheetData.Height; y++)
{
var row = new FlowLayoutWidget()
var edit = new MHTextEditWidget(sheetData[x, y].Expression, theme)
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
SelectAllOnFocus = true,
};
this.AddChild(row);
// add row count
row.AddChild(new TextWidget((y + 1).ToString())
CellWidgetsByLocation.Add((capturedX, capturedY), edit);
edit.MouseDown += (s, e) => SelectCell(capturedX, capturedY);
row.AddChild(edit);
edit.ActualTextEditWidget.EditComplete += (s, e) =>
{
TextColor = theme.TextColor,
});
for (int x = 0; x < sheetData.Width; x++)
{
var capturedX = x;
var capturedY = y;
var edit = new MHTextEditWidget(sheetData[x, y].Expression, theme)
{
HAnchor = HAnchor.Stretch,
SelectAllOnFocus = true,
};
CellWidgetsByLocation.Add((capturedX, capturedY), edit);
edit.MouseDown += (s, e) => SelectCell(capturedX, capturedY);
row.AddChild(edit);
edit.ActualTextEditWidget.EditComplete += (s, e) =>
{
editSelectedExpression.Text = edit.Text;
sheetData[capturedX, capturedY].Expression = edit.Text;
Recalculate();
};
}
editSelectedExpression.Text = edit.Text;
sheetData[capturedX, capturedY].Expression = edit.Text;
sheetData.Recalculate();
};
}
}
private void ActualTextEditWidget_EditComplete1(object sender, EventArgs e)
{
if (selectedCell.x == -1)
{
return;
}
sheetData[selectedCell.x, selectedCell.y].Expression = editSelectedExpression.Text;
CellWidgetsByLocation[(selectedCell.x, selectedCell.y)].Text = editSelectedExpression.Text;
}
private void SelectedName_EditComplete(object sender, EventArgs e)
{
if (selectedCell.x == -1)
{
return;
}
var existingNames = new HashSet<string>();
for (int y = 0; y < sheetData.Height; y++)
{
for (int x = 0; x < sheetData.Width; x++)
{
if (x != selectedCell.x || y != selectedCell.y)
{
var currentName = sheetData[x, y].Name;
if (!string.IsNullOrEmpty(currentName))
{
existingNames.Add(currentName);
}
}
}
}
var name = agg_basics.GetNonCollidingName(editSelectedName.Text, existingNames);
editSelectedName.Text = name;
sheetData[selectedCell.x, selectedCell.y].Name = name;
}
private void SelectCell(int x, int y)
{
if (selectedCell.x != -1)
{
CellWidgetsByLocation[(selectedCell.x, selectedCell.y)].BorderColor = Color.Transparent;
}
selectedCell.x = x;
selectedCell.y = y;
CellWidgetsByLocation[(selectedCell.x, selectedCell.y)].BorderColor = theme.PrimaryAccentColor;
editSelectedExpression.Text = sheetData[x, y].Expression;
if (string.IsNullOrEmpty(sheetData[x, y].Name))
{
editSelectedName.Text = $"{(char)('A' + x)}{y + 1}";
}
else
{
editSelectedName.Text = sheetData[x, y].Name;
}
}
private void Recalculate()
{
sheetObject.Invalidate(InvalidateType.SheetUpdated);
}
}
private void ActualTextEditWidget_EditComplete1(object sender, EventArgs e)
{
if (selectedCell.x == -1)
{
return;
}
sheetData[selectedCell.x, selectedCell.y].Expression = editSelectedExpression.Text;
CellWidgetsByLocation[(selectedCell.x, selectedCell.y)].Text = editSelectedExpression.Text;
}
private void SelectedName_EditComplete(object sender, EventArgs e)
{
if (selectedCell.x == -1)
{
return;
}
var existingNames = new HashSet<string>();
for (int y = 0; y < sheetData.Height; y++)
{
for (int x = 0; x < sheetData.Width; x++)
{
if (x != selectedCell.x || y != selectedCell.y)
{
var currentName = sheetData[x, y].Name;
if (!string.IsNullOrEmpty(currentName))
{
existingNames.Add(currentName);
}
}
}
}
var name = agg_basics.GetNonCollidingName(editSelectedName.Text, existingNames);
editSelectedName.Text = name;
sheetData[selectedCell.x, selectedCell.y].Name = name;
}
private void SelectCell(int x, int y)
{
if (selectedCell.x != -1)
{
CellWidgetsByLocation[(selectedCell.x, selectedCell.y)].BorderColor = Color.Transparent;
}
selectedCell.x = x;
selectedCell.y = y;
CellWidgetsByLocation[(selectedCell.x, selectedCell.y)].BorderColor = theme.PrimaryAccentColor;
editSelectedExpression.Text = sheetData[x, y].Expression;
if (string.IsNullOrEmpty(sheetData[x, y].Name))
{
editSelectedName.Text = $"{(char)('A' + x)}{y + 1}";
}
else
{
editSelectedName.Text = sheetData[x, y].Name;
}
}
}
public class SheetEditor : IObject3DEditor
{
string IObject3DEditor.Name => "Sheet Editor";
IEnumerable<Type> IObject3DEditor.SupportedTypes() => new[] { typeof(SheetObject3D) };
public GuiWidget Create(IObject3D item, UndoBuffer undoBuffer, ThemeConfig theme)
{
return new SheetEditorWidget(item, undoBuffer, theme);
if (item is SheetObject3D sheetObject)
{
return new SheetEditorWidget(sheetObject.SheetData, undoBuffer, theme);
}
return null;
}
}
}