1+
2+ import { Theme } from "../../colors/Theme" ;
3+ import { ComboBox } from "../../components/ComboBox" ;
4+ import { Script } from "../../export/Script" ;
5+ import { InputOrValue } from "../../properties/InputOrValue" ;
6+ import { Output } from "../../properties/Output" ;
7+ import { WinNode } from "../WinNode" ;
8+
9+ type Operator = [ symbol :string, tslMethod :string ] ;
10+
11+ export class MathNode extends WinNode {
12+ protected operators : Operator [ ] ;
13+ protected A :InputOrValue ;
14+ protected B :InputOrValue ;
15+ protected combo :ComboBox ;
16+
17+ constructor ( ) {
18+
19+ const operators : Operator [ ] = [
20+ [ "+" , "add" ] ,
21+ [ "-" , "sub" ] ,
22+ [ "*" , "mul" ] ,
23+ [ "/" , "div" ] ,
24+ [ "%" , "mod" ] ,
25+ [ "==" , "equal" ] ,
26+ [ "!=" , "notEqual" ]
27+ // TODO: Add more....
28+ ]
29+
30+ super ( "Math Operator" , Theme . config . groupMath , [
31+ new Output ( "Result" , 0 ) ,
32+ new InputOrValue ( 0 , "A" , ( ) => this . update ( ) ) ,
33+ new ComboBox ( "Operator" , operators . map ( o => o [ 0 ] ) , i => this . onComboChange ( i ) ) ,
34+ new InputOrValue ( 0 , "B" , ( ) => this . update ( ) ) ,
35+ ] ) ;
36+
37+ this . A = this . getChildOfType ( InputOrValue , 0 ) ! ;
38+ this . B = this . getChildOfType ( InputOrValue , 1 ) ! ;
39+ this . combo = this . getChildOfType ( ComboBox ) ! ;
40+
41+ this . operators = operators ;
42+
43+ this . onComboChange ( 0 )
44+ }
45+
46+ protected onComboChange ( newIndex :number )
47+ {
48+ const op = this . operators [ newIndex ] ;
49+
50+ this . setTitle ( `.${ op [ 1 ] } A ${ op [ 0 ] } B` ) ;
51+
52+ this . update ( ) ;
53+ }
54+
55+ override writeScript ( script : Script ) : string {
56+
57+ const a = this . A . writeScript ( script ) ;
58+ const b = this . B . writeScript ( script ) ;
59+
60+ const op = this . operators [ this . combo . index ] [ 1 ] ;
61+
62+ console . log ( `${ a } .${ op } (${ b } )` )
63+
64+ return script . define ( this . nodeName , `${ a } .${ op } (${ b } )` ) ;
65+ }
66+
67+ override serialize ( ) : Record < string , any > {
68+ return {
69+ ...super . serialize ( ) ,
70+ a : this . A . value ,
71+ b : this . B . value ,
72+ op : this . combo . index
73+ }
74+ }
75+
76+ override unserialize ( data : Record < string , any > ) : void {
77+ super . unserialize ( data ) ;
78+
79+ this . A . value = data . a ?? 0 ;
80+ this . B . value = data . b ?? 0 ;
81+ this . combo . index = data . op ?? 0 ;
82+ }
83+ }
0 commit comments