fixed cinnamon+x11

This commit is contained in:
Radek Davidek 2025-12-21 18:41:44 +01:00
parent d39a36fc6f
commit 37f49c5f33
2 changed files with 31 additions and 147 deletions

View File

@ -43,23 +43,6 @@ public class PokemonGoAutomation {
loadButtonTemplates();
}
/**
* Zjistí insets displeje (offset zp ůsobený OS - panel, taskbar, apod.)
* Důležité pro Ubuntu s GNOME panelem na horní straně
*/
private Insets getScreenInsets() {
try {
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration());
System.out.println("Zjištěny OS insets: top=" + insets.top + ", left=" + insets.left +
", bottom=" + insets.bottom + ", right=" + insets.right);
return insets;
} catch (Exception e) {
System.out.println("Chyba při zjištění insets: " + e.getMessage());
return new Insets(0, 0, 0, 0);
}
}
/**
* Vrátí počet dosud transfernutých pokémonů
*/
@ -186,130 +169,6 @@ public class PokemonGoAutomation {
return (double) matchingPixels / totalPixels;
}
/**
* Detekuje pozice Pokémonů pomocí template matchingu s pok1.png
* Hledá v oblasti od vrchu obrazovky do 75% výšky
* Může najít 9 Pokémonů
*/
private List<Point> detectPokemonsByTemplateMatching2() {
List<Point> positions = new ArrayList<>();
System.out.println("\n=== Detekce Pokémonů pomocí template matchingu (pok1.png) ===");
if (pok1Template == null) {
System.out.println("⚠️ Template pok1.png není dostupný");
return positions;
}
BufferedImage screenshot = captureScreen(windowBounds);
int tWidth = pok1Template.getWidth();
int tHeight = pok1Template.getHeight();
System.out.println("Template velikost: " + tWidth + "x" + tHeight);
// Grid parametry - 3x3 (9 Pokémonů) - pevné pozice podle layoutu aplikace
int gridStartY = (int) (windowBounds.height * 0.15);
int gridEndY = (int) (windowBounds.height * 0.75);
int gridHeight = gridEndY - gridStartY;
int gridMargin = (int) (windowBounds.width * 0.05);
int gridStartX = gridMargin;
int gridWidth = windowBounds.width - (2 * gridMargin);
int columns = 3;
int rows = 3; // 3x3 grid = 9 Pokémonů
int columnWidth = gridWidth / columns;
int rowHeight = gridHeight / rows;
System.out.println("Hledám Pokémony v gridu 3x3 (9 pozic)");
// Pro každou buňku v gridu hledat template
for (int row = 0; row < rows; row++) {
for (int col = 0; col < columns; col++) {
int cellX = gridStartX + (col * columnWidth);
int cellY = gridStartY + (row * rowHeight);
double bestScore = 0.0;
int bestX = cellX + columnWidth / 2;
int bestY = cellY + rowHeight / 2;
// Skenovat buňku s jemností
for (int sy = cellY; sy < cellY + rowHeight - tHeight; sy += 3) {
for (int sx = cellX; sx < cellX + columnWidth - tWidth; sx += 3) {
double score = templateMatch(screenshot, sx, sy, pok1Template, 115); // Zvýšená tolerance
if (score > bestScore) {
bestScore = score;
bestX = sx + tWidth / 2;
bestY = sy + tHeight / 2;
}
}
}
// Pokud jsme našli match (skóre > 75%), považovat za Pokémona
if (bestScore > 0.98) {
int x = windowBounds.x + bestX;
int y = windowBounds.y + bestY;
positions.add(new Point(x, y));
System.out.println(" Detekován Pokémon na [" + row + "," + col + "] -> (" + x + ", " + y
+ ") skóre=" + String.format("%.1f", bestScore * 100) + "%");
}
}
}
System.out.println("Celkem detekováno: " + positions.size() + " Pokémonů");
System.out.println("=================================\n");
return positions;
}
/**
* Najde a vrátí pozici transferu tlačítka bez kliknutí
*/
private Point findTransferButtonPosition() {
System.out.println("Hledám tlačítko TRANSFER pomocí template matchingu (t1.png)...");
BufferedImage screenshot = captureScreen(windowBounds);
int tWidth = t1Template.getWidth();
int tHeight = t1Template.getHeight();
// Template je celý řádek - zkusíme matchovat od 75% do 95% (kde by měl být
// TRANSFER řádek)
int searchStartY = (int) (windowBounds.height * 0.75);
int searchEndY = (int) (windowBounds.height * 0.95);
double bestScore = 0.0;
int bestX = 0;
int bestY = 0;
// Skenovat Y-ové pozice s větším krokem - 15px místo 5px pro zrychlení
for (int y = searchStartY; y <= searchEndY - tHeight; y += 15) {
// Skenovat také X pozice pro nalezení středu shody
for (int x = 0; x <= windowBounds.width - tWidth; x += 30) {
double score = templateMatch(screenshot, x, y, t1Template, 90);
if (score > bestScore) {
bestScore = score;
bestX = x;
bestY = y;
}
}
}
if (bestScore > 0.92) {
Point bestMatch = new Point(
windowBounds.x + bestX + (tWidth / 2),
windowBounds.y + bestY + (tHeight / 2));
System.out.println("✅ Nalezeno TRANSFER tlačítko (template match) na: " + bestMatch + " (shoda: "
+ String.format("%.1f", bestScore * 100) + "%)");
return bestMatch;
}
System.out.println("Template nenalezen");
return null;
}
/**
* Najde a vrátí pozici potvrzovacího tlačítka bez kliknutí
*/
@ -430,7 +289,7 @@ public class PokemonGoAutomation {
// Kliknutí na horní panel okna (title bar) pro aktivaci
// Title bar je obvykle 30-40px vysoký v horní části okna
int centerX = windowBounds.x + windowBounds.width / 2;
int titleBarY = windowBounds.y + 15; // 15px od vrcholu okna = horní panel
int titleBarY = windowBounds.y - 15; // 15px od vrcholu okna = horní panel
System.out.println("Aktivuji okno kliknutím na title bar: (" + centerX + ", " + titleBarY + ")");
@ -684,8 +543,8 @@ public class PokemonGoAutomation {
* @return Absolutní pozice na obrazovce
*/
private Point getAbsolutePoint(int relativeX, int relativeY) {
int xCorrection = 17;
int yCorrection = 50;
int xCorrection = 0;
int yCorrection = 0;
int absoluteX = windowBounds.x + relativeX + xCorrection;
int absoluteY = windowBounds.y + relativeY + yCorrection;
return new Point(absoluteX, absoluteY);

View File

@ -79,6 +79,12 @@ public class WindowFinder {
int XFetchName(Pointer display, long window, PointerByReference window_name_return);
// XTranslateCoordinates - převede souřadnice z jednoho okna do jiného (root)
boolean XTranslateCoordinates(Pointer display, long src_w, long dest_w,
int src_x, int src_y,
IntByReference dest_x_return, IntByReference dest_y_return,
PointerByReference child_return);
Pointer XFree(Pointer ptr);
}
@ -213,7 +219,8 @@ public class WindowFinder {
}
/**
* Získá bounds okna (pozici a velikost)
* Získá bounds okna (pozici a velikost) - ABSOLUTNÍ pozici na obrazovce
* Používá XTranslateCoordinates na správný převod souřadnic z okna do root
*/
private static Rectangle getWindowBounds(Pointer display, Long window) {
try {
@ -225,10 +232,28 @@ public class WindowFinder {
return null;
}
System.out.println("WindowFinder: Window bounds - x:" + attrs.x + " y:" + attrs.y +
// Použít XTranslateCoordinates na převod souřadnic z okna do root
long rootWindow = X11.INSTANCE.XDefaultRootWindow(display);
IntByReference absX = new IntByReference();
IntByReference absY = new IntByReference();
PointerByReference child = new PointerByReference();
boolean translated = X11.INSTANCE.XTranslateCoordinates(display, window, rootWindow,
0, 0, absX, absY, child);
if (!translated) {
System.err.println("WindowFinder - Chyba při XTranslateCoordinates");
// Fallback - vrátit alespoň relativní pozici
return new Rectangle(attrs.x, attrs.y, attrs.width, attrs.height);
}
int absoluteX = absX.getValue();
int absoluteY = absY.getValue();
System.out.println("WindowFinder: Window bounds - ABSOLUTNÍ: x:" + absoluteX + " y:" + absoluteY +
" w:" + attrs.width + " h:" + attrs.height);
return new Rectangle(attrs.x, attrs.y, attrs.width, attrs.height);
return new Rectangle(absoluteX, absoluteY, attrs.width, attrs.height);
} catch (Exception e) {
System.err.println("WindowFinder - Chyba při získávání bounds: " + e.getMessage());