Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Godot;
- using System;
- using Godot.Collections;
- using System.Runtime.InteropServices;
- public partial class Blah : Node
- {
- //Mouse stuff
- [DllImport("user32.dll")]
- public static extern bool SetCursorPos(int X, int Y);
- [System.Runtime.InteropServices.DllImport("user32.dll")]
- public static extern void mouse_event(int dwFlags, int dx, int
- dy, int cButtons, int dwExtraInfo);
- public const int LMBDown = 0x02;
- public const int LMBUp = 0x04;
- [Export] int pixelSize;
- [Export] Label instructions;
- Array<Vector2I> colorPos;
- [Export] Vector2I anchor;
- [Export] Vector2I size;
- [Export] Texture2D texture;
- Image img;
- [Export] Array<Godot.Color> cols;
- Vector2I currentCoord;
- bool isRunning = false;
- Vector2I imgSize;
- // Called when the node enters the scene tree for the first time.
- public override void _Ready()
- {
- currentCoord = Vector2I.Zero;
- img = texture.GetImage();
- imgSize = img.GetSize();
- anchor = Vector2I.Zero;
- size = Vector2I.Zero;
- colorPos = new Array<Vector2I>();
- }
- // Called every frame. 'delta' is the elapsed time since the previous frame.
- public override void _Process(double delta)
- {
- if (colorPos.Count < cols.Count)
- {
- string s = "Colors: " + colorPos.Count + "\n";
- foreach (Vector2I v in colorPos)
- {
- s += v.ToString()+"\n";
- }
- instructions.Text = s;
- }
- else if(anchor == Vector2I.Zero)
- {
- instructions.Text = "Set topleft";
- }
- else if(size == Vector2I.Zero)
- {
- instructions.Text = "Set bottomright";
- }
- else if(!isRunning)
- {
- instructions.Text = "Ready";
- }
- else
- {
- instructions.Text = "Print";
- }
- if (Input.IsActionJustPressed("start"))
- {
- if (colorPos.Count < cols.Count)
- {
- colorPos.Add(DisplayServer.MouseGetPosition());
- //colorPos.Add(curso)
- }
- else if (anchor == Vector2I.Zero)
- {
- anchor = DisplayServer.MouseGetPosition();
- }
- else if (size == Vector2I.Zero)
- {
- size = DisplayServer.MouseGetPosition() - anchor;
- }
- else
- {
- StartBlah();
- }
- }
- if (isRunning)
- {
- if (currentCoord.X > imgSize.X)
- {
- NextCoord();
- return;
- }
- if (currentCoord.Y > imgSize.Y)
- {
- NextCoord();
- return;
- }
- Color col = img.GetPixel(currentCoord.X, currentCoord.Y);
- int nearest = NearestColor(col);
- ClickAt(nearest);
- NextCoord();
- }
- }
- private void ClickAt(int color)
- {
- Vector2I colPos = colorPos[color];
- SetCursorPos(colPos.X, colPos.Y);
- mouse_event(LMBDown, colPos.X, colPos.Y, 0, 0);
- mouse_event(LMBUp, colPos.X, colPos.Y, 0, 0);
- Vector2I pixPos = currentCoord + anchor;
- SetCursorPos(pixPos.X, pixPos.Y);
- mouse_event(LMBDown, pixPos.X, pixPos.Y, 0, 0);
- mouse_event(LMBUp, pixPos.X, pixPos.Y, 0, 0);
- }
- private void NextCoord()
- {
- currentCoord.X += pixelSize;
- if (currentCoord.X > size.X)
- {
- currentCoord.X = 0;
- currentCoord.Y += pixelSize;
- if (currentCoord.Y > size.Y)
- {
- isRunning = false;
- }
- }
- }
- public void StartBlah()
- {
- if (isRunning)
- {
- isRunning = false;
- return;
- }
- currentCoord = Vector2I.Zero;
- isRunning = true;
- }
- private int NearestColor(Color pixelCol)
- {
- int indx = -1;
- float dist = float.MaxValue;
- Vector3 px = new Vector3(pixelCol.R, pixelCol.G, pixelCol.B);
- foreach (Color c in cols)
- {
- Vector3 comp = new Vector3(c.R, c.G, c.B);
- float sq = px.DistanceSquaredTo(comp);
- if (sq < dist)
- {
- dist = sq;
- indx = cols.IndexOf(c);
- }
- }
- return indx;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment