Skip to content

Commit

Permalink
feat: the initial framework for PropertyMapView is implemented, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Mar 22, 2024
1 parent 4c740dd commit 7e4b1de
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 184 deletions.
76 changes: 38 additions & 38 deletions fxgl-samples/src/main/java/sandbox/view/PropertyMapViewSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,72 @@
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.core.collection.PropertyMap;
import com.almasb.fxgl.ui.PropertyMapView;
import com.almasb.fxgl.core.math.Vec2;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;

import static com.almasb.fxgl.dsl.FXGL.*;

public class PropertyMapViewSample extends GameApplication {

private enum TestEnum {
ONE,TWO,THREE
}

PropertyMap map = new PropertyMap();
private PropertyMap map = new PropertyMap();

@Override
protected void initSettings(GameSettings settings) {
settings.setWidth(800);
settings.setHeight(600);
settings.setTitle("PropertyMapViewSample");
settings.setVersion("0.1");
}

protected void initSettings(GameSettings settings) { }

@Override
protected void initGame() {
entityBuilder()
.view("background.png")
.buildAndAttach();
map.setValue("Music Volume", 10);
map.setValue("Camera Distance",120);
map.setValue("Initial Health", 100.0);
map.setValue("Initial Position", new Point2D(0, 0));
map.setValue("Enum Test", TestEnum.ONE);
map.setValue("Color Blind", false);
map.setValue("Player Name", "Jamie");

Node display = new PropertyMapView(map);
display.setLayoutX(getAppWidth() / 2);
getGameScene().addUINode(display);

protected void initInput() {
onKeyDown(KeyCode.G, "map", () -> {
for (String key : map.keys()) {
debug(key + " - " + map.getValue(key).toString());
System.out.println(key + " - " + map.getValue(key).toString());
}
});
onKeyDown(KeyCode.DIGIT1, "1", () -> {
onKeyDown(KeyCode.DIGIT1, () -> {
map.setValue("Enum Test", TestEnum.ONE);
map.setValue("Color Blind", true);
map.setValue("Initial Health", 1.0);
map.setValue("Player Name", "Jamie");
map.setValue("Main color", Color.ORANGE);
map.setValue("Initial Position Point2D", new Point2D(5, 1));
map.setValue("Initial Position Vec2", new Vec2(5, 1));
});
onKeyDown(KeyCode.DIGIT2, "2", () -> {
onKeyDown(KeyCode.DIGIT2, () -> {
map.setValue("Enum Test", TestEnum.TWO);
map.setValue("Color Blind", false);
map.setValue("Initial Health", 100.0);
map.setValue("Player Name", "Dave");
});
onKeyDown(KeyCode.DIGIT3, "3", () -> {
map.setValue("Enum Test", TestEnum.THREE);
map.setValue("Color Blind", true);
map.setValue("Initial Health", 123.45);
map.setValue("Player Name", "Christ");
map.setValue("Main color", Color.RED);
map.setValue("Initial Position Point2D", new Point2D(-6, 2));
map.setValue("Initial Position Vec2", new Vec2(-6, 2));
});
}

@Override
protected void initGame() {
getGameScene().setBackgroundColor(Color.DARKGRAY);

map.setValue("Music Volume", 10);
map.setValue("Camera Distance", 120);
map.setValue("Initial Health", 100.0);
map.setValue("Initial Position Point2D", new Point2D(0, 0));
map.setValue("Initial Position Vec2", new Vec2(0, 0));
map.setValue("Enum Test", TestEnum.ONE);
map.setValue("Color Blind", false);
map.setValue("Player Name", "Jamie");
map.setValue("Main color", Color.BLUE);

public static void main(String[] args) {
launch(args);
Node view = getUIFactoryService().newPropertyMapView(map);

addUINode(view, 100, 100);
}

private enum TestEnum {
ONE,TWO,THREE
public static void main(String[] args) {
launch(args);
}
}
125 changes: 0 additions & 125 deletions fxgl-scene/src/main/java/com/almasb/fxgl/ui/PropertyMapView.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.almasb.fxgl.ui;

import com.almasb.fxgl.core.EngineService;
import com.almasb.fxgl.core.collection.PropertyMap;
import javafx.beans.binding.StringBinding;
import javafx.beans.binding.StringExpression;
import javafx.collections.ObservableList;
Expand Down Expand Up @@ -67,4 +68,6 @@ public abstract class UIFactoryService extends EngineService {
public abstract FXGLTextFlow newTextFlow();

public abstract Node newPropertyView(String propertyName, Object property);

public abstract Node newPropertyMapView(PropertyMap map);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB ([email protected]).
* See LICENSE for details.
*/

package com.almasb.fxgl.ui.property;

import javafx.beans.property.ObjectProperty;
import javafx.geometry.Point2D;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;

/**
* // TODO: read-only version?
*
* @author Almas Baimagambetov ([email protected])
*/
public class Point2DPropertyViewFactory implements PropertyViewFactory<Point2D, HBox> {

private boolean ignoreChangeView = false;
private boolean ignoreChangeProperty = false;

@Override
public HBox makeView(ObjectProperty<Point2D> value) {
var fieldX = new TextField();
var fieldY = new TextField();
HBox view = new HBox(fieldX, fieldY);

value.addListener((obs, o, newValue) -> {
if (ignoreChangeProperty)
return;

onPropertyChanged(value, view);
});

fieldX.textProperty().addListener((obs, o, x) -> {
if (ignoreChangeView)
return;

onViewChanged(value, view);
});

fieldY.textProperty().addListener((obs, o, y) -> {
if (ignoreChangeView)
return;

onViewChanged(value, view);
});

onPropertyChanged(value, view);

return view;
}

@Override
public void onPropertyChanged(ObjectProperty<Point2D> value, HBox view) {
var fieldX = (TextField) view.getChildren().get(0);
var fieldY = (TextField) view.getChildren().get(1);

ignoreChangeView = true;

fieldX.setText(Double.toString(value.getValue().getX()));
fieldY.setText(Double.toString(value.getValue().getY()));

ignoreChangeView = false;
}

@Override
public void onViewChanged(ObjectProperty<Point2D> value, HBox view) {
var fieldX = (TextField) view.getChildren().get(0);
var fieldY = (TextField) view.getChildren().get(1);

ignoreChangeProperty = true;

var newPoint = new Point2D(
Double.parseDouble(fieldX.getText()),
Double.parseDouble(fieldY.getText())
);

value.setValue(newPoint);

ignoreChangeProperty = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,32 @@
* See LICENSE for details.
*/

package com.almasb.fxgl.ui;
package com.almasb.fxgl.ui.property;

import javafx.beans.property.ObjectProperty;
import javafx.scene.Node;

/**
* @author Almas Baimagambetov ([email protected])
*/
public interface PropertyViewChangeListener<T, V extends Node> {
public interface PropertyViewFactory<T, V extends Node> {

default V makeViewInternal(ObjectProperty<?> v) {
return makeView((ObjectProperty<T>) v);
}

/**
* @return the visual representation of the property [value]
*/
V makeView(ObjectProperty<T> value);


/**
* Called when the property has changed, so that the view can be updated.
*/
void onPropertyChanged(ObjectProperty<T> value, V view);


/**
* Called when the view has changed, so that the property can be updated.
*/
void onViewChanged(ObjectProperty<T> value, V view);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@

import com.almasb.fxgl.core.collection.UpdatableObjectProperty;
import com.almasb.fxgl.core.math.Vec2;
import com.almasb.fxgl.ui.PropertyViewChangeListener;
import javafx.beans.property.ObjectProperty;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;

/**
* // TODO: read-only version?
* // TODO: empty String check when view is updated
*
* @author Almas Baimagambetov ([email protected])
*/
public class Vec2PropertyViewChangeListener implements PropertyViewChangeListener<Vec2, HBox> {
public class Vec2PropertyViewFactory implements PropertyViewFactory<Vec2, HBox> {

private boolean ignoreChangeView = false;
private boolean ignoreChangeProperty = false;
Expand Down
Loading

0 comments on commit 7e4b1de

Please sign in to comment.