2
2
3
3
import de .jensd .fx .glyphs .materialdesignicons .MaterialDesignIcon ;
4
4
import javafx .application .Platform ;
5
+ import javafx .beans .binding .Bindings ;
5
6
import javafx .beans .property .SimpleDoubleProperty ;
6
7
import javafx .beans .property .SimpleObjectProperty ;
7
8
import javafx .geometry .Pos ;
9
+ import javafx .scene .Node ;
8
10
import javafx .scene .canvas .Canvas ;
9
11
import javafx .scene .canvas .GraphicsContext ;
10
12
import javafx .scene .control .Label ;
11
13
import javafx .scene .input .MouseEvent ;
12
14
import javafx .scene .layout .*;
15
+ import javafx .scene .text .TextAlignment ;
13
16
import javafx .scene .transform .Affine ;
17
+ import org .openbase .jul .iface .provider .LabelProvider ;
14
18
import org .openbase .jul .processing .StringProcessor ;
15
19
import org .openbase .jul .visual .javafx .geometry .svg .SVGGlyphIcon ;
16
20
import org .openbase .jul .visual .javafx .iface .DynamicPane ;
@@ -28,11 +32,16 @@ public class EmphasisControlTrianglePane extends BorderPane implements DynamicPa
28
32
private final Canvas canvas ;
29
33
private final Label emphasisLabel = new Label ("Emphasis" );
30
34
private final SVGGlyphIcon emphasisIcon ;
35
+ private final HBox triangleOuterHPane ;
31
36
private GraphicsContext gc ;
32
37
private double scale ;
38
+ private double xTranslate ;
39
+ private double yTranslate ;
33
40
private final Pane trianglePane ;
34
41
private volatile boolean emphasisStateUpdate ;
35
42
43
+ private EmphasisDescriptionProvider labelProvider = primaryEmphasisCategory -> "Optimize " + StringProcessor .transformFirstCharToUpperCase (primaryEmphasisCategory .name ().toLowerCase ());
44
+
36
45
public EmphasisControlTrianglePane () {
37
46
38
47
this .emphasisStateProperty = new SimpleObjectProperty <>(EmphasisState .getDefaultInstance ());
@@ -68,39 +77,12 @@ public EmphasisControlTrianglePane() {
68
77
69
78
this .gc = canvas .getGraphicsContext2D ();
70
79
71
- this .trianglePane .setMinHeight (50 );
72
- this .trianglePane .setMinWidth (50 );
73
-
74
80
this .trianglePane .setPrefSize (200 , 185 );
75
81
76
- //this.trianglePane.prefHeightProperty().bind(trianglePane.widthProperty());
77
-
78
- // this.trianglePane.prefWidthProperty()/;
79
-
80
82
this .canvas .setCache (true );
81
83
82
84
canvas .widthProperty ().bind (trianglePane .widthProperty ());
83
85
canvas .heightProperty ().bind (trianglePane .heightProperty ());
84
- // trianglePane.widthProperty().addListener((observable, oldValue, newWidth) -> {
85
- // if(newWidth.doubleValue() < trianglePane.getHeight()) {
86
- // this.canvas.widthProperty().unbind();
87
- // this.canvas.heightProperty().unbind();
88
- //
89
- // this.canvas.widthProperty().bind(trianglePane.widthProperty());
90
- // this.canvas.heightProperty().bind(trianglePane.widthProperty().multiply(0.85d));
91
- // }
92
- // });
93
- //
94
- // trianglePane.heightProperty().addListener((observable, oldValue, newHeight) -> {
95
- // if(newHeight.doubleValue() <= trianglePane.getWidth()) {
96
- //
97
- // this.canvas.widthProperty().unbind();
98
- // this.canvas.heightProperty().unbind();
99
- //
100
- // this.canvas.heightProperty().bind(trianglePane.heightProperty());
101
- // this.canvas.widthProperty().bind(trianglePane.heightProperty().multiply(1.15d));
102
- // }
103
- // });
104
86
105
87
this .trianglePane .heightProperty ().addListener ((observable , oldValue , newValue ) -> {
106
88
this .updateDynamicContent ();
@@ -123,6 +105,7 @@ public EmphasisControlTrianglePane() {
123
105
this .canvas .setOnMousePressed (event -> {
124
106
applyMousePositionUpdate (event .getX (), event .getY (), scale , true , gc );
125
107
event .consume ();
108
+ interactionProperty .set (event );
126
109
});
127
110
128
111
this .canvas .setOnMouseClicked (event -> {
@@ -137,18 +120,31 @@ public EmphasisControlTrianglePane() {
137
120
});
138
121
139
122
this .trianglePane .getChildren ().addAll (canvas , emphasisIcon );
123
+ AnchorPane .setTopAnchor (trianglePane , 0.0 );
124
+ AnchorPane .setBottomAnchor (trianglePane , 0.0 );
125
+ AnchorPane .setLeftAnchor (trianglePane , 0.0 );
126
+ AnchorPane .setRightAnchor (trianglePane , 0.0 );
127
+
128
+ final VBox triangleOuterVPane = new VBox ();
129
+ triangleOuterVPane .setAlignment (Pos .CENTER );
130
+ triangleOuterVPane .setFillWidth (true );
131
+ triangleOuterVPane .getChildren ().add (trianglePane );
140
132
141
- final HBox triangleOuterPane = new HBox ();
142
- triangleOuterPane .setAlignment (Pos .CENTER );
143
- triangleOuterPane .setFillHeight (true );
144
- triangleOuterPane .getChildren ().add (trianglePane );
133
+ triangleOuterHPane = new HBox ();
134
+ triangleOuterHPane .setAlignment (Pos .CENTER );
135
+ triangleOuterHPane .setFillHeight (true );
136
+ triangleOuterHPane .getChildren ().add (triangleOuterVPane );
145
137
146
- //innerPane.getChildren().add(triangleOuterPane);
138
+ trianglePane .prefHeightProperty ().bind (triangleOuterHPane .heightProperty ());
139
+ trianglePane .prefWidthProperty ().bind (triangleOuterHPane .widthProperty ());
140
+
141
+ emphasisLabel .setTextAlignment (TextAlignment .CENTER );
142
+ emphasisLabel .setStyle ("-fx-font-size: 20px; -fx-font-weight: bold;" );
147
143
148
144
final HBox labelBox = new HBox (emphasisLabel );
149
145
labelBox .setAlignment (Pos .CENTER );
150
146
151
- this .setCenter (triangleOuterPane );
147
+ this .setCenter (triangleOuterHPane );
152
148
this .setBottom (labelBox );
153
149
154
150
this .initContent ();
@@ -178,7 +174,7 @@ private void updateEmphasisCategory(Category primaryEmphasisCategory) {
178
174
emphasisIcon .setForegroundIcon (MaterialDesignIcon .FLASH );
179
175
break ;
180
176
}
181
- emphasisLabel .setText ("Optimize " + StringProcessor . transformFirstCharToUpperCase (primaryEmphasisCategory . name (). toLowerCase () ));
177
+ emphasisLabel .setText (labelProvider . getLabel (primaryEmphasisCategory ));
182
178
} else {
183
179
Platform .runLater (() -> {
184
180
switch (primaryEmphasisCategory ) {
@@ -195,7 +191,7 @@ private void updateEmphasisCategory(Category primaryEmphasisCategory) {
195
191
emphasisIcon .setForegroundIcon (MaterialDesignIcon .FLASH );
196
192
break ;
197
193
}
198
- emphasisLabel .setText ("Optimize " + StringProcessor . transformFirstCharToUpperCase (primaryEmphasisCategory . name (). toLowerCase () ));
194
+ emphasisLabel .setText (labelProvider . getLabel (primaryEmphasisCategory ));
199
195
});
200
196
}
201
197
}
@@ -219,21 +215,27 @@ private void computeEmphasisState() {
219
215
@ Override
220
216
public void updateDynamicContent () {
221
217
222
- scale = Math .min (trianglePane .getWidth (), trianglePane .getHeight ()) / (EMPHASIS_TRIANGLE_OUTER_LINE + EmphasisControlTriangle .PADDING * 2 );
223
- // System.out.println("canvas.getWidth(): " + canvas.getWidth());`
218
+ scale = Math .min (canvas .getWidth (), canvas .getHeight ()) / (EMPHASIS_TRIANGLE_OUTER_LINE + EmphasisControlTriangle .PADDING * 2 );
219
+ // System.out.println("canvas.getWidth(): " + canvas.getWidth());
224
220
// System.out.println("canvas.getHeight(): " + canvas.getHeight());
225
221
// System.out.println("getHeight(): " + getHeight());
226
222
// System.out.println("getWidth: " + getWidth());
227
223
// System.out.println("scale: " + scale);
224
+ // System.out.println("triangle size: " + EMPHASIS_TRIANGLE_OUTER_LINE);
225
+ // System.out.println("triangle size scaled: " + (EMPHASIS_TRIANGLE_OUTER_LINE * scale));
226
+ // System.out.println("triangle leftover: " + ((canvas.getWidth() - EMPHASIS_TRIANGLE_OUTER_LINE * scale)/2));
227
+
228
+ xTranslate = ((canvas .getWidth ()/scale - EMPHASIS_TRIANGLE_OUTER_LINE )/2 );
229
+ yTranslate = ((canvas .getHeight ()/scale - EMPHASIS_TRIANGLE_HEIGHT )/2 );
228
230
229
231
// reset previous scale value
230
232
gc .setTransform (new Affine ());
231
233
232
234
// set new scale
233
235
gc .scale (scale , scale );
234
236
235
- // translate regarding frame
236
- gc .translate (EmphasisControlTriangle . PADDING , EmphasisControlTriangle . PADDING );
237
+ // translate into center of canvas
238
+ gc .translate (xTranslate , yTranslate );
237
239
238
240
// initial triangle draw
239
241
emphasisControlTriangle .drawShapes (false , gc );
@@ -253,18 +255,17 @@ public void updateDynamicContent() {
253
255
}
254
256
255
257
private void applyMousePositionUpdate (final double sceneX , final double sceneY , final double scale , final boolean mouseClicked , final GraphicsContext gc ) {
256
- emphasisControlTriangle .updateHandlePosition (sceneX , sceneY , scale , mouseClicked , gc );
258
+ emphasisControlTriangle .updateHandlePosition (sceneX , sceneY , scale , xTranslate , yTranslate , mouseClicked , gc );
257
259
}
258
260
259
-
260
261
private void updateIcon (final boolean mouseClicked ) {
261
262
// setup icon animation
262
263
if (!mouseClicked ) {
263
264
emphasisIcon .setForegroundIconColorAnimated (emphasisControlTriangle .getEmphasisColor (), 2 );
264
265
}
265
266
266
- emphasisIcon .setLayoutX (emphasisControlTriangle .getHandlePosX () * scale - (emphasisIcon .getSize () / 2 ));
267
- emphasisIcon .setLayoutY (emphasisControlTriangle .getHandlePosY () * scale - (emphasisIcon .getSize () / 2 ));
267
+ emphasisIcon .setLayoutX (( emphasisControlTriangle .getHandlePosX () + xTranslate ) * scale - (emphasisIcon .getSize () / 2 ) - EmphasisControlTriangle . PADDING * scale );
268
+ emphasisIcon .setLayoutY (( emphasisControlTriangle .getHandlePosY () + yTranslate ) * scale - (emphasisIcon .getSize () / 2 ) - EmphasisControlTriangle . PADDING * scale );
268
269
}
269
270
270
271
@@ -289,6 +290,10 @@ public SimpleObjectProperty<MouseEvent> interactionProperty() {
289
290
}
290
291
291
292
public void setTrianglePrefSize (double prefWidth , double prefHeight ) {
292
- trianglePane .setPrefSize (prefWidth , prefHeight );
293
+ triangleOuterHPane .setPrefSize (prefWidth , prefHeight );
294
+ }
295
+
296
+ public void setLabelProvider (EmphasisDescriptionProvider labelProvider ) {
297
+ this .labelProvider = labelProvider ;
293
298
}
294
299
}
0 commit comments