<commit_before>package com.ore.infinium;

import com.artemis.ComponentMapper;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Scaling;
import com.ore.infinium.components.BlockComponent;
import com.ore.infinium.components.ItemComponent;
import com.ore.infinium.components.SpriteComponent;
import com.ore.infinium.systems.NetworkClientSystem;
import com.ore.infinium.systems.TileRenderSystem;

/**
 * ***************************************************************************
 * Copyright (C) 2015 by <NAME> <<EMAIL>>                *
 * *
 * This program is free software; you can redistribute it and/or            *
 * modify it under the terms of the GNU General Public License as           *
 * published by the Free Software Foundation; either version 2 of           *
 * the License, or (at your option) any later version.                      *
 * *
 * This program is distributed in the hope that it will be useful,          *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 * GNU General Public License for more details.                             *
 * *
 * You should have received a copy of the GNU General Public License        *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.    *
 * ***************************************************************************
 */
public class HotbarInventoryView implements Inventory.SlotListener {
    private Skin m_skin;
    private Table container;
    private SlotElement[] m_slots = new SlotElement[Inventory.maxHotbarSlots];
    private OreWorld m_world;

    private ComponentMapper<ItemComponent> itemMapper;
    private ComponentMapper<BlockComponent> blockMapper;
    private ComponentMapper<SpriteComponent> spriteMapper;

    //the model for this view
    private Inventory m_hotbarInventory;

    //the main player inventory, for drag and drop
    private Inventory m_inventory;

    private Label m_tooltip;
    private Stage m_stage;

    public HotbarInventoryView(Stage stage, Skin skin, Inventory hotbarInventory, Inventory inventory,
                               DragAndDrop dragAndDrop, OreWorld world) {
        m_skin = skin;
        m_inventory = inventory;
        m_world = world;
        m_stage = stage;

        m_world.m_artemisWorld.inject(this);

        m_hotbarInventory = hotbarInventory;
        //attach to the inventory model
        m_hotbarInventory.addListener(this);

        container = new Table(m_skin);
        container.setFillParent(true);
        container.top().left().setSize(800, 100);
        container.padLeft(10).padTop(10);

        container.defaults().space(4);

        stage.addActor(container);

        Image dragImage = new Image();
        dragImage.setSize(32, 32);

        for (byte i = 0; i < Inventory.maxHotbarSlots; ++i) {

            Image slotImage = new Image();

            SlotElement element = new SlotElement();
            m_slots[i] = element;

            element.itemImage = slotImage;

            Table slotTable = new Table(m_skin);
            element.table = slotTable;
            slotTable.setTouchable(Touchable.enabled);
            slotTable.addListener(new SlotClickListener(this, i));
            slotTable.addListener(new SlotInputListener(this, i));

            slotTable.add(slotImage);
            slotTable.background("default-pane");

            slotTable.row();

            Label itemCount = new Label(null, m_skin);
            slotTable.add(itemCount).bottom().fill();
            element.itemCountLabel = itemCount;

            //            container.add(slotTable).size(50, 50);
            container.add(slotTable).fill().size(50, 50);
            setHotbarSlotVisible(i, false);

            dragAndDrop.addSource(new HotbarDragSource(slotTable, i, dragImage, this));

            dragAndDrop.addTarget(new HotbarDragTarget(slotTable, i, this));
        }

        m_tooltip = new Label(null, m_skin);
        stage.addActor(m_tooltip);
    }

    private void deselectPreviousSlot() {
        m_slots[m_hotbarInventory.m_previousSelectedSlot].table.setColor(Color.WHITE);
    }

    @Override
    public void countChanged(byte index, Inventory inventory) {
        ItemComponent itemComponent = itemMapper.get(inventory.itemEntity(index));
        m_slots[index].itemCountLabel.setText(Integer.toString(itemComponent.stackSize));
    }

    @Override
    public void set(byte index, Inventory inventory) {
        SlotElement slot = m_slots[index];

        int itemEntity = inventory.itemEntity(index);
        ItemComponent itemComponent = itemMapper.get(itemEntity);
        m_slots[index].itemCountLabel.setText(Integer.toString(itemComponent.stackSize));

        TextureRegion region;
        SpriteComponent spriteComponent = spriteMapper.get(itemEntity);
        if (blockMapper.get(itemEntity) != null) {
            //fixme
            region = m_world.m_artemisWorld.getSystem(TileRenderSystem.class).m_tilesAtlas.findRegion(
                    spriteComponent.textureName.concat("-00"));
        } else {
            region = m_world.m_artemisWorld.getSystem(TileRenderSystem.class).m_tilesAtlas.findRegion(
                    spriteComponent.textureName);
        }

        Image slotImage = slot.itemImage;
        //        //m_blockAtlas.findRegion("stone"));
        slotImage.setDrawable(new TextureRegionDrawable(region));
        slotImage.setSize(region.getRegionWidth(), region.getRegionHeight());
        slotImage.setScaling(Scaling.fit);

        setHotbarSlotVisible(index, true);

        //do not exceed the max size/resort to horrible upscaling. prefer native size of each inventory sprite.
        //.maxSize(region.getRegionWidth(), region.getRegionHeight()).expand().center();

    }

    @Override
    public void removed(byte index, Inventory inventory) {
        SlotElement slot = m_slots[index];
        //       slot.itemImage.setDrawable(null);
        //        slot.itemCountLabel.setText(null);
        setHotbarSlotVisible(index, false);
    }

    @Override
    public void selected(byte index, Inventory inventory) {
        deselectPreviousSlot();<commit_msg>add hotbar inventory view assertion for null texture region<commit_after>package com.ore.infinium;

import com.artemis.ComponentMapper;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Scaling;
import com.ore.infinium.components.BlockComponent;
import com.ore.infinium.components.ItemComponent;
import com.ore.infinium.components.SpriteComponent;
import com.ore.infinium.systems.NetworkClientSystem;
import com.ore.infinium.systems.TileRenderSystem;

/**
 * ***************************************************************************
 * Copyright (C) 2015 by <NAME> <<EMAIL>>                *
 * *
 * This program is free software; you can redistribute it and/or            *
 * modify it under the terms of the GNU General Public License as           *
 * published by the Free Software Foundation; either version 2 of           *
 * the License, or (at your option) any later version.                      *
 * *
 * This program is distributed in the hope that it will be useful,          *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 * GNU General Public License for more details.                             *
 * *
 * You should have received a copy of the GNU General Public License        *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.    *
 * ***************************************************************************
 */
public class HotbarInventoryView implements Inventory.SlotListener {
    private Skin m_skin;
    private Table container;
    private SlotElement[] m_slots = new SlotElement[Inventory.maxHotbarSlots];
    private OreWorld m_world;

    private ComponentMapper<ItemComponent> itemMapper;
    private ComponentMapper<BlockComponent> blockMapper;
    private ComponentMapper<SpriteComponent> spriteMapper;

    //the model for this view
    private Inventory m_hotbarInventory;

    //the main player inventory, for drag and drop
    private Inventory m_inventory;

    private Label m_tooltip;
    private Stage m_stage;

    public HotbarInventoryView(Stage stage, Skin skin, Inventory hotbarInventory, Inventory inventory,
                               DragAndDrop dragAndDrop, OreWorld world) {
        m_skin = skin;
        m_inventory = inventory;
        m_world = world;
        m_stage = stage;

        m_world.m_artemisWorld.inject(this);

        m_hotbarInventory = hotbarInventory;
        //attach to the inventory model
        m_hotbarInventory.addListener(this);

        container = new Table(m_skin);
        container.setFillParent(true);
        container.top().left().setSize(800, 100);
        container.padLeft(10).padTop(10);

        container.defaults().space(4);

        stage.addActor(container);

        Image dragImage = new Image();
        dragImage.setSize(32, 32);

        for (byte i = 0; i < Inventory.maxHotbarSlots; ++i) {

            Image slotImage = new Image();

            SlotElement element = new SlotElement();
            m_slots[i] = element;

            element.itemImage = slotImage;

            Table slotTable = new Table(m_skin);
            element.table = slotTable;
            slotTable.setTouchable(Touchable.enabled);
            slotTable.addListener(new SlotClickListener(this, i));
            slotTable.addListener(new SlotInputListener(this, i));

            slotTable.add(slotImage);
            slotTable.background("default-pane");

            slotTable.row();

            Label itemCount = new Label(null, m_skin);
            slotTable.add(itemCount).bottom().fill();
            element.itemCountLabel = itemCount;

            //            container.add(slotTable).size(50, 50);
            container.add(slotTable).fill().size(50, 50);
            setHotbarSlotVisible(i, false);

            dragAndDrop.addSource(new HotbarDragSource(slotTable, i, dragImage, this));

            dragAndDrop.addTarget(new HotbarDragTarget(slotTable, i, this));
        }

        m_tooltip = new Label(null, m_skin);
        stage.addActor(m_tooltip);
    }

    private void deselectPreviousSlot() {
        m_slots[m_hotbarInventory.m_previousSelectedSlot].table.setColor(Color.WHITE);
    }

    @Override
    public void countChanged(byte index, Inventory inventory) {
        ItemComponent itemComponent = itemMapper.get(inventory.itemEntity(index));
        m_slots[index].itemCountLabel.setText(Integer.toString(itemComponent.stackSize));
    }

    @Override
    public void set(byte index, Inventory inventory) {
        SlotElement slot = m_slots[index];

        int itemEntity = inventory.itemEntity(index);
        ItemComponent itemComponent = itemMapper.get(itemEntity);
        m_slots[index].itemCountLabel.setText(Integer.toString(itemComponent.stackSize));

        TextureRegion region;
        SpriteComponent spriteComponent = spriteMapper.get(itemEntity);
        if (blockMapper.get(itemEntity) != null) {
            //fixme
            region = m_world.m_artemisWorld.getSystem(TileRenderSystem.class).m_tilesAtlas.findRegion(
                    spriteComponent.textureName.concat("-00"));
        } else {
            region = m_world.m_artemisWorld.getSystem(TileRenderSystem.class).m_tilesAtlas.findRegion(
                    spriteComponent.textureName);
        }

        Image slotImage = slot.itemImage;
        //        //m_blockAtlas.findRegion("stone"));

        assert region != null : "textureregion for inventory item was not found!";
        slotImage.setDrawable(new TextureRegionDrawable(region));
        slotImage.setSize(region.getRegionWidth(), region.getRegionHeight());
        slotImage.setScaling(Scaling.fit);

        setHotbarSlotVisible(index, true);

        //do not exceed the max size/resort to horrible upscaling. prefer native size of each inventory sprite.
        //.maxSize(region.getRegionWidth(), region.getRegionHeight()).expand().center();
    }

    @Override
    public void removed(byte index, Inventory inventory) {
        SlotElement slot = m_slots[index];
        //       slot.itemImage.setDrawable(null);
        //        slot.itemCountLabel.setText(null);
        setHotbarSlotVisible(index, false);
    }

    @Override
    public void selected(byte index, Inventory inventory) {
        deselectPreviousSlot();
{'fraction_non_alphanumeric': 0.1161456417800662, 'fraction_numerical': 0.005884516366311144, 'mean_word_length': 2.8746081504702192, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1882342', 'n_tokens_mistral': 3871, 'n_tokens_neox': 3663, 'n_words': 823}