Guest User

Untitled

a guest
Apr 1st, 2025
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. using Godot;
  2. using System;
  3. using Godot.Collections;
  4. using System.Runtime.InteropServices;
  5.  
  6. public partial class Blah : Node
  7. {
  8.     //Mouse stuff
  9.  
  10.     [DllImport("user32.dll")]
  11.     public static extern bool SetCursorPos(int X, int Y);
  12.     [System.Runtime.InteropServices.DllImport("user32.dll")]
  13.     public static extern void mouse_event(int dwFlags, int dx, int
  14.     dy, int cButtons, int dwExtraInfo);
  15.  
  16.     public const int LMBDown = 0x02;
  17.     public const int LMBUp = 0x04;
  18.  
  19.     [Export] int pixelSize;
  20.     [Export] Label instructions;
  21.     Array<Vector2I> colorPos;
  22.     [Export] Vector2I anchor;
  23.     [Export] Vector2I size;
  24.     [Export] Texture2D texture;
  25.     Image img;
  26.     [Export] Array<Godot.Color> cols;
  27.     Vector2I currentCoord;
  28.     bool isRunning = false;
  29.     Vector2I imgSize;
  30.  
  31.     // Called when the node enters the scene tree for the first time.
  32.     public override void _Ready()
  33.     {
  34.         currentCoord = Vector2I.Zero;
  35.         img = texture.GetImage();
  36.         imgSize = img.GetSize();
  37.         anchor = Vector2I.Zero;
  38.         size = Vector2I.Zero;
  39.         colorPos = new Array<Vector2I>();
  40.     }
  41.  
  42.     // Called every frame. 'delta' is the elapsed time since the previous frame.
  43.     public override void _Process(double delta)
  44.     {
  45.         if (colorPos.Count < cols.Count)
  46.         {
  47.             string s = "Colors: " + colorPos.Count + "\n";
  48.             foreach (Vector2I v in colorPos)
  49.             {
  50.                 s += v.ToString()+"\n";
  51.             }
  52.             instructions.Text = s;
  53.         }
  54.         else if(anchor ==  Vector2I.Zero)
  55.         {
  56.             instructions.Text = "Set topleft";
  57.         }
  58.         else if(size == Vector2I.Zero)
  59.         {
  60.             instructions.Text = "Set bottomright";
  61.         }
  62.         else if(!isRunning)
  63.         {
  64.             instructions.Text = "Ready";
  65.         }
  66.         else
  67.         {
  68.             instructions.Text = "Print";
  69.         }
  70.         if (Input.IsActionJustPressed("start"))
  71.         {
  72.             if (colorPos.Count < cols.Count)
  73.             {
  74.                 colorPos.Add(DisplayServer.MouseGetPosition());
  75.                 //colorPos.Add(curso)
  76.             }
  77.             else if (anchor == Vector2I.Zero)
  78.             {
  79.                 anchor = DisplayServer.MouseGetPosition();
  80.             }
  81.             else if (size == Vector2I.Zero)
  82.             {
  83.                 size = DisplayServer.MouseGetPosition() - anchor;
  84.             }
  85.             else
  86.             {
  87.                 StartBlah();
  88.             }
  89.         }
  90.         if (isRunning)
  91.         {
  92.             if (currentCoord.X > imgSize.X)
  93.             {
  94.                 NextCoord();
  95.                 return;
  96.             }
  97.             if (currentCoord.Y > imgSize.Y)
  98.             {
  99.                 NextCoord();
  100.                 return;
  101.             }
  102.             Color col = img.GetPixel(currentCoord.X, currentCoord.Y);
  103.             int nearest = NearestColor(col);
  104.  
  105.             ClickAt(nearest);
  106.  
  107.             NextCoord();
  108.         }
  109.     }
  110.     private void ClickAt(int color)
  111.     {
  112.         Vector2I colPos = colorPos[color];
  113.         SetCursorPos(colPos.X, colPos.Y);
  114.         mouse_event(LMBDown, colPos.X, colPos.Y, 0, 0);
  115.         mouse_event(LMBUp, colPos.X, colPos.Y, 0, 0);
  116.  
  117.         Vector2I pixPos = currentCoord + anchor;
  118.         SetCursorPos(pixPos.X, pixPos.Y);
  119.         mouse_event(LMBDown, pixPos.X, pixPos.Y, 0, 0);
  120.         mouse_event(LMBUp, pixPos.X, pixPos.Y, 0, 0);
  121.     }
  122.     private void NextCoord()
  123.     {
  124.         currentCoord.X += pixelSize;
  125.         if (currentCoord.X > size.X)
  126.         {
  127.             currentCoord.X = 0;
  128.             currentCoord.Y += pixelSize;
  129.             if (currentCoord.Y > size.Y)
  130.             {
  131.                 isRunning = false;
  132.             }
  133.         }
  134.     }
  135.     public void StartBlah()
  136.     {
  137.         if (isRunning)
  138.         {
  139.             isRunning = false;
  140.             return;
  141.         }
  142.         currentCoord = Vector2I.Zero;
  143.         isRunning = true;
  144.     }
  145.     private int NearestColor(Color pixelCol)
  146.     {
  147.         int indx = -1;
  148.         float dist = float.MaxValue;
  149.         Vector3 px = new Vector3(pixelCol.R, pixelCol.G, pixelCol.B);
  150.         foreach (Color c in cols)
  151.         {
  152.             Vector3 comp = new Vector3(c.R, c.G, c.B);
  153.             float sq = px.DistanceSquaredTo(comp);
  154.             if (sq < dist)
  155.             {
  156.                 dist = sq;
  157.                 indx = cols.IndexOf(c);
  158.             }
  159.         }
  160.         return indx;
  161.     }
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment