using CDSAE3_Lian_Lian_Kan.Forms; using System; using System.Collections.Concurrent; 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; namespace CDSAE3_Lian_Lian_Kan.Forms { public partial class Single_Block : UserControl { public Single_Block() { InitializeComponent(); if (Etcs.gameForm == null) throw new Exception("game_form is null but try to make a new Single_Block"); Selected += Etcs.gameForm.Selected_Handler; } int imageID = -1; public Single_Block(int image, Color default_backColor, Color select_Color, (int, int) pos) { block_id = image; position = pos; imageID = image; InitializeComponent(); picture.SizeMode = PictureBoxSizeMode.Zoom; nor_color = default_backColor; sel_color = select_Color; BackColor = default_backColor; picture.BackColor = default_backColor; if (Etcs.gameForm == null) throw new Exception("game_form is null but try to make a new Single_Block"); Selected += Etcs.gameForm.Selected_Handler; } public Single_Block((int, int) pos) { position = pos; InitializeComponent(); picture.SizeMode = PictureBoxSizeMode.Zoom; BackColor = Color.FromArgb(0, 0, 0, 0); can_be_selected = false; if (Etcs.gameForm == null) throw new Exception("game_form is null but try to make a new Single_Block"); Selected += Etcs.gameForm.Selected_Handler; } int block_id; Color nor_color; Color sel_color; Color mouse_upper_color = Etcs.mouse_upper_color; 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; } Etcs.Direction direction = Etcs.Direction.none; public void hint_path(Etcs.Direction direction) { if (picture == null) { picture = new PictureBox { Dock = DockStyle.Fill }; Invoke(() => { Controls.Add(picture); picture.BringToFront(); }); } this.direction |= direction; Image_change(Etcs.get_tip_direction_Image(this.direction)); } public void to_path(Etcs.Direction direction) { if (picture == null) { picture = new PictureBox { Dock = DockStyle.Fill }; Invoke(() => { Controls.Add(picture); picture.BringToFront(); }); } Image_change(Etcs.get_direction_Image(direction)); direction = Etcs.Direction.none; } public void de_path() { Image_change(Etcs.trans_Image); direction = Etcs.Direction.none; } public void destroyAsync() { Image_change(Etcs.get_disappear_Images(block_id)); BackColor = Color.FromArgb(0, 0, 0, 0); can_be_selected = false; var timer = Etcs.hunderd_millsecond_timer; timer_Eplased = 0; timer.Elapsed += Image_Clear; } object locker = new object(); int timer_Eplased = 0; public void Image_Clear(object? sender, ElapsedEventArgs e) { if (timer_Eplased++ > 5) { Etcs.hunderd_millsecond_timer.Elapsed -= Image_Clear; try { if (picture == null) return; Invoke(() => { picture?.Dispose(); if (Controls.Contains(picture)) Controls.Remove(picture); }); picture = null; } catch { } } } ConcurrentQueue images_queue = new ConcurrentQueue(); Thread? Image_setting_thread; public void Image_change(Image new_image) { try { try { images_queue.Enqueue(new_image); } catch (Exception) { Console.WriteLine("Unusual Exception"); throw; } if (Image_setting_thread == null || !Image_setting_thread.IsAlive) { Image_setting_thread = new Thread(() => { while (images_queue.TryDequeue(out Image? image)) { try { lock (image) { Image_set(image); } } catch (Exception) { images_queue.Enqueue(image); } } }); Image_setting_thread.Start(); } } catch (Exception) { Console.WriteLine("unuasual 2"); throw; } } private void Image_set(Image image) { try { lock (locker) { if (picture == null) picture = new PictureBox { Dock = DockStyle.Fill }; picture!.Image = image; } } catch (Exception) { Image_change(image); } } public void Re_create(int image, Color? default_backColor, Color? select_Color, (int, int)? pos) { if (block_id == image) return; block_id = image; Image_change(Etcs.get_block_Image(image)); if (default_backColor != null) nor_color = (Color)default_backColor; if (select_Color != null) sel_color = (Color)select_Color; if (pos != null) position = ((int, int))pos; } private void picture_MouseEnter(object sender, EventArgs e) { if (!can_be_selected || selected) return; BackColor = mouse_upper_color; } private void picture_MouseLeave(object sender, EventArgs e) { if (!can_be_selected || selected) return; BackColor = nor_color; } private void Single_Block_Load(object sender, EventArgs e) { Image_change(imageID == -1 ? Properties.Resources.trans : Etcs.get_block_Image(imageID)); } 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); }