🚍 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 strokeColor = "#888888"
private inputs: number private inputs: number
private outputs: number private outputs: number
private activation: (ctx: activationContext) => any private activation: ((ctx: activationContext) => any)[] = []
private subscriptions: Subscription[] = [] private subscriptions: Subscription[] = []
public inputPins: Pin[] = [] public inputPins: Pin[] = []
@ -58,14 +58,17 @@ export class Component {
this.inputPins = [...Array(this.inputs)].fill(true).map(() => new Pin(false, this)) 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.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 : ""]
try{ .map(val => {
${data.activation} return new Function(`return (ctx) => {
} try{
catch(err){ ${val}
ctx.error(err,"",ctx.alertOptions) }
} catch(err){
}`)() ctx.error(err,"",ctx.alertOptions)
}
}`)()
})
this.inputPins.forEach(val => { this.inputPins.forEach(val => {
const subscription = val.valueChanges.pipe(debounce(() => timer(1000 / 60))) const subscription = val.valueChanges.pipe(debounce(() => timer(1000 / 60)))
@ -87,12 +90,15 @@ export class Component {
this.clickedChanges.next(this.clicked) this.clickedChanges.next(this.clicked)
} }
private activate() { private activate(index: number = 0) {
this.activation({ this.activation[index]({
outputs: this.outputPins, outputs: this.outputPins,
inputs: this.inputPins, inputs: this.inputPins,
succes: (mes: string) => { success(mes, "", alertOptions) }, 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) } as activationContext)
} }
@ -111,6 +117,9 @@ export class Component {
) )
this.clicked = true this.clicked = true
this.clickedChanges.next(this.clicked) this.clickedChanges.next(this.clicked)
this.activate(1)
this.activate(0)
} }
handlePinClick(e: MouseEvent, pin: Pin) { handlePinClick(e: MouseEvent, pin: Pin) {
@ -163,7 +172,7 @@ export class Component {
const middleX = subscribe(this.x.pipe(map(val => { const middleX = subscribe(this.x.pipe(map(val => {
const scale = this.scale.value[0] const scale = this.scale.value[0]
return val + ((mode === "input") ? scale / 10 : 9 * scale / 10 ) return val + ((mode === "input") ? scale / 10 : 9 * scale / 10)
}))) })))
return svg` return svg`
@ -203,7 +212,7 @@ export class Component {
return new Component(state.template, state.position, state.scale, state.id) return new Component(state.template, state.position, state.scale, state.id)
} }
public static getId(){ public static getId() {
const data = runCounter.get() const data = runCounter.get()
runCounter.increase() runCounter.increase()
return data return data

View file

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

View file

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

View file

@ -112,9 +112,9 @@ export class ComponentTemplateStore {
activation: ` activation: `
ctx.outputs[0].value = true ctx.outputs[0].value = true
`.trim(), `.trim(),
material:{ material: {
mode:"color", mode: "color",
data:"green" data: "green"
} }
}) })
this.store.set("false", { this.store.set("false", {
@ -125,10 +125,46 @@ export class ComponentTemplateStore {
activation: ` activation: `
ctx.outputs[0].value = false ctx.outputs[0].value = false
`.trim(), `.trim(),
material:{ material: {
mode:"color", mode: "color",
data:"yellow" 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 name: string
version: string version: string
activation: string activation: string
onclick?: string
inputs: number inputs: number
outputs: number outputs: number
material: { material: {