How do I delete individual renderables from the scene using a button using arcore and sceneform? - arcore

I'm trying to make a button that will delete a 3d model from the scene after it is selected.
I know that the way to delete 3d objects is by using anchor.detach(). And I can select individual models using node.select(). However, I am not sure how to access the anchor after selecting the node.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
deleteObject(//I'm not sure how to get the anchor from the selected node//);
}
});
private void deleteObject(Anchor anchor){
anchor.detach();
}

If you want to delete a particular Renderable and you already know the Anchronode, which is what I believe your case is from the question, then the code below will remove the Renderable and Anchornode:
private void removeAnchorNode(AnchorNode nodeToremove) {
//Remove an anchor node
if (nodeToremove != null) {
arFragment.getArSceneView().getScene().removeChild(nodeToremove);
nodeToremove.getAnchor().detach();
nodeToremove.setParent(null);
nodeToremove = null;
Toast.makeText(LineViewMainActivity.this, "Test Delete - anchorNode removed", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LineViewMainActivity.this, "Test Delete - markAnchorNode was null", Toast.LENGTH_SHORT).show();
}
}
The toast are just to help testing so can be removed or left as you choose.
Update - Kotlin version (tested April 2020):
private fun removeAnchorNode(nodeToRemove: AnchorNode) {
//Remove an Anchor node
arFragment.getArSceneView().getScene().removeChild(nodeToRemove);
nodeToRemove.getAnchor()?.detach();
nodeToRemove.setParent(null);
nodeToRemove.renderable = null
}

Related

How to preview a links in Recyclerview

I need to preview links of pages in recyclerview . I used this Library : https://github.com/LeonardoCardoso/Android-Link-Preview but it does not work. anyone know another way ?
Based on our chat in the comments section, you want to add the preview at some position in your scroll list. In that case, you can maintain some state (eg. position) in LinkPreviewCallback. So that when the preview results are ready, based on the maintained state, you can find the exact view where preview needs to be rendered.
Create a Class and Implement LinkPreviewCallback.
class MyPreviewCallback implements LinkPreviewCallback {
int position;
public MyPreviewCallback(int position) {
super();
this.position = position;
}
#Override
public void onPre() {
}
#Override
public void onPos(SourceContent sourceContent, boolean b) {
// update adapter list item based on the position.
}
}
Pass callback to the crawler.
textCrawler.makePreview(new MyPreviewCallback(3), url); // passing position
The main idea is basically to maintain some state in your class which will let you identify the exact View to be updated with Preview.

How to open custom dialog box / popup using Xamarin.Forms?

I am newbie to Xamarin.Forms and stuck with a situation where I want to open up a popup box with my control details [e.g. View Employee Details] on click of parent page.
How can I open custom dialog box / popup using Xamarin.Forms?
Any example code will be appreciated?
Thanks in advance!
If you still want to have your popup's code in its own Page you can set up some custom renderers along the following logic.
1. A ModalPage & corresponding renderer
public class ModalPage : ContentPage { }
public class ModalPageRenderer : PageRenderer {
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
this.View.BackgroundColor = UIColor.Clear;
this.ModalPresentationStyle = UIModalPresentationStyle.OverCurrentContext;
}
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
SetElementSize (new Size (View.Bounds.Width, View.Bounds.Height));
}
}
2. HostPage
public class ModalHostPage : ContentPage, IModalHost
{
#region IModalHost implementation
public Task DisplayPageModal(Page page)
{
var displayEvent = DisplayPageModalRequested;
Task completion = null;
if (displayEvent != null)
{
var eventArgs = new DisplayPageModalRequestedEventArgs(page);
displayEvent(this, eventArgs);
completion = eventArgs.DisplayingPageTask;
}
// If there is no task, just create a new completed one
return completion ?? Task.FromResult<object>(null);
}
#endregion
public event EventHandler<DisplayPageModalRequestedEventArgs> DisplayPageModalRequested;
public sealed class DisplayPageModalRequestedEventArgs : EventArgs
{
public Task DisplayingPageTask { get; set;}
public Page PageToDisplay { get; }
public DisplayPageModalRequestedEventArgs(Page modalPage)
{
PageToDisplay = modalPage;
}
}
}
3. HostPage renderer
public class ModalHostPageRenderer: PageRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if(e.OldElement as ModalHostPage != null)
{
var hostPage = (ModalHostPage)e.OldElement;
hostPage.DisplayPageModalRequested -= OnDisplayPageModalRequested;
}
if (e.NewElement as ModalHostPage != null)
{
var hostPage = (ModalHostPage)e.NewElement;
hostPage.DisplayPageModalRequested += OnDisplayPageModalRequested;
}
}
void OnDisplayPageModalRequested(object sender, ModalHostPage.DisplayPageModalRequestedEventArgs e)
{
e.PageToDisplay.Parent = this.Element;
var renderer = RendererFactory.GetRenderer (e.PageToDisplay);
e.DisplayingPageTask = this.PresentViewControllerAsync(renderer.ViewController, true);
}
}
Then it is as simple as calling
await ModalHost.DisplayPageModal(new PopUpPage());
from your host page or in this particular case from the ViewModel behind.
What Pete said about PushModalAsync / PopModalAsync still remains valid for this solution too (which in my opinion is not a disadvantage), but your popup would appear with transparent background.
The main advantage of this approach, in my opinion, is that you can have your popup XAML/code definition separate from the host page and reuse it on any other page where you wish to show that popup.
The general purpose of what you are trying to achieve can be accomplished by using the PushModalAsync and PopModalAsync methods of Xamarin.Forms Navigation object.
The chances are that this is good enough for what you are needing - However - this isn't truely modal. I will explain after a small code snippet:-
StackLayout objStackLayout = new StackLayout()
{
};
//
Button cmdButton_LaunchModalPage = new Button();
cmdButton_LaunchModalPage.Text = "Launch Modal Window";
objStackLayout.Children.Add(cmdButton_LaunchModalPage);
//
cmdButton_LaunchModalPage.Clicked += (async (o2, e2) =>
{
ContentPage objModalPage = new ContentPage();
objModalPage.Content = await CreatePageContent_Page2();
//
await Navigation.PushModalAsync(objModalPage);
//
// Code will get executed immediately here before the page is dismissed above.
});
//
return objStackLayout;
private async Task<StackLayout> CreatePageContent_Page2()
{
StackLayout objStackLayout = new StackLayout()
{
};
//
Button cmdButton_CloseModalPage = new Button();
cmdButton_CloseModalPage.Text = "Close";
objStackLayout.Children.Add(cmdButton_CloseModalPage);
//
cmdButton_CloseModalPage.Clicked += ((o2, e2) =>
{
this.Navigation.PopModalAsync();
});
//
return objStackLayout;
}
The problem with the above is that the
await Navigation.PushModalAsync(objModalPage);
will immediately return after the animation.
Although you can't interact with the previous page, as we are displaying a new NavigationPage with a Close button shown - the parent Navigation Page is still executing behind the scenes in parallel.
So if you had any timers or anything executing these still would get called unless you stopped those.
You could also use the TaskCompletionSource approach as outlined in the following post also How can I await modal form dismissal using Xamarin.Forms?.
Note - that although you can now await the 2nd page displaying and then when that page is dismissed allowing code execution to continue on the next line - this is still not truely a modal form. Again timers or anything executing still will get called on the parent page.
Update 1:-
To have the content appear over the top of existing content then simply include it on the current page, however make this section of content invisible until you need it.
If you use an outer container such like a Grid that supports multiple child controls in the same cell, then you will be able to achieve what you want.
You will also want to use something like a filled Box with transparency that will cover the entire page also, to control the visible, see through section, that surrounds your inner content section.
I followed above approach and found it impossible to run on iOS 7.
I found this library BTProgressHUD which you can modify and use.
I Use its methods by Dependency service.
Actual library for popups.
https://github.com/nicwise/BTProgressHUD
Following example uses BTProgressHUD library internally.
https://github.com/xximjasonxx/ScorePredictForms

BlackBerry java detecting screen foreground event

In my BlackBerry application, I have a home screen. The user can then navigate to a settings screen. When the user goes back to the home screen, is there no method that is called on the home screen indicating that the screen has come to the foreground?
I have tried onFocus() with no avail.
Thanks!
Unfortunately, hooking on the onExposed is not enough. I found that in Blackberry dialogs are also screens and even context menus are screens too. They are pushed on top of your screen so you receive onExposed callback when they are dismissed.
Though it's OK in many cases, in other cases it poses a problem - e.g. if I must refresh the screen's content only when the user returns to it, but not after menus/dialogs, then how do I do that? My case is, unfortunately, one of those.
I found no documented way of detecting "covered"/"uncovered" events. Here is my approach. onCovered/onUncovered callbacks are called when the current screen is covered/uncovered by another screen of the app, but not by dialogs/menus/virtual keyboard:
public class MyAppScreen extends MainScreen {
private boolean isCovered;
protected void onExposed() {
Log.d("onExposed");
super.onExposed();
if (isCovered) {
onUncovered();
isCovered = false;
}
}
protected void onObscured() {
Log.d("onObscured");
super.onObscured();
final Screen above = getScreenAbove();
if (above != null) {
if (isMyAppScreen(above)) {
isCovered = true;
onCovered();
}
}
}
private boolean isMyAppScreen(final Screen above) {
return (above instanceof MyAppScreen);
}
protected void onUncovered() {
Log.d("onUncovered");
}
protected void onCovered() {
Log.d("onCovered");
}
protected void onUiEngineAttached(final boolean attached) {
if (attached) {
Log.d("UI Engine ATTACHED");
} else {
Log.d("UI Engine DETACHED");
}
super.onUiEngineAttached(attached);
}
protected void onFocusNotify(final boolean focus) {
if(focus){
Log.d("focus GAINED");
} else {
Log.d("focus LOST");
}
super.onFocusNotify(focus);
}
}
And a test. Try various combinations and see what events you receive in the log.
public class TestLifecycle extends MyAppScreen implements FieldChangeListener {
private final ABNTextEdit txt1;
private final ButtonField btn1;
private final ButtonField btn2;
public TestLifecycle() {
final Manager manager = getMainManager();
txt1 = new ABNTextEdit();
manager.add(txt1);
btn1 = new ButtonField("Dialog", ButtonField.CONSUME_CLICK);
btn1.setChangeListener(this);
manager.add(btn1);
btn2 = new ButtonField("Screen", ButtonField.CONSUME_CLICK);
btn2.setChangeListener(this);
manager.add(btn2);
}
public void fieldChanged(final Field field, final int context) {
if (field == btn1) {
Dialog.alert("Example alert");
} else if (field == btn2) {
UiApplication.getUiApplication().pushScreen(new TestLifecycle());
}
}
}
Update:
This method has a limitation: if a new screen is pushed when a dialog or the soft keyboard has focus your current screen will not receive onCovered/onUncovered notification.
Example A: if you have an input field of fixed size and you push a new screen when the user completes it, your current screen will not receive the notification if the user types very quickly. This happens because in the moment between you call push(newScreen) and it is actually pushed the user clicks on a letter on soft KB and it grabs the focus. So only onObscured is called, but not onCovered.
Solution: explicitly hide the soft keyboard before the push(newScreen).
Example B: if you have a customized dialog which pushes new screen and then dismisses itself, your current screen will not receive the notification. This happens because your customized dialog is not recognized as a screen, so only onObscured is called, but not onCovered.
Solution: dismiss the dialog in the first place returning a result value, and let your screen push the new screen based on that value. -OR- override isMyAppScreen() to return true also for your customized dialog.
You should be able to use protected void onExposed() to detect when it is displayed again.

Connect Blackberry menuitem to onscreen buttons

I have requirement for having onscreen navigation buttons along with menu items on blackberry. I need to generate menu item commands as onscreen buttons. Is there a way to generate onscreen menu item buttons in Blackberry? i.e On each screen of my application the menu items should be populated as onscreen buttons both having same functionality?
Thank you
The easiest way to accomplish what you're trying to do is write one function then have both the button and the menu item use the same function.
For example:
function doSomething() {
// Your Code Here
}
// In the function building your screen
MenuItem somethingMi = new MenuItem() {
private MenuItem() { super("Do Something",100001, 5); }
public void run() { doSomething() };
}
Button somethingBtn = new ButtonField("Do Something");
somethingBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context){
doSomething();
}
}
addMenuItem(somethingMI);
add(somethingBtn);

How to create a custom field containing a VideoControl and an image?

Here is my problem : I would like to create a specific component with the camera and an image representing a target overlaying the VideoControl's view.
First I would like to display the camera in a MainScreen with a TitleBar.
Here is my code :
public class ScanScreen extends MainScreen {
private ScanScreen()
{
super();
this.vfm = new VerticalFieldManager(Field.FIELD_VCENTER);
this.controller = new ScanController(this);
//Initialize the player.
try
{
this.player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768");
this.player.realize();
this.player.prefetch();
this.videoControl = (VideoControl) this.player.getControl("VideoControl");
if(this.videoControl != null)
{
// Initialize the field where the content of the camera shall be displayed.
Field videoField = (Field) this.videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
// Display the video control.
this.videoControl.setDisplayFullScreen(true);
this.videoControl.setVisible(true);
// Start the player.
this.player.start();
// Add the video field to the main screen.
if(videoField != null)
{
this.vfm.add(videoField);
}
else
{
LabelField sorry = new LabelField("Sorry, we cannot use camera right now.");
this.vfm.add(sorry);
}
}
}
catch(Exception e)
{
Dialog.alert(e.toString());
}
// TODO : the camera is hiding the title bar
this.setTitle("Title");
this.add(this.vfm);
}
}
The first problem is that the view of the VideoContol is hiding my title bar. How can I fix that ?
The second thing : I have a specific rectangular image representing a target with transparency which I would like to displaying over the view of the VideoControl.
I first tried to create a new class extending Field returning the dimensions of this image and make the image displayed in the paint method (this class is "mypackage.CameraField"). Then I tried to instantiate my VideoField with the name of my new class "mypackage.CameraField" in the initDisplayMode ; but the created videoField is null.
So, is there a solution to create that kind of behaviour ? I am thinking about the native application "Video Camera" in the "Multimedia" folder that displays a VideoControl in a specific area of the screen.
Thank you.
Ok, it seems that is not possible to customize the VideoControl in any way. We must use it in fullscreen and cannot display anything on it.

Resources