From 3add5c231891785918dee730b7d033f961b6645b Mon Sep 17 00:00:00 2001 From: choelzl Date: Fri, 7 Apr 2023 13:24:16 +0200 Subject: [PATCH] [M] CSS for map --- .../beendroid/activity/FoldingListAdapter.kt | 3 +-- .../helcel/beendroid/activity/MainActivity.kt | 23 +++++++++++++------ .../net/helcel/beendroid/svg/CSSWrapper.kt | 23 +++++++++++++++++++ .../net/helcel/beendroid/svg/PSVGWrapper.kt | 19 +++++++++++---- 4 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/net/helcel/beendroid/svg/CSSWrapper.kt diff --git a/app/src/main/java/net/helcel/beendroid/activity/FoldingListAdapter.kt b/app/src/main/java/net/helcel/beendroid/activity/FoldingListAdapter.kt index 4663de3..c7cca73 100644 --- a/app/src/main/java/net/helcel/beendroid/activity/FoldingListAdapter.kt +++ b/app/src/main/java/net/helcel/beendroid/activity/FoldingListAdapter.kt @@ -38,6 +38,7 @@ class FoldingListAdapter( val el = cg.toList()[position] holder.bind(el) { notifyItemChanged(position) + parentLambda() } holder.addListeners( { @@ -50,8 +51,6 @@ class FoldingListAdapter( visited.setVisited(el.first, it) parentLambda() }) - - } override fun getItemCount(): Int { diff --git a/app/src/main/java/net/helcel/beendroid/activity/MainActivity.kt b/app/src/main/java/net/helcel/beendroid/activity/MainActivity.kt index 71118d0..a45386b 100644 --- a/app/src/main/java/net/helcel/beendroid/activity/MainActivity.kt +++ b/app/src/main/java/net/helcel/beendroid/activity/MainActivity.kt @@ -6,10 +6,12 @@ import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.caverock.androidsvg.RenderOptions import com.caverock.androidsvg.SVGImageView import net.helcel.beendroid.R import net.helcel.beendroid.countries.Visited import net.helcel.beendroid.countries.World +import net.helcel.beendroid.svg.CSSWrapper import net.helcel.beendroid.svg.PSVGWrapper @@ -20,27 +22,34 @@ class MainActivity : AppCompatActivity() { private lateinit var visited : Visited private lateinit var psvg : PSVGWrapper + private lateinit var css : CSSWrapper + + private val bitmap: Bitmap = Bitmap.createBitmap(1200,900, Bitmap.Config.ARGB_8888) + private val canvas = Canvas(bitmap) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + canvas.drawRGB(255, 255, 255) + visited = Visited(this) visited.load() psvg = PSVGWrapper(this) + css = CSSWrapper(visited) setContentView(R.layout.activity_main) map = findViewById(R.id.map) - - val bitmap = Bitmap.createBitmap(1200,900, Bitmap.Config.ARGB_8888) - val canvas = Canvas(bitmap) - canvas.drawRGB(255, 255, 255) - - psvg.get().renderToCanvas(canvas) map.setImageBitmap(bitmap) + refreshMap() + list = findViewById(R.id.list) list.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false) - list.adapter = FoldingListAdapter(this, World.WWW.children, visited) { } + list.adapter = FoldingListAdapter(this, World.WWW.children, visited) { refreshMap() } + } + + private fun refreshMap(){ + psvg.get().renderToCanvas(canvas,RenderOptions.create().css(css.get())) } } \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beendroid/svg/CSSWrapper.kt b/app/src/main/java/net/helcel/beendroid/svg/CSSWrapper.kt new file mode 100644 index 0000000..d5705ec --- /dev/null +++ b/app/src/main/java/net/helcel/beendroid/svg/CSSWrapper.kt @@ -0,0 +1,23 @@ +package net.helcel.beendroid.svg + +import net.helcel.beendroid.countries.Visited +import net.helcel.beendroid.countries.World + +class CSSWrapper(private val visited: Visited) { + + fun get() : String { + return listOf(World.WWW.children + .filter { visited.visited(it)} + .map { ".${it.code}{fill:blue;}"} + .fold(""){acc, s-> acc + s}, + World.WWW.children + .filter { !visited.visited(it) } + .map { cg -> cg.children + .filter { visited.visited(it) } + .map { ".${it.code}{fill:blue;}"} + .fold(""){acc, s-> acc + s} + }.fold(""){acc,s->acc+s}, + ).fold(""){acc,s-> acc+s} + } + +} \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beendroid/svg/PSVGWrapper.kt b/app/src/main/java/net/helcel/beendroid/svg/PSVGWrapper.kt index e498361..30b5f11 100644 --- a/app/src/main/java/net/helcel/beendroid/svg/PSVGWrapper.kt +++ b/app/src/main/java/net/helcel/beendroid/svg/PSVGWrapper.kt @@ -3,24 +3,35 @@ package net.helcel.beendroid.svg import android.content.Context import com.caverock.androidsvg.SVG import net.helcel.beendroid.countries.Country +import net.helcel.beendroid.countries.GeoLoc +import net.helcel.beendroid.countries.World class PSVGWrapper(ctx: Context) { - - - private val cm = HashMap() + private val cm = HashMap() + private var fm = "" init { Country.values().forEach { cm[it] = PSVGLoader(ctx, it, Level.ZERO).load() } + build() } fun level(el: Country, level: Level){ cm[el]?.changeLevel(level) } + fun build(){ + fm = World.WWW.children.map { gr -> + gr.children.map {c -> + val cc = cm[c] + if (cc!=null) "${cc.data}" + else "" + }.fold("") { acc, e -> acc + e } + }.fold("") { acc, e -> acc + e } + } + fun get(): SVG { - val fm = cm.values.fold("") { acc, e -> acc + e.data } return SVG.getFromString("$fm") }