Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve attach objects module #752

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions addons/attached_objects/XEH_PREP.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
PREP(attach);
PREP(attachBone);
PREP(attachToSelection);
PREP(detach);
PREP(handleObjectEdited);
PREP(module);
83 changes: 83 additions & 0 deletions addons/attached_objects/functions/fnc_attachBone.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "script_component.hpp"
/*
* Author: Ampersand
* Attaches the given objects to a parent object selected by Zeus.
*
* Arguments:
* 0: Objects <ARRAY>
* 1: Is Bone <BOOLEAN>
*
* Return Value:
* None
*
* Example:
* [_object] call zen_attached_objects_fnc_attachBone
*
* Public: No
*/

[_this, {
params ["_successful", "_objects"];

if (!_successful) exitWith {};

curatorMouseOver params ["_type", "_entity"];
if (_type != "OBJECT") exitWith {};

// Prevent attaching object to itself
private _index = _objects find _entity;

if (_index != -1 && {count _objects == 1}) exitWith {
[LSTRING(CannotAttachToSelf)] call EFUNC(common,showMessage);
};

_objects deleteAt _index;

if (isNil QGVAR(modelSelections)) then {
GVAR(modelSelections) = createHashMap;
};

private _selections = GVAR(modelSelections) getOrDefaultCall [getText (configOf _entity >> "model"), {
[""] + (_entity selectionNames LOD_MEMORY select {
_entity selectionVectorDirAndUp [_x, LOD_MEMORY] isNotEqualTo [[0,0,0], [0,0,0]]
})
}, true];

[LSTRING(AttachTo), [
[
"COMBO",
"Selection",
[_selections, _selections, _selections find _selection]
],
[
"TOOLBOX",
["Orientation", "Selection: match selection orientation (i.e. along gun barrel). Relative: maintain current orientation relative to selection."],
[true, 1, 2, ["Selection", "Relative"]],
true
]
], {
params ["_values", "_args"];
_values params ["_selection", "_isRelative"];
_args params ["_objects", "_entity"];

{
if (_selection == "") then {
private _dirAndUp = [_entity vectorWorldToModel vectorDir _x, _entity vectorWorldToModel vectorUp _x];
_x attachTo [_entity];
[QEGVAR(common,setVectorDirAndUp), [_x, _dirAndUp], _x] call CBA_fnc_targetEvent;
} else {
[_x, _entity, _selection, _isRelative] call FUNC(attachToSelection);
};
} forEach _objects params ["_selectionPos", "_selectionY", "_selectionZ"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually the intention ?

^ Since _objects will be iterated before params gets to it, and params returns bool based on the success of, or use of a default value in, the creation of the mentioned vars, so even putting it as forEach (...) would give an error, IIUC, and the mentioned vars are only used after the forEach.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah you're right. forEach does return the last iteration, but it's not a good use for it, and it was in the wrong place relative to the if anyway.


private _hintPos = _entity modelToWorldVisual _selectionPos;
[[
["ICON", [_hintPos, "\a3\ui_f\data\IGUI\Cfg\Targeting\LaserTarget_ca.paa"]],
["LINE", [_hintPos, _entity vectorModelToWorldVisual _selectionY vectorMultiply 2 vectorAdd _hintPos, [0, 1, 0, 1]]],
["LINE", [_hintPos, _entity vectorModelToWorldVisual _selectionZ vectorAdd _hintPos, [0, 0, 1, 1]]]
], 3, _entity] call EFUNC(common,drawHint);

[LSTRING(ObjectsAttached), count _objects] call EFUNC(common,showMessage);
}, {}, [_objects, _entity]] call EFUNC(dialog,create);

}, [], LSTRING(AttachTo)] call EFUNC(common,selectPosition);
47 changes: 47 additions & 0 deletions addons/attached_objects/functions/fnc_attachToSelection.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "script_component.hpp"
/*
* Author: Ampersand
* Attaches the child object to a selection on a parent object.
*
* Arguments:
* 0: Child <OBJECT>
* 1: Parent <OBJECT>
* 2: Selection <STRING>
* 3: Relative <BOOLEAN> True to maintain current relative orientation, false to match selection orientation
*
* Return Value:
* 0: Selection Position and Orientation <ARRAY> For drawing hints
*
* Example:
* [_child] call zen_attached_objects_fnc_attachToSelection
*
* Public: No
*/

params ["_child", "_parent", "_selection", ["_isRelative", true]];

if (!isNull attachedTo _child) then {detach _child;};

private _childPos = _parent worldToModel ASLToAGL getPosWorldVisual _child;
private _childY = _parent vectorWorldToModelVisual vectorDirVisual _child;
private _childZ = _parent vectorWorldToModelVisual vectorUpVisual _child;
private _childX = _childY vectorCrossProduct _childZ;

private _selectionPos = _parent selectionPosition [_selection, LOD_MEMORY];
private _offset = _childPos vectorDiff _selectionPos;
_parent selectionVectorDirAndUp [_selection, LOD_MEMORY] params ["_selectionY", "_selectionZ"];
private _selectionX = _selectionY vectorCrossProduct _selectionZ;

private _m = matrixTranspose [_selectionX, _selectionY, _selectionZ];
private _pos = flatten ([_offset] matrixMultiply _m);
_child attachTo [_parent, _pos, _selection, true];

_child setVariable [QGVAR(attachedToSelection), _selection, true];

if (_isRelative) then {
[_childX, _childY, _childZ] matrixMultiply _m params ["", "_vY", "_vZ"];
_child setVectorDirAndUp [_vY, _vZ];
[QEGVAR(common,setVectorDirAndUp), [_child, [_vY, _vZ]], _child] call CBA_fnc_targetEvent;
};

[_selectionPos, _selectionY, _selectionZ]
8 changes: 7 additions & 1 deletion addons/attached_objects/functions/fnc_handleObjectEdited.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ params ["", "_object"];
// Update the position of attached objects relative to their parent object
private _parentObject = attachedTo _object;

if (!isNull _parentObject && {isVehicleCargo _object != _parentObject}) then {
if (isNull _parentObject || {isVehicleCargo _object == _parentObject}) exitWith {};

private _selection = _object getVariable [QGVAR(attachedToSelection), ""];

if (_selection == "") then {
private _offset = _parentObject worldToModel ASLtoAGL getPosWorld _object;
private _dirAndUp = [vectorDir _object, vectorUp _object] apply {_parentObject vectorWorldToModel _x};

Expand All @@ -31,4 +35,6 @@ if (!isNull _parentObject && {isVehicleCargo _object != _parentObject}) then {
_object attachTo [_parentObject, _offset];

[QEGVAR(common,setVectorDirAndUp), [_object, _dirAndUp], _object] call CBA_fnc_targetEvent;
} else {
[_object, _parentObject, _selection, true] call FUNC(attachToSelection);
};
2 changes: 2 additions & 0 deletions addons/attached_objects/functions/script_component.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#include "\x\zen\addons\attached_objects\script_component.hpp"

#define LOD_MEMORY 1e15
7 changes: 7 additions & 0 deletions addons/attached_objects/initKeybinds.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
};
}, {}, [DIK_A, [true, true, false]]] call CBA_fnc_addKeybind; // Default: CTRL + SHIFT + A

[[ELSTRING(main,DisplayName), LSTRING(DisplayName)], QGVAR(attachBone), [LSTRING(AttachBone), LSTRING(AttachBone_Description)], {
if (!isNull curatorCamera && {!GETMVAR(RscDisplayCurator_search,false)}) then {
SELECTED_OBJECTS call FUNC(attachBone);
true // handled
};
}, {}, []] call CBA_fnc_addKeybind; // Default: Unbound

[[ELSTRING(main,DisplayName), LSTRING(DisplayName)], QGVAR(detach), [LSTRING(Detach), LSTRING(Detach_Description)], {
if (!isNull curatorCamera && {!GETMVAR(RscDisplayCurator_search,false)}) then {
SELECTED_OBJECTS call FUNC(detach);
Expand Down
6 changes: 6 additions & 0 deletions addons/attached_objects/stringtable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<Key ID="STR_ZEN_Attached_Objects_Attach_Description">
<English>Attaches all selected objects to the specified parent object.</English>
</Key>
<Key ID="STR_ZEN_Attached_Objects_AttachBone">
<English>Attach Objects to Bone</English>
</Key>
<Key ID="STR_ZEN_Attached_Objects_AttachBone_Description">
<English>Attaches all selected objects to a bone on the specified parent object.</English>
</Key>
<Key ID="STR_ZEN_Attached_Objects_Detach">
<English>Detach Objects</English>
</Key>
Expand Down