|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.gfx; |
|
7 |
|
8 import org.mozilla.gecko.util.FloatUtils; |
|
9 |
|
10 import org.json.JSONException; |
|
11 import org.json.JSONObject; |
|
12 |
|
13 public class FloatSize { |
|
14 public final float width, height; |
|
15 |
|
16 public FloatSize(FloatSize size) { width = size.width; height = size.height; } |
|
17 public FloatSize(IntSize size) { width = size.width; height = size.height; } |
|
18 public FloatSize(float aWidth, float aHeight) { width = aWidth; height = aHeight; } |
|
19 |
|
20 public FloatSize(JSONObject json) { |
|
21 try { |
|
22 width = (float)json.getDouble("width"); |
|
23 height = (float)json.getDouble("height"); |
|
24 } catch (JSONException e) { |
|
25 throw new RuntimeException(e); |
|
26 } |
|
27 } |
|
28 |
|
29 @Override |
|
30 public String toString() { return "(" + width + "," + height + ")"; } |
|
31 |
|
32 public boolean isPositive() { |
|
33 return (width > 0 && height > 0); |
|
34 } |
|
35 |
|
36 public boolean fuzzyEquals(FloatSize size) { |
|
37 return (FloatUtils.fuzzyEquals(size.width, width) && |
|
38 FloatUtils.fuzzyEquals(size.height, height)); |
|
39 } |
|
40 |
|
41 public FloatSize scale(float factor) { |
|
42 return new FloatSize(width * factor, height * factor); |
|
43 } |
|
44 |
|
45 /* |
|
46 * Returns the size that represents a linear transition between this size and `to` at time `t`, |
|
47 * which is on the scale [0, 1). |
|
48 */ |
|
49 public FloatSize interpolate(FloatSize to, float t) { |
|
50 return new FloatSize(FloatUtils.interpolate(width, to.width, t), |
|
51 FloatUtils.interpolate(height, to.height, t)); |
|
52 } |
|
53 } |
|
54 |