How create loop tween? - dart

I'm using stagexl (on dart lang) for my game. I tried find in api how restart tween after it will be complete, but could not. Can you help me? Thnx.

You can not restart a Tween, you have to create a new one. You could also use the new async methods like juggler.onElapsedTimeChange, juggler.interval or juggler.timespan. Or you could implement your own tween-like class which implements the Animatable interface.
Here is an example for the juggler.interval method:
await for (var counter in juggler.interval(delay).take(666)) {
// do something 666 times.
}
Here is an example for juggler.onElapsedTimeChange
await for (var time in juggler.onElapsedTimeChange) {
// do something with time.
}
Here is an exmaple for the Animatable interface
class MyAnimation implements Animatable {
bool advanceTime(num time) {
// do something with time
return true; // animation should continue on next frame
}
}
Please check out the Juggler documentation:
http://www.stagexl.org/docs/wiki-articles.html?article=juggler

Related

Flutter: How to prevent banner ad from showing if view is already disposed

I am currently experimenting with banner ads from the firebase_admob plugin. The process to show and dispose them is pretty straightforward, I do it in initState() and dispose().
The code to create and display the add looks like this:
_bannerAd = createBannerAd();
_bannerAd
..load().then((loaded) {
if (loaded) {
_bannerAd..show();
}
});
However, as I am calling show() asynchronously, it is possible that the view was already closed when the ad is being shown (i.e. by clicking back button really fast). In that case, the dispose() method will never be called and the ad will be "stuck" on the bottom of the screen.
How can I solve this problem? Am I using the banner ad wrong or is it possible to detect if the view was already changed? I tried using the "mounted" property of the state but it didn't seem to work.
Just check "this.mounted" property of state class before showing the add.
_bannerAd = createBannerAd();
_bannerAd
..load().then((loaded) {
if (loaded && this.mounted) {
_bannerAd..show();
}
});
From https://github.com/flutter/flutter/issues/21474#issuecomment-535188820, that's a little hack but it works for me.
You can add a little delay in your dispose method like this:
static void hideBannerAd() {
Future.delayed(Duration(milliseconds: 500), () {
if (_bannerAd != null) _bannerAd.dispose();
_bannerAd = null;
});
}
500 milliseconds is enough.

How to update the display while still in a function (iOS with MonoTouch/Xamarin)

I have a process that queries information and I want to display it on the screen as it is being received in my UIViewController. What I want to do is to use an overlay like this one
https://github.com/xamarin/mobile-samples/blob/master/MWC/MWC.iOS/UI/Controls/LoadingOverlay.cs
and update loadingLabel.Text from the UIViewController. However, adding this view doesn't seem to make it work instantly - i'm guessing that I need the function call to exit before the graphics are updated.
What is the methodology to allow me to display this type of overlay, and have it "run" while I process data? Do I need to move the calls to a separate thread and update the display through delegate functions?
After re-reading and Jason's comment you probably want to do something like the following:
Task.Factory.StartNew (() => {
//Do Process intensive things here
for (int i = 0; i < 999999999; i++) {
System.Threading.Thread.Sleep(1000);
this.InvokeOnMainThread(() =>
{
this.MyLabel.Text == string.Format("Counted to {0}", i);
});
}
});
Basically whenever you want to update the UI you need to do it on the Main Thread.
For UIKit to update the screen, your code needs to run the main loop. The easy way to run the main loop is to run some code and return control, so the existing loop runs and handles your events (timers, input, display changes).
There are a few things that you can do:
Run any blocking operations on the background, and then update the UI by using InvokeOnMainThread. This means that your app is always running the main loop, but every once in a while, a background thread requests that some code is executed (like displaying a status).
Manually run the main loop in the middle of your application. This is not really a good idea, but can work on some very limited cases. See my answer in this question: UIAlertView does not wait
If you can afford running Xamarin.iOS from the Alpha channel, you have access to C#5 async/await. It nicely hide the tasks and callbacks complexity.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
muButton.TouchUpInside += DoSomething;
}
async void DoSomething (object o, EventArge e)
{
myLabel.Text = "Starting";
for (var i = 0; i<10;i++)
{
await ProcessPart(i);
myLabel.Text = String.Format ("Progress : {i}0%", i+1);
}
myLabel.Text = "Done";
}
Task ProcessPart (int i)
{
Task.Delay (1000);
}
If your processing is smart enough to report its own progress, you could change it's signature to IEnumerable<Task<int>> Process () and have a nice refresh loop like:
async void DoSomething (object o, EventArge e)
{
foreach (var tp in Process())
myLabel.Text = String.Format ("Progress : {i}0%", await tp +1);
}
A dummy Process() function could look like:
IEnumerable<Task<int>> Process ()
{
for (var i=0; i<10; i++)
yield return Task.Delay (1000).ContinueWith(x => i);
}

Record sound from microphone in Monotouch.Dialog

Xamarin example found here : http://docs.xamarin.com/samples/Sound works, but the GUI is not Monotouch.Dialog, and it looks poor together with the rest of my MD APP.
How to add a Monotouch.Dialog controller, to start and stop recording, and show the elapsed time while recording.
Apple made this http://developer.apple.com/library/ios/#samplecode/SpeakHere/Introduction/Intro.html with a VU meter included.
Look at this class inside the Xamarin sample https://github.com/xamarin/monotouch-samples/blob/master/Sound/Sound/SoundViewController.cs
Basically, you would want to create you're own DialogViewController:
You will need the using statements from the sample.
using MonoTouch.AVFoundation;
using System.Diagnostics;
using System.IO;
using MonoTouch.AudioToolbox;
public class SoundRecorder : DialogViewController {
this.Title = "Record Sound";
root = new RootElement() {
new section () {
new StringElement ("Record", delegate {
// sound recording code from sample for the first button
}
}
}
}
This should give you a start.
You may want to separate the elements and declare them separately like this:
StringElement myElement = new StyledStringElement("record something");
You can then subscribe to the tapped event with a delegate and handle the when a button is hit that way too.
So that you can do a little more. For more styling choices you would want a StyledStringElement
Hope this helps

Blocking/disabling JavaFX 2 WebView right click

I'm looking for a way to block/disable right click in javafx.scene.web.WebView. To be more specific I don't want the context menu to show up on right click. I'm new to the technology and can't find the way to do it.
Just for the record, it is implemented in JavaFX 2.2. See documentation for setContextMenuEnabled (JavaFX 2) and setContextMenuEnabled (JavaFX 8)
I've came up with working, but ugly, inelegant and, I'd say, partisan solution, which I don't really like, but actually I have no (or just can't find) other way out.
It includes modifying EventDispatcher of WebView.
So my implementation of EventDispatcher needs a reference to original WebView EventDispatcher and looks like that:
public class MyEventDispatcher implements EventDispatcher {
private EventDispatcher originalDispatcher;
public MyEventDispatcher(EventDispatcher originalDispatcher) {
this.originalDispatcher = originalDispatcher;
}
#Override
public Event dispatchEvent(Event event, EventDispatchChain tail) {
if (event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) event;
if (MouseButton.SECONDARY == mouseEvent.getButton()) {
mouseEvent.consume();
}
}
return originalDispatcher.dispatchEvent(event, tail);
}
}
Everytime event is dispatched it goes through our dispatcher and I check if it's right click. If it is I just consume it and proceed further.
To make it work you have to use WebView like that:
WebView webView = new WebView();
EventDispatcher originalDispatcher = webView.getEventDispatcher();
webView.setEventDispatcher(new MyEventDispatcher(originalDispatcher));
Every comment, clue and so on are appreciated.
With JavaFX 2.2+ it's now possible to set WebView's ContextMenuEnabled to false:
webView.setContextMenuEnabled(false);
WebView API Doc.
You can style context menus away using the following css.
.context-menu { -fx-background-color: transparent; }
.menu-item { -fx-background-color: transparent; }
.menu-item .label { -fx-text-fill: transparent; }
.menu-item:show-mnemonics .mnemonic-underline { -fx-stroke: -transparent; }
This will make all context menus and menu items transparent. I'm not sure of the css selector you could use to make this only apply to WebView context menus. If you don't have other menus in your application, that may not be a big deal.
You can do it with js (jquery used)
$(document).ready( function() { document.oncontextmenu = function() { return false } } );
Unfortunately it's not yet possible. There is a feature request for that, which you may want to track: http://javafx-jira.kenai.com/browse/RT-15684
This will will remove the context menu for the entire stage:
primaryStage.getScene().addEventFilter(MouseEvent.MOUSE_RELEASED,
new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
if (event.getButton() == MouseButton.SECONDARY) {
event.consume();
}
}
});

After clicking a button, how do I wait for SwingWorker to finish before proceeding?

I have the following code:
public class FileLoader extends SwingWorker(Void, Void) {
#Override
private Void doInBackground() {
loadFiles();
}
}
public class LogInPage {
private FileLoader fileLoader = new FileLoader();
public LogInPage() {
fileLoader.execute();
}
loginButtonActionPerformed(ActionEvent evt) {
//wait for files to finish loading
//while displaying a waiting cursor
showMainForm();
}
}
My question would be:
After clicking the button, I would want all the files to be loaded first (while displaying an hourglass cursor and progress bar) before showing the main form.
I have done this before with Thread's join() but was not able to do the same with SwingWorker.
I have read about overriding done() and implementing listeners but I can't apply it here.
Any help?
Thanks.
From what you're saying and contrary to what you think, I think you can actually use SwingWorker's done() method. Before execute(), disable the button, start a busy animation, whatever, then in the done() method, do whatever it is you need to do to continue the program. That's what it's for :-)
You should also look at the SwingWorker.publish() and process() to send and receive the progress bar events.
See also: How do I wait for a SwingWorker's doInBackground() method?

Resources