1414import com .jme3 .gde .core .assets .ProjectAssetManager ;
1515import com .jme3 .gde .core .properties .TexturePropertyEditor ;
1616import com .jme3 .gde .core .properties .preview .TexturePreview ;
17- import com .jme3 .gde .materials .MaterialProperty ;
1817import com .jme3 .gde .materials .dnd .TextureDropTargetListener ;
1918import com .jme3 .gde .materials .dnd .TextureDropTargetListener .TextureDropTarget ;
2019import com .jme3 .gde .materials .multiview .MaterialEditorTopComponent ;
2120import com .jme3 .gde .materials .multiview .widgets .icons .Icons ;
2221import java .awt .Component ;
23- import java .awt .Graphics2D ;
2422import java .awt .dnd .DropTarget ;
25- import java .awt .image .BufferedImage ;
23+ import java .util .ArrayList ;
24+ import java .util .List ;
2625import java .util .concurrent .ScheduledThreadPoolExecutor ;
2726import java .util .logging .Level ;
2827import java .util .logging .Logger ;
3534 */
3635public class TexturePanel extends MaterialPropertyWidget implements TextureDropTarget {
3736
37+ private final String REPEAT = "Repeat" ;
38+ private final String FLIP = "Flip" ;
39+ private final String EMPTY = "" ;
40+
3841 private TexturePropertyEditor editor ;
3942 private ProjectAssetManager manager ;
4043 private boolean flip = false ;
4144 private boolean repeat = false ;
4245 protected String textureName = null ; // always enclosed with ""
46+ protected String extraProperties = "" ; // in case the source file has extra properties on texture
4347 private TexturePreview texPreview ;
4448 private final ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor (1 );
4549
4650 /**
4751 * Used by tests
4852 */
4953 protected TexturePanel () {
50-
54+ initComponents ();
5155 }
5256
5357 /**
@@ -60,66 +64,47 @@ public TexturePanel(ProjectAssetManager manager) {
6064
6165 setDropTarget (new DropTarget (this , new TextureDropTargetListener (this )));
6266 }
63-
67+
6468 private void displayPreview () {
65- if (!"" .equals (textureName )) {
66- exec .execute (new Runnable () {
67- @ Override
68- public void run () {
69- try {
70- if (texPreview == null ) {
71- texPreview = new TexturePreview (manager );
72- }
73- texPreview .requestPreview (extractTextureName (textureName ), "" , 80 , 25 , texturePreview , null );
74- } catch (AssetNotFoundException a ) {
75- Logger .getLogger (MaterialEditorTopComponent .class .getName ()).log (Level .WARNING , "Could not load texture {0}" , textureName );
69+ if (!EMPTY .equals (textureName )) {
70+ exec .execute (() -> {
71+ try {
72+ if (texPreview == null ) {
73+ texPreview = new TexturePreview (manager );
7674 }
75+ texPreview .requestPreview (extractTextureName (textureName ), "" , 80 , 25 , texturePreview , null );
76+ } catch (AssetNotFoundException a ) {
77+ Logger .getLogger (MaterialEditorTopComponent .class .getName ()).log (Level .WARNING , "Could not load texture {0}" , textureName );
7778 }
7879 });
7980 }
8081 }
8182
8283 // visible for tests
83- protected String extractTextureName (String textureName ) {
84- final String [] textureNameComponents = textureName .split ("\" " );
85- return textureNameComponents [textureNameComponents .length - 1 ];
84+ protected String extractTextureName (final String property ) {
85+ final String [] textureNameComponents = property .split ("\" " );
86+ final int length = textureNameComponents .length ;
87+ if (property .endsWith ("\" " ) || length == 1 ) {
88+ // texture name is last in property, or it's empty
89+ return textureNameComponents [length - 1 ].trim ();
90+ }
91+ // has extra properties after name, return segment second to last
92+ return textureNameComponents [length - 2 ].trim ();
8693 }
8794
8895 // visible for tests
8996 protected void updateFlipRepeat () {
90- String propertyValue = property .getValue ();
91- propertyValue = propertyValue .replaceFirst (textureName , "" );
92- if (flip && !propertyValue .contains ("Flip " )) {
93- propertyValue += "Flip " ;
94- } else if (!flip ) {
95- propertyValue = propertyValue .replaceFirst ("Flip " , "" );
97+ final List <String > segments = new ArrayList <>();
98+ if (flip ) {
99+ segments .add (FLIP );
96100 }
97- if (repeat && !propertyValue .contains ("Repeat " )) {
98- propertyValue += "Repeat " ;
99- } else if (!repeat ) {
100- propertyValue = propertyValue .replaceFirst ("Repeat " , "" );
101+ if (repeat ) {
102+ segments .add (REPEAT );
101103 }
102- propertyValue += textureName ;
103- property .setValue (propertyValue );
104- texturePreview .setToolTipText (propertyValue );
105- }
106-
107- private static BufferedImage resizeImage (BufferedImage originalImage ) {
108- int type = originalImage .getType () == 0 ? BufferedImage .TYPE_INT_ARGB : originalImage .getType ();
109- float ratio = (float ) originalImage .getWidth () / (float ) originalImage .getHeight ();
110- int width = 80 ;
111- int height = 25 ;
112- if (ratio <= 1 ) {
113- height = (int ) ((float ) width * ratio );
114- } else {
115- width = (int ) ((float ) height * ratio );
116- }
117- BufferedImage resizedImage = new BufferedImage (width , height , type );
118- Graphics2D g = resizedImage .createGraphics ();
119- g .drawImage (originalImage , 0 , 0 , width , height , null );
120- g .dispose ();
121-
122- return resizedImage ;
104+ segments .add (textureName );
105+ segments .add (extraProperties );
106+ property .setValue (String .join (" " , segments ).trim ());
107+ texturePreview .setToolTipText (property .getValue ());
123108 }
124109
125110 /**
@@ -282,7 +267,7 @@ private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI
282267
283268 private void texturePreviewMouseClicked (java .awt .event .MouseEvent evt ) {//GEN-FIRST:event_texturePreviewMouseClicked
284269 Component view = editor .getCustomEditor ();
285- property .setValue ("" );
270+ property .setValue (EMPTY );
286271 view .setVisible (true );
287272 if (editor .getValue () != null ) {
288273 textureName = "\" " + editor .getAsText () + "\" " ;
@@ -300,25 +285,25 @@ private void texturePreviewMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FI
300285 @ Override
301286 protected void readProperty () {
302287 java .awt .EventQueue .invokeLater (() -> {
303- textureName = property .getValue ();
304- if (textureName .contains ("Flip " )) {
288+ String prop = property .getValue (). trim ();
289+ if (prop .contains (FLIP )) {
305290 flip = true ;
306- textureName = textureName . replaceFirst ( "Flip " , "" ).trim ();
291+ prop = prop . replace ( FLIP , EMPTY ).trim ();
307292 }
308- if (textureName .contains ("Repeat " )) {
293+ if (prop .contains (REPEAT )) {
309294 repeat = true ;
310- textureName = textureName . replaceFirst ( "Repeat " , "" ).trim ();
295+ prop = prop . replace ( REPEAT , EMPTY ).trim ();
311296 }
312- property .setValue (textureName );
297+ textureName = "\" " + extractTextureName (prop ) + "\" " ;
298+ extraProperties = prop .replace (textureName , "" ).trim ();
299+
300+
313301 jLabel1 .setText (property .getName ());
314302 jLabel1 .setToolTipText (property .getName ());
315303 displayPreview ();
316304 texturePreview .setToolTipText (property .getValue ());
317- MaterialProperty prop = property ;
318- property = null ;
319305 jCheckBox1 .setSelected (flip );
320306 jCheckBox2 .setSelected (repeat );
321- property = prop ;
322307 });
323308 }
324309
@@ -349,12 +334,23 @@ public void setTexture(String name) {
349334 textureName = "\" " + name + "\" " ;
350335 }
351336 property .setValue (textureName );
352- displayPreview ();
337+
353338 updateFlipRepeat ();
339+ displayPreview ();
354340 java .awt .EventQueue .invokeLater (() -> {
355341 fireChanged ();
356342 });
357343 });
358344
359345 }
346+
347+ // visible for tests
348+ protected boolean isFlip () {
349+ return flip ;
350+ }
351+
352+ // visible for tests
353+ protected boolean isRepeat () {
354+ return repeat ;
355+ }
360356}
0 commit comments