Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 561 #574

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
import com.jme3.gde.core.assets.ProjectAssetManager;
import com.jme3.gde.core.properties.TexturePropertyEditor;
import com.jme3.gde.core.properties.preview.TexturePreview;
import com.jme3.gde.materials.MaterialProperty;
import com.jme3.gde.materials.dnd.TextureDropTargetListener;
import com.jme3.gde.materials.dnd.TextureDropTargetListener.TextureDropTarget;
import com.jme3.gde.materials.multiview.MaterialEditorTopComponent;
import com.jme3.gde.materials.multiview.widgets.icons.Icons;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.dnd.DropTarget;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -35,19 +34,24 @@
*/
public class TexturePanel extends MaterialPropertyWidget implements TextureDropTarget{

private final String REPEAT = "Repeat";
private final String FLIP = "Flip";
private final String EMPTY = "";

private TexturePropertyEditor editor;
private ProjectAssetManager manager;
private boolean flip = false;
private boolean repeat = false;
protected String textureName = null; // always enclosed with ""
protected String extraProperties = ""; // in case the source file has extra properties on texture
private TexturePreview texPreview;
private final ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);

/**
* Used by tests
*/
protected TexturePanel() {

initComponents();
}

/**
Expand All @@ -60,66 +64,47 @@ public TexturePanel(ProjectAssetManager manager) {

setDropTarget(new DropTarget(this, new TextureDropTargetListener(this)));
}

private void displayPreview() {
if (!"".equals(textureName)) {
exec.execute(new Runnable() {
@Override
public void run() {
try {
if (texPreview == null) {
texPreview = new TexturePreview(manager);
}
texPreview.requestPreview(extractTextureName(textureName), "", 80, 25, texturePreview, null);
} catch (AssetNotFoundException a) {
Logger.getLogger(MaterialEditorTopComponent.class.getName()).log(Level.WARNING, "Could not load texture {0}", textureName);
if (!EMPTY.equals(textureName)) {
exec.execute(() -> {
try {
if (texPreview == null) {
texPreview = new TexturePreview(manager);
}
texPreview.requestPreview(extractTextureName(textureName), "", 80, 25, texturePreview, null);
} catch (AssetNotFoundException a) {
Logger.getLogger(MaterialEditorTopComponent.class.getName()).log(Level.WARNING, "Could not load texture {0}", textureName);
}
});
}
}

// visible for tests
protected String extractTextureName(String textureName) {
final String[] textureNameComponents = textureName.split("\"");
return textureNameComponents[textureNameComponents.length - 1];
protected String extractTextureName(final String property) {
final String[] textureNameComponents = property.split("\"");
final int length = textureNameComponents.length;
if(property.endsWith("\"") || length == 1) {
// texture name is last in property, or it's empty
return textureNameComponents[length - 1].trim();
}
// has extra properties after name, return segment second to last
return textureNameComponents[length - 2].trim();
}

// visible for tests
protected void updateFlipRepeat() {
String propertyValue = property.getValue();
propertyValue = propertyValue.replaceFirst(textureName, "");
if (flip && !propertyValue.contains("Flip ")) {
propertyValue += "Flip ";
} else if (!flip) {
propertyValue = propertyValue.replaceFirst("Flip ", "");
final List<String> segments = new ArrayList<>();
if (flip) {
segments.add(FLIP);
}
if (repeat && !propertyValue.contains("Repeat ")) {
propertyValue += "Repeat ";
} else if (!repeat) {
propertyValue = propertyValue.replaceFirst("Repeat ", "");
if (repeat) {
segments.add(REPEAT);
}
propertyValue += textureName;
property.setValue(propertyValue);
texturePreview.setToolTipText(propertyValue);
}

private static BufferedImage resizeImage(BufferedImage originalImage) {
int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
float ratio = (float) originalImage.getWidth() / (float) originalImage.getHeight();
int width = 80;
int height = 25;
if (ratio <= 1) {
height = (int) ((float) width * ratio);
} else {
width = (int) ((float) height * ratio);
}
BufferedImage resizedImage = new BufferedImage(width, height, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, width, height, null);
g.dispose();

return resizedImage;
segments.add(textureName);
segments.add(extraProperties);
property.setValue(String.join(" ", segments).trim());
texturePreview.setToolTipText(property.getValue());
}

/**
Expand Down Expand Up @@ -282,7 +267,7 @@ private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI

private void texturePreviewMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_texturePreviewMouseClicked
Component view = editor.getCustomEditor();
property.setValue("");
property.setValue(EMPTY);
view.setVisible(true);
if (editor.getValue() != null) {
textureName = "\"" + editor.getAsText() + "\"";
Expand All @@ -300,25 +285,25 @@ private void texturePreviewMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FI
@Override
protected void readProperty() {
java.awt.EventQueue.invokeLater(() -> {
textureName = property.getValue();
if (textureName.contains("Flip ")) {
String prop = property.getValue().trim();
if (prop.contains(FLIP)) {
flip = true;
textureName = textureName.replaceFirst("Flip ", "").trim();
prop = prop.replace(FLIP, EMPTY).trim();
}
if (textureName.contains("Repeat ")) {
if (prop.contains(REPEAT)) {
repeat = true;
textureName = textureName.replaceFirst("Repeat ", "").trim();
prop = prop.replace(REPEAT, EMPTY).trim();
}
property.setValue(textureName);
textureName = "\"" + extractTextureName(prop) + "\"";
extraProperties = prop.replace(textureName, "").trim();


jLabel1.setText(property.getName());
jLabel1.setToolTipText(property.getName());
displayPreview();
texturePreview.setToolTipText(property.getValue());
MaterialProperty prop = property;
property = null;
jCheckBox1.setSelected(flip);
jCheckBox2.setSelected(repeat);
property = prop;
});
}

Expand Down Expand Up @@ -349,12 +334,23 @@ public void setTexture(String name) {
textureName = "\"" + name + "\"";
}
property.setValue(textureName);
displayPreview();

updateFlipRepeat();
displayPreview();
java.awt.EventQueue.invokeLater(() -> {
fireChanged();
});
});

}

// visible for tests
protected boolean isFlip() {
return flip;
}

// visible for tests
protected boolean isRepeat() {
return repeat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@
package com.jme3.gde.materials.multiview.widgets;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.jupiter.api.Test;
import com.jme3.gde.materials.MaterialProperty;
import java.awt.EventQueue;
import java.lang.reflect.InvocationTargetException;
import org.openide.util.Exceptions;



/**
Expand All @@ -44,24 +51,125 @@ public class TexturePanelTest {
public TexturePanelTest() {
}

String textureNameWithModifier = "Flip Repeat \"simple_name.jpg\"";
String textureNameWithSpaces = "\"texture name with spaces.jpg\"";
String textureNameWithSpaceAndModifier = "Flip Repeat \"texture name with spaces.jpg\"";
String textureName = "\"simple_name.jpg\"";
String textureNameWithExtraInfo = "Flip \"simple_name.jpg\" LINEAR";

@Test
public void testExtractTextureName() {
TexturePanel texturePanel = new TexturePanel();
String textureName = "\"simple_name.jpg\"";

String extractedName = texturePanel.extractTextureName(textureName);
assertEquals("simple_name.jpg", extractedName);

String textureNameWithModifier = "Flip Repeat \"simple_name.jpg\"";

extractedName = texturePanel.extractTextureName(textureNameWithModifier);
assertEquals("simple_name.jpg", extractedName);

String textureNameWithSpaces = "\"texture name with spaces.jpg\"";

extractedName = texturePanel.extractTextureName(textureNameWithSpaces);
assertEquals("texture name with spaces.jpg", extractedName);

String textureNameWithSpaceAndModifier = "Flip Repeat \"texture name with spaces.jpg\"";

extractedName = texturePanel.extractTextureName(textureNameWithSpaceAndModifier);
assertEquals("texture name with spaces.jpg", extractedName);

extractedName = texturePanel.extractTextureName(textureNameWithExtraInfo);
assertEquals("simple_name.jpg", extractedName);
}

@Test
public void testReadPropertyFlipRepeat() {
TexturePanel texturePanel = new TexturePanel();
texturePanel.setProperty(new MaterialProperty());
texturePanel.property.setValue(textureNameWithModifier);
try {
EventQueue.invokeAndWait(() -> {
texturePanel.readProperty();
});
} catch (InterruptedException | InvocationTargetException ex) {
Exceptions.printStackTrace(ex);
}


assertEquals(textureName, texturePanel.textureName);
assertTrue(texturePanel.isFlip());
assertTrue(texturePanel.isRepeat());
assertEquals(texturePanel.extraProperties, "");
}

@Test
public void testReadProperty() {
TexturePanel texturePanel = new TexturePanel();
texturePanel.setProperty(new MaterialProperty());
texturePanel.property.setValue(textureName);

try {
EventQueue.invokeAndWait(() -> {
texturePanel.readProperty();
});
} catch (InterruptedException | InvocationTargetException ex) {
Exceptions.printStackTrace(ex);
}

assertEquals(texturePanel.textureName, textureName);
assertFalse(texturePanel.isFlip());
assertFalse(texturePanel.isRepeat());
}

@Test
public void testReadPropertyExtraProperty() {
TexturePanel texturePanel = new TexturePanel();
texturePanel.setProperty(new MaterialProperty());
texturePanel.property.setValue(textureNameWithExtraInfo);
try {
EventQueue.invokeAndWait(() -> {
texturePanel.readProperty();
});
} catch (InterruptedException | InvocationTargetException ex) {
Exceptions.printStackTrace(ex);
}

assertEquals(texturePanel.textureName, textureName);
assertEquals(texturePanel.extraProperties, "LINEAR");
}

@Test
public void testUpdateFlipRepeat() {
TexturePanel texturePanel = new TexturePanel();
texturePanel.setProperty(new MaterialProperty());

texturePanel.property.setValue(textureNameWithModifier);
try {
EventQueue.invokeAndWait(() -> {
texturePanel.readProperty();
});
} catch (InterruptedException | InvocationTargetException ex) {
Exceptions.printStackTrace(ex);
}

texturePanel.updateFlipRepeat();
assertEquals(texturePanel.property.getValue(), textureNameWithModifier);
}

@Test
public void testUpdateFlipRepeatExtraProperty() {
TexturePanel texturePanel = new TexturePanel();
texturePanel.setProperty(new MaterialProperty());
texturePanel.property.setValue(textureNameWithExtraInfo);
try {
EventQueue.invokeAndWait(() -> {
texturePanel.readProperty();
});
} catch (InterruptedException | InvocationTargetException ex) {
Exceptions.printStackTrace(ex);
}

texturePanel.updateFlipRepeat();
assertTrue(texturePanel.property.getValue().contains("LINEAR"));

}

}