<commit_before>// The MIT License (MIT)
//
// Copyright (c) 2015, 2016 <NAME>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions: The above copyright notice and this permission
// notice shall be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
package phasereditor.canvas.ui.editors.behaviors;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.json.JSONObject;

import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.input.DragEvent;
import phasereditor.assetpack.core.AssetPackCore;
import phasereditor.assetpack.core.FrameData;
import phasereditor.assetpack.core.IAssetFrameModel;
import phasereditor.assetpack.core.IAssetKey;
import phasereditor.assetpack.core.ImageAssetModel;
import phasereditor.assetpack.core.SpritesheetAssetModel;
import phasereditor.assetpack.core.SpritesheetAssetModel.FrameModel;
import phasereditor.canvas.core.BaseObjectModel;
import phasereditor.canvas.core.CanvasCore;
import phasereditor.canvas.core.CanvasModelFactory;
import phasereditor.canvas.core.CanvasType;
import phasereditor.canvas.core.GroupModel;
import phasereditor.canvas.core.Prefab;
import phasereditor.canvas.core.TextModel;
import phasereditor.canvas.ui.CanvasUI;
import phasereditor.canvas.ui.editors.ObjectCanvas;
import phasereditor.canvas.ui.editors.operations.AddNodeOperation;
import phasereditor.canvas.ui.editors.operations.CompositeOperation;
import phasereditor.canvas.ui.editors.operations.DeleteNodeOperation;
import phasereditor.canvas.ui.editors.operations.SelectOperation;
import phasereditor.canvas.ui.shapes.BaseObjectControl;
import phasereditor.canvas.ui.shapes.CanvasObjectFactory;
import phasereditor.canvas.ui.shapes.GroupControl;
import phasereditor.canvas.ui.shapes.GroupNode;
import phasereditor.canvas.ui.shapes.IObjectNode;
import phasereditor.canvas.ui.shapes.ISpriteNode;
import phasereditor.lic.LicCore;

/**
 * @author arian
 *
 */
public class CreateBehavior {
	private ObjectCanvas _canvas;

	public CreateBehavior(ObjectCanvas canvas) {
		_canvas = canvas;
	}

	public void dropObjects(IStructuredSelection selection, DragEvent event) {
		double sceneX = event.getSceneX();
		double sceneY = event.getSceneY();
		dropObjects(selection, sceneX, sceneY, (group, elem) -> {
			return CanvasModelFactory.createModel(group, elem);
		});
	}

	public void dropObjects(IStructuredSelection selection, BiFunction<GroupModel, Object, BaseObjectModel> factory) {
		dropObjects(selection, _canvas.getScene().getWidth() / 2, _canvas.getScene().getHeight() / 2, (group, elem) -> {
			return factory.apply(group, elem);
		});
	}

	public void dropObjects(IStructuredSelection selection, double sceneX, double sceneY,
			BiFunction<GroupModel, Object, BaseObjectModel> factory) {

		Object[] elems = selection.toArray();

		// check the elements come from the same project

		IProject project = _canvas.getEditor().getEditorInputFile().getProject();
		for (Object elem : elems) {
			if (elem instanceof IAssetKey) {
				IProject srcProject = ((IAssetKey) elem).getAsset().getPack().getFile().getProject();
				if (!srcProject.equals(project)) {
					MessageDialog.openInformation(_canvas.getShell(), "Canvas",
							"Cannot paste assets from other projects.");
					return;
				}
			}
		}

		// check if free edition rules
		if (LicCore.isEvaluationProduct()) {

			String rule = CanvasCore.isFreeVersionAllowed(project);
			if (rule != null) {
				LicCore.launchGoPremiumDialogs(rule);
				return;
			}

			rule = AssetPackCore.isFreeVersionAllowed(project);
			if (rule != null) {
				LicCore.launchGoPremiumDialogs(rule);
				return;
			}

		}

		// handle when dropping into a sprite prefab (texture substitution)

		if (_canvas.getEditor().getModel().getType() == CanvasType.SPRITE) {
			Object first = selection.getFirstElement();

			if (!(first instanceof IAssetFrameModel || first instanceof ImageAssetModel)) {
				return;
			}

			IObjectNode node = (IObjectNode) _canvas.getWorldNode().getChildren().get(0);
			if (node instanceof ISpriteNode) {
				_canvas.getHandlerBehavior().clear();

				CompositeOperation operations = new CompositeOperation();
				CanvasUI.changeSpriteTexture(node, first, operations);
				operations.add(new SelectOperation(node.getModel().getId()));
				_canvas.getUpdateBehavior().executeOperations(operations);
			}

			return;
		}

		// check if the elements are from a spritesheet
<commit_msg>(v1) Show an info dialog when try to add an object to a sprite prefab.<commit_after>// The MIT License (MIT)
//
// Copyright (c) 2015, 2016 <NAME>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions: The above copyright notice and this permission
// notice shall be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
package phasereditor.canvas.ui.editors.behaviors;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.json.JSONObject;

import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.input.DragEvent;
import phasereditor.assetpack.core.AssetPackCore;
import phasereditor.assetpack.core.FrameData;
import phasereditor.assetpack.core.IAssetKey;
import phasereditor.assetpack.core.SpritesheetAssetModel;
import phasereditor.assetpack.core.SpritesheetAssetModel.FrameModel;
import phasereditor.canvas.core.BaseObjectModel;
import phasereditor.canvas.core.CanvasCore;
import phasereditor.canvas.core.CanvasModelFactory;
import phasereditor.canvas.core.CanvasType;
import phasereditor.canvas.core.GroupModel;
import phasereditor.canvas.core.Prefab;
import phasereditor.canvas.core.TextModel;
import phasereditor.canvas.ui.editors.ObjectCanvas;
import phasereditor.canvas.ui.editors.operations.AddNodeOperation;
import phasereditor.canvas.ui.editors.operations.CompositeOperation;
import phasereditor.canvas.ui.editors.operations.DeleteNodeOperation;
import phasereditor.canvas.ui.editors.operations.SelectOperation;
import phasereditor.canvas.ui.shapes.BaseObjectControl;
import phasereditor.canvas.ui.shapes.CanvasObjectFactory;
import phasereditor.canvas.ui.shapes.GroupControl;
import phasereditor.canvas.ui.shapes.GroupNode;
import phasereditor.canvas.ui.shapes.IObjectNode;
import phasereditor.lic.LicCore;

/**
 * @author arian
 *
 */
public class CreateBehavior {
	private ObjectCanvas _canvas;

	public CreateBehavior(ObjectCanvas canvas) {
		_canvas = canvas;
	}

	public void dropObjects(IStructuredSelection selection, DragEvent event) {
		double sceneX = event.getSceneX();
		double sceneY = event.getSceneY();
		dropObjects(selection, sceneX, sceneY, (group, elem) -> {
			return CanvasModelFactory.createModel(group, elem);
		});
	}

	public void dropObjects(IStructuredSelection selection, BiFunction<GroupModel, Object, BaseObjectModel> factory) {
		dropObjects(selection, _canvas.getScene().getWidth() / 2, _canvas.getScene().getHeight() / 2, (group, elem) -> {
			return factory.apply(group, elem);
		});
	}

	public void dropObjects(IStructuredSelection selection, double sceneX, double sceneY,
			BiFunction<GroupModel, Object, BaseObjectModel> factory) {

		Object[] elems = selection.toArray();

		// check the elements come from the same project

		IProject project = _canvas.getEditor().getEditorInputFile().getProject();
		for (Object elem : elems) {
			if (elem instanceof IAssetKey) {
				IProject srcProject = ((IAssetKey) elem).getAsset().getPack().getFile().getProject();
				if (!srcProject.equals(project)) {
					MessageDialog.openInformation(_canvas.getShell(), "Canvas",
							"Cannot paste assets from other projects.");
					return;
				}
			}
		}

		// check if free edition rules
		if (LicCore.isEvaluationProduct()) {

			String rule = CanvasCore.isFreeVersionAllowed(project);
			if (rule != null) {
				LicCore.launchGoPremiumDialogs(rule);
				return;
			}

			rule = AssetPackCore.isFreeVersionAllowed(project);
			if (rule != null) {
				LicCore.launchGoPremiumDialogs(rule);
				return;
			}

		}

		// handle when dropping into a sprite prefab (texture substitution)

		if (_canvas.getEditor().getModel().getType() == CanvasType.SPRITE) {

			MessageDialog.openInformation(_canvas.getShell(), "Not Allowed",
					"A sprite prefab only supports one object.");

			/*
			 * 
			 * Object first = selection.getFirstElement();
			 * 
			 * if (!(first instanceof IAssetFrameModel || first instanceof ImageAssetModel))
			 * { return; }
			 * 
			 * 
			 * 
			 * IObjectNode node = (IObjectNode) _canvas.getWorldNode().getChildren().get(0);
			 * if (node instanceof ISpriteNode) { _canvas.getHandlerBehavior().clear();
			 * 
			 * CompositeOperation operations = new CompositeOperation();
			 * CanvasUI.changeSpriteTexture(node, first, operations); operations.add(new
			 * SelectOperation(node.getModel().getId()));
			 * _canvas.getUpdateBehavior().executeOperations(operations); }
			 * 
			 */

			return;
		}

		// check if the elements are from a spritesheet

{'fraction_non_alphanumeric': 0.10233812949640288, 'fraction_numerical': 0.0022482014388489208, 'mean_word_length': 4.759192128430865, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1470192', 'n_tokens_mistral': 3530, 'n_tokens_neox': 3123, 'n_words': 868}