🚍 buttons and lights 🍆

This commit is contained in:
Matei Adriel 2019-06-03 22:32:08 +00:00
parent ce5005f704
commit 6e93a23ec2
5 changed files with 70 additions and 22 deletions

View file

@ -28,7 +28,7 @@ export class Component {
private strokeColor = "#888888"
private inputs: number
private outputs: number
private activation: (ctx: activationContext) => any
private activation: ((ctx: activationContext) => any)[] = []
private subscriptions: Subscription[] = []
public inputPins: Pin[] = []
@ -58,14 +58,17 @@ export class Component {
this.inputPins = [...Array(this.inputs)].fill(true).map(() => new Pin(false, this))
this.outputPins = [...Array(this.outputs)].fill(true).map(() => new Pin(true, this))
this.activation = new Function(`return (ctx) => {
this.activation = [data.activation, data.onclick ? data.onclick : ""]
.map(val => {
return new Function(`return (ctx) => {
try{
${data.activation}
${val}
}
catch(err){
ctx.error(err,"",ctx.alertOptions)
}
}`)()
})
this.inputPins.forEach(val => {
const subscription = val.valueChanges.pipe(debounce(() => timer(1000 / 60)))
@ -87,12 +90,15 @@ export class Component {
this.clickedChanges.next(this.clicked)
}
private activate() {
this.activation({
private activate(index: number = 0) {
this.activation[index]({
outputs: this.outputPins,
inputs: this.inputPins,
succes: (mes: string) => { success(mes, "", alertOptions) },
error: (mes: string) => { error(mes, "", alertOptions) }
error: (mes: string) => { error(mes, "", alertOptions) },
color: (color: string) => {
this.material.color.next(color)
}
} as activationContext)
}
@ -111,6 +117,9 @@ export class Component {
)
this.clicked = true
this.clickedChanges.next(this.clicked)
this.activate(1)
this.activate(0)
}
handlePinClick(e: MouseEvent, pin: Pin) {

View file

@ -12,6 +12,7 @@ export interface activationContext {
outputs: Pin[]
succes: (mes: string) => any
error: (mes:string) => any
color: (color:string) => void
}
export type materialMode = "standard_image" | "color"

View file

@ -7,6 +7,7 @@ declare function require<T>(path:string):T
type partFactory = (part:Part) => void
export class Material {
private static cache = false
private static images: {
[key: string]: string
} = {
@ -20,7 +21,7 @@ export class Material {
constructor (public mode: materialMode,public name:string) {
const saved = Material.cached.get(mode + name)
if (saved)
if (saved && Material.cache)
return saved
else Material.cached.set(mode + name,this)

View file

@ -130,5 +130,41 @@ export class ComponentTemplateStore {
data: "yellow"
}
})
this.store.set("light", {
inputs: 1,
outputs: 0,
name: "light",
version: "1.0.0",
activation: `
if (ctx.inputs[0].value)
ctx.color("yellow")
else
ctx.color("white")
`.trim(),
material: {
mode: "color",
data: "white"
}
})
this.store.set("button", {
inputs: 0,
outputs: 1,
name: "button",
version: "1.0.0",
activation: `
ctx.outputs[0].value = ctx.outputs[0].memory.value
`.trim(),
material: {
mode: "color",
data: "red"
},
onclick: `
ctx.outputs[0].memory.value = !ctx.outputs[0].memory.value
if (ctx.outputs[0].memory.value)
ctx.color("#880000")
else
ctx.color("red")
`
})
}
}

View file

@ -12,6 +12,7 @@ export interface ComponentTemplate {
name: string
version: string
activation: string
onclick?: string
inputs: number
outputs: number
material: {