| 
									
										
										
										
											2025-01-22 16:18:30 +01:00
										 |  |  | from lnx.logicnode.lnx_nodes import * | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class OnEventNode(LnxLogicTreeNode): | 
					
						
							|  |  |  |     """Activates the output when the given event is received.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @seeNode Send Event to Object | 
					
						
							|  |  |  |     @seeNode Send Global Event"""
 | 
					
						
							|  |  |  |     bl_idname = 'LNOnEventNode' | 
					
						
							|  |  |  |     bl_label = 'On Event' | 
					
						
							|  |  |  |     lnx_version = 2 | 
					
						
							|  |  |  |     lnx_section = 'custom' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     operators = { | 
					
						
							|  |  |  |         'init': 'Init', | 
					
						
							|  |  |  |         'update': 'Update', | 
					
						
							|  |  |  |         'custom': 'Custom' | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-19 19:15:41 +00:00
										 |  |  |     def update(self): | 
					
						
							|  |  |  |         if self.property1 != 'custom': | 
					
						
							|  |  |  |             if self.inputs[0].is_linked: | 
					
						
							|  |  |  |                 self.label = f'{self.bl_label}: {self.property1}' | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 self.label = f'{self.bl_label}: {self.property1} {self.inputs[0].get_default_value()}' | 
					
						
							|  |  |  |         elif self.inputs[1].is_linked: | 
					
						
							|  |  |  |             self.label = f'{self.bl_label}: {self.property1}' | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.label = f'{self.bl_label}: {self.property1} {self.inputs[1].get_default_value()}' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 16:18:30 +01:00
										 |  |  |     def set_mode(self, context): | 
					
						
							|  |  |  |         if self.property1 != 'custom': | 
					
						
							|  |  |  |             if len(self.inputs) > 1: | 
					
						
							|  |  |  |                 self.inputs.remove(self.inputs[0]) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             if len(self.inputs) < 2: | 
					
						
							|  |  |  |                 self.add_input('LnxNodeSocketAction', 'In') | 
					
						
							|  |  |  |                 self.inputs.move(1, 0) | 
					
						
							| 
									
										
										
										
											2025-09-19 19:15:41 +00:00
										 |  |  |                  | 
					
						
							|  |  |  |         if self.property1 != 'custom': | 
					
						
							|  |  |  |             if self.inputs[0].is_linked: | 
					
						
							|  |  |  |                 self.label = f'{self.bl_label}: {self.property1}' | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 self.label = f'{self.bl_label}: {self.property1} {self.inputs[0].get_default_value()}' | 
					
						
							|  |  |  |         elif self.inputs[1].is_linked: | 
					
						
							|  |  |  |             self.label = f'{self.bl_label}: {self.property1}' | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.label = f'{self.bl_label}: {self.property1} {self.inputs[1].get_default_value()}' | 
					
						
							|  |  |  |              | 
					
						
							| 
									
										
										
										
											2025-01-22 16:18:30 +01:00
										 |  |  |     # Use a new property to preserve compatibility | 
					
						
							|  |  |  |     property1: HaxeEnumProperty( | 
					
						
							|  |  |  |         'property1', | 
					
						
							|  |  |  |         items=[ | 
					
						
							|  |  |  |             ('init', 'Init', 'Assigns an Event listener at runtime'), | 
					
						
							|  |  |  |             ('update', 'Update', 'Assigns an Event listener continuously'), | 
					
						
							|  |  |  |             None, | 
					
						
							|  |  |  |             ('custom', 'Custom', 'Assigns an Event listener everytime input is detected'), | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         name='', | 
					
						
							|  |  |  |         description='Chosen method for assigning an Event listener', | 
					
						
							|  |  |  |         default='init', | 
					
						
							|  |  |  |         update=set_mode | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def lnx_init(self, context): | 
					
						
							|  |  |  |         self.add_input('LnxStringSocket', 'String In') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.add_output('LnxNodeSocketAction', 'Out') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.set_mode(context) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def draw_buttons(self, context, layout): | 
					
						
							|  |  |  |         layout.prop(self, 'property1', text='') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def draw_label(self) -> str: | 
					
						
							| 
									
										
										
										
											2025-09-19 19:15:41 +00:00
										 |  |  |         if self.property1 != 'custom': | 
					
						
							|  |  |  |             if self.inputs[0].is_linked: | 
					
						
							|  |  |  |                 return f'{self.bl_label}: {self.property1}' | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 return f'{self.bl_label}: {self.property1} {self.inputs[0].get_default_value()}' | 
					
						
							|  |  |  |         elif self.inputs[1].is_linked: | 
					
						
							|  |  |  |             return f'{self.bl_label}: {self.property1}' | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return f'{self.bl_label}: {self.property1} {self.inputs[1].get_default_value()}' | 
					
						
							| 
									
										
										
										
											2025-01-22 16:18:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def get_replacement_node(self, node_tree: bpy.types.NodeTree): | 
					
						
							|  |  |  |         if self.lnx_version not in (0, 1): | 
					
						
							|  |  |  |             raise LookupError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         newnode = node_tree.nodes.new('LNOnEventNode') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             newnode.inputs[0].default_value_raw = self["property0"] | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for link in self.outputs[0].links: | 
					
						
							|  |  |  |             node_tree.links.new(newnode.outputs[0], link.to_socket) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return newnode |