66import com .badlogic .gdx .InputMultiplexer ;
77import com .badlogic .gdx .files .FileHandle ;
88import com .badlogic .gdx .graphics .*;
9- import com .badlogic .gdx .graphics .g2d .TextureRegion ;
109import com .badlogic .gdx .graphics .g3d .Material ;
1110import com .badlogic .gdx .graphics .g3d .ModelInstance ;
1211import com .badlogic .gdx .graphics .g3d .attributes .ColorAttribute ;
13- import com .badlogic .gdx .graphics .g3d .attributes .TextureAttribute ;
1412import com .badlogic .gdx .graphics .glutils .ShapeRenderer ;
1513import com .badlogic .gdx .math .Intersector ;
1614import com .badlogic .gdx .math .Plane ;
3028import com .interrupt .doomtest .levels .Level ;
3129import com .interrupt .doomtest .levels .Line ;
3230import com .interrupt .doomtest .levels .Sector ;
31+ import com .interrupt .doomtest .levels .Surface ;
3332import com .interrupt .doomtest .levels .editor .Editor ;
3433
3534public class DoomLikeEditor extends ApplicationAdapter {
@@ -85,7 +84,7 @@ public enum EditorModes { SECTOR, POINT, SPLIT };
8584
8685 public static float GRID_SNAP = 2f ;
8786
88- public TextureRegion currentTexture ;
87+ public Surface currentTexture ;
8988 public Stage hudStage ;
9089
9190 @ Override
@@ -113,8 +112,8 @@ public void create () {
113112 OrthographicCamera hudCamera = new OrthographicCamera (Gdx .graphics .getWidth (), Gdx .graphics .getHeight ());
114113
115114 // Load textures
116- Array <TextureRegion > textures = loadTexturesFromAtlas ("textures/textures.png" );
117- textures .add (new TextureRegion ( Art . getTexture ( "textures/wall1.png" ) ));
115+ Array <Surface > textures = loadTexturesFromAtlas ("textures/textures.png" );
116+ textures .add (new Surface ( "textures/wall1.png" ));
118117
119118 // Setup the menu / HUD
120119 hudStage = Hud .create (textures , textures .get (textures .size - 1 ), this );
@@ -379,7 +378,6 @@ else if(editorMode == EditorModes.POINT) {
379378 else if (hoveredLine != null ) pickedLine = hoveredLine ;
380379 else if (hoveredSector != null ) pickedSector = hoveredSector ;
381380
382- setHighlights ();
383381 refreshRenderer ();
384382
385383 lastMousePoint .set (Gdx .input .getX (), Gdx .input .getY ());
@@ -481,8 +479,8 @@ public void finishSector() {
481479 level .sectors .add (current );
482480
483481 // set texture
484- current .floorMaterial .set ( TextureAttribute . createDiffuse ( currentTexture ) );
485- current .ceilingMaterial .set ( TextureAttribute . createDiffuse ( currentTexture ) );
482+ current .floorMaterial .match ( currentTexture );
483+ current .ceilingMaterial .match ( currentTexture );
486484 }
487485
488486
@@ -500,7 +498,7 @@ public void finishSector() {
500498 editPlane .set (Vector3 .Zero , Vector3 .Y );
501499 }
502500
503- public void setHighlights () {
501+ /* public void setHighlights() {
504502 for(Sector s : level.sectors) {
505503 resetSectorHighlights(s);
506504 }
@@ -525,7 +523,7 @@ public void resetSectorHighlights(Sector sector) {
525523
526524 public void resetWallHighlights(Line line) {
527525 line.lowerMaterial.set(ColorAttribute.createDiffuse(Color.WHITE));
528- }
526+ }*/
529527
530528 @ Override
531529 public void render () {
@@ -731,11 +729,15 @@ public void renderGrid() {
731729 lineRenderer .end ();
732730 }
733731
734- public Array <TextureRegion > loadTexturesFromAtlas (String filename ) {
732+ public String getTextureAtlasKey (String filename , int x , int y ) {
733+ return filename + "_" + x + "_" + y ;
734+ }
735+
736+ public Array <Surface > loadTexturesFromAtlas (String filename ) {
735737 Pixmap atlas = new Pixmap (Gdx .files .local (filename ));
736738 int texSize = atlas .getWidth () / 4 ;
737739
738- Array <TextureRegion > textures = new Array <TextureRegion >();
740+ Array <Surface > textures = new Array <Surface >();
739741
740742 for (int x = 0 ; x < atlas .getWidth () / texSize ; x ++) {
741743 for (int y = 0 ; y < atlas .getHeight () / texSize ; y ++) {
@@ -746,13 +748,24 @@ public Array<TextureRegion> loadTexturesFromAtlas(String filename) {
746748 texture .setWrap (Texture .TextureWrap .Repeat , Texture .TextureWrap .Repeat );
747749 texture .setFilter (Texture .TextureFilter .Nearest , Texture .TextureFilter .Nearest );
748750
749- textures .add (new TextureRegion (texture ));
751+ String atlasKey = getTextureAtlasKey (filename , x , y );
752+ Surface surface = new Surface (atlasKey );
753+
754+ Art .cacheTexture (atlasKey , texture );
755+
756+ textures .add (surface );
750757 }
751758 }
752759
753760 return textures ;
754761 }
755762
763+ public void newLevel () {
764+ level = new Level ();
765+ editor .level = level ;
766+ refreshRenderer ();
767+ }
768+
756769 public void saveLevel (FileHandle file ) {
757770 Json json = new Json ();
758771 file .writeString (json .prettyPrint (level ), false );
@@ -762,5 +775,37 @@ public void openLevel(FileHandle file) {
762775 Json json = new Json ();
763776 String js = file .readString ();
764777 level = json .fromJson (Level .class , js );
778+
779+ for (Sector s : level .sectors ) {
780+ matchSectorVertices (level .vertices , s );
781+ }
782+ for (Line l : level .lines ) {
783+ matchLineVertices (level .vertices , l );
784+ }
785+ editor .level = level ;
786+ refreshRenderer ();
787+ }
788+
789+ public void matchSectorVertices (Array <Vector2 > vertices , Sector sector ) {
790+ for (int i = 0 ; i < sector .points .size ; i ++) {
791+ Vector2 p = sector .points .get (i );
792+ int existing = vertices .indexOf (p , false );
793+ if (existing >= 0 ) {
794+ sector .points .set (i , vertices .get (existing ));
795+ }
796+ }
797+
798+ for (Sector s : sector .subsectors ) {
799+ matchSectorVertices (vertices , s );
800+ }
801+ }
802+
803+ public void matchLineVertices (Array <Vector2 > vertices , Line line ) {
804+ Vector2 start = line .start ;
805+ Vector2 end = line .end ;
806+ int existingStart = vertices .indexOf (start , false );
807+ int existingEnd = vertices .indexOf (end , false );
808+ if (existingStart >= 0 ) line .start = vertices .get (existingStart );
809+ if (existingEnd >= 0 ) line .end = vertices .get (existingEnd );
765810 }
766811}
0 commit comments