1 /* <lambda>null2 * Copyright 2017 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package traceviewer.ui 18 19 import java.awt.event.ActionListener 20 import javax.swing.Timer 21 22 class RenderState(minX: Double, maxX: Double, viewWidth: Int) { 23 var scale: Double 24 private set 25 var panX: Double 26 private set 27 28 val listeners = mutableListOf<() -> Unit>() 29 30 init { 31 panX = minX 32 scale = viewWidth / (maxX - minX) 33 } 34 35 private fun notifyListeners() { 36 listeners.forEach { it() } 37 } 38 39 fun xViewToWorld(x: Int) = (x / scale) + panX 40 41 fun zoomBy(amount: Double, viewX: Int) { 42 val worldX = xViewToWorld(viewX) 43 val endScale = scale * amount 44 animate(scale, endScale) { 45 scale = it 46 panX = worldX - (viewX / scale) 47 notifyListeners() 48 } 49 } 50 51 fun pan(deltaX: Int) { 52 val endPanX = panX + (deltaX / scale) 53 animate(panX, endPanX) { value -> 54 panX = value 55 notifyListeners() 56 } 57 } 58 }