forked from jsettlers/settlers-remake
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
10 changed files
with
243 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
jsettlers.mapcreator/src/main/java/jsettlers/mapcreator/main/window/newmap/FilePicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package jsettlers.mapcreator.main.window.newmap; | ||
|
||
import jsettlers.mapcreator.localization.EditorLabels; | ||
|
||
import javax.swing.Box; | ||
import javax.swing.BoxLayout; | ||
import javax.swing.JButton; | ||
import javax.swing.JFileChooser; | ||
import javax.swing.JLabel; | ||
import javax.swing.filechooser.FileFilter; | ||
import java.awt.event.ActionEvent; | ||
import java.awt.event.ActionListener; | ||
import java.io.File; | ||
|
||
public abstract class FilePicker extends Box { | ||
|
||
private final JLabel filename = new JLabel(); | ||
private final JButton dialogButton = new JButton(EditorLabels.getLabel("newfile.select-image")); | ||
private final JFileChooser chooserDialog = new JFileChooser(); | ||
|
||
public FilePicker() { | ||
super(BoxLayout.X_AXIS); | ||
|
||
add(filename); | ||
dialogButton.addActionListener(new ActionListener() { | ||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
int result = chooserDialog.showOpenDialog(FilePicker.this); | ||
if(result != JFileChooser.APPROVE_OPTION) { | ||
return; | ||
} | ||
File file = chooserDialog.getSelectedFile(); | ||
if(!checkFile(file)) { | ||
return; | ||
} | ||
|
||
filename.setText(file.getName()); | ||
} | ||
}); | ||
add(dialogButton); | ||
} | ||
|
||
protected abstract boolean checkFile(File file); | ||
} |
53 changes: 53 additions & 0 deletions
53
...rs.mapcreator/src/main/java/jsettlers/mapcreator/main/window/newmap/FillNewFilePanel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package jsettlers.mapcreator.main.window.newmap; | ||
|
||
import jsettlers.common.landscape.ELandscapeType; | ||
import jsettlers.logic.map.loading.newmap.MapFileHeader; | ||
import jsettlers.mapcreator.data.MapData; | ||
import jsettlers.mapcreator.localization.EditorLabels; | ||
import jsettlers.mapcreator.main.window.sidebar.RectIcon; | ||
|
||
import javax.swing.DefaultListCellRenderer; | ||
import javax.swing.JComboBox; | ||
import javax.swing.JList; | ||
import javax.swing.JPanel; | ||
import java.awt.Color; | ||
import java.awt.Component; | ||
|
||
public class FillNewFilePanel extends JPanel implements InitialMapProvider { | ||
|
||
|
||
/** | ||
* Available ground types | ||
*/ | ||
private static final ELandscapeType[] GROUND_TYPES = new ELandscapeType[] { ELandscapeType.WATER8, ELandscapeType.GRASS, | ||
ELandscapeType.DRY_GRASS, ELandscapeType.SNOW, ELandscapeType.DESERT }; | ||
|
||
/** | ||
* Selected ground type | ||
*/ | ||
private final JComboBox<ELandscapeType> groundTypes; | ||
|
||
public FillNewFilePanel() { | ||
this.groundTypes = new JComboBox<>(GROUND_TYPES); | ||
add(groundTypes); | ||
groundTypes.setRenderer(new DefaultListCellRenderer() { | ||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { | ||
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); | ||
|
||
ELandscapeType type = (ELandscapeType) value; | ||
setIcon(new RectIcon(22, new Color(type.color.getARGB()), Color.GRAY)); | ||
setText(EditorLabels.getLabel("landscape." + type.name())); | ||
|
||
return this; | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public MapData getMapData(MapFileHeader header) { | ||
return new MapData(header, (ELandscapeType) groundTypes.getSelectedItem()); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...rs.mapcreator/src/main/java/jsettlers/mapcreator/main/window/newmap/ImageImportPanel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package jsettlers.mapcreator.main.window.newmap; | ||
|
||
import jsettlers.common.Color; | ||
import jsettlers.common.landscape.ELandscapeType; | ||
import jsettlers.logic.map.loading.newmap.MapFileHeader; | ||
import jsettlers.mapcreator.data.MapData; | ||
import jsettlers.mapcreator.tools.buffers.ByteMapArea; | ||
|
||
import javax.imageio.ImageIO; | ||
import javax.swing.Box; | ||
import javax.swing.BoxLayout; | ||
import javax.swing.ImageIcon; | ||
import javax.swing.JLabel; | ||
import javax.swing.JOptionPane; | ||
import java.awt.Dimension; | ||
import java.awt.Graphics; | ||
import java.awt.Image; | ||
import java.awt.image.BufferedImage; | ||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
public class ImageImportPanel extends Box implements InitialMapProvider { | ||
|
||
private BufferedImage currentImage; | ||
|
||
|
||
private final FilePicker picker = new FilePicker() { | ||
@Override | ||
protected boolean checkFile(File file) { | ||
return checkImageFile(file); | ||
} | ||
}; | ||
|
||
private final JLabel imagePreview = new JLabel(); | ||
|
||
public ImageImportPanel() { | ||
super(BoxLayout.Y_AXIS); | ||
add(picker); | ||
|
||
imagePreview.setMaximumSize(new Dimension(256, 256)); | ||
add(imagePreview); | ||
} | ||
|
||
private boolean checkImageFile(File file) { | ||
if(!file.exists()) return false; | ||
|
||
BufferedImage content; | ||
try { | ||
content = ImageIO.read(file); | ||
} catch (IOException e) { | ||
JOptionPane.showMessageDialog(this, e); | ||
return false; | ||
} | ||
|
||
currentImage = content; | ||
|
||
imagePreview.setIcon(new ImageIcon(currentImage)); | ||
return true; | ||
} | ||
|
||
@Override | ||
public MapData getMapData(MapFileHeader header) { | ||
if(currentImage == null) return null; | ||
|
||
int width = header.getWidth(); | ||
int height = header.getHeight(); | ||
|
||
Image scaledImg = currentImage.getScaledInstance(width, height, Image.SCALE_SMOOTH); | ||
BufferedImage templateImg = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); | ||
Graphics graph = templateImg.getGraphics(); | ||
graph.drawImage(scaledImg, 0, 0, null); | ||
graph.dispose(); | ||
|
||
MapData data = new MapData(header, ELandscapeType.WATER8); | ||
|
||
byte[][] grassArea = new byte[width][height]; | ||
for(int y = 0; y < height; y++) { | ||
for(int x = 0; x < width; x++) { | ||
if((templateImg.getRGB(x, y)&0x0000FF00) > 0) { | ||
grassArea[x][y] = Byte.MAX_VALUE; | ||
} | ||
} | ||
} | ||
|
||
data.fill(ELandscapeType.GRASS, new ByteMapArea(grassArea)); | ||
|
||
for(int y = 0; y < height; y++) { | ||
for(int x = 0; x < width; x++) { | ||
Color c = Color.getABGR(templateImg.getRGB(x, y)); | ||
if(c.getGreen() > 0.1f) { | ||
data.setHeightUnsafe(x, y, (int) ((1.f-c.toGreyScale().getGreen())*255)); | ||
} | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
....mapcreator/src/main/java/jsettlers/mapcreator/main/window/newmap/InitialMapProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package jsettlers.mapcreator.main.window.newmap; | ||
|
||
import jsettlers.logic.map.loading.newmap.MapFileHeader; | ||
import jsettlers.mapcreator.data.MapData; | ||
|
||
public interface InitialMapProvider { | ||
MapData getMapData(MapFileHeader header); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.