Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
paulwedeck committed Aug 19, 2023
2 parents ad14987 + f7597a2 commit 9901344
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

import jsettlers.common.CommonConstants;
import jsettlers.common.buildings.EBuildingType;
import jsettlers.common.landscape.ELandscapeType;
import jsettlers.graphics.map.MapDrawContext;
import jsettlers.logic.map.loading.MapLoadException;
import jsettlers.common.menu.FakeMapGame;
Expand Down Expand Up @@ -78,7 +77,7 @@
import jsettlers.mapcreator.main.map.MapEditorControls;
import jsettlers.mapcreator.main.window.EditorFrame;
import jsettlers.mapcreator.main.window.LastUsedHandler;
import jsettlers.mapcreator.main.window.NewFileDialog;
import jsettlers.mapcreator.main.window.newmap.NewFileDialog;
import jsettlers.mapcreator.main.window.OpenExistingDialog;
import jsettlers.mapcreator.main.window.SettingsDialog;
import jsettlers.mapcreator.main.window.sidebar.RectIcon;
Expand Down Expand Up @@ -237,11 +236,11 @@ public void loadMap(MapLoader loader) throws MapLoadException {
*
* @param header
* Header of the file to open
* @param ground
* Ground to use for the new map
* @param data
* Initial state of the map
*/
public void createNewMap(MapFileHeader header, ELandscapeType ground) {
loadMap(header, new MapData(header.getWidth(), header.getHeight(), header.getMaxPlayers(), ground));
public void createNewMap(MapFileHeader header, MapData data) {
loadMap(header, data);
}

/**
Expand Down Expand Up @@ -394,7 +393,7 @@ private void createNewFile() {

// create new control for new map
EditorControl control = new EditorControl();
control.createNewMap(header, dlg.getGroundTypes());
control.createNewMap(header, dlg.getNewMapData());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import jsettlers.logic.map.loading.data.objects.StoneMapDataObject;
import jsettlers.logic.map.loading.newmap.FreshMapSerializer;
import jsettlers.logic.map.loading.newmap.FreshMapSerializer.IMapDataReceiver;
import jsettlers.logic.map.loading.newmap.MapFileHeader;
import jsettlers.mapcreator.data.MapDataDelta.HeightChange;
import jsettlers.mapcreator.data.MapDataDelta.LandscapeChange;
import jsettlers.mapcreator.data.MapDataDelta.ObjectAdder;
Expand Down Expand Up @@ -99,6 +100,10 @@ public class MapData implements IMapData {

private LandscapeEditor landscapeEditor = new LandscapeEditor((pt) -> getLandscape(pt.x, pt.y), (pt, type) -> setLandscape(pt.x, pt.y, type));

public MapData(MapFileHeader header, ELandscapeType ground) {
this(header.getWidth(), header.getHeight(), header.getMaxPlayers(), ground);
}

public MapData(int width, int height, int playerCount, ELandscapeType ground) {
if (width <= 0 || height <= 0) {
throw new IllegalArgumentException("width and height must be positive");
Expand Down Expand Up @@ -315,6 +320,10 @@ public void setHeight(int x, int y, int height) {
}
}

setHeightUnsafe(x, y, height);
}

public void setHeightUnsafe(int x, int y, int height) {
byte safeHeight;
if (height >= Byte.MAX_VALUE) {
safeHeight = Byte.MAX_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import jsettlers.common.resources.ResourceManager;
import jsettlers.logic.map.loading.MapLoadException;
import jsettlers.exceptionhandler.ExceptionHandler;
import jsettlers.main.swing.JSettlersSwingUtil;
import jsettlers.main.swing.SwingManagedJSettlers;
import jsettlers.main.swing.resources.SwingResourceProvider;
import jsettlers.main.swing.settings.SettingsManager;
import jsettlers.mapcreator.control.EditorControl;
import jsettlers.mapcreator.main.window.EditorFrame;
import jsettlers.mapcreator.main.window.NewFilePanel;
import jsettlers.mapcreator.main.window.newmap.NewFilePanel;
import jsettlers.mapcreator.main.window.NewOrOpenDialog;
import jsettlers.mapcreator.main.window.OpenPanel;

Expand Down Expand Up @@ -91,7 +87,7 @@ private static void startWithSelectionDialog() {
} else {
NewFilePanel newFile = dlg.getNewFilePanel();
EditorControl control = new EditorControl();
control.createNewMap(newFile.getHeader(), newFile.getGroundTypes());
control.createNewMap(newFile.getHeader(), newFile.getMapData());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.swing.JTabbedPane;

import jsettlers.mapcreator.localization.EditorLabels;
import jsettlers.mapcreator.main.window.newmap.NewFilePanel;

/**
* Display a dialog to create a new map or open an existing one, displayed at startup
Expand Down
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);
}
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());
}
}
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;
}
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*******************************************************************************/
package jsettlers.mapcreator.main.window;
package jsettlers.mapcreator.main.window.newmap;

import java.awt.BorderLayout;

import javax.swing.JFrame;

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.AbstractOkCancelDialog;

/**
* Display new file dialog
Expand Down Expand Up @@ -55,8 +56,8 @@ public NewFileDialog(JFrame parent) {
/**
* @return The selected ground type
*/
public ELandscapeType getGroundTypes() {
return newFilePanel.getGroundTypes();
public MapData getNewMapData() {
return newFilePanel.getMapData();
}

/**
Expand Down
Loading

0 comments on commit 9901344

Please sign in to comment.