1.7.5 : Clicking on the mario tray icon spawn another one

This commit is contained in:
Kleme
2017-09-14 21:09:07 +02:00
parent 0ec98f66a2
commit 982b719f43
2 changed files with 116 additions and 65 deletions
+69 -20
View File
@@ -1,6 +1,9 @@
package fr.klemek.minimario; package fr.klemek.minimario;
import java.awt.AWTException; import java.awt.AWTException;
import java.awt.Dialog.ModalExclusionType;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Menu; import java.awt.Menu;
import java.awt.MenuItem; import java.awt.MenuItem;
import java.awt.PopupMenu; import java.awt.PopupMenu;
@@ -8,7 +11,10 @@ import java.awt.SystemTray;
import java.awt.TrayIcon; import java.awt.TrayIcon;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.IOException; import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@@ -19,7 +25,7 @@ import fr.klemek.minimario.LocalServer.ConnectionListener;
public abstract class Launch { public abstract class Launch {
private static final String VERSION = "1.7.4"; private static final String VERSION = "1.7.5";
private static TrayIcon trayIcon; private static TrayIcon trayIcon;
private static PopupMenu popup; private static PopupMenu popup;
@@ -33,24 +39,15 @@ public abstract class Launch {
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {
@Override @Override
public void run() { public void run() {
try {
LocalServer.startServer(new ConnectionListener(){ LocalServer.startServer(new ConnectionListener(){
@Override @Override
public void onConnection() { public void onConnection() {
try {
new MarioWindow(null, currentFactor, null); new MarioWindow(null, currentFactor, null);
refreshTray(); refreshTray();
} catch (IOException e) {
e.printStackTrace();
}
} }
}); });
new MarioWindow(null, currentFactor, null); new MarioWindow(null, currentFactor, null);
addTrayIcon(MarioWindow.getAll().get(0)); addTrayIcon(MarioWindow.getAll().get(0));
} catch (IOException e) {
e.printStackTrace();
}
} }
}); });
} }
@@ -87,12 +84,8 @@ public abstract class Launch {
add.addActionListener(new ActionListener(){ add.addActionListener(new ActionListener(){
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try {
new MarioWindow(null, currentFactor, null); new MarioWindow(null, currentFactor, null);
refreshTray(); refreshTray();
} catch (IOException e1) {
e1.printStackTrace();
}
} }
}); });
@@ -105,16 +98,12 @@ public abstract class Launch {
MarioWindow.getAll().clear(); MarioWindow.getAll().clear();
currentFactor /= 2; currentFactor /= 2;
for(MarioWindow mw:save){ for(MarioWindow mw:save){
try {
MarioWindow child1 = new MarioWindow(mw.getCenter(), currentFactor, mw.getTilesetName()); MarioWindow child1 = new MarioWindow(mw.getCenter(), currentFactor, mw.getTilesetName());
MarioWindow child2 = new MarioWindow(mw.getCenter(), currentFactor, mw.getTilesetName()); MarioWindow child2 = new MarioWindow(mw.getCenter(), currentFactor, mw.getTilesetName());
child1.getAi().setInvicible(true); child1.getAi().setInvicible(true);
child2.getAi().setInvicible(true); child2.getAi().setInvicible(true);
child1.getAi().jump(false); child1.getAi().jump(false);
child2.getAi().jump(true); child2.getAi().jump(true);
} catch (IOException e1) {
e1.printStackTrace();
}
mw.kill(); mw.kill();
} }
save = null; save = null;
@@ -144,8 +133,68 @@ public abstract class Launch {
} }
}); });
final Frame frame = new Frame("MiniMario");
frame.setResizable(false);
frame.setUndecorated(true);
frame.setType(Frame.Type.UTILITY);
frame.setAlwaysOnTop(true);
frame.setAutoRequestFocus(true);
frame.add(popup);
frame.setVisible(false);
frame.addFocusListener(new FocusListener(){
@Override
public void focusGained(FocusEvent e) {
frame.setVisible(false);
}
@Override
public void focusLost(FocusEvent e) {
frame.setVisible(false);
}
});
trayIcon.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
switch(e.getButton()){
case MouseEvent.BUTTON1:
if(e.getClickCount() == 2){
new MarioWindow(null, currentFactor, null);
refreshTray();
}
break;
case MouseEvent.BUTTON3:
EventQueue.invokeLater(new Runnable(){
@Override
public void run() {
frame.setVisible(true);
popup.show(frame, e.getXOnScreen(), e.getYOnScreen());
frame.setVisible(false);
}
});
break;
}
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
});
//trayIcon.setPopupMenu(popup);
refreshTray(); refreshTray();
trayIcon.setPopupMenu(popup);
final SystemTray tray = SystemTray.getSystemTray(); final SystemTray tray = SystemTray.getSystemTray();
try { try {
+30 -28
View File
@@ -17,7 +17,7 @@ import javax.imageio.ImageIO;
import javax.swing.JWindow; import javax.swing.JWindow;
import javax.swing.Timer; import javax.swing.Timer;
public class MarioWindow extends JWindow implements ActionListener { public class MarioWindow extends JWindow{
private static final long serialVersionUID = 797825180779341089L; private static final long serialVersionUID = 797825180779341089L;
@@ -40,21 +40,18 @@ public class MarioWindow extends JWindow implements ActionListener {
//constructor //constructor
public MarioWindow(Point start, int factor, String tilesetName) throws IOException { public MarioWindow(Point start, int factor, String tilesetName){
windows.add(this);
this.setBackground(new Color(0, 0, 0, 0)); this.setBackground(new Color(0, 0, 0, 0));
//this.setLocationRelativeTo(null); this.refresh = new Timer(REFRESH_MS, new ActionListener(){
if(start == null) @Override
this.setLocation(Utils.randomScreenLocation(TILE_W*factor, TILE_H*factor)); public void actionPerformed(ActionEvent e) {
else setLocation(ai.refresh(REFRESH_MS));
this.setLocation((int)(start.x + TILE_W*factor/2f), (int)( start.y + TILE_H*factor/2f)); p.setTile(ai.getTile(), ai.isReversed());
setAlwaysOnTop(true);
this.factor = factor; }
});
this.ai = new MarioAI(TILE_W*factor,TILE_H*factor, factor);
if(tilesetName == null){ if(tilesetName == null){
Random r = new Random(); Random r = new Random();
@@ -70,7 +67,24 @@ public class MarioWindow extends JWindow implements ActionListener {
this.tilesetName = tilesetName; this.tilesetName = tilesetName;
BufferedImage tileset = ImageIO.read(this.getClass().getResource("/"+tilesetName+".png")); BufferedImage tileset;
try {
tileset = ImageIO.read(this.getClass().getResource("/"+tilesetName+".png"));
} catch (IOException e1) {
e1.printStackTrace();
return;
}
//this.setLocationRelativeTo(null);
if(start == null)
this.setLocation(Utils.randomScreenLocation(TILE_W*factor, TILE_H*factor));
else
this.setLocation((int)(start.x + TILE_W*factor/2f), (int)( start.y + TILE_H*factor/2f));
this.factor = factor;
this.ai = new MarioAI(TILE_W*factor,TILE_H*factor, factor);
this.p = new TilePanel(this, tileset, TILE_W, TILE_H, factor); this.p = new TilePanel(this, tileset, TILE_W, TILE_H, factor);
this.p.addMouseListener(new MouseAdapter() { this.p.addMouseListener(new MouseAdapter() {
@@ -102,8 +116,6 @@ public class MarioWindow extends JWindow implements ActionListener {
this.add(this.p); this.add(this.p);
this.refresh = new Timer(REFRESH_MS, this);
this.pack(); this.pack();
this.setAlwaysOnTop(true); this.setAlwaysOnTop(true);
@@ -113,6 +125,8 @@ public class MarioWindow extends JWindow implements ActionListener {
this.refresh.start(); this.refresh.start();
windows.add(this);
System.out.println("Spawned "+tilesetName+" at ("+this.getX()+","+this.getY()+")"); System.out.println("Spawned "+tilesetName+" at ("+this.getX()+","+this.getY()+")");
} }
@@ -122,18 +136,6 @@ public class MarioWindow extends JWindow implements ActionListener {
this.setVisible(false); this.setVisible(false);
} }
//events
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(this.refresh)) {
this.setLocation(this.ai.refresh(REFRESH_MS));
this.p.setTile(this.ai.getTile(), this.ai.isReversed());
this.setAlwaysOnTop(true);
}
}
//getter/setter //getter/setter
public Point getCenter(){ public Point getCenter(){