onClick()
event for each button.static propsDefinition = {
ball: { type: PropTypes.Entity },
};
MyButton()
function, you have the following code:type MyButtonProps = {
label: string;
onClick: Callback;
style: ViewStyle;
baseColor: string;
};
function MyButton(props: MyButtonProps): UINode {
const DEFAULT_COLOR = props.baseColor;
const HOVERED_COLOR = "blue";
const backgroundColor = new Binding<string>(DEFAULT_COLOR);
const buttonText = new Binding<string>(props.label);
return Pressable({
children: Text({
text: buttonText,
style: {color: "white"},
}),
onClick: props.onClick,
onEnter: (player: Player) => {
console.log("onEnter";
backgroundColor.set(HOVERED_COLOR, [player]);
buttonText.set("hovered", [player]);
},
onExit: (player: Player) => {
console.log("onExit");
backgroundColor.set(DEFAULT_COLOR, [player]);
buttonText.set(props.label, [player]);
},
style: {
backgroundColor: backgroundColor,
borderRadius: 8,
height: 36,
width: 120,
alignItems: "center",
justifyContent: "center",
// additional styles are spreaded the last
// to override default styles
...props.style,
},
});
}
MyButtonProps
contains four fields:label: string;
onClick: Callback;
style: ViewStyle;
baseColor: string;
Field | Description |
---|---|
label | Text label that appears on the button. |
onClick | The onClick() event is defined as a callback to a function defined in the call to the MyButton function. |
style | This is a customUI object type called ViewStyle , which enables styling of a UI View definition with JavaScript-like styling attributes. |
baseColor | Text string identifying the base color for the button |
const colorOff: Color = new Color(0, 0, 1.0)
const colorRed: Color = new Color(0.8, 0, 0)
const colorGreen: Color = new Color (0, 0.8, 0)
style
properties, which include the color properties.initializeUI()
method, you can see the internal variable that is defined to hold the Sphere entity from the hz.props.ball
property value as a MeshEntity object. If this entity is valid, then the styling properties are applied to set the default color for the sphere:const myBall = this.props.ball?.as(MeshEntity)!
if (myBall) {
myBall.style.tintStrength.set(1)
myBall.style.brightness.set(100)
myBall.style.tintColor.set(colorOff)
}
View()
objects. The second, nested View()
object is defined as follows:View({
children: [
MyButton({
label: "Off",
baseColor: "black",
onClick: () => {
console.log("Pressed Off button.");
myBall.color.set(new Color(colorOff)); // resets ball color to default.
},
style: {
//backgroundColor: "#CF1313",
marginRight: 24,
},
}),
MyButton({
label: "Red",
baseColor: "red",
onClick: () => {
console.log("Pressed Red button.");
myBall.color.set(new Color(colorRed));
},
style: {
//backgroundColor: "#CF1313",
marginRight: 24,
},
}),
MyButton({
label: "Green",
baseColor: "green",
onClick: () => {
console.log("Pressed Green button.");
myBall.color.set(new Color(colorGreen));
},
style: {
// backgroundColor: "#19AD0E",
},
}),
],
style: { flexDirection: "row", marginTop: 12 },
}),
MyButton()
, which returns a Pressable object inserted into the View()
. As part of each call, you can see the parameters that are passed into the function.onClick()
event for each button the arrow function that is called back from the MyFunction()
function. This callback functionality is enabled through the type definition for MyButtonProps
.MyButton()
function:
onClick()
property as a callback to MyButtonProps
.props.onClick
in the MyButtonProps
definition.Callback
type, you can define the callback function as an arrow function in the parameters of the call you make to MyButton()
function from within the View()
in your initializeUI()
method.