-
Notifications
You must be signed in to change notification settings - Fork 0
/
Context.java
250 lines (232 loc) · 8.99 KB
/
Context.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*******************************************************************************
* Copyright (C) 2019-2020 ROMAINPC
* Copyright (C) 2019-2020 Picachoc
*
* This file is part of PICROM-Wars
*
* PICROM-Wars is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PICROM-Wars is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package picrom.gameboard;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.When;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Region;
import javafx.scene.shape.Rectangle;
/**
* JavaFX layout, just an area defined by the coordinates of its top left
* corner, its height and its width.
*
* All this values are Properties, use it to bind your JavaFX components.
*/
public class Context extends Group {
private SimpleDoubleProperty X;
private SimpleDoubleProperty Y;
private SimpleDoubleProperty width;
private SimpleDoubleProperty height;
private SimpleDoubleProperty largerWidth;
private SimpleDoubleProperty largerHeight;
/**
* Create context with empty properties.
*/
public Context() {
X = new SimpleDoubleProperty();
Y = new SimpleDoubleProperty();
this.width = new SimpleDoubleProperty();
this.height = new SimpleDoubleProperty();
}
/**
* Create context with the following parameters as first values for properties.
*
* @param x top left x
* @param y top left y
* @param width area width
* @param height area height
*/
public Context(double x, double y, double width, double height) {
this();
X.set(x);
Y.set(y);
this.width.set(width);
this.height.set(height);
}
/**
* Create Context and bind its top left corner and its size to the following
* properties.
*
* @param x top left x
* @param y top left y
* @param width area width
* @param height area height
*/
public Context(ReadOnlyDoubleProperty x, ReadOnlyDoubleProperty y, ReadOnlyDoubleProperty width,
ReadOnlyDoubleProperty height) {
this();
X.bind(x);
Y.bind(y);
this.width.bind(width);
this.height.bind(height);
}
/**
* Context with preserved ratio, context property may be different from property
* passed, however there is always a binding.
*
* May lead to empty area around the context area.
*
* @param x top left x
* @param y top left y
* @param width area width
* @param height area height
* @param ratio Wanted preserved ratio, Context surface will cover the maximum
* area but whithout affect this ratio. Ratio = Width / Height
*/
public Context(ReadOnlyDoubleProperty x, ReadOnlyDoubleProperty y, ReadOnlyDoubleProperty width,
ReadOnlyDoubleProperty height, double ratio) {
this();
When condition = Bindings.when((width.divide(height)).greaterThanOrEqualTo(ratio));
// The purpose of this bind is to compare format of the area to decide to have
// empty area at the top/bottom or at the right/left pf the Context.
this.width.bind(condition.then(this.height.multiply(ratio)).otherwise(width));
this.height.bind(condition.then(height).otherwise(this.width.divide(ratio)));
X.bind(x.add((width.subtract(this.width)).divide(2)));
Y.bind(y.add((height.subtract(this.height)).divide(2)));
largerWidth = new SimpleDoubleProperty();
largerWidth.bind(width);
largerHeight = new SimpleDoubleProperty();
largerHeight.bind(height);
}
/**
* @return Top Left X coordinate of the larger part of the context.
*/
public SimpleDoubleProperty xProperty() {
return X;
}
/**
* @return Top Left Y coordinate of the larger part of the context.
*/
public SimpleDoubleProperty yProperty() {
return Y;
}
/**
* @return Width of the used context, may be reduced if ratio is preserved.
*/
public SimpleDoubleProperty widthProperty() {
return width;
}
/**
* @return Height of the used context, may be reduced if ratio is preserved.
*/
public SimpleDoubleProperty heightProperty() {
return height;
}
/**
* @return Larger width possible, may be larger than
* {@link Context#widthProperty()} if ratio is preserved.
*/
public SimpleDoubleProperty largerWidthProperty() {
return largerWidth;
}
/**
* @return Larger Height possible, may be larger than
* {@link Context#heightProperty()} if ratio is preserved.
*/
public SimpleDoubleProperty largerHeightProperty() {
return largerHeight;
}
/**
* Function to anchor a shape in the context.
*
* @param s The shape to anchor
* @param xRatio X position in the context (value between 0.0 and 1.0)
* @param yRatio Y position in the context (value between 0.0 and 1.0)
* @param widthRatio Fraction of the Width of the context (value between 0.0
* and 1.0)
* @param heightRatio Fraction of the height of the context (value between 0.0
* and 1.0)
*/
public void bindIn(Rectangle s, double xRatio, double yRatio, double widthRatio, double heightRatio) {
s.xProperty().bind(X.add(width.multiply(xRatio)));
s.yProperty().bind(Y.add(height.multiply(yRatio)));
s.widthProperty().bind(width.multiply(widthRatio));
s.heightProperty().bind(height.multiply(heightRatio));
}
/**
* Function to anchor a ImageView in the context.
*
* @param iV The ImageView to anchor
* @param xRatio X position in the context (value between 0.0 and 1.0)
* @param yRatio Y position in the context (value between 0.0 and 1.0)
* @param widthRatio Fraction of the Width of the context (value between 0.0
* and 1.0)
* @param heightRatio Fraction of the height of the context (value between 0.0
* and 1.0)
*/
public void bindIn(ImageView iV, double xRatio, double yRatio, double widthRatio, double heightRatio) {
iV.xProperty().bind(X.add(width.multiply(xRatio)));
iV.yProperty().bind(Y.add(height.multiply(yRatio)));
iV.fitWidthProperty().bind(width.multiply(widthRatio));
iV.fitHeightProperty().bind(height.multiply(heightRatio));
}
/**
* Function to anchor a Region in the context.
*
* @param r The Region to anchor
* @param xRatio X position in the context (value between 0.0 and 1.0)
* @param yRatio Y position in the context (value between 0.0 and 1.0)
* @param widthRatio Fraction of the Width of the context (value between 0.0
* and 1.0)
* @param heightRatio Fraction of the height of the context (value between 0.0
* and 1.0)
*/
public void bindIn(Region r, double xRatio, double yRatio, double widthRatio, double heightRatio) {
r.layoutXProperty().bind(X.add(width.multiply(xRatio)));
r.layoutYProperty().bind(Y.add(height.multiply(yRatio)));
r.prefWidthProperty().bind(width.multiply(widthRatio));
r.prefHeightProperty().bind(height.multiply(heightRatio));
r.maxHeightProperty().bind(height.multiply(heightRatio));
r.minHeightProperty().bind(height.multiply(heightRatio));
}
/**
* Function to anchor a Label in the context relative to the center of the
* label;
*
* @param l The Region to anchor
* @param xRatio X position in the context (value between 0.0 and 1.0)
* @param yRatio Y position in the context (value between 0.0 and 1.0)
*/
public void bindCenterIn(Label l, double xRatio, double yRatio) {
l.layoutXProperty().bind(X.add(width.multiply(xRatio).subtract(l.widthProperty().divide(2))));
l.layoutYProperty().bind(Y.add(height.multiply(yRatio).subtract(l.heightProperty().divide(2))));
}
/**
* Function to anchor a ImageView in the context relative to the center of it.
*
* @param iV The ImageView to anchor
* @param xRatio X position in the context (value between 0.0 and 1.0)
* @param yRatio Y position in the context (value between 0.0 and 1.0)
* @param widthRatio Fraction of the Width of the context (value between 0.0
* and 1.0)
* @param heightRatio Fraction of the height of the context (value between 0.0
* and 1.0)
*/
public void bindCenterIn(ImageView iV, double xRatio, double yRatio, double widthRatio, double heightRatio) {
iV.xProperty().bind(X.add(width.multiply(xRatio).subtract(iV.fitWidthProperty().divide(2))));
iV.yProperty().bind(Y.add(height.multiply(yRatio).subtract(iV.fitHeightProperty().divide(2))));
iV.fitWidthProperty().bind(width.multiply(widthRatio));
iV.fitHeightProperty().bind(height.multiply(heightRatio));
}
}