Render theme callback, closes #274

This commit is contained in:
Emux 2016-12-20 15:55:26 +02:00
parent feae45f78b
commit 06ac5142f7
9 changed files with 118 additions and 50 deletions

View File

@ -0,0 +1,25 @@
/*
* Copyright 2016 devemux86
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.theme;
/**
* Callback methods for render theme.
*/
public interface ThemeCallback {
/**
* @return the color-int
*/
int getColor(int color);
}

View File

@ -30,8 +30,20 @@ public class ThemeLoader {
return load(new ExternalRenderTheme(renderThemePath, menuCallback)); return load(new ExternalRenderTheme(renderThemePath, menuCallback));
} }
public static IRenderTheme load(String renderThemePath, ThemeCallback themeCallback) throws ThemeException {
return load(new ExternalRenderTheme(renderThemePath), themeCallback);
}
public static IRenderTheme load(String renderThemePath, XmlRenderThemeMenuCallback menuCallback, ThemeCallback themeCallback) throws ThemeException {
return load(new ExternalRenderTheme(renderThemePath, menuCallback), themeCallback);
}
public static IRenderTheme load(ThemeFile theme) throws ThemeException { public static IRenderTheme load(ThemeFile theme) throws ThemeException {
IRenderTheme t = XmlThemeBuilder.read(theme); return load(theme, null);
}
public static IRenderTheme load(ThemeFile theme, ThemeCallback themeCallback) throws ThemeException {
IRenderTheme t = XmlThemeBuilder.read(theme, themeCallback);
if (t != null) if (t != null)
t.scaleTextSize(CanvasAdapter.textScale + (CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI - 1)); t.scaleTextSize(CanvasAdapter.textScale + (CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI - 1));
return t; return t;

View File

@ -91,7 +91,17 @@ public class XmlThemeBuilder extends DefaultHandler {
* @throws ThemeException if an error occurs while parsing the render theme XML. * @throws ThemeException if an error occurs while parsing the render theme XML.
*/ */
public static IRenderTheme read(ThemeFile theme) throws ThemeException { public static IRenderTheme read(ThemeFile theme) throws ThemeException {
XmlThemeBuilder renderThemeHandler = new XmlThemeBuilder(theme); return read(theme, null);
}
/**
* @param theme an input theme containing valid render theme XML data.
* @param themeCallback the theme callback.
* @return a new RenderTheme which is created by parsing the XML data from the input theme.
* @throws ThemeException if an error occurs while parsing the render theme XML.
*/
public static IRenderTheme read(ThemeFile theme, ThemeCallback themeCallback) throws ThemeException {
XmlThemeBuilder renderThemeHandler = new XmlThemeBuilder(theme, themeCallback);
try { try {
new XMLReaderAdapter().parse(renderThemeHandler, theme.getRenderThemeAsStream()); new XMLReaderAdapter().parse(renderThemeHandler, theme.getRenderThemeAsStream());
@ -138,6 +148,7 @@ public class XmlThemeBuilder extends DefaultHandler {
private float mTextScale = 1; private float mTextScale = 1;
private final ThemeFile mTheme; private final ThemeFile mTheme;
private final ThemeCallback mThemeCallback;
private RenderTheme mRenderTheme; private RenderTheme mRenderTheme;
private final float mScale, mScale2; private final float mScale, mScale2;
@ -147,7 +158,12 @@ public class XmlThemeBuilder extends DefaultHandler {
private XmlRenderThemeStyleMenu mRenderThemeStyleMenu; private XmlRenderThemeStyleMenu mRenderThemeStyleMenu;
public XmlThemeBuilder(ThemeFile theme) { public XmlThemeBuilder(ThemeFile theme) {
this(theme, null);
}
public XmlThemeBuilder(ThemeFile theme, ThemeCallback themeCallback) {
mTheme = theme; mTheme = theme;
mThemeCallback = themeCallback;
mScale = CanvasAdapter.scale + (CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI - 1); mScale = CanvasAdapter.scale + (CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI - 1);
mScale2 = CanvasAdapter.scale + (CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI - 1) * 0.5f; mScale2 = CanvasAdapter.scale + (CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI - 1) * 0.5f;
} }
@ -461,6 +477,7 @@ public class XmlThemeBuilder extends DefaultHandler {
LineBuilder<?> b = mLineBuilder.set(line); LineBuilder<?> b = mLineBuilder.set(line);
b.isOutline(isOutline); b.isOutline(isOutline);
b.level(level); b.level(level);
b.themeCallback(mThemeCallback);
String src = null; String src = null;
for (int i = 0; i < attributes.getLength(); i++) { for (int i = 0; i < attributes.getLength(); i++) {
@ -577,6 +594,7 @@ public class XmlThemeBuilder extends DefaultHandler {
int level) { int level) {
AreaBuilder<?> b = mAreaBuilder.set(area); AreaBuilder<?> b = mAreaBuilder.set(area);
b.level(level); b.level(level);
b.themeCallback(mThemeCallback);
String src = null; String src = null;
for (int i = 0; i < attributes.getLength(); i++) { for (int i = 0; i < attributes.getLength(); i++) {
@ -794,10 +812,11 @@ public class XmlThemeBuilder extends DefaultHandler {
if ("version".equals(name)) if ("version".equals(name))
version = Integer.parseInt(value); version = Integer.parseInt(value);
else if ("map-background".equals(name)) else if ("map-background".equals(name)) {
mapBackground = Color.parseColor(value); mapBackground = Color.parseColor(value);
if (mThemeCallback != null)
else if ("base-stroke-width".equals(name)) mapBackground = mThemeCallback.getColor(mapBackground);
} else if ("base-stroke-width".equals(name))
baseStrokeWidth = Float.parseFloat(value); baseStrokeWidth = Float.parseFloat(value);
else if ("base-text-scale".equals(name)) else if ("base-text-scale".equals(name))
@ -858,6 +877,7 @@ public class XmlThemeBuilder extends DefaultHandler {
b.caption = caption; b.caption = caption;
} else } else
b = mTextBuilder.from(style); b = mTextBuilder.from(style);
b.themeCallback(mThemeCallback);
String symbol = null; String symbol = null;
for (int i = 0; i < attributes.getLength(); i++) { for (int i = 0; i < attributes.getLength(); i++) {
@ -949,6 +969,7 @@ public class XmlThemeBuilder extends DefaultHandler {
private CircleStyle createCircle(String elementName, Attributes attributes, int level) { private CircleStyle createCircle(String elementName, Attributes attributes, int level) {
CircleBuilder<?> b = mCircleBuilder.reset(); CircleBuilder<?> b = mCircleBuilder.reset();
b.level(level); b.level(level);
b.themeCallback(mThemeCallback);
for (int i = 0; i < attributes.getLength(); i++) { for (int i = 0; i < attributes.getLength(); i++) {
String name = attributes.getLocalName(i); String name = attributes.getLocalName(i);
@ -1032,6 +1053,7 @@ public class XmlThemeBuilder extends DefaultHandler {
private ExtrusionStyle createExtrusion(String elementName, Attributes attributes, int level) { private ExtrusionStyle createExtrusion(String elementName, Attributes attributes, int level) {
ExtrusionBuilder<?> b = mExtrusionBuilder.reset(); ExtrusionBuilder<?> b = mExtrusionBuilder.reset();
b.level(level); b.level(level);
b.themeCallback(mThemeCallback);
for (int i = 0; i < attributes.getLength(); ++i) { for (int i = 0; i < attributes.getLength(); ++i) {
String name = attributes.getLocalName(i); String name = attributes.getLocalName(i);

View File

@ -107,11 +107,11 @@ public class AreaStyle extends RenderStyle<AreaStyle> {
this.level = b.level; this.level = b.level;
this.style = b.style; this.style = b.style;
this.fadeScale = b.fadeScale; this.fadeScale = b.fadeScale;
this.blendColor = b.blendColor; this.blendColor = b.themeCallback != null ? b.themeCallback.getColor(b.blendColor) : b.blendColor;
this.blendScale = b.blendScale; this.blendScale = b.blendScale;
this.color = b.fillColor; this.color = b.themeCallback != null ? b.themeCallback.getColor(b.fillColor) : b.fillColor;
this.texture = b.texture; this.texture = b.texture;
this.strokeColor = b.strokeColor; this.strokeColor = b.themeCallback != null ? b.themeCallback.getColor(b.strokeColor) : b.strokeColor;
this.strokeWidth = b.strokeWidth; this.strokeWidth = b.strokeWidth;
this.mesh = b.mesh; this.mesh = b.mesh;
@ -190,11 +190,11 @@ public class AreaStyle extends RenderStyle<AreaStyle> {
this.level = area.level; this.level = area.level;
this.style = area.style; this.style = area.style;
this.fadeScale = area.fadeScale; this.fadeScale = area.fadeScale;
this.blendColor = area.blendColor; this.blendColor = themeCallback != null ? themeCallback.getColor(area.blendColor) : area.blendColor;
this.blendScale = area.blendScale; this.blendScale = area.blendScale;
this.fillColor = area.color; this.fillColor = themeCallback != null ? themeCallback.getColor(area.color) : area.color;
this.texture = area.texture; this.texture = area.texture;
this.strokeColor = area.strokeColor; this.strokeColor = themeCallback != null ? themeCallback.getColor(area.strokeColor) : area.strokeColor;
this.strokeWidth = area.strokeWidth; this.strokeWidth = area.strokeWidth;
this.mesh = area.mesh; this.mesh = area.mesh;

View File

@ -45,8 +45,8 @@ public final class CircleStyle extends RenderStyle<CircleStyle> {
public CircleStyle(CircleBuilder<?> b) { public CircleStyle(CircleBuilder<?> b) {
this.radius = b.radius; this.radius = b.radius;
this.scaleRadius = b.scaleRadius; this.scaleRadius = b.scaleRadius;
this.fillColor = b.fillColor; this.fillColor = b.themeCallback != null ? b.themeCallback.getColor(b.fillColor) : b.fillColor;
this.strokeColor = b.strokeColor; this.strokeColor = b.themeCallback != null ? b.themeCallback.getColor(b.strokeColor) : b.strokeColor;
this.strokeWidth = b.strokeWidth; this.strokeWidth = b.strokeWidth;
this.level = b.level; this.level = b.level;
} }
@ -75,8 +75,8 @@ public final class CircleStyle extends RenderStyle<CircleStyle> {
this.radius = circle.radius; this.radius = circle.radius;
this.scaleRadius = circle.scaleRadius; this.scaleRadius = circle.scaleRadius;
this.fillColor = circle.fillColor; this.fillColor = themeCallback != null ? themeCallback.getColor(circle.fillColor) : circle.fillColor;
this.strokeColor = circle.strokeColor; this.strokeColor = themeCallback != null ? themeCallback.getColor(circle.strokeColor) : circle.strokeColor;
this.strokeWidth = circle.strokeWidth; this.strokeWidth = circle.strokeWidth;
this.level = circle.level; this.level = circle.level;

View File

@ -46,9 +46,9 @@ public class ExtrusionStyle extends RenderStyle<ExtrusionStyle> {
public ExtrusionStyle(ExtrusionBuilder<?> b) { public ExtrusionStyle(ExtrusionBuilder<?> b) {
this.level = b.level; this.level = b.level;
this.colorSide = b.colorSide; this.colorSide = b.themeCallback != null ? b.themeCallback.getColor(b.colorSide) : b.colorSide;
this.colorTop = b.colorTop; this.colorTop = b.themeCallback != null ? b.themeCallback.getColor(b.colorTop) : b.colorTop;
this.colorLine = b.colorLine; this.colorLine = b.themeCallback != null ? b.themeCallback.getColor(b.colorLine) : b.colorLine;
this.colors = new float[16]; this.colors = new float[16];
fillColors(colorSide, colorTop, colorLine, colors); fillColors(colorSide, colorTop, colorLine, colors);
@ -106,9 +106,9 @@ public class ExtrusionStyle extends RenderStyle<ExtrusionStyle> {
return reset(); return reset();
this.level = extrusion.level; this.level = extrusion.level;
this.colorSide = extrusion.colorSide; this.colorSide = themeCallback != null ? themeCallback.getColor(extrusion.colorSide) : extrusion.colorSide;
this.colorTop = extrusion.colorTop; this.colorTop = themeCallback != null ? themeCallback.getColor(extrusion.colorTop) : extrusion.colorTop;
this.colorLine = extrusion.colorLine; this.colorLine = themeCallback != null ? themeCallback.getColor(extrusion.colorLine) : extrusion.colorLine;
this.defaultHeight = extrusion.defaultHeight; this.defaultHeight = extrusion.defaultHeight;
return self(); return self();

View File

@ -93,14 +93,14 @@ public final class LineStyle extends RenderStyle<LineStyle> {
this.level = b.level; this.level = b.level;
this.style = b.style; this.style = b.style;
this.width = b.strokeWidth; this.width = b.strokeWidth;
this.color = b.fillColor; this.color = b.themeCallback != null ? b.themeCallback.getColor(b.fillColor) : b.fillColor;
this.cap = b.cap; this.cap = b.cap;
this.outline = b.outline; this.outline = b.outline;
this.fixed = b.fixed; this.fixed = b.fixed;
this.fadeScale = b.fadeScale; this.fadeScale = b.fadeScale;
this.blur = b.blur; this.blur = b.blur;
this.stipple = b.stipple; this.stipple = b.stipple;
this.stippleColor = b.stippleColor; this.stippleColor = b.themeCallback != null ? b.themeCallback.getColor(b.stippleColor) : b.stippleColor;
this.stippleWidth = b.stippleWidth; this.stippleWidth = b.stippleWidth;
this.texture = b.texture; this.texture = b.texture;
this.randomOffset = b.randomOffset; this.randomOffset = b.randomOffset;
@ -149,14 +149,14 @@ public final class LineStyle extends RenderStyle<LineStyle> {
this.level = line.level; this.level = line.level;
this.style = line.style; this.style = line.style;
this.strokeWidth = line.width; this.strokeWidth = line.width;
this.fillColor = line.color; this.fillColor = themeCallback != null ? themeCallback.getColor(line.color) : line.color;
this.cap = line.cap; this.cap = line.cap;
this.outline = line.outline; this.outline = line.outline;
this.fixed = line.fixed; this.fixed = line.fixed;
this.fadeScale = line.fadeScale; this.fadeScale = line.fadeScale;
this.blur = line.blur; this.blur = line.blur;
this.stipple = line.stipple; this.stipple = line.stipple;
this.stippleColor = line.stippleColor; this.stippleColor = themeCallback != null ? themeCallback.getColor(line.stippleColor) : line.stippleColor;
this.stippleWidth = line.stippleWidth; this.stippleWidth = line.stippleWidth;
this.texture = line.texture; this.texture = line.texture;
this.randomOffset = line.randomOffset; this.randomOffset = line.randomOffset;

View File

@ -18,6 +18,8 @@
*/ */
package org.oscim.theme.styles; package org.oscim.theme.styles;
import org.oscim.theme.ThemeCallback;
import static org.oscim.backend.canvas.Color.parseColor; import static org.oscim.backend.canvas.Color.parseColor;
/** /**
@ -36,6 +38,8 @@ public abstract class RenderStyle<T extends RenderStyle<T>> {
public int strokeColor; public int strokeColor;
public float strokeWidth; public float strokeWidth;
public ThemeCallback themeCallback;
public T cat(String cat) { public T cat(String cat) {
this.cat = cat; this.cat = cat;
return self(); return self();
@ -82,6 +86,11 @@ public abstract class RenderStyle<T extends RenderStyle<T>> {
return self(); return self();
} }
public T themeCallback(ThemeCallback themeCallback) {
this.themeCallback = themeCallback;
return self();
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected T self() { protected T self() {
return (T) this; return (T) this;

View File

@ -185,11 +185,11 @@ public final class TextStyle extends RenderStyle<TextStyle> {
this.areaSize = text.areaSize; this.areaSize = text.areaSize;
this.bitmap = text.bitmap; this.bitmap = text.bitmap;
this.texture = text.texture; this.texture = text.texture;
this.fillColor = text.paint.getColor(); this.fillColor = themeCallback != null ? themeCallback.getColor(text.paint.getColor()) : text.paint.getColor();
this.fontFamily = text.fontFamily; this.fontFamily = text.fontFamily;
this.fontStyle = text.fontStyle; this.fontStyle = text.fontStyle;
if (text.stroke != null) { if (text.stroke != null) {
this.strokeColor = text.stroke.getColor(); this.strokeColor = themeCallback != null ? themeCallback.getColor(text.stroke.getColor()) : text.stroke.getColor();
this.strokeWidth = text.stroke.getStrokeWidth(); this.strokeWidth = text.stroke.getStrokeWidth();
} }
this.fontSize = text.fontSize; this.fontSize = text.fontSize;
@ -202,41 +202,41 @@ public final class TextStyle extends RenderStyle<TextStyle> {
} }
} }
TextStyle(TextBuilder<?> tb) { TextStyle(TextBuilder<?> b) {
this.style = tb.style; this.style = b.style;
this.textKey = tb.textKey; this.textKey = b.textKey;
this.caption = tb.caption; this.caption = b.caption;
this.dy = tb.dy; this.dy = b.dy;
this.priority = tb.priority; this.priority = b.priority;
this.areaSize = tb.areaSize; this.areaSize = b.areaSize;
this.bitmap = tb.bitmap; this.bitmap = b.bitmap;
this.texture = tb.texture; this.texture = b.texture;
paint = CanvasAdapter.newPaint(); paint = CanvasAdapter.newPaint();
paint.setTextAlign(Align.CENTER); paint.setTextAlign(Align.CENTER);
paint.setTypeface(tb.fontFamily, tb.fontStyle); paint.setTypeface(b.fontFamily, b.fontStyle);
paint.setColor(tb.fillColor); paint.setColor(b.themeCallback != null ? b.themeCallback.getColor(b.fillColor) : b.fillColor);
paint.setTextSize(tb.fontSize); paint.setTextSize(b.fontSize);
if (tb.strokeWidth > 0) { if (b.strokeWidth > 0) {
stroke = CanvasAdapter.newPaint(); stroke = CanvasAdapter.newPaint();
stroke.setStyle(Paint.Style.STROKE); stroke.setStyle(Paint.Style.STROKE);
stroke.setTextAlign(Align.CENTER); stroke.setTextAlign(Align.CENTER);
stroke.setTypeface(tb.fontFamily, tb.fontStyle); stroke.setTypeface(b.fontFamily, b.fontStyle);
stroke.setColor(tb.strokeColor); stroke.setColor(b.themeCallback != null ? b.themeCallback.getColor(b.strokeColor) : b.strokeColor);
stroke.setStrokeWidth(tb.strokeWidth); stroke.setStrokeWidth(b.strokeWidth);
stroke.setTextSize(tb.fontSize); stroke.setTextSize(b.fontSize);
} else } else
stroke = null; stroke = null;
this.fontFamily = tb.fontFamily; this.fontFamily = b.fontFamily;
this.fontStyle = tb.fontStyle; this.fontStyle = b.fontStyle;
this.fontSize = tb.fontSize; this.fontSize = b.fontSize;
this.symbolWidth = tb.symbolWidth; this.symbolWidth = b.symbolWidth;
this.symbolHeight = tb.symbolHeight; this.symbolHeight = b.symbolHeight;
this.symbolPercent = tb.symbolPercent; this.symbolPercent = b.symbolPercent;
} }
public final String style; public final String style;