From 24af23a3a2e1b1c7095e42e1fecbfae65f622621 Mon Sep 17 00:00:00 2001 From: maxluli Date: Mon, 17 Oct 2022 09:23:47 +0200 Subject: [PATCH] Fixed the bug where some files would not work, and now you can click on the tiles --- NonoGramme/Form1.Designer.cs | 22 ++++++------ NonoGramme/Form1.cs | 12 +++++++ NonoGramme/Grid.cs | 65 ++++++++++++++++++++++++++++++++---- NonoGramme/Line.cs | 26 ++++++++++++++- NonoGramme/Nonogramme.cs | 4 +++ 5 files changed, 111 insertions(+), 18 deletions(-) diff --git a/NonoGramme/Form1.Designer.cs b/NonoGramme/Form1.Designer.cs index 652a697..00fe80d 100644 --- a/NonoGramme/Form1.Designer.cs +++ b/NonoGramme/Form1.Designer.cs @@ -28,21 +28,12 @@ /// private void InitializeComponent() { - this.nonogramme1 = new NonoGramme.Nonogramme(); this.lsbFiles = new System.Windows.Forms.ListBox(); this.btnImportFiles = new System.Windows.Forms.Button(); + this.nonogramme1 = new NonoGramme.Nonogramme(); ((System.ComponentModel.ISupportInitialize)(this.nonogramme1)).BeginInit(); this.SuspendLayout(); // - // nonogramme1 - // - this.nonogramme1.GridSize = new System.Drawing.Size(0, 0); - this.nonogramme1.Location = new System.Drawing.Point(12, 12); - this.nonogramme1.Name = "nonogramme1"; - this.nonogramme1.Size = new System.Drawing.Size(400, 400); - this.nonogramme1.TabIndex = 0; - this.nonogramme1.TabStop = false; - // // lsbFiles // this.lsbFiles.FormattingEnabled = true; @@ -63,6 +54,17 @@ this.btnImportFiles.UseVisualStyleBackColor = true; this.btnImportFiles.Click += new System.EventHandler(this.button1_Click); // + // nonogramme1 + // + this.nonogramme1.GridSize = new System.Drawing.Size(0, 0); + this.nonogramme1.Location = new System.Drawing.Point(12, 12); + this.nonogramme1.Name = "nonogramme1"; + this.nonogramme1.Size = new System.Drawing.Size(400, 400); + this.nonogramme1.TabIndex = 0; + this.nonogramme1.TabStop = false; + this.nonogramme1.Click += new System.EventHandler(this.nonogramme1_Click); + this.nonogramme1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.nonogramme1_MouseDown); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); diff --git a/NonoGramme/Form1.cs b/NonoGramme/Form1.cs index 2cabe13..a0e55ae 100644 --- a/NonoGramme/Form1.cs +++ b/NonoGramme/Form1.cs @@ -67,6 +67,7 @@ namespace NonoGramme if (Directory.Exists(selectedFolder)) { + lsbFiles.Items.Clear(); string[] nonFiles = Directory.GetFiles(selectedFolder, "*.non"); foreach (string file in nonFiles) @@ -86,5 +87,16 @@ namespace NonoGramme RefreshUi(); } } + + private void nonogramme1_Click(object sender, EventArgs e) + { + //The problem with click is that it is called 2 times because it detects mouse down AND ouse up + } + + private void nonogramme1_MouseDown(object sender, MouseEventArgs e) + { + nonogramme1.CursorClick(nonogramme1.PointToClient(MousePosition)); + RefreshUi(); + } } } diff --git a/NonoGramme/Grid.cs b/NonoGramme/Grid.cs index 6e5facd..4ef9d31 100644 --- a/NonoGramme/Grid.cs +++ b/NonoGramme/Grid.cs @@ -12,9 +12,10 @@ namespace NonoGramme { internal class Grid { - private readonly Color DEFAULT_FULL_COLOR = Color.Black; + private readonly Color DEFAULT_FULL_COLOR = Color.Red; private readonly Color DEFAULT_EMPTY_COLOR = Color.LightGray; - private readonly Color DEBUG_UNDISCOVERED_COLOR = Color.DarkOliveGreen; + private readonly Color DEFAULT_UNDISCOVERED_COLOR = Color.DarkOliveGreen; + private readonly Color DEFAULT_DISCOVERED_COLOR = Color.Black; private readonly Color DEFAULT_BORDER_COLOR = Color.Pink; private readonly Color DEFAULT_ERROR_COLOR = Color.Purple; @@ -22,21 +23,49 @@ namespace NonoGramme private Line[] _horizontalLines; private Line[] _verticalLines; + private Point _gridOffset; + private Size _drawedBlocSize; + public Size GridSize { get => _gridSize; set => _gridSize = value; } - internal Line[] HorizontalLines { get => _horizontalLines; set => _horizontalLines = value; } - internal Line[] VerticalLines { get => _verticalLines; set => _verticalLines = value; } + public Line[] HorizontalLines { get => _horizontalLines; set => _horizontalLines = value; } + public Line[] VerticalLines { get => _verticalLines; set => _verticalLines = value; } + public Point GridOffset + { + get { return _gridOffset; } + set { if (value.X >= 0 && value.Y >= 0) { _gridOffset = value; } else { _gridOffset = new Point(-1, -1); }} + } + + public Size DrawedBlocSize { get => _drawedBlocSize; set => _drawedBlocSize = value; } public Grid(Size size) { GridSize = size; HorizontalLines = new Line[size.Height]; VerticalLines = new Line[size.Width]; + GridOffset = new Point(-1,-1); Populate(); } public Grid(string filePath) { ImportFromFile(filePath); } + public void Click(Point position) + { + //MessageBox.Show(position.ToString()); + if (GridOffset != new Point(-1, -1)) + { + //The Grid has been Drawned and has a valid offset + if (position.X >= GridOffset.X && position.Y >= GridOffset.Y) + { + //The click is on the grid + Point relativePoint = new Point(position.X-GridOffset.X, position.Y-GridOffset.Y); + Point reducedIndex = new Point(relativePoint.X / DrawedBlocSize.Width,relativePoint.Y / DrawedBlocSize.Height); + //int flatIndex = position.Y * HorizontalLines.Count() + position.X; + HorizontalLines[reducedIndex.Y].Click(reducedIndex.X); + VerticalLines[reducedIndex.X].Click(reducedIndex.Y); + } + } + } public void ImportFromFile(string filePath) { //GridSize = size; @@ -91,15 +120,34 @@ namespace NonoGramme license = matches[0].Groups[1].Value; } + /* rx = new Regex(@"^height (?[0-9]+)\nwidth (?[0-9]+)", RegexOptions.Multiline | RegexOptions.IgnoreCase); matches = rx.Matches(fileContent); if (matches.Count > 0 && matches[0].Groups.Count > 0) { size =new Size(Convert.ToInt32(matches[0].Groups["width"].Value), Convert.ToInt32(matches[0].Groups["height"].Value)); } + */ - string TEST = ""; + int height = 0; + int width = 0; + rx = new Regex(@"^height (?[0-9]+)", RegexOptions.Multiline | RegexOptions.IgnoreCase); + matches = rx.Matches(fileContent); + if (matches.Count > 0 && matches[0].Groups.Count > 0) + { + height = Convert.ToInt32(matches[0].Groups[1].Value); + } + rx = new Regex(@"^width (?[0-9]+)", RegexOptions.Multiline | RegexOptions.IgnoreCase); + matches = rx.Matches(fileContent); + if (matches.Count > 0 && matches[0].Groups.Count > 0) + { + width = Convert.ToInt32(matches[0].Groups[1].Value); + } + + size = new Size(width,height); + + //string TEST = ""; rows = new List[size.Height]; rx = new Regex(@"^rows\n(?[0-9,]+\r?\n)+", RegexOptions.Multiline); matches = rx.Matches(fileContent); @@ -209,6 +257,9 @@ namespace NonoGramme Size actualSize = new Size(UI.Width - offset.X, UI.Height - offset.Y); Size blocSize = new Size(actualSize.Width/GridSize.Width,actualSize.Height/GridSize.Height); + DrawedBlocSize = blocSize; + GridOffset = offset; + using (Graphics g = Graphics.FromImage(UI)) { //Drawing the rows Text @@ -248,10 +299,10 @@ namespace NonoGramme g.FillRectangle(new SolidBrush(DEFAULT_EMPTY_COLOR), new Rectangle(position, blocSize)); break; case Tile.State.Discovered: - g.FillRectangle(new SolidBrush(DEFAULT_EMPTY_COLOR), new Rectangle(position, blocSize)); + g.FillRectangle(new SolidBrush(DEFAULT_DISCOVERED_COLOR), new Rectangle(position, blocSize)); break; case Tile.State.Undiscovered: - g.FillRectangle(new SolidBrush(DEBUG_UNDISCOVERED_COLOR), new Rectangle(position, blocSize)); + g.FillRectangle(new SolidBrush(DEFAULT_UNDISCOVERED_COLOR), new Rectangle(position, blocSize)); break; default: g.FillRectangle(new SolidBrush(DEFAULT_ERROR_COLOR), new Rectangle(position, blocSize)); diff --git a/NonoGramme/Line.cs b/NonoGramme/Line.cs index 6ac21fc..3fc1904 100644 --- a/NonoGramme/Line.cs +++ b/NonoGramme/Line.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows.Forms; namespace NonoGramme { @@ -66,6 +67,29 @@ namespace NonoGramme Data[index].CurrentState = state; RecalculateHints(); } + public void Click(int index) + { + Tile tile = Data[index]; + //MessageBox.Show("Tile pressed :"+tile.CurrentState); + switch (tile.CurrentState) + { + case Tile.State.Empty: + tile.CurrentState = Tile.State.Full; + break; + case Tile.State.Undiscovered: + tile.CurrentState = Tile.State.Discovered; + break; + case Tile.State.Full: + tile.CurrentState = Tile.State.Empty; + break; + case Tile.State.Discovered: + tile.CurrentState = Tile.State.Undiscovered; + break; + default: + //We dont do anything + break; + } + } public void RecalculateHints() { Hints = new List(); @@ -100,7 +124,7 @@ namespace NonoGramme } if (currentTile) { - //if the last tilke is full then we need to add it at the end + //if the last tile is full then we need to add it at the end Hints.Add(counter); } } diff --git a/NonoGramme/Nonogramme.cs b/NonoGramme/Nonogramme.cs index 9a4472c..0e91a6d 100644 --- a/NonoGramme/Nonogramme.cs +++ b/NonoGramme/Nonogramme.cs @@ -34,5 +34,9 @@ namespace NonoGramme Bitmap bmp = new Bitmap(Width, Height); this.Image = GameGrid.Draw(bmp, new Point((Width / 100) * DEFAULT_UI_RATIO, (Height / 100) * DEFAULT_UI_RATIO)); } + public void CursorClick(Point position) + { + GameGrid.Click(position); + } } }