| 7746 | |
| 7747 | |
| 7748 | double Line::ScriptGetJoyState(JoyControls aJoy, int aJoystickID, Var *aOutputVar) |
| 7749 | // For buttons: Returns 0 if "up", non-zero if down. |
| 7750 | // For axes and other controls: Returns a number indicating that controls position or status. |
| 7751 | // If there was a problem determining the position/state, aOutputVar is made blank and zero is returned. |
| 7752 | // Also returns zero in cases where a non-numerical result is requested, such as the joystick name. |
| 7753 | // In those cases, caller should normally have provided a non-NULL aOutputVar for the result. |
| 7754 | { |
| 7755 | if (aOutputVar) // Set default in case of early return. |
| 7756 | aOutputVar->Assign(""); |
| 7757 | if (!aJoy) // Currently never called this way. |
| 7758 | return 0; |
| 7759 | |
| 7760 | bool aJoy_is_button = IS_JOYSTICK_BUTTON(aJoy); |
| 7761 | |
| 7762 | JOYCAPS jc; |
| 7763 | if (!aJoy_is_button && aJoy != JOYCTRL_POV) |
| 7764 | { |
| 7765 | // Get the joystick's range of motion so that we can report position as a percentage. |
| 7766 | if (joyGetDevCaps(aJoystickID, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR) |
| 7767 | ZeroMemory(&jc, sizeof(jc)); // Zero it on failure, for use of the zeroes later below. |
| 7768 | } |
| 7769 | |
| 7770 | // Fetch this struct's info only if needed: |
| 7771 | JOYINFOEX jie; |
| 7772 | if (aJoy != JOYCTRL_NAME && aJoy != JOYCTRL_BUTTONS && aJoy != JOYCTRL_AXES && aJoy != JOYCTRL_INFO) |
| 7773 | { |
| 7774 | jie.dwSize = sizeof(JOYINFOEX); |
| 7775 | jie.dwFlags = JOY_RETURNALL; |
| 7776 | if (joyGetPosEx(aJoystickID, &jie) != JOYERR_NOERROR) |
| 7777 | return 0; // And leave aOutputVar set to blank. |
| 7778 | if (aJoy_is_button) |
| 7779 | { |
| 7780 | bool is_down = ((jie.dwButtons >> (aJoy - JOYCTRL_1)) & (DWORD)0x01); |
| 7781 | if (aOutputVar) |
| 7782 | aOutputVar->Assign(is_down ? "D" : "U"); |
| 7783 | return is_down; |
| 7784 | } |
| 7785 | } |
| 7786 | |
| 7787 | // Otherwise: |
| 7788 | UINT range; |
| 7789 | char buf[128], *buf_ptr; |
| 7790 | double result_double; // Not initialized to help catch bugs. |
| 7791 | |
| 7792 | switch(aJoy) |
| 7793 | { |
| 7794 | case JOYCTRL_XPOS: |
| 7795 | range = (jc.wXmax > jc.wXmin) ? jc.wXmax - jc.wXmin : 0; |
| 7796 | result_double = range ? 100 * (double)jie.dwXpos / range : jie.dwXpos; |
| 7797 | break; |
| 7798 | case JOYCTRL_YPOS: |
| 7799 | range = (jc.wYmax > jc.wYmin) ? jc.wYmax - jc.wYmin : 0; |
| 7800 | result_double = range ? 100 * (double)jie.dwYpos / range : jie.dwYpos; |
| 7801 | break; |
| 7802 | case JOYCTRL_ZPOS: |
| 7803 | range = (jc.wZmax > jc.wZmin) ? jc.wZmax - jc.wZmin : 0; |
| 7804 | result_double = range ? 100 * (double)jie.dwZpos / range : jie.dwZpos; |
| 7805 | break; |