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
+86 -37
View File
@@ -1,6 +1,9 @@
package fr.klemek.minimario;
import java.awt.AWTException;
import java.awt.Dialog.ModalExclusionType;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
@@ -8,7 +11,10 @@ import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
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.List;
import java.util.Random;
@@ -19,7 +25,7 @@ import fr.klemek.minimario.LocalServer.ConnectionListener;
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 PopupMenu popup;
@@ -33,24 +39,15 @@ public abstract class Launch {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
LocalServer.startServer(new ConnectionListener(){
@Override
public void onConnection() {
try {
new MarioWindow(null, currentFactor, null);
refreshTray();
} catch (IOException e) {
e.printStackTrace();
}
}
});
new MarioWindow(null, currentFactor, null);
addTrayIcon(MarioWindow.getAll().get(0));
} catch (IOException e) {
e.printStackTrace();
}
LocalServer.startServer(new ConnectionListener(){
@Override
public void onConnection() {
new MarioWindow(null, currentFactor, null);
refreshTray();
}
});
new MarioWindow(null, currentFactor, null);
addTrayIcon(MarioWindow.getAll().get(0));
}
});
}
@@ -87,12 +84,8 @@ public abstract class Launch {
add.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try {
new MarioWindow(null, currentFactor, null);
refreshTray();
} catch (IOException e1) {
e1.printStackTrace();
}
new MarioWindow(null, currentFactor, null);
refreshTray();
}
});
@@ -105,16 +98,12 @@ public abstract class Launch {
MarioWindow.getAll().clear();
currentFactor /= 2;
for(MarioWindow mw:save){
try {
MarioWindow child1 = new MarioWindow(mw.getCenter(), currentFactor, mw.getTilesetName());
MarioWindow child2 = new MarioWindow(mw.getCenter(), currentFactor, mw.getTilesetName());
child1.getAi().setInvicible(true);
child2.getAi().setInvicible(true);
child1.getAi().jump(false);
child2.getAi().jump(true);
} catch (IOException e1) {
e1.printStackTrace();
}
MarioWindow child1 = new MarioWindow(mw.getCenter(), currentFactor, mw.getTilesetName());
MarioWindow child2 = new MarioWindow(mw.getCenter(), currentFactor, mw.getTilesetName());
child1.getAi().setInvicible(true);
child2.getAi().setInvicible(true);
child1.getAi().jump(false);
child2.getAi().jump(true);
mw.kill();
}
save = null;
@@ -143,9 +132,69 @@ public abstract class Launch {
System.exit(0);
}
});
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();
trayIcon.setPopupMenu(popup);
final SystemTray tray = SystemTray.getSystemTray();
try {
+30 -28
View File
@@ -17,7 +17,7 @@ import javax.imageio.ImageIO;
import javax.swing.JWindow;
import javax.swing.Timer;
public class MarioWindow extends JWindow implements ActionListener {
public class MarioWindow extends JWindow{
private static final long serialVersionUID = 797825180779341089L;
@@ -40,21 +40,18 @@ public class MarioWindow extends JWindow implements ActionListener {
//constructor
public MarioWindow(Point start, int factor, String tilesetName) throws IOException {
windows.add(this);
public MarioWindow(Point start, int factor, String tilesetName){
this.setBackground(new Color(0, 0, 0, 0));
//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.refresh = new Timer(REFRESH_MS, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
setLocation(ai.refresh(REFRESH_MS));
p.setTile(ai.getTile(), ai.isReversed());
setAlwaysOnTop(true);
}
});
if(tilesetName == null){
Random r = new Random();
@@ -70,7 +67,24 @@ public class MarioWindow extends JWindow implements ActionListener {
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.addMouseListener(new MouseAdapter() {
@@ -102,8 +116,6 @@ public class MarioWindow extends JWindow implements ActionListener {
this.add(this.p);
this.refresh = new Timer(REFRESH_MS, this);
this.pack();
this.setAlwaysOnTop(true);
@@ -113,6 +125,8 @@ public class MarioWindow extends JWindow implements ActionListener {
this.refresh.start();
windows.add(this);
System.out.println("Spawned "+tilesetName+" at ("+this.getX()+","+this.getY()+")");
}
@@ -122,18 +136,6 @@ public class MarioWindow extends JWindow implements ActionListener {
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
public Point getCenter(){