Skip to content

Commit

Permalink
Show modal with error if the quest contains objects for Icon Packs no…
Browse files Browse the repository at this point in the history
…t yet installed
  • Loading branch information
adelolmo committed Apr 9, 2024
1 parent 760142a commit ed3af9f
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 57 deletions.
26 changes: 8 additions & 18 deletions src/main/java/org/lightless/heroscribe/gui/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static void removeFromBoardObjectsNotPresentInObjectList(Gui gui) {
final List<Quest.Board.Object> objectsInBoard = board.getObjects();
Arrays.stream(objectsInBoard.toArray(Quest.Board.Object[]::new))
.filter(o -> gui.getObjectList().getOptionalObject(o.getId()).isEmpty())
.forEach(objectsInBoard::remove);
.forEach(board::removeObject);
});
}

Expand All @@ -93,26 +93,15 @@ public void paintComponent(Graphics g) {
}

private Quest.Board.Object getNewObject(boolean floating) {
Quest.Board.Object newObject;
final String id = gui.tools.selectorPanel.getSelectedObject();

if (floating) {
newObject = new Quest.Board.Object();
newObject.setId(id);
newObject.setZorder(Integer.MAX_VALUE);
} else {
newObject = new Quest.Board.Object();
newObject.setId(gui.tools.selectorPanel.getSelectedObject());

final ObjectList.Object obj = gui.getObjectList().getObjectById(id);

newObject.setZorder(obj.getZorder());
}

final ObjectList.Object obj = gui.getObjectList().getObjectById(id);
final Quest.Board.Object newObject = new Quest.Board.Object();
newObject.setId(id);
newObject.setZorder(floating ? Integer.MAX_VALUE : obj.getZorder());
newObject.setRotation(Rotation.fromNumber(rotation));
newObject.setLeft(lastLeft);
newObject.setTop(lastTop);

newObject.setKind(gui.getObjectList().getKind(obj.getKind()));
return newObject;
}

Expand Down Expand Up @@ -263,8 +252,9 @@ public void mousePressed(MouseEvent e) {
Quest.Board.Object obj = getNewObject(false);

if (isWellPositioned(obj))
if (gui.getQuest().getBoard(lastColumn, lastRow).addObject(obj)) {
if (gui.getQuest().getBoard(lastColumn, lastRow).addObjectIfNotPresentInCell(obj)) {
hasAdded = true;
// gui.getQuest().getKinds().add(gui.getObjectList().getKind(obj.getId()));
gui.getQuest().setModified(true);
}
}
Expand Down
61 changes: 48 additions & 13 deletions src/main/java/org/lightless/heroscribe/gui/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,33 @@ HeroScribe Enhanced (changes are prefixed with HSE in comments)

package org.lightless.heroscribe.gui;

import org.lightless.heroscribe.*;
import org.lightless.heroscribe.export.*;
import org.lightless.heroscribe.helper.*;
import org.lightless.heroscribe.iconpack.*;
import org.lightless.heroscribe.Constants;
import org.lightless.heroscribe.Preferences;
import org.lightless.heroscribe.export.ExportEPS;
import org.lightless.heroscribe.export.ExportIPDF;
import org.lightless.heroscribe.export.ExportPDF;
import org.lightless.heroscribe.export.ExportRaster;
import org.lightless.heroscribe.helper.BoardPainter;
import org.lightless.heroscribe.iconpack.IconPackService;
import org.lightless.heroscribe.utils.OS;
import org.lightless.heroscribe.xml.*;
import org.slf4j.*;
import org.lightless.heroscribe.xml.Kind;
import org.lightless.heroscribe.xml.ObjectList;
import org.lightless.heroscribe.xml.Quest;
import org.lightless.heroscribe.xml.QuestParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

import static org.lightless.heroscribe.export.ExportRaster.ImageFormat.*;
import static org.lightless.heroscribe.export.ExportRaster.ImageFormat.PNG;

public class Gui extends JFrame implements WindowListener, ItemListener, ActionListener {

Expand Down Expand Up @@ -504,6 +514,20 @@ public void actionPerformed(ActionEvent e) {
objectList.getBoard().getHeight());

tools.none.doClick();

final Set<Kind> unsupportedKinds = findUnsupportedKinds(newXmlQuest, objectList.getKinds());
if(!unsupportedKinds.isEmpty()) {
JOptionPane.showMessageDialog(this,
"The Quest contains objects not supported.\n\n" +
"Please import the following Icon Pack(s) and try again:\n"
+ unsupportedKinds.stream()
.map(Kind::getName)
.collect(Collectors.joining("\n", "• ", "")),
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}

quest = newXmlQuest;
setMenuRegion();

Expand Down Expand Up @@ -534,7 +558,7 @@ public void actionPerformed(ActionEvent e) {
quest.setFile(file);
}

questParser.saveToDisk(quest, quest.getFile());
questParser.saveToDisk(objectList, quest, quest.getFile());
updateTitle();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this,
Expand All @@ -549,9 +573,7 @@ public void actionPerformed(ActionEvent e) {

if ((file = askPath("xml")) != null) {
try {
quest.setFile(file);
questParser.saveToDisk(quest, file);

questParser.saveToDisk(objectList, quest, file);
updateTitle();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this,
Expand Down Expand Up @@ -725,6 +747,19 @@ public void actionPerformed(ActionEvent e) {

}

private Set<Kind> findUnsupportedKinds(Quest newXmlQuest, List<Kind> kinds) {
final Set<Kind> unsupportedKinds = new HashSet<>();
final List<String> supportedKindIds = kinds.stream()
.map(Kind::getId)
.collect(Collectors.toList());
for (Kind questKind : newXmlQuest.getKinds()) {
if (!supportedKindIds.contains(questKind.getId())) {
unsupportedKinds.add(questKind);
}
}
return unsupportedKinds;
}

private File askPath(String extension) {
fileChooser.resetChoosableFileFilters();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ public void showDialog() {

final ObjectList.Object selectedItem = (ObjectList.Object) wandering.getSelectedItem();
if (!quest.hasWanderingMonster()) {
quest.setWandering(selectedItem.getName(), selectedItem.getId());
quest.setWandering(selectedItem.getId());
quest.setModified(true);
}
if (!selectedItem.getId().equals(quest.getWanderingId())) {
quest.setWandering(selectedItem.getName(), selectedItem.getId());
quest.setWandering(selectedItem.getId());
quest.setModified(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();

if (obj != null) {
gui.getQuest().getBoard(lastColumn, lastRow).getObjects().remove(obj);
gui.getQuest().getBoard(lastColumn, lastRow).removeObject(obj);
selected.remove(obj);
}

Expand All @@ -180,7 +180,7 @@ public void actionPerformed(ActionEvent e) {
}

if (obj != null) {
gui.getQuest().getBoard(lastColumn, lastRow).getObjects().add(obj);
gui.getQuest().getBoard(lastColumn, lastRow).addObject(obj);
selected.add(obj);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ private List<Kind> getNewKindsFromIconPack(final ObjectList referenceObjectList,
final ObjectList iconPackObjectList) {
return iconPackObjectList.getKinds().stream()
.filter(kind -> !referenceObjectList.getKindIds().contains(kind.getId()))
.peek(kind -> kind.setOriginal(false))
.collect(Collectors.toList());
}

Expand Down
34 changes: 29 additions & 5 deletions src/main/java/org/lightless/heroscribe/xml/Kind.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
/*
HeroScribe Enhanced Skull
Copyright (C) 2022 Andoni del Olmo
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 (not
later versions) as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

package org.lightless.heroscribe.xml;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
* @author Andoni del Olmo
* @since 7/4/24
*/
public class Kind {

@JacksonXmlProperty(isAttribute = true)
private String id;
@JacksonXmlProperty(isAttribute = true)
private String name;
@JacksonXmlProperty(isAttribute = true)
private boolean original = true;

public String getId() {
return id;
Expand All @@ -31,6 +47,14 @@ public void setName(String name) {
this.name = name;
}

public boolean isOriginal() {
return original;
}

public void setOriginal(boolean original) {
this.original = original;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -44,7 +68,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37).append(id).append(name).toHashCode();
return new HashCodeBuilder(17, 37).append(id).toHashCode();
}

@Override
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/lightless/heroscribe/xml/ObjectList.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ public ObjectList.Object getObjectById(String id) {
.orElseThrow(() -> new IllegalStateException(format("Unknown object '%s'", id)));
}

public boolean containsObjectById(String id) {
return getOptionalObject(id).isPresent();
}

public Optional<Object> getOptionalObject(String id) {
return objects.stream()
.filter(object -> object.getId().equals(id))
Expand Down Expand Up @@ -200,6 +196,13 @@ public void setBasePath(Path basePath) {
this.basePath = basePath;
}

public Kind getKind(String id) {
return kinds.stream()
.filter(kind -> id.equals(kind.getId()))
.findFirst()
.orElseThrow(() -> new IllegalStateException(format("Unknown kind '%s'", id)));
}

public static class Board {

@JacksonXmlProperty(isAttribute = true)
Expand Down
Loading

0 comments on commit ed3af9f

Please sign in to comment.