Skip to content

Commit

Permalink
Some fixes in the model panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Retera committed Aug 13, 2022
1 parent 0ed2e36 commit 10ca6fa
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.hiveworkshop.wc3.gui.modeledit;

import java.awt.Component;
import java.awt.Font;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.HashMap;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JList;

import com.hiveworkshop.wc3.mdl.Bitmap;
import com.hiveworkshop.wc3.mdl.EditableModel;

public class BitmapListCellRenderer extends DefaultListCellRenderer {
private static final Bitmap NO_VALUE_BITMAP = new Bitmap("Textures\\White.blp");
EditableModel model;
HashMap<Bitmap, ImageIcon> map = new HashMap<Bitmap, ImageIcon>();
Font theFont = new Font("Arial", Font.BOLD, 32);

public BitmapListCellRenderer(final EditableModel model) {
this.model = model;
}

@Override
public Component getListCellRendererComponent(final JList list, Object value, final int index, final boolean iss,
final boolean chf) {
String name;
if (value == null) {
name = "(No value)";
// the no value helps with the map cache
value = NO_VALUE_BITMAP;
}
else {
name = ((Bitmap) value).getName();
}
ImageIcon myIcon = map.get(value);
if (myIcon == null) {
final BufferedImage bufferedImage = ((Bitmap) value).getBufferedImage(model.getWrappedDataSource());
if (bufferedImage != null) {
myIcon = new ImageIcon(bufferedImage.getScaledInstance(64, 64, Image.SCALE_SMOOTH));
map.put((Bitmap) value, myIcon);
}
}
super.getListCellRendererComponent(list, name, index, iss, chf);
setIcon(myIcon);
setFont(theFont);

return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public void valueChanged(final TreeSelectionEvent e) {
}

public void reloadFromModelView() {
System.out.println("Reloading ModelComponentBrowserTree");
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.hiveworkshop.wc3.gui.modeledit;

import java.awt.Component;
import java.awt.Font;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;

import com.hiveworkshop.wc3.mdl.EditableModel;
import com.hiveworkshop.wc3.mdl.TextureAnim;

public class TextureAnimListCellRenderer extends DefaultListCellRenderer {
Font theFont = new Font("Arial", Font.BOLD, 32);
private final EditableModel model;

public TextureAnimListCellRenderer(final EditableModel model) {
this.model = model;
}

@Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index,
final boolean iss, final boolean chf) {
String name;
if (value == null) {
name = "(No value)";
}
else {
name = "TextureAnim " + model.getTextureAnimId((TextureAnim) value);
}
super.getListCellRendererComponent(list, name, index, iss, chf);
setFont(theFont);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.hiveworkshop.wc3.gui.modeledit.actions.componenttree.material;

import com.hiveworkshop.wc3.gui.modeledit.UndoAction;
import com.hiveworkshop.wc3.gui.modeledit.actions.newsys.ModelStructureChangeListener;
import com.hiveworkshop.wc3.mdl.Bitmap;
import com.hiveworkshop.wc3.mdl.Layer;
import com.hiveworkshop.wc3.mdl.ShaderTextureTypeHD;

public class SetLayerBitmapAction implements UndoAction {

private final Layer layer;
private final ShaderTextureTypeHD shaderTextureTypeHD;
private final Bitmap prevBitmap;
private final Bitmap newBitmap;
private final ModelStructureChangeListener modelStructureChangeListener;

public SetLayerBitmapAction(final Layer layer, final ShaderTextureTypeHD shaderTextureTypeHD,
final Bitmap prevBitmap, final Bitmap newBitmap,
final ModelStructureChangeListener modelStructureChangeListener) {
this.layer = layer;
this.shaderTextureTypeHD = shaderTextureTypeHD;
this.prevBitmap = prevBitmap;
this.newBitmap = newBitmap;
this.modelStructureChangeListener = modelStructureChangeListener;
}

@Override
public void undo() {
if (prevBitmap == null) {
layer.getShaderTextures().remove(shaderTextureTypeHD);
}
else {
layer.getShaderTextures().put(shaderTextureTypeHD, prevBitmap);
}
modelStructureChangeListener.texturesChanged();
}

@Override
public void redo() {
if (newBitmap == null) {
layer.getShaderTextures().remove(shaderTextureTypeHD);
}
else {
layer.getShaderTextures().put(shaderTextureTypeHD, newBitmap);
}
modelStructureChangeListener.texturesChanged();
}

@Override
public String actionName() {
return "set Layer Bitmap";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.hiveworkshop.wc3.gui.modeledit.actions.componenttree.material;

import com.hiveworkshop.wc3.gui.modeledit.UndoAction;
import com.hiveworkshop.wc3.gui.modeledit.actions.newsys.ModelStructureChangeListener;
import com.hiveworkshop.wc3.mdl.Layer;

public class SetLayerCoordIdAction implements UndoAction {
private final Layer layer;
private final int prevCoordId;
private final int newCoordId;
private final ModelStructureChangeListener modelStructureChangeListener;

public SetLayerCoordIdAction(final Layer layer, final int prevCoordId, final int newCoordId,
final ModelStructureChangeListener modelStructureChangeListener) {
this.layer = layer;
this.prevCoordId = prevCoordId;
this.newCoordId = newCoordId;
this.modelStructureChangeListener = modelStructureChangeListener;
}

@Override
public void undo() {
layer.setCoordId(prevCoordId);
modelStructureChangeListener.texturesChanged();
}

@Override
public void redo() {
layer.setCoordId(newCoordId);
modelStructureChangeListener.texturesChanged();
}

@Override
public String actionName() {
return "set Layer CoordId";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.hiveworkshop.wc3.gui.modeledit.actions.componenttree.material;

import com.hiveworkshop.wc3.gui.modeledit.UndoAction;
import com.hiveworkshop.wc3.gui.modeledit.actions.newsys.ModelStructureChangeListener;
import com.hiveworkshop.wc3.mdl.Layer;
import com.hiveworkshop.wc3.mdl.TextureAnim;

public class SetLayerTextureAnimAction implements UndoAction {
private final Layer layer;
private final TextureAnim prevTexAnim;
private final TextureAnim newTexAnim;
private final ModelStructureChangeListener modelStructureChangeListener;

public SetLayerTextureAnimAction(final Layer layer, final TextureAnim prevTexAnim, final TextureAnim newTexAnim,
final ModelStructureChangeListener modelStructureChangeListener) {
this.layer = layer;
this.prevTexAnim = prevTexAnim;
this.newTexAnim = newTexAnim;
this.modelStructureChangeListener = modelStructureChangeListener;
}

@Override
public void undo() {
layer.setTextureAnim(prevTexAnim);
modelStructureChangeListener.texturesChanged();
}

@Override
public void redo() {
layer.setTextureAnim(newTexAnim);
modelStructureChangeListener.texturesChanged();
}

@Override
public String actionName() {
return "set Layer TVertexAnim";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,40 @@
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpinnerNumberModel;

import com.hiveworkshop.wc3.gui.BLPHandler;
import com.hiveworkshop.wc3.gui.datachooser.DataSource;
import com.hiveworkshop.wc3.gui.modeledit.BitmapListCellRenderer;
import com.hiveworkshop.wc3.gui.modeledit.TextureAnimListCellRenderer;
import com.hiveworkshop.wc3.gui.modeledit.actions.componenttree.material.SetLayerBitmapAction;
import com.hiveworkshop.wc3.gui.modeledit.actions.componenttree.material.SetLayerCoordIdAction;
import com.hiveworkshop.wc3.gui.modeledit.actions.componenttree.material.SetLayerFilterModeAction;
import com.hiveworkshop.wc3.gui.modeledit.actions.componenttree.material.SetLayerShaderAction;
import com.hiveworkshop.wc3.gui.modeledit.actions.componenttree.material.SetLayerTextureAnimAction;
import com.hiveworkshop.wc3.gui.modeledit.actions.newsys.ModelStructureChangeListener;
import com.hiveworkshop.wc3.gui.modeledit.activity.UndoActionListener;
import com.hiveworkshop.wc3.gui.modeledit.components.editors.ColorValuePanel;
import com.hiveworkshop.wc3.gui.modeledit.components.editors.ComponentEditorJSpinner;
import com.hiveworkshop.wc3.gui.modeledit.components.editors.ComponentEditorTextField;
import com.hiveworkshop.wc3.gui.modeledit.components.editors.FloatValuePanel;
import com.hiveworkshop.wc3.mdl.Bitmap;
import com.hiveworkshop.wc3.mdl.EditableModel;
import com.hiveworkshop.wc3.mdl.Layer;
import com.hiveworkshop.wc3.mdl.Layer.FilterMode;
import com.hiveworkshop.wc3.mdl.LayerShader;
import com.hiveworkshop.wc3.mdl.ShaderTextureTypeHD;
import com.hiveworkshop.wc3.mdl.TextureAnim;
import com.hiveworkshop.wc3.mdl.v2.ModelViewManager;
import com.hiveworkshop.wc3.util.IconUtils;
import com.hiveworkshop.wc3.util.ModelUtils;

Expand All @@ -53,6 +65,7 @@ public class ComponentLayerPanel extends JPanel {
private UndoActionListener undoActionListener;
private ModelStructureChangeListener modelStructureChangeListener;
private boolean listenersEnabled = true;
private ModelViewManager modelViewManager;

public ComponentLayerPanel() {
setLayout(new MigLayout("fill", "", "[fill][fill]"));
Expand Down Expand Up @@ -107,16 +120,66 @@ public void actionPerformed(final ActionEvent e) {
textureButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {

final EditableModel model = modelViewManager.getModel();
final ArrayList<Bitmap> textures = model.getTextures();
final DefaultListModel<Bitmap> bitmapListModel = new DefaultListModel<>();
bitmapListModel.addElement(null);
for (final Bitmap tex : textures) {
bitmapListModel.addElement(tex);
}
final JList<Bitmap> bitmapList = new JList<>(bitmapListModel);
bitmapList.setCellRenderer(new BitmapListCellRenderer(model));
if (JOptionPane.showConfirmDialog(ComponentLayerPanel.this.getRootPane(),
new JScrollPane(bitmapList), "Choose BItmap",
JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
final Bitmap selectedValue = bitmapList.getSelectedValue();
final SetLayerBitmapAction setLayerBitmapAction = new SetLayerBitmapAction(layer,
shaderTextureTypeHD, layer.getShaderTextures().get(shaderTextureTypeHD), selectedValue,
modelStructureChangeListener);
setLayerBitmapAction.redo();
undoActionListener.pushAction(setLayerBitmapAction);
}
}
});
leftHandSettingsPanel.add(textureButton, "wrap, growx");
textureTypeToButton.put(shaderTextureTypeHD, textureButton);
}
coordIdSpinner = new ComponentEditorJSpinner(
new SpinnerNumberModel(0, Integer.MIN_VALUE, Integer.MAX_VALUE, 1));
coordIdSpinner.addActionListener(new Runnable() {
@Override
public void run() {
final SetLayerCoordIdAction setLayerCoordIdAction = new SetLayerCoordIdAction(layer, layer.getCoordId(),
((Number) coordIdSpinner.getValue()).intValue(), modelStructureChangeListener);
setLayerCoordIdAction.redo();
undoActionListener.pushAction(setLayerCoordIdAction);
}
});
leftHandSettingsPanel.add(new JLabel("TVertex Anim:"));
tVertexAnimButton = new JButton("Choose TVertex Anim");
tVertexAnimButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
final EditableModel model = modelViewManager.getModel();
final ArrayList<TextureAnim> textureAnims = model.getTexAnims();
final DefaultListModel<TextureAnim> textureAnimsListModel = new DefaultListModel<>();
textureAnimsListModel.addElement(null);
for (final TextureAnim texAnim : textureAnims) {
textureAnimsListModel.addElement(texAnim);
}
final JList<TextureAnim> textureAnimList = new JList<>(textureAnimsListModel);
textureAnimList.setCellRenderer(new TextureAnimListCellRenderer(model));
if (JOptionPane.showConfirmDialog(ComponentLayerPanel.this.getRootPane(),
new JScrollPane(textureAnimList), "Choose TextureAnim",
JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
final TextureAnim selectedValue = textureAnimList.getSelectedValue();
final SetLayerTextureAnimAction setLayerTextureAnimAction = new SetLayerTextureAnimAction(layer,
layer.getTextureAnim(), selectedValue, modelStructureChangeListener);
setLayerTextureAnimAction.redo();
undoActionListener.pushAction(setLayerTextureAnimAction);
}
}
});
leftHandSettingsPanel.add(tVertexAnimButton, "wrap, growx");
leftHandSettingsPanel.add(new JLabel("CoordID:"));
leftHandSettingsPanel.add(coordIdSpinner, "wrap, growx");
Expand All @@ -136,7 +199,8 @@ public void actionPerformed(final ActionEvent e) {

public void setLayer(final DataSource workingDirectory, final Layer layer, final int formatVersion,
final UndoActionListener undoActionListener,
final ModelStructureChangeListener modelStructureChangeListener) {
final ModelStructureChangeListener modelStructureChangeListener, final ModelViewManager modelViewManager) {
this.modelViewManager = modelViewManager;
final boolean hdShader = layer.getLayerShader() == LayerShader.HD;
listenersEnabled = false;
this.layer = layer;
Expand All @@ -148,13 +212,18 @@ public void setLayer(final DataSource workingDirectory, final Layer layer, final
final JButton textureButton = textureTypeToButton.get(shaderTextureTypeHD);

final Bitmap textureBitmap = layer.getShaderTextures().get(shaderTextureTypeHD);
boolean hasIcon = false;
if (textureBitmap != null) {
final BufferedImage image = BLPHandler.getImage(textureBitmap, workingDirectory);
if (image != null) {
textureButton.setIcon(new ImageIcon(IconUtils.worldEditStyleIcon(image)));
hasIcon = true;
}
}
textureButton.setText(layer.getTextureName(textureBitmap));
if (!hasIcon) {
textureButton.setIcon(null);
}
textureButton.setText(layer.getTextureName(textureBitmap, "None"));
if (shaderTextureTypeHD != ShaderTextureTypeHD.Diffuse) {
textureButton.setVisible(hdShader);
}
Expand All @@ -163,7 +232,8 @@ public void setLayer(final DataSource workingDirectory, final Layer layer, final
label.setVisible(hdShader);
}
coordIdSpinner.reloadNewValue(layer.getCoordId());
tVertexAnimButton.setText(layer.getTextureAnim() == null ? "None" : layer.getTextureAnim().toString());
tVertexAnimButton.setText(layer.getTextureAnim() == null ? "None"
: ("TextureAnim " + modelViewManager.getModel().getTextureAnimId(layer.getTextureAnim())));
alphaPanel.reloadNewValue((float) layer.getStaticAlpha(), layer.getFlag("Alpha"));
emissiveGainPanel.setVisible(ModelUtils.isEmissiveLayerSupported(formatVersion) && hdShader);
emissiveGainPanel.reloadNewValue((float) layer.getEmissive(), layer.getFlag("EmissiveGain"));
Expand Down
Loading

0 comments on commit 10ca6fa

Please sign in to comment.