I have a JavaFX Application based on vlcj and I want to observe or bind a float.
float position = mediaPlayer.getPosition(); // returns actual position
How can I observe or bind position to a slider? Position has no property.
Slider timeSlider = new Slider(0d, 100d, 0d);
timeSlider.setValue(position); // but position is permanently changing
Do you have any ideas? Thanks a lot. :)
You cannot use a binding here, but:
As I can see in MediaPlayer, you can add listeners:
void addMediaPlayerEventListener(MediaPlayerEventListener listener);
and MediaPlayerEventListener class has a
void positionChanged(MediaPlayer mediaPlayer, float newPosition); method which class is implemented by MediaPlayerEventAdapter.
Media play-back position changed.
Therefore this two way listening should work:
mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
#Override
public void positionChanged(MediaPlayer mediaPlayer, float newPosition) {
timeSlider.setValue(newPosition);
}
});
timeSlider.valueProperty().addListener((obs, oldval, newval) -> mediaPlayer.setPosition(newval));
(Haven't tried it)
Related
I want to make a button that directly extends Actor. The reason I want to do this is because I don't want to go through the trouble of making a text button that has skins and such. I just want to be able to load my png file on the screen and make it clickable. Anyone have ideas or should I just stick to the Textbutton?
How about an ImageTextButton?
Or you use a Button, and add a Stack (which holds multiple items, that you can place on them self)
I don't recommend you, to do your own button. You'll reinvent the wheel.
I suggest: try ImageTextButton.
If that does not help --> Button + Stack
public class TextureActor extends Actor {
private Texture texture;
public TextureActor(Texture texture) {
this.texture = texture;
}
#Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(texture, getX(), getY(), getWidth(), getHeight());
}
}
You can certainly replace Texture with anything else which can be drawn by the batch. Or use another draw method with more detailed parameters.
Buttons don't actually need a skin, so it would be far easier to just make a factory method to simplify it, rather than code all the logic of a proper button:
public static Button makeButton (TextureRegion upImage, TextureRegion downImage, Actor contentActor) {
Button.ButtonStyle style = new Button.ButtonStyle();
style.up = new TextureRegionDrawable(upImage);
style.down = new TextureRegionDrawable(downImage);
return contentActor == null ? new Button(style) : new Button(contentActor, style);
}
For organization's sake, I use multiple scenes for my game and rather than having each scene have a constructor that receives a Viewport (my game is scalable), I would like to set each stage's viewport separate of the constructor, then after the viewport is set, add the actors. In the main class, it would happen like this:
public void setStage(Stage s)
{
if(currentStage != null)
currentStage.dispose();
currentStage = s;
currentStage.setViewport(view);
}
To make this go fluidly, each stage has an init method that is called within an overriden setViewport:
#Override
public void setViewport(Viewport v)
{
super.setViewport(v);
init();
}
However, all this gives me is a black screen... I have tried updating the camera and viewport, but no avail (note that the actors are having their render methods called).
Why am I getting this black screen and how do I fix it? If it's not possible I'll just revert to using the constructor.
If I understood correctly you want to do this:
Stage stage1 = new Stage();
stage1.getViewport().update(width, height);
rather than this:
Stage stage1 = new Stage (new StretchViewport(width, height)); // It doesn't have to be StretchViewport
In the first case (what you are trying to do) a ScalingViewport will be costructed automatically for you with dimensions of the Gdx.graphics and an orthographic camera and acts like a StretchViewport. Why not using the second case directly where you pass the viewport you want. You can always alter your viewport whenever you want by calling stage1.getViewport().update(width, height);
or by calling stage1.setViewport(width, height, false); in older Libgdx versions.
Viewport has changed recently so if you can extend Viewport class to Override the update method maybe you can achieve what you want:
public class ViewportExtendClass extends StretchViewport{
public ViewportExtendClass(float worldWidth, float worldHeight) {
super(worldWidth, worldHeight);
}
#Override
public void update (int screenWidth, int screenHeight, boolean centerCamera) {
super.update(screenWidth, screenHeight, centerCamera);
// DO YOUR INITIALIZATION HERE
}
}
From your main class you create new stage :
Stage stage1 = new Stage (new ViewportExtendClass (width, height));
and then you call :
stage1.getViewport().update(width, height);
Like this you can alter stage viewport and re initialize your assets.
#Override
public void setViewport(Viewport v)
{
super.setViewport(v);
this.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
Camera c = this.getViewport().getCamera();
c.position.set(c.viewportWidth/2, c.viewportHeight/2, 0);
init();
}
This works, but you should also be able to update the Viewport like that at the begin of your application, if you continue to use the same one. I set the position like that instead of centering because some of my Stages will be larger than the screen.
I have a menu screen in libgdx and I had a text button that started a new game like this.
textButton.addListener(new ChangeListener() {
public void changed (ChangeEvent event, Actor actor) {
g.setScreen( new level1(g));
}
});
It looked like crap so I changed it to an image.
playbuttontexture = new Texture(Gdx.files.internal("data/playbutton.png"));
playbuttontexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion playbuttonregion = new TextureRegion(playbuttontexture, 0, 0, 512, 256);//powers of 2
playbutton = new Image(playbuttonregion);
playbutton.setSize(512,256);
playbutton.setBounds(width/2-playbutton.getWidth()/2, height/2-playbutton.getHeight()/2, 512, 256);
//playbutton.setOrigin(playbutton.getWidth()/2, playbutton.getHeight()/2);
playbutton.setPosition(width/2-playbutton.getWidth()/2, height/2-playbutton.getHeight()/2);
and
playbutton.addListener(new ChangeListener() {
public void changed (ChangeEvent event, Actor actor) {
g.setScreen( new level1(g));
}
});
Now when I click it nothing happens? What am I doing wrong?
The problem here is that Image does not fire a changed(...) event anymore. This event is only fired by the TextButton you've used before when the status changes from clicked to not-clicked and the other way around. It can also be fired in other cases, since it is kind of a "generic" event, as the JavaDoc states, but that varies from actor to actor.
Change it to a ClickListener and use the clicked(...) method instead. This event should be fired by all actors in the scene2d.ui package.
for me this is what it looks like when I implemented the code ( I was having a similar issue. noone did a great job at pointing me to the right places to look. )
playbutton.addListener( new ClickListener(){
#Override
public void clicked (InputEvent event, float x, float y) {
//your code to do stuff when the button is clicked
}
});
I am building a simple game with LIBGDX, and I have come across this irritating problem.
I have a MenuScreen.java class, which looks like this:
Here's the full class on pastebin
The important part is the imageButton's inputListener:
button.addListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button){
MineFinder.Log.debug("xpos: " + x + ", ypos: " + y);
game.setScreen(new GameScreen(game));
}
});
My problem is, that if this class is present (and GameScreen gets called from this) for some reason if I click to the ImageButton's position in GameScreen, it still handles the input. This is really irritating, because if a player clicks on that part of the screen the game gets reseted.
If i remove references to my MenuScreen.java file, and skip it alltogether the problem isn't present, so I am pretty sure that the problem is that the InputListener doesn't get "deleted"
Any ideas how to fix this?
Thanks!
The problem is that in your resize method. This line:
Gdx.input.setInputProcessor(stage);
sets the global input processor to be the stage. The input handling code doesn't really care about what is being rendered on the screen.
I think the easiest fix is to set the input processor in show and clear it in hide. Something like this:
#Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
#Override
public void hide() {
Gdx.input.setInputProcessor(null);
}
Alternatively, you could have your callbacks check to see if the button/stage/screen they are in is active, or unregister/re-register the callbacks in show/hide, depending on how you are using your Screens.
When trying to put a simple ImageButton on stage, It didn't seem to detect clicks.
ImageButton btnStart = new ImageButton(ButtonArt.UP, ButtonArt.DOWN));
// btnStart.setClickListener(new ClickListener() {
// #Override
// public void click(Actor a, float arg1, float arg2) {
// a.visible = false;
// }
// });
stage.addActor(btnStart);
ButtonArt.UP and ButtonArt.DOWN are TextureRegions, of each state.
Now when I click on the button, it doesn't change state! I also tried the above ClickListener (for testing), but it seemed that didn't work either.
In my render method I just call stage.act() and stage.render().
I also tried drawing the TextureRegions with SpriteBatch in my render method, and they are in fact different textures.
Am I doing something wrong?
You will need to set the stage as your inputprocessor:
Gdx.input.setInputProcessor(stage);
If you need to have multiple inputprocessors (e.g., you need clicks registered outside your scene), you will need to use an InputMultiplexer, like this:
InputMultiplexer plex = new InputMultiplexer();
plex.addProcessor(myOtherProcessor);
plex.addProcessor(stage);
Gdx.input.setInputProcessor(plex);