autoclick/cmd/main/main.go

124 lines
2.6 KiB
Go

package main
import (
"fmt"
"log"
"os"
"time"
"github.com/go-vgo/robotgo"
"gopkg.in/yaml.v3"
)
type Point struct {
X int
Y int
}
func (p Point) String() string {
return fmt.Sprintf("(%v, %v)", p.X, p.Y)
}
func (p Point) Click() {
robotgo.Move(p.X, p.Y)
robotgo.Click("left", false)
}
type PointGroup struct {
Name string
Points []Point
}
type SavedGroups struct {
Groups []PointGroup
}
func getSavedGroups(yamlFile string) *SavedGroups {
s := SavedGroups{}
inputFile, err := os.ReadFile(yamlFile)
if err == nil { // No yaml file found
err = yaml.Unmarshal(inputFile, &s)
if err != nil {
log.Fatalf("%v - Exiting", err)
}
}
return &s
}
func addSavedGroup(s *SavedGroups) {
g := PointGroup{}
var name string
var points int
for {
name = ""
fmt.Print("If you want to add a new group of locations, enter a name. Press enter to skip. ")
fmt.Scanln(&name)
if name == "" {
break
}
fmt.Printf("How many positions are there in group %v? ", name)
_, err := fmt.Scanln(&points)
if err != nil {
fmt.Println("I haven't understood, try again...")
var discard string
fmt.Scanln(&discard)
continue
}
for i := 0; i < points; i++ {
fmt.Printf("Please move your mouse into position %v of %v (group %v)\n", i+1, points, name)
time.Sleep(2 * time.Second)
x, y := robotgo.GetMousePos()
fmt.Printf("Added point (x=%v, y=%v) to %v\n", x, y, name)
p := Point{X: x, Y: y}
g.Points = append(g.Points, p)
}
g.Name = name
s.Groups = append(s.Groups, g)
}
}
func storeSavedGroups(s *SavedGroups, yamlFile string) {
d, err := yaml.Marshal(s)
if err != nil {
log.Fatalf("error: %v", err)
}
err = os.WriteFile(yamlFile, d, 0644)
if err != nil {
log.Fatalf("error: %v", err)
}
//fmt.Printf("--- d dump:\n%s\n\n", string(d))
}
func main() {
yamlFile := "SavedGroups.yaml"
s := getSavedGroups(yamlFile)
addSavedGroup(s)
storeSavedGroups(s, yamlFile)
for index, g := range s.Groups {
fmt.Printf("- Press %v to select %v\n", index+1, g.Name)
}
var choice int
for choice < 1 {
fmt.Print("Your selection: ")
_, err := fmt.Scanln(&choice)
if err != nil {
fmt.Println("I haven't understood, try again...")
var discard string
fmt.Scanln(&discard)
continue
}
}
selection := s.Groups[choice-1]
fmt.Printf("You selected %v. I'll be clicking in the following positions:\n", selection.Name)
for index, p := range selection.Points {
fmt.Printf("%v - %v\n", index+1, p)
}
for {
for _, p := range selection.Points {
p.Click()
time.Sleep(100 * time.Millisecond)
}
time.Sleep(500 * time.Millisecond)
}
}