[plugins] Added plugins support to Mage.Client (jspf). Created Mage.Theme.Plugin. Please run Mage.Client/install_3d_party_libs.sh (.bat) once to put library to local maven repository.

This commit is contained in:
magenoxx 2010-10-31 12:44:34 +00:00
parent 4d6c70e390
commit b356d2c7c1
22 changed files with 508 additions and 50 deletions

View file

@ -0,0 +1,2 @@
rem installs 3d party libs to local repository
mvn install:install-file -Dfile=plugins/core/jspf.core-0.9.0.jar -DgroupId=org.plugins.jspf -DartifactId=jspf-core -Dversion=0.9.1 -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true

View file

@ -0,0 +1,2 @@
# installs 3d party libs to local repository
mvn install:install-file -Dfile=plugins/core/jspf.core-0.9.0.jar -DgroupId=org.plugins.jspf -DartifactId=jspf-core -Dversion=0.9.1 -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true

Binary file not shown.

Binary file not shown.

View file

@ -32,6 +32,11 @@
<artifactId>Mage-Sets</artifactId>
<version>${mage-version}</version>
</dependency>
<dependency>
<groupId>org.plugins.jspf</groupId>
<artifactId>jspf-core</artifactId>
<version>${jspf-version}</version>
</dependency>
</dependencies>
<build>
@ -44,6 +49,11 @@
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
@ -77,5 +87,6 @@
<properties>
<mage-version>0.3</mage-version>
<jspf-version>0.9.1</jspf-version>
</properties>
</project>

View file

@ -37,23 +37,31 @@ package mage.client;
import java.awt.Cursor;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javax.swing.Box;
import javax.swing.JDesktopPane;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import mage.client.dialog.AboutDialog;
import mage.client.dialog.CombatDialog;
import mage.client.dialog.ConnectDialog;
import mage.client.dialog.PickNumberDialog;
import mage.client.plugins.impl.Plugins;
import mage.client.remote.Session;
import mage.client.util.EDTExceptionHandler;
import mage.interfaces.plugin.ThemePlugin;
import mage.util.Logging;
import net.xeoh.plugins.base.PluginManager;
import net.xeoh.plugins.base.impl.PluginManagerFactory;
import net.xeoh.plugins.base.util.PluginManagerUtil;
/**
*
@ -103,6 +111,7 @@ public class MageFrame extends javax.swing.JFrame {
logger.log(Level.SEVERE, null, ex);
}
Plugins.getInstance().loadPlugins();
initComponents();
@ -344,6 +353,7 @@ public class MageFrame extends javax.swing.JFrame {
private void exitApp() {
session.disconnect();
Plugins.getInstance().shutdown();
dispose();
System.exit(0);
}

View file

@ -38,13 +38,11 @@ import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
@ -55,10 +53,10 @@ import mage.client.dialog.ExileZoneDialog;
import mage.client.dialog.PickChoiceDialog;
import mage.client.dialog.ShowCardsDialog;
import mage.client.game.FeedbackPanel.FeedbackMode;
import mage.client.plugins.impl.Plugins;
import mage.client.remote.Session;
import mage.client.util.Config;
import mage.client.util.gui.ArrowBuilder;
import mage.client.util.gui.ImagePanel;
import mage.util.Logging;
import mage.view.AbilityPickerView;
import mage.view.CardsView;
@ -86,8 +84,8 @@ public class GamePanel extends javax.swing.JPanel {
//FIXME: remove from here
try {
BufferedImage background = ImageIO.read(this.getClass().getResourceAsStream("/green.jpg"));
ImagePanel bgPanel = new ImagePanel(background, ImagePanel.TILED);
/*BufferedImage background = ImageIO.read(this.getClass().getResourceAsStream("/green.jpg"));
ImagePanel bgPanel = new ImagePanel(background, ImagePanel.TILED);*/
// Override layout (I can't edit generated code)
this.setLayout(new BorderLayout());
@ -95,16 +93,11 @@ public class GamePanel extends javax.swing.JPanel {
j.add(ArrowBuilder.getArrowsPanel(), JLayeredPane.MODAL_LAYER);
j.setSize(1024,768);
//j.setBorder(BorderFactory.createLineBorder(Color.green));
//this.add(j);
this.add(j);
j.add(jSplitPane1, JLayeredPane.DEFAULT_LAYER);
jSplitPane1.setOpaque(false);
pnlBattlefield.setOpaque(false);
jPanel3.setOpaque(false);
hand.setOpaque(false);
chatPanel.setOpaque(false);
bgPanel.add(j);
this.add(bgPanel);
Map<String, JComponent> ui = getUIComponents(j);
Plugins.getInstance().updateGamePanel(ui);
// Enlarge jlayeredpane on resize
addComponentListener(new ComponentAdapter(){
@ -125,6 +118,20 @@ public class GamePanel extends javax.swing.JPanel {
}
}
private Map<String, JComponent> getUIComponents(JLayeredPane jLayeredPane) {
Map<String, JComponent> components = new HashMap<String, JComponent>();
components.put("jSplitPane1", jSplitPane1);
components.put("pnlBattlefield", pnlBattlefield);
components.put("jPanel3", jPanel3);
components.put("hand", hand);
components.put("chatPanel", chatPanel);
components.put("jLayeredPane", jLayeredPane);
components.put("gamePanel", this);
return components;
}
public void cleanUp() {
MageFrame.getCombatDialog().hideDialog();
MageFrame.getPickNumberDialog().hide();

View file

@ -0,0 +1,11 @@
package mage.client.plugins;
import java.util.Map;
import javax.swing.JComponent;
public interface MagePlugins {
void loadPlugins();
void shutdown();
void updateGamePanel(Map<String, JComponent> ui);
}

View file

@ -0,0 +1,50 @@
package mage.client.plugins.impl;
import java.io.File;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import mage.client.plugins.MagePlugins;
import mage.client.util.Constants;
import mage.interfaces.plugin.ThemePlugin;
import mage.util.Logging;
import net.xeoh.plugins.base.PluginManager;
import net.xeoh.plugins.base.impl.PluginManagerFactory;
import net.xeoh.plugins.base.util.PluginManagerUtil;
public class Plugins implements MagePlugins {
private static final MagePlugins fINSTANCE = new Plugins();
private static PluginManager pm;
private final static Logger logger = Logging.getLogger(Plugins.class.getName());
public static MagePlugins getInstance() {
return fINSTANCE;
}
@Override
public void loadPlugins() {
logger.log(Level.INFO, "Loading plugins...");
pm = PluginManagerFactory.createPluginManager();
pm.addPluginsFrom(new File(Constants.PLUGINS_DIRECTORY).toURI());
logger.log(Level.INFO, "Done.");
}
@Override
public void shutdown() {
if (pm != null) pm.shutdown();
}
@Override
public void updateGamePanel(Map<String, JComponent> ui) {
PluginManagerUtil pmu = new PluginManagerUtil(pm);
for (ThemePlugin pl : pmu.getPlugins(ThemePlugin.class)) {
pl.apply(ui);
}
}
}

View file

@ -68,4 +68,6 @@ public final class Constants {
public static final int DAMAGE_MAX_LEFT = 180;
public static final double SCALE_FACTOR = 0.5;
public static final String PLUGINS_DIRECTORY = "plugins/";
}

View file

@ -22,6 +22,11 @@
<artifactId>Mage</artifactId>
<version>${mage-version}</version>
</dependency>
<dependency>
<groupId>org.plugins.jspf</groupId>
<artifactId>jspf-core</artifactId>
<version>${jspf-version}</version>
</dependency>
</dependencies>
<build>
@ -50,5 +55,6 @@
<properties>
<mage-version>0.3</mage-version>
<jspf-version>0.9.1</jspf-version>
</properties>
</project>

View file

@ -0,0 +1,13 @@
package mage.interfaces.plugin;
import net.xeoh.plugins.base.Plugin;
/**
* Interface for card plugins
*
* @version 0.1 31.10.2010
* @author nantuko
*/
public interface CardPlugin extends Plugin {
}

View file

@ -0,0 +1,17 @@
package mage.interfaces.plugin;
import java.util.Map;
import javax.swing.JComponent;
import net.xeoh.plugins.base.Plugin;
/**
* Interface for theme plugins
*
* @version 0.1 31.10.2010
* @author nantuko
*/
public interface ThemePlugin extends Plugin {
void apply(Map<String, JComponent> ui);
}

View file

@ -10,7 +10,7 @@
</parent>
<groupId>org.mage</groupId>
<artifactId>mage-player-aiminimax</artifactId>
<artifactId>Mage-Player-AIMinimax</artifactId>
<packaging>jar</packaging>
<version>0.3</version>
<name>Mage Player AI Minimax</name>

View file

@ -0,0 +1,46 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mage</groupId>
<artifactId>Mage-Plugins</artifactId>
<version>${mage-version}</version>
</parent>
<groupId>org.mage</groupId>
<artifactId>Mage-Card-Plugin</artifactId>
<packaging>jar</packaging>
<version>${plugin-version}</version>
<name>Mage Card Plugin</name>
<description>Plugin for drawing card</description>
<dependencies>
<dependency>
<groupId>org.plugins.jspf</groupId>
<artifactId>jspf-core</artifactId>
<version>${jspf-version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<finalName>mage-card-plugin</finalName>
</build>
<properties>
<plugin-version>0.3</plugin-version>
<jspf-version>0.9.1</jspf-version>
</properties>
</project>

View file

@ -0,0 +1,81 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mage</groupId>
<artifactId>Mage-Plugins</artifactId>
<version>${mage-version}</version>
</parent>
<groupId>org.mage</groupId>
<artifactId>Mage-Theme-Plugin</artifactId>
<packaging>jar</packaging>
<version>${plugin-version}</version>
<name>Mage Theme Plugin</name>
<description>Contains resources for drawing background</description>
<dependencies>
<dependency>
<groupId>org.mage</groupId>
<artifactId>Mage-Common</artifactId>
<version>${mage-version}</version>
</dependency>
<dependency>
<groupId>org.plugins.jspf</groupId>
<artifactId>jspf-core</artifactId>
<version>${jspf-version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<artifactSet>
<includes>
<include>log4j:log4j:jar:</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>mage-theme-plugin</finalName>
</build>
<properties>
<mage-version>0.3</mage-version>
<plugin-version>0.3</plugin-version>
<jspf-version>0.9.1</jspf-version>
</properties>
</project>

View file

@ -0,0 +1,105 @@
package org.mage.plugins.component;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
@SuppressWarnings("serial")
public class ImagePanel extends JPanel {
public static final int TILED = 0;
public static final int SCALED = 1;
public static final int ACTUAL = 2;
private BufferedImage image;
private int style;
private float alignmentX = 0.5f;
private float alignmentY = 0.5f;
public ImagePanel(BufferedImage image) {
this(image, TILED);
}
public ImagePanel(BufferedImage image, int style) {
this.image = image;
this.style = style;
setLayout(new BorderLayout());
}
public void setImageAlignmentX(float alignmentX) {
this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX;
}
public void setImageAlignmentY(float alignmentY) {
this.alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f : alignmentY;
}
public void add(JComponent component) {
add(component, null);
}
public void add(JComponent component, Object constraints) {
component.setOpaque(false);
if (component instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane) component;
JViewport viewport = scrollPane.getViewport();
viewport.setOpaque(false);
Component c = viewport.getView();
if (c instanceof JComponent) {
((JComponent) c).setOpaque(false);
}
}
super.add(component, constraints);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image == null)
return;
switch (style) {
case TILED:
drawTiled(g);
break;
case SCALED:
Dimension d = getSize();
g.drawImage(image, 0, 0, d.width, d.height, null);
break;
case ACTUAL:
drawActual(g);
break;
}
}
private void drawTiled(Graphics g) {
Dimension d = getSize();
int width = image.getWidth(null);
int height = image.getHeight(null);
for (int x = 0; x < d.width; x += width) {
for (int y = 0; y < d.height; y += height) {
g.drawImage(image, x, y, null, null);
}
}
}
private void drawActual(Graphics g) {
Dimension d = getSize();
float x = (d.width - image.getWidth()) * alignmentX;
float y = (d.height - image.getHeight()) * alignmentY;
g.drawImage(image, (int) x, (int) y, this);
}
}

View file

@ -0,0 +1,71 @@
package org.mage.plugins.theme;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import mage.interfaces.plugin.ThemePlugin;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import net.xeoh.plugins.base.annotations.events.Init;
import net.xeoh.plugins.base.annotations.events.PluginLoaded;
import net.xeoh.plugins.base.annotations.meta.Author;
import org.apache.log4j.Logger;
import org.mage.plugins.component.ImagePanel;
@PluginImplementation
@Author(name="nantuko")
public class ThemePluginImpl implements ThemePlugin {
private final static Logger log = Logger.getLogger(ThemePluginImpl.class);
@Init
public void init() {
}
@PluginLoaded
public void newPlugin(ThemePlugin plugin) {
log.info(plugin.toString() + " has been loaded.");
}
public String toString() {
return "[Theme plugin by nantuko, version 0.1]";
}
public void apply(Map<String, JComponent> ui) {
log.info("Adding background...");
try {
BufferedImage background = ImageIO.read(this.getClass().getResourceAsStream("/green.jpg"));
if (background == null)
throw new FileNotFoundException("Couldn't find green.jpg in resources.");
if (ui.containsKey("gamePanel") && ui.containsKey("jLayeredPane")) {
ImagePanel bgPanel = new ImagePanel(background, ImagePanel.TILED);
unsetOpaque(ui.get("jSplitPane1"));
unsetOpaque(ui.get("pnlBattlefield"));
unsetOpaque(ui.get("jPanel3"));
unsetOpaque(ui.get("hand"));
unsetOpaque(ui.get("chatPanel"));
ui.get("gamePanel").remove(ui.get("jLayeredPane"));
bgPanel.add(ui.get("jLayeredPane"));
ui.get("gamePanel").add(bgPanel);
} else {
log.error("error: no components");
}
} catch (Exception e) {
log.error(e.getMessage(), e);
return;
}
log.info("Done.");
}
private void unsetOpaque(JComponent c) {
if (c != null) c.setOpaque(false);
}
}

View file

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

View file

@ -0,0 +1,8 @@
#default levels
log4j.rootLogger=debug, console
#console log
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p [%d{yyyy-MM-dd HH:mm [ss:SSS]}] %C{1}[%t]: %m%n
log4j.appender.console.Threshold=DEBUG

25
Mage.Plugins/pom.xml Normal file
View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mage</groupId>
<artifactId>mage-root</artifactId>
<version>${mage-version}</version>
</parent>
<groupId>org.mage</groupId>
<artifactId>Mage-Plugins</artifactId>
<version>${mage-version}</version>
<packaging>pom</packaging>
<name>Mage Plugins</name>
<description>Mage Plugins POM</description>
<modules>
<module>Mage.Theme.Plugin</module>
<module>Mage.Card.Plugin</module>
</modules>
</project>

15
pom.xml
View file

@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mage</groupId>
<artifactId>mage-root</artifactId>
<version>0.3</version>
<version>${mage-version}</version>
<packaging>pom</packaging>
<name>Mage Root</name>
<description>Mage Root POM</description>
@ -22,20 +23,10 @@
<module>Mage.Player.AI</module>
<module>Mage.Player.AIMinimax</module>
<module>Mage.Player.Human</module>
<module>Mage.Plugins</module>
</modules>
<profiles>
<profile>
<id>normal-build</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
<properties>
<mage-version>0.3</mage-version>
</properties>
</project>