Skip to content

Commit 496a936

Browse files
committed
Test update to fix scaling
1 parent bc5795c commit 496a936

17 files changed

+224
-164
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ bin/
2424
# project
2525
bobrust.properties
2626
hs_err_pid*.log
27+
button_config.json

button_config.json

Lines changed: 0 additions & 77 deletions
This file was deleted.

circle_config.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/java/com/bobrust/gui/ApplicationWindow.java

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bobrust.gui;
22

3+
import com.bobrust.gui.comp.JResizeComponent;
34
import com.bobrust.gui.comp.JToolbarButton;
45
import com.bobrust.gui.dialog.*;
56
import com.bobrust.robot.ButtonConfiguration;
@@ -48,7 +49,6 @@ public class ApplicationWindow extends JDialog {
4849
private GraphicsConfiguration monitor;
4950
private final Rectangle canvasRect = new Rectangle(-1, -1);
5051
private final Rectangle imageRect = new Rectangle(-1, -1);
51-
private final Rectangle paletteRect = new Rectangle(-1, -1);
5252
private Image drawImage;
5353

5454
// State Toolbar Buttons
@@ -90,7 +90,7 @@ public void windowClosing(WindowEvent e) {
9090
setContentPane(panel);
9191

9292
// If we are running from an IDE then we want to add some debug stuff
93-
if (AppConstants.IS_IDE && AppConstants.DEBUG_AUTO_IMAGE ) {
93+
if (AppConstants.IS_IDE && AppConstants.DEBUG_AUTO_IMAGE) {
9494
canvasAreaButton.setEnabled(true);
9595
imageAreaButton.setEnabled(true);
9696
drawButton.setEnabled(true);
@@ -108,9 +108,6 @@ public void windowClosing(WindowEvent e) {
108108
LOGGER.error("Could not find image", e);
109109
}
110110
}
111-
112-
// Load button configuration
113-
loadButtonConfiguration();
114111
}
115112

116113
private JPanel createToolbar() {
@@ -146,8 +143,9 @@ private JPanel createToolbar() {
146143
return toolbarPanel;
147144
}
148145

149-
Point[] initialPositions;
150146
private void setupButtonRegions() {
147+
loadButtonConfiguration();
148+
151149
String[] steps = {
152150
"Select the Save Button",
153151
"Select the Brush Circle",
@@ -157,10 +155,20 @@ private void setupButtonRegions() {
157155
"Select the Opacity Bar End",
158156
"Select Color Preview"
159157
};
160-
158+
159+
// Create initial button config
161160
Coordinate[] coords = new Coordinate[steps.length];
161+
coords[0] = config.saveImage;
162+
coords[1] = config.brush_circle;
163+
coords[2] = config.size_1;
164+
coords[3] = config.size_32;
165+
coords[4] = config.opacity_0;
166+
coords[5] = config.opacity_1;
167+
coords[6] = config.colorPreview;
168+
162169
for (int i = 0; i < steps.length; i++) {
163-
RegionSelectionDialog.Region region = regionSelectionDialog.openArrowMarker(monitor, false, steps[i] + " or press ESC", initialPositions[i]);
170+
RegionSelectionDialog.Region region = regionSelectionDialog.openArrowMarker(
171+
monitor, false, steps[i] + " or press ESC", coords[i]);
164172
Rectangle rect = region.selection();
165173
coords[i] = new Coordinate(rect.x, rect.y);
166174

@@ -176,16 +184,25 @@ private void setupButtonRegions() {
176184
config.opacity_1 = coords[5];
177185
config.colorPreview = coords[6];
178186
config.focus = coords[6];
187+
selectPaletteRegion();
188+
179189
saveButtonConfiguration();
180190
loadButtonConfiguration();
181-
182-
selectPaletteRegion();
183191
}
184192

185193
private void selectPaletteRegion() {
186-
var region = regionSelectionDialog.openDialog(monitor, false, "Select palette region and press ESC", null, paletteRect);
194+
Rectangle paletteRect = new Rectangle(
195+
config.color_topLeft.x(),
196+
config.color_topLeft.y(),
197+
config.color_botRight.x() - config.color_topLeft.x(),
198+
config.color_botRight.y() - config.color_topLeft.y()
199+
);
200+
var region = regionSelectionDialog.openDialog(
201+
monitor, false, JResizeComponent.RenderType.DOTTED_4_16, "Select palette region and press ESC", null, paletteRect);
202+
var selection = region.selection();
187203
monitor = region.monitor();
188-
paletteRect.setBounds(region.selection());
204+
config.color_topLeft = ButtonConfiguration.Coordinate.from(selection.x, selection.y);
205+
config.color_botRight = ButtonConfiguration.Coordinate.from(selection.x + selection.width, selection.y + selection.height);
189206
imageAreaButton.setEnabled(true);
190207

191208
if (region.hasChanged()) {
@@ -196,8 +213,8 @@ private void selectPaletteRegion() {
196213
region.selection().width,
197214
region.selection().height
198215
);
199-
200-
BufferedImage screenshot = RustWindowUtil.captureScreenshot(monitor);
216+
217+
BufferedImage screenshot = RustWindowUtil.captureScreenshotWithScale(monitor);
201218
if (screenshot == null) {
202219
LOGGER.error("Could not capture screenshot");
203220
return;
@@ -266,7 +283,8 @@ private void importImage() {
266283
}
267284

268285
private void selectCanvasRegion() {
269-
var region = regionSelectionDialog.openDialog(null, true, "Select canvas region and press ESC", null, canvasRect);
286+
var region = regionSelectionDialog.openDialog(
287+
null, true, JResizeComponent.RenderType.BASIC, "Select canvas region and press ESC", null, canvasRect);
270288
monitor = region.monitor();
271289
canvasRect.setBounds(region.selection());
272290
imageAreaButton.setEnabled(true);
@@ -283,7 +301,8 @@ private void selectCanvasRegion() {
283301
}
284302

285303
private void selectImageRegion() {
286-
var region = regionSelectionDialog.openDialog(monitor, false, "Select image region and press ESC", drawImage, imageRect);
304+
var region = regionSelectionDialog.openDialog(
305+
monitor, false, JResizeComponent.RenderType.BASIC, "Select image region and press ESC", drawImage, imageRect);
287306
imageRect.setBounds(region.selection());
288307
drawButton.setEnabled(true);
289308

@@ -373,56 +392,47 @@ public Rectangle getImageRect() {
373392
return imageRect;
374393
}
375394

395+
private void saveButtonConfiguration() {
396+
try (FileWriter writer = new FileWriter(CONFIG_FILE_PATH)) {
397+
gson.toJson(config, writer);
398+
LOGGER.info("Saved button configuration to {}", CONFIG_FILE_PATH);
399+
} catch (IOException e) {
400+
LOGGER.error("Failed to save button configuration", e);
401+
}
402+
}
403+
376404
private void loadButtonConfiguration() {
377405
File configFile = new File(CONFIG_FILE_PATH);
378406
if (configFile.exists()) {
379407
try (FileReader reader = new FileReader(configFile)) {
380408
config.update(gson.fromJson(reader, ButtonConfiguration.class));
381409
LOGGER.info("Loaded button configuration from {}", CONFIG_FILE_PATH);
382-
383-
applyLoadedConfiguration();
384410
} catch (IOException e) {
385411
LOGGER.error("Failed to load button configuration", e);
386412
}
387413
} else {
388414
LOGGER.info("Configuration file not found, creating new one with default values.");
415+
config.update(new ButtonConfiguration()); // Clear internal configuration
389416
saveButtonConfiguration();
390417
}
391-
}
392-
393-
private void applyLoadedConfiguration() {
394-
if (config.color_topLeft != null && config.color_botRight != null) {
395-
paletteRect.setBounds(
396-
config.color_topLeft.x(),
397-
config.color_topLeft.y(),
398-
config.color_botRight.x() - config.color_topLeft.x(),
399-
config.color_botRight.y() - config.color_topLeft.y()
400-
);
401-
LOGGER.info("Applied paletteRect from loaded configuration: {}", paletteRect);
402-
}
403-
404-
// TODO: Use monitor by default
405-
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
406-
int screenWidth = screenSize.width;
407-
int screenHeight = screenSize.height;
408-
initialPositions = new Point[]{
409-
config.saveImage != null ? new Point(config.saveImage.x(), config.saveImage.y()) : new Point((int)(screenWidth * 0.2), screenHeight / 2),
410-
config.brush_circle != null ? new Point(config.brush_circle.x(), config.brush_circle.y()) : new Point((int)(screenWidth * 0.2), screenHeight / 2),
411-
config.size_1 != null ? new Point(config.size_1.x(), config.size_1.y()) : new Point((int)(screenWidth * 0.2), screenHeight / 2),
412-
config.size_32 != null ? new Point(config.size_32.x(), config.size_32.y()) : new Point((int)(screenWidth * 0.2), screenHeight / 2),
413-
config.opacity_0 != null ? new Point(config.opacity_0.x(), config.opacity_0.y()) : new Point((int)(screenWidth * 0.2), screenHeight / 2),
414-
config.opacity_1 != null ? new Point(config.opacity_1.x(), config.opacity_1.y()) : new Point((int)(screenWidth * 0.2), screenHeight / 2),
415-
config.focus != null ? new Point(config.colorPreview.x(), config.colorPreview.y()) : new Point((int)(screenWidth * 0.2), screenHeight / 2)
416-
};
417-
}
418-
419-
420-
private void saveButtonConfiguration() {
421-
try (FileWriter writer = new FileWriter(CONFIG_FILE_PATH)) {
422-
gson.toJson(config, writer);
423-
LOGGER.info("Saved button configuration to {}", CONFIG_FILE_PATH);
424-
} catch (IOException e) {
425-
LOGGER.error("Failed to save button configuration", e);
418+
419+
GraphicsConfiguration mon = monitor;
420+
if (mon == null) {
421+
mon = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
426422
}
423+
424+
// Set default configuration
425+
double deltaWidth = (double) mon.getBounds().width / 1920.0;
426+
double deltaHeight = (double) mon.getBounds().height / 1080.0;
427+
if (!config.saveImage.valid()) config.saveImage = new ButtonConfiguration.Coordinate((int) (deltaWidth * 251), (int) (deltaHeight * 66));
428+
if (!config.brush_circle.valid()) config.brush_circle = new ButtonConfiguration.Coordinate((int) (deltaWidth * 1690), (int) (deltaHeight * 232));
429+
if (!config.size_1.valid()) config.size_1 = new ButtonConfiguration.Coordinate((int) (deltaWidth * 1687), (int) (deltaHeight * 285));
430+
if (!config.size_32.valid()) config.size_32 = new ButtonConfiguration.Coordinate((int) (deltaWidth * 1832), (int) (deltaHeight * 283));
431+
if (!config.opacity_0.valid()) config.opacity_0 = new ButtonConfiguration.Coordinate((int) (deltaWidth * 1687), (int) (deltaHeight * 367));
432+
if (!config.opacity_1.valid()) config.opacity_1 = new ButtonConfiguration.Coordinate((int) (deltaWidth * 1832), (int) (deltaHeight * 368));
433+
if (!config.color_topLeft.valid()) config.color_topLeft = new ButtonConfiguration.Coordinate((int) (deltaWidth * 1571), (int) (deltaHeight * 469));
434+
if (!config.color_botRight.valid()) config.color_botRight = new ButtonConfiguration.Coordinate((int) (deltaWidth * 1885), (int) (deltaHeight * 902));
435+
if (!config.focus.valid()) config.focus = new ButtonConfiguration.Coordinate((int) (deltaWidth * 1446), (int) (deltaHeight * 975));
436+
if (!config.colorPreview.valid()) config.colorPreview = new ButtonConfiguration.Coordinate((int) (deltaWidth * 1446), (int) (deltaHeight * 975));
427437
}
428438
}

src/main/java/com/bobrust/gui/comp/JCoordinateMarker.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.bobrust.gui.comp;
22

3-
import com.bobrust.robot.ButtonConfiguration;
4-
53
import javax.swing.*;
64
import java.awt.*;
75
import java.awt.event.MouseEvent;
@@ -57,15 +55,28 @@ public void mousePressed(MouseEvent e) {
5755
}
5856
}
5957

60-
public ButtonConfiguration.Coordinate getCoordinate() {
58+
public Point getSelectedPoint() {
6159
Point point = getLocation();
6260
int x = point.x + HANDLE_SIZE / 2;
6361
int y = point.y + HANDLE_SIZE / 2;
64-
return new ButtonConfiguration.Coordinate(x, y);
62+
63+
// TODO: Don't make these values hardcoded
64+
x += 10;
65+
y += 50;
66+
67+
return new Point(x, y);
6568
}
6669

67-
public void setPosition(Point position) {
68-
setBounds(position.x - HANDLE_SIZE / 2, position.y - HANDLE_SIZE / 2, HANDLE_SIZE, HANDLE_SIZE);
70+
public void setSelectedPoint(Point position) {
71+
int x = position.x - HANDLE_SIZE / 2;
72+
int y = position.y - HANDLE_SIZE / 2;
73+
74+
// TODO: Don't make these values hardcoded
75+
x -= 10;
76+
y -= 50;
77+
78+
setBounds(x, y, HANDLE_SIZE, HANDLE_SIZE);
79+
revalidateSelection();
6980
repaint();
7081
}
7182

@@ -98,6 +109,10 @@ public void revalidateSelection() {
98109

99110
// Make sure we fit the parent element
100111
Rectangle parentBounds = getParent().getBounds();
112+
if (parentBounds.width == 0 || parentBounds.height == 0) {
113+
return;
114+
}
115+
101116
parentBounds.setLocation(0, 0);
102117

103118
Point location = new Point(getLocation());

0 commit comments

Comments
 (0)