Component Scripting for Template

Hi there,
I’m using Ignition 8.1. I want to write a simple code for mouseClicked inside a template that I move to the screen. Here is the image of my simple problem;
image

I want to change the visibility of another image on the screen. I don’t get any error in this code but it doesn’t even work at all. I simply typed print “test” in it, but I can’t see it in the console either.

By the way, I can easily use this code in a button.

Looking at some old posts, adding the script to the template instance might not be available (?), but there are workarounds. Seems like you cannot get the event to fire at all.

One thing that I have used lately has been putting the script on the Template itself (not the template instance) that calls this potential GUI scenario and changes a component. This isn't a good code block, but it works:

currentWindow = 'Window_Path_That_Has_The_Image'
try:
	image = system.gui.getWindow(currentWindow).getRootContainer().getComponent('Image')
	image.visible = 0
except:
	pass

Previous posts:

Do you have any overlaying components, or a mouse event handler on the template instance in your actual window?

Something might be intercepting the mouse event before it gets to your template’s inner handling code. Consider a print "event fired" to ensure it’s actually being executed.

Surprisingly, it’s really sad that we can’t use mouseClicked directly. As mentioned in other posts, I think the best way is to make another component invisible and write on it. Thank you guys!

You can definitely use mouseClicked directly, Ive done it lots before. Where are you attaching your event script to?

If you have a template that has mouse events on components within it, then attach mouse events to the Template instance itself, mouse events won’t get passed through and your events won’t run that are defined in the template without code to pass it through

2 Likes

So, basically, you have to have all of your mouse event scripts in one place? Either on a template or the instance of the template. You can’t have a common “mouse over” effect done in a template, and have each instance navigate to a different page via mouseClicked?!

If each template is doing the same thing, why not pass the navigation target as a template parameter?

The underlying behavior here (only one mouse event handler captures events at a time) is a Java Swing behavior, not an intentional decision we made. Vision has relatively few abstractions over Swing, so Swing's limitations are Vision's limitations.

If you're really motivated, you could use the EventDelegateDispatcher class we use internally to automatically "forward" events from one component to another, but it's very situational and usually not worth messing with.

1 Like