| 17356 | |
| 17357 | |
| 17358 | bool ScriptGetJoyState(JoyControls aJoy, int aJoystickID, ExprTokenType &aToken, LPTSTR aBuf) |
| 17359 | // Caller must ensure that aToken.marker is a buffer large enough to handle the longest thing put into |
| 17360 | // it here, which is currently jc.szPname (size=32). Caller has set aToken.symbol to be SYM_STRING. |
| 17361 | // aToken is used for the value being returned by GetKeyState() to the script, while this function's |
| 17362 | // bool return value is used only by KeyWait, so is false for "up" and true for "down". |
| 17363 | // If there was a problem determining the position/state, aToken is made blank and false is returned. |
| 17364 | { |
| 17365 | // Set default in case of early return. |
| 17366 | aToken.symbol = SYM_STRING; |
| 17367 | aToken.marker = aBuf; |
| 17368 | *aBuf = '\0'; // Blank vs. string "0" serves as an indication of failure. |
| 17369 | |
| 17370 | if (!aJoy) // Currently never called this way. |
| 17371 | return false; // And leave aToken set to blank. |
| 17372 | |
| 17373 | bool aJoy_is_button = IS_JOYSTICK_BUTTON(aJoy); |
| 17374 | |
| 17375 | JOYCAPS jc; |
| 17376 | if (!aJoy_is_button && aJoy != JOYCTRL_POV) |
| 17377 | { |
| 17378 | // Get the joystick's range of motion so that we can report position as a percentage. |
| 17379 | if (joyGetDevCaps(aJoystickID, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR) |
| 17380 | ZeroMemory(&jc, sizeof(jc)); // Zero it on failure, for use of the zeroes later below. |
| 17381 | } |
| 17382 | |
| 17383 | // Fetch this struct's info only if needed: |
| 17384 | JOYINFOEX jie; |
| 17385 | if (aJoy != JOYCTRL_NAME && aJoy != JOYCTRL_BUTTONS && aJoy != JOYCTRL_AXES && aJoy != JOYCTRL_INFO) |
| 17386 | { |
| 17387 | jie.dwSize = sizeof(JOYINFOEX); |
| 17388 | jie.dwFlags = JOY_RETURNALL; |
| 17389 | if (joyGetPosEx(aJoystickID, &jie) != JOYERR_NOERROR) |
| 17390 | return false; // And leave aToken set to blank. |
| 17391 | if (aJoy_is_button) |
| 17392 | { |
| 17393 | bool is_down = ((jie.dwButtons >> (aJoy - JOYCTRL_1)) & (DWORD)0x01); |
| 17394 | aToken.symbol = SYM_INTEGER; // Override default type. |
| 17395 | aToken.value_int64 = is_down; // Always 1 or 0, since it's "bool" (and if not for that, bitwise-and). |
| 17396 | return is_down; |
| 17397 | } |
| 17398 | } |
| 17399 | |
| 17400 | // Otherwise: |
| 17401 | UINT range; |
| 17402 | LPTSTR buf_ptr; |
| 17403 | double result_double; // Not initialized to help catch bugs. |
| 17404 | |
| 17405 | switch(aJoy) |
| 17406 | { |
| 17407 | case JOYCTRL_XPOS: |
| 17408 | range = (jc.wXmax > jc.wXmin) ? jc.wXmax - jc.wXmin : 0; |
| 17409 | result_double = range ? 100 * (double)jie.dwXpos / range : jie.dwXpos; |
| 17410 | break; |
| 17411 | case JOYCTRL_YPOS: |
| 17412 | range = (jc.wYmax > jc.wYmin) ? jc.wYmax - jc.wYmin : 0; |
| 17413 | result_double = range ? 100 * (double)jie.dwYpos / range : jie.dwYpos; |
| 17414 | break; |
| 17415 | case JOYCTRL_ZPOS: |