Skip to content

Commit

Permalink
Mapsforge themes compatibility improvements #388 opensciencemap#100
Browse files Browse the repository at this point in the history
  • Loading branch information
devemux86 committed Sep 3, 2017
1 parent b695d43 commit f4f8eb8
Show file tree
Hide file tree
Showing 20 changed files with 275 additions and 228 deletions.
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## New since 0.8.0

- Mapsforge themes compatibility [#100](https://github.com/mapsforge/vtm/issues/100)
- vtm-theme-comparator module [#387](https://github.com/mapsforge/vtm/issues/387)
- Many other minor improvements and bug fixes
- [Solved issues](https://github.com/mapsforge/vtm/issues?q=is%3Aclosed+milestone%3A0.9.0)
Expand Down
4 changes: 2 additions & 2 deletions vtm-android-example/res/menu/theme_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
android:id="@+id/theme_newtron"
android:title="@string/theme_newtron" />
<item
android:id="@+id/theme_load"
android:title="@string/theme_load" />
android:id="@+id/theme_external"
android:title="@string/theme_external" />
</group>

<item
Expand Down
2 changes: 1 addition & 1 deletion vtm-android-example/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="theme_osmagray">Osmagray</string>
<string name="theme_tubes">Tubes</string>
<string name="theme_newtron">NewTron</string>
<string name="theme_external">External theme</string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
<string name="error">Error</string>
Expand All @@ -17,6 +18,5 @@
<string name="style_1">Show nature</string>
<string name="style_2">Hide nature</string>
<string name="menu_gridlayer">Grid</string>
<string name="theme_load">load theme extern</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ public boolean accept(File file) {
try {
ThemeFile theme = new ExternalRenderTheme(file.getAbsolutePath());
DefaultHandler renderThemeHandler;
if(ThemeUtils.isMapsforgeTheme(new FileInputStream(file))) {
if (ThemeUtils.isMapsforgeTheme(new FileInputStream(file)))
renderThemeHandler = new XmlMapsforgeThemeBuilder(theme);
}else{
else
renderThemeHandler = new XmlThemeBuilder(theme);
}
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
xmlReader.setContentHandler(renderThemeHandler);
xmlReader.parse(new InputSource(theme.getRenderThemeAsStream()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class MapsforgeMapActivity extends MapActivity {

private TileGridLayer mGridLayer;
private DefaultMapScaleBar mMapScaleBar;
private Menu mMenu;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -85,6 +86,7 @@ public ThemeFilePicker() {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.theme_menu, menu);
mMenu = menu;
return true;
}

Expand Down Expand Up @@ -117,8 +119,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
item.setChecked(true);
return true;

case R.id.theme_load:
startActivityForResult(new Intent(MapsforgeMapActivity.this, ThemeFilePicker.class),
case R.id.theme_external:
startActivityForResult(new Intent(this, ThemeFilePicker.class),
SELECT_THEME_FILE);
return true;

Expand Down Expand Up @@ -181,18 +183,13 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
}
} else if (requestCode == SELECT_THEME_FILE) {
if (resultCode != RESULT_OK || intent == null || intent.getStringExtra(FilePicker.SELECTED_FILE) == null) {
finish();
return;
}

String themePath = intent.getStringExtra(FilePicker.SELECTED_FILE);

ExternalRenderTheme externalRenderTheme = new ExternalRenderTheme(themePath);
try {
mMap.setTheme(externalRenderTheme, true);
} catch (Exception e) {
e.printStackTrace();
}
String file = intent.getStringExtra(FilePicker.SELECTED_FILE);
ExternalRenderTheme externalRenderTheme = new ExternalRenderTheme(file);
mMap.setTheme(externalRenderTheme);
mMenu.findItem(R.id.theme_external).setChecked(true);
}
}

Expand Down
16 changes: 8 additions & 8 deletions vtm-android/src/org/oscim/android/canvas/AndroidCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public void fillColor(int color) {
canvas.drawColor(color, color == Color.TRANSPARENT ? PorterDuff.Mode.CLEAR : PorterDuff.Mode.SRC_OVER);
}

@Override
public void fillRectangle(float x, float y, float width, float height, int color) {
RectF rect = new RectF(x, y, x + width, y + height);
android.graphics.Paint paint = new android.graphics.Paint();
paint.setColor(color);
canvas.drawRect(rect, paint);
}

@Override
public int getHeight() {
return canvas.getHeight();
Expand All @@ -97,12 +105,4 @@ public int getHeight() {
public int getWidth() {
return canvas.getWidth();
}

@Override
public void fillRectangle(int x, int y, int width, int height, int color) {
RectF rec = new RectF(x, y, x + width, y + height);
android.graphics.Paint paint = new android.graphics.Paint();
paint.setColor(color);
canvas.drawRect(rec, paint);
}
}
20 changes: 10 additions & 10 deletions vtm-desktop/src/org/oscim/awt/AwtCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ public void fillColor(int color) {
fillRectangle(0, 0, getWidth(), getHeight(), color);
}

@Override
public void fillRectangle(float x, float y, float width, float height, int color) {
java.awt.Color awtColor = color == Color.TRANSPARENT ? TRANSPARENT : new java.awt.Color(color);
Composite originalComposite = this.canvas.getComposite();
this.canvas.setComposite(AlphaComposite.getInstance(color == Color.TRANSPARENT ? AlphaComposite.CLEAR : AlphaComposite.SRC_OVER));
this.canvas.setColor(awtColor);
this.canvas.fillRect((int) x, (int) y, (int) width, (int) height);
this.canvas.setComposite(originalComposite);
}

@Override
public int getHeight() {
return this.bitmap != null ? this.bitmap.getHeight() : 0;
Expand All @@ -195,14 +205,4 @@ public int getHeight() {
public int getWidth() {
return this.bitmap != null ? this.bitmap.getWidth() : 0;
}

@Override
public void fillRectangle(int x, int y, int width, int height, int color) {
java.awt.Color awtColor = color == Color.TRANSPARENT ? TRANSPARENT : new java.awt.Color(color);
Composite originalComposite = this.canvas.getComposite();
this.canvas.setComposite(AlphaComposite.getInstance(color == Color.TRANSPARENT ? AlphaComposite.CLEAR : AlphaComposite.SRC_OVER));
this.canvas.setColor(awtColor);
this.canvas.fillRect(x, y, width, height);
this.canvas.setComposite(originalComposite);
}
}
16 changes: 8 additions & 8 deletions vtm-ios/src/org/oscim/ios/backend/IosCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ public void fillColor(int color) {
this.cgBitmapContext.fillRect(rect);
}

@Override
public void fillRectangle(float x, float y, float width, float height, int color) {
CGRect rect = new CGRect(x, y, width, height);
setFillColor(this.cgBitmapContext, (color));
this.cgBitmapContext.setBlendMode(CGBlendMode.Normal);
this.cgBitmapContext.fillRect(rect);
}

@Override
public int getHeight() {
return this.cgBitmapContext != null ? (int) this.cgBitmapContext.getHeight() : 0;
Expand All @@ -159,12 +167,4 @@ public int getHeight() {
public int getWidth() {
return this.cgBitmapContext != null ? (int) this.cgBitmapContext.getWidth() : 0;
}

@Override
public void fillRectangle(int x, int y, int width, int height, int color) {
CGRect rect = new CGRect(x, y, width, height);
setFillColor(this.cgBitmapContext, (color));
this.cgBitmapContext.setBlendMode(CGBlendMode.Normal);
this.cgBitmapContext.fillRect(rect);
}
}
10 changes: 5 additions & 5 deletions vtm-playground/src/org/oscim/test/LineRenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ void addLines(LineTest l, int layer, boolean addOutline, boolean fixed) {
line2 = new LineStyle(Color.GREEN, 1);
line4 = new LineStyle(Color.LTGRAY, 3);
} else {
line1 = new LineStyle(0, null, Color.fade(Color.RED, 0.5f), 4.0f, Cap.BUTT, false, 0, 0, 0, 0, 1f, false, null, true);
line2 = new LineStyle(0, null, Color.GREEN, 6.0f, Cap.BUTT, false, 0, 0, 0, 0, 1f, false, null, true);
line4 = new LineStyle(0, null, Color.LTGRAY, 2.0f, Cap.ROUND, false, 0, 0, 0, 0, 1f, false, null, true);
line1 = new LineStyle(0, null, Color.fade(Color.RED, 0.5f), 4.0f, Cap.BUTT, false, 0, 0, 0, 0, 1f, false, null, true, null);
line2 = new LineStyle(0, null, Color.GREEN, 6.0f, Cap.BUTT, false, 0, 0, 0, 0, 1f, false, null, true, null);
line4 = new LineStyle(0, null, Color.LTGRAY, 2.0f, Cap.ROUND, false, 0, 0, 0, 0, 1f, false, null, true, null);
}

TextureItem tex = new TextureItem(CanvasAdapter.getBitmapAsset("", "patterns/dot.png"));
Expand All @@ -90,8 +90,8 @@ void addLines(LineTest l, int layer, boolean addOutline, boolean fixed) {
.randomOffset(true)
.build();

LineStyle outline = new LineStyle(0, null, Color.BLUE, 2.0f, Cap.ROUND, false, 0, 0, 0, 0, 1f, true, null, true);
LineStyle outline2 = new LineStyle(0, null, Color.RED, 2.0f, Cap.ROUND, false, 0, 0, 0, 0, 0, true, null, true);
LineStyle outline = new LineStyle(0, null, Color.BLUE, 2.0f, Cap.ROUND, false, 0, 0, 0, 0, 1f, true, null, true, null);
LineStyle outline2 = new LineStyle(0, null, Color.RED, 2.0f, Cap.ROUND, false, 0, 0, 0, 0, 0, true, null, true, null);

LineBucket ol = l.buckets.addLineBucket(0, outline);
LineBucket ol2 = l.buckets.addLineBucket(5, outline2);
Expand Down
10 changes: 5 additions & 5 deletions vtm-web/src/org/oscim/gdx/client/GwtCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public void fillColor(int color) {
// TODO
}

@Override
public void fillRectangle(float x, float y, float width, float height, int color) {
// TODO
}

@Override
public int getHeight() {
return this.bitmap != null ? this.bitmap.getHeight() : 0;
Expand All @@ -125,9 +130,4 @@ public int getHeight() {
public int getWidth() {
return this.bitmap != null ? this.bitmap.getWidth() : 0;
}

@Override
public void fillRectangle(int x, int y, int width, int height, int color) {
// TODO
}
}
35 changes: 35 additions & 0 deletions vtm-web/src/org/oscim/gdx/emu/org/oscim/theme/ThemeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2017 Longri
* Copyright 2017 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;

import java.io.InputStream;

/**
* A utility class with theme specific helper methods.
*/
public final class ThemeUtils {

/**
* Check if the given InputStream is a Mapsforge render theme.
*/
public static boolean isMapsforgeTheme(InputStream is) {
// TODO
return false;
}

private ThemeUtils() {
}
}
7 changes: 3 additions & 4 deletions vtm/resources/assets/shaders/linetex_layer_tex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ uniform float u_mode;
void
main(){
if (u_mode >= 1.0) {

float step= 2.0;
if (u_mode == 3.0){// dashed texture
step =1.0;
float step = 2.0;
if (u_mode == 2.0) { // dashed texture
step = 1.0;
}
vec4 c=texture2D(tex,vec2(abs(mod(v_st.s+1.0,step)),(v_st.t+1.0)*0.5));
float fuzz=fwidth(c.a);
Expand Down
4 changes: 2 additions & 2 deletions vtm/src/org/oscim/backend/canvas/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public interface Canvas {

void fillColor(int color);

void fillRectangle(float x, float y, float width, float height, int color);

int getHeight();

int getWidth();

void fillRectangle(int x, int y, int width, int height, int color);
}
4 changes: 2 additions & 2 deletions vtm/src/org/oscim/renderer/bucket/LineTexBucket.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2013 Hannes Janetzek
* Copyright 2016 devemux86
* Copyright 2016-2017 devemux86
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
Expand Down Expand Up @@ -359,7 +359,7 @@ public static RenderBucket draw(RenderBucket b, GLViewport v,
LineTexBucket lb = (LineTexBucket) b;
LineStyle line = lb.line.current();

gl.uniform1f(shader.uMode, line.dashTexture? 3 : line.texture != null ? 1 : 0);
gl.uniform1f(shader.uMode, line.dashArray != null ? 2 : (line.texture != null ? 1 : 0));

if (line.texture != null)
line.texture.bind();
Expand Down
23 changes: 23 additions & 0 deletions vtm/src/org/oscim/theme/SAXTerminationException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2017 Longri
*
* 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;

import org.xml.sax.SAXException;

public class SAXTerminationException extends SAXException {
public SAXTerminationException() {
super();
}
}
31 changes: 6 additions & 25 deletions vtm/src/org/oscim/theme/ThemeLoader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2013 Hannes Janetzek
* Copyright 2016 devemux86
* Copyright 2016-2017 devemux86
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
Expand All @@ -18,21 +18,11 @@
*/
package org.oscim.theme;


import org.oscim.backend.CanvasAdapter;
import org.oscim.theme.IRenderTheme.ThemeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

public class ThemeLoader {

private static final Logger log = LoggerFactory.getLogger(ThemeLoader.class);

public static boolean USE_ATLAS;
public static boolean POT_TEXTURES;

Expand All @@ -56,21 +46,12 @@ public static IRenderTheme load(ThemeFile theme) throws ThemeException {
return load(theme, null);
}



public static IRenderTheme load(ThemeFile theme, ThemeCallback themeCallback) throws ThemeException {
IRenderTheme t = null;

try {
if(ThemeUtils.isMapsforgeTheme(theme.getRenderThemeAsStream())){
t = USE_ATLAS ? XmlMapsforgeAtlasThemeBuilder.read(theme, themeCallback) : XmlMapsforgeThemeBuilder.read(theme, themeCallback);
}else{
t = USE_ATLAS ? XmlAtlasThemeBuilder.read(theme, themeCallback) : XmlThemeBuilder.read(theme, themeCallback);
}
} catch (IOException | ParserConfigurationException | SAXException e) {
e.printStackTrace();
}

IRenderTheme t;
if (ThemeUtils.isMapsforgeTheme(theme.getRenderThemeAsStream()))
t = USE_ATLAS ? XmlMapsforgeAtlasThemeBuilder.read(theme, themeCallback) : XmlMapsforgeThemeBuilder.read(theme, themeCallback);
else
t = USE_ATLAS ? XmlAtlasThemeBuilder.read(theme, themeCallback) : XmlThemeBuilder.read(theme, themeCallback);
if (t != null)
t.scaleTextSize(CanvasAdapter.textScale + (CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI - 1));
return t;
Expand Down
Loading

0 comments on commit f4f8eb8

Please sign in to comment.