Skip to content

Commit 210e6a3

Browse files
fix emphasis triangle scaling and center drawing. Make Text configurable.
1 parent d153fad commit 210e6a3

File tree

4 files changed

+60
-47
lines changed

4 files changed

+60
-47
lines changed

.idea/encodings.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/org/openbase/bco/bcozy/view/generic/EmphasisControlTriangle.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ public EmphasisControlTriangle() {
112112
double mousePosX;
113113
double mousePosY;
114114

115-
public void updateHandlePosition(final double sceneMousePosX, final double sceneMousePosY, final double scale, final boolean mouseClicked, final GraphicsContext gc) {
116-
mousePosX = (sceneMousePosX) / scale - PADDING;
117-
mousePosY = (sceneMousePosY) / scale - PADDING;
115+
public void updateHandlePosition(final double sceneMousePosX, final double sceneMousePosY, final double scale, final double xTranslate, final double yTranslate, final boolean mouseClicked, final GraphicsContext gc) {
116+
mousePosX = (sceneMousePosX) / scale - xTranslate;
117+
mousePosY = (sceneMousePosY) / scale - yTranslate;
118118

119119
// compute handle position
120120
if (contains(mousePosX, (EMPHASIS_TRIANGLE_HEIGHT - mousePosY))) {

src/main/java/org/openbase/bco/bcozy/view/generic/EmphasisControlTrianglePane.java

+49-44
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIcon;
44
import javafx.application.Platform;
5+
import javafx.beans.binding.Bindings;
56
import javafx.beans.property.SimpleDoubleProperty;
67
import javafx.beans.property.SimpleObjectProperty;
78
import javafx.geometry.Pos;
9+
import javafx.scene.Node;
810
import javafx.scene.canvas.Canvas;
911
import javafx.scene.canvas.GraphicsContext;
1012
import javafx.scene.control.Label;
1113
import javafx.scene.input.MouseEvent;
1214
import javafx.scene.layout.*;
15+
import javafx.scene.text.TextAlignment;
1316
import javafx.scene.transform.Affine;
17+
import org.openbase.jul.iface.provider.LabelProvider;
1418
import org.openbase.jul.processing.StringProcessor;
1519
import org.openbase.jul.visual.javafx.geometry.svg.SVGGlyphIcon;
1620
import org.openbase.jul.visual.javafx.iface.DynamicPane;
@@ -28,11 +32,16 @@ public class EmphasisControlTrianglePane extends BorderPane implements DynamicPa
2832
private final Canvas canvas;
2933
private final Label emphasisLabel = new Label("Emphasis");
3034
private final SVGGlyphIcon emphasisIcon;
35+
private final HBox triangleOuterHPane;
3136
private GraphicsContext gc;
3237
private double scale;
38+
private double xTranslate;
39+
private double yTranslate;
3340
private final Pane trianglePane;
3441
private volatile boolean emphasisStateUpdate;
3542

43+
private EmphasisDescriptionProvider labelProvider = primaryEmphasisCategory -> "Optimize " + StringProcessor.transformFirstCharToUpperCase(primaryEmphasisCategory.name().toLowerCase());
44+
3645
public EmphasisControlTrianglePane() {
3746

3847
this.emphasisStateProperty = new SimpleObjectProperty<>(EmphasisState.getDefaultInstance());
@@ -68,39 +77,12 @@ public EmphasisControlTrianglePane() {
6877

6978
this.gc = canvas.getGraphicsContext2D();
7079

71-
this.trianglePane.setMinHeight(50);
72-
this.trianglePane.setMinWidth(50);
73-
7480
this.trianglePane.setPrefSize(200, 185);
7581

76-
//this.trianglePane.prefHeightProperty().bind(trianglePane.widthProperty());
77-
78-
// this.trianglePane.prefWidthProperty()/;
79-
8082
this.canvas.setCache(true);
8183

8284
canvas.widthProperty().bind(trianglePane.widthProperty());
8385
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-
// });
10486

10587
this.trianglePane.heightProperty().addListener((observable, oldValue, newValue) -> {
10688
this.updateDynamicContent();
@@ -123,6 +105,7 @@ public EmphasisControlTrianglePane() {
123105
this.canvas.setOnMousePressed(event -> {
124106
applyMousePositionUpdate(event.getX(), event.getY(), scale, true, gc);
125107
event.consume();
108+
interactionProperty.set(event);
126109
});
127110

128111
this.canvas.setOnMouseClicked(event -> {
@@ -137,18 +120,31 @@ public EmphasisControlTrianglePane() {
137120
});
138121

139122
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);
140132

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);
145137

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;");
147143

148144
final HBox labelBox = new HBox(emphasisLabel);
149145
labelBox.setAlignment(Pos.CENTER);
150146

151-
this.setCenter(triangleOuterPane);
147+
this.setCenter(triangleOuterHPane);
152148
this.setBottom(labelBox);
153149

154150
this.initContent();
@@ -178,7 +174,7 @@ private void updateEmphasisCategory(Category primaryEmphasisCategory) {
178174
emphasisIcon.setForegroundIcon(MaterialDesignIcon.FLASH);
179175
break;
180176
}
181-
emphasisLabel.setText("Optimize " + StringProcessor.transformFirstCharToUpperCase(primaryEmphasisCategory.name().toLowerCase()));
177+
emphasisLabel.setText(labelProvider.getLabel(primaryEmphasisCategory));
182178
} else {
183179
Platform.runLater(() -> {
184180
switch (primaryEmphasisCategory) {
@@ -195,7 +191,7 @@ private void updateEmphasisCategory(Category primaryEmphasisCategory) {
195191
emphasisIcon.setForegroundIcon(MaterialDesignIcon.FLASH);
196192
break;
197193
}
198-
emphasisLabel.setText("Optimize " + StringProcessor.transformFirstCharToUpperCase(primaryEmphasisCategory.name().toLowerCase()));
194+
emphasisLabel.setText(labelProvider.getLabel(primaryEmphasisCategory));
199195
});
200196
}
201197
}
@@ -219,21 +215,27 @@ private void computeEmphasisState() {
219215
@Override
220216
public void updateDynamicContent() {
221217

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());
224220
// System.out.println("canvas.getHeight(): " + canvas.getHeight());
225221
// System.out.println("getHeight(): " + getHeight());
226222
// System.out.println("getWidth: " + getWidth());
227223
// 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);
228230

229231
// reset previous scale value
230232
gc.setTransform(new Affine());
231233

232234
// set new scale
233235
gc.scale(scale, scale);
234236

235-
// translate regarding frame
236-
gc.translate(EmphasisControlTriangle.PADDING, EmphasisControlTriangle.PADDING);
237+
// translate into center of canvas
238+
gc.translate(xTranslate, yTranslate);
237239

238240
// initial triangle draw
239241
emphasisControlTriangle.drawShapes(false, gc);
@@ -253,18 +255,17 @@ public void updateDynamicContent() {
253255
}
254256

255257
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);
257259
}
258260

259-
260261
private void updateIcon(final boolean mouseClicked) {
261262
// setup icon animation
262263
if (!mouseClicked) {
263264
emphasisIcon.setForegroundIconColorAnimated(emphasisControlTriangle.getEmphasisColor(), 2);
264265
}
265266

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);
268269
}
269270

270271

@@ -289,6 +290,10 @@ public SimpleObjectProperty<MouseEvent> interactionProperty() {
289290
}
290291

291292
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;
293298
}
294299
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.openbase.bco.bcozy.view.generic;
2+
3+
import org.openbase.type.domotic.action.ActionEmphasisType.ActionEmphasis.Category;
4+
5+
public interface EmphasisDescriptionProvider {
6+
String getLabel(Category primaryCategory);
7+
}

0 commit comments

Comments
 (0)