6
6
import com .badlogic .gdx .InputMultiplexer ;
7
7
import com .badlogic .gdx .files .FileHandle ;
8
8
import com .badlogic .gdx .graphics .*;
9
- import com .badlogic .gdx .graphics .g2d .TextureRegion ;
10
9
import com .badlogic .gdx .graphics .g3d .Material ;
11
10
import com .badlogic .gdx .graphics .g3d .ModelInstance ;
12
11
import com .badlogic .gdx .graphics .g3d .attributes .ColorAttribute ;
13
- import com .badlogic .gdx .graphics .g3d .attributes .TextureAttribute ;
14
12
import com .badlogic .gdx .graphics .glutils .ShapeRenderer ;
15
13
import com .badlogic .gdx .math .Intersector ;
16
14
import com .badlogic .gdx .math .Plane ;
30
28
import com .interrupt .doomtest .levels .Level ;
31
29
import com .interrupt .doomtest .levels .Line ;
32
30
import com .interrupt .doomtest .levels .Sector ;
31
+ import com .interrupt .doomtest .levels .Surface ;
33
32
import com .interrupt .doomtest .levels .editor .Editor ;
34
33
35
34
public class DoomLikeEditor extends ApplicationAdapter {
@@ -85,7 +84,7 @@ public enum EditorModes { SECTOR, POINT, SPLIT };
85
84
86
85
public static float GRID_SNAP = 2f ;
87
86
88
- public TextureRegion currentTexture ;
87
+ public Surface currentTexture ;
89
88
public Stage hudStage ;
90
89
91
90
@ Override
@@ -113,8 +112,8 @@ public void create () {
113
112
OrthographicCamera hudCamera = new OrthographicCamera (Gdx .graphics .getWidth (), Gdx .graphics .getHeight ());
114
113
115
114
// 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" ));
118
117
119
118
// Setup the menu / HUD
120
119
hudStage = Hud .create (textures , textures .get (textures .size - 1 ), this );
@@ -379,7 +378,6 @@ else if(editorMode == EditorModes.POINT) {
379
378
else if (hoveredLine != null ) pickedLine = hoveredLine ;
380
379
else if (hoveredSector != null ) pickedSector = hoveredSector ;
381
380
382
- setHighlights ();
383
381
refreshRenderer ();
384
382
385
383
lastMousePoint .set (Gdx .input .getX (), Gdx .input .getY ());
@@ -481,8 +479,8 @@ public void finishSector() {
481
479
level .sectors .add (current );
482
480
483
481
// 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 );
486
484
}
487
485
488
486
@@ -500,7 +498,7 @@ public void finishSector() {
500
498
editPlane .set (Vector3 .Zero , Vector3 .Y );
501
499
}
502
500
503
- public void setHighlights () {
501
+ /* public void setHighlights() {
504
502
for(Sector s : level.sectors) {
505
503
resetSectorHighlights(s);
506
504
}
@@ -525,7 +523,7 @@ public void resetSectorHighlights(Sector sector) {
525
523
526
524
public void resetWallHighlights(Line line) {
527
525
line.lowerMaterial.set(ColorAttribute.createDiffuse(Color.WHITE));
528
- }
526
+ }*/
529
527
530
528
@ Override
531
529
public void render () {
@@ -731,11 +729,15 @@ public void renderGrid() {
731
729
lineRenderer .end ();
732
730
}
733
731
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 ) {
735
737
Pixmap atlas = new Pixmap (Gdx .files .local (filename ));
736
738
int texSize = atlas .getWidth () / 4 ;
737
739
738
- Array <TextureRegion > textures = new Array <TextureRegion >();
740
+ Array <Surface > textures = new Array <Surface >();
739
741
740
742
for (int x = 0 ; x < atlas .getWidth () / texSize ; x ++) {
741
743
for (int y = 0 ; y < atlas .getHeight () / texSize ; y ++) {
@@ -746,13 +748,24 @@ public Array<TextureRegion> loadTexturesFromAtlas(String filename) {
746
748
texture .setWrap (Texture .TextureWrap .Repeat , Texture .TextureWrap .Repeat );
747
749
texture .setFilter (Texture .TextureFilter .Nearest , Texture .TextureFilter .Nearest );
748
750
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 );
750
757
}
751
758
}
752
759
753
760
return textures ;
754
761
}
755
762
763
+ public void newLevel () {
764
+ level = new Level ();
765
+ editor .level = level ;
766
+ refreshRenderer ();
767
+ }
768
+
756
769
public void saveLevel (FileHandle file ) {
757
770
Json json = new Json ();
758
771
file .writeString (json .prettyPrint (level ), false );
@@ -762,5 +775,37 @@ public void openLevel(FileHandle file) {
762
775
Json json = new Json ();
763
776
String js = file .readString ();
764
777
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 );
765
810
}
766
811
}
0 commit comments