Skip to content

Commit

Permalink
Merge pull request #4 from Interrupt/texture-picker
Browse files Browse the repository at this point in the history
Refactored textures into intermediate surfaces. Fixed a bunch of saving / loading bugs.
  • Loading branch information
Interrupt authored Jul 16, 2016
2 parents 15f15f0 + 209f5bd commit cd94cc3
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 56 deletions.
71 changes: 58 additions & 13 deletions core/src/com/interrupt/doomtest/DoomLikeEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Plane;
Expand All @@ -30,6 +28,7 @@
import com.interrupt.doomtest.levels.Level;
import com.interrupt.doomtest.levels.Line;
import com.interrupt.doomtest.levels.Sector;
import com.interrupt.doomtest.levels.Surface;
import com.interrupt.doomtest.levels.editor.Editor;

public class DoomLikeEditor extends ApplicationAdapter {
Expand Down Expand Up @@ -85,7 +84,7 @@ public enum EditorModes { SECTOR, POINT, SPLIT };

public static float GRID_SNAP = 2f;

public TextureRegion currentTexture;
public Surface currentTexture;
public Stage hudStage;

@Override
Expand Down Expand Up @@ -113,8 +112,8 @@ public void create () {
OrthographicCamera hudCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

// Load textures
Array<TextureRegion> textures = loadTexturesFromAtlas("textures/textures.png");
textures.add(new TextureRegion(Art.getTexture("textures/wall1.png")));
Array<Surface> textures = loadTexturesFromAtlas("textures/textures.png");
textures.add(new Surface("textures/wall1.png"));

// Setup the menu / HUD
hudStage = Hud.create(textures, textures.get(textures.size - 1), this);
Expand Down Expand Up @@ -379,7 +378,6 @@ else if(editorMode == EditorModes.POINT) {
else if(hoveredLine != null) pickedLine = hoveredLine;
else if(hoveredSector != null) pickedSector = hoveredSector;

setHighlights();
refreshRenderer();

lastMousePoint.set(Gdx.input.getX(), Gdx.input.getY());
Expand Down Expand Up @@ -481,8 +479,8 @@ public void finishSector() {
level.sectors.add(current);

// set texture
current.floorMaterial.set(TextureAttribute.createDiffuse(currentTexture));
current.ceilingMaterial.set(TextureAttribute.createDiffuse(currentTexture));
current.floorMaterial.match(currentTexture);
current.ceilingMaterial.match(currentTexture);
}


Expand All @@ -500,7 +498,7 @@ public void finishSector() {
editPlane.set(Vector3.Zero, Vector3.Y);
}

public void setHighlights() {
/*public void setHighlights() {
for(Sector s : level.sectors) {
resetSectorHighlights(s);
}
Expand All @@ -525,7 +523,7 @@ public void resetSectorHighlights(Sector sector) {
public void resetWallHighlights(Line line) {
line.lowerMaterial.set(ColorAttribute.createDiffuse(Color.WHITE));
}
}*/

@Override
public void render () {
Expand Down Expand Up @@ -731,11 +729,15 @@ public void renderGrid() {
lineRenderer.end();
}

public Array<TextureRegion> loadTexturesFromAtlas(String filename) {
public String getTextureAtlasKey(String filename, int x, int y) {
return filename + "_" + x + "_" + y;
}

public Array<Surface> loadTexturesFromAtlas(String filename) {
Pixmap atlas = new Pixmap(Gdx.files.local(filename));
int texSize = atlas.getWidth() / 4;

Array<TextureRegion> textures = new Array<TextureRegion>();
Array<Surface> textures = new Array<Surface>();

for(int x = 0; x < atlas.getWidth() / texSize; x++) {
for(int y = 0; y < atlas.getHeight() / texSize; y++) {
Expand All @@ -746,13 +748,24 @@ public Array<TextureRegion> loadTexturesFromAtlas(String filename) {
texture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
texture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);

textures.add(new TextureRegion(texture));
String atlasKey = getTextureAtlasKey(filename, x, y);
Surface surface = new Surface(atlasKey);

Art.cacheTexture(atlasKey, texture);

textures.add(surface);
}
}

return textures;
}

public void newLevel() {
level = new Level();
editor.level = level;
refreshRenderer();
}

public void saveLevel(FileHandle file) {
Json json = new Json();
file.writeString(json.prettyPrint(level), false);
Expand All @@ -762,5 +775,37 @@ public void openLevel(FileHandle file) {
Json json = new Json();
String js = file.readString();
level = json.fromJson(Level.class, js);

for(Sector s : level.sectors) {
matchSectorVertices(level.vertices, s);
}
for(Line l : level.lines) {
matchLineVertices(level.vertices, l);
}
editor.level = level;
refreshRenderer();
}

public void matchSectorVertices(Array<Vector2> vertices, Sector sector) {
for(int i = 0; i < sector.points.size; i++) {
Vector2 p = sector.points.get(i);
int existing = vertices.indexOf(p, false);
if(existing >= 0) {
sector.points.set(i, vertices.get(existing));
}
}

for(Sector s : sector.subsectors) {
matchSectorVertices(vertices, s);
}
}

public void matchLineVertices(Array<Vector2> vertices, Line line) {
Vector2 start = line.start;
Vector2 end = line.end;
int existingStart = vertices.indexOf(start, false);
int existingEnd = vertices.indexOf(end, false);
if(existingStart >= 0) line.start = vertices.get(existingStart);
if(existingEnd >= 0) line.end = vertices.get(existingEnd);
}
}
26 changes: 11 additions & 15 deletions core/src/com/interrupt/doomtest/editor/ui/Hud.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
Expand All @@ -25,6 +23,7 @@
import com.interrupt.doomtest.editor.ui.menu.MenuItem;
import com.interrupt.doomtest.editor.ui.menu.Scene2dMenuBar;
import com.interrupt.doomtest.levels.Level;
import com.interrupt.doomtest.levels.Surface;

import javax.swing.*;
import java.awt.*;
Expand All @@ -42,7 +41,7 @@ private static Skin loadSkin() {
new TextureAtlas(Gdx.files.local("ui/HoloSkin/Holo-dark-ldpi.atlas")));
}

public static Stage create(final Array<TextureRegion> textures, TextureRegion current, final DoomLikeEditor doomLikeEditor) {
public static Stage create(final Array<Surface> textures, Surface current, final DoomLikeEditor doomLikeEditor) {

editor = doomLikeEditor;

Expand All @@ -60,7 +59,7 @@ public static Stage create(final Array<TextureRegion> textures, TextureRegion cu
.addItem(new MenuItem("Open", hudSkin, openAction)));
menuBar.pack();

final Image texturePickerButton = new Image(new TextureRegionDrawable(current));
final Image texturePickerButton = new Image(new TextureRegionDrawable(current.getTextureRegion()));
texturePickerButton.setScaling(Scaling.stretch);

Table wallPickerLayoutTable = new Table();
Expand All @@ -77,9 +76,9 @@ public static Stage create(final Array<TextureRegion> textures, TextureRegion cu
public void clicked(InputEvent event, float x, float y) {
TextureRegionPicker picker = new TextureRegionPicker("Pick Current Texture", hudSkin, textures) {
@Override
public void result(Integer value, TextureRegion region) {
public void result(Integer value, Surface region) {
setTexture(region, editor);
texturePickerButton.setDrawable(new TextureRegionDrawable(region));
texturePickerButton.setDrawable(new TextureRegionDrawable(region.getTextureRegion()));
}
};
stage.addActor(picker);
Expand All @@ -105,13 +104,13 @@ public boolean touchDown(InputEvent event, float x, float y, int pointer, int bu
return stage;
}

public static void setTexture(TextureRegion texture, DoomLikeEditor editor) {
public static void setTexture(Surface texture, DoomLikeEditor editor) {
editor.currentTexture = texture;

if (editor.pickedLine != null) {
editor.pickedLine.lowerMaterial.set(TextureAttribute.createDiffuse(texture));
editor.pickedLine.lowerMaterial.match(texture);
} else if (editor.pickedSector != null) {
editor.pickedSector.floorMaterial.set(TextureAttribute.createDiffuse(texture));
editor.pickedSector.floorMaterial.match(texture);
}
editor.refreshRenderer();
}
Expand Down Expand Up @@ -161,7 +160,7 @@ public boolean accept(File dir, String name) {
if (dir != null && file != null && file.trim().length() != 0) {
currentFileName = file;
currentDirectory = dir;
editor.saveLevel(new FileHandle(dir + file));
editor.saveLevel(Gdx.files.absolute((dir + file)));
}

frame.dispose();
Expand All @@ -171,8 +170,7 @@ public boolean accept(File dir, String name) {
private static ActionListener newAction = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
editor.level = new Level();
editor.refreshRenderer();
editor.newLevel();
}
};

Expand Down Expand Up @@ -200,7 +198,7 @@ public boolean accept(File dir, String name) {

if (dialog.getFile() != null) {
currentFileName = dialog.getFile();
currentDirectory = dialog.getDirectory();
currentDirectory = "";
}

final String file = dialog.getFile();
Expand All @@ -211,8 +209,6 @@ public boolean accept(File dir, String name) {
if (level.exists()) {
currentFileName = level.path();
editor.openLevel(Gdx.files.absolute(dir + file));
editor.refreshRenderer();

frame.dispose();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Scaling;
import com.interrupt.doomtest.gfx.Art;
import com.interrupt.doomtest.levels.Surface;

import static com.badlogic.gdx.scenes.scene2d.actions.Actions.sequence;

Expand All @@ -21,11 +23,10 @@ public abstract class TextureRegionPicker extends Window {
ScrollPane pane;
Table buttonLayout;
Cell paneCell;
SelectBox selectBox;

final Array<TextureRegion> regions;
final Array<Surface> regions;

public TextureRegionPicker(String title, Skin skin, Array<TextureRegion> regions) {
public TextureRegionPicker(String title, Skin skin, Array<Surface> regions) {
super(title, skin);

this.regions = regions;
Expand Down Expand Up @@ -77,8 +78,8 @@ protected void makeRegionButtons() {
buttonLayout.reset();

int num = 0;
for(TextureRegion region : regions) {
ImageButton button = new ImageButton(new TextureRegionDrawable(region));
for(Surface region : regions) {
ImageButton button = new ImageButton(new TextureRegionDrawable(region.getTextureRegion()));
button.getImage().setScaling(Scaling.fill);
button.getImageCell().width(spriteSize).height(spriteSize);

Expand Down Expand Up @@ -107,7 +108,7 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
}
};

public void button(Button button, final Integer value, final TextureRegion region) {
public void button(Button button, final Integer value, final Surface region) {
buttonLayout.add(button).pad(4f);

button.addListener(new ClickListener() {
Expand Down Expand Up @@ -162,5 +163,5 @@ public void hide (Action action) {
remove();
}

public abstract void result(Integer value, TextureRegion region);
public abstract void result(Integer value, Surface region);
}
6 changes: 5 additions & 1 deletion core/src/com/interrupt/doomtest/gfx/Art.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ public static Texture getTexture(String filename) {
texture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
texture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);

loadedTextures.put(filename, texture);
cacheTexture(filename, texture);

return texture;
}

public static void cacheTexture(String key, Texture texture) {
loadedTextures.put(key, texture);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.interrupt.doomtest.gfx.tesselators;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.interrupt.doomtest.gfx.Art;
import com.interrupt.doomtest.levels.Sector;
import org.lwjgl.util.glu.GLUtessellator;

Expand All @@ -18,7 +23,7 @@ public class SectorTesselator {

public static Array<ModelInstance> tesselate(Sector sector) {

TessCallback callback = new TessCallback(sector.floorMaterial);
TessCallback callback = new TessCallback();

tesselator.gluTessCallback(GLU_TESS_VERTEX, callback);
tesselator.gluTessCallback(GLU_TESS_BEGIN, callback);
Expand Down Expand Up @@ -74,8 +79,11 @@ private static Model makeFloorModel(Sector sector, Array<TessCallback.MeshPiece>
ModelBuilder mb = new ModelBuilder();
mb.begin();
int indx = 0;

Material material = sector.floorMaterial.createMaterial(sector.hashCode() + "_floor");

for(TessCallback.MeshPiece m : meshPieces) {
mb.part((indx++) + "", m.mesh, m.drawType, sector.floorMaterial);
mb.part((indx++) + "", m.mesh, m.drawType, material);
}
return mb.end();
}
Expand All @@ -84,9 +92,11 @@ private static Model makeCeilingModel(Sector sector, Array<TessCallback.MeshPiec
ModelBuilder mb = new ModelBuilder();
mb.begin();
int indx = 0;
Matrix4 transform = new Matrix4().translate(0, sector.ceilHeight - sector.floorHeight, 0);

Material material = sector.ceilingMaterial.createMaterial(sector.hashCode() + "_ceiling");

for(TessCallback.MeshPiece m : meshPieces) {
mb.part((indx++) + "", m.mesh, m.drawType, sector.ceilingMaterial);
mb.part((indx++) + "", m.mesh, m.drawType, material);
}
return mb.end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ public class MeshPiece {

public Array<MeshPiece> meshPieces = new Array<MeshPiece>();

public TessCallback(Material material) {
public TessCallback() {
modelBuilder.begin();
this.material = material;
}

public void begin(int type) {
Expand Down
Loading

0 comments on commit cd94cc3

Please sign in to comment.