Added the function of drawing rectangular hitboxes, as well as the ability to erase and save the result. You need to draw from left to right

This commit is contained in:
2026-03-29 22:47:57 +03:00
parent a0a69c3587
commit 2686d036cf
13 changed files with 190 additions and 180 deletions

BIN
res/NewHueta.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 867 B

View File

@ -2,12 +2,12 @@
<objecttypes>
<objecttype color="#a0a0a4" name="misato">
<property default="Solid" name="class" type="string"/>
<property default="15 4 217 0 209 197 30 180 " name="drawbox" type="string"/>
<property default="9 3 214 11 220 190 15 185 " name="drawbox" type="string"/>
<property default="Circle 4 6 24" name="hitbox" type="string"/>
</objecttype>
<objecttype color="#a0a0a4" name="starlight">
<property default="Solid" name="class" type="string"/>
<property default="2 1 253 1 254 468 2 490" name="drawbox" type="string"/>
<property default="11 1 196 0 181 141 26 131 " name="drawbox" type="string"/>
<property default="Rectangle 0 0 76 119" name="hitbox" type="string"/>
</objecttype>
<objecttype color="#a0a0a4" name="tavern">
@ -22,7 +22,7 @@
</objecttype>
<objecttype color="000000" name="newtest">
<property default="solid" name="class" type="string"/>
<property default="93 15 404 57 389 162 207 164 " name="drawbox" type="string"/>
<property default="Rectangle 0 0 0 0" name="hitbox" type="string"/>
<property default="252 116 505 120 500 247 235 237 " name="drawbox" type="string"/>
<property default="Rectangle 80 -607 27 49" name="hitbox" type="string"/>
</objecttype>
</objecttypes>

View File

@ -46,20 +46,4 @@ public class DrawboxEditor extends Editable {
logger.setLevel(Level.CONFIG);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
}

View File

@ -22,7 +22,7 @@ import javax.swing.event.ListSelectionListener;
import model.Entity;
import repository.Project;
public abstract class Editable extends JPanel implements MouseListener, MouseMotionListener, ListSelectionListener {
public abstract class Editable extends JPanel implements ListSelectionListener {
protected ListGUI listGUI;
protected Entity entity;
@ -66,22 +66,6 @@ public abstract class Editable extends JPanel implements MouseListener, MouseMot
// из mouse motion listener'a
@Override
public void mouseDragged(MouseEvent e) {}
// этих ребят нас обязывает создать MouseListener, так что они здесь
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void valueChanged(ListSelectionEvent e) {
logger.log(Level.FINEST, "entering method {0} of class {1}", new Object[]{"MouseClicked()" , this.getClass().getName()});

View File

@ -31,12 +31,14 @@ public class EditableCanvas extends JPanel implements MouseListener, MouseMotion
hitboxRectengleRenderFunct = new HitboxRectengleRenderingFunction();
hitboxCircleRenderFunct = new HitboxCircleRenderingFunction();
drawboxRectengleRenderFunct.subscribe(Project.getInstance()); // подписка: Project получит данные ввиде обьекта Event, содержащий данные drawbox при отрисовке последней точки из 4-х.
//hitboxPanel.subscribe(Project.getInstance());
hitboxRectengleRenderFunct.subscribe(Project.getInstance());
addMouseListener(this);
addMouseMotionListener(this);
}
public HitboxRectengleRenderingFunction getHitboxRectengleRenderFunct() {
return hitboxRectengleRenderFunct;
}
public void drawing(Graphics2D g) {
if(renderingFunction!=null&&entity!=null) {
@ -58,21 +60,12 @@ public class EditableCanvas extends JPanel implements MouseListener, MouseMotion
public void mouseMoved(MouseEvent e) {
//System.out.println("moved");
if(renderingFunction!=null&&entity!=null) {
System.out.println("x="+e.getX()+"y="+e.getY());
//System.out.println("x="+e.getX()+"y="+e.getY());
renderingFunction.mouseMoved(e);
repaint();
}
}
@Override
public void mouseClicked(MouseEvent e) {
//System.out.println("Clicked");
if(renderingFunction!=null&&entity!=null) {
renderingFunction.mouseClicked(e);
repaint();
}
}
@Override
public void mousePressed(MouseEvent e) {
//System.out.println("mousePressed");
@ -94,15 +87,16 @@ public class EditableCanvas extends JPanel implements MouseListener, MouseMotion
public void setEntity(Entity e) {
entity = e;
drawboxRectengleRenderFunct.setEntity(e);
drawboxRectengleRenderFunct.setEntityInDrawboxRectengle(e);
hitboxRectengleRenderFunct.setEntityInHtiboxRectengle(e);
}
public void setImage(BufferedImage image) {
this.image = image;
}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseDragged(MouseEvent e) {}
@Override

View File

@ -14,84 +14,35 @@ import events.EntityDrawboxChangedListener;
import events.EntityHitboxChangedEvent;
import events.EntityHitboxChangedListener;
import model.Hitbox;
import model.HitboxRectangle;
import model.Point;
public class HitboxEditor extends Editable {
Point firstIsoPoint = null;
Point currentIsoPoint = new Point(0,0);
Point firstCartesianPoint = new Point(0,0), currentCartesianPoint = new Point(0,0);
private List<EntityHitboxChangedListener> listeners = new ArrayList<>();
HitboxEditor(ListGUI listGUI) {
super(listGUI);
}
//@Override
//public void drawing(Graphics2D g) {
/*Hitbox nowHitbox = entity.getHitbox();
//if(firstIsoPoint != null) {
int x1,y1,x2,y2;
int size = nowHitbox.getListPointsIso().size();
for(int i = 0; i < size;i++) {
x1 = (int)nowHitbox.getListPointsIso().get(i % size).x;
y1 = (int)nowHitbox.getListPointsIso().get(i % size).y;
x2 = (int)nowHitbox.getListPointsIso().get((i+1) % size).x;
y2 = (int)nowHitbox.getListPointsIso().get((i+1) % size).y;
g.drawLine(x1, y1, x2, y2);
}
x1 = (int)nowHitbox.getListPointsIso().get(0).x;
y1 = (int)nowHitbox.getListPointsIso().get(0).y;
x2 = (int)nowHitbox.getListPointsIso().get(3).x;
y2 = (int)nowHitbox.getListPointsIso().get(3).y;
g.drawLine(x1, y1, x2, y2);
//}
*/
//}
canvas = new EditableCanvas();
add(canvas);
canvas.setHitboxRectengleRenderingFunction();
canvas.setVisible(true);
@Override
public void mouseClicked(MouseEvent e) {
if(firstIsoPoint == null) {
firstIsoPoint = new Point(currentIsoPoint.x,currentIsoPoint.y);
//System.out.println("firstPoint("+firstIsoPoint.x+";"+firstIsoPoint.y+");");
firstCartesianPoint = Hitbox.isometricToCartesian(firstIsoPoint.x, firstIsoPoint.y,firstCartesianPoint);
}else{
/*
* В данном месте при нажатии закрепляющей точки, необходимо вызывать функцию,
* которая будет формировать из текущих декартовых координат:
* 1. Точку старта и ширину с высотой.
* 2. Так же необходимо реализовать слушатель сохранения новых хитбоксов в дерево.
*
* */
repaint();
}
}
@Override
public void mouseMoved(MouseEvent e) {
/*currentIsoPoint.x = e.getX();
currentIsoPoint.y = e.getY();
if(entity!=null) {
Hitbox nowHitbox = entity.getHitbox();
if(firstIsoPoint != null) {
//System.out.println("firstPoint("+firstIsoPoint.x+";"+firstIsoPoint.y+");");
currentCartesianPoint = Hitbox.isometricToCartesian(currentIsoPoint.x, currentIsoPoint.y, currentCartesianPoint);
nowHitbox.getListPointsCartesian().get(0).setXY(firstCartesianPoint.x, firstCartesianPoint.y);
nowHitbox.getListPointsCartesian().get(1).setXY(currentCartesianPoint.x, firstCartesianPoint.y);
nowHitbox.getListPointsCartesian().get(2).setXY(currentCartesianPoint.x, currentCartesianPoint.y);
nowHitbox.getListPointsCartesian().get(3).setXY(firstCartesianPoint.x, currentCartesianPoint.y);
nowHitbox.convertCartesianPointsToIso();
repaint();
clearLinesJButton.addActionListener((e)->{
if(entity != null) {
HitboxRectangle nowHitbox = (HitboxRectangle)entity.getHitbox();
System.out.println("1. nowHitbox.getListPointsIso().size() = "+ nowHitbox.getListPointsIso().size());
nowHitbox.getListPointsIso().clear();
System.out.println("2. nowHitbox.getListPointsIso().size() = "+ nowHitbox.getListPointsIso().size());
System.out.println("1. nowHitbox.getListPointsCartesian().size() = "+ nowHitbox.getListPointsCartesian().size() );
nowHitbox.getListPointsCartesian().clear();
System.out.println("2. nowHitbox.getListPointsCartesian().size() = "+ nowHitbox.getListPointsCartesian().size());
canvas.getHitboxRectengleRenderFunct().getLocalListPointsCartesian().clear();
canvas.getHitboxRectengleRenderFunct().getLocalListPointsIso().clear();
canvas.getHitboxRectengleRenderFunct().setFirstIsoPointIsNull();
canvas.repaint();
}
}*/
}
public void subscribe(EntityHitboxChangedListener listener) {
listeners.add(listener);
}
public void unsubscribe(EntityHitboxChangedListener listener) {
listeners.remove(listener);
});
}
//НАДО ДОБАВИТЬ ЧТО БЫ ПОД КОНЕЦ РИСОВАНИЯ ХИТБОКСА ОНО ВЫЗЫВАЛО ФУНКЦИЮ И ВСЕ СОХРАНЯЛО.
@ -99,29 +50,7 @@ public class HitboxEditor extends Editable {
//НО НЕ ДОБАВЛЕН ВЫЗОВ ФУНКЦИИ И НЕ ОФОРМЛЕНА ПОДПИСКА
//UPD: вроде подписку оформил в MainGUI;
private void notifySubscribers() {
for (EntityHitboxChangedListener listener : listeners) {
listener.hitboxChanged(
new EntityHitboxChangedEvent(entity.getHitbox(), entity)
);
}
}
//это старая очистка которая пока убрана. В будущем будет убрана вовсе, после рефакторинга.
/*@Override
public void actionPerformed(ActionEvent e) {
JTabbedPane parent = (JTabbedPane) getParent();
if(parent.getSelectedComponent() == this){
if(entity != null) {
firstIsoPoint = null;
currentIsoPoint.setXY(0,0);
repaint();
}
}
}*/
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
//super.valueChanged(e);
}
}

View File

@ -58,10 +58,6 @@ public class MainGUI extends JFrame{
TopButtonBar.setBorder(BorderFactory.createLoweredBevelBorder());
add(TopButtonBar,BorderLayout.NORTH);
//clearLinesJButton.addActionListener(drawBoxPanel); - ранее регистрировались слушатели для кнопки очистки.
//clearLinesJButton.addActionListener(hitboxPanel);
editorPane = new JTabbedPane();
editorPane.setVisible(true);
add(editorPane,BorderLayout.CENTER);

View File

@ -84,11 +84,6 @@ public class DrawboxRectengleRenderingFunction implements ShapeRenderingFunction
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
@ -147,7 +142,7 @@ public class DrawboxRectengleRenderingFunction implements ShapeRenderingFunction
entity.setDrawbox(new Drawbox(drawboxPoints));
}
public void setEntity(Entity e) {
public void setEntityInDrawboxRectengle(Entity e) {
entity = e;
}

View File

@ -17,12 +17,6 @@ public class HitboxCircleRenderingFunction implements ShapeRenderingFunction {
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub

View File

@ -2,31 +2,154 @@ package gui.render;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import events.EntityHitboxChangedEvent;
import events.EntityHitboxChangedListener;
import model.Entity;
import model.Hitbox;
import model.HitboxRectangle;
import model.Point;
public class HitboxRectengleRenderingFunction implements ShapeRenderingFunction {
Point firstIsoPoint = null;
Point currentIsoPoint = new Point(0,0);
Point firstCartesianPoint = new Point(0,0), currentCartesianPoint = new Point(0,0);
private List<EntityHitboxChangedListener> listeners = new ArrayList<>();
Entity entity;
private List<Point> LocalListPointsIso = new ArrayList<Point>();
private List<Point> LocalListPointsCartesian = new ArrayList<Point>();
public List<Point> getLocalListPointsIso() {
return LocalListPointsIso;
}
public List<Point> getLocalListPointsCartesian() {
return LocalListPointsCartesian;
}
public void setFirstIsoPointIsNull(){
firstIsoPoint = null;
}
@Override
public void drawing(Graphics2D g) {
// TODO Auto-generated method stub
HitboxRectangle nowHitbox = (HitboxRectangle)entity.getHitbox();
int x1,y1,x2,y2;
if(nowHitbox.getListPointsIso().size() == 4) {
drawingLinesFromListPoints(nowHitbox.getListPointsIso(),g);
}
if(firstIsoPoint!=null&&nowHitbox.getListPointsIso().size() == 0) {
drawingLinesFromListPoints(LocalListPointsIso,g);
}
}
public void drawingLinesFromListPoints(List<Point> listPointsIso, Graphics2D g) {
int x1,y1,x2,y2;
int size = listPointsIso.size();
for(int i = 0; i < size;i++) {
x1 = (int)listPointsIso.get(i % size).x;
y1 = (int)listPointsIso.get(i % size).y;
x2 = (int)listPointsIso.get((i+1) % size).x;
y2 = (int)listPointsIso.get((i+1) % size).y;
g.drawLine(x1, y1, x2, y2);
}
x1 = (int)listPointsIso.get(0).x;
y1 = (int)listPointsIso.get(0).y;
x2 = (int)listPointsIso.get(3).x;
y2 = (int)listPointsIso.get(3).y;
g.drawLine(x1, y1, x2, y2);
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.println("mousePressed");
//System.out.println("mouseClicked");
HitboxRectangle nowHitbox = (HitboxRectangle)entity.getHitbox();
if(nowHitbox.getListPointsIso().size() == 0 && firstIsoPoint == null) {
initListsPoints();
firstIsoPoint = new Point(currentIsoPoint.x,currentIsoPoint.y);
LocalListPointsIso.get(0).setXY(firstIsoPoint.x, firstIsoPoint.y);
firstCartesianPoint = Hitbox.isometricToCartesian(firstIsoPoint.x, firstIsoPoint.y,firstCartesianPoint);
LocalListPointsCartesian.get(0).setXY(firstCartesianPoint.x, firstCartesianPoint.y);
System.out.println("mouseClicked Event 1 TIME: "+LocalTime.now());
}else if(nowHitbox.getListPointsIso().size() == 0 && firstIsoPoint != null) {
nowHitbox.getListPointsIso().addAll(LocalListPointsIso);
nowHitbox.getListPointsCartesian().addAll(LocalListPointsCartesian);
System.out.println("mouseClicked Event 2 TIME: "+LocalTime.now());
/*
* В данном месте при нажатии закрепляющей точки, необходимо вызывать функцию,
* которая будет формировать из текущих декартовых координат:
* 1. Точку старта и ширину с высотой.
* 2. Так же необходимо реализовать слушатель сохранения новых хитбоксов в дерево.
*
* */
notifySubscribers();
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
currentIsoPoint.x = e.getX();
currentIsoPoint.y = e.getY();
HitboxRectangle nowHitbox = (HitboxRectangle)entity.getHitbox();
if(entity!=null) {
if(firstIsoPoint != null && nowHitbox.getListPointsIso().size() == 0) {
//System.out.println("firstPoint("+firstIsoPoint.x+";"+firstIsoPoint.y+");");
currentCartesianPoint = Hitbox.isometricToCartesian(currentIsoPoint.x, currentIsoPoint.y, currentCartesianPoint);
LocalListPointsCartesian.get(0).setXY(firstCartesianPoint.x, firstCartesianPoint.y);
LocalListPointsCartesian.get(1).setXY(currentCartesianPoint.x, firstCartesianPoint.y);
LocalListPointsCartesian.get(2).setXY(currentCartesianPoint.x, currentCartesianPoint.y);
LocalListPointsCartesian.get(3).setXY(firstCartesianPoint.x, currentCartesianPoint.y);
convertCartesianPointsToIso();
}
}
}
public void setEntityInHtiboxRectengle(Entity e) {
entity = e;
}
public void subscribe(EntityHitboxChangedListener listener) {
listeners.add(listener);
}
public void unsubscribe(EntityHitboxChangedListener listener) {
listeners.remove(listener);
}
private void notifySubscribers() {
for (EntityHitboxChangedListener listener : listeners) {
listener.hitboxChanged(
new EntityHitboxChangedEvent(entity.getHitbox(), entity)
);
}
}
public void initListsPoints() {
if(LocalListPointsIso.size()<1) {
for(int i=0;i<4;i++) {
LocalListPointsIso.add(new Point(currentIsoPoint.x,currentIsoPoint.y));
}
}
if(LocalListPointsCartesian.size()<1) {
for(int i=0;i<4;i++) {
LocalListPointsCartesian.add(new Point(0,0));
}
}
}
public void convertCartesianPointsToIso() {
Point cartesianPoint;
for (int i = 0 ; i<4;i++) {
cartesianPoint = LocalListPointsCartesian.get(i);
Hitbox.cartesianToIsometric(cartesianPoint.x,cartesianPoint.y,LocalListPointsIso.get(i));
}
}
}

View File

@ -6,7 +6,6 @@ import java.awt.event.MouseEvent;
public interface ShapeRenderingFunction {
void drawing(Graphics2D g);
public void mousePressed(MouseEvent e);
public void mouseClicked(MouseEvent e);
public void mouseMoved(MouseEvent e);
//private void notifySubscribers() ?? надо ли? подумать
}

View File

@ -61,7 +61,7 @@ public abstract class Hitbox {
public String getShape() {
return shape;
}
public void calculationReferencePoint() {}
//not the same as toString()! the latter is for XML while printToConsole() is for console
public void printToConsole() {

View File

@ -11,7 +11,6 @@ public class HitboxRectangle extends Hitbox {
public HitboxRectangle(String shape, Entity owner){
super(shape);
setOwnerEntity(owner);
initListsPoints();
}
public HitboxRectangle(String shape,List<Point> listPointsIso,List<Point> listPointsCartesian){
super(shape);
@ -32,7 +31,7 @@ public class HitboxRectangle extends Hitbox {
printToConsole();
}
private void initListsPoints() {
public void initListsPoints() {
if(listPointsIso.size()<1) {
for(int i=0;i<4;i++) {
listPointsIso.add(new Point(0,0));
@ -89,6 +88,19 @@ public class HitboxRectangle extends Hitbox {
}
@Override
public String listPointsToString() {
return "Rectangle "+referencePoint.x+" "+referencePoint.y+" "+widthHitbox+" "+heightHitbox;
System.out.println("Данные");
parseCartesianListPointsToWidthHeight();
calculationReferencePoint();
return "Rectangle "+(int)referencePoint.x+" "+(int)referencePoint.y+" "+(int)widthHitbox+" "+(int)heightHitbox;
}
public void parseCartesianListPointsToWidthHeight() {
widthHitbox = Math.abs(listPointsCartesian.get(0).x-listPointsCartesian.get(1).x);
heightHitbox = Math.abs(listPointsCartesian.get(1).y-listPointsCartesian.get(2).y);
}
@Override
public void calculationReferencePoint() {
referencePoint.x = listPointsIso.get(1).x-owner.getImage().getWidth()/2;
referencePoint.y = listPointsIso.get(1).y-owner.getImage().getHeight();
}
}