Skip to content

Commit

Permalink
Merge pull request #558 from go-vgo/bitmap-pr
Browse files Browse the repository at this point in the history
Add: add mouse of scale support and optimize the multi screen
  • Loading branch information
vcaesar authored Jan 23, 2023
2 parents b133208 + 23d3df9 commit 5e5c4dd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
4 changes: 2 additions & 2 deletions examples/scale/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func main() {
// robotgo.SaveBitmap(bitmap, "test.png")
robotgo.Save(robotgo.ToImage(bitmap), "test.png")

sx := robotgo.ScaleX()
s := robotgo.Scale()
sx := robotgo.ScaleX() // Deprecated
s := robotgo.Scale1() // Deprecated, use the ScaleF() function
robotx, roboty := 35*s/100, 25*s/100
fmt.Println("scale: ", sx, s, " pos: ", robotx, roboty)

Expand Down
22 changes: 9 additions & 13 deletions mouse/mouse_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void moveMouse(MMPointInt32 point){
#define MOUSE_COORD_TO_ABS(coord, width_or_height) ( \
((65536 * coord) / width_or_height) + (coord < 0 ? -1 : 1))

MMRectInt32 rect = getScreenRect(-1);
MMRectInt32 rect = getScreenRect(1);
int32_t x = MOUSE_COORD_TO_ABS(point.x - rect.origin.x, rect.size.w);
int32_t y = MOUSE_COORD_TO_ABS(point.y - rect.origin.y, rect.size.h);

Expand Down Expand Up @@ -239,12 +239,8 @@ void scrollMouseXY(int x, int y) {
int xdir = 6;
Display *display = XGetMainDisplay();

if (y < 0) {
ydir = 5;
}
if (x < 0) {
xdir = 7;
}
if (y < 0) { ydir = 5; }
if (x < 0) { xdir = 7; }

int xi; int yi;
for (xi = 0; xi < abs(x); xi++) {
Expand Down Expand Up @@ -299,7 +295,7 @@ static double crude_hypot(double x, double y){

bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed){
MMPointInt32 pos = location();
MMSizeInt32 screenSize = getMainDisplaySize();
// MMSizeInt32 screenSize = getMainDisplaySize();
double velo_x = 0.0, velo_y = 0.0;
double distance;

Expand All @@ -315,13 +311,13 @@ bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed)
velo_x /= veloDistance;
velo_y /= veloDistance;

pos.x += floor(velo_x + 0.5);
pos.y += floor(velo_y + 0.5);
pos.x += floor(velo_x + 0.8);
pos.y += floor(velo_y + 0.8);

/* Make sure we are in the screen boundaries! (Strange things will happen if we are not.) */
if (pos.x >= screenSize.w || pos.y >= screenSize.h) {
return false;
}
// if (pos.x >= screenSize.w || pos.y >= screenSize.h) {
// return false;
// }
moveMouse(pos);

/* Wait 1 - 3 milliseconds. */
Expand Down
29 changes: 22 additions & 7 deletions robotgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ var (

// NotPid used the hwnd not pid in windows
NotPid bool
// Scale option the os screen scale
Scale bool
)

type (
Expand Down Expand Up @@ -253,7 +255,7 @@ func SysScale(displayId ...int) float64 {
return float64(s)
}

// Scaled get the screen scaled size
// Scaled get the screen scaled return scale size
func Scaled(x int, displayId ...int) int {
f := ScaleF(displayId...)
return Scaled0(x, f)
Expand Down Expand Up @@ -282,7 +284,8 @@ func GetScreenRect(displayId ...int) Rect {
int(rect.size.w), int(rect.size.h)

if runtime.GOOS == "windows" {
f := ScaleF(displayId...)
// f := ScaleF(displayId...)
f := ScaleF()
x, y, w, h = Scaled0(x, f), Scaled0(y, f), Scaled0(w, f), Scaled0(h, f)
}
return Rect{
Expand Down Expand Up @@ -474,17 +477,24 @@ func CheckMouse(btn string) C.MMMouseButton {
return C.LEFT_BUTTON
}

// MoveScale calculate the os scale factor x, y
func MoveScale(x, y int, displayId ...int) (int, int) {
if Scale && runtime.GOOS == "windows" {
f := ScaleF()
x, y = Scaled0(x, f), Scaled0(y, f)
}

return x, y
}

// Move move the mouse to (x, y)
//
// Examples:
//
// robotgo.MouseSleep = 100 // 100 millisecond
// robotgo.Move(10, 10)
func Move(x, y int) {
// if runtime.GOOS == "windows" {
// f := ScaleF()
// x, y = Scaled0(x, f), Scaled0(y, f)
// }
func Move(x, y int, displayId ...int) {
x, y = MoveScale(x, y, displayId...)

cx := C.int32_t(x)
cy := C.int32_t(y)
Expand All @@ -498,6 +508,8 @@ func Move(x, y int) {
// Drag drag the mouse to (x, y),
// It's not valid now, use the DragSmooth()
func Drag(x, y int, args ...string) {
x, y = MoveScale(x, y)

var button C.MMMouseButton = C.LEFT_BUTTON
cx := C.int32_t(x)
cy := C.int32_t(y)
Expand All @@ -516,6 +528,8 @@ func Drag(x, y int, args ...string) {
//
// robotgo.DragSmooth(10, 10)
func DragSmooth(x, y int, args ...interface{}) {
x, y = MoveScale(x, y)

Toggle("left")
MilliSleep(50)
MoveSmooth(x, y, args...)
Expand All @@ -536,6 +550,7 @@ func MoveSmooth(x, y int, args ...interface{}) bool {
// f := ScaleF()
// x, y = Scaled0(x, f), Scaled0(y, f)
// }
x, y = MoveScale(x, y)

cx := C.int32_t(x)
cy := C.int32_t(y)
Expand Down
6 changes: 3 additions & 3 deletions robotgo_fn_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func TypeStringDelayed(str string, delay int) {

// Deprecated: use the ScaledF(),
//
// Scale get the screen scale (only windows old), drop
func Scale() int {
// Scale1 get the screen scale (only windows old), drop
func Scale1() int {
dpi := map[int]int{
0: 100,
// DPI Scaling Level
Expand Down Expand Up @@ -90,6 +90,6 @@ func Scale0() int {
//
// Mul mul the scale, drop
func Mul(x int) int {
s := Scale()
s := Scale1()
return x * s / 100
}
17 changes: 9 additions & 8 deletions screen/screen_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,20 @@ MMRectInt32 getScreenRect(int32_t display_id) {
(int32_t)DisplayWidth(display, screen),
(int32_t)DisplayHeight(display, screen));
#elif defined(IS_WINDOWS)
// if (GetSystemMetrics(SM_CMONITORS) == 1) {
if (GetSystemMetrics(SM_CMONITORS) == 1
|| display_id == -1 || display_id == 0) {
return MMRectInt32Make(
(int32_t)0,
(int32_t)0,
(int32_t)GetSystemMetrics(SM_CXSCREEN),
(int32_t)GetSystemMetrics(SM_CYSCREEN));
// } else {
// return MMRectInt32Make(
// (int32_t)GetSystemMetrics(SM_XVIRTUALSCREEN),
// (int32_t)GetSystemMetrics(SM_YVIRTUALSCREEN),
// (int32_t)GetSystemMetrics(SM_CXVIRTUALSCREEN),
// (int32_t)GetSystemMetrics(SM_CYVIRTUALSCREEN));
// }
} else {
return MMRectInt32Make(
(int32_t)GetSystemMetrics(SM_XVIRTUALSCREEN),
(int32_t)GetSystemMetrics(SM_YVIRTUALSCREEN),
(int32_t)GetSystemMetrics(SM_CXVIRTUALSCREEN),
(int32_t)GetSystemMetrics(SM_CYVIRTUALSCREEN));
}
#endif
}

Expand Down

0 comments on commit 5e5c4dd

Please sign in to comment.