Adobe flex popup single instance - actionscript-3

I need to create a flex popup which would be a single instance but we can make it visible and invisible whenver we want to display it. I am not sure we can implement this kind of functionality using createPopup or addpopup method. Instance must be one and need to update it every time some event happen and make it visible or invisible.
thanks

createPopUp requires a class name. All the internals of creating the popup are in that method. You won't be able to use createPopUp with an existing instance of a window. However, when you call createPopUp, the results you get will be the instance of the new popup you just created.
However, addPopUp does accept an instance of an already creating component. You'll want to proceed in one of a few ways:
1) if the popup instance exists; use addPopUp; otherwise use createPopUp:
if(myPopUp){
PopUpManager.addPopUp(myPopUp, etc...)
} else {
myPopUp = PopUpManager.createPopUp(this, myPopUpClassName, etc..);
}
2) Create the popup yourself and always use addPopUp
if(!myPopUp){
myPopUp = new myPopUpClass();
}
PopUpManager.addPopUp(myPopUp, etc...);
Whenever you want to hide the pop up, do so using the removePopUp() method. This method will not destroy the pop up instance, just remove it from view.
PopUpManager.removePopUp(myPopUp);
You're going to have to figure out how to store the reference to your popup outside of the PopUpManager.
And I warn you that all the code I wrote here is psuedo code.

Related

How to assign hotkeys to trigger event in Slate

Is there a way to schedule an event when a key is pressed on the keyboard. I can't seem to find that in the event triggers.
There are two possible answers to this.
First one is that slate is just javascript, with sandboxing layers (for security and functionality) so to an extent if you know the dom element ID could just try to reach it with something similar to this: https://developer.mozilla.org/en-US/docs/Web/API/Document/keypress_event#examples
const log = document.getElementById('log');
document.addEventListener('keypress', logKey);
function logKey(e) {
log.textContent += ` ${e.code}`;
}
Due to the sandboxing layers it may not be possible depending on what you are trying to achieve. If you want to do this natively, then the trick to effectively designing apps with slate is to think that the whole world gets recomputed whenever a variable changes. Similar to what happens with the lifecycle refreshes on Angular.js
There isn't a native global event for a key press in slate, so if you want to listen for any keypress that bubbles to the document, that won't work. But for most use cases you can work around it. For example by setting a variable from an input and then just read it from somewhere else.
1 - Go to the variables tab and create a new variable.
2 - Create an input box.
3 - Set the variable to update every time the variable changes.
4 - Read the variable from some widget or function.

How to make visible infinite movieclips with one button?

I'm working on an interactive map in Actionscript-3 (Adobe Flash CS6).
What I'm trying to do is, with a single button, to show multiple objects (movieclips) with each mouse click.
I'm currently working with this code, but I can't manage to find out how to show multiple movieclips, I can only show ONE:
btn_ally_unit.addEventListener(MouseEvent.CLICK, mostrar_ally_unit2);
function mostrar_ally_unit2(event:MouseEvent):void
{
map_editor.ally_unit.visible = true;
}
How do I extend this to apply to any number of movieclips?
I'm sure by infinite you mean indefinite.
Target the unit clicked by using the target property of the Event class. Looks something like this:
btn_ally_unit.addEventListener(MouseEvent.CLICK, mostrar_ally_unit2);
function mostrar_ally_unit2(event:MouseEvent):void
{
event.target.visible = true;
}
You see? (event:MouseEvent) is saying that this function expects one argument (a MouseEvent) which you are giving the variable name of event. That's a convention but I like to use me as an abbreviation for mouse event. Others just use the letter e. Ok. Now event has a property, target, which is the thing receiving the event. In this case it will be one of your units. Your units have a property of visible which you can toggle off like you have been doing but by using the relative mouse event target, you can use the same line of code for all units.
note
Of course you must add the event listener to each unit. You could make it part of the class or just add it when a new unit is instantiated.
note
Using the event flow in actionscript 3 can be tricky. Seek out a tutorial on this. Here is one link related to event flow from Adobe.

Pause UI in WinRT 8.1

I can't for the life of me get the following functionality to work:
User taps item
Item's image becomes visible via changing visibility property of image
After a short period of time image becomes invisible again (with no user input) via changing the
visibility property
Or, more simply:
Make visible UI change
Pause so user can see UI change
Reverse step 1's UI change
Step 2 happens before steps 1 and 3 regardless of where the code is because the UI is not updating until the logic finishes (I assume).
I am setting the visibility of the image via data binding with INotifyPropertyChanged. All works as expected except when I'm trying to introduce the pause.
I'm trying to pause with this simple method:
private void Pause()
{
new System.Threading.ManualResetEvent(false).WaitOne(1000);
}
It does pause, but the UI changes wait until after that pause even though a change to the bound data happen befores the pause is called, and the other change after.
I have tried using the dispatcher, but it doesn't change anything, and I don't understand it enough:
await dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
clickedCard.IsFaceDown = false; // makes the image visible via data binding
}
);
Pause();
I think I need to do something with threading, but I am going in circles.
Any ideas?
You should never do something like this inside the UI thread of your app:
new System.Threading.ManualResetEvent(false).WaitOne(1000);
There are various reasons for not doing it, but in your particular case the problem is that XAML only re-draws once your event-handler completes. So basically this happens:
The item is invisible
Your event handler is called
You set it to visible (but the UI doesn't refresh yet)
You freeze the thread for a second
You set it to invisible again
The event-handler completes
Now the UI updates based on the current value (which is invisible)
I suggest you look at building a Storyboard to do this - Blend can help. See here.

AS3 interclass variables

I have a button, that's a class, say, Button. And a main document(class Main) where I can see all the changes in a text field, refreshed every frame (yeah, I don't like trace()). The button has a function of changing a variable that's used in main file. So when I press the button, that I add in the main file to the stage, I need it's function to work. Button class has this.addEventListener(MouseEvent.CLICK,function), further function adds to the var from main class. So if it did add, I would see the changes in the text field. I see changes made by an object I add directly, in the graphic editor, but no changes from the button, whose child I add. Variables are public. Functions are all public. So...
Classes cannot exchange variables freely?
How do I change a variable via a function in a different class?
Maybe I need to export and import var? I thought they all work as a united program that shares all public variables.
Main: I create var i, add Button child to stage and show i every frame =D
Button: When you press me, I add 1 to i =D
SherWood: Main, Y U don't show i, changed by Button? T_T
Code example:
http://piratepad.net/g5tTMFX4Bo
The Button (or any other class) has no built-in knowledge of the environment that is using it's instance.
The DisplayObject ancestors have the ability to check the parent container, and "main" container via parent and stage properties.
The programmers role (you) is to code flexible way to exchange information that any 2 (or more) classes needs to have, e.g. by exposing public methods/properties, dispatching events (that other party can listen) etc.
So, if the Main needs to know that Button (instance of Button) was clicked add listener to that button for click event, and then you will be able to do whatever you want e.g. add one to i
If Button then, for instance needs to present up-to-date value Main via exposed method can do that e.g. myButton.setLabel("Value of i="+i); which may be executed in aforementioned click listener,
regards

How remove/close multiple popup in Flex application?

if we open lot of popup during browsing(web) or in an AIR application, how remove them at once?
I don't think there's really a call for removing all pop-ups with the pop-up manager. I think you would need to keep a reference to each instance in a list and call PopUpManager.removePopUp for each one. Honestly though it's probably not a good idea to have a ton of pop-ups (in terms of user experience) there may be a case for it but I would definitely take some time to consider if it's the best option really.
EDIT:
You could also consider extending PopUpManager and maintain an internal collection, it looks like PopUpManager uses PopUpManagerImpl and doesn't seem to expose the impl property it uses for delegating the actual work so you'd probably need to extend both. But you could then use the PopUpManagerImpl.mx_internal::popupInfo which is an array that has objects that have a property called owner that seems like it would be what you'd want to supply to the calls to removePopUp.
Add all popups in array when u create it. And remove all popups
var popupCollection:ArrayCollection = new ArrayCollection;
var mypopup:IFlexDisplayObject;
PopUpManager.centerPopUp(mypopup=PopUpManager.createPopUp(this,popupWindow));
popupCollection.addItem(mypopup);
u can remove all popup using loop
PopUpManager.removePopUp(popupCollection[index] as IFlexDisplayObject);