001package squidpony.squidgrid.gui.gdx;
002
003import com.badlogic.gdx.ApplicationAdapter;
004import com.badlogic.gdx.Gdx;
005import com.badlogic.gdx.graphics.Color;
006import squidpony.SquidTags;
007
008/**
009 * A partial application adapter that uses the Zodiac-Square fonts. It should be
010 * completed as follows: the {@link #create()} method should assign
011 * {@link #screen}. Then, you should implement
012 * {@link AbstractSquidScreen#getNext()}; and you'll be done.
013 * 
014 * @author smelC
015 */
016public class SquidApplicationAdapter extends ApplicationAdapter {
017
018        protected final IPanelBuilder ipb;
019
020        /** Should be assigned in {@link #create()} */
021        protected /* @Nullable */ AbstractSquidScreen<Color> screen;
022
023        /**
024         *            An {@link IPanelBuilder} that specifies which font sizes are
025         *            available. Use {@link IPanelBuilder.Skeleton} to help build
026         *            this instance.
027         */
028        public SquidApplicationAdapter() {
029                this.ipb = new SquidPanelBuilder(12, 24, 0, DefaultResources.getSCC(), null) {
030                        @Override
031                        protected String fontfile(int sz) {
032                                if (sz == 12)
033                                        return "Zodiac-Square-12x12.fnt";
034                                else if (sz == 24)
035                                        return "Zodiac-Square-24x24.fnt";
036                                else
037                                        throw new IllegalStateException(
038                                                        "Sorry! This panel builder only supports a square font of size 12 or 24");
039                        }
040                };
041        }
042
043        @Override
044        public void render() {
045                if (screen == null) {
046                        /* Weird */
047                        Gdx.app.log(SquidTags.SCREEN,
048                                        "Unexpected state in " + getClass().getSimpleName() + ". Did create get called ?");
049                        return;
050                }
051
052                if (screen.isDisposed()) {
053                        screen = screen.getNext();
054                        if (screen == null) {
055                                /* Quit */
056                                Gdx.app.exit();
057                                /* This point is unreachable */
058                        }
059                } else if (screen.hasPendingResize())
060                        /* Rebuild a new screen */
061                        screen = screen.getNext();
062                else
063                        /* Normal behavior, forward */
064                        screen.render(Gdx.graphics.getDeltaTime());
065        }
066
067        @Override
068        public void resize(int width, int height) {
069                if (screen == null) {
070                        /* Weird */
071                        Gdx.app.log(SquidTags.SCREEN,
072                                        "Unexpected state in " + getClass().getSimpleName() + ". Did create get called ?");
073                } else
074                        /* forward */
075                        screen.resize(width, height);
076        }
077
078        @Override
079        public void pause() {
080                if (screen != null)
081                        /* forward */
082                        screen.pause();
083        }
084
085        @Override
086        public void resume() {
087                if (screen != null)
088                        /* forward */
089                        screen.pause();
090        }
091
092        @Override
093        public void dispose() {
094                if (screen != null)
095                        /* forward, to clean up */
096                        screen.dispose();
097        }
098
099}