Java tutorial
/* * Logic-Builder. * Copyright (C) 2016 Jesse Prescott (BleedObsidian) * * 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 3 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/>. */ package com.gmail.bleedobsidian.logicbuilder.screens.builder.managers; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.assets.loaders.TextureLoader; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.gmail.bleedobsidian.logicbuilder.LogicBuilder; import com.gmail.bleedobsidian.logicbuilder.screens.builder.managers.components.AndGateComponent; import com.gmail.bleedobsidian.logicbuilder.screens.builder.managers.components.ComponentTypes; import com.gmail.bleedobsidian.logicbuilder.screens.builder.managers.components.NandGateComponent; import com.gmail.bleedobsidian.logicbuilder.screens.builder.managers.components.NorGateComponent; import com.gmail.bleedobsidian.logicbuilder.screens.builder.managers.components.NotGateComponent; import com.gmail.bleedobsidian.logicbuilder.screens.builder.managers.components.OrGateComponent; import com.gmail.bleedobsidian.logicbuilder.screens.builder.managers.components.XnorGateComponent; import com.gmail.bleedobsidian.logicbuilder.screens.builder.managers.components.XorGateComponent; import com.gmail.bleedobsidian.logicbuilder.utils.GameInterface; import com.gmail.bleedobsidian.logicbuilder.utils.InvisibleButton; import com.gmail.bleedobsidian.logicbuilder.utils.ResourceManager; /** * This manager controls the gallery and allows elements to be dragged on to the * grid. * * @author Jesse Prescott (BleedObsidian) */ public class GalleryManager implements GameInterface, ResourceManager.ResourceManagerInterface { /** * AND Gate button. */ private InvisibleButton andGateButton; /** * NAND Gate button. */ private InvisibleButton nandGateButton; /** * OR Gate button. */ private InvisibleButton orGateButton; /** * NOR Gate button. */ private InvisibleButton norGateButton; /** * XOR Gate button. */ private InvisibleButton xorGateButton; /** * XNOR Gate button. */ private InvisibleButton xnorGateButton; /** * NOT Gate button. */ private InvisibleButton notGateButton; /** * The component that is currently being dragged. */ private ComponentTypes componentCurrentlyDragging; /** * If the left click mouse button is down. */ private boolean isMouseDown; /** * If the currently dragged component has been snapped to the grid. */ private boolean isComponentSnapped; /** * New GalleryManager. */ public GalleryManager() { } @Override public void resume() { } @Override public void update(float delta) { this.andGateButton.update(delta); this.nandGateButton.update(delta); this.orGateButton.update(delta); this.norGateButton.update(delta); this.xorGateButton.update(delta); this.xnorGateButton.update(delta); this.notGateButton.update(delta); if (!Gdx.input.isTouched()) { if (this.isMouseDown == true) { if (this.isComponentSnapped) { Vector3 mouseCoords = LogicBuilder.getInstance().getGridCamera() .unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0)); int gridX = GridManager.getGridX((int) mouseCoords.x); int gridY = GridManager.getGridY((int) mouseCoords.y); switch (this.componentCurrentlyDragging) { case AND: gridX -= (int) (ComponentTypes.AND.getGridWidth() / 2); gridY -= (int) (ComponentTypes.AND.getGridHeight() / 2); AndGateComponent andGate = new AndGateComponent(gridX, gridY); LogicBuilder.getInstance().getBuilderScreen().getComponentManager().addComponent(andGate); break; case NAND: gridX -= (int) (ComponentTypes.NAND.getGridWidth() / 2); gridY -= (int) (ComponentTypes.NAND.getGridHeight() / 2); NandGateComponent nandGate = new NandGateComponent(gridX, gridY); LogicBuilder.getInstance().getBuilderScreen().getComponentManager().addComponent(nandGate); break; case OR: gridX -= (int) (ComponentTypes.OR.getGridWidth() / 2); gridY -= (int) (ComponentTypes.OR.getGridHeight() / 2); OrGateComponent orGate = new OrGateComponent(gridX, gridY); LogicBuilder.getInstance().getBuilderScreen().getComponentManager().addComponent(orGate); break; case NOR: gridX -= (int) (ComponentTypes.OR.getGridWidth() / 2); gridY -= (int) (ComponentTypes.OR.getGridHeight() / 2); NorGateComponent norGate = new NorGateComponent(gridX, gridY); LogicBuilder.getInstance().getBuilderScreen().getComponentManager().addComponent(norGate); break; case XOR: gridX -= (int) (ComponentTypes.XOR.getGridWidth() / 2); gridY -= (int) (ComponentTypes.XOR.getGridHeight() / 2); XorGateComponent xorGate = new XorGateComponent(gridX, gridY); LogicBuilder.getInstance().getBuilderScreen().getComponentManager().addComponent(xorGate); break; case XNOR: gridX -= (int) (ComponentTypes.XNOR.getGridWidth() / 2); gridY -= (int) (ComponentTypes.XNOR.getGridHeight() / 2); XnorGateComponent xnorGate = new XnorGateComponent(gridX, gridY); LogicBuilder.getInstance().getBuilderScreen().getComponentManager().addComponent(xnorGate); break; case NOT: gridX -= (int) (ComponentTypes.NOT.getGridWidth() / 2); gridY -= (int) (ComponentTypes.NOT.getGridHeight() / 2); NotGateComponent notGate = new NotGateComponent(gridX, gridY); LogicBuilder.getInstance().getBuilderScreen().getComponentManager().addComponent(notGate); break; } } this.isMouseDown = false; } else { this.isMouseDown = false; } } } @Override public void render(SpriteBatch spriteBatch) { // Gallery spriteBatch.draw( LogicBuilder.getInstance().getResourceManager().getTexture("Textures/Logic-Gates-Title.png"), 0, 680); spriteBatch.draw(LogicBuilder.getInstance().getResourceManager().getTexture("Textures/Logic Gates/AND.png"), 25, 575); spriteBatch.draw( LogicBuilder.getInstance().getResourceManager().getTexture("Textures/Logic Gates/NAND.png"), 165, 575); spriteBatch.draw(LogicBuilder.getInstance().getResourceManager().getTexture("Textures/Logic Gates/OR.png"), 25, 475); spriteBatch.draw(LogicBuilder.getInstance().getResourceManager().getTexture("Textures/Logic Gates/NOR.png"), 165, 475); spriteBatch.draw(LogicBuilder.getInstance().getResourceManager().getTexture("Textures/Logic Gates/XOR.png"), 25, 375); spriteBatch.draw( LogicBuilder.getInstance().getResourceManager().getTexture("Textures/Logic Gates/XNOR.png"), 165, 375); spriteBatch.draw(LogicBuilder.getInstance().getResourceManager().getTexture("Textures/Logic Gates/NOT.png"), 25, 275); spriteBatch.draw( LogicBuilder.getInstance().getResourceManager().getTexture("Textures/Input-Output-Title.png"), 0, 175); // // Snapping if (this.isMouseDown) { spriteBatch.draw( LogicBuilder.getInstance().getResourceManager() .getTexture(this.componentCurrentlyDragging.getTexturePath()), Gdx.input.getX() - 50, (LogicBuilder.TARGET_HEIGHT - Gdx.input.getY()) - 38); spriteBatch.end(); Gdx.gl20.glDisable(GL20.GL_SCISSOR_TEST); Gdx.gl20.glEnable(GL20.GL_SCISSOR_TEST); Gdx.gl20.glScissor(300, 0, 1000, LogicBuilder.TARGET_HEIGHT); Vector3 touchLocation = LogicBuilder.getInstance().getGridCamera() .unproject(new Vector3(Gdx.app.getInput().getX(), Gdx.app.getInput().getY(), 0)); LogicBuilder.getInstance().getBuilderScreen().getGridSpriteBatch().begin(); Vector3 mouseCoords = LogicBuilder.getInstance().getGridCamera() .unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0)); int gridX = GridManager.getGridX((int) mouseCoords.x); int gridY = GridManager.getGridY((int) mouseCoords.y); if (gridX == -1 | gridY == -1) { LogicBuilder.getInstance().getBuilderScreen().getGridSpriteBatch() .draw(LogicBuilder.getInstance().getResourceManager() .getTexture(this.componentCurrentlyDragging.getTexturePath()), touchLocation.x - 50, touchLocation.y - 38); this.isComponentSnapped = false; } else { int leftGrid = GridManager.getGridX((int) (mouseCoords.x - ((this.componentCurrentlyDragging.getGridWidth() / 2) * GridManager.GRID_WIDTH))); int rightGrid = GridManager.getGridX((int) (mouseCoords.x + ((this.componentCurrentlyDragging.getGridWidth() / 2) * GridManager.GRID_WIDTH) - (GridManager.GRID_WIDTH / 2))); int topGrid = GridManager.getGridY((int) (mouseCoords.y + ((this.componentCurrentlyDragging.getGridHeight() / 2) * GridManager.GRID_HEIGHT) + (GridManager.GRID_HEIGHT / 2))); int bottomGrid = GridManager.getGridY((int) (mouseCoords.y - ((this.componentCurrentlyDragging.getGridHeight() / 2) * GridManager.GRID_HEIGHT))); if (leftGrid == -1 | rightGrid == -1 | topGrid == -1 | bottomGrid == -1) { LogicBuilder.getInstance().getBuilderScreen().getGridSpriteBatch() .draw(LogicBuilder.getInstance().getResourceManager() .getTexture(this.componentCurrentlyDragging.getTexturePath()), touchLocation.x - 50, touchLocation.y - 38); this.isComponentSnapped = false; } else { if (!(LogicBuilder.getInstance().getBuilderScreen().getComponentManager() .isIntersecting(leftGrid, bottomGrid) || LogicBuilder.getInstance().getBuilderScreen().getComponentManager() .isIntersecting(leftGrid, topGrid) || LogicBuilder.getInstance().getBuilderScreen().getComponentManager() .isIntersecting(rightGrid, bottomGrid) || LogicBuilder.getInstance().getBuilderScreen().getComponentManager() .isIntersecting(rightGrid, topGrid))) { LogicBuilder.getInstance().getBuilderScreen().getGridSpriteBatch() .draw(LogicBuilder.getInstance().getResourceManager() .getTexture(this.componentCurrentlyDragging.getTexturePath()), GridManager.gridXToX(leftGrid), GridManager.gridYToY(bottomGrid)); this.isComponentSnapped = true; } else { LogicBuilder.getInstance().getBuilderScreen().getGridSpriteBatch() .draw(LogicBuilder.getInstance().getResourceManager() .getTexture(this.componentCurrentlyDragging.getTexturePath()), touchLocation.x - 50, touchLocation.y - 38); this.isComponentSnapped = false; } } } LogicBuilder.getInstance().getBuilderScreen().getGridSpriteBatch().end(); Gdx.gl20.glDisable(GL20.GL_SCISSOR_TEST); Gdx.gl20.glEnable(GL20.GL_SCISSOR_TEST); Gdx.gl20.glScissor(0, 0, 300, LogicBuilder.TARGET_HEIGHT); spriteBatch.begin(); } // } @Override public void pause() { } /** * Set the currently dragging component. * * @param componentType */ public void setDragging(ComponentTypes componentType) { GalleryManager.this.isMouseDown = true; GalleryManager.this.componentCurrentlyDragging = componentType; } /** * @return If the mouse is currently down. */ public boolean isMouseDown() { return this.isMouseDown; } @Override public void loadResources(ResourceManager resourceManager) { TextureLoader.TextureParameter filter = new TextureLoader.TextureParameter(); filter.magFilter = Texture.TextureFilter.Linear; filter.minFilter = Texture.TextureFilter.Linear; resourceManager.loadTexture("Textures/Logic-Gates-Title.png", filter); resourceManager.loadTexture("Textures/Input-Output-Title.png", filter); resourceManager.loadTexture("Textures/Logic Gates/AND.png", filter); resourceManager.loadTexture("Textures/Logic Gates/NAND.png", filter); resourceManager.loadTexture("Textures/Logic Gates/OR.png", filter); resourceManager.loadTexture("Textures/Logic Gates/NOR.png", filter); resourceManager.loadTexture("Textures/Logic Gates/XOR.png", filter); resourceManager.loadTexture("Textures/Logic Gates/XNOR.png", filter); resourceManager.loadTexture("Textures/Logic Gates/NOT.png", filter); } @Override public void onResourcesLoaded(ResourceManager resourceManager) { this.andGateButton = new InvisibleButton(LogicBuilder.getInstance().getGalleryCamera(), 25, 575, 100, 75); this.andGateButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { if (!GalleryManager.this.isMouseDown) { GalleryManager.this.isMouseDown = true; GalleryManager.this.componentCurrentlyDragging = ComponentTypes.AND; } } }); this.nandGateButton = new InvisibleButton(LogicBuilder.getInstance().getGalleryCamera(), 165, 575, 100, 75); this.nandGateButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { if (!GalleryManager.this.isMouseDown) { GalleryManager.this.isMouseDown = true; GalleryManager.this.componentCurrentlyDragging = ComponentTypes.NAND; } } }); this.orGateButton = new InvisibleButton(LogicBuilder.getInstance().getGalleryCamera(), 25, 475, 100, 75); this.orGateButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { if (!GalleryManager.this.isMouseDown) { GalleryManager.this.isMouseDown = true; GalleryManager.this.componentCurrentlyDragging = ComponentTypes.OR; } } }); this.norGateButton = new InvisibleButton(LogicBuilder.getInstance().getGalleryCamera(), 165, 475, 100, 75); this.norGateButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { if (!GalleryManager.this.isMouseDown) { GalleryManager.this.isMouseDown = true; GalleryManager.this.componentCurrentlyDragging = ComponentTypes.NOR; } } }); this.xorGateButton = new InvisibleButton(LogicBuilder.getInstance().getGalleryCamera(), 25, 375, 100, 75); this.xorGateButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { if (!GalleryManager.this.isMouseDown) { GalleryManager.this.isMouseDown = true; GalleryManager.this.componentCurrentlyDragging = ComponentTypes.XOR; } } }); this.xnorGateButton = new InvisibleButton(LogicBuilder.getInstance().getGalleryCamera(), 165, 375, 100, 75); this.xnorGateButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { if (!GalleryManager.this.isMouseDown) { GalleryManager.this.isMouseDown = true; GalleryManager.this.componentCurrentlyDragging = ComponentTypes.XNOR; } } }); this.notGateButton = new InvisibleButton(LogicBuilder.getInstance().getGalleryCamera(), 25, 275, 100, 75); this.notGateButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { if (!GalleryManager.this.isMouseDown) { GalleryManager.this.isMouseDown = true; GalleryManager.this.componentCurrentlyDragging = ComponentTypes.NOT; } } }); } }