summaryrefslogtreecommitdiff
path: root/core/src/kz/ilotterytea/maxon/ui/ShakingImageButton.java
blob: 2a28586f7279711ed6aa9b10e29a784a506c97a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package kz.ilotterytea.maxon.ui;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.RepeatAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class ShakingImageButton extends Image {
    public ShakingImageButton(Skin skin, String style) {
        super(skin.getRegion(style));

        this.setOrigin(getWidth() / 2f, getHeight() / 2f);

        this.addListener(new ClickListener() {
            @Override
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                super.enter(event, x, y, pointer, fromActor);

                addAction(
                        Actions.repeat(
                                RepeatAction.FOREVER,
                                Actions.sequence(
                                        Actions.rotateTo(-2f, 0.1f),
                                        Actions.rotateTo(2f, 0.1f)
                                )
                        )
                );
            }

            @Override
            public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
                super.exit(event, x, y, pointer, toActor);

                clearActions();
                addAction(Actions.rotateTo(0f, 0.1f));
            }
        });
    }
}