Update to FRoundedPanel:

- Can now decide which corners to round.
- No settings file required in skin (all removed)
This commit is contained in:
Doublestrike
2011-10-10 11:57:39 +00:00
parent 3a60fffb6e
commit 0b46ea60e3
5 changed files with 150 additions and 238 deletions

2
.gitattributes vendored
View File

@@ -9396,7 +9396,6 @@ res/images/skins/default/btnRup.png -text
res/images/skins/default/font1.ttf -text res/images/skins/default/font1.ttf -text
res/images/skins/default/font2.ttf -text res/images/skins/default/font2.ttf -text
res/images/skins/default/palette.jpg -text res/images/skins/default/palette.jpg -text
res/images/skins/default/settings.txt -text
res/images/skins/default/texture1.jpg -text res/images/skins/default/texture1.jpg -text
res/images/skins/default/texture2.jpg -text res/images/skins/default/texture2.jpg -text
res/images/skins/default/texture3.jpg -text res/images/skins/default/texture3.jpg -text
@@ -9413,7 +9412,6 @@ res/images/skins/rebel/btnRup.png -text
res/images/skins/rebel/font1.ttf -text res/images/skins/rebel/font1.ttf -text
res/images/skins/rebel/font2.ttf -text res/images/skins/rebel/font2.ttf -text
res/images/skins/rebel/palette.jpg -text res/images/skins/rebel/palette.jpg -text
res/images/skins/rebel/settings.txt -text
res/images/skins/rebel/texture1.jpg -text res/images/skins/rebel/texture1.jpg -text
res/images/skins/rebel/texture2.jpg -text res/images/skins/rebel/texture2.jpg -text
res/images/skins/rebel/texture3.jpg -text res/images/skins/rebel/texture3.jpg -text

View File

@@ -1,16 +0,0 @@
# Settings are stored in a key=value format.
# Color values are stored as RR,GG,BB[,AA] values 0 to 255
# Dimension values are stored as x,y
# Incomplete settings will make the file switch to the default theme.
# Texture opacity settings, 0-100
texture1opacity=100;
texture2opacity=100;
texture3opacity=100;
# FRoundedPanel
shadowColor=150,150,150,150
borderColor=0,0,0
shadowDistance=5,5
shadowThickness=5
cornerDiameter=20

View File

@@ -1,16 +0,0 @@
# Settings are stored in a key=value format.
# Color values are stored as RR,GG,BB[,AA] values 0 to 255
# Dimension values are stored as x,y
# Incomplete settings will make the file switch to the default theme.
# Texture opacity settings, 0-100
texture1opacity=100;
texture2opacity=100;
texture3opacity=100;
# FRoundedPanel
shadowColor=150,150,150,150
borderColor=0,0,0
shadowDistance=5,5
shadowThickness=5
cornerDiameter=20

View File

@@ -1,34 +1,28 @@
package forge.gui.skin; package forge.gui.skin;
import java.awt.BasicStroke;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.LayoutManager; import java.awt.LayoutManager;
import java.awt.RenderingHints; import java.awt.RenderingHints;
import javax.swing.JPanel; import javax.swing.JPanel;
import forge.AllZone;
/** <p>FRoundedPanel.</p> /** <p>FRoundedPanel.</p>
* A subclass of JPanel with optional rounded corners and * A subclass of JPanel with any of four corners rounded,
* optional drop shadow, for special cases only. FPanel recommended for regular use. * drop shadow, and 1px line border.
* Limitations - cannot use background image, and single line border only.
* *
* Default values provided, later updated from skin settings, and can be * Limitations: Cannot tile background image, cannot set border width.
* set dynamically.
* *
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class FRoundedPanel extends JPanel { public class FRoundedPanel extends JPanel {
private FSkin skin; public boolean[] corners = {true,true,true,true}; //NW, SW, SE, NE
private Color shadowColor = new Color(150,150,150,150); private Color shadowColor = new Color(150,150,150,150);
private Color borderColor = Color.black; private Color borderColor = Color.black;
private Dimension shadowDistance = new Dimension(5,5); private int shadowOffset = 5;
private int shadowThickness = 5; private int cornerRadius = 10;
private int cornerDiameter = 20; private boolean showShadow = false;
/** /**
* <p>FRoundedPanel.</p> * <p>FRoundedPanel.</p>
@@ -38,26 +32,6 @@ public class FRoundedPanel extends JPanel {
public FRoundedPanel() { public FRoundedPanel() {
super(); super();
this.setOpaque(false); this.setOpaque(false);
skin = AllZone.getSkin();
Color tempC;
Dimension tempD;
String tempS;
tempC = parseColorString(skin.getSetting("shadowColor"));
if(tempC != null) { shadowColor = tempC; }
tempC = parseColorString(skin.getSetting("borderColor"));
if(tempC != null) { borderColor = tempC; }
tempD = parseDimensionString(skin.getSetting("shadowDistance"));
if(tempD != null) { shadowDistance = tempD; }
tempS = skin.getSetting("shadowThickness");
if(tempS != null) { shadowThickness = Integer.parseInt(tempS); }
tempS = skin.getSetting("cornerDiameter");
if(tempS != null) { cornerDiameter = Integer.parseInt(tempS); }
} }
/** /**
@@ -81,37 +55,131 @@ public class FRoundedPanel extends JPanel {
super.paintComponent(g); super.paintComponent(g);
int w = getWidth(); int w = getWidth();
int h = getHeight(); int h = getHeight();
int so = shadowOffset;
int r = cornerRadius;
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); RenderingHints.VALUE_ANTIALIAS_ON);
// Draw shadow if(showShadow) {
g2d.setColor(shadowColor); // Mid, left, right rectangles: shadow
g2d.setStroke(new BasicStroke(shadowThickness)); g2d.setColor(shadowColor);
g2d.drawRoundRect( g2d.fillRect(r + so, so, w - 2*r - so, h - so);
0 + (int)shadowDistance.getWidth() + (shadowThickness/2), g2d.fillRect(so, r + so, r, h - 2*r - so);
0 + (int)shadowDistance.getHeight() + (shadowThickness/2), g2d.fillRect(w - r, r + so, r, h - 2*r - so);
w - (int)shadowDistance.getWidth() - shadowThickness,
h - (int)shadowDistance.getHeight() - shadowThickness,
cornerDiameter, cornerDiameter);
// Draw content rectangle (on top of shadow) // Corners: shadow
g2d.setColor(this.getBackground()); // NW
g2d.fillRoundRect( if(corners[0]) {
0, 0, g2d.fillArc(so, so, 2*r, 2*r, 90, 90);
w - shadowThickness, }
h - shadowThickness, else {
cornerDiameter, cornerDiameter); g2d.fillRect(so, so, r, r);
}
// SW
if(corners[1]) {
g2d.fillArc(so, h - 2*r, 2*r, 2*r, 180, 90);
}
else {
g2d.fillRect(so, h - r, r, r);
}
// SE
if(corners[2]) {
g2d.fillArc(w - 2*r, h - 2*r, 2*r, 2*r, 270, 90);
}
else {
g2d.fillRect(w - r, h - r, r, r);
}
// NE
if(corners[3]) {
g2d.fillArc(w - 2*r, so, 2*r, 2*r, 0, 90);
}
else {
g2d.fillRect(w - r, so, r, r);
}
} // End if(showShadow)
else {
so = 0;
so = 0;
}
// Stroke border // Mid, left, right rectangles: content
g2d.setColor(getBackground());
g2d.fillRect(r, 0, w - 2*r - so, h - so);
g2d.fillRect(0, r, r, h - 2*r - so);
g2d.fillRect(w - r - so, r, r, h - 2*r - so);
// Corners: content
// NW
if(corners[0]) {
g2d.fillArc(0, 0, 2*r, 2*r, 90, 90);
}
else {
g2d.fillRect(0, 0, r, r);
}
// SW
if(corners[1]) {
g2d.fillArc(0, h - 2*r - so, 2*r, 2*r, 180, 90);
}
else {
g2d.fillRect(0, h - r - so, r, r);
}
// SE
if(corners[2]) {
g2d.fillArc(w - 2*r - so, h - 2*r - so, 2*r, 2*r, 270, 90);
}
else {
g2d.fillRect(w - r - so, h - r - so, r, r);
}
// NE
if(corners[3]) {
g2d.fillArc(w - 2*r - so, 0, 2*r, 2*r, 0, 90);
}
else {
g2d.fillRect(w - r - so, 0, r, r);
}
// Mid, left, right rectangles: border
g2d.setColor(this.borderColor); g2d.setColor(this.borderColor);
g2d.setStroke(new BasicStroke(1)); g2d.drawLine(r, 0, w - r - so, 0);
g2d.drawRoundRect( g2d.drawLine(r, h - so - 1, w - r - so, h - so - 1);
0,0, g2d.drawLine(0, r, 0, h - r - so);
w - shadowThickness - 1, g2d.drawLine(w - so - 1, r, w - so - 1, h - r - so);
h - shadowThickness - 1,
cornerDiameter, cornerDiameter); // Corners: border
// NW
if(corners[0]) {
g2d.drawArc(0, 0, 2*r, 2*r, 90, 90);
}
else {
g2d.drawLine(0, 0, r, 0);
g2d.drawLine(0, 0, 0, r);
}
// SW
if(corners[1]) {
g2d.drawArc(0, h - 2*r - so, 2*r, 2*r - 1, 180, 90);
}
else {
g2d.drawLine(0, h - so - 1, 0, h - r - so - 1);
g2d.drawLine(0, h - so - 1, r, h - so - 1);
}
// SE
if(corners[2]) {
g2d.drawArc(w - 2*r - so, h - 2*r - so, 2*r - 1, 2*r - 1, 270, 90);
}
else {
g2d.drawLine(w - so - 1, h - so - 1, w - so - 1, h - r - so);
g2d.drawLine(w - so - 1, h - so - 1, w - r - so, h - so - 1);
}
// NE
if(corners[3]) {
g2d.drawArc(w - 2*r - so, 0, 2*r - 1, 2*r - 1, 0, 90);
}
else {
g2d.drawLine(w - so - 1, 0, w - so - r, 0);
g2d.drawLine(w - so - 1, 0, w - so - 1, r);
}
} }
/** /**
@@ -135,96 +203,47 @@ public class FRoundedPanel extends JPanel {
} }
/** /**
* <p>setShadowDistance.</p> * <p>setShadowOffset.</p>
* Sets distance of shadow from rounded panel. * Sets offset of shadow from rounded panel.
* *
* @param {@link java.lang.Integer} side * @param {@link java.lang.Integer} side
*/ */
public void setShadowDistance(int side) { public void setShadowOffset(int i) {
this.shadowDistance = new Dimension(side,side); if(i < 0) {
i = 0;
}
this.shadowOffset = i;
} }
/** /**
* <p>setShadowDistance.</p> * <p>setCornerRadius.</p>
* Sets distance of shadow from rounded panel. * Sets radius of each corner on rounded panel.
*
* @param {@link java.lang.Integer} x
* @param {@link java.lang.Integer} y
*/
public void setShadowDistance(int x, int y) {
this.shadowDistance = new Dimension(x,y);
}
/**
* <p>setShadowDistance.</p>
* Sets thickness of rounded panel shadow.
*
* @param {@link java.lang.Integer} t
*/
public void setShadowThickness(int t) {
this.shadowThickness = t;
}
/**
* <p>setCornerDiameter.</p>
* Sets diameter of each corner on rounded panel.
* *
* @param {@link java.lang.Integer} r * @param {@link java.lang.Integer} r
*/ */
public void setCornerDiameter(int r) { public void setCornerRadius(int r) {
if(r < 0) { if(r < 0) {
r = 0; r = 0;
} }
this.cornerDiameter = r*2; this.cornerRadius = r;
} }
/** /**
* <p>parseColorString.</p> * <p>setCorners.</p>
* Uses string from settings file to make a new rgba color instance. * Sets if corners should be rounded or not in the following order:
* * NW, SW, SE, NE
* @param {@link java.lang.String} s * @param boolean vals[] must be length 4
*/ */
private Color parseColorString(String s) { public void setCorners(boolean vals[] ) {
Color c = null; if(vals.length!=4) {
int r,g,b,a = 0; throw new IllegalArgumentException("FRoundedPanel > setCorners requires an array of booleans of length 4.");
String[] temp = s.split(",");
if(temp.length==3 || temp.length==4) {
r = Integer.parseInt(temp[0]);
g = Integer.parseInt(temp[1]);
b = Integer.parseInt(temp[2]);
if(temp.length==4) {
a = Integer.parseInt(temp[3]);
}
c = new Color(r,g,b,a);
} }
return c; corners = vals;
} }
/** public void setShowShadow(boolean b) {
* <p>parseDimensionString.</p> showShadow = b;
* Uses string from settings file to make a new dimension instance.
*
* @param {@link java.lang.String} s
*/
private Dimension parseDimensionString(String s) {
Dimension d = null;
int w,h = 0;
String[] temp = s.split(",");
if(temp.length==2) {
w = Integer.parseInt(temp[0]);
h = Integer.parseInt(temp[0]);
d = new Dimension(w,h);
}
else if(temp.length==1 && !temp[0].equals("")) {
w = Integer.parseInt(temp[0]);
h = w;
d = new Dimension(w,h);
}
return d;
} }
} }

View File

@@ -3,11 +3,8 @@ package forge.gui.skin;
import java.awt.Color; import java.awt.Color;
import java.awt.Font; import java.awt.Font;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
@@ -52,8 +49,9 @@ public class FSkin {
public Color txt3a = Color.red; public Color txt3a = Color.red;
public Color txt3b = Color.red; public Color txt3b = Color.red;
public String name = "default";
//===== Private fields //===== Private fields
private final String settingsfile = "settings.txt";
private final String paletteFile = "palette.jpg"; private final String paletteFile = "palette.jpg";
private final String font1file = "font1.ttf"; private final String font1file = "font1.ttf";
private final String font2file = "font2.ttf"; private final String font2file = "font2.ttf";
@@ -76,10 +74,6 @@ public class FSkin {
private String skin; private String skin;
private String notfound = "FSkin.java: \""+skin+ private String notfound = "FSkin.java: \""+skin+
"\" skin can't find "; "\" skin can't find ";
private Hashtable<String, String> customSettings =
new Hashtable<String, String>();
private Hashtable<String, String> defaultSettings =
new Hashtable<String, String>();
/** /**
* FSkin constructor. No arguments, will generate default skin settings, * FSkin constructor. No arguments, will generate default skin settings,
@@ -99,49 +93,13 @@ public class FSkin {
* @throws Exception * @throws Exception
*/ */
public FSkin(String skinName) throws Exception { public FSkin(String skinName) throws Exception {
defaultSettings = loadSettings("default");
loadFontAndImages("default"); loadFontAndImages("default");
if(!skinName.equals("default")) { if(!skinName.equals("default")) {
customSettings = loadSettings(skinName);
loadFontAndImages(skinName); loadFontAndImages(skinName);
} }
} }
/**
* Retrieves skin settings from file, returns in hashtable.
*
* @param skinName
* @return settings hashtable
* @throws Exception
*/
private Hashtable<String, String> loadSettings(String skinName) throws Exception {
String filename = "res/images/skins/"+skinName+"/"+settingsfile;
String notfound = "FSkin > load: Can't find \"" + filename + "\".";
Hashtable<String, String> settings = new Hashtable<String, String>();
File f = new File(filename);
customSettings.put("name",skinName);
if (!f.exists()) {
throw new RuntimeException(notfound);
}
BufferedReader input = new BufferedReader(new FileReader(f));
String line = null;
while ((line = input.readLine()) != null) {
String[] data = line.split("=");
if (line.startsWith("#") || line.length() == 0 || data.length != 2) {
continue;
}
settings.put(data[0],data[1]);
}
return settings;
}
/** /**
* Loads objects from skin folder, prints brief error if not found. * Loads objects from skin folder, prints brief error if not found.
* *
@@ -237,35 +195,4 @@ public class FSkin {
(pixel & 0x000000ff) (pixel & 0x000000ff)
); );
} }
/**
* <p>getSetting.</p>
* Retrieves a specific skin setting. If skin setting is not defined,
* will attempt to use default value.
*
* @param key a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String getSetting(String key) {
String val = null;
if(customSettings.size() > 1) {
val = customSettings.get(key);
if(val==null) {
System.err.println("FSkin > getSetting: Could not find "+key+
" setting for "+customSettings.get("name")+" skin!");
}
}
if(val==null) {
val = defaultSettings.get(key);
}
if(val==null) {
System.err.println("FSkin > getSetting: Could not find "+key+
" in default settings!");
}
return val;
}
} }