1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 package org.libreoffice;
10 
11 import android.graphics.Rect;
12 import android.graphics.RectF;
13 
14 import org.mozilla.gecko.gfx.IntSize;
15 
16 /**
17  * Identifies the tile by its position (x and y coordinate on the document), zoom and tile size (currently static)
18  */
19 public class TileIdentifier {
20     public final int x;
21     public final int y;
22     public final float zoom;
23     public final IntSize size;
24 
TileIdentifier(int x, int y, float zoom, IntSize size)25     public TileIdentifier(int x, int y, float zoom, IntSize size) {
26         this.x = x;
27         this.y = y;
28         this.zoom = zoom;
29         this.size = size;
30     }
31 
32     /**
33      * Returns a rectangle of the tiles position in scaled coordinates.
34      */
getRectF()35     public RectF getRectF() {
36         return new RectF(x, y, x + size.width, y + size.height);
37     }
38 
39     /**
40      * Returns a rectangle of the tiles position in non-scaled coordinates (coordinates as the zoom would be 1).
41      */
getCSSRectF()42     public RectF getCSSRectF() {
43         float cssX = x / zoom;
44         float cssY = y / zoom;
45         float cssSizeW = size.width / zoom;
46         float cssSizeH = size.height / zoom;
47         return new RectF(cssX, cssY, cssX + cssSizeW, cssY + cssSizeH);
48     }
49 
50     /**
51      * Returns an integer rectangle of the tiles position in non-scaled and rounded coordinates (coordinates as the zoom would be 1).
52      */
getCSSRect()53     public Rect getCSSRect() {
54         float cssX = x / zoom;
55         float cssY = y / zoom;
56         float sizeW = size.width / zoom;
57         float sizeH = size.height / zoom;
58         return new Rect(
59                 (int) cssX, (int) cssY,
60                 (int) (cssX + sizeW),
61                 (int) (cssY + sizeH) );
62     }
63 
64     @Override
equals(Object o)65     public boolean equals(Object o) {
66         if (this == o) return true;
67         if (o == null || getClass() != o.getClass()) return false;
68 
69         TileIdentifier that = (TileIdentifier) o;
70 
71         if (x != that.x) return false;
72         if (y != that.y) return false;
73         if (Float.compare(that.zoom, zoom) != 0) return false;
74 
75         return true;
76     }
77 
78     @Override
hashCode()79     public int hashCode() {
80         int result = x;
81         result = 31 * result + y;
82         result = 31 * result + (zoom != +0.0f ? Float.floatToIntBits(zoom) : 0);
83         return result;
84     }
85 
86     @Override
toString()87     public String toString() {
88         return String.format("TileIdentifier (%d, %d) z=%f s=(%d, %d)", x, y, zoom, size.width, size.height);
89     }
90 }
91 
92 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
93