How to implement Mouse Look in Java3d - java-3d

I'm at a loss here. I've got a simple terrain generation algorithm working, and I've got some simple keyboard navigation working by extending ViewPlatformAWTBehavior and handling my own events. That's all well and good, and I can follow terrain. Hooray!
What I'd like to do is get some simple "mouse look" working. MouseRotate is close, but I'm looking for something more like an FPS ... where you aren't going to roll the camera, you're limited to 90 degrees vertical (up or down), and the mouse cursor is captured by the JFrame (with an escape).
I just can't seem to get it to work. I can capture the mouse event just fine, and mouseMoved works. I could probably just use an invisible cursor, and that would get me a large part of the way there (maybe), but I'd be stuck when trying to keep the mouse in the screen ... as soon as you're out of the frame, the mouse would be visible and stop rotating the view.
I keep thinking I must be going about this wrong, because this has to be a fairly common implementation, but I can't find anything on it.
Does anyone have some pointers or references?

If you want this is the code to make a full screen window which will help you with mouse problems
package fullscreen;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FullScreen extends JFrame{
private JPanel contentPane = new JPanel();
private JButton fullscreenButton = new JButton("Fullscreen Mode");
private boolean Am_I_In_FullScreen = false;
private int PrevX,PrevY,PrevWidth,PrevHeight;
public static void main(String[] args) {
FullScreen frame = new FullScreen();
frame.setSize(600,500);
frame.setVisible(true);
}
public FullScreen(){
super("My FullscreenJFrame");
setContentPane(contentPane);
//From Here starts the trick
FullScreenEffect effect = new FullScreenEffect();
fullscreenButton.addActionListener(effect);
contentPane.add(fullscreenButton);
fullscreenButton.setVisible(true);
}
private class FullScreenEffect implements ActionListener{
#Override
public void actionPerformed(ActionEvent arg0) {
if(Am_I_In_FullScreen == false){
PrevX = getX();
PrevY = getY();
PrevWidth = getWidth();
PrevHeight = getHeight();
dispose(); //Destroys the whole JFrame but keeps organized every Component
//Needed if you want to use Undecorated JFrame
//dispose() is the reason that this trick doesn't work with videos
setUndecorated(true);
setBounds(-10,-100,getToolkit().getScreenSize().width+30,getToolkit()
.getScreenSize().height+110);
setVisible(true);
Am_I_In_FullScreen = true;
}
else{
setVisible(true);
setBounds(PrevX, PrevY, PrevWidth, PrevHeight);
dispose();
setUndecorated(false);
setVisible(true);
Am_I_In_FullScreen = false;
}
}
}
}

Related

Is there a way to be able to flip an FlxCamera to the x axis?

setScale() doesn't work either.
I was going to flip the camera for when something interesting happens, but I don't how how.
You could flip the camera (and apply other interesting effects) with shaders!
FlxCamera has a function called setFilters() that allows you to add a list of bitmap filters to an active camera.
Here's a simple filter I wrote that flips all textures horizontally:
import openfl.filters.ShaderFilter;
var filters:Array<BitmapFilter> = [];
// Add a filter that flips everything horizontally
var filter = new ShaderFilter(new FlipXAxis());
filters.push(filter);
// Apply filters to camera
FlxG.camera.setFilters(filters);
And in a separate class called FlipXAxis:
import flixel.system.FlxAssets.FlxShader;
class FlipXAxis extends FlxShader
{
#:glFragmentSource('
#pragma header
void main()
{
vec2 uv = vec2(1.0 - openfl_TextureCoordv.x, openfl_TextureCoordv.y);
gl_FragColor = texture2D(bitmap, uv);
}')
public function new()
{
super();
}
}

MRTK and Grip Press

I'm using MRTK 2.3.0 trying to catch a "Grip Press" event from the Mixed Reality motion controller.
I've setup the Input Action in the MRTK Toolkit in the Hierarchy of Unity. I've also assigned the action to the controller's grip button in the Controller Definitions. I'm using the following code and made sure the Grip variable is assigned to the Grip Press event. Nothing happens... I'm able to catch touchpad and joystick, menu button press, but not Grip? Why?
According to this documentation: https://microsoft.github.io/MixedRealityToolkit-Unity/Documentation/MixedRealityConfigurationGuide.html the Grip should be a "float" as it's designed as single axis (I wonder why, since it's a button and not a trigger...). However, I'm trying to catch the event where I can... not working...
Anyone understand what the heck I'm trying to say here? (sorry, the code below also includes other events that I can catch without a hickup).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit;
public class TestInput : MonoBehaviour, IMixedRealityBaseInputHandler,
IMixedRealityInputHandler<Vector2>, IMixedRealityInputHandler
{
public MixedRealityInputAction Trigger, Grip, Scroll, Swipe, Joystick, DPad, TouchPadClicked, TouchPadTouched, MenuPressed;
public MixedRealityInputAction DPadUp, DPadDown, DPadLeft, DPadRight, JoystickUp, JoystickDown, JoystickLeft, JoystickRight;
private bool TouchpadPressed, MenuButtonPressed, GrabPressed, TouchDialogPadPressed, TouchpadClicked, Thouchpadtouched,
ThumbstickActive, ThumbstickPressed, VRManipulationStarted, ScrubStarted, PlayPause = false;
private void OnEnable()
{
IMixedRealityInputSystem inputSystem;
if (MixedRealityServiceRegistry.TryGetService<IMixedRealityInputSystem>(out inputSystem))
{
inputSystem?.RegisterHandler<IMixedRealityBaseInputHandler>(this);
inputSystem?.RegisterHandler<IMixedRealityInputHandler<Vector2>>(this);
inputSystem?.RegisterHandler<IMixedRealityInputHandler>(this);
inputSystem?.RegisterHandler<IMixedRealityInputHandler<float>>(this);
}
}
private void OnDisable()
{
IMixedRealityInputSystem inputSystem;
if (MixedRealityServiceRegistry.TryGetService<IMixedRealityInputSystem>(out inputSystem))
{
inputSystem?.UnregisterHandler<IMixedRealityBaseInputHandler>(this);
inputSystem?.UnregisterHandler<IMixedRealityInputHandler<Vector2>>(this);
inputSystem?.UnregisterHandler<IMixedRealityInputHandler>(this);
inputSystem?.UnregisterHandler<IMixedRealityInputHandler<float>>(this);
}
}
public void OnInputChanged(InputEventData<Vector2> ed)
{
Debug.Log("InputChanged");
if (ed.MixedRealityInputAction == DPad)
{
Debug.Log("Touched Touchpad at:" + ed.InputData.x.ToString() + "," + ed.InputData.y.ToString());
}
if (ed.MixedRealityInputAction == Joystick)
{
Debug.Log("Touched Joystick at:" + ed.InputData.x.ToString() + "," + ed.InputData.y.ToString());
}
}
public void OnInputChanged(InputEventData ed)
{
if (ed.MixedRealityInputAction == MenuPressed)
{
Debug.Log("Menu button pressed");
//ActionText.text = "Grab pressed";
}
}
public void OnInputChanged(InputEventData<float> ed)
{
Debug.Log("Float Changed");
if (ed.MixedRealityInputAction == Grip)
Debug.Log("Grab Pressed");
}
Ok... I got this..... It's a BUG in MRTK.
I have 18 actions defined (when you have DPad left, up, down etc it goes by fast!).
When I try to assign my MixedRealityInputAction to an InputAction in the Inspector, when I try to select GrabPressed (which is the 18th action), I get
IndexOutOfRangeException: Index was outside the bounds of the array.
Microsoft.MixedReality.Toolkit.Input.Editor.InputActionPropertyDrawer.OnGUI
Therefore the action does not work....
I tried to use another InputAction instead and assign it to the grab button (I used Select in this case) and it works like a charm....
Beware....
Try
Input.GetAxisRaw(ControllerMappingLibrary.AXIS_11) > 0 || Input.GetAxisRaw(ControllerMappingLibrary.AXIS_12) > 0;
AXIS_11 is grip press for the left controller while 12 is for the right controller

Selenium.StaleElementReferenceException on action.SendKeys after following link?

I am having some issues using selenium, and specifically using actions, although this could just be a symptom for a bigger issue. To quickly explain what try to do:
I scroll down to the bottom of a page using SendKeys(Keys.ArrowDown)
I press a button, and I change the page to a different language.
I try to scroll down on the new page using SendKeys(Keys.ArrowDown). This is where i receive an error!
The strange thing here is that i have no issues with the scrolling in step 1 even though I am using the same function, but in step 3 i receive an error message:
OpenQA.Selenium.StaleElementReferenceException: 'The element reference of is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed'
I have tried to redeclare my footer variable, and also included it as a Footer class variable (originally it wasn't), but nothing i do change anything
The reason I am using SendKeys and not MoveToElement is due to MoveToElement doesn't work for off-screen elements in Firefox. I have included all relevant code below, including an image of the error and when it happens.
Could anyone please advice what I am doing wrong?
[TestMethod]
public void Reset_newsletter_subscription_form_BR_site()
{
Browser.Goto(siteUrl);
Webpage.Footer.GoTo_CountryPageViaFooter("br");
Webpage.Footer.ScrollToFooter(); // -> This is where it fails!
Other.Irrelevant.Stuff();
}
Below this is the Selenium parts:
public static class Browser
{
public static IWebDriver webDriver;
public static Actions actions;
public static void Goto(string url)
{
webDriver.Url = url;
}
}
public static class Webpage
{
public static Footer Footer
{
get
{
var footer = new Footer(Browser.webDriver, Browser.actions);
return footer;
}
}
}
public class Footer
{
private IWebDriver webDriver;
private Actions actions;
private IWebElement footer;
public Footer(IWebDriver webDriver, Actions actions)
{
this.webDriver = webDriver;
this.actions = actions;
}
public void GoTo_CountryPageViaFooter(string CountryTag)
{
footer = webDriver.FindElement(By.ClassName("c-footer"));
var changeCountryButton = footer.FindElement(By.ClassName("c-footer__toggle-country-selector"));
ScrollToFooter();
actions.MoveToElement(footer).Perform();
actions.MoveToElement(changeCountryButton).Perform();
changeCountryButton.Click();
var intPageLink = footer.FindElement(By.XPath("//*[#href='/" + CountryTag + "/']"));
intPageLink.Click();
}
public void ScrollToFooter()
{
footer = webDriver.FindElement(By.ClassName("c-footer"));
//MoveToElement does not work for Firefox, so a workaround is needed.
if (webDriver is FirefoxDriver)
{
int i = 0;
while (i < 5)
{
actions.SendKeys(Keys.ArrowDown).Perform(); //This is where it fails!
i++;
}
}
actions.MoveToElement(footer).Perform();
}
Image of the exception
Whenever Perform() method is invoked, it figures how to interact with the Page. So its job is to find out the active element present in the Page(if element not specified as in MoveToElement(element) or during clicking an element using Actions).
So in your case, as no Element is specified actions.SendKeys(Keys.ArrowDown).Perform(); so Actions will focus on any Active Element present in the Page and Perform the SendKeys Operation on that.
Details about Actions Interaction with Web Page..
So, as soon as the Language change link is getting clicked the Elements attached to the DOM are changing as a result Selenium Webdriver detects this as a change in the current Active Element as a result StaleElementReference Exception thrown.
In order to get rid of the Exception, you can add wait statement in between or there is a great way to handle StaleElementReference Exception given here
Thanks :)
I wouldn't use .SendKeys() to scroll the page. It won't be consistent. Imagine if the page is longer or shorter... how many times will you need to scroll? I think a better approach is to use JS to scroll the page to the desired element.
public void ScrollToFooter()
{
footer = webDriver.FindElement(By.ClassName("c-footer"));
// MoveToElement does not work for Firefox, so a workaround is needed
if (webDriver is FirefoxDriver)
{
IJavaScriptExecutor jse = (IJavaScriptExecutor)webDriver;
jse.ExecuteScript("arguments[0].scrollIntoView();", footer);
}
else
{
actions.MoveToElement(footer).Perform();
}
}
If you decide to stick with your method, you have a bug because of a missing else. If the driver is FF, after your scrolldown code is executed, it will execute .MoveToElement() and fail.
You could simplify this function to just use JS for all drivers.
public void ScrollToFooter()
{
footer = webDriver.FindElement(By.ClassName("c-footer"));
IJavaScriptExecutor jse = (IJavaScriptExecutor)webDriver;
jse.ExecuteScript("arguments[0].scrollIntoView();", footer);
}
Instead of
actions.SendKeys(Keys.ArrowDown).Perform();
I suggest:
webDriver.FindElement(By.cssSelector("body")).sendKeys(Keys.ArrowDown);

Javafx slider: text as tick label

I just started to learn Javafx, and I really like it so far. However in my current project I would need to use text as the tick labels for the javafx slider. I googled it a lot, but couldn't find any help. So for example I would like a slider with 4-5 positions, and them being "bad", "good", etc. instead of 1,2,3,...
I know I could build a custom UI manually with labels placed at the correct places, but I need to generate the sliders with custom text, and diofferent length every time, so it wouldn't work. In swing it is possible to change the labels with a hashtable for example, so my question is, is it possible to do it in Javafx?
Here is a sample using slider.setLabelFormatter.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;
public class SlidingScale extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Slider slider = new Slider(0, 3, 0);
slider.setMin(0);
slider.setMax(3);
slider.setValue(1);
slider.setMinorTickCount(0);
slider.setMajorTickUnit(1);
slider.setSnapToTicks(true);
slider.setShowTickMarks(true);
slider.setShowTickLabels(true);
slider.setLabelFormatter(new StringConverter<Double>() {
#Override
public String toString(Double n) {
if (n < 0.5) return "Novice";
if (n < 1.5) return "Intermediate";
if (n < 2.5) return "Advanced";
return "Expert";
}
#Override
public Double fromString(String s) {
switch (s) {
case "Novice":
return 0d;
case "Intermediate":
return 1d;
case "Advanced":
return 2d;
case "Expert":
return 3d;
default:
return 3d;
}
}
});
slider.setMinWidth(380);
HBox layout = new HBox(slider);
layout.setPadding(new Insets(30));
primaryStage.setScene(new Scene(layout));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The sample above only works in Java 8 due to a bug in JavaFX 2.2.
Some of the labels disappear as you make the the slider smaller, so I set a min width on the slider so that the labels remain (an alternate solution, might be to set a new label formatter with abbreviated labels used if the slider is sized small).
You might also be interested in voting for or commenting on the following feature request: RT-27863 A LabelFormatter isn't enough to customize Slider ticks.

Animation not starting until UI updates or touch event

I have a strange problem with an AlphaAnimation. It is supposed to run repeatedly when an AsyncTask handler is called.
However, the first time the handler is called in the Activity, the animation won't start unless I touch the screen or if the UI is updated (by pressing the phone's menu button for example).
The strange part is that once the animation has run at least once, it will start without problem if the handler is called again.
Here's what the code looks like:
// AsyncTask handler
public void onNetworkEvent()
{
this.runOnUiThread(new Runnable() {
#Override
public void run()
{
flashScreen(Animation.INFINITE);
}
});
}
// Called method
private void flashScreen(int repeatCount)
{
final View flashView = this.findViewById(R.id.mainMenuFlashView);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setRepeatCount(repeatCount);
alphaAnimation.setRepeatMode(Animation.RESTART);
alphaAnimation.setDuration(300);
alphaAnimation.setInterpolator(new DecelerateInterpolator());
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation)
{
flashView.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation)
{
flashView.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
flashView.startAnimation(alphaAnimation);
}
I have noticed that runOnUIThread isn't necessary (same results occur if I don't use it), but I prefer keeping it as I'm not on the UI thread.
Any ideas on what could cause this?
A little more research showed that my problem was the same a this question:
Layout animation not working on first run
The flashView's visibility was set to GONE by default (causing the Animation not to start immediately as the View had never been rendered), so I just need to set it to INVISIBLE before calling flashView.startAnimation()
If setting the View to VISIBLE won't work, as was in my case, it helped for me to call requestLayout() before starting the Animation, like so:
Animation an = new Animation() {
...
view.requestLayout();
view.startAnimation(an);
In my case, my View was 0dip high which prevented onAnimationStart from being called, this helped me around that problem.
This worked for me:
view.setVisibility(View.VISIBLE);
view.startAnimation(animation);
I had to set the view to VISIBLE (not INVISIBLE, neither GONE), causing the view renderization needed to animate it.
That's not an easy one. Till you got a real answer : The animation start is triggered by onNetworkEvent. As we don't know the rest of the code, you should look there, try to change onNetworkEvent by an other event that you can easily identify, just to debug if the rest of the code is ok or if it's just the trigger that is responsible for it.
May be it will help someone, because previous answers not helped me.
My animation was changing height of view (from 0 to it's real height and back) on click - expand and collapse animations.
Nothing worked until i added listener and set visibility to GONE, when animation ends:
collapseAnim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
view.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
And when expand just set it to VISIBLE before animation:
view.setVisibility(View.VISIBLE);
view.startAnimation(expandAnim);