Package org.iremake.client.ui.main

Source Code of org.iremake.client.ui.main.MainScreen

/*
* Copyright (C) 2012 Trilarion
*
* 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 org.iremake.client.ui.main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
import nu.xom.ParsingException;
import org.iremake.client.ui.Button;
import org.iremake.client.ui.FrameCloseListener;
import org.iremake.client.ui.FrameManager;
import org.iremake.client.ui.StartScreen;
import org.iremake.client.ui.UIFrame;
import org.iremake.client.ui.game.GameDialogBuilder;
import org.iremake.client.ui.game.GamePanel;
import org.iremake.client.ui.map.MainMapPanel;
import org.iremake.client.ui.map.MiniMapPanel;
import org.iremake.client.ui.model.UIScenario;
import org.iremake.common.model.ScenarioChangedListener;
import org.iremake.common.model.map.MapPosition;
import org.tools.io.Resource;
import org.tools.ui.ButtonBar;
import org.tools.ui.ClockLabel;
import org.tools.xml.XMLHelper;

/**
* Main game screen.
*/
public class MainScreen extends UIFrame {

    private static final Logger LOG = Logger.getLogger(MainScreen.class.getName());

    /* client ui scenario */
    private UIScenario scenario = new UIScenario();
    private MainMapPanel mainMapPanel;
    private MiniMapPanel miniMapPanel;

    /**
     * Setup of the whole screen.
     */
    public MainScreen() {
        JPanel panel = new JPanel();

        // Add MapPanel
        mainMapPanel = new MainMapPanel(scenario);

        panel.setLayout(new MigLayout("fill", "[grow][]"));
        panel.add(mainMapPanel, "grow");
        panel.add(createControlPanel(), "growy, wmin 300");

        scenario.addScenarioChangedListener(new ScenarioChangedListener() {
            @Override
            public void tileChanged(MapPosition p) {
                miniMapPanel.tileChanged();
                mainMapPanel.tileChanged(p);
            }

            @Override
            public void scenarioChanged() {
                // TODO size of map could also have changed!!!
                mainMapPanel.mapChanged();

                Dimension size = mainMapPanel.getSize();
                Dimension tileSize = scenario.getTileSize();
                // tell the minimap about our size
                float fractionRows = (float) size.height / tileSize.height / scenario.getNumberRows();
                float fractionColumns = (float) size.width / tileSize.width / scenario.getNumberColumns();
                miniMapPanel.mapChanged(fractionRows, fractionColumns);
            }
        });

        setContent(panel);

        FrameManager.getInstance().setClosingListener(new FrameCloseListener() {
            @Override
            public void close() {
                UIFrame frame = new StartScreen();
                frame.switchTo();
            }
        });
    }

    /**
     * Control panel which is everything apart from the main map.
     *
     * @return a component
     */
    private JComponent createControlPanel() {
        JPanel panel = new JPanel();

        JPanel infPanel = new JPanel();
        infPanel.setBorder(BorderFactory.createLineBorder(Color.black, 1));

        // exit button
        JButton exitButton = Button.NormalExit.create();
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UIFrame frame = new StartScreen();
                frame.switchTo();
            }
        });

        // save button
        JButton saveButton = Button.ScenarioSave.create();

        // add buttons to toolbar
        ButtonBar ubar = new ButtonBar();
        ubar.add(saveButton, exitButton);

        // create mini map and add to panel
        miniMapPanel = new MiniMapPanel(scenario);
        miniMapPanel.setFocusChangedListener(mainMapPanel);


        ButtonBar lbar = new ButtonBar();
        for (final GamePanel p : GamePanel.values()) {
            JButton button = p.getButton();
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO only if not active yet, otherwise just change
                    GameDialogBuilder.build(p);
                }
            });
            lbar.add(button);
        }

        // create info panel
        JPanel infoPanel = new JPanel();
        infoPanel.setBorder(BorderFactory.createLineBorder(Color.black, 1));

        JButton turnButton = new JButton("End Turn");
        turnButton.setFocusable(false);
        turnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });

        // layout
        panel.setLayout(new MigLayout("wrap 1, fill", "", "[][][][][grow][][]"));
        panel.add(infPanel, "growx");
        panel.add(ubar.get());
        panel.add(miniMapPanel, "growx"); // because the extra space goes here
        panel.add(lbar.get());
        panel.add(infoPanel, "grow");
        panel.add(turnButton, "growx, alignx center");
        panel.add(new ClockLabel(), "alignx center");

        return panel;
    }

    /**
     * On start the scenario is loaded.
     *
     * @param resource Resource pointing to the scenario.
     */
    public void switchTo(Resource resource) {
        super.switchTo();
        try {
            // load given scenario
            XMLHelper.read(resource, scenario);
        } catch (IOException | ParsingException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }
}
TOP

Related Classes of org.iremake.client.ui.main.MainScreen

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.