<commit_before>package kien.lmbseditor.window.animation;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import kien.lmbseditor.core.EditorProperty;
import kien.lmbseditor.core.animation.AnimationLMBSTimingBase;
import kien.lmbseditor.core.animation.AnimationLMBSTimingDamage;
import kien.lmbseditor.mv.Animation;
import kien.util.KienLogger;

public class AnimationContent extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -909798655021372590L;
	public int frameNumber;
	public Animation animation;
	ArrayList<ArrayList<Double>> frame;
	LinkedHashMap<Integer, ArrayList<AnimationLMBSTimingBase>> timings;
	
	private BufferedImage image1;
	private BufferedImage image2;
	
	public AnimationContent() {
		super();
		this.frameNumber = -1;
		this.animation = new Animation();
		this.frame = new ArrayList<ArrayList<Double>>();
		this.image1 = null;
		this.image2 = null;
		this.timings = null;
		this.setLayout(null);
		this.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
	}
	
	public void setAnimation(Animation anim) {
		this.animation = anim;
		if (this.animation != null) {
			this.loadImage();
			if (this.frameNumber >= 0) {
				frame = animation.frames.get(frameNumber);
				this.repaint();
			}
		}
	}
	
	public void setFrame(int f) {
		frameNumber = f;
		if (this.animation != null && this.frameNumber >= 0) {
			frame = animation.frames.get(frameNumber); 
			this.repaint();
		}
	}
	
	public void setTimings(LinkedHashMap<Integer, ArrayList<AnimationLMBSTimingBase>> timing) {
		this.timings = timing;
		this.repaint();
	}
	
	public void loadImage() {
		if (this.image1 != null) {
			this.image1.flush();
		}
		if (this.image2 != null) {
			this.image2.flush();
		}
		if (animation.animation1Name != null && !animation.animation1Name.isEmpty()){
			try {
				image1 = ImageIO.read(new File(EditorProperty.projectDirectory + "\\img\\animations\\" + animation.animation1Name + ".png"));
				//Util.rotateHue(image1, animation.animation1Hue);
			} catch (IOException e) {
				KienLogger.error("An error occured while loading Animatino Image File : ", e);
				image1 = null;
			}
		}
		if (animation.animation2Name != null && !animation.animation2Name.isEmpty()) {
			try {
				image2 = ImageIO.read(new File(EditorProperty.projectDirectory + "\\img\\animations\\" + animation.animation2Name + ".png"));
				//Util.rotateHue(image2, animation.animation2Hue);
			} catch (IOException e) {
				KienLogger.error("An error occured while loading Animatino Image File : ", e);
				image2 = null;
			}
		}
	} 
	
	@Override
	@SuppressWarnings("unused")
	public void paintComponent(Graphics g) {
		
		Graphics2D g2 = (Graphics2D)g.create();
		g2.clearRect(0, 0, this.getWidth(), this.getHeight());
		g2.setPaint(EditorProperty.getBackgroundTexturePaint());
		g2.fillRect(0, 0, this.getWidth(), this.getHeight());
		
		if (animation == null || frameNumber == -1 || (image1 == null && image2 == null)) {
			return;
		}
		
		if (this.animation.position == 0) {
			g2.translate(this.getWidth()/2, 30);
		} else if (this.animation.position == 1) {
			g2.translate(this.getWidth()/2, (this.getHeight()-30)/2);
		} else {
			g2.translate(this.getWidth()/2, this.getHeight()-30);
		}
		
		AffineTransform original = g2.getTransform();
		for (int n = 0; n < frame.size(); n++) {
			AffineTransform at = new AffineTransform();
			ArrayList<Double> cell = frame.get(n);
			int pattern = (int) Math.round(cell.get(0));
			if (pattern >= 0) {
				int sx = (pattern % 5) * 192;
				int sy = (int) Math.round(Math.floor(pattern % 100 / 5) * 192);
				BufferedImage img = pattern < 100 ? image1 : image2;
				if (img != null) {
					boolean mirror = Math.round(cell.get(5)) > 0 ? true : false;
					int rotation = (int) Math.round(cell.get(4));
					double scale = cell.get(3) / 100;
					double tx = cell.get(1) - 192/2 * scale;
					double ty = cell.get(2) - 192/2 * scale;
					int opacity = (int) Math.round(cell.get(6));
					at.rotate(Math.toRadians(rotation));
					at.scale(mirror ? -scale : scale, scale);
					g2.translate(tx, ty);
					<commit_msg>Fixed few painting error in animation with rotation.<commit_after>package kien.lmbseditor.window.animation;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import kien.lmbseditor.core.EditorProperty;
import kien.lmbseditor.core.animation.AnimationLMBSTimingBase;
import kien.lmbseditor.core.animation.AnimationLMBSTimingDamage;
import kien.lmbseditor.mv.Animation;
import kien.util.KienLogger;

public class AnimationContent extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -909798655021372590L;
	public int frameNumber;
	public Animation animation;
	ArrayList<ArrayList<Double>> frame;
	LinkedHashMap<Integer, ArrayList<AnimationLMBSTimingBase>> timings;
	
	private BufferedImage image1;
	private BufferedImage image2;
	
	public AnimationContent() {
		super();
		this.frameNumber = -1;
		this.animation = new Animation();
		this.frame = new ArrayList<ArrayList<Double>>();
		this.image1 = null;
		this.image2 = null;
		this.timings = null;
		this.setLayout(null);
		this.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
	}
	
	public void setAnimation(Animation anim) {
		this.animation = anim;
		if (this.animation != null) {
			this.loadImage();
			if (this.frameNumber >= 0) {
				frame = animation.frames.get(frameNumber);
				this.repaint();
			}
		}
	}
	
	public void setFrame(int f) {
		frameNumber = f;
		if (this.animation != null && this.frameNumber >= 0) {
			frame = animation.frames.get(frameNumber); 
			this.repaint();
		}
	}
	
	public void setTimings(LinkedHashMap<Integer, ArrayList<AnimationLMBSTimingBase>> timing) {
		this.timings = timing;
		this.repaint();
	}
	
	public void loadImage() {
		if (this.image1 != null) {
			this.image1.flush();
		}
		if (this.image2 != null) {
			this.image2.flush();
		}
		if (animation.animation1Name != null && !animation.animation1Name.isEmpty()){
			try {
				image1 = ImageIO.read(new File(EditorProperty.projectDirectory + "\\img\\animations\\" + animation.animation1Name + ".png"));
				//Util.rotateHue(image1, animation.animation1Hue);
			} catch (IOException e) {
				KienLogger.error("An error occured while loading Animatino Image File : ", e);
				image1 = null;
			}
		}
		if (animation.animation2Name != null && !animation.animation2Name.isEmpty()) {
			try {
				image2 = ImageIO.read(new File(EditorProperty.projectDirectory + "\\img\\animations\\" + animation.animation2Name + ".png"));
				//Util.rotateHue(image2, animation.animation2Hue);
			} catch (IOException e) {
				KienLogger.error("An error occured while loading Animatino Image File : ", e);
				image2 = null;
			}
		}
	} 
	
	@Override
	@SuppressWarnings("unused")
	public void paintComponent(Graphics g) {
		
		Graphics2D g2 = (Graphics2D)g.create();
		g2.clearRect(0, 0, this.getWidth(), this.getHeight());
		g2.setPaint(EditorProperty.getBackgroundTexturePaint());
		g2.fillRect(0, 0, this.getWidth(), this.getHeight());
		
		if (animation == null || frameNumber == -1 || (image1 == null && image2 == null)) {
			return;
		}
		
		if (this.animation.position == 0) {
			g2.translate(this.getWidth()/2, 30);
		} else if (this.animation.position == 1) {
			g2.translate(this.getWidth()/2, (this.getHeight()-30)/2);
		} else {
			g2.translate(this.getWidth()/2, this.getHeight()-30);
		}
		
		AffineTransform original = g2.getTransform();
		for (int n = 0; n < frame.size(); n++) {
			AffineTransform at = new AffineTransform();
			ArrayList<Double> cell = frame.get(n);
			int pattern = (int) Math.round(cell.get(0));
			if (pattern >= 0) {
				int sx = (pattern % 5) * 192;
				int sy = (int) Math.round(Math.floor(pattern % 100 / 5) * 192);
				BufferedImage img = pattern < 100 ? image1 : image2;
				if (img != null) {
					boolean mirror = Math.round(cell.get(5)) > 0 ? true : false;
					int rotation = (int) Math.round(cell.get(4));
					double scale = cell.get(3) / 100;
					double tx = cell.get(1) - 192/2 * scale;
					double ty = cell.get(2) - 192/2 * scale;
					int opacity = (int) Math.round(cell.get(6)); // 0-255
					at.rotate(Math.toRadians(rotation), 192/2 * scale, 192/2 * scale);
					at.scale(mirror ? -scale : scale, scale);
					g2.translate(tx, ty);
					AlphaComposite ac = AlphaComposite.getInstance(
							AlphaComposite.SRC_OVER,((float) opacity) / 255);
					g2.setComposite(ac);
{'fraction_non_alphanumeric': 0.14494184770682467, 'fraction_numerical': 0.026333113890717578, 'mean_word_length': 3.8329798515376456, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, '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': '7587405', 'n_tokens_mistral': 3543, 'n_tokens_neox': 3039, 'n_words': 651}