2024-03-22 17:03:01 +08:00
|
|
|
|
using CDSAE3_Lian_Lian_Kan.Forms;
|
|
|
|
|
using System;
|
2024-04-02 23:14:34 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
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.Timers;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
2024-04-18 08:38:59 +08:00
|
|
|
|
namespace CDSAE3_Lian_Lian_Kan.Forms
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
|
|
|
|
public partial class Single_Block : UserControl
|
|
|
|
|
{
|
|
|
|
|
public Single_Block()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2024-04-18 08:38:59 +08:00
|
|
|
|
if (Etcs.gameForm == null)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
throw new Exception("game_form is null but try to make a new Single_Block");
|
2024-04-18 08:38:59 +08:00
|
|
|
|
Selected += Etcs.gameForm.Selected_Handler;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
}
|
2024-04-19 10:00:37 +08:00
|
|
|
|
int imageID = -1;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
public Single_Block(int image, Color default_backColor, Color select_Color, (int, int) pos)
|
|
|
|
|
{
|
|
|
|
|
block_id = image;
|
|
|
|
|
position = pos;
|
2024-04-19 10:00:37 +08:00
|
|
|
|
imageID = image;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
picture.SizeMode = PictureBoxSizeMode.Zoom;
|
|
|
|
|
nor_color = default_backColor;
|
|
|
|
|
sel_color = select_Color;
|
|
|
|
|
BackColor = default_backColor;
|
|
|
|
|
picture.BackColor = default_backColor;
|
2024-04-18 08:38:59 +08:00
|
|
|
|
if (Etcs.gameForm == null)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
throw new Exception("game_form is null but try to make a new Single_Block");
|
2024-04-18 08:38:59 +08:00
|
|
|
|
Selected += Etcs.gameForm.Selected_Handler;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
|
|
|
|
|
}
|
2024-04-19 10:00:37 +08:00
|
|
|
|
public Single_Block((int, int) pos)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
|
|
|
|
position = pos;
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
picture.SizeMode = PictureBoxSizeMode.Zoom;
|
|
|
|
|
BackColor = Color.FromArgb(0, 0, 0, 0);
|
|
|
|
|
can_be_selected = false;
|
2024-04-18 08:38:59 +08:00
|
|
|
|
if (Etcs.gameForm == null)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
throw new Exception("game_form is null but try to make a new Single_Block");
|
2024-04-18 08:38:59 +08:00
|
|
|
|
Selected += Etcs.gameForm.Selected_Handler;
|
2024-04-02 21:57:04 +08:00
|
|
|
|
|
|
|
|
|
|
2024-03-22 17:03:01 +08:00
|
|
|
|
}
|
|
|
|
|
int block_id;
|
|
|
|
|
Color nor_color;
|
|
|
|
|
Color sel_color;
|
2024-04-02 21:57:04 +08:00
|
|
|
|
Color mouse_upper_color = Etcs.mouse_upper_color;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
public bool selected { get; set; } = false;
|
|
|
|
|
bool can_be_selected = true;
|
|
|
|
|
public (int, int) position { get; set; }//height,width
|
|
|
|
|
private void picture_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!can_be_selected)
|
|
|
|
|
return;
|
|
|
|
|
if (selected)
|
|
|
|
|
{
|
|
|
|
|
deselect();
|
|
|
|
|
Selected.Invoke(this, new SelectedEventArgs(position, false));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
select();
|
|
|
|
|
Selected.Invoke(this, new SelectedEventArgs(position, true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void select()
|
|
|
|
|
{
|
|
|
|
|
selected = true;
|
|
|
|
|
BackColor = sel_color;
|
|
|
|
|
}
|
|
|
|
|
public void deselect()
|
|
|
|
|
{
|
|
|
|
|
selected = false;
|
|
|
|
|
BackColor = nor_color;
|
|
|
|
|
}
|
2024-04-02 21:57:04 +08:00
|
|
|
|
Etcs.Direction direction = Etcs.Direction.none;
|
|
|
|
|
public void hint_path(Etcs.Direction direction)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
{
|
2024-04-19 10:00:37 +08:00
|
|
|
|
if (picture == null)
|
|
|
|
|
{
|
|
|
|
|
picture = new PictureBox { Dock = DockStyle.Fill };
|
|
|
|
|
Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
Controls.Add(picture);
|
|
|
|
|
picture.BringToFront();
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-03-29 14:18:38 +08:00
|
|
|
|
this.direction |= direction;
|
2024-04-02 21:57:04 +08:00
|
|
|
|
Image_change(Etcs.get_tip_direction_Image(this.direction));
|
2024-03-29 14:18:38 +08:00
|
|
|
|
}
|
2024-04-02 21:57:04 +08:00
|
|
|
|
public void to_path(Etcs.Direction direction)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
{
|
2024-04-19 10:00:37 +08:00
|
|
|
|
if (picture == null)
|
|
|
|
|
{
|
|
|
|
|
picture = new PictureBox { Dock = DockStyle.Fill };
|
|
|
|
|
Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
Controls.Add(picture);
|
|
|
|
|
picture.BringToFront();
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-04-02 21:57:04 +08:00
|
|
|
|
Image_change(Etcs.get_direction_Image(direction));
|
|
|
|
|
direction = Etcs.Direction.none;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
}
|
|
|
|
|
public void de_path()
|
|
|
|
|
{
|
2024-04-02 21:57:04 +08:00
|
|
|
|
Image_change(Etcs.trans_Image);
|
|
|
|
|
direction = Etcs.Direction.none;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
}
|
|
|
|
|
public void destroyAsync()
|
|
|
|
|
{
|
2024-04-02 21:57:04 +08:00
|
|
|
|
Image_change(Etcs.get_disappear_Images(block_id));
|
2024-03-22 17:03:01 +08:00
|
|
|
|
BackColor = Color.FromArgb(0, 0, 0, 0);
|
|
|
|
|
can_be_selected = false;
|
2024-04-08 21:04:47 +08:00
|
|
|
|
var timer = Etcs.hunderd_millsecond_timer;
|
|
|
|
|
timer_Eplased = 0;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
timer.Elapsed += Image_Clear;
|
|
|
|
|
}
|
2024-03-29 14:18:38 +08:00
|
|
|
|
object locker = new object();
|
2024-04-08 21:04:47 +08:00
|
|
|
|
int timer_Eplased = 0;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
public void Image_Clear(object? sender, ElapsedEventArgs e)
|
|
|
|
|
{
|
2024-04-19 10:00:37 +08:00
|
|
|
|
if (timer_Eplased++ > 5)
|
2024-04-08 21:04:47 +08:00
|
|
|
|
{
|
2024-04-19 10:00:37 +08:00
|
|
|
|
Etcs.hunderd_millsecond_timer.Elapsed -= Image_Clear;
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-04-22 13:48:40 +08:00
|
|
|
|
if (picture == null)
|
|
|
|
|
return;
|
2024-04-19 10:00:37 +08:00
|
|
|
|
Invoke(() =>
|
|
|
|
|
{
|
2024-04-22 13:48:40 +08:00
|
|
|
|
picture?.Dispose();
|
|
|
|
|
if (Controls.Contains(picture))
|
|
|
|
|
Controls.Remove(picture);
|
2024-04-19 10:00:37 +08:00
|
|
|
|
});
|
2024-04-22 13:48:40 +08:00
|
|
|
|
picture = null;
|
2024-04-19 10:00:37 +08:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{ }
|
2024-04-08 21:04:47 +08:00
|
|
|
|
}
|
2024-03-22 17:03:01 +08:00
|
|
|
|
}
|
2024-04-19 10:00:37 +08:00
|
|
|
|
ConcurrentQueue<Image> images_queue = new ConcurrentQueue<Image>();
|
2024-04-02 23:14:34 +08:00
|
|
|
|
Thread? Image_setting_thread;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
public void Image_change(Image new_image)
|
2024-04-02 21:57:04 +08:00
|
|
|
|
{
|
2024-04-19 10:00:37 +08:00
|
|
|
|
try
|
2024-04-02 23:14:34 +08:00
|
|
|
|
{
|
2024-04-19 10:00:37 +08:00
|
|
|
|
try
|
|
|
|
|
{ images_queue.Enqueue(new_image); }
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Unusual Exception");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
if (Image_setting_thread == null || !Image_setting_thread.IsAlive)
|
2024-04-02 23:14:34 +08:00
|
|
|
|
{
|
2024-04-19 10:00:37 +08:00
|
|
|
|
Image_setting_thread = new Thread(() =>
|
2024-04-02 23:14:34 +08:00
|
|
|
|
{
|
2024-04-19 10:00:37 +08:00
|
|
|
|
while (images_queue.TryDequeue(out Image? image))
|
2024-04-08 21:04:47 +08:00
|
|
|
|
{
|
2024-04-19 10:00:37 +08:00
|
|
|
|
try
|
2024-04-08 21:04:47 +08:00
|
|
|
|
{
|
2024-04-19 10:00:37 +08:00
|
|
|
|
lock (image)
|
|
|
|
|
{
|
|
|
|
|
Image_set(image);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
images_queue.Enqueue(image);
|
2024-04-08 21:04:47 +08:00
|
|
|
|
}
|
2024-04-18 08:38:59 +08:00
|
|
|
|
|
2024-04-19 10:00:37 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Image_setting_thread.Start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("unuasual 2");
|
|
|
|
|
throw;
|
2024-04-02 23:14:34 +08:00
|
|
|
|
}
|
2024-04-02 21:57:04 +08:00
|
|
|
|
}
|
|
|
|
|
private void Image_set(Image image)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
2024-04-19 10:00:37 +08:00
|
|
|
|
if (picture == null)
|
|
|
|
|
picture = new PictureBox { Dock = DockStyle.Fill };
|
|
|
|
|
picture!.Image = image;
|
2024-03-22 17:03:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-02 21:57:04 +08:00
|
|
|
|
catch (Exception)
|
2024-03-22 17:03:01 +08:00
|
|
|
|
{
|
2024-04-02 21:57:04 +08:00
|
|
|
|
Image_change(image);
|
2024-03-22 17:03:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-29 14:18:38 +08:00
|
|
|
|
public void Re_create(int image, Color? default_backColor, Color? select_Color, (int, int)? pos)
|
|
|
|
|
{
|
|
|
|
|
if (block_id == image)
|
|
|
|
|
return;
|
|
|
|
|
block_id = image;
|
2024-04-02 21:57:04 +08:00
|
|
|
|
Image_change(Etcs.get_block_Image(image));
|
2024-03-29 14:18:38 +08:00
|
|
|
|
if (default_backColor != null)
|
|
|
|
|
nor_color = (Color)default_backColor;
|
2024-04-02 21:57:04 +08:00
|
|
|
|
if (select_Color != null)
|
2024-03-29 14:18:38 +08:00
|
|
|
|
sel_color = (Color)select_Color;
|
|
|
|
|
if (pos != null)
|
|
|
|
|
position = ((int, int))pos;
|
|
|
|
|
}
|
2024-04-02 21:57:04 +08:00
|
|
|
|
|
|
|
|
|
private void picture_MouseEnter(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-04-19 10:00:37 +08:00
|
|
|
|
if (!can_be_selected || selected)
|
2024-04-02 21:57:04 +08:00
|
|
|
|
return;
|
|
|
|
|
BackColor = mouse_upper_color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void picture_MouseLeave(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!can_be_selected || selected)
|
|
|
|
|
return;
|
|
|
|
|
BackColor = nor_color;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 10:00:37 +08:00
|
|
|
|
private void Single_Block_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Image_change(imageID == -1 ? Properties.Resources.trans : Etcs.get_block_Image(imageID));
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-22 17:03:01 +08:00
|
|
|
|
public event SelectedEventHandler Selected;
|
|
|
|
|
}
|
|
|
|
|
public class SelectedEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
public SelectedEventArgs((int, int) pos, bool sel)//width,height sel for be_selected
|
|
|
|
|
{
|
|
|
|
|
position = pos;
|
|
|
|
|
be_selected = sel;
|
|
|
|
|
}
|
|
|
|
|
public (int, int) position { get; set; }//height,width
|
|
|
|
|
public bool be_selected { get; set; }
|
|
|
|
|
}
|
|
|
|
|
public delegate void SelectedEventHandler(Single_Block sender, SelectedEventArgs e);
|
|
|
|
|
}
|