Cannot show user input with a textfield - input

I'm trying to use a TextField to get some user input:
public void render() {
Gdx.gl.glClear(GL11.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(0, 0, 0, 0);
batch.begin();
batch.end();
stage = new Stage();
Gdx.input.setInputProcessor(stage);
Skin skin = new Skin(Gdx.files.internal("assets/uiskin.json"));
TextButton btnLogin = new TextButton("Click", skin);
btnLogin.setPosition(300, 300);
btnLogin.setSize(300, 60);
btnLogin.addListener(new ClickListener() {
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
System.out.println(txfUsername.getText());
return false;
}
});
txfUsername = new TextField("", skin);
txfUsername.setPosition(300, 250);
txfUsername.setSize(300, 40);
stage.addActor(txfUsername);
stage.addActor(btnLogin);
stage.act();
stage.draw();
}
All I get is a blank field. The user can't interact with it in any way.
I followed the instructions in this video
How do I make the textfield editable?

You use txfUsername = new TextField ("", skin); in your render method. That creates a new TextField from scratch on each render.
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
// do other rendering ...
batch.end();
Gdx.app.log("MyTextField", txfUsername.getText());
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
In your class variables:
private TextButton btnLogin;
TextField txfUsername;
In your method show or create (not render):
#Override
public void show() {
btnLogin = new TextButton("Click", skin);
btnLogin.setPosition(300, 300);
btnLogin.setSize(300, 60);
btnLogin.addListener(new ClickListener() {
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
System.out.println(txfUsername.getText());
return false;
}
});
txfUsername = new TextField("", skin);
txfUsername.setPosition(300, 250);
txfUsername.setSize(300, 40);
stage.addActor(txfUsername);
stage.addActor(btnLogin);
}
Use txfUsername.getText(); written for the field
Edit: I do not know how you worked with GL11, if GL10, I could understand a you malfunction, if you get error in GL10, you should update libgdx, GL10 is an interface and I think last versions for in libgdx not bring already

Related

Is there any way to force execute the OnSizeAllocated method?

I have created a view using Xaml code behind. I did it using the code behind because I wanted to change the layout of the view based on the device orientation. So, the problem which I am facing is that the OnSizeAllocated method is being called after the view is loaded. So, it is unable to change the layout as per the device orientation. I just want to know if there is any way to invoke the OnSizeAllocated method before the view is loaded. Please click on the below link to view the code:
Please click Here to view the Code
1.Rearrange the Page
you could check if width is greater than height to determine if the device is now in landscape or portrait:
public partial class Page13 : ContentPage
{
private double _width ;
private double _height ;
private Grid grid;
private Label label;
private Entry entry;
private Button button;
public Page13 ()
{
_width = this.Width;
_height = this.Height;
label = new Label(){Text = "i am a laber"};
entry = new Entry(){WidthRequest = 200};
button = new Button(){Text = "Submit"};
grid = new Grid();
UpdateLayout();
StackLayout stackLayout = new StackLayout();
stackLayout.Children.Add(grid);
Content = stackLayout;
}
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
if (_width != width || _height != height)
{
_width = width;
_height = height;
UpdateLayout();
}
}
void UpdateLayout()
{
grid.RowDefinitions.Clear();
grid.ColumnDefinitions.Clear();
grid.Children.Clear();
if (_width > _height)
{
ScreenRotatedToLandscape();
}
else
{
ScreenRotatedToPortrait();
}
}
private void ScreenRotatedToLandscape()
{
grid.RowDefinitions.Add(new RowDefinition(){Height = new GridLength(1,GridUnitType.Auto)});
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
grid.ColumnDefinitions.Add(new ColumnDefinition(){Width = new GridLength(1,GridUnitType.Auto)});
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
grid.Children.Add(label,0,0);
grid.Children.Add(entry, 1, 0);
grid.Children.Add(button, 0, 1);
Grid.SetColumnSpan(button,2);
}
private void ScreenRotatedToPortrait()
{
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
grid.Children.Add(label, 0, 0);
grid.Children.Add(entry, 0, 1);
grid.Children.Add(button, 0, 2);
}
}
This is the recommended implementation pulled right from the Xamarin.Forms documentation.
2.Using Xamarin.Essentials
It adds additional functionality to cross-platform applications built in Xamarin. One of these new features is the ability to ping the device for the current orientation by accessing the DeviceDisplay.ScreenMetrics.Orientation property. This returns the current device orientation, which can be used to determine which layout to render.
it's similar to the one above
private bool IsPortrait;
public Page13 ()
{
...
IsPortrait = DeviceDisplay.ScreenMetrics.Orientation == ScreenOrientation.Portrait;
UpdateLayout();
...
}
void UpdateLayout()
{
grid.RowDefinitions.Clear();
grid.ColumnDefinitions.Clear();
grid.Children.Clear();
if (IsPortrait)
{
ScreenRotatedToPortrait();
}
else
{
ScreenRotatedToLandscape();
}
}
You can't force run that since the SizeAllocation hasn't changed, but you could do this to get orientation on initial load:
If you add the Xamarin.Essentials nuget package, as you can see here, you can get the orientation using this line of code DeviceDisplay.MainDisplayInfo.Orientation and you will get Landscape, Portrait, Square, or Unknown.
If you don't want to add the package, you can just use Application.Current.MainPage.Width and Application.Current.MainPage.Height to figure out orientation.

How to add objects to an ArrayList?

I am very new on programming, and I am trying to build a little game so I can teach myself OOP, however I have an problem that I cant solve it at all.
I have a class ComponentsPanel from which I am calling another (JavaFish) as an ArrayList. The addFish() method adds to the ArrayList a JavaFish. When I call addFish() method from the constructor works perfectly fine.
The problem is, when I call it from the start(), from another class and package, it dose not add any element to the ArrayList.
Main class
package framework;
public class Core {
JFrame window;
ComponentsPanel panel;
int width = 600;
int height = 400;
public void start() {
window = new JFrame();
window.setTitle("Java Game");
window.setPreferredSize(new Dimension(width, height));
window.setMinimumSize(new Dimension(width, height));
window.setMaximumSize(new Dimension(width, height));
window.setResizable(false);
panel = new ComponentsPanel();
window.add(panel, BorderLayout.CENTER);
window.setVisible(true);
window.pack();
}
public void stop() {
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
ComponentsPanel
package framework;
public class ComponentsPanel extends JPanel {
ArrayList<JavaFish> javaFishes = new ArrayList<JavaFish>();
public ComponentsPanel() {
javaFishes = new ArrayList<JavaFish>();
//addFish(new JavaFish(100, 100, 25, 25));
}
public void addFish(JavaFish javaFish) {
javaFishes.add(javaFish);
repaint();
}
public void start() {
addFish(new JavaFish(100, 100, 25, 25));
}
#Override
public void paint(Graphics g) {
for (JavaFish jFish : javaFishes) {
jFish.draw(g);
}
}
}
Fish class
package framework;
public class JavaFish {
BufferedImage img;
private int x_pos;
private int y_pos;
private int sizeX;
private int sizeY;
public JavaFish(int x_pos, int y_pos, int sizeX, int sizeY) {
this.x_pos = x_pos;
this.y_pos = y_pos;
this.sizeX = sizeX;
this.sizeY = sizeY;
}
public void draw(Graphics g) {
BufferedImage JavaFish = LoadImage("img/JavaFish.png");
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(JavaFish, x_pos, y_pos, sizeX, sizeY, null);
}
BufferedImage LoadImage(String FileName) {
img = null;
try {
img = ImageIO.read(new File (FileName));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
}
Simulation class
package user;
import framework.ComponentsPanel;
import framework.Core;
public class Simulation {
Core c;
ComponentsPanel panel;
boolean endSim = false;
public Simulation() {
c = new Core();
panel = new ComponentsPanel();
}
private void populate() {
panel.start()
}
private void updateWorld() {
while (!endSim) {
c.start();
break;
}
c.stop();
}
public static void main(String[] args) {
Simulation simulation = new Simulation();
simulation.populate();
simulation.updateWorld();
}
}
start() method present in ComponentsPanel is not a lifecycle method. So unless you invoke start() method manually it won't get invoked. You can add a print statement for tracking the calls.
#Cristea Thank you for the updated code.
This will work if you call the addFish() method on the ComponentsPanel instance created in Core class's start() method as below.
public void start() {
window = new JFrame();
window.setTitle("Java Game");
window.setPreferredSize(new Dimension(width, height));
window.setMinimumSize(new Dimension(width, height));
window.setMaximumSize(new Dimension(width, height));
window.setResizable(false);
panel = new ComponentsPanel(); // This panel is passed to the JFrame
panel.addFish(new JavaFish(100, 100, 25, 25)); // so do addFish() here.
window.add(panel, BorderLayout.CENTER);
window.setVisible(true);
window.pack();
}
The problem with your code was, you are creating two instances of ComponentsPanel class. On the first instance(let's call it panel-1 ) you created, you are calling addFish() method, through the method below. This start() method is called on panel-1.
// panel you used to call this start() method was not used in JFrame
public void start() {
addFish(new JavaFish(100, 100, 25, 25));
}
Then you are creating 2nd instance of ComponentsPanel class(let's call it panel-2) from the Core class's start() method and panel-2 does not do addFish(). But this instance (panel-2)is what is added to the JFrame, which will be used for the purpose of paint(). Since panel-2 does not do addFish(), paint() does not find any JavaFishes.
Remember, instance variable javaFishes is specific to each instance of ComponentsPanel. javaFishes added by panel-1 would not be available for panel-2.

Libgdx - camera.unproject is not fixing my coordinates

The following code gives me very strange y coordinates.
10-18 00:13:36.834 30543-30567/com.xxxx.yyyy.android I/x﹕ 137.4782
10-18 00:13:36.834 30543-30567/com.xxxx.yyyy.android I/y﹕ -1984.2426
10-18 00:13:36.835 30543-30567/com.xxxx.yyyy.android I/ux﹕ 91.65213
10-18 00:13:36.835 30543-30567/com.xxxx.yyyy.android I/uy﹕ -1984.2426
I imagine I set up everything wrong rather than do it wrong while running?
The camera.unproject call should take care of all remapping from screen coordinates to game coordinates, shouldn't it? Or do i have to scale and invert before unprojecting?
package com.xxxx.yyyy;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
public class LetterActor extends Actor
{
private Texture texture;
private Vector3 touchPosition = new Vector3();
private Camera camera;
private boolean unproject = true;
public LetterActor(Texture letterTexture, Camera theCamera)
{
texture = letterTexture;
camera = theCamera;
touchPosition.set(240, 800, 0);
camera.unproject(touchPosition);
setPosition(touchPosition.x, touchPosition.y);
setSize(texture.getWidth(), texture.getHeight());
addListener(new InputListener()
{
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
touchPosition.set(x, y, 0);
if (unproject)
{
camera.unproject(touchPosition);
}
setPosition(touchPosition.x, touchPosition.y);
logPositions(x, y, touchPosition.x, touchPosition.y);
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button)
{
touchPosition.set(x, y, 0);
if (unproject)
{
camera.unproject(touchPosition);
}
setPosition(touchPosition.x, touchPosition.y);
logPositions(x, y, touchPosition.x, touchPosition.y);
}
#Override
public void touchDragged(InputEvent event, float x, float y, int pointer)
{
touchPosition.set(x, y, 0);
if (unproject)
{
camera.unproject(touchPosition);
}
setPosition(touchPosition.x, touchPosition.y);
logPositions(x, y, touchPosition.x, touchPosition.y);
}
});
}
private void screenTo()
{
}
private void logPositions(float x, float y,float ux, float uy)
{
Gdx.app.log("x", Float.toString(x));
Gdx.app.log("y", Float.toString(y));
Gdx.app.log("ux", Float.toString(ux));
Gdx.app.log("uy", Float.toString(y));
}
#Override
public void draw(Batch batch, float alpha)
{
batch.draw(texture, getX(), getY(), getWidth(), getHeight());
}
#Override
public void act(float delta) {}
}
package com.xxxx.yyyy;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.utils.viewport.FitViewport;
public class WordPuzzle extends ApplicationAdapter
{
private final static float VIRTUAL_WIDTH = 480;
private final static float VIRTUAL_HEIGHT = 800;
private OrthographicCamera camera;
private FitViewport viewport;
private Stage stage;
#Override
public void create()
{
camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
camera.setToOrtho(false, VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
viewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, camera);
stage = new Stage();
stage.setViewport(viewport);
Gdx.input.setInputProcessor(stage);
Texture[] textures = LetterLoader.loadLetters();
for (int i = 0; i < textures.length; i++)
{
LetterActor letterActor = new LetterActor(textures[i], camera);
letterActor.setTouchable(Touchable.enabled);
stage.addActor(letterActor);
}
}
#Override
public void render()
{
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
#Override public void resize(int width, int height)
{
stage.getViewport().update(width, height, true);
}
#Override public void dispose()
{
stage.dispose();
}
}
package com.xxxx.yyyy;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
public class LetterLoader {
public static Texture[] loadLetters()
{
Texture[] letters = new Texture[26];
for (int i = 0; i < 26; i++)
{
char letter = (char) (i + 65);
letters[i] = new Texture(Gdx.files.internal("bigletters/" + letter + ".png"));
}
return letters;
}
}
First, the touch position (x, y) you get from the input listener are already the correct coordinates.
Concerning your output, you actually print y two times, but call it uy the second time:
Gdx.app.log("uy", Float.toString(y));
If touchPosition.set(240, 800, 0); is in screen coordinates, then you need to unproject them, but
camera.unproject(touchPosition);
assumes that your camera fills the whole screen, thus it calls internally:
unproject(screenCoords, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Since you use a virtual size, this is wrong. The most simple solution would be to use the unproject method from the viewport that you are using:
viewport.unproject(touchPosition);
This will call the camera unproject method with the correct parameters automatically.
Since you are using Stage and InputListener, the coordinates you get in touchDown and the related methods are already in world coordinates, so it doesn't make sense to unproject them. You can use the x and y directly.
Also (although this is irrelevant to InputListener), camera.unproject assumes a Viewport that fills the screen, which is not true of FitViewport, which you're using. If you are using a Viewport class, you need to use viewport.unproject instead of camera.unproject, so it takes the black bars into account.
But you only need to worry about unprojecting for stuff not related to the Stage.

LibGDX stage coordinates change on window resize

I have seen lots of topics on LibGDX and screen/cam coordinates and also some on window resizing, but I just can't find the solution to the following problem I have.
When making a basic stage and a basic actor in this stage, say windowsize 480x320, everything is OK. I can click my actor and it will respond. But when I resize my window, say to 600x320, everything looks right, but my clicklistener is not working anymore. Also, the stage coordinates are moved or messed up.
I use the following code:
stage.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
//determine if actor was hit
System.out.println(stage.hit(x, y, true));
return true;
}
});
Also, I am resizing my stage camera viewport to correspond to the window:
stage.getCamera().viewportWidth = Gdx.graphics.getWidth();
stage.getCamera().viewportHeight = Gdx.graphics.getHeight();
So when resizing, I get the desired effect on screen, but my listener does not respond - the actor seems 'offset' of where I am clicking. What am I doing wrong? Should I move my actor or my cam, or zoom my cam according to the resize? Can someone please explain this to me?
Thanks a lot in advance!
EDIT: below is the complete code of my class.
public class HelpMePlease implements ApplicationListener{
// A standard simple Actor Class
class CustomActor extends Actor {
Texture texture = new Texture(Gdx.files.internal("data/testTex2.png"));
TextureRegion pixelTexture = new TextureRegion(texture, 0, 0, 1, 1);
Sprite sprite = new Sprite(texture);
public CustomActor() {
setWidth(128);
setHeight(128);
setBounds(getX(), getY(), getWidth(), getHeight());
}
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
batch.draw(sprite, getX(), getY(), 0f, 0f, getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
}
}
public Stage stage;
public CustomActor actor;
#Override
public void create() {
stage = new Stage(480,320,true);
actor = new CustomActor();
stage.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
//determine if actor was hit
System.out.println(stage.hit(x, y, true));
return true;
}
});
Gdx.input.setInputProcessor(stage);
stage.addActor(actor);
}
#Override
public void resize(int width, int height) {
//resize cam viewport
stage.getCamera().viewportWidth = Gdx.graphics.getWidth();
stage.getCamera().viewportHeight = Gdx.graphics.getHeight();
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
stage.getCamera().update(); //just to be sure, I don't know if this is necessary
stage.act();
stage.draw();
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
}
You can change your resize por this using the latest nightly.
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
the last parameter "true" will center the camera in the screen
I think your actor is positioning itself good enough, but your display may be a bit off.
Try
batch.draw(sprite, getX(), getY(), 0f, 0f, getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
Instead of
batch.draw(sprite, getX(), getY(), getWidth(), getHeight(), 0f, 0f, getScaleX(), getScaleY(), getRotation());
Spritebatch has the following arguments:
public void draw (Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth,int srcHeight, boolean flipX, boolean flipY)
I guess you just mixed some arguments up by mistake, would you kindly take a look at it?
Problem solved by updating my LibGDX Version using Gradle and using the new Viewport options!
Thanks for taking the time everyone!
In my case adding
stage.getViewport().setScreenSize(width, height);
in resize() solved problem

Why are my screenshots only black?

could someone tell me why my screenshots are only black? I am still learning and couldnt find a clue why they are only black.
This is my Utility class
static class XNAUtilities
{
private static RenderTarget2D ssTexture;
private static KeyboardState currentState, previousState;
private static int counter;
public static void TakeScreenShot(GraphicsDevice device, Keys theKey)
{
// Take Screenshot
currentState = Keyboard.GetState();
if (currentState.IsKeyDown(theKey) && previousState.IsKeyUp(theKey))
{
//device.SetRenderTarget(null);
PresentationParameters pparameters = device.PresentationParameters;
ssTexture = new RenderTarget2D(device, pparameters.BackBufferWidth, pparameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None); //??
FileStream fileStream = new System.IO.FileStream(#"Screenshot" + "_" + counter + ".png", System.IO.FileMode.CreateNew);
ssTexture.SaveAsPng(fileStream, pparameters.BackBufferWidth, pparameters.BackBufferHeight);
counter++;
}
previousState = currentState;
}
}
}
This is my Update and Draw from Game1.cs
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
myModelRotation += MathHelper.ToRadians(1f);
// Take a Screenshot
XNAUtilities.TakeScreenShot(GraphicsDevice, Keys.F8);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Matrix[] transforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in myModel.Meshes)
{
foreach (BasicEffect effects in mesh.Effects)
{
effects.EnableDefaultLighting();
effects.World = transforms[mesh.ParentBone.Index]
* Matrix.CreateRotationY(myModelRotation)
* Matrix.CreateTranslation(myModelPosition);
effects.View = Matrix.CreateLookAt(new Vector3(200, 100, 400), Vector3.Zero, Vector3.Up);
effects.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f),
GraphicsDevice.Viewport.AspectRatio, 1, 5000);
}
mesh.Draw();
}
smileySprite.DrawSprites(GraphicsDevice, spriteBatch, new Vector2(10,10), Color.White);
base.Draw(gameTime);
}
You're not actually rendering to your render target. So you're saving the blank target.
You need to wrap your scene drawing like so:
GraphicsDevice.SetRenderTarget(ssTexture);
// Render your scene here
GraphicsDevice.SetRenderTarget(null);
// Now you can save your render target as a texture