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