LIBGDX game engine's ScrollPane component

Source: Internet
Author: User

1. Operating interface



This is a horizontal scrollpanel that does not show the scrollbar, and moves horizontally by gesture control Scrollpanel

2. Sample Code

Import Com.badlogic.gdx.graphics.color;import Com.badlogic.gdx.scenes.scene2d.actor;import Com.badlogic.gdx.scenes.scene2d.inputevent;import Com.badlogic.gdx.scenes.scene2d.inputlistener;import Com.badlogic.gdx.scenes.scene2d.touchable;import Com.badlogic.gdx.scenes.scene2d.ui.image;import Com.badlogic.gdx.scenes.scene2d.ui.imagebutton;import Com.badlogic.gdx.scenes.scene2d.ui.scrollpane;import Com.badlogic.gdx.scenes.scene2d.ui.skin;import Com.badlogic.gdx.scenes.scene2d.ui.table;import Com.badlogic.gdx.scenes.scene2d.utils.clicklistener;import Com.badlogic.gdx.scenes.scene2d.utils.textureregiondrawable;import com.swallowgames.supermario.game.Assets; Import Com.swallowgames.supermario.screen.mainscreen;import Com.swallowgames.supermario.utils.utils;public class Themescrollpanel extends Scrollpane{private buttonclicklistener clicklistener;private mainscreen mainScreen;public Themescrollpanel (Actor widget, skin skin) {Super (Widget, skin);} Public Themescrollpanel (Actor widget, ScrollPaneStyle style) {super (widget, style);} Public Themescrollpanel init () {Clicklistener = new Buttonclicklistener (); Table table = new table (); Table.debug (); ImageButton themebackground = new ImageButton (New Textureregiondrawable ( ASSETS.INSTANCE.ASSETUI.THEME1), New Textureregiondrawable (Assets.instance.assetUI.theme1p)); Themebackground.setuserobject (0); Themebackground.addcapturelistener (Clicklistener); Table.add (ThemeBackground); Table.columndefaults (0); themebackground = new ImageButton (New Textureregiondrawable (Assets.instance.assetUI.theme2 ), New Textureregiondrawable (Assets.instance.assetUI.theme2p)); Themebackground.setuserobject (1); Themebackground.settouchable (touchable.disabled); Themebackground.addcapturelistener (ClickListener); Image lock = New Image (New Textureregiondrawable (Assets.instance.assetUI.lock)); Lock.setposition ((Themebackground.getwidth ()- Lock.getwidth ())/2, (Themebackground.getheight ()-lock.getheight ())/2); Themebackground.addactor (lock); Table.add ( Themebackground). Width (themebAckground.getwidth ()). Height (themebackground.getheight ()); Table.columndefaults (1); themebackground = new ImageButton (New Textureregiondrawable (Assets.instance.assetUI.theme3), New Textureregiondrawable ( Assets.instance.assetUI.theme3p)); Themebackground.setuserobject (2); Themebackground.settouchable ( touchable.disabled); Themebackground.addcapturelistener (clicklistener); lock = new Image (New Textureregiondrawable ( Assets.instance.assetUI.lock)); Lock.setposition ((Themebackground.getwidth ()-lock.getwidth ())/2, ( Themebackground.getheight ()-lock.getheight ())/2); Themebackground.addactor (lock); Table.add (Themebackground). Width (themebackground.getwidth ()). Height (themebackground.getheight ()); Table.columndefaults (2); themebackground = new ImageButton (New Textureregiondrawable (Assets.instance.assetUI.theme4 ), New Textureregiondrawable (Assets.instance.assetUI.theme4p)); Themebackground.setuserobject (3); Themebackground.setcolor (Color.gray); themebackground.settouchable (touchable.disabled); themeBackground.aDdcapturelistener (clicklistener); lock = new Image (new Textureregiondrawable (Assets.instance.assetUI.lock)); Lock.setposition ((Themebackground.getwidth ()-lock.getwidth ())/2, (Themebackground.getheight ()-lock.getHeight ()) /2); Themebackground.addactor (lock); Lock.setcolor (Color.gray); Table.add (themebackground). Width ( Themebackground.getwidth ()). Height (themebackground.getheight ()); Table.columndefaults (3); Table.pack (); setwidget (table); setscrollingdisabled (false, True); AddListener (new Inputlistener () {public boolean TouchDown (InputEvent event, float x, float y, int pointer, int button) {event.stop (); retur n True;}}); Setsmoothscrolling (True); Setflickscroll (false); SetSize (Themebackground.getwidth () * *, table.getheight ()); SetPosition (Utils.xaxiscenter (GetWidth ()), Utils.yaxiscenter (GetHeight ()), return this; public void Setmainscreen (Mainscreen mainscreen) {this.mainscreen = Mainscreen;} Class Buttonclicklistener extends Clicklistener {@Overridepublic void TouchUp (InputEvent event, float x,float y, int pointer, int button) {Super.touchup (event, x, y, pointer, button); Actor actor = Event.getlisteneractor (); Integer themeindex = (integer) actor.getuserobject (); Mainscreen.addlevelwindow (Themeindex);}}}
3. Gesture Monitoring

Import Com.badlogic.gdx.input.gesturedetector.gestureadapter;public class Mainscreengesturelistener extends Gestureadapter{private Levelwindow levelwindow;private themescrollpanel themescrollpanel;float panx = 0f;float pany = 0f ; float Pandeltax = 0f;float Pandeltay = 0f; @Overridepublic boolean pan (float x, float y, float deltax, float deltay) {if (l Evelwindow! = null && levelwindow.getcolor (). A! = 0f) {This.panx = X;this.pany = Y;this.pandeltax = Deltax;this.pan DeltaY = Deltay;return true;} else if (themescrollpanel! = null && themescrollpanel.isvisible ()) {THEMESCROLLPANEL.SETSCROLLX ( THEMESCROLLPANEL.GETSCROLLX ()-deltax); return true;} Else{return false;}} public void Setlevelwindow (Levelwindow levelwindow) {This.levelwindow = Levelwindow;} public void Setthemescrollpanel (Themescrollpanel themescrollpanel) {this.themescrollpanel = Themescrollpanel;} @Overridepublic boolean panstop (float x, float y, int pointer, int button) {if (Levelwindow! = null && Levelwindow. IsVisible()) {if (Panx = = x && pany ==y && pandeltax < 0f) {//Swipe left levelwindow.nextpage ();} if (Panx = = x && pany ==y && pandeltax > 0f) {//Swipe right to Levelwindow.prevpage ();} return true;} Else{return false;}}}
4. Code description

The content inside the ScrollPane is a few pictures, using a table component layout.

A few lines of code to note:

Control bit Horizontal Slide

Setscrollingdisabled (False, True);

It needs to be added, and I don't know if it's dry.

AddListener (New Inputlistener () {
public boolean TouchDown (InputEvent event, float x, float y, int pointer, int button) {
Event.stop ();
return true;
}
});

Look at the method name and you know what it is.
Setsmoothscrolling (TRUE);

Do not show your own scroll bar
Setflickscroll (FALSE);

Gesture listener, not clear can go to <a href= "www.baidu.com" > Baidu </a>

@Override
Public boolean pan (float x, float y, Float deltax, float deltay) {
if (Levelwindow! = null && Levelwindow.getcolor (). A! = 0f) {
This.panx = x;
This.pany = y;
this.pandeltax = DeltaX;
This.pandeltay = DeltaY;
return true;
}else if (themescrollpanel! = null && themescrollpanel.isvisible ()) {
themescrollpanel.setscrollx (THEMESCROLLPANEL.GETSCROLLX ()-deltaX);
return true;
}else{
return false;
}
}

This piece of code, where Themescrollpanel.setscrollx (THEMESCROLLPANEL.GETSCROLLX ()-deltax); is to move the scrolling component through gestures,

DeltaX is the speed of movement.

LIBGDX game engine's ScrollPane component

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.