Files
meelstorm 745a15b162 2.0.10
2025-10-20 09:30:08 +02:00

140 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CandyboxPlugin.Recipe;
using MaterialSkin.Controls;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using Point = OpenCvSharp.Point;
namespace Lindt.Candybox.Demo
{
public partial class MapEditor : MaterialForm
{
private readonly Mat _original;
private readonly HistoRecipe _runningRecipe;
private HistoRecipe _recipe;
public MapEditor(Mat original, HistoRecipe runningRecipe)
{
_original = original;
_runningRecipe = runningRecipe;
InitializeComponent();
_recipe = runningRecipe.Clone();
_recipe.ProcessImage(original);
Mat matContours2;
if (_recipe.LearnedParameters.CandyParameters.Count == 0)
{
Mat matContours = new Mat(new OpenCvSharp.Size(512, 512), MatType.CV_8UC3, Scalar.Black);
Cv2.DrawContours(matContours, _recipe.LastContours, -1, Scalar.White, -1);
matContours2 = matContours.Resize(new OpenCvSharp.Size(1800, 1408));
}
else
{
Mat matContours = new Mat(new OpenCvSharp.Size(512, 512), MatType.CV_8UC3, Scalar.Black);
Cv2.DrawContours(matContours, _recipe.LastContours, -1, Scalar.White, -1);
matContours2 = matContours.Resize(new OpenCvSharp.Size(1800, 1408));
}
udMemoryBuffer.Value = _recipe.LearnedParameters.Configuration.MemoryBuffer;
maskControl1.Mask = matContours2.ToBitmap();
maskControl1.Original = original.Clone().ToBitmap();
maskControl1.VectorLines = _recipe.LearnedParameters.Cuts.Select(x => x.Translate(_recipe.BlisterCenter).Scale(1800f / 512f, 1408f / 512f)).ToList();
// Set default mode to Brush
SetMode(EditMode.Brush);
}
private void SetMode(EditMode mode)
{
maskControl1.CurrentMode = mode;
// Reset button colors
btnBrush.Primary = mode == EditMode.Brush;
btnDrawLine.Primary = mode == EditMode.DrawLine;
btnRemoveLine.Primary = mode == EditMode.RemoveLine;
btnMoveLine.Primary = mode == EditMode.MoveLine;
btnMoveVertices.Primary = mode == EditMode.MoveVertices;
btnBrush.Invalidate();
btnDrawLine.Invalidate();
btnRemoveLine.Invalidate();
btnMoveLine.Invalidate();
btnMoveVertices.Invalidate();
}
private void btnBrush_Click(object sender, EventArgs e)
{
SetMode(EditMode.Brush);
}
private void btnDrawLine_Click(object sender, EventArgs e)
{
SetMode(EditMode.DrawLine);
}
private void btnRemoveLine_Click(object sender, EventArgs e)
{
SetMode(EditMode.RemoveLine);
}
private void btnMoveLine_Click(object sender, EventArgs e)
{
SetMode(EditMode.MoveLine);
}
private void btnMoveVertices_Click(object sender, EventArgs e)
{
SetMode(EditMode.MoveVertices);
}
private void materialRaisedButton3_Click(object sender, EventArgs e)
{
List<Point[]> contours = new List<Point[]>();
foreach (Point[] contour in maskControl1.LastContours)
{
contours.Add(contour.Select(x => new Point(x.X / 1800f * 512f, x.Y / 1408f * 512f)).ToArray());
}
_recipe.LearnedParameters.Configuration.MemoryBuffer = (int)udMemoryBuffer.Value;
_recipe.LearnContours(contours.ToArray(), _original, maskControl1.VectorLines.Select(x => x.Scale(512f / 1800f, 512f / 1408f).Sub(_recipe.BlisterCenter)).ToList(), relearn: false);
DialogResult = DialogResult.OK;
}
private void btnRelearnPositions_Click(object sender, EventArgs e)
{
List<Point[]> contours = new List<Point[]>();
foreach (Point[] contour in maskControl1.LastContours)
{
contours.Add(contour.Select(x => new Point(x.X / 1800f * 512f, x.Y / 1408f * 512f)).ToArray());
}
_recipe.LearnedParameters.Configuration.MemoryBuffer = (int)udMemoryBuffer.Value;
_recipe.LearnContours(contours.ToArray(), _original, maskControl1.VectorLines.Select(x => x.Scale(512f / 1800f, 512f / 1408f).Sub(_recipe.BlisterCenter)).ToList(),relearn:true);
_runningRecipe.Reload();
DialogResult = DialogResult.OK;
}
private void materialRaisedButton1_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
}
}