Current section

Files

Jump to
cadre src cadre component.gleam
Raw

src/cadre/component.gleam

//// View on [A-Frame document](https://aframe.io/docs/master/core/component.html)
import gleam/float
import gleam/int
import gleam/json
import gleam/list
import gleam/string
import lustre/attribute.{type Attribute}
import vec/vec2f.{type Vec2f}
import vec/vec3f.{type Vec3f}
import vec/vec4f.{type Vec4f}
/// View on [A-Frame document](https://aframe.io/docs/master/core/component.html#property-types)
///
pub type Property {
Property(value: String)
Boolean(value: Bool)
Integer(value: Int)
Number(value: Float)
Vec2(value: Vec2f)
Vec3(value: Vec3f)
Vec4(value: Vec4f)
Array(values: List(Property))
}
/// Create an A-Frame attribute. This is like saying
/// `element.setAttribute("position", "0 0 0")` in JavaScript. Attributes will
/// be rendered when calling [`element.to_string`](./element.html#to_string).
///
pub fn property(name: String, property: Property) -> Attribute(message) {
attribute.attribute(name, property |> property_to_string)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/anchored.html)
///
pub fn anchored(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("anchored", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/animation.html)
///
pub fn animation(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("animation", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/animation.html)
///
pub fn animation_(
id: String,
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("animation__" <> id, properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/ar-hit-test.html)
///
pub fn ar_hit_test(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("ar-hit-test", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/background.html)
///
pub fn background(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("background", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/camera.html)
///
pub fn camera(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("camera", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/cursor.html)
///
pub fn cursor(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("cursor", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/debug.html)
///
pub fn debug(is_debug: Bool) -> Attribute(message) {
boolean_attribute("debug", is_debug)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/device-orientation-permission-ui.html)
///
pub fn device_orientation_permission_ui(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("device-orientation-permission-ui", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/embedded.html)
///
pub fn embedded(is_embedded: Bool) -> Attribute(message) {
boolean_attribute("embedded", is_embedded)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/fog.html)
///
pub fn fog(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("fog", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/geometry.html)
///
pub fn geometry(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("geometry", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/gltf-model.html)
///
pub fn gltf_model(value: String) -> Attribute(message) {
attribute.attribute("gltf-model", value)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/hand-controls.html)
///
pub fn hand_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("hand-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/hand-tracking-controls.html)
///
pub fn hand_tracking_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("hand-tracking-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/hand-tracking-grab-controls.html)
///
pub fn hand_tracking_grab_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("hand-tracking-grab-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/hide-on-enter-ar.html)
///
pub fn hide_on_enter_ar(is_hide_on_enter_ar: Bool) -> Attribute(message) {
boolean_attribute("hide-on-enter-ar", is_hide_on_enter_ar)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/hide-on-enter-vr.html)
///
pub fn hide_on_enter_vr(is_hide_on_enter_vr: Bool) -> Attribute(message) {
boolean_attribute("hide-on-enter-vr", is_hide_on_enter_vr)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/keyboard-shortcuts.html)
///
pub fn keyboard_shortcuts(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("keyboard-shortcuts", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/laser-controls.html)
///
pub fn laser_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("laser-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/layer.html)
///
pub fn layer(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("layer", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/light.html)
///
pub fn light(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("light", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/line.html)
///
pub fn line(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("line", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/line.html)
///
pub fn line_(
id: String,
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("line__" <> id, properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/link.html)
///
pub fn link(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("link", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/loading-screen.html)
///
pub fn loading_screen(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("loading-screen", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/logitech-mx-ink-controls.html)
///
pub fn logitech_mx_ink_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("logitech-mx-ink-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/look-controls.html)
///
pub fn look_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("look-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/magicleap-controls.html)
///
pub fn magicleap_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("magicleap-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/material.html)
///
pub fn material(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("material", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/meta-touch-controls.html)
///
pub fn meta_touch_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("meta-touch-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/obb-collider.html)
///
pub fn obb_collider(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("obb-collider", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/obj-model.html)
///
pub fn obj_model(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("obj-model", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/pool.html)
///
pub fn pool(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("pool", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/pool.html)
///
pub fn pool_(
id: String,
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("pool__" <> id, properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/position.html)
///
pub fn position(position: Vec3f) -> Attribute(message) {
attribute.attribute("position", position |> vec3_to_string)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/raycaster.html)
///
pub fn raycaster(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("raycaster", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/real-world-meshing.html)
///
pub fn real_world_meshing(is_real_world_meshing: Bool) -> Attribute(message) {
boolean_attribute("real-world-meshing", is_real_world_meshing)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/reflection.html)
///
pub fn reflection(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("reflection", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/renderer.html)
///
pub fn renderer(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("renderer", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/rotation.html)
///
pub fn rotation(rotation: Vec3f) -> Attribute(message) {
attribute.attribute("rotation", rotation |> vec3_to_string)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/scale.html)
///
pub fn scale(scale: Vec3f) -> Attribute(message) {
attribute.attribute("scale", scale |> vec3_to_string)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/screenshot.html)
///
pub fn screenshot(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("screenshot", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/shadow.html)
///
pub fn shadow(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("shadow", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/sound.html)
///
pub fn sound(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("sound", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/stats.html)
///
pub fn stats(is_stats: Bool) -> Attribute(message) {
boolean_attribute("stats", is_stats)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/text.html)
///
pub fn text(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("text", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/tracked-controls.html)
///
pub fn tracked_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("tracked-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/visible.html)
///
pub fn visible(visible: Bool) -> Attribute(message) {
attribute.attribute("visible", visible |> bool_to_string)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/vive-controls.html)
///
pub fn vive_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("vive-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/vive-focus-controls.html)
///
pub fn vive_focus_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("vive-focus-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/wasd-controls.html)
///
pub fn wasd_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("wasd-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/webxr.html)
///
pub fn webxr(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("webxr", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/windows-motion-controls.html)
///
pub fn windows_motion_controls(
properties: List(#(String, Property)),
) -> Attribute(message) {
properties_attribute("windows-motion-controls", properties)
}
/// View on [A-Frame document](https://aframe.io/docs/master/components/xr-mode-ui.html)
///
pub fn xr_mode_ui(properties: List(#(String, Property))) -> Attribute(message) {
properties_attribute("xr-mode-ui", properties)
}
fn boolean_attribute(name: String, value: Bool) -> Attribute(message) {
case value {
True -> attribute.attribute(name, "")
False -> attribute.property(name, json.bool(False))
}
}
fn properties_attribute(
name: String,
properties: List(#(String, Property)),
) -> Attribute(message) {
attribute.attribute(name, do_properties(properties, ""))
}
fn do_properties(
properties: List(#(String, Property)),
styles: String,
) -> String {
case properties {
[] -> styles
[#(name, property), ..rest] -> {
do_properties(
rest,
styles <> name <> ":" <> property_to_string(property) <> ";",
)
}
}
}
fn property_to_string(property: Property) -> String {
case property {
Property(value:) -> value
Boolean(value: True) -> "true"
Boolean(value: False) -> "false"
Integer(value:) -> value |> int.to_string
Number(value:) -> value |> float.to_string
Vec2(value:) -> value |> vec2_to_string
Vec3(value:) -> value |> vec3_to_string
Vec4(value:) -> value |> vec4_to_string
Array(values:) ->
values |> list.map(property_to_string) |> string.join(", ")
}
}
fn bool_to_string(bool: Bool) -> String {
case bool {
True -> "true"
False -> "false"
}
}
fn vec2_to_string(vector: Vec2f) -> String {
""
<> { vector.x |> float.to_string }
<> " "
<> { vector.y |> float.to_string }
}
fn vec3_to_string(vector: Vec3f) -> String {
""
<> { vector.x |> float.to_string }
<> " "
<> { vector.y |> float.to_string }
<> " "
<> { vector.z |> float.to_string }
}
fn vec4_to_string(vector: Vec4f) -> String {
""
<> { vector.x |> float.to_string }
<> " "
<> { vector.y |> float.to_string }
<> " "
<> { vector.z |> float.to_string }
<> " "
<> { vector.w |> float.to_string }
}