<commit_before>
XMOT.namespace("XMOT.interaction.widgets");

/**
 * TODO
 *
 * @extends XMOT.interaction.widgets.Widget
 */
XMOT.interaction.widgets.TranslateGizmo = new XMOT.Class(
    XMOT.interaction.widgets.Widget, {

    GeometryType: XMOT.interaction.geometry.TranslateGizmo,

    /** Unlike the other widgets we have to use an xml3d overlay.
     *  Thus, in the constructor at first the overlay will be created and there the target
     *  will be mirrored in the overlay. On that mirrored node the widget's constructor will
     *  be invoked.
     *
     *  @this {XMOT.interaction.widgets.TranslateGizmo}
     *  @param {string} _id
     *  @param {XMOT.Transformable} _target
     */
    initialize: function(_id, _target)
    {
        if(_target.object.parentNode.tagName !== "group")
            throw new Error("XMOT.interaction.widgets.TranslateGizmo: target's parent node must be a group.");

        this._realTarget = _target;

        var xml3dTarget = XMOT.util.getXml3dRoot(_target.object);

        this._xml3dOverlay = new XMOT.XML3DOverlay(xml3dTarget);

        var mirroredTarget = this._createMirroredTarget(_id, _target);

        var mirroredTargetXfmable =
            XMOT.ClientMotionFactory.createTransformable(mirroredTarget);
        this.callSuper(_id, mirroredTargetXfmable);
    },

    /**
     *  @this {XMOT.interaction.widgets.TranslateGizmo}
     *  @override
     *  @protected
     */
    onCreateBehavior: function()
    {
        this._setup1DTranslaters();
        this._setup2DTranslaters();
    },

    /** Will mirror the transformations of the target node in the
     *  xml3d overlay and append it to the overlay.
     *
     *  The following tree is created:
     *  o target's grandparent
     *      o target's parent
     *          o target
     *
     *  This is needed because:
     *  1) the widget needs access to the target node itself, so it's replicated.
     *  2) the widget will modify the target's parent node, so the exact parent node transformation
     *      has to be replicated.
     *  3) because of the above the grandparent node will hold the world transformation of the
     *      target's grandparent node.
     *
     *  @this {XMOT.interaction.widgets.TranslateGizmo}
     *  @return {Element} the mirrored target
     */
    _createMirroredTarget: function(id, target)
    {
        var defsOverlay = XMOT.util.getOrCreateDefs(this._xml3dOverlay.xml3d);

        var targetNode = target.object;

        // target node
        var mirroredTarget = this._createTransformedGroup(
            "t_mirroredTarget_" + id, targetNode.getLocalMatrix());

        // target parent
        var targetParent = targetNode.parentNode;
        var parentMatrix = new XML3DMatrix();
        if(targetParent && targetParent.getLocalMatrix)
            parentMatrix = targetParent.getLocalMatrix();

        var mirroredTargetParent = this._createTransformedGroup("t_mirroredTargetParent_" + id,
            parentMatrix, mirroredTarget);

        // target's grandparent
        var targetGrandparent = targetParent ? targetParent.parentNode : null;
        var grandparentMatrix = new XML3DMatrix();
        if(targetGrandparent && targetGrandparent.getWorldMatrix)
            grandparentMatrix = targetGrandparent.getWorldMatrix();

        var mirroredTargetGrandparent = this._createTransformedGroup(
            "t_mirroredTargetParentsParent_" + id, grandparentMatrix, mirroredTargetParent);

        this._xml3dOverlay.xml3d.appendChild(mirroredTargetGrandparent);

        return mirroredTarget;
    },

    /** Create a group that is transformed by the given matrix.
     *
     *  @this {XMOT.interaction.widgets.TranslateGizmo}
     *  @private
     *
     *  @param {string} transformId
     *  @param {window.XML3DMatrix} xfmMatrix
     *  @param {window.Element=} child
     */
    _createTransformedGroup: function(transformId, xfmMatrix, child)
    {
        var defsOverlay = XMOT.util.getOrCreateDefs(this._xml3dOverlay.xml3d);

        defsOverlay.appendChild(XMOT.creation.element("transform", {
            id: transformId,
            translation: xfmMatrix.translation().str(),
            rotation: xfmMatrix.rotation().str()
        }));

        var group = XMOT.creation.element("group", {
            transform: "#" + transformId
        });

        if(child)
            group.appendChild(child);

        return group;
    },

    _setup1DTranslaters: function()
    {
        var xAxisConstraintFn = function(currentTranslation, newTranslation) {

            newTranslation.y = currentTranslation.y;<commit_msg>translategizmo: applied usage of new mirroredtarget class<commit_after>
XMOT.namespace("XMOT.interaction.widgets");

/**
 * TODO
 *
 * @extends XMOT.interaction.widgets.Widget
 */
XMOT.interaction.widgets.TranslateGizmo = new XMOT.Class(
    XMOT.interaction.widgets.Widget, {

    GeometryType: XMOT.interaction.geometry.TranslateGizmo,

    /** Unlike the other widgets we have to use an xml3d overlay.
     *  Thus, in the constructor at first the overlay will be created and there the target
     *  will be mirrored in the overlay. On that mirrored node the widget's constructor will
     *  be invoked.
     *
     *  @this {XMOT.interaction.widgets.TranslateGizmo}
     *  @param {string} _id
     *  @param {XMOT.Transformable} _target
     */
    initialize: function(_id, _target)
    {
        if(_target.object.parentNode.tagName !== "group")
            throw new Error("XMOT.interaction.widgets.TranslateGizmo: target's parent node must be a group.");

        this._realTarget = _target;

        // overlay
        var xml3dTarget = XMOT.util.getXml3dRoot(_target.object);
        this._xml3dOverlay = new XMOT.XML3DOverlay(xml3dTarget);

        // mirror the target node
        var mirroredTargetId = _id + "_mirroredTarget";
        this._mirroredTarget = new XMOT.interaction.behaviors.MirroredWidgetTarget(
            mirroredTargetId, this._xml3dOverlay, _target);
        this._mirroredTarget.attach();

        // setup widget using mirrored node
        var mirroredTargetXfmable =
            XMOT.ClientMotionFactory.createTransformable(this._mirroredTarget.getNode());
        this.callSuper(_id, mirroredTargetXfmable);
    },

    /**
     *  @this {XMOT.interaction.widgets.TranslateGizmo}
     *  @override
     *  @protected
     */
    onCreateBehavior: function()
    {
        this._setup1DTranslaters();
        this._setup2DTranslaters();
    },

    _setup1DTranslaters: function()
    {
        var xAxisConstraintFn = function(currentTranslation, newTranslation) {

            newTranslation.y = currentTranslation.y;
{'fraction_non_alphanumeric': 0.0927201917315758, 'fraction_numerical': 0.004793289394847214, 'mean_word_length': 2.6930309734513274, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2877534', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1912, 'n_words': 438}