<commit_before>package authoring.level;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import com.syntacticsugar.vooga.util.ResourceManager;
import com.syntacticsugar.vooga.util.gui.factory.AlertBoxFactory;
import com.syntacticsugar.vooga.util.gui.factory.SliderDialogFactory;

import authoring.data.MapData;
import authoring.data.TileData;
import authoring.data.TileImplementation;
import authoring.icons.implementations.AbstractIcon;
import authoring.icons.implementations.ImageIcon;
import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;
import javafx.collections.SetChangeListener;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.control.Tooltip;
import javafx.scene.effect.Effect;
import javafx.scene.effect.InnerShadow;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.stage.Screen;

public class MapEditor implements IMapEditor {
	
	private static final Effect TILE_EFFECT = createEffect();
	private static final String DEFAULT_TILE_IMAGE = "gray.png";

	private ObservableSet<TileData> myTileSelection;
	private Map<TileData, AbstractIcon> myTileIconMap;

	private MapData myMapData;
	private int myMapSize;
	private double mapDisplayWidth = Screen.getPrimary().getVisualBounds().getHeight()/1.5;
	private GridPane myMapGrid;
	
	public MapEditor() throws Exception {
		myTileSelection = buildSelectionSet();
		myMapSize = inputMapSize();
		myMapData = new MapData(myMapSize, DEFAULT_TILE_IMAGE);
		myMapGrid = new GridPane();
		initializeMapView(myMapData);
	}

	private static Effect createEffect() {
		InnerShadow shadow = new InnerShadow();
		shadow.setColor(Color.RED);
		return shadow;
	}
	
	public void loadMapData(MapData loadedMap) {
		myTileSelection = buildSelectionSet();
		myMapData = loadedMap;
		myMapSize = loadedMap.getMapSize();
		initializeMapView(loadedMap);
	}
	
	private ObservableSet<TileData> buildSelectionSet() {
		ObservableSet<TileData> set = FXCollections.observableSet();
		set.addListener(new SetChangeListener<TileData>() {
			@Override
			public void onChanged(javafx.collections.SetChangeListener.Change<? extends TileData> change) {
				if (change.wasAdded())
					tileEffectOn(myTileIconMap.get(change.getElementAdded()));
				else if (change.wasRemoved()) {
					tileEffectOff(myTileIconMap.get(change.getElementRemoved()));
				}
			}
		});
		return set;
	}
	
	private int inputMapSize() throws Exception {
		double size = SliderDialogFactory.createSliderDialog("Input Map Size", 10, 30, 1);
		if (size == Double.MIN_VALUE) {
			throw new Exception("Must select a map size before editing");
		}
		return (int) size;
	}
	
	private void initializeMapView(MapData mapData) {
		myMapGrid.getChildren().clear();
		myTileIconMap = new HashMap<TileData, AbstractIcon>();
		myMapGrid.setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
		myMapGrid.getStylesheets().add("/com/syntacticsugar/vooga/authoring/css/default.css");
		for (int i=0; i<myMapSize; i++) {
			for (int j=0; j<myMapSize; j++) {
				TileData tile = mapData.getTileData(i, j);
				AbstractIcon icon = new ImageIcon(tile.getImagePath(),  mapDisplayWidth/(1.0*myMapSize));
				TileInfoTooltip tileInfo = new TileInfoTooltip(tile);
				myTileIconMap.put(tile, icon);
				Tooltip.install(icon, tileInfo);
				icon.setOnMouseEntered(e -> mouseOverHandler(tile, e.isControlDown(), e.isShiftDown()));
				icon.setOnMouseClicked(e -> mouseClickHandler(tile));
				myMapGrid.getChildren().add(icon);
				GridPane.setConstraints(icon, i, j, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS, null);
			}
		}
	}

	private void mouseOverHandler(TileData tile, boolean isControlDown, boolean isShiftDown) {
		multiSelectTile(tile, isControlDown, isShiftDown);
	}

	private void mouseClickHandler(TileData tile) {
		toggleTileSelection(tile);
	}
	
	private void multiSelectTile(TileData tile, boolean isControlDown, boolean isShiftDown) {
		if (isControlDown) {
			myTileSelection.add(tile);
		}
		else if (isShiftDown) {
			myTileSelection.remove(tile);
		}
	}
	
	private void toggleTileSelection(TileData tile) {
		if (myTileSelection.contains(tile))
				myTileSelection.remove(tile);
		else<commit_msg>Drag Dropped enabled for ImgView only. Need to change tile in map.<commit_after>package authoring.level;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import com.syntacticsugar.vooga.util.ResourceManager;
import com.syntacticsugar.vooga.util.gui.factory.AlertBoxFactory;
import com.syntacticsugar.vooga.util.gui.factory.SliderDialogFactory;

import authoring.data.MapData;
import authoring.data.TileData;
import authoring.data.TileImplementation;
import authoring.icons.implementations.AbstractIcon;
import authoring.icons.implementations.ImageIcon;
import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;
import javafx.collections.SetChangeListener;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.control.Tooltip;
import javafx.scene.effect.Effect;
import javafx.scene.effect.InnerShadow;
import javafx.scene.image.Image;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.stage.Screen;

public class MapEditor implements IMapEditor {
	
	private static final Effect TILE_EFFECT = createEffect();
	private static final String DEFAULT_TILE_IMAGE = "gray.png";

	private ObservableSet<TileData> myTileSelection;
	private Map<TileData, AbstractIcon> myTileIconMap;

	private MapData myMapData;
	private int myMapSize;
	private double mapDisplayWidth = Screen.getPrimary().getVisualBounds().getHeight()/1.5;
	private GridPane myMapGrid;
	
	public MapEditor() throws Exception {
		myTileSelection = buildSelectionSet();
		myMapSize = inputMapSize();
		myMapData = new MapData(myMapSize, DEFAULT_TILE_IMAGE);
		myMapGrid = new GridPane();
		initializeMapView(myMapData);
	}

	private static Effect createEffect() {
		InnerShadow shadow = new InnerShadow();
		shadow.setColor(Color.RED);
		return shadow;
	}
	
	public void loadMapData(MapData loadedMap) {
		myTileSelection = buildSelectionSet();
		myMapData = loadedMap;
		myMapSize = loadedMap.getMapSize();
		initializeMapView(loadedMap);
	}
	
	private ObservableSet<TileData> buildSelectionSet() {
		ObservableSet<TileData> set = FXCollections.observableSet();
		set.addListener(new SetChangeListener<TileData>() {
			@Override
			public void onChanged(javafx.collections.SetChangeListener.Change<? extends TileData> change) {
				if (change.wasAdded())
					tileEffectOn(myTileIconMap.get(change.getElementAdded()));
				else if (change.wasRemoved()) {
					tileEffectOff(myTileIconMap.get(change.getElementRemoved()));
				}
			}
		});
		return set;
	}
	
	private int inputMapSize() throws Exception {
		double size = SliderDialogFactory.createSliderDialog("Input Map Size", 10, 30, 1);
		if (size == Double.MIN_VALUE) {
			throw new Exception("Must select a map size before editing");
		}
		return (int) size;
	}
	
	private void initializeMapView(MapData mapData) {
		myMapGrid.getChildren().clear();
		myTileIconMap = new HashMap<TileData, AbstractIcon>();
		myMapGrid.setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
		myMapGrid.getStylesheets().add("/com/syntacticsugar/vooga/authoring/css/default.css");
		for (int i=0; i<myMapSize; i++) {
			for (int j=0; j<myMapSize; j++) {
				TileData tile = mapData.getTileData(i, j);
				AbstractIcon icon = new ImageIcon(tile.getImagePath(),  mapDisplayWidth/(1.0*myMapSize));
				TileInfoTooltip tileInfo = new TileInfoTooltip(tile);
				myTileIconMap.put(tile, icon);
				Tooltip.install(icon, tileInfo);
				icon.setOnMouseEntered(e -> mouseOverHandler(tile, e.isControlDown(), e.isShiftDown()));
				icon.setOnMouseClicked(e -> mouseClickHandler(tile));
				icon.setOnDragOver(new EventHandler<DragEvent>() {
				    public void handle(DragEvent event) {
				        /* data is dragged over the target */
				        /* accept it only if it is not dragged from the same node 
				         * and if it has a string data */
				    		Dragboard db = event.getDragboard();
				    		System.out.println(db);
				            /* allow for both copying and moving, whatever user chooses */
				        	System.out.println("Tile is ready to accept dragged object");
				            event.acceptTransferModes(TransferMode.COPY_OR_MOVE);

				     
				        event.consume();
				    }
				});
				
				icon.setOnDragEntered(new EventHandler<DragEvent>() {
				    public void handle(DragEvent event) {
				    /* the drag-and-drop gesture entered the target */
				    /* show to the user that it is an actual gesture target */

			            if (event.getGestureSource() != icon &&
		                        event.getDragboard().hasString()) {
			            	icon.setOpacity(0);
		                }
		                
				    }
				});
				
				icon.setOnDragExited(new EventHandler<DragEvent>(){

					@Override
					public void handle(DragEvent event) {
						// TODO Auto-generated method stub
						if (event.getGestureSource() != icon &&
		                        event.getDragboard().hasString()) {
							icon.setOpacity(1);
						}
						
					}
					
				});
				icon.setOnDragDropped(new EventHandler<DragEvent>() {
				    public void handle(DragEvent event) {
				        /* data dropped */
				        /* if there is a string data on dragboard, read it and use it */
				        Dragboard db = event.getDragboard();
				        boolean success = false;
				        if (db.hasString()) {
				        	icon.setOpacity(1);
				        	icon.setImage(new Image(getClass().getClassLoader().getResourceAsStream(db.getString())));
				           success = true;
				        }
				        /* let the source know whether the string was successfully 
				         * transferred and used */
				        event.setDropCompleted(success);
				        
				        event.consume();
				     }
				});
				myMapGrid.getChildren().add(icon);
				GridPane.setConstraints(icon, i, j, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS, null);
			}
		}
	}

	private void mouseOverHandler(TileData tile, boolean isControlDown, boolean isShiftDown) {
		multiSelectTile(tile, isControlDown, isShiftDown);
	}

	private void mouseClickHandler(TileData tile) {
		toggleTileSelection(tile);
	}
	
	private void multiSelectTile(TileData tile, boolean isControlDown, boolean isShiftDown) {
		if (isControlDown) {
			myTileSelection.add(tile);
		}
		else if (isShiftDown) {
			myTileSelection.remove(tile);
		}
	}
	
	private void toggleTileSelection(TileData tile) {
		if (myTileSelection.contains(tile))
				myTileSelection.remove(tile);
		else
{'fraction_non_alphanumeric': 0.10739706279845031, 'fraction_numerical': 0.002612848004324714, 'mean_word_length': 4.036297640653357, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '1667899', 'n_tokens_mistral': 3810, 'n_tokens_neox': 3341, 'n_words': 715}