2021-05-30 23:01:31 -07:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2018, Lars Brubaker, John Lewin
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
The views and conclusions contained in the software and documentation are those
|
|
|
|
|
|
of the authors and should not be interpreted as representing official policies,
|
|
|
|
|
|
either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using MatterHackers.Agg;
|
|
|
|
|
|
using MatterHackers.Agg.UI;
|
|
|
|
|
|
using MatterHackers.MatterControl.PartPreviewWindow;
|
|
|
|
|
|
using MatterHackers.DataConverters3D;
|
|
|
|
|
|
using MatterHackers.Localizations;
|
2021-09-06 08:35:34 -07:00
|
|
|
|
using System.Linq;
|
2021-05-30 23:01:31 -07:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.DesignTools
|
|
|
|
|
|
{
|
2021-06-14 08:00:14 -07:00
|
|
|
|
public class SheetEditorWidget : FlowLayoutWidget
|
2021-05-30 23:01:31 -07:00
|
|
|
|
{
|
2021-09-06 08:35:34 -07:00
|
|
|
|
public SheetData SheetData { get; private set; }
|
2021-06-14 08:00:14 -07:00
|
|
|
|
Point2D selectedCell = new Point2D(-1, -1);
|
|
|
|
|
|
Dictionary<(int, int), GuiWidget> CellWidgetsByLocation = new Dictionary<(int, int), GuiWidget>();
|
2022-03-13 11:12:01 -07:00
|
|
|
|
|
|
|
|
|
|
public UndoBuffer UndoBuffer { get; }
|
|
|
|
|
|
|
|
|
|
|
|
private ThemeConfig theme;
|
2022-07-15 17:28:39 -07:00
|
|
|
|
private ThemedTextEditWidget editSelectedName;
|
|
|
|
|
|
public ThemedTextEditWidget EditSelectedExpression { get; private set; }
|
2021-09-06 08:35:34 -07:00
|
|
|
|
private GridWidget gridWidget;
|
2021-06-14 08:00:14 -07:00
|
|
|
|
|
2021-09-28 12:55:39 -07:00
|
|
|
|
public SheetEditorWidget(SheetData sheetData, SheetObject3D sheetObject, UndoBuffer undoBuffer, ThemeConfig theme)
|
2021-06-14 08:00:14 -07:00
|
|
|
|
: base(FlowDirection.TopToBottom)
|
2021-05-30 23:01:31 -07:00
|
|
|
|
{
|
2022-03-13 11:12:01 -07:00
|
|
|
|
this.UndoBuffer = undoBuffer;
|
2021-06-14 08:00:14 -07:00
|
|
|
|
this.theme = theme;
|
|
|
|
|
|
HAnchor = HAnchor.MaxFitOrStretch;
|
|
|
|
|
|
|
2021-09-06 08:35:34 -07:00
|
|
|
|
this.SheetData = sheetData;
|
|
|
|
|
|
var cellEditNameWidth = 80 * GuiWidget.DeviceScale;
|
2021-06-14 08:00:14 -07:00
|
|
|
|
|
|
|
|
|
|
// put in the edit row
|
|
|
|
|
|
var editSelectionGroup = this.AddChild(new FlowLayoutWidget()
|
|
|
|
|
|
{
|
|
|
|
|
|
HAnchor = HAnchor.Stretch,
|
|
|
|
|
|
VAnchor = VAnchor.Fit,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-07-15 17:28:39 -07:00
|
|
|
|
editSelectedName = new ThemedTextEditWidget("", theme, cellEditNameWidth, messageWhenEmptyAndNotSelected: "Name".Localize())
|
2021-06-14 08:00:14 -07:00
|
|
|
|
{
|
|
|
|
|
|
HAnchor = HAnchor.Absolute,
|
|
|
|
|
|
};
|
|
|
|
|
|
editSelectedName.ActualTextEditWidget.EditComplete += SelectedName_EditComplete;
|
|
|
|
|
|
editSelectionGroup.AddChild(editSelectedName);
|
2022-07-15 17:28:39 -07:00
|
|
|
|
EditSelectedExpression = new ThemedTextEditWidget("", theme, messageWhenEmptyAndNotSelected: "Select cell to edit".Localize())
|
2021-05-30 23:01:31 -07:00
|
|
|
|
{
|
2021-06-14 08:00:14 -07:00
|
|
|
|
HAnchor = HAnchor.Stretch,
|
|
|
|
|
|
};
|
2021-09-06 08:35:34 -07:00
|
|
|
|
editSelectionGroup.AddChild(EditSelectedExpression);
|
|
|
|
|
|
EditSelectedExpression.ActualTextEditWidget.EditComplete += ActualTextEditWidget_EditComplete1;
|
2021-06-14 08:00:14 -07:00
|
|
|
|
|
2021-09-06 08:35:34 -07:00
|
|
|
|
gridWidget = new GridWidget(sheetData.Width + 1, sheetData.Height + 1, theme: theme);
|
2021-05-30 23:01:31 -07:00
|
|
|
|
|
2021-09-06 08:35:34 -07:00
|
|
|
|
this.AddChild(gridWidget);
|
2021-05-30 23:01:31 -07:00
|
|
|
|
|
2021-06-14 08:00:14 -07:00
|
|
|
|
for (int x = 0; x < sheetData.Width; x++)
|
|
|
|
|
|
{
|
2021-09-06 08:35:34 -07:00
|
|
|
|
var letterCell = gridWidget.GetCell(x + 1, 0);
|
|
|
|
|
|
letterCell.AddChild(new TextWidget(((char)('A' + x)).ToString())
|
2021-05-31 09:07:59 -07:00
|
|
|
|
{
|
2021-09-03 16:58:15 -07:00
|
|
|
|
HAnchor = HAnchor.Center,
|
|
|
|
|
|
VAnchor = VAnchor.Center,
|
|
|
|
|
|
TextColor = theme.TextColor,
|
2021-05-31 09:07:59 -07:00
|
|
|
|
});
|
2021-09-06 08:35:34 -07:00
|
|
|
|
|
|
|
|
|
|
letterCell.BackgroundColor = theme.SlightShade;
|
2021-06-14 08:00:14 -07:00
|
|
|
|
}
|
2021-05-30 23:01:31 -07:00
|
|
|
|
|
2021-09-06 08:35:34 -07:00
|
|
|
|
gridWidget.SetColumnWidth(0, 20);
|
2021-09-03 16:58:15 -07:00
|
|
|
|
|
2021-06-14 08:00:14 -07:00
|
|
|
|
for (int y = 0; y < sheetData.Height; y++)
|
|
|
|
|
|
{
|
|
|
|
|
|
// add row count
|
2021-09-06 08:35:34 -07:00
|
|
|
|
var numCell = gridWidget.GetCell(0, y + 1);
|
|
|
|
|
|
numCell.AddChild(new TextWidget((y + 1).ToString())
|
2021-05-30 23:01:31 -07:00
|
|
|
|
{
|
2021-06-14 08:00:14 -07:00
|
|
|
|
TextColor = theme.TextColor,
|
2021-09-06 08:35:34 -07:00
|
|
|
|
VAnchor = VAnchor.Center,
|
2021-05-30 23:01:31 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
2021-09-06 08:35:34 -07:00
|
|
|
|
numCell.BackgroundColor = theme.SlightShade;
|
|
|
|
|
|
|
2021-05-31 09:07:59 -07:00
|
|
|
|
for (int x = 0; x < sheetData.Width; x++)
|
2021-05-30 23:01:31 -07:00
|
|
|
|
{
|
2021-06-14 08:00:14 -07:00
|
|
|
|
var capturedX = x;
|
|
|
|
|
|
var capturedY = y;
|
2021-05-30 23:01:31 -07:00
|
|
|
|
|
2021-09-06 08:35:34 -07:00
|
|
|
|
var edit = new SheetFieldWidget(this, x, y, theme);
|
2021-09-03 16:58:15 -07:00
|
|
|
|
|
2021-06-14 08:00:14 -07:00
|
|
|
|
CellWidgetsByLocation.Add((capturedX, capturedY), edit);
|
2021-05-31 09:07:59 -07:00
|
|
|
|
|
2021-09-06 08:35:34 -07:00
|
|
|
|
edit.MouseUp += (s, e) => SelectCell(capturedX, capturedY);
|
2021-05-31 09:07:59 -07:00
|
|
|
|
|
2021-09-06 08:35:34 -07:00
|
|
|
|
gridWidget.GetCell(x + 1, y + 1).AddChild(edit);
|
2021-05-30 23:01:31 -07:00
|
|
|
|
}
|
2021-09-03 16:58:15 -07:00
|
|
|
|
|
2021-09-06 08:35:34 -07:00
|
|
|
|
gridWidget.ExpandToFitContent();
|
2021-05-30 23:01:31 -07:00
|
|
|
|
}
|
2021-09-28 12:55:39 -07:00
|
|
|
|
|
|
|
|
|
|
if (sheetObject != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
PublicPropertyEditor.AddWebPageLinkIfRequired(sheetObject, this, theme);
|
|
|
|
|
|
}
|
2021-06-14 08:00:14 -07:00
|
|
|
|
}
|
2021-05-30 23:01:31 -07:00
|
|
|
|
|
2021-06-14 08:00:14 -07:00
|
|
|
|
private void ActualTextEditWidget_EditComplete1(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (selectedCell.x == -1)
|
2021-05-31 09:07:59 -07:00
|
|
|
|
{
|
2021-06-14 08:00:14 -07:00
|
|
|
|
return;
|
2021-05-31 09:07:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-06 08:35:34 -07:00
|
|
|
|
SheetData[selectedCell.x, selectedCell.y].Expression = EditSelectedExpression.Text;
|
|
|
|
|
|
CellWidgetsByLocation[(selectedCell.x, selectedCell.y)].Text = EditSelectedExpression.Text;
|
|
|
|
|
|
SheetData.Recalculate();
|
2021-06-14 08:00:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SelectedName_EditComplete(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (selectedCell.x == -1)
|
2021-05-31 09:07:59 -07:00
|
|
|
|
{
|
2021-06-14 08:00:14 -07:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-05-31 09:07:59 -07:00
|
|
|
|
|
2021-06-14 08:00:14 -07:00
|
|
|
|
var existingNames = new HashSet<string>();
|
2021-09-06 08:35:34 -07:00
|
|
|
|
for (int y = 0; y < SheetData.Height; y++)
|
2021-06-14 08:00:14 -07:00
|
|
|
|
{
|
2021-09-06 08:35:34 -07:00
|
|
|
|
for (int x = 0; x < SheetData.Width; x++)
|
2021-06-06 22:18:29 -07:00
|
|
|
|
{
|
2021-06-14 08:00:14 -07:00
|
|
|
|
if (x != selectedCell.x || y != selectedCell.y)
|
2021-06-06 22:18:29 -07:00
|
|
|
|
{
|
2021-09-06 08:35:34 -07:00
|
|
|
|
var currentName = SheetData[x, y].Name;
|
2021-06-14 08:00:14 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(currentName))
|
2021-06-06 22:18:29 -07:00
|
|
|
|
{
|
2022-10-28 18:00:19 -07:00
|
|
|
|
existingNames.Add(currentName.ToLower());
|
2021-06-06 22:18:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-06-14 08:00:14 -07:00
|
|
|
|
}
|
2021-06-06 22:18:29 -07:00
|
|
|
|
|
2022-10-28 18:00:19 -07:00
|
|
|
|
var reservedWords = new string[] { "pi", "e", "true", "false", "round" };
|
|
|
|
|
|
|
|
|
|
|
|
// add all the reserved words to the existing names
|
|
|
|
|
|
foreach (var reservedWord in reservedWords)
|
|
|
|
|
|
{
|
|
|
|
|
|
existingNames.Add(reservedWord);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// first replace spaces with '_'
|
|
|
|
|
|
var name = editSelectedName.Text.Replace(' ', '_');
|
2021-09-30 15:29:09 -07:00
|
|
|
|
// next make sure we don't have the exact name already
|
2022-11-28 11:18:26 -08:00
|
|
|
|
name = agg_basics.GetNonCollidingName(name, (name) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
return !existingNames.Contains(name.ToLower());
|
|
|
|
|
|
}, false);
|
2021-06-14 08:00:14 -07:00
|
|
|
|
editSelectedName.Text = name;
|
2021-09-06 08:35:34 -07:00
|
|
|
|
SheetData[selectedCell.x, selectedCell.y].Name = name;
|
|
|
|
|
|
SheetData.Recalculate();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnDraw(Graphics2D graphics2D)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnDraw(graphics2D);
|
|
|
|
|
|
|
|
|
|
|
|
// draw the selected widget
|
|
|
|
|
|
var x = selectedCell.x;
|
|
|
|
|
|
var y = selectedCell.y;
|
|
|
|
|
|
if (x < 0 || x >= SheetData.Width || y < 0 || y >= SheetData.Height)
|
|
|
|
|
|
{
|
|
|
|
|
|
// out of bounds
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var cell = gridWidget.GetCell(x + 1, y + 1);
|
|
|
|
|
|
var bounds = cell.TransformToParentSpace(this, cell.LocalBounds);
|
|
|
|
|
|
graphics2D.Rectangle(bounds, theme.PrimaryAccentColor);
|
2021-06-14 08:00:14 -07:00
|
|
|
|
}
|
2021-09-02 17:12:32 -07:00
|
|
|
|
|
2021-09-06 08:35:34 -07:00
|
|
|
|
public void SelectCell(int x, int y)
|
2021-06-14 08:00:14 -07:00
|
|
|
|
{
|
2021-09-06 08:35:34 -07:00
|
|
|
|
if (x < 0 || x >= SheetData.Width || y < 0 || y >= SheetData.Height)
|
|
|
|
|
|
{
|
|
|
|
|
|
// out of bounds
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-14 08:00:14 -07:00
|
|
|
|
if (selectedCell.x != -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
CellWidgetsByLocation[(selectedCell.x, selectedCell.y)].BorderColor = Color.Transparent;
|
2021-05-31 09:07:59 -07:00
|
|
|
|
}
|
2021-06-14 08:00:14 -07:00
|
|
|
|
selectedCell.x = x;
|
|
|
|
|
|
selectedCell.y = y;
|
|
|
|
|
|
CellWidgetsByLocation[(selectedCell.x, selectedCell.y)].BorderColor = theme.PrimaryAccentColor;
|
2021-09-06 08:35:34 -07:00
|
|
|
|
EditSelectedExpression.Text = SheetData[x, y].Expression;
|
|
|
|
|
|
if (string.IsNullOrEmpty(SheetData[x, y].Name))
|
2021-05-31 09:07:59 -07:00
|
|
|
|
{
|
2021-06-14 08:00:14 -07:00
|
|
|
|
editSelectedName.Text = $"{(char)('A' + x)}{y + 1}";
|
2021-05-31 09:07:59 -07:00
|
|
|
|
}
|
2021-06-14 08:00:14 -07:00
|
|
|
|
else
|
2021-05-31 09:07:59 -07:00
|
|
|
|
{
|
2021-09-06 08:35:34 -07:00
|
|
|
|
editSelectedName.Text = SheetData[x, y].Name;
|
2021-05-31 09:07:59 -07:00
|
|
|
|
}
|
2021-09-06 08:35:34 -07:00
|
|
|
|
|
|
|
|
|
|
gridWidget.GetCell(x + 1, y + 1).Children.FirstOrDefault()?.Focus();
|
2021-05-30 23:01:31 -07:00
|
|
|
|
}
|
2021-06-14 08:00:14 -07:00
|
|
|
|
}
|
2021-05-30 23:01:31 -07:00
|
|
|
|
|
2021-06-14 08:00:14 -07:00
|
|
|
|
public class SheetEditor : IObject3DEditor
|
|
|
|
|
|
{
|
2021-05-31 09:07:59 -07:00
|
|
|
|
string IObject3DEditor.Name => "Sheet Editor";
|
|
|
|
|
|
|
|
|
|
|
|
IEnumerable<Type> IObject3DEditor.SupportedTypes() => new[] { typeof(SheetObject3D) };
|
|
|
|
|
|
|
|
|
|
|
|
public GuiWidget Create(IObject3D item, UndoBuffer undoBuffer, ThemeConfig theme)
|
2021-05-30 23:01:31 -07:00
|
|
|
|
{
|
2021-06-14 08:00:14 -07:00
|
|
|
|
if (item is SheetObject3D sheetObject)
|
|
|
|
|
|
{
|
2021-09-28 12:55:39 -07:00
|
|
|
|
return new SheetEditorWidget(sheetObject.SheetData, sheetObject, undoBuffer, theme);
|
2021-06-14 08:00:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
2021-05-30 23:01:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|