this repo has no description

casting works

phaz.uk 2f32a594 0c6c359d

verified
Changed files
+87 -25
src
src-tauri
src
runtime
nodes
conditional
structs
+3 -8
src-tauri/src/runtime/nodes/conditional/iffalse.rs
··· 26 26 } 27 27 28 28 fn update_arg( &mut self, _: usize, arg: ParameterType ) -> bool { 29 - if let ParameterType::Boolean(boolean) = arg{ 30 - if boolean{ 31 - self.runtime_active = true; 32 - true 33 - } else{ 34 - self.runtime_active = false; 35 - false 36 - } 29 + if arg.as_bool().unwrap(){ 30 + self.runtime_active = true; 31 + true 37 32 } else{ 38 33 self.runtime_active = false; 39 34 false
+3 -8
src-tauri/src/runtime/nodes/conditional/iftrue.rs
··· 26 26 } 27 27 28 28 fn update_arg( &mut self, _: usize, arg: ParameterType ) -> bool { 29 - if let ParameterType::Boolean(boolean) = arg{ 30 - if boolean{ 31 - self.runtime_active = true; 32 - true 33 - } else{ 34 - self.runtime_active = false; 35 - false 36 - } 29 + if arg.as_bool().unwrap(){ 30 + self.runtime_active = true; 31 + true 37 32 } else{ 38 33 self.runtime_active = false; 39 34 false
+40
src-tauri/src/structs/parameter_types.rs
··· 1 + use anyhow::{Result, bail}; 1 2 use serde::Serialize; 2 3 3 4 #[derive(Serialize, Clone, Debug, PartialEq)] ··· 14 15 15 16 None 16 17 } 18 + 19 + impl ParameterType{ 20 + pub fn as_bool( &self ) -> Result<bool>{ 21 + match self{ 22 + ParameterType::Boolean( val ) => Ok(val.clone()), 23 + ParameterType::Int( val ) => if *val == 0{ Ok(false) } else { Ok(true) }, 24 + _ => bail!("Cannot cast to bool.") 25 + } 26 + } 27 + 28 + pub fn as_int( &self ) -> Result<i32>{ 29 + match self{ 30 + ParameterType::Boolean( val ) => if *val{ Ok(1) } else { Ok(0) }, 31 + ParameterType::Int( val ) => Ok(val.clone()), 32 + ParameterType::Float( val ) => Ok(val.round().clone() as i32), 33 + ParameterType::String( val ) => Ok(val.clone().parse()?), 34 + _ => bail!("Cannot cast to int.") 35 + } 36 + } 37 + 38 + pub fn as_float( &self ) -> Result<f32>{ 39 + match self{ 40 + ParameterType::Int( val ) => Ok(val.clone() as f32), 41 + ParameterType::Float( val ) => Ok(val.clone()), 42 + ParameterType::String( val ) => Ok(val.clone().parse()?), 43 + _ => bail!("Cannot cast to float.") 44 + } 45 + } 46 + 47 + pub fn as_string( &self ) -> Result<String>{ 48 + match self{ 49 + ParameterType::Boolean( val ) => Ok(val.clone().to_string()), 50 + ParameterType::Int( val ) => Ok(val.clone().to_string()), 51 + ParameterType::Float( val ) => Ok(val.clone().to_string()), 52 + ParameterType::String( val ) => Ok(val.clone()), 53 + _ => bail!("Cannot cast to string.") 54 + } 55 + } 56 + }
+13 -7
src/renderer.ts
··· 145 145 146 146 node.outputs.map(( output, i ) => { 147 147 output.connections.map(partner => { 148 - ctx.strokeStyle = NodeIOLinkColours(output); 148 + let x0 = (nodeX + (node.w - 10) + 10 + startX + position.x) * position.scale; 149 + let y0 = (nodeY + 50 + (30 * i) + 10 + startY + position.y) * position.scale; 150 + let x1 = ((Math.round(partner.parent.x / 10) * 10) + startX + position.x) * position.scale; 151 + let y1 = ((Math.round(partner.parent.y / 10) * 10) + 60 + (30 * partner.index) + startY + position.y) * position.scale; 152 + 153 + let colours = NodeIOLinkColours(output, partner); 154 + let grad = ctx.createLinearGradient(x0, y0, x1, y1); 155 + 156 + grad.addColorStop(0, colours[0]); 157 + grad.addColorStop(1, colours[1]); 158 + 159 + ctx.strokeStyle = grad; 149 160 ctx.lineWidth = 3 * position.scale; 150 161 151 - drawCurve(ctx, 152 - (nodeX + (node.w - 10) + 10 + startX + position.x) * position.scale, 153 - (nodeY + 50 + (30 * i) + 10 + startY + position.y) * position.scale, 154 - ((Math.round(partner.parent.x / 10) * 10) + startX + position.x) * position.scale, 155 - ((Math.round(partner.parent.y / 10) * 10) + 60 + (30 * partner.index) + startY + position.y) * position.scale, 156 - ); 162 + drawCurve(ctx, x0, y0, x1, y1); 157 163 ctx.stroke(); 158 164 }) 159 165 })
+28 -2
src/structs/node.ts
··· 74 74 ParameterList = 10 75 75 } 76 76 77 + let NodeIOCastTable: any = { 78 + 1: { 2: true, 3: true, 4: true }, // Strings -> Floats, Ints, Bools 79 + 2: { 1: true, 3: true }, // Floats -> Strings, Ints 80 + 3: { 1: true, 2: true, 4: true }, // Ints -> Strings, Floats, Bools 81 + 4: { 1: true, 2: true, 3: true } // Bools -> Strings, Ints, Floats 82 + }; 83 + 77 84 export let NodeIOCanCast = ( input: NodeType | null, output: NodeType | null ): boolean => { 78 85 if(input === output)return true; 79 86 if(!input || !output)return false; 80 87 81 - return false; 88 + let CastFrom = NodeIOCastTable[input]; 89 + if(!CastFrom)return false; 90 + 91 + let CastTo = CastFrom[output]; 92 + if(!CastTo)return false; 93 + 94 + return true; 82 95 } 83 96 84 97 export let NodeIOResolveAnyTypes = ( nodeio: NodeIO ): NodeType | null => { ··· 112 125 return null; 113 126 } 114 127 115 - export let NodeIOLinkColours = ( nodeio: NodeIO ) => { 128 + export let NodeIOLinkColours = ( nodeio: NodeIO, output?: NodeIO ) => { 116 129 let cols: any = { 117 130 1: '#ffff9f', 118 131 2: '#cda0cb', ··· 122 135 } 123 136 124 137 let type = NodeIOResolveAnyTypes(nodeio); 138 + if(output){ 139 + let outputType = NodeIOResolveAnyTypes(output); 140 + let startType = type ? cols[type] : '#fff5'; 141 + 142 + if(type !== outputType){ 143 + let endType = outputType ? cols[outputType] : '#fff5'; 144 + 145 + return [ startType, endType ]; 146 + } else{ 147 + return [ startType, startType ]; 148 + } 149 + } 150 + 125 151 return type ? cols[type] : '#fff5'; 126 152 } 127 153