0
\$\begingroup\$

I am using the LibGDX framework. I'm not using Scene2D but I want to implement my own "scrolling UI". This is only what I could think of (see image below):

two cameras

I will make the 2nd camera look at very far coordinates (and draw the UI stuff there) and have it (within the blue box) receive its own input. But I think this is not possible with LibGDX, since if you set the size of the camera you are like setting how big it views the world and not how big the viewport is. I mean you can't set the size of the camera of something like 50px width and height and have it place on the center. If you setPosition, you are setting it to where it looks at and not where it is placed in the 2D space. I'm trying to explain it clearly and I hope it is clear.

\$\endgroup\$
0
\$\begingroup\$

Since you are using 2 cameras for each separate area you can split the input for each area using an input multiplexer. Now you can have one input processor for the main camera and one for the UI. Then its simply a case of responding to mouse drag events in the UI processor and if the event is not in the UI area you return false in the input processor method to signify that this processor did not process the event and it needs to be passed to the next input processor(main camera input processor) in the multiplexer chain.

Example

import com.badlogic.gdx.InputProcessor;


public class UIInputProcessor implements InputProcessor{



    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        if(screenX >= 50 && screenX <= 100
                && screenY >= 50 && screenY <= 150 ){
            // I assume the sreen UI window is 50px x 100px with its lower left corner at 50,50

            //TODO respond to UI events

            return true; // this event has been processed so respond true so events do not get passed to next input processor
        }

        return false;
    }

    //.. some events removed for clarity

}

Main Input Processor

import com.badlogic.gdx.InputProcessor;


public class MainInputProcessor implements InputProcessor{

    @Override
    public boolean keyDown(int keycode) {
        //  input events for main area
        return false;
    }

    //.. some events removed for clarity

    @Override
    public boolean scrolled(int amount) {
    //  input events for main area
        return false;
    }

}

Game/Screen Class

Using 2 normal InputProcessors

  MyProcessor = new InputMultiplexer(new UIInputMuliplexer(), new MainInputProcessor());

Or if you use scene2d stage

MyProcessor = new InputMultiplexer(MyStage, new MainInputProcessor());

Camera Now you need to make 2 cameras. One for the main area and one for UI

This example is adapted from How to implement 2 cameras in a unique stage using LibGDX

mainCamera = new OrthographicCamera(GAME_WIDTH,GAME_HEIGHT);
mainCamera.position.set(GAME_WIDTH /2, GAME_HEIGHT /2, 0);
mainCamera.update();

secondCamera = new OrthographicCamera(UI_WIDTH,UI_HEIGHT);
secondCamera.position.set(UI_WIDTH /2,UI_HEIGHT /2, 0);
secondCamera.update();

In your render method you would add

//For the main cam
Gdx.gl.glViewport(0,0,GAME_WIDTH, GAME_HEIGHT); // set viewport to full screen
batch.setProjectionMatrix(mainCamera.combined);
batch.begin();
    // draw main screen items
batch.end();

//For the UI cam
Gdx.gl.glViewport(100,100,300, 400); // set smaller region for UI
batch.setProjectionMatrix(secondCamera.combined);
batch.begin();
    // draw UI items
batch.end();

The important bit being the glViewport which sets which part of the screen to draw to.

\$\endgroup\$
  • \$\begingroup\$ Thanks but my problem is with cameras. How can I have two cameras? I mean I can't set the size of the camera to something like 100px by 100px and have it placed on the middle of the screen. The camera seems to always take the full size of the screen and by setting its size you are actually setting how big it looks at and not how big is the viewport, \$\endgroup\$ – Zik Sep 27 '17 at 7:40
  • \$\begingroup\$ I have updated the answer with some split screen code \$\endgroup\$ – dfour Sep 27 '17 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.