Author Topic: Mouse Pan script - hold a button and move your mouse to pan the view!  (Read 2011 times)

0 Members and 1 Guest are viewing this topic.

Offline evilc

  • 23
Mouse Pan script - hold a button and move your mouse to pan the view!
I wrote this script because I was unsatisfied with the view panning options in MCO. I guess this is probably a MC2 limitation, I dunno as I never played it.

I dislike edge-scrolling, and the cursor keys are awkwardly placed (plus redefines become a pain etc) - normally in RTS style games I like to be able to hold my middle mouse button and move the mouse to pan around the map.

So... I wrote an AutoHotkey script to simulate this behavior for games that do not feature it.

Here is the prototype. In order to configure it (eg you want to use a button other than middle mouse), you would need to edit the settings section. If the script proves popular, I may create it using a library I have that adds a GUI to choose settings etc.

It's pretty simple, and heavily commented though, so I reckon it will probably do as-is.
You will need to install AutoHotkey, then save the following code as a text file with the extension .ahk and double-click it.
Code: [Select]
/*
Mouse Pan prototype AHK Script by evilC - http://evilc.com
Adds "Hold-to-Pan" functionality to games that do not support it by emulating the arrow keys
ie hold a button and the mouse now controls the arrow keys, letting you pan around the map in RTS games.
Written for MechCommander 2 (Omnitech mod) - mileage may vary in other games.

Features:
* Mouse cursor stays in same place when you move
  So panning does not trigger edge scroll, and mouse cursor stays where you need it.
* Mouse cursor allowed to move when rotate button held.

*/
; User settings - configure these to your desires

; The key you press to engage pan mode.
pan_button := "MButton"

; Enable / disable rotate mode
disable_mouse_move_on_rotate := 1
; The key you press to engage rotate mode.
rotate_button := "RButton"

; See AHK Key list here: http://www.autohotkey.com/docs/KeyList.htm

move_threshold := 10 ; The amount you must move to use the mouse - may need to adjust for sensitivity
tick_time := 50 ; How often the pan updates (in ms). Lower = more accurate, Higher = less mouse move for more pan

; Keys that move around in the game.
; For example, you may wish to change to WSAD
key_up := "Up" ; Typical settings: W or Up
key_down := "Down" ; Typical settings: S or Down
key_left := "Left" ; Typical settings: A or Left
key_right := "Right" ; Typical settings: D or Right

; End of user settings

; vars to store state of movement keys
state_left := 0
state_right := 0
state_up := 0
state_down := 0

rotate_on := 0

; Declare Hotkeys
Hotkey, *%pan_button%, PanOn
Hotkey, *%pan_button% up, PanOff
if (disable_mouse_move_on_rotate){
; Watch rotate button, but do not stop game from seeing it (prefix with ~)
Hotkey, *~%rotate_button%, RotateOn
Hotkey, *~%rotate_button% up, RotateOff
}

; Stop execution and wait for hotkeys
Return

; Functions
; =========

; Called when button pressed
PanOn:
; Store position of mouse when button was held
MouseGetPos, held_x, held_y
; Start running DoPan repeatedly
SetTimer, DoPan, %tick_time%
Gosub DoPan
return

; Called when button released
PanOff:
; Stop calling DoPan
SetTimer, DoPan, Off
; Release any held keys
ReleaseLeftRight()
ReleaseUpDown()
return

; Called when we are in rotate mode
RotateOn:
rotate_on := 1
Return

; Called when we leave rotate mode
RotateOff:
rotate_on := 0
Return


; Perform the Pan
DoPan:
MouseGetPos, cur_x, cur_y
if (rotate_on){
held_x := cur_x
held_y := cur_y
} else {
delta_x := cur_x - held_x
delta_y := cur_y - held_y
if (abs(delta_x) > move_threshold){
if (delta_x > 0){
Send {%key_right% down}
state_right := 1
} else {
Send {%key_right% up}
state_right := 0
}

if (delta_x < 0){
Send {%key_left% down}
state_left := 1
} else {
Send {%key_left% up}
state_left := 0
}
} else {
ReleaseLeftRight()
}

if (abs(delta_y) > move_threshold){
if (delta_y > 0){
Send {%key_down% down}
state_down := 1
} else {
Send {%key_down% up}
state_down := 0
}

if (delta_y < 0){
Send {%key_up% down}
state_up := 1
} else {
Send {%key_up% up}
state_up := 0
}
} else {
ReleaseUpDown()
}
; Move mouse back to where it was when we held pan
MouseMove, held_x, held_y, 0
}
Return

; Releases left or right, if held
ReleaseLeftRight(){
Global state_left
Global state_right
Global key_left
Global key_right

if (state_left){
Send {%key_left% up}
state_left := 0
}
if (state_right){
Send {%key_right% up}
state_right := 0
}
}

; Releases up or down, if held
ReleaseUpDown(){
Global state_up
Global state_down
Global key_up
Global key_down

if (state_up){
Send {%key_up% up}
state_up := 0
}
if (state_down){
Send {%key_down% up}
state_down := 0
}
}

Usage Notes:
  • Note that in MCO, the pan speed is directly influenced by the zoom rate, so if you want to pan quicker, zoom out more!
  • When you hold the button to pan, the script will keep moving your mouse back to the same place as when you held the pan button. This is to keep the mouse pointer where you left it ;) If you rotate the view with the Right mouse button, this feature will be disabled (But when holding the right mouse, the cursor doesn't move much anyway)
  • As it stands, the code only really supports left-click/left-click mode. It could be updated to support l/r, but for me shift+arrows is so fast that I would need to write code to strobe it, so as to slow the rotation rate down. This would also require a deticated button for rotate mode. If you use l/r click, you would probably want to set disable_mouse_move_on_rotate to 0

enjoy!
« Last Edit: December 13, 2013, 04:20:26 pm by evilc »