How to add an event trigger listener(callback) - unity3d

Basically how you would create a public void MethodToDoStuff(), attach a monobehaviour script and link the method on a button, so that when its clicked, said method "MethodToDoStuff" is called.
Now I want to do that via an editor script.
Add an event trigger component
On the event trigger component, add a PointerDown and PointerUp event
On the PointerUp and PointerDown, link a public method on another script to be run (doesHandler.HidePanel()) "see code below"
I could do this manually but having an editor script is super effiecient.
Here is what I have so far:
All help is appreciated, Thanks!
EDITOR SCRIPT:
void OnWizardCreate()
{
doesHandler = GameObject.FindWithTag("WhatItDoes").GetComponent<WhatThisDoes>();
GameObject selection = Selection.activeGameObject;
EventTrigger trig=(EventTrigger)selection.AddComponent(typeof(EventTrigger));
EventTrigger.Entry onPointerDown = new EventTrigger.Entry();
onPointerDown.eventID = EventTriggerType.PointerDown;
EventTrigger.Entry onPointerUp = new EventTrigger.Entry();
onPointerUp.eventID = EventTriggerType.PointerUp;
trig.triggers.Add(onPointerDown);
trig.triggers.Add(onPointerUp);
}
OTHER SCRIPT:
public void HidePanel()
{
whatItDoesPanel.SetActive(false);
}

On runtime you would usually call e.g.
onPointerDown.AddListener(doesHandler.HidePanel);
however this would only add the listener temporarily.
Adding a persistent listener is a bit more complex but luckily there now is a tool for this: UnityEventTools.AddPersistentListener so afaik you would only have to add
UnityEventTools.AddPersistentListener(onPointerDown, doesHandler.HidePanel);
UnityEventTools.AddPersistentListener(onPointerUp, doesHandler.HidePanel);
Additional afaik you should before use Undo.RecordObject in order to mark the changed object and the scene as dirty and add a Undo/Redo entry like
Undo.RecordObject(selection, "Added event triggers");
So probably something like
void OnWizardCreate()
{
doesHandler = Object.FindObjectOfType<WhatThisDoes>();
if(!doesHandler)
{
Debug.LogWarning("No WhatThisDoes found in the scene -> Ignored");
return;
}
var selection = Selection.activeGameObject;
if(!selection)
{
Debug.LogWarning("Nothing selected -> Ignored")
return;
}
if(selection.GetComponent<EventTrigger>())
{
Debug.LogWarning($"The selected object {selection} already has an EventTrigger attached! -> Ignored");
return;
}
// log the undo before making changes
Undo.RecordObject(selection, "Added event triggers");
var onPointerDown = new EventTrigger.Entry();
onPointerDown.eventID = EventTriggerType.PointerDown;
UnityEventTools.AddPersistentListener(onPointerDown, doesHandler.HidePanel);
var onPointerUp = new EventTrigger.Entry();
onPointerUp.eventID = EventTriggerType.PointerUp;
UnityEventTools.AddPersistentListener(onPointerUp, doesHandler.HidePanel);
var trig = selection.AddComponent<EventTrigger>();
trig.triggers.Add(onPointerDown);
trig.triggers.Add(onPointerUp);
}

Related

Add style when creating button with script Unity ui toolkit

So I want to add a lot of buttons with a script. I want to style them, but I can't find a method to do so.
I want to do something like
Button current = new Button();
current.addStyle("button-style");
Is this possible?
UNITY UI TOOLKIT
When a button is clicked I run a function
public void displayChildrenIcons(GameObject parent, string type)
{
var root = GetComponent<UIDocument>().rootVisualElement;
var displayArea = root.Q<VisualElement>("options");
displayArea.Clear();
for (int i = 0; i < parent.transform.childCount; i++)
{
Button current = new Button();
//How do I style this button before adding it?
displayArea.Add(current);
}
}
Check out the class IStyle. You can create one of these variables and change it so you can customimze style. Then assign it with Button.style = IStle;
public IStyle iStyle = new();
Button current = new();
curent.style = bStyle;
displayArea.Add(current);

ActionScript3 How to increment score when working with classes

I would like to increment the value of a TextField by one every time an event listener in another class is activated. This is proving very difficult because the event listener can only be triggered once the Timer in the main class reaches completion.
I have tried using a getter method on a Boolean to ensure that the event listener in a different class has been triggered. This has proven unsucsessful however because of the Timer which is only triggered once on completion.
I am a begginer to AS3 and so any help would be greately appreciated. Thank You
THIS IS THE CODE FOR THE MAIN CLASS
public function t1Finish(e:TimerEvent):void {
ranNum2 = randomNum1(0, 11);
holes[ranNum2].sendUp();
setCounter();
}
public function setCounter():void {
if(holes[ranNum2].returnHasHit() == true) {
counter1++;
txt1instance.text = counter1.toString();
} else {
txt1instance.text = counter1.toString();
}
}
THIS IS THE CODE FOR THE OTHER CLASS
public function click1(e:MouseEvent):void {
this.gotoAndPlay(32);
hasHit = true;
}
public function returnHasHit():Boolean {
return hasHit;
}
You need to properly arrange your app hierarchy. Your case is not exactly problematic: you have main class that wants to capture some events occurring in certain subservient objects.
// That HOLE class.
// I assume it is an inner event handler, not a public
// interface, thus there's no need for it to be public.
private function click1(e:MouseEvent):void
{
// Normally you don't need to explicitly state "this".
gotoAndPlay(32);
// Produce a custom event.
dispatchEvent(new Event(Event.OPEN));
}
Then, you need to catch that event in the main class.
// The MAIN class.
// You need to subscribe for that custom event somewhere.
private function subscribeAll():void
{
for each (var aHole:MovieClip in holes)
{
aHole.addEventListener(Event.OPEN, onOpen);
}
}
// The custom event handler.
private function onOpen(e:Event):void
{
// If you want to know which hole dispatched this event.
var aHole:MovieClip = e.target as MovieClip;
// Here you might want to put any additional criteria
// to check if you want to accept this event.
// This handler is invoked ONLY if that "click1" handler was invoked
// on any of the holes, so there's no need to check "wasHit" interface.
counter1++;
txt1instance.text = counter1.toString();
}

UWP Custom ListView to scroll down

So, I have a listview and I want it whenever an item is created to scroll to that item (bottom). Because I am using MVVM I found really nice explanation on how to make a new control that inherits from listview that scrolls down. The problem is that this answer (the third) is referring to WPF 6 years ago.
I am making a UWP app, so I copied the code and tried to format it to my needs. The following code doesn't give any error or exception but instead it loads the "ChatListView" as I call it perfectly and then does nothing. The comments are only a bit edited compared to the original code.
What can I do ? Thank you in advance!
public class ChatListView : ListView
{
//Define the AutoScroll property. If enabled, causes the ListBox to scroll to
//the last item whenever a new item is added.
public static readonly DependencyProperty AutoScrollProperty =
DependencyProperty.Register(
"AutoScroll",
typeof(Boolean),
typeof(ChatListView),
new PropertyMetadata(
true, //Default value.
new PropertyChangedCallback(AutoScroll_PropertyChanged)));
//Gets or sets whether or not the list should scroll to the last item
//when a new item is added.
public bool AutoScroll
{
get { return (bool)GetValue(AutoScrollProperty); }
set { SetValue(AutoScrollProperty, value); }
}
//Event handler for when the AutoScroll property is changed.
//This delegates the call to SubscribeToAutoScroll_ItemsCollectionChanged().
//d = The DependencyObject whose property was changed.</param>
//e = Change event args.</param>
private static void AutoScroll_PropertyChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SubscribeToAutoScroll_ItemsCollectionChanged(
(ChatListView)d,
(bool)e.NewValue);
}
//Subscribes to the list items' collection changed event if AutoScroll is enabled.
//Otherwise, it unsubscribes from that event.
//For this to work, the underlying list must implement INotifyCollectionChanged.
//
//(This function was only creative for brevity)
//listBox = The list box containing the items collection.
//subscribe = Subscribe to the collection changed event?
private static void SubscribeToAutoScroll_ItemsCollectionChanged(
ChatListView listView, bool subscribe)
{
INotifyCollectionChanged notifyCollection =
listView as INotifyCollectionChanged;
if (notifyCollection != null)
{
if (subscribe)
{
//AutoScroll is turned on, subscribe to collection changed events.
notifyCollection.CollectionChanged +=
listView.AutoScroll_ItemsCollectionChanged;
}
else
{
//AutoScroll is turned off, unsubscribe from collection changed events.
notifyCollection.CollectionChanged -=
listView.AutoScroll_ItemsCollectionChanged;
}
}
}
//Event handler called only when the ItemCollection changes
//and if AutoScroll is enabled.
//sender = The ItemCollection.
//e = Change event args.
private void AutoScroll_ItemsCollectionChanged(
object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
int count = Items.Count;
ScrollIntoView(Items[count - 1]);
}
}
//Constructor a new ChatListView.
public ChatListView()
{
//Subscribe to the AutoScroll property's items collection
//changed handler by default if AutoScroll is enabled by default.
SubscribeToAutoScroll_ItemsCollectionChanged(
this, (bool)AutoScrollProperty.GetMetadata(typeof(ChatListView)).DefaultValue);
}
}
If you want to create a chat application you can use the ItemsStackPanel's ItemsUpdatingScrollMode particular property to KeepLastItemInView value to scroll to the latest item.
Usage:
<ListView>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel ItemsUpdatingScrollMode="KeepLastItemInView" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
Note: KeepLastItemInView enum member was introduced in the 14393 SDK.
Related link:
https://learn.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Controls.ItemsStackPanel#properties_
The accepted answer is pretty nice. However I there is one thing it won't do (at least if I simply copy and paste the above XAML): it won't do its intended scrolling if, say, the user was away from that page while new items were added, and then they navigated to the page.
For that I had to hook into
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (MyListView.Items.Count == 0)
return;
object lastItem = MyListView.Items[MyListView.Items.Count - 1];
MyListView.ScrollIntoView(lastItem);
}

GWT/MVP: detecting change events in a table with proper MVP pattern

We're using gwt-presenter, but not really a question specific to that...
I've got a table with users in it. As I build the table in the view (from the data provided by the presenter), I need to add two action buttons ("Edit", and "Delete") at the end of the row.
What's the best way to assign click handlers to these buttons so the presenter knows which was clicked? Previous to this, we could pass a private field from the view to the presenter and attach a discrete click handler to that button. However, this method is rather rigid and doesn't work in this scenario very well.
Thanks in advance.
How about having the view allowing the subscription for edit/delete click events, registering internally the individual row click events, and then delegating the event handling to the ones registered by the view?
I mean something like the following pesudo code:
View:
addRowEditClickHandler(ClickHandler handler) {
this.rowEditClickHandler = handler;
}
addRowDeleteClickHandler(ClickHandler handler) {
this.rowDeleteClickHandler = handler;
}
//... somewhere when setting up of the grid...
rowEditButton.addClickHandler = new ClickHandler() {
onClick(args) {
this.rowEditClickHandler.onClick(args)
}
rowDeleteButton.addClickHandler = new ClickHandler() {
onClick(args) {
this.rowDeleteClickHandler.onClick(args)
}
Presenter:
View view = new View();
view.addRowEditClickHandler( new ClickHandler() {
onClick(args) {
doSomething();
}
});
view.addRowDeleteClickHandler( new ClickHandler() {
onClick(args) {
doSomething();
}
});

.Net CF Prevent Overzealous, Impatient Clicking (while screen is redrawing)

.Net Compact Framework
Scenario: User is on a screen. Device can't finds a printer and asks the user if they want to try again. If they click "No", the current screen is closed and they are returned to the parent menu screen. If they click the "No" button multiple times, the first click will be used by the No button and the next click will take effect once the screen has completed redrawing. (In effect clicking a menu item which then takes the user to another screen.)
I don't see a good place to put a wait cursor...there isn't much happening when the user clicks "No" except a form closing. But the CF framework is slow to redraw the screen.
Any ideas?
you can skip pending clicks by clearing the windows message queue with
Application.DoEvents();
We use the following custom Event class to solve your problem (preventing multiple clicks and showing a wait cursor if necessary):
using System;
using System.Windows.Forms;
public sealed class Event {
bool forwarding;
public event EventHandler Action;
void Forward (object o, EventArgs a) {
if ((Action != null) && (!forwarding)) {
forwarding = true;
Cursor cursor = Cursor.Current;
try {
Cursor.Current = Cursors.WaitCursor;
Action(o, a);
} finally {
Cursor.Current = cursor;
Application.DoEvents();
forwarding = false;
}
}
}
public EventHandler Handler {
get {
return new EventHandler(Forward);
}
}
}
You can verify that it works with the following example (Console outputs click only if HandleClick has terminated):
using System;
using System.Threading;
using System.Windows.Forms;
class Program {
static void HandleClick (object o, EventArgs a) {
Console.WriteLine("Click");
Thread.Sleep(1000);
}
static void Main () {
Form f = new Form();
Button b = new Button();
//b.Click += new EventHandler(HandleClick);
Event e = new Event();
e.Action += new EventHandler(HandleClick);
b.Click += e.Handler;
f.Controls.Add(b);
Application.Run(f);
}
}
To reproduce your problem change the above code as follows (Console outputs all clicks, with a delay):
b.Click += new EventHandler(HandleClick);
//Event e = new Event();
//e.Action += new EventHandler(HandleClick);
//b.Click += e.Handler;
The Event class can be used for every control exposing EventHandler events (Button, MenuItem, ListView, ...).
Regards,
tamberg
Random thoughts:
Disable the some of the controls on the parent dialog while a modal dialog is up. I do not believe that you can disable the entire form since it is the parent of the modal dialog.
Alternatively I would suggest using a Transparent control to catch the clicks but transparency is not supported on CF.
How many controls are on the parent dialog? I have not found CF.Net that slow in updating. Is there any chance that the dialog is overloaded and could be custom drawn faster that with sub controls?
override the DialogResult property and the Dispose method of the class to handle adding/remvoing a wait cursor.