Scrollable html list in libgdx - list

I want to make achievements table in my libgdx game. Is there any component in libgdx wchih i can use? For example something that allows me to turn screen into html page or something like this.

simple test for you start:
Variable Class:
Stage stage;
ScrollPane scrollPane;
Table outerTable, innerTable;
.
#Override
public void show() {
stage = new Stage();
outerTable = new Table();
innerTable = new Table();
image = new Image(new Texture(
Gdx.files.internal("badlogic.jpg")));
//innerTable.add(YourActor); for example Image, or TextButton
innerTable.add(image);
scrollPane = new ScrollPane(innerTable);
outerTable.setPosition(0, 0);
outerTable.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
outerTable.debug();
outerTable.add(scrollPane).fill().expand();
stage.addActor(outerTable);
}
#Override
public void render(float delta) {
Gdx.gl20.glClearColor(0, 0, 0, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
look this: https://github.com/libgdx/libgdx/wiki/Table
I hope I have understood correctly, and that this is what you want.

Related

How to get current visible portion of QGraphicsView in Qt?

I have QGraphicsView, which has multiple QGraphicsItem's. On this view, I am applying some transformation using features like Zoom-in, zoom-out, fit -In etc.
So before applying fit-in feature on my current view, I want to store view's current transformation ( view's current situation ) in a variable. And then I want to apply Fit-in feature. ( so that, in Undo-Redo I can use that stored position)
But I do not know how to store current position of view in a variable. I tried this way :
void myClass::FitIn()
{
QTransform t = _view->transform();
QScrollBar* hrSBar = _view->horizontalScrollBar();
QScrollBar* verSBar = _view->verticalScrollBar();
myCommand* c = new myCommand(t,hrSBar,verSBar);
undoStack->push(c);
_view->resetTransform();
}
How to store, current view in another variable ?
***** EDIT ******
myCommand.cpp
myCommand::myCommand(QTransform t, QScrollBar *hr, QScrollBar *vr)
{
this->trans = t;
this->horizontalSBar = hr;
this->verticalSBar = vr;
}
void myCommand::undo()
{
_mView->setTransform(trans);
_mView->horizontalScrollBar()->setValue(horizontalSBar->maximum());
_mView->verticalScrollBar()->setValue(verticalSBar->maximum());
}
void myCommand::redo()
{
_mView->setTransform(trans); // Segmentation Fault occurs
_mView->horizontalScrollBar()->setValue(horizontalSBar->maximum());
_mView->verticalScrollBar()->setValue(verticalSBar->maximum());
}
myCommand.h
class myCommand: public QUndoCommand
{
public:
myCommand(QTransform t, QScrollBar* hr, QScrollBar* vr);
private:
QGraphicsScene* _mScene;
QGraphicsView* _mView;
}
As revealed by source code of mapToScene, three parameters define the part of the scene that is visible on the viewport:
QGraphicsView::transform()
QAbstractScrollArea::horizontalScrollBar()->value()
QAbstractScrollArea::verticalScrollBar()->value()
To implement an undo-redo framework, those three parameters should be stored before every operation an restored upon undo.

How to test what material is applied to an object in Unity

I have a diamond-sprite and I want to be able to change the colour of the diamond from white, it's original colour to green. However, I can not figure out how to do this.
public class MoveControl : MonoBehaviour {
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.A) )
{
if (GetComponent<Renderer>().material.color == Color.white)
{
GetComponent<Renderer>().material.color = Color.green;
}
}
}
}
This above code is what I have right now and it only works if the material applied to the sprite, being white, is a sprites/default shader. This may not sound like a big problem but whenever I apply a different material of a different colour, such as blue, and change its settings so it has a sprites/default shader, the sprite becomes invisible.
I'm new at Unity and if someone could help me out, it would be very much appreciated
I'm not sure what you're trying to do but this may help you.
public class Material : MonoBehaviour {
Renderer renderer;
Color currentColor;
Color originalColor;
// Use this for initialization
void Start () {
renderer = gameObject.GetComponent<Renderer>(); //If you don't know the gameObject is the object this script is attached to
originalColor = renderer.material.color; //The original color ( in your case white )
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.A)) {
if (renderer.material.color == originalColor) { //Here we are checking if the color on the object is the original color
renderer.material.color = Color.blue;
currentColor = renderer.material.color; //Here we are assining the current color
}
}
}
}
I created a material and assigned it to the gameObject in the editor.

Can't turn light off in libgdx

Use the code below, if I turn the light off, the blue box will be black.
But there seems no effect on the entity, it is still colorful. What's wrong with the code? Please help thanks.
package com.louxiu.game;
/**
* Created by louxiu on 2/22/16.
*/
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.*;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.graphics.g3d.loader.ObjLoader;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.math.Vector3;
public class TestApp implements ApplicationListener {
private PerspectiveCamera camera;
private ModelBatch modelBatch;
private Model box;
private ModelInstance boxInstance;
public Model entityModel;
public ModelInstance entityInstance;
private Environment environment;
#Override
public void create() {
// Create camera sized to screens width/height with Field of View of 75 degrees
camera = new PerspectiveCamera(
75,
Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
// Move the camera 3 units back along the z-axis and look at the origin
camera.position.set(0f, 0f, 3f);
camera.lookAt(0f, 0f, 0f);
// Near and Far (plane) repesent the minimum and maximum ranges of the camera in, um, units
camera.near = 0.1f;
camera.far = 300.0f;
// A ModelBatch is like a SpriteBatch, just for models. Use it to batch up geometry for OpenGL
modelBatch = new ModelBatch();
// A ModelBuilder can be used to build meshes by hand
ModelBuilder modelBuilder = new ModelBuilder();
// It also has the handy ability to make certain premade shapes, like a Cube
// We pass in a ColorAttribute, making our cubes diffuse ( aka, color ) red.
// And let openGL know we are interested in the Position and Normal channels
box = modelBuilder.createBox(2f, 2f, 2f,
new Material(ColorAttribute.createDiffuse(Color.BLUE)),
Usage.Position | Usage.Normal
);
// A entityModel holds all of the information about an, um, entityModel, such as vertex data and texture info
// However, you need an entityInstance to actually render it. The entityInstance contains all the
// positioning information ( and more ). Remember Model==heavy ModelInstance==Light
boxInstance = new ModelInstance(box, 0, 0, 0);
String entity = "creeper/creeper";
ObjLoader loader = new ObjLoader();
entityModel = loader.loadModel(Gdx.files.internal(entity + ".obj"), new ObjLoader.ObjLoaderParameters(true));
entityInstance = new ModelInstance(entityModel, 0, 0, 0);
Texture texture = new Texture(Gdx.files.internal(entity + ".png"));
entityInstance.materials.get(0).set(TextureAttribute.createDiffuse(texture));
// Finally we want some light, or we wont see our color. The environment gets passed in during
// the rendering process. Create one, then create an Ambient ( non-positioned, non-directional ) light.
environment = new Environment();
// environment.add(new DirectionalLight().set(1f, 1f, 1f, -1f, -0.8f, -0.2f));
// environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.8f, 0.8f, 0.8f, 1.0f));
}
#Override
public void dispose() {
modelBatch.dispose();
box.dispose();
}
#Override
public void render() {
// You've seen all this before, just be sure to clear the GL_DEPTH_BUFFER_BIT when working in 3D
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT);
// For some flavor, lets spin our camera around the Y axis by 1 degree each time render is called
camera.rotateAround(Vector3.Zero, new Vector3(0, 1, 0), 1f);
// When you change the camera details, you need to call update();
// Also note, you need to call update() at least once.
camera.update();
// Like spriteBatch, just with models! pass in the box Instance and the environment
modelBatch.begin(camera);
modelBatch.render(boxInstance, environment);
// modelBatch.render(entityInstance, environment);
modelBatch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
You wavefront model probably does not have normals, which is required for lighting to work.
Check the log, there should be an error message showing you that you should not use ObjLoader altogether. Instead use G3dModelLoader or even better: use AssetManager and the g3dj or g3db file format.
Export your model from your modeling application into e.g. the FBX file format and convert it using fbx-conv. Do not convert your .obj file into a .g3dx file using fbx-conv, that won't work.
Btw, although not related you might want to take into consideration that:
your camera far/near ratio is very high, you usually should never use a near value below 1.
Unlike what your comment says, ModelBatch is not used to batch geometry and not that comparable to SpriteBatch.
ObjLoader has a loadModel method which accepts a boolean, so you don't have to create ObjLoaderParameters for that (although, as said, you shouldnt be using ObjLoader altogether).
You are creating a Texture without properly disposing it when no longer needed. This will cause a potential resource leak.
Creating a new Vector3 every frame is going to pressure the GC and will cause hick-ups. Simply use Vector3.Y instead of new Vector3(0, 1, 0) to fix that.

How to work with local coordinates in libgdx custom group?

I am creating a custom libgdx group to show a card image. It contains labels and images like these.
My first image is a texture region which is the actual size of the group. I now need to add the second image to the top left using local coordinate and a label for the name of the suit. This is the card I am using.
public class Card extends Group {
private Player player;
private AtlasRegion cardImage;
private Label lblCard;
public Card() {
super();
init();
}
#Override
public void draw(Batch batch, float alpha){
super.draw(batch, alpha);
batch.draw(cardImage, getX(), getY());
//lblName.draw()
lblCard.draw(batch, getOriginX() + 70..values I have used for this arent working so far);
}
#Override
public void act(float delta){
}
#Override
public void setSize(float width, float height) {
super.setSize(width, height);
}
private void init() {
cardImage = Assets.instance.cards.hearts;
// Add player name
lblName = new Label("H", Assets.instance.menu, "knul-medium", Color.BLACK);
}
}
I have tried using getX() and getY() values but when I draw the group inside a table, only the cardImage fits onto the table whereas the labels all draw at one point in the screen. How can I draw the labels and other texture regions inside my Card using local coordinates and appear at same appear as one image whenever I create a new instance of Card in another class?

What's wrong with this simple OpenGL/JOGL stencil test?

I'm learning how to use a stencil buffer, but so far have been unsuccessful at getting a even a simple example to work. In fact, despite trying various combinations of parameters for glStencilOp and glStencilFunc I have not been able to see any evidence that the stencil buffer is working at all. I'm starting to suspect my graphics driver (Mac Pro, Mac OS X 10.8.5) or JOGL (2.0.2) doesn't support it... or I'm missing something really basic.
Here's what I'm seeing:
I'm expecting to see the red diamond clipped by the green diamond. What am I doing wrong?
public class Test {
public static void main(String[] args) {
GLProfile glprofile = GLProfile.getDefault();
final GLCapabilities glcapabilities = new GLCapabilities(glprofile);
final GLCanvas glcanvas = new GLCanvas(glcapabilities);
final GLU glu = new GLU();
glcanvas.addGLEventListener(new GLEventListener() {
#Override
public void reshape(GLAutoDrawable glautodrawable, int x, int y, int width, int height) {}
#Override
public void init(GLAutoDrawable glautodrawable) {
GL2 gl = glautodrawable.getGL().getGL2();
glcapabilities.setStencilBits(8);
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45, 1, 1, 10000);
glu.gluLookAt(0, 0, 100, 0, 0, 0, 0, 1, 0);
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
}
#Override
public void dispose(GLAutoDrawable glautodrawable) {}
#Override
public void display(GLAutoDrawable glautodrawable) {
GL2 gl = glautodrawable.getGL().getGL2();
gl.glEnable(GL.GL_STENCIL_TEST);
gl.glClearStencil(0x0);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT);
gl.glStencilFunc(GL.GL_ALWAYS, 1, 1);
gl.glStencilOp(GL.GL_REPLACE, GL.GL_REPLACE, GL.GL_REPLACE);
gl.glStencilMask(0xFF);
//gl.glColorMask(false, false, false, false);
//gl.glDepthMask(false);
gl.glColor3f(0, 1, 0);
gl.glBegin(GL2.GL_QUADS);
gl.glVertex2f(-25.0f, 0.0f);
gl.glVertex2f(0.0f, 15.0f);
gl.glVertex2f(25.0f, 0.0f);
gl.glVertex2f(0.0f, -15.0f);
gl.glEnd();
gl.glStencilMask(0);
gl.glStencilFunc(GL2.GL_EQUAL, 1, 1);
gl.glStencilOp(GL2.GL_KEEP, GL2.GL_KEEP, GL2.GL_KEEP);
//gl.glColorMask(true, true, true, true);
//gl.glDepthMask(true);
gl.glColor3f(1, 0, 0);
gl.glBegin(GL2.GL_QUADS);
gl.glVertex2f(-20.0f, 0.0f);
gl.glVertex2f(0.0f, 20.0f);
gl.glVertex2f(20.0f, 0.0f);
gl.glVertex2f(0.0f, -20.0f);
gl.glEnd();
}
});
final JFrame jframe = new JFrame("One Triangle Swing GLCanvas");
jframe.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent windowevent) {
jframe.dispose();
System.exit(0);
}
});
jframe.getContentPane().add(glcanvas, BorderLayout.CENTER);
jframe.setSize(640, 480);
jframe.setVisible(true);
}
}
Zero298 has the right idea, though fails to explain why what you tried in your code does not work. This becomes more apparent when you understand how framebuffer pixel formats work in OpenGL; I will touch on this a little bit below, but first just to re-hash the proper solution:
public static void main(String[] args) {
GLProfile glprofile = GLProfile.getDefault ();
GLCapabilities glcapabilities = new GLCapabilities (glprofile);
// You must do this _BEFORE_ creating a render context
glcapabilities.setStencilBits (8);
final GLCanvas glcanvas = new GLCanvas (glcapabilities);
final GLU glu = new GLU ();
The important thing is that you do this before creating your render context ("canvas"). The stencil buffer is not something you can enable or disable whenever you need it -- you first have to select a pixel format that reserves storage for it. Since pixel formats are fixed from the time you create your render context onward, you need to do this before new GLCanvas (...).
You can actually use an FBO to do stencil operations in a render context that does not have a stencil buffer, but this is much more advanced than you should be considering at the moment. Something to consider if you ever want to do MSAA though, FBOs are a much nicer way of changing pixel formats at run-time than creating and destroying your render context ("canvas").
You need a call to glStencilMask() it's what controls what gets written or not. Set it to do or don't write, draw a stencil (in your case, the diamond), set the glStencilMask() again, and then draw what you want to get clipped.
This has a good sample: Stencil Buffer explanation
EDIT:
OK, I think I found the problem. You need to set your capabilities up at the top of the program.
final GLCapabilities glcapabilities = new GLCapabilities(glprofile);
glcapabilities.setStencilBits(8);
final GLCanvas glcanvas = new GLCanvas(glcapabilities);
The important part being:
glcapabilities.setStencilBits(8);
Thanks to: enabling stencil in jogl