From 82ebb01a20b1b959a0a9d1ae27d6e429e08de68e Mon Sep 17 00:00:00 2001 From: Jack Rainy Date: Sun, 25 Jun 2023 09:37:08 +0300 Subject: [PATCH] Zoom in/out of the history charts (#9660) * Do not recreate the Line Chart every time * Simplifed the Line Chart creation * Do not create objects in draw(): VictoryScreenCivGroup table * Do not create objects in draw(): Labels * Create labels without negative Y * Lift the X axis if there is an negative number * Arbitrary number of -Y labels * Autoscale by Y axis * Zoom in/out by click * Autoscale by X axis * Unit tests for LineChart * Rework of the line chart rendering --- core/src/com/unciv/ui/components/LineChart.kt | 263 ++++++++++-------- .../victoryscreen/VictoryScreenCharts.kt | 42 +-- .../victoryscreen/VictoryScreenCivGroup.kt | 62 +++-- .../com/unciv/ui/components/LineChartTests.kt | 140 ++++++++++ 4 files changed, 338 insertions(+), 169 deletions(-) create mode 100644 tests/src/com/unciv/ui/components/LineChartTests.kt diff --git a/core/src/com/unciv/ui/components/LineChart.kt b/core/src/com/unciv/ui/components/LineChart.kt index 522145b843..4d66ad9819 100644 --- a/core/src/com/unciv/ui/components/LineChart.kt +++ b/core/src/com/unciv/ui/components/LineChart.kt @@ -2,35 +2,28 @@ package com.unciv.ui.components import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.g2d.Batch -import com.badlogic.gdx.graphics.glutils.ShapeRenderer -import com.badlogic.gdx.math.Matrix4 -import com.badlogic.gdx.math.Vector2 -import com.badlogic.gdx.scenes.scene2d.ui.Image +import com.badlogic.gdx.math.MathUtils.lerp import com.badlogic.gdx.scenes.scene2d.ui.Label -import com.badlogic.gdx.scenes.scene2d.ui.Widget +import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup import com.badlogic.gdx.utils.Align import com.unciv.logic.civilization.Civilization -import com.unciv.ui.components.extensions.surroundWithCircle +import com.unciv.ui.images.ImageGetter import com.unciv.ui.screens.victoryscreen.VictoryScreenCivGroup import com.unciv.ui.screens.victoryscreen.VictoryScreenCivGroup.DefeatedPlayerStyle +import kotlin.math.abs import kotlin.math.min import kotlin.math.ceil +import kotlin.math.floor import kotlin.math.log10 import kotlin.math.max import kotlin.math.pow import kotlin.math.sqrt -private data class DataPoint(val x: T, val y: T, val civ: Civilization) +data class DataPoint(val x: T, val y: T, val civ: Civilization) class LineChart( - data: Map>, - private val viewingCiv: Civilization, - private val selectedCiv: Civilization, - private val chartWidth: Float, - private val chartHeight: Float -) : Widget() { - - private val shapeRenderer = ShapeRenderer() + private val viewingCiv: Civilization +) : WidgetGroup() { private val axisLineWidth = 2f private val axisColor = Color.WHITE @@ -45,40 +38,85 @@ class LineChart( * as `0` is not counted. */ private val maxLabels = 10 - private val xLabels: List - private val yLabels: List + private var xLabels = emptyList() + private var yLabels = emptyList() + private var xLabelsAsLabels = emptyList