Skip to content

Commit 3866623

Browse files
authored
Merge pull request #574 from neph1/Issue-561
Issue 561
2 parents 828da64 + d6df89b commit 3866623

File tree

2 files changed

+169
-65
lines changed

2 files changed

+169
-65
lines changed

jme3-materialeditor/src/com/jme3/gde/materials/multiview/widgets/TexturePanel.java

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
import com.jme3.gde.core.assets.ProjectAssetManager;
1515
import com.jme3.gde.core.properties.TexturePropertyEditor;
1616
import com.jme3.gde.core.properties.preview.TexturePreview;
17-
import com.jme3.gde.materials.MaterialProperty;
1817
import com.jme3.gde.materials.dnd.TextureDropTargetListener;
1918
import com.jme3.gde.materials.dnd.TextureDropTargetListener.TextureDropTarget;
2019
import com.jme3.gde.materials.multiview.MaterialEditorTopComponent;
2120
import com.jme3.gde.materials.multiview.widgets.icons.Icons;
2221
import java.awt.Component;
23-
import java.awt.Graphics2D;
2422
import java.awt.dnd.DropTarget;
25-
import java.awt.image.BufferedImage;
23+
import java.util.ArrayList;
24+
import java.util.List;
2625
import java.util.concurrent.ScheduledThreadPoolExecutor;
2726
import java.util.logging.Level;
2827
import java.util.logging.Logger;
@@ -35,19 +34,24 @@
3534
*/
3635
public 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
}

jme3-materialeditor/test/unit/src/com/jme3/gde/materials/multiview/widgets/TexturePanelTest.java

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@
3232
package com.jme3.gde.materials.multiview.widgets;
3333

3434
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
36+
import static org.junit.jupiter.api.Assertions.assertFalse;
3537
import org.junit.jupiter.api.Test;
38+
import com.jme3.gde.materials.MaterialProperty;
39+
import java.awt.EventQueue;
40+
import java.lang.reflect.InvocationTargetException;
41+
import org.openide.util.Exceptions;
42+
3643

3744

3845
/**
@@ -44,24 +51,125 @@ public class TexturePanelTest {
4451
public TexturePanelTest() {
4552
}
4653

54+
String textureNameWithModifier = "Flip Repeat \"simple_name.jpg\"";
55+
String textureNameWithSpaces = "\"texture name with spaces.jpg\"";
56+
String textureNameWithSpaceAndModifier = "Flip Repeat \"texture name with spaces.jpg\"";
57+
String textureName = "\"simple_name.jpg\"";
58+
String textureNameWithExtraInfo = "Flip \"simple_name.jpg\" LINEAR";
59+
4760
@Test
4861
public void testExtractTextureName() {
4962
TexturePanel texturePanel = new TexturePanel();
50-
String textureName = "\"simple_name.jpg\"";
63+
5164
String extractedName = texturePanel.extractTextureName(textureName);
5265
assertEquals("simple_name.jpg", extractedName);
5366

54-
String textureNameWithModifier = "Flip Repeat \"simple_name.jpg\"";
67+
5568
extractedName = texturePanel.extractTextureName(textureNameWithModifier);
5669
assertEquals("simple_name.jpg", extractedName);
5770

58-
String textureNameWithSpaces = "\"texture name with spaces.jpg\"";
71+
5972
extractedName = texturePanel.extractTextureName(textureNameWithSpaces);
6073
assertEquals("texture name with spaces.jpg", extractedName);
6174

62-
String textureNameWithSpaceAndModifier = "Flip Repeat \"texture name with spaces.jpg\"";
75+
6376
extractedName = texturePanel.extractTextureName(textureNameWithSpaceAndModifier);
6477
assertEquals("texture name with spaces.jpg", extractedName);
78+
79+
extractedName = texturePanel.extractTextureName(textureNameWithExtraInfo);
80+
assertEquals("simple_name.jpg", extractedName);
81+
}
82+
83+
@Test
84+
public void testReadPropertyFlipRepeat() {
85+
TexturePanel texturePanel = new TexturePanel();
86+
texturePanel.setProperty(new MaterialProperty());
87+
texturePanel.property.setValue(textureNameWithModifier);
88+
try {
89+
EventQueue.invokeAndWait(() -> {
90+
texturePanel.readProperty();
91+
});
92+
} catch (InterruptedException | InvocationTargetException ex) {
93+
Exceptions.printStackTrace(ex);
94+
}
95+
96+
97+
assertEquals(textureName, texturePanel.textureName);
98+
assertTrue(texturePanel.isFlip());
99+
assertTrue(texturePanel.isRepeat());
100+
assertEquals(texturePanel.extraProperties, "");
101+
}
102+
103+
@Test
104+
public void testReadProperty() {
105+
TexturePanel texturePanel = new TexturePanel();
106+
texturePanel.setProperty(new MaterialProperty());
107+
texturePanel.property.setValue(textureName);
108+
109+
try {
110+
EventQueue.invokeAndWait(() -> {
111+
texturePanel.readProperty();
112+
});
113+
} catch (InterruptedException | InvocationTargetException ex) {
114+
Exceptions.printStackTrace(ex);
115+
}
116+
117+
assertEquals(texturePanel.textureName, textureName);
118+
assertFalse(texturePanel.isFlip());
119+
assertFalse(texturePanel.isRepeat());
120+
}
121+
122+
@Test
123+
public void testReadPropertyExtraProperty() {
124+
TexturePanel texturePanel = new TexturePanel();
125+
texturePanel.setProperty(new MaterialProperty());
126+
texturePanel.property.setValue(textureNameWithExtraInfo);
127+
try {
128+
EventQueue.invokeAndWait(() -> {
129+
texturePanel.readProperty();
130+
});
131+
} catch (InterruptedException | InvocationTargetException ex) {
132+
Exceptions.printStackTrace(ex);
133+
}
134+
135+
assertEquals(texturePanel.textureName, textureName);
136+
assertEquals(texturePanel.extraProperties, "LINEAR");
137+
}
138+
139+
@Test
140+
public void testUpdateFlipRepeat() {
141+
TexturePanel texturePanel = new TexturePanel();
142+
texturePanel.setProperty(new MaterialProperty());
143+
144+
texturePanel.property.setValue(textureNameWithModifier);
145+
try {
146+
EventQueue.invokeAndWait(() -> {
147+
texturePanel.readProperty();
148+
});
149+
} catch (InterruptedException | InvocationTargetException ex) {
150+
Exceptions.printStackTrace(ex);
151+
}
152+
153+
texturePanel.updateFlipRepeat();
154+
assertEquals(texturePanel.property.getValue(), textureNameWithModifier);
155+
}
156+
157+
@Test
158+
public void testUpdateFlipRepeatExtraProperty() {
159+
TexturePanel texturePanel = new TexturePanel();
160+
texturePanel.setProperty(new MaterialProperty());
161+
texturePanel.property.setValue(textureNameWithExtraInfo);
162+
try {
163+
EventQueue.invokeAndWait(() -> {
164+
texturePanel.readProperty();
165+
});
166+
} catch (InterruptedException | InvocationTargetException ex) {
167+
Exceptions.printStackTrace(ex);
168+
}
169+
170+
texturePanel.updateFlipRepeat();
171+
assertTrue(texturePanel.property.getValue().contains("LINEAR"));
172+
65173
}
66174

67175
}

0 commit comments

Comments
 (0)