com.rpgsheet.xcom.state.SelectLanguageState.java Source code

Java tutorial

Introduction

Here is the source code for com.rpgsheet.xcom.state.SelectLanguageState.java

Source

/*
 * SelectLanguageState.java
 * Copyright 2012 Patrick Meade
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.rpgsheet.xcom.state;

import com.rpgsheet.xcom.PimpMyXcom;
import com.rpgsheet.xcom.XcomEditor;
import com.rpgsheet.xcom.render.Button;
import com.rpgsheet.xcom.render.Renderable;
import com.rpgsheet.xcom.render.Window;
import com.rpgsheet.xcom.service.UfoResourceService;
import com.rpgsheet.xcom.slick.Font;
import com.rpgsheet.xcom.slick.Palette;
import com.rpgsheet.xcom.type.Language;
import java.io.IOException;
import java.util.Properties;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("selectLanguageState")
public class SelectLanguageState extends BasicGameState {
    /**
     * Generated by Random.Org
     * @see http://www.random.org/cgi-bin/randbyte?nbytes=4&format=h
     */
    public static final int ID = 0x6ba75293;

    @Override
    public int getID() {
        return ID;
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        // nothing to init
    }

    @Override
    public void enter(GameContainer gc, StateBasedGame sbg) {
        Font smallFont = ufoResourceService.getFontSmall();
        Palette imagePalette = ufoResourceService.getPaletteMicro(0);
        Palette mainPalette = ufoResourceService.getPaletteFull(0);
        Image background = ufoResourceService.getBackground(0, imagePalette);

        englishButton = new Button(64, 90, 255, 109, mainPalette, 134, smallFont, 134, "ENGLISH", new Runnable() {
            public void run() {
                xcomEditor.setLanguage(Language.ENGLISH);
                updateLanguageProperty(Language.ENGLISH);
            }
        });
        frenchButton = new Button(64, 146, 255, 165, mainPalette, 134, smallFont, 134, "FRANCAIS", new Runnable() {
            public void run() {
                xcomEditor.setLanguage(Language.FRENCH);
                updateLanguageProperty(Language.FRENCH);
            }
        });
        germanButton = new Button(64, 118, 255, 137, mainPalette, 134, smallFont, 134, "DEUTSCHE", new Runnable() {
            public void run() {
                xcomEditor.setLanguage(Language.GERMAN);
                updateLanguageProperty(Language.GERMAN);
            }
        });

        mainMenuWindow = new Window(32, 20, 287, 179, mainPalette, 134, background);
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        mainMenuWindow.render(gc, g);
        englishButton.render(gc, g);
        germanButton.render(gc, g);
        frenchButton.render(gc, g);
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int timeDelta) throws SlickException {
        // if we already have a selected language, go to the main menu
        if (xcomEditor.getLanguage() != null) {
            sbg.enterState(MainMenuState.ID);
        }
    }

    @Override
    public void mousePressed(int button, int x, int y) {
        englishButton.mousePressed(button, x, y);
        frenchButton.mousePressed(button, x, y);
        germanButton.mousePressed(button, x, y);
    }

    @Override
    public void mouseReleased(int button, int x, int y) {
        englishButton.mouseReleased(button, x, y);
        frenchButton.mouseReleased(button, x, y);
        germanButton.mouseReleased(button, x, y);
    }

    private Button englishButton;
    private Button frenchButton;
    private Button germanButton;

    private Renderable mainMenuWindow;

    @Autowired
    private UfoResourceService ufoResourceService;
    @Autowired
    private XcomEditor xcomEditor;

    private static void updateLanguageProperty(Language language) {
        Properties appProps = PimpMyXcom.loadApplicationProperties();
        appProps.setProperty("xcom.lang", language.name());
        try {
            PimpMyXcom.saveApplicationProperties(appProps);
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }
}