Attach - Add config property to set object orientation (#9623)

* added possibility to define orientation on attach items in config

* added documentation on attach framework

* whitespace

* changed to use roll and yaw parameters instead of vectordir for easier usage, and to support placement view with rolled objects. Changed vector calculations

* Update addons/attach/functions/fnc_attach.sqf

* Docs and comments

* fix derp

* Update docs/wiki/framework/attach-framework.md

* Update attach-framework.md

---------

Co-authored-by: Grim <69561145+LinkIsGrim@users.noreply.github.com>
This commit is contained in:
Crowdedlight 2023-11-11 01:04:22 +01:00 committed by GitHub
parent 0330532cd2
commit 548ab810df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 6 deletions

View File

@ -26,10 +26,12 @@ if ((_itemClassname == "") || {(!_silentScripted) && {!(_this call FUNC(canAttac
private _itemVehClass = getText (configFile >> "CfgWeapons" >> _itemClassname >> "ACE_Attachable");
private _onAttachText = getText (configFile >> "CfgWeapons" >> _itemClassname >> "displayName");
private _itemModelOrientation = getArray (configFile >> "CfgWeapons" >> _itemClassname >> QGVAR(orientation));
if (_itemVehClass == "") then {
_itemVehClass = getText (configFile >> "CfgMagazines" >> _itemClassname >> "ACE_Attachable");
_onAttachText = getText (configFile >> "CfgMagazines" >> _itemClassname >> "displayName");
_itemModelOrientation = getArray (configFile >> "CfgWeapons" >> _itemClassname >> QGVAR(orientation));
};
if (_itemVehClass == "") exitWith {ERROR("no ACE_Attachable for Item");};
@ -68,7 +70,7 @@ if (_unit == _attachToVehicle) then { //Self Attachment
[{
params ["_args","_idPFH"];
_args params ["_unit","_attachToVehicle","_itemClassname","_itemVehClass","_onAttachText","_actionID"];
_args params ["_unit","_attachToVehicle","_itemClassname","_itemVehClass","_onAttachText","_actionID", "_itemModelOrientation"];
private _virtualPosASL = (eyePos _unit) vectorAdd (positionCameraToWorld [0,0,0.6]) vectorDiff (positionCameraToWorld [0,0,0]);
if (cameraView == "EXTERNAL") then {
@ -95,7 +97,7 @@ if (_unit == _attachToVehicle) then { //Self Attachment
(QGVAR(virtualAmmo) call BIS_fnc_rscLayer) cutText ["", "PLAIN"];
if (GVAR(placeAction) == PLACE_APPROVE) then {
[_unit, _attachToVehicle, _itemClassname, _itemVehClass, _onAttachText, _virtualPos] call FUNC(placeApprove);
[_unit, _attachToVehicle, _itemClassname, _itemVehClass, _onAttachText, _virtualPos, _itemModelOrientation] call FUNC(placeApprove);
};
} else {
//Show the virtual object:
@ -112,9 +114,13 @@ if (_unit == _attachToVehicle) then { //Self Attachment
((uiNamespace getVariable [QGVAR(virtualAmmoDisplay), displayNull]) displayCtrl 800851) ctrlSetPosition _screenPos;
private _dir = (positionCameraToWorld [0,0,1]) vectorFromTo (positionCameraToWorld [0,0,0]);
private _angle = asin (_dir select 2);
private _up = [0, cos _angle, sin _angle];
((uiNamespace getVariable [QGVAR(virtualAmmoDisplay), displayNull]) displayCtrl 800851) ctrlSetModelDirAndUp [[1,0,0], _up];
// Tranform yaw/roll angle to vector, defaults are pre-#9623 behavior
_itemModelOrientation params [["_roll", 0], ["_yaw", 90]];
private _dirAndUp = [[[0,1,0], [0,0,1]], _yaw, 0, _roll] call BIS_fnc_transformVectorDirAndUp;
private _dirAndUp = [_dirAndUp, 0, _angle, 0] call BIS_fnc_transformVectorDirAndUp;
((uiNamespace getVariable [QGVAR(virtualAmmoDisplay), displayNull]) displayCtrl 800851) ctrlSetModelDirAndUp _dirAndUp;
};
};
}, 0, [_unit, _attachToVehicle, _itemClassname, _itemVehClass, _onAttachText, _actionID]] call CBA_fnc_addPerFrameHandler;
}, 0, [_unit, _attachToVehicle, _itemClassname, _itemVehClass, _onAttachText, _actionID, _itemModelOrientation]] call CBA_fnc_addPerFrameHandler;
};

View File

@ -15,6 +15,7 @@
* 3: Light Vehicle Classname <STRING>
* 4: On Attach Text <STRING>
* 5: Starting Pos of dummy item <ARRAY>
* 5: Orientation of model <ARRAY>
*
* Return Value:
* None
@ -25,7 +26,7 @@
* Public: No
*/
params ["_unit", "_attachToVehicle", "_itemClassname", "_itemVehClass", "_onAttachText", "_startingPosition"];
params ["_unit", "_attachToVehicle", "_itemClassname", "_itemVehClass", "_onAttachText", "_startingPosition", "_itemModelOrientation"];
TRACE_6("params",_unit,_attachToVehicle,_itemClassname,_itemVehClass,_onAttachText,_startingPosition);
private _startingOffset = _attachToVehicle worldToModel _startingPosition;
@ -93,6 +94,19 @@ _endPosTestOffset set [2, (_startingOffset select 2)];
private _attachedObject = _itemVehClass createVehicle (getPos _unit);
_attachedObject attachTo [_attachToVehicle, _endPosTestOffset];
// Get wanted orientation if any is set
_itemModelOrientation params [["_roll", 0], ["_yaw", 90]];
private _dirAndUp = [[[0,1,0],[0,0,1]], -_yaw, 0, _roll] call BIS_fnc_transformVectorDirAndUp;
// Transform dir and up vector from player model to world, then to model-space of _attachToVehicle
private _dir = _unit vectorModelToWorldVisual _dirAndUp#0;
_dir = _attachToVehicle vectorWorldToModelVisual _dir;
private _up = _unit vectorModelToWorldVisual _dirAndUp#1;
_up = _attachToVehicle vectorWorldToModelVisual _up;
_attachedObject setVectorDirAndUp [_dir, _up];
//Remove Item from inventory
_unit removeItem _itemClassname;

View File

@ -0,0 +1,59 @@
---
layout: wiki
title: Attach Framework
description: Explains how to add items to the ACE Attach Framework.
group: framework
order: 0
parent: wiki
mod: ace
version:
major: 3
minor: 17
patch: 0
---
## 1. Config Values
### 1.1 Make item attachable
An item can be added to the ACE Attach framework by adding the ``ACE_attachable`` property to a class in ``CfgWeapons`` or ``CfgMagazines``. The value must be the classname of a valid class in ``CfgVehicles``:
```cpp
class CfgWeapons {
class attach_item: CBA_MiscItem {
ACE_attachable = "new_attachable_item_classname";
};
};
class CfgVehicles {
class ThingX;
class new_attachable_item_classname: ThingX {
scope = 1; // Should be 1 (private) or 2 (public), scope 0 will cause errors on object creation
displayName = "New ACE attachable item";
model = "\path\to\my\model.p3d";
vehicleClass = "";
};
};
```
### 1.2 Define attach orientation for non-symmetric items
In the case the item needs to have a particular orientation when attached, add the config value: ``ace_attach_orientation`` which is an array describing the ``roll`` and ``yaw`` orientation of the object.
The default value is: ``[0,0]``.
Example:
```cpp
class CfgWeapons {
class attach_item: CBA_MiscItem {
ACE_attachable = "new_attachable_item_classname";
ace_attach_orientation[] = {0,180}; // 180deg yaw
};
};
```
## 2. Event Handlers
### 2.1 Listenable Events
| Event Key | Parameters | Locality | Description |
|----------|---------|---------|---------|---------|---------|
|`ace_attach_attached` | [_attachedObject, _itemClassname, _temporary] | Local | Called after an item is attached to an object. `_temporary` flag means the item is being re-attached (after a unit is exiting a vehicle, for example)
|`ace_attach_detaching` | [_attachedObject, _itemClassname, _temporary] | Local | Called just before an item is detached/removed from an object. `_temporary` flag means the item will be reattached later, see above.
### 2.2 Other events for attached objects
Use [CBA Extended Event Handlers](https://github.com/CBATeam/CBA_A3/wiki/Extended-Event-Handlers-(new)). Note that objects attached to units will be deleted/created upon entering/exiting vehicles and should be handled accordingly.