<commit_before>package mnm.mods.util.gui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.List;

import mnm.mods.util.gui.events.ActionPerformed;
import mnm.mods.util.gui.events.GuiKeyboardAdapter;
import mnm.mods.util.gui.events.GuiKeyboardEvent;
import mnm.mods.util.gui.events.GuiMouseAdapter;
import mnm.mods.util.gui.events.GuiMouseEvent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiScreen;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import com.google.common.collect.Lists;

/**
 * The base class for all gui components.
 *
 * @author Matthew
 */
public abstract class GuiComponent extends Gui {

    private boolean enabled = true;
    private boolean visible = true;
    private boolean hovered = false;
    private boolean entered = false;
    private boolean buttonHeld;

    protected Minecraft mc = Minecraft.getMinecraft();

    private int backColor = 0;
    private int foreColor = -1;
    private GuiPanel parent;
    private Rectangle bounds;
    private Dimension minimumSize = new Dimension();
    private float scale = 1;

    private List<ActionPerformed> actionListeners = Lists.newArrayList();
    private List<GuiMouseAdapter> mouseAdapters = Lists.newArrayList();
    private List<GuiKeyboardAdapter> keyboardAdapters = Lists.newArrayList();

    public GuiComponent() {
        this.setBounds(new Rectangle());
        if (this instanceof ActionPerformed) {
            actionListeners.add((ActionPerformed) this);
        }
        if (this instanceof GuiMouseAdapter) {
            mouseAdapters.add((GuiMouseAdapter) this);
        }
        if (this instanceof GuiKeyboardAdapter) {
            keyboardAdapters.add((GuiKeyboardAdapter) this);
        }
    }

    /**
     * Draws this component on screen.
     *
     * @param mouseX The mouse x
     * @param mouseY The mouse y
     */
    public abstract void drawComponent(int mouseX, int mouseY);

    /**
     * Draws borders around the provided points.
     *
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     */
    protected void drawBorders(int x1, int y1, int x2, int y2) {
        int val = 0xaa;
        Gui.drawRect(x1 - 1, y1 - 1, x1, y2, getBackColor() + val << 24); // left
        Gui.drawRect(x1 - 1, y1 - 1, x2 + 1, y1, getBackColor() + val << 24); // top
        Gui.drawRect(x2, y1 - 1, x2 + 1, y2 + 1, getBackColor() + val << 24); // right
        Gui.drawRect(x1 - 1, y2, x2 + 1, y2 + 1, getBackColor() + val << 24); // bottom
    }

    /**
     * Updates the component. Called when it is called on the {@link GuiScreen}.
     */
    public void updateComponent() {}

    /**
     * Handles the mouse input and sends it to the mouse and action listeners.
     */
    public void handleMouseInput() {
        if (!isEnabled()) {
            this.hovered = false;
            return;
        }
        if (mc.currentScreen != null) {
            int mouseX = Mouse.getX() * mc.currentScreen.width / mc.displayWidth;
            int mouseY = mc.currentScreen.height - Mouse.getY() * mc.currentScreen.height
                    / mc.displayHeight - 1;
            Point point = new Point(mouseX, mouseY);

            int button = Mouse.getEventButton();
            int scroll = Mouse.getEventDWheel();
            Point actual = getActualPosition();

            float scale = getActualScale();

            if (mouseX > actual.x && mouseX < actual.x + getBounds().width * scale
                    && mouseY > actual.y && mouseY < actual.y + getBounds().height * scale) {
                if (!isHovered()) {
                    this.entered = true;
                }
                this.hovered = true;
            } else {
                if (!isHovered()) {
                    this.entered = false;
                }
                this.hovered = false;
            }
            if (button != -1 && Mouse.getEventButtonState()) {
                this.buttonHeld = (true);

            }
            GuiMouseEvent event = new GuiMouseEvent(this, GuiMouseEvent.RAW, point, button, scroll);
            for (GuiMouseAdapter adapter : mouseAdapters) {
                event.event = GuiMouseEvent.RAW;
                adapter.accept(event);

                if (isHovered() || isButtonHeld()) {
                    if (Mouse.getEventDX() != 0 && Mouse.getEventDY() != 0) {
                        // mouse moved
                        event.event = GuiMouseEvent.MOVED;
                        adapter.accept(event);
                        if (isButtonHeld()) {
                            // mouse dragged
                            event.event = GuiMouseEvent.DRAGGED;
                            adapter.accept(event);
                        }
                    }
                    if (isHovered() && button != -1) {
                        if (Mouse.getEventButtonState()) {
                            // button pressed
                            event.event = GuiMouseEvent.PRESSED;
                            adapter.accept(event);
                        } else if (!Mouse.getEventButtonState()) {
                            // button released
                            event.event = GuiMouseEvent.RELEASED;
                            adapter.accept(event);
                            if (isButtonHeld()) {
                                // button clicked
                                event.event = GuiMouseEvent.CLICKED;
                                adapter.accept(event);
                            }
                            this.buttonHeld = false;
                        }
                    }

                    if (isHovered()) {
                        if (entered) {
                            // mouse entered
                            event.event = GuiMouseEvent.ENTERED;
                            adapter.accept(event);
                        } else {
                            // mouse left
                            event.event = GuiMouseEvent.HOVERED;
                            adapter.accept(event);
                        }
                    } else {
                        if (entered) {<commit_msg>Fix release and click mouse events<commit_after>package mnm.mods.util.gui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.List;

import mnm.mods.util.gui.events.ActionPerformed;
import mnm.mods.util.gui.events.GuiKeyboardAdapter;
import mnm.mods.util.gui.events.GuiKeyboardEvent;
import mnm.mods.util.gui.events.GuiMouseAdapter;
import mnm.mods.util.gui.events.GuiMouseEvent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiScreen;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import com.google.common.collect.Lists;

/**
 * The base class for all gui components.
 *
 * @author Matthew
 */
public abstract class GuiComponent extends Gui {

    private boolean enabled = true;
    private boolean visible = true;
    private boolean hovered = false;
    private boolean entered = false;
    private boolean buttonHeld;

    protected Minecraft mc = Minecraft.getMinecraft();

    private int backColor = 0;
    private int foreColor = -1;
    private GuiPanel parent;
    private Rectangle bounds;
    private Dimension minimumSize = new Dimension();
    private float scale = 1;

    private List<ActionPerformed> actionListeners = Lists.newArrayList();
    private List<GuiMouseAdapter> mouseAdapters = Lists.newArrayList();
    private List<GuiKeyboardAdapter> keyboardAdapters = Lists.newArrayList();

    public GuiComponent() {
        this.setBounds(new Rectangle());
        if (this instanceof ActionPerformed) {
            actionListeners.add((ActionPerformed) this);
        }
        if (this instanceof GuiMouseAdapter) {
            mouseAdapters.add((GuiMouseAdapter) this);
        }
        if (this instanceof GuiKeyboardAdapter) {
            keyboardAdapters.add((GuiKeyboardAdapter) this);
        }
    }

    /**
     * Draws this component on screen.
     *
     * @param mouseX The mouse x
     * @param mouseY The mouse y
     */
    public abstract void drawComponent(int mouseX, int mouseY);

    /**
     * Draws borders around the provided points.
     *
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     */
    protected void drawBorders(int x1, int y1, int x2, int y2) {
        int val = 0xaa;
        Gui.drawRect(x1 - 1, y1 - 1, x1, y2, getBackColor() + val << 24); // left
        Gui.drawRect(x1 - 1, y1 - 1, x2 + 1, y1, getBackColor() + val << 24); // top
        Gui.drawRect(x2, y1 - 1, x2 + 1, y2 + 1, getBackColor() + val << 24); // right
        Gui.drawRect(x1 - 1, y2, x2 + 1, y2 + 1, getBackColor() + val << 24); // bottom
    }

    /**
     * Updates the component. Called when it is called on the {@link GuiScreen}.
     */
    public void updateComponent() {}

    /**
     * Handles the mouse input and sends it to the mouse and action listeners.
     */
    public void handleMouseInput() {
        if (!isEnabled()) {
            this.hovered = false;
            return;
        }
        if (mc.currentScreen != null) {
            int mouseX = Mouse.getX() * mc.currentScreen.width / mc.displayWidth;
            int mouseY = mc.currentScreen.height - Mouse.getY() * mc.currentScreen.height
                    / mc.displayHeight - 1;
            Point point = new Point(mouseX, mouseY);

            int button = Mouse.getEventButton();
            int scroll = Mouse.getEventDWheel();
            Point actual = getActualPosition();

            float scale = getActualScale();

            if (mouseX > actual.x && mouseX < actual.x + getBounds().width * scale
                    && mouseY > actual.y && mouseY < actual.y + getBounds().height * scale) {
                if (!isHovered()) {
                    this.entered = true;
                }
                this.hovered = true;
            } else {
                if (!isHovered()) {
                    this.entered = false;
                }
                this.hovered = false;
            }
            if (button != -1 && Mouse.getEventButtonState()) {
                this.buttonHeld = (true);

            }
            GuiMouseEvent event = new GuiMouseEvent(this, GuiMouseEvent.RAW, point, button, scroll);
            for (GuiMouseAdapter adapter : mouseAdapters) {
                event.event = GuiMouseEvent.RAW;
                adapter.accept(event);

                if (isHovered() || isButtonHeld()) {
                    if (Mouse.getEventDX() != 0 && Mouse.getEventDY() != 0) {
                        // mouse moved
                        event.event = GuiMouseEvent.MOVED;
                        adapter.accept(event);
                        if (isButtonHeld()) {
                            // mouse dragged
                            event.event = GuiMouseEvent.DRAGGED;
                            adapter.accept(event);
                        }
                    }
                    if ((isHovered() || isButtonHeld()) && button != -1) {
                        if (Mouse.getEventButtonState()) {
                            // button pressed
                            event.event = GuiMouseEvent.PRESSED;
                            adapter.accept(event);
                        } else if (!Mouse.getEventButtonState()) {
                            // button released
                            event.event = GuiMouseEvent.RELEASED;
                            adapter.accept(event);
                            if (isButtonHeld() && isHovered()) {
                                // button clicked
                                event.event = GuiMouseEvent.CLICKED;
                                adapter.accept(event);
                            }
                            this.buttonHeld = false;
                        }
                    }

                    if (isHovered()) {
                        if (entered) {
                            // mouse entered
                            event.event = GuiMouseEvent.ENTERED;
                            adapter.accept(event);
                        } else {
                            // mouse left
                            event.event = GuiMouseEvent.HOVERED;
                            adapter.accept(event);
                        }
                    } else {
                        if (entered) {
{'fraction_non_alphanumeric': 0.10011176752355101, 'fraction_numerical': 0.00830273032093246, 'mean_word_length': 1.4562745098039216, 'pattern_counts': {'":': 0, '<': 29, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4257610', 'n_tokens_mistral': 3462, 'n_tokens_neox': 3286, 'n_words': 807}