Storing sprites in Entity objects now, moderate ListGUI refactoring to simplify list icons creation. See more in the task description https://trello.com/c/tkAjRpcG
This commit is contained in:
@ -43,7 +43,6 @@ public class ListGUI extends JPanel {
|
||||
JButton addListElementEntity;
|
||||
JButton removeListElementEntity;
|
||||
JButton addPicEntity;
|
||||
String pathImage;
|
||||
JList list;
|
||||
JScrollPane scroll;
|
||||
ActionListener removeEntity;
|
||||
@ -91,7 +90,7 @@ public class ListGUI extends JPanel {
|
||||
return (String) list.getSelectedValue();
|
||||
}
|
||||
|
||||
public void addListElement(String name,String solid) throws DuplicateEntryException {
|
||||
public void addListElement(String name, String solid) throws DuplicateEntryException {
|
||||
// плейсхолдеры для новых хитбоксов и дроубоксов, иначе всё валится с NPE
|
||||
List<Point> hitboxPoints = new LinkedList<Point>();
|
||||
List<Point> drawboxPoints = new LinkedList<Point>();
|
||||
@ -107,13 +106,15 @@ public class ListGUI extends JPanel {
|
||||
}
|
||||
|
||||
public void updateList() {
|
||||
String[] nameList = createNameList();
|
||||
testModel.removeAllElements();
|
||||
for (String name : nameList)
|
||||
testModel.addElement(name);
|
||||
|
||||
for (Entity e: Project.getInstance()) {
|
||||
testModel.addElement(e.getName());
|
||||
|
||||
pathImage = Project.getInstance().getXMLPath();
|
||||
createImageMap(nameList);
|
||||
// Create and store JList items icons in a map to prevent their unneccessary re-creation
|
||||
iconMap.put(e.getName(), createListIconFromSprite( e.getImage() ));
|
||||
}
|
||||
|
||||
list.updateUI();
|
||||
}
|
||||
|
||||
@ -136,7 +137,7 @@ public class ListGUI extends JPanel {
|
||||
}
|
||||
private final float iconMaxWidth = 40, iconMaxHeight = 40;
|
||||
|
||||
public Icon imageScaling(BufferedImage image) {
|
||||
private Icon createListIconFromSprite(BufferedImage image) {
|
||||
CustomIcon icon = null;
|
||||
try {
|
||||
int imageWidth = image.getWidth(), imageHeight = image.getHeight();
|
||||
@ -184,14 +185,6 @@ public class ListGUI extends JPanel {
|
||||
}
|
||||
}
|
||||
|
||||
private void createImageMap(String[] nameList) {
|
||||
for (int i = 0; i < nameList.length; i++) {
|
||||
String name = nameList[i];
|
||||
BufferedImage image = Project.getInstance().loadImageByName(name);
|
||||
iconMap.put(name, imageScaling(image));
|
||||
}
|
||||
}
|
||||
|
||||
private JButton createButton(int width,int height,ActionListener listener,String pathImage) {
|
||||
JButton button = new JButton(new ImageIcon(pathImage));
|
||||
button.setSize(110, 46);
|
||||
|
||||
@ -1,10 +1,13 @@
|
||||
package model;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class Entity {
|
||||
private String thisName;
|
||||
private Drawbox thisDrawbox;
|
||||
private Hitbox thisHitbox;
|
||||
private String type;
|
||||
private BufferedImage sprite;
|
||||
|
||||
public Entity(String name,String drawbox,String hitbox) {
|
||||
thisName = new String(name);
|
||||
@ -50,6 +53,19 @@ public class Entity {
|
||||
return thisHitbox;
|
||||
};
|
||||
|
||||
public Drawbox getThisDrawbox() {
|
||||
return thisDrawbox;
|
||||
}
|
||||
|
||||
/** @return BufferedImage object or null if an image is not set */
|
||||
public BufferedImage getImage() {
|
||||
return sprite;
|
||||
}
|
||||
|
||||
public void setImage(BufferedImage sprite) {
|
||||
this.sprite = sprite;
|
||||
}
|
||||
|
||||
public void PrintEntity() {
|
||||
System.out.println("---------------------");
|
||||
System.out.println("Name: "+thisName);
|
||||
|
||||
@ -132,6 +132,7 @@ public class Project implements Iterable<Entity>, EntityDrawboxChangedListener {
|
||||
factory = DocumentBuilderFactory.newInstance();
|
||||
builder = factory.newDocumentBuilder();
|
||||
document = builder.parse(new File(path+fileName));
|
||||
// TODO: remove this from Project, it should not know SHIT about MainGUI
|
||||
if(Launcher.getMainGUI() != null) // at the first program launch, main gui creates list gui before static link to main gui is set
|
||||
Launcher.getMainGUI().setTitle("Hitbox/Drawbox Editor: " + path + fileName);
|
||||
// Получение списка всех элементов objecttype внутри корневого элемента (getDocumentElement возвращает ROOT элемент XML файла).
|
||||
@ -144,14 +145,18 @@ public class Project implements Iterable<Entity>, EntityDrawboxChangedListener {
|
||||
}
|
||||
}
|
||||
|
||||
// эта колбаса парсит сущности из XML в программные объекты внутри модели, является частью внутренней кухни так что обычно её не нужно трогать
|
||||
private void parsingElementXMLtoElementList(String entityName,Node objecttype) {
|
||||
private void parsingElementXMLtoElementList(String entityName, Node objecttype) {
|
||||
String newDrawbox = null;
|
||||
String newHitbox = null;
|
||||
String type = null;
|
||||
String type = null;
|
||||
BufferedImage sprite = null;
|
||||
Element element = (Element)objecttype;
|
||||
NodeList propertyElements = element.getElementsByTagName("property");
|
||||
if(propertyElements!=null) {
|
||||
if(propertyElements != null) {
|
||||
|
||||
// do not lazy load images here - Hitbox parser needs real image sizes
|
||||
sprite = loadImageByName(entityName);
|
||||
|
||||
for(int i = 0; i < propertyElements.getLength(); i++) {
|
||||
Element property = (Element)propertyElements.item(i);
|
||||
String propertyName = property.getAttribute("name");
|
||||
@ -168,8 +173,10 @@ public class Project implements Iterable<Entity>, EntityDrawboxChangedListener {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Entity e = new Entity(entityName,newDrawbox,newHitbox);
|
||||
|
||||
Entity e = new Entity(entityName, newDrawbox, newHitbox);
|
||||
e.setType(type);
|
||||
e.setImage(sprite);
|
||||
listEntity.add(e);
|
||||
}
|
||||
}
|
||||
@ -179,9 +186,6 @@ public class Project implements Iterable<Entity>, EntityDrawboxChangedListener {
|
||||
* XML-файл. Возвращает null если изображение не найдено.
|
||||
* */
|
||||
public BufferedImage loadImageByName(String name) {
|
||||
//TODO: сделать кеширование - не дело подгружать одну и ту же картинку по десять раз!
|
||||
|
||||
String path = Project.getInstance().getXMLPath();
|
||||
String extension = "png";
|
||||
// TODO: изображения следует подгружать в отдельном потоке!
|
||||
|
||||
@ -209,6 +213,9 @@ public class Project implements Iterable<Entity>, EntityDrawboxChangedListener {
|
||||
if(getEntityByName(e.getName()) != null)
|
||||
throw new DuplicateEntryException("The entity with the name '" + e.getName() + "' already exists!");
|
||||
|
||||
//TODO: move image loading to AddListElementEntityListener and make it lazy
|
||||
e.setImage(loadImageByName(e.getName()));
|
||||
|
||||
listEntity.add(e);
|
||||
|
||||
Element objecttypeElement = document.createElement("objecttype");
|
||||
@ -365,8 +372,9 @@ public class Project implements Iterable<Entity>, EntityDrawboxChangedListener {
|
||||
return null;
|
||||
}
|
||||
|
||||
//https://stackoverflow.com/a/64659614/6929164
|
||||
public static void stripEmptyElements(Node node)
|
||||
// https://stackoverflow.com/a/64659614/6929164
|
||||
// new empty lines will appear on every XML save without this function
|
||||
private static void stripEmptyElements(Node node)
|
||||
{
|
||||
NodeList children = node.getChildNodes();
|
||||
for(int i = 0; i < children.getLength(); ++i) {
|
||||
|
||||
Reference in New Issue
Block a user