Skip to content

Commit

Permalink
Added min max for math and other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
newcat committed Sep 13, 2019
1 parent b668ade commit a17cacf
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 26 deletions.
10 changes: 5 additions & 5 deletions packages/baklavajs-core/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export abstract class Node implements INode {
public id: string = "node_" + generateId();
/** A map of all interfaces of the node.
* | Key = Name of the interface
* | Value = [[NodeInterface]] instance
* | Value = NodeInterface instance
*/
public interfaces: Map<string, NodeInterface> = new Map();
/** A map of all options of the node.
* | Key = Name of the option
* | Value = [[NodeOption]] instance
* | Value = NodeOption instance
*/
public options: Map<string, NodeOption> = new Map();

Expand Down Expand Up @@ -125,7 +125,7 @@ export abstract class Node implements INode {
const intf = this.addInterface(true, name, option);
intf.events.setValue.addListener(this, () => this.events.update.emit({ name, interface: intf }));
intf.value = defaultValue;
Object.entries(additionalProperties || {}).forEach(([k, v]) => { (intf as any)[k] = v; });
Object.entries(additionalProperties || {}).forEach(([k, v]) => { intf[k] = v; });
this.events.addInterface.emit(intf);
return intf;
}
Expand All @@ -139,7 +139,7 @@ export abstract class Node implements INode {
protected addOutputInterface(name: string, additionalProperties?: Record<string, any>) {
if (this.events.beforeAddInterface.emit({ name, isInput: false })) { return; }
const intf = this.addInterface(false, name);
Object.entries(additionalProperties || {}).forEach(([k, v]) => { (intf as any)[k] = v; });
Object.entries(additionalProperties || {}).forEach(([k, v]) => { intf[k] = v; });
this.events.addInterface.emit(intf);
return intf;
}
Expand Down Expand Up @@ -188,7 +188,7 @@ export abstract class Node implements INode {
sidebarComponent?: string, additionalProperties?: Record<string, any>) {
if (this.events.beforeAddOption.emit({ name, component, defaultValue, sidebarComponent })) { return; }
const opt = new NodeOption(component, defaultValue, sidebarComponent);
Object.entries(additionalProperties || {}).forEach(([k, v]) => { (opt as any)[k] = v; });
Object.entries(additionalProperties || {}).forEach(([k, v]) => { opt[k] = v; });
opt.events.setValue.addListener(this, () => { this.events.update.emit({ name, option: opt }); });
this.options.set(name, opt);
this.events.addOption.emit({ name, option: opt });
Expand Down
3 changes: 2 additions & 1 deletion packages/baklavajs-core/src/nodeInterface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Node } from "./node";
import generateId from "./idGenerator";
import { IInterfaceState } from "../types/state";
import { BaklavaEvent, PreventableBaklavaEvent, SequentialHook } from "./events";
Expand All @@ -11,6 +10,8 @@ export class NodeInterface implements INodeInterface {
public parent: INode;
public option?: string;

[k: string]: any;

public events = {
setConnectionCount: new BaklavaEvent<number>(),
beforeSetValue: new PreventableBaklavaEvent<any>(),
Expand Down
3 changes: 3 additions & 0 deletions packages/baklavajs-core/src/nodeOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export class NodeOption implements INodeOption {
setValue: new BaklavaEvent<any>()
};

/** Additional Properties */
[k: string]: any;

private _value: any;

public constructor(optionComponent: string, value?: any, sidebarComponent?: any) {
Expand Down
3 changes: 3 additions & 0 deletions packages/baklavajs-core/types/nodeInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { IInterfaceState } from "./state";

export interface INodeInterface {

/** Additional Properties */
[k: string]: any;

id: string;
isInput: boolean;
parent: INode;
Expand Down
4 changes: 4 additions & 0 deletions packages/baklavajs-core/types/nodeOption.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { IBaklavaEvent, IPreventableBaklavaEvent } from "./events";

export interface INodeOption {

/** Additional Properties */
[k: string]: any;

optionComponent: string;
value: any;
sidebarComponent?: string;
Expand Down
9 changes: 3 additions & 6 deletions packages/baklavajs-playground/src/MathNode.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { NodeBuilder } from "../../baklavajs-core/src";

export default new NodeBuilder("MathNode")
.addInputInterface("Number 1", "NumberOption", 1)
.addInputInterface("Number 2", "NumberOption", 10)
.addOption("Operation", "SelectOption", () => ({
selected: "Add",
items: [ "Add", "Subtract" ]
}))
.addInputInterface("Number 1", "NumberOption", 1, { displayName: "Number" })
.addInputInterface("Number 2", "NumberOption", 10, { displayName: "Number" })
.addOption("Operation", "SelectOption", "Add", undefined, { items: [ "Add", "Subtract" ] })
.addOutputInterface("Output")
.onCalculate((n) => {
const n1 = n.getInterface("Number 1").value;
Expand Down
5 changes: 3 additions & 2 deletions packages/baklavajs-playground/src/TestNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export default class TestNode extends Node {
this.addInputInterface("Test", "NumberOption", 5, { type: "number" });
this.addOutputInterface("Output", { type: "boolean" });
this.addOption("test", "InputOption");
this.addOption("Select", "SelectOption", { selected: "Test1", items: ["Test1", "Test2", "Test3"] });
this.addOption("Select", "SelectOption", "Test1", undefined, { items: ["Test1", "Test2", "Test3"] });
this.addOption("This is a checkbox", "CheckboxOption", true);
this.addOption("Number", "NumberOption", 5);
this.addOption("Number", "NumberOption", 5, undefined, { min: 0, max: 10 });
this.addOption("Slider", "SliderOption", 0.5, undefined, { min: 0, max: 1 });
}

public registerEditor(editor: Editor) {
Expand Down
43 changes: 38 additions & 5 deletions packages/baklavajs-plugin-options-vue/src/NumberOption.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div class="dark-num-input">
<div class="dark-num-input" :class="{ '--invalid': invalid }">
<div @click="decrement" class="__button --dec">
<i-arrow></i-arrow>
</div>
Expand Down Expand Up @@ -28,6 +28,7 @@
<script lang="ts">
import { Component, Prop, Vue, Watch } from "vue-property-decorator";
import Arrow from "./Arrow.vue";
import { INodeOption } from "../../baklavajs-core/types";
@Component({
components: {
Expand All @@ -44,7 +45,11 @@ export default class NumberOption extends Vue {
@Prop({ type: String })
name!: string;
@Prop({ type: Object })
option!: INodeOption;
editMode = false;
invalid = false;
tempValue = "0";
get v() {
Expand All @@ -65,11 +70,17 @@ export default class NumberOption extends Vue {
}
increment() {
this.$emit("input", this.v + 0.1);
const newValue = this.v + 0.1;
if (this.validate(newValue)) {
this.$emit("input", newValue);
}
}
decrement() {
this.$emit("input", this.v - 0.1);
const newValue = this.v - 0.1;
if (this.validate(newValue)) {
this.$emit("input", newValue);
}
}
async enterEditMode() {
Expand All @@ -80,8 +91,30 @@ export default class NumberOption extends Vue {
}
leaveEditMode() {
this.$emit("input", parseFloat(this.tempValue));
this.editMode = false;
const v = parseFloat(this.tempValue);
if (!this.validate(v)) {
this.invalid = true;
} else {
this.$emit("input", v);
this.editMode = false;
}
}
@Watch("tempValue")
resetInvalid() {
this.invalid = false;
}
validate(v: number) {
if (Number.isNaN(v)) {
return false;
} else if (typeof(this.option.min) === "number" && v < this.option.min) {
return false;
} else if (typeof(this.option.max) === "number" && v > this.option.max) {
return false;
} else {
return true;
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions packages/baklavajs-plugin-options-vue/src/SelectOption.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator";
import Arrow from "./Arrow.vue";
import { INodeOption } from "../../baklavajs-core/types";
// @ts-ignore
import ClickOutside from "v-click-outside";
Expand All @@ -46,19 +47,22 @@ export default class SelectOption extends Vue {
@Prop({ type: String })
name!: string;
@Prop({ type: Object })
@Prop({ type: String })
value!: any;
@Prop({ type: Object })
option!: INodeOption;
get selected() {
return this.value ? this.value.selected : "";
return this.value || "";
}
get items() {
return this.value ? this.value.items : [];
return this.option.items || [];
}
setSelected(item: string) {
this.$emit("input", { selected: item, items: this.items });
this.$emit("input", item);
}
}
Expand Down
37 changes: 37 additions & 0 deletions packages/baklavajs-plugin-options-vue/src/SliderOption.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<template>
<div class="dark-slider">
<div class="__slider" style="width: 50%;"></div>
<div class="__content">
<div class="__label">{{ name }}</div>
<div class="__value">{{ (value || 0).toFixed(3) }}</div>
</div>
</div>
</template>

<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator";
import { INodeOption } from "../../baklavajs-core/types";
// @ts-ignore
import ClickOutside from "v-click-outside";
@Component({
directives: {
ClickOutside: ClickOutside.directive
}
})
export default class SliderOption extends Vue {
open = false;
@Prop({ type: String })
name!: string;
@Prop({ type: Number })
value!: any;
@Prop({ type: Object })
option!: INodeOption;
}
</script>
2 changes: 2 additions & 0 deletions packages/baklavajs-plugin-options-vue/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import InputOption from "./InputOption.vue";
import IntegerOption from "./IntegerOption.vue";
import NumberOption from "./NumberOption.vue";
import SelectOption from "./SelectOption.vue";
import SliderOption from "./SliderOption.vue";
import TextOption from "./TextOption.vue";

export {
Expand All @@ -13,5 +14,6 @@ export {
IntegerOption,
NumberOption,
SelectOption,
SliderOption,
TextOption
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
<div :id="data.id" :class="classes">
<div class="__port" @mouseover="startHover" @mouseout="endHover"></div>
<span v-if="data.connectionCount > 0 || !data.option || !getOptionComponent(data.option)" class="align-middle">
{{ name }}
{{ displayName }}
</span>
<component
v-else
:is="getOptionComponent(data.option)"
:option="data"
:value="value"
@input="data.value = $event"
:name="name"
:name="displayName"
></component>
</div>
</template>
Expand Down Expand Up @@ -48,6 +49,10 @@ export default class NodeInterfaceView extends Vue {
};
}
get displayName() {
return this.data.displayName || this.name;
}
beforeMount() {
this.value = this.data.value;
this.data.events.setValue.addListener(this, (v) => { this.value = v; });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<template>
<component
:is="component"
:name="name"
:name="displayName"
class="node-option"
:node="node"
:value="value"
:option="option"
@input="updateValue"
@openSidebar="$emit('openSidebar')"
></component>
Expand Down Expand Up @@ -41,6 +42,10 @@ export default class NodeOptionView extends Vue {
return this.plugin.options[this.componentName];
}
get displayName() {
return this.option.displayName || this.name;
}
beforeMount() {
this.value = this.option.value;
this.option.events.setValue.addListener(this, (v) => { this.value = v; });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
@import "./context-menu.scss";
@import "./input.scss";
@import "./select";
@import "./slider";
@import "./numinput";
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@

}

&.--invalid > .__content > input {
box-shadow: 0 0 2px 2px red;
}

}
43 changes: 43 additions & 0 deletions packages/baklavajs-plugin-renderer-vue/src/styles/dark/slider.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.dark-slider {
background: $color-control-dark-background;
color: $color-control-dark-foreground;
border-radius: $border-radius-control;
position: relative;

& > .__content {
display: flex;
cursor: pointer;
position: relative;
z-index: 1;

& > .__label, & > .__value {
flex: 1 1 auto;
margin: 0.35em 0;
padding: 0 0.5em;
text-overflow: ellipsis;
}

& > .__value {
text-align: right;
}

& > input {
background-color: $color-control-dark-background;
color: $color-control-dark-foreground;
caret-color: $color-primary;
padding: 0.35em;
width: 100%;
}

}

& > .__slider {
position: absolute;
top: 0;
bottom: 0;
left: 0;
background-color: $color-control-dark-hover;
border-radius: $border-radius-control;
}

}

0 comments on commit a17cacf

Please sign in to comment.