From 06ac5142f7295d96bc1c44f843c1edb3505f0f21 Mon Sep 17 00:00:00 2001 From: Emux Date: Tue, 20 Dec 2016 15:55:26 +0200 Subject: [PATCH] Render theme callback, closes #274 --- vtm/src/org/oscim/theme/ThemeCallback.java | 25 ++++++++++ vtm/src/org/oscim/theme/ThemeLoader.java | 14 +++++- vtm/src/org/oscim/theme/XmlThemeBuilder.java | 30 +++++++++-- vtm/src/org/oscim/theme/styles/AreaStyle.java | 12 ++--- .../org/oscim/theme/styles/CircleStyle.java | 8 +-- .../oscim/theme/styles/ExtrusionStyle.java | 12 ++--- vtm/src/org/oscim/theme/styles/LineStyle.java | 8 +-- .../org/oscim/theme/styles/RenderStyle.java | 9 ++++ vtm/src/org/oscim/theme/styles/TextStyle.java | 50 +++++++++---------- 9 files changed, 118 insertions(+), 50 deletions(-) create mode 100644 vtm/src/org/oscim/theme/ThemeCallback.java diff --git a/vtm/src/org/oscim/theme/ThemeCallback.java b/vtm/src/org/oscim/theme/ThemeCallback.java new file mode 100644 index 00000000..617d68eb --- /dev/null +++ b/vtm/src/org/oscim/theme/ThemeCallback.java @@ -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 . + */ +package org.oscim.theme; + +/** + * Callback methods for render theme. + */ +public interface ThemeCallback { + /** + * @return the color-int + */ + int getColor(int color); +} diff --git a/vtm/src/org/oscim/theme/ThemeLoader.java b/vtm/src/org/oscim/theme/ThemeLoader.java index 4231b19a..afffa07d 100644 --- a/vtm/src/org/oscim/theme/ThemeLoader.java +++ b/vtm/src/org/oscim/theme/ThemeLoader.java @@ -30,8 +30,20 @@ public class ThemeLoader { 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 { - 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) t.scaleTextSize(CanvasAdapter.textScale + (CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI - 1)); return t; diff --git a/vtm/src/org/oscim/theme/XmlThemeBuilder.java b/vtm/src/org/oscim/theme/XmlThemeBuilder.java index 7d8794e3..e94a8218 100644 --- a/vtm/src/org/oscim/theme/XmlThemeBuilder.java +++ b/vtm/src/org/oscim/theme/XmlThemeBuilder.java @@ -91,7 +91,17 @@ public class XmlThemeBuilder extends DefaultHandler { * @throws ThemeException if an error occurs while parsing the render theme XML. */ 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 { new XMLReaderAdapter().parse(renderThemeHandler, theme.getRenderThemeAsStream()); @@ -138,6 +148,7 @@ public class XmlThemeBuilder extends DefaultHandler { private float mTextScale = 1; private final ThemeFile mTheme; + private final ThemeCallback mThemeCallback; private RenderTheme mRenderTheme; private final float mScale, mScale2; @@ -147,7 +158,12 @@ public class XmlThemeBuilder extends DefaultHandler { private XmlRenderThemeStyleMenu mRenderThemeStyleMenu; public XmlThemeBuilder(ThemeFile theme) { + this(theme, null); + } + + public XmlThemeBuilder(ThemeFile theme, ThemeCallback themeCallback) { mTheme = theme; + mThemeCallback = themeCallback; mScale = CanvasAdapter.scale + (CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI - 1); 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); b.isOutline(isOutline); b.level(level); + b.themeCallback(mThemeCallback); String src = null; for (int i = 0; i < attributes.getLength(); i++) { @@ -577,6 +594,7 @@ public class XmlThemeBuilder extends DefaultHandler { int level) { AreaBuilder b = mAreaBuilder.set(area); b.level(level); + b.themeCallback(mThemeCallback); String src = null; for (int i = 0; i < attributes.getLength(); i++) { @@ -794,10 +812,11 @@ public class XmlThemeBuilder extends DefaultHandler { if ("version".equals(name)) version = Integer.parseInt(value); - else if ("map-background".equals(name)) + else if ("map-background".equals(name)) { mapBackground = Color.parseColor(value); - - else if ("base-stroke-width".equals(name)) + if (mThemeCallback != null) + mapBackground = mThemeCallback.getColor(mapBackground); + } else if ("base-stroke-width".equals(name)) baseStrokeWidth = Float.parseFloat(value); else if ("base-text-scale".equals(name)) @@ -858,6 +877,7 @@ public class XmlThemeBuilder extends DefaultHandler { b.caption = caption; } else b = mTextBuilder.from(style); + b.themeCallback(mThemeCallback); String symbol = null; 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) { CircleBuilder b = mCircleBuilder.reset(); b.level(level); + b.themeCallback(mThemeCallback); for (int i = 0; i < attributes.getLength(); i++) { String name = attributes.getLocalName(i); @@ -1032,6 +1053,7 @@ public class XmlThemeBuilder extends DefaultHandler { private ExtrusionStyle createExtrusion(String elementName, Attributes attributes, int level) { ExtrusionBuilder b = mExtrusionBuilder.reset(); b.level(level); + b.themeCallback(mThemeCallback); for (int i = 0; i < attributes.getLength(); ++i) { String name = attributes.getLocalName(i); diff --git a/vtm/src/org/oscim/theme/styles/AreaStyle.java b/vtm/src/org/oscim/theme/styles/AreaStyle.java index caa05a68..efb37032 100644 --- a/vtm/src/org/oscim/theme/styles/AreaStyle.java +++ b/vtm/src/org/oscim/theme/styles/AreaStyle.java @@ -107,11 +107,11 @@ public class AreaStyle extends RenderStyle { this.level = b.level; this.style = b.style; 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.color = b.fillColor; + this.color = b.themeCallback != null ? b.themeCallback.getColor(b.fillColor) : b.fillColor; 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.mesh = b.mesh; @@ -190,11 +190,11 @@ public class AreaStyle extends RenderStyle { this.level = area.level; this.style = area.style; this.fadeScale = area.fadeScale; - this.blendColor = area.blendColor; + this.blendColor = themeCallback != null ? themeCallback.getColor(area.blendColor) : area.blendColor; this.blendScale = area.blendScale; - this.fillColor = area.color; + this.fillColor = themeCallback != null ? themeCallback.getColor(area.color) : area.color; this.texture = area.texture; - this.strokeColor = area.strokeColor; + this.strokeColor = themeCallback != null ? themeCallback.getColor(area.strokeColor) : area.strokeColor; this.strokeWidth = area.strokeWidth; this.mesh = area.mesh; diff --git a/vtm/src/org/oscim/theme/styles/CircleStyle.java b/vtm/src/org/oscim/theme/styles/CircleStyle.java index 758ebef0..ea6cb4ad 100644 --- a/vtm/src/org/oscim/theme/styles/CircleStyle.java +++ b/vtm/src/org/oscim/theme/styles/CircleStyle.java @@ -45,8 +45,8 @@ public final class CircleStyle extends RenderStyle { public CircleStyle(CircleBuilder b) { this.radius = b.radius; this.scaleRadius = b.scaleRadius; - this.fillColor = b.fillColor; - this.strokeColor = b.strokeColor; + this.fillColor = b.themeCallback != null ? b.themeCallback.getColor(b.fillColor) : b.fillColor; + this.strokeColor = b.themeCallback != null ? b.themeCallback.getColor(b.strokeColor) : b.strokeColor; this.strokeWidth = b.strokeWidth; this.level = b.level; } @@ -75,8 +75,8 @@ public final class CircleStyle extends RenderStyle { this.radius = circle.radius; this.scaleRadius = circle.scaleRadius; - this.fillColor = circle.fillColor; - this.strokeColor = circle.strokeColor; + this.fillColor = themeCallback != null ? themeCallback.getColor(circle.fillColor) : circle.fillColor; + this.strokeColor = themeCallback != null ? themeCallback.getColor(circle.strokeColor) : circle.strokeColor; this.strokeWidth = circle.strokeWidth; this.level = circle.level; diff --git a/vtm/src/org/oscim/theme/styles/ExtrusionStyle.java b/vtm/src/org/oscim/theme/styles/ExtrusionStyle.java index 87eeba4e..c56fa2db 100644 --- a/vtm/src/org/oscim/theme/styles/ExtrusionStyle.java +++ b/vtm/src/org/oscim/theme/styles/ExtrusionStyle.java @@ -46,9 +46,9 @@ public class ExtrusionStyle extends RenderStyle { public ExtrusionStyle(ExtrusionBuilder b) { this.level = b.level; - this.colorSide = b.colorSide; - this.colorTop = b.colorTop; - this.colorLine = b.colorLine; + this.colorSide = b.themeCallback != null ? b.themeCallback.getColor(b.colorSide) : b.colorSide; + this.colorTop = b.themeCallback != null ? b.themeCallback.getColor(b.colorTop) : b.colorTop; + this.colorLine = b.themeCallback != null ? b.themeCallback.getColor(b.colorLine) : b.colorLine; this.colors = new float[16]; fillColors(colorSide, colorTop, colorLine, colors); @@ -106,9 +106,9 @@ public class ExtrusionStyle extends RenderStyle { return reset(); this.level = extrusion.level; - this.colorSide = extrusion.colorSide; - this.colorTop = extrusion.colorTop; - this.colorLine = extrusion.colorLine; + this.colorSide = themeCallback != null ? themeCallback.getColor(extrusion.colorSide) : extrusion.colorSide; + this.colorTop = themeCallback != null ? themeCallback.getColor(extrusion.colorTop) : extrusion.colorTop; + this.colorLine = themeCallback != null ? themeCallback.getColor(extrusion.colorLine) : extrusion.colorLine; this.defaultHeight = extrusion.defaultHeight; return self(); diff --git a/vtm/src/org/oscim/theme/styles/LineStyle.java b/vtm/src/org/oscim/theme/styles/LineStyle.java index 92c8a849..7edbff4a 100644 --- a/vtm/src/org/oscim/theme/styles/LineStyle.java +++ b/vtm/src/org/oscim/theme/styles/LineStyle.java @@ -93,14 +93,14 @@ public final class LineStyle extends RenderStyle { this.level = b.level; this.style = b.style; 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.outline = b.outline; this.fixed = b.fixed; this.fadeScale = b.fadeScale; this.blur = b.blur; 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.texture = b.texture; this.randomOffset = b.randomOffset; @@ -149,14 +149,14 @@ public final class LineStyle extends RenderStyle { this.level = line.level; this.style = line.style; this.strokeWidth = line.width; - this.fillColor = line.color; + this.fillColor = themeCallback != null ? themeCallback.getColor(line.color) : line.color; this.cap = line.cap; this.outline = line.outline; this.fixed = line.fixed; this.fadeScale = line.fadeScale; this.blur = line.blur; this.stipple = line.stipple; - this.stippleColor = line.stippleColor; + this.stippleColor = themeCallback != null ? themeCallback.getColor(line.stippleColor) : line.stippleColor; this.stippleWidth = line.stippleWidth; this.texture = line.texture; this.randomOffset = line.randomOffset; diff --git a/vtm/src/org/oscim/theme/styles/RenderStyle.java b/vtm/src/org/oscim/theme/styles/RenderStyle.java index 5a246994..d2607b6f 100644 --- a/vtm/src/org/oscim/theme/styles/RenderStyle.java +++ b/vtm/src/org/oscim/theme/styles/RenderStyle.java @@ -18,6 +18,8 @@ */ package org.oscim.theme.styles; +import org.oscim.theme.ThemeCallback; + import static org.oscim.backend.canvas.Color.parseColor; /** @@ -36,6 +38,8 @@ public abstract class RenderStyle> { public int strokeColor; public float strokeWidth; + public ThemeCallback themeCallback; + public T cat(String cat) { this.cat = cat; return self(); @@ -82,6 +86,11 @@ public abstract class RenderStyle> { return self(); } + public T themeCallback(ThemeCallback themeCallback) { + this.themeCallback = themeCallback; + return self(); + } + @SuppressWarnings("unchecked") protected T self() { return (T) this; diff --git a/vtm/src/org/oscim/theme/styles/TextStyle.java b/vtm/src/org/oscim/theme/styles/TextStyle.java index 707d747e..bd0dd675 100644 --- a/vtm/src/org/oscim/theme/styles/TextStyle.java +++ b/vtm/src/org/oscim/theme/styles/TextStyle.java @@ -185,11 +185,11 @@ public final class TextStyle extends RenderStyle { this.areaSize = text.areaSize; this.bitmap = text.bitmap; 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.fontStyle = text.fontStyle; 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.fontSize = text.fontSize; @@ -202,41 +202,41 @@ public final class TextStyle extends RenderStyle { } } - TextStyle(TextBuilder tb) { - this.style = tb.style; - this.textKey = tb.textKey; - this.caption = tb.caption; - this.dy = tb.dy; - this.priority = tb.priority; - this.areaSize = tb.areaSize; - this.bitmap = tb.bitmap; - this.texture = tb.texture; + TextStyle(TextBuilder b) { + this.style = b.style; + this.textKey = b.textKey; + this.caption = b.caption; + this.dy = b.dy; + this.priority = b.priority; + this.areaSize = b.areaSize; + this.bitmap = b.bitmap; + this.texture = b.texture; paint = CanvasAdapter.newPaint(); paint.setTextAlign(Align.CENTER); - paint.setTypeface(tb.fontFamily, tb.fontStyle); + paint.setTypeface(b.fontFamily, b.fontStyle); - paint.setColor(tb.fillColor); - paint.setTextSize(tb.fontSize); + paint.setColor(b.themeCallback != null ? b.themeCallback.getColor(b.fillColor) : b.fillColor); + paint.setTextSize(b.fontSize); - if (tb.strokeWidth > 0) { + if (b.strokeWidth > 0) { stroke = CanvasAdapter.newPaint(); stroke.setStyle(Paint.Style.STROKE); stroke.setTextAlign(Align.CENTER); - stroke.setTypeface(tb.fontFamily, tb.fontStyle); - stroke.setColor(tb.strokeColor); - stroke.setStrokeWidth(tb.strokeWidth); - stroke.setTextSize(tb.fontSize); + stroke.setTypeface(b.fontFamily, b.fontStyle); + stroke.setColor(b.themeCallback != null ? b.themeCallback.getColor(b.strokeColor) : b.strokeColor); + stroke.setStrokeWidth(b.strokeWidth); + stroke.setTextSize(b.fontSize); } else stroke = null; - this.fontFamily = tb.fontFamily; - this.fontStyle = tb.fontStyle; - this.fontSize = tb.fontSize; + this.fontFamily = b.fontFamily; + this.fontStyle = b.fontStyle; + this.fontSize = b.fontSize; - this.symbolWidth = tb.symbolWidth; - this.symbolHeight = tb.symbolHeight; - this.symbolPercent = tb.symbolPercent; + this.symbolWidth = b.symbolWidth; + this.symbolHeight = b.symbolHeight; + this.symbolPercent = b.symbolPercent; } public final String style;