Skip to content

Commit

Permalink
Constrained zoom levels
Browse files Browse the repository at this point in the history
  • Loading branch information
valb3r committed May 7, 2020
1 parent d570857 commit babaf04
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import java.awt.geom.Point2D
import java.awt.geom.Rectangle2D
import javax.swing.JPanel
import javax.swing.JTable
import kotlin.math.max
import kotlin.math.min

class Canvas: JPanel() {

Expand Down Expand Up @@ -66,6 +68,11 @@ class Canvas: JPanel() {

fun zoom(anchor: Point2D.Float, factor: Int) {
val scale = Math.pow(zoomFactor.toDouble(), factor.toDouble()).toFloat()

if (min(camera.zoom.x, camera.zoom.y) * scale < 0.3f || max(camera.zoom.x, camera.zoom.y) * scale > 2.0f) {
return
}

val scenePoint = camera.fromCameraView(anchor)
camera = Camera(
Point2D.Float(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CanvasPainter(val graphics2D: Graphics2D, val camera: Camera) {
private val solidLineStroke = BasicStroke(regularLineWidth)

private val cachedIcons = CacheBuilder.newBuilder()
.expireAfterAccess(1L, TimeUnit.SECONDS)
.expireAfterAccess(10L, TimeUnit.SECONDS)
.maximumSize(100)
.build<String, BufferedImage>()

Expand Down Expand Up @@ -245,7 +245,8 @@ class CanvasPainter(val graphics2D: Graphics2D, val camera: Camera) {
}

fun rasterizeSvg(svgFile: String, width: Float, height: Float): BufferedImage {
return cachedIcons.get(Hashing.md5().hashString(svgFile, UTF_8).toString()) {
val cacheKey = Hashing.md5().hashString(svgFile + ":" + width.toInt() + "@" + height.toInt(), UTF_8).toString()
return cachedIcons.get(cacheKey) {
val imageTranscoder = BufferedImageTranscoder()
imageTranscoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, width)
imageTranscoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, height)
Expand Down

0 comments on commit babaf04

Please sign in to comment.