package org.gbcpainter.view.editor;
import net.jcip.annotations.GuardedBy;
import org.gbcpainter.env.GameSettings;
import org.gbcpainter.env.GraphicsEnv;
import org.gbcpainter.env.LanguageDictionary;
import org.gbcpainter.loaders.textures.ResolutionTextureLoader;
import org.gbcpainter.loaders.textures.TextureNotFoundException;
import org.gbcpainter.view.*;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashSet;
import java.util.Set;
/**
* @author Lorenzo Pellegrini
*
* @exclude
*/
public class LevelEditor extends WindowAdapter implements ExitListener,
PCViewHolder {
/* Window data */
private static final double PERCENTAGE_HEADER_HEIGHT = 0.25;
private static final int DEFAULT_WINDOW_WIDTH = 500;
private static final int DEFAULT_WINDOW_HEIGHT = 500;
@NonNls
private static final String WINDOW_TITLE = "Painter Editor";
@NonNls
private static final String HEADER_TEXTURE = "Header";
@NotNull
private final Frame gameView;
@NotNull
private final Component header;
/* Start ViewHolder data */
@NotNull
private final Set<ExitListener> listeners = new HashSet<>( 1 );
@NotNull
private final Panel holder;
@NotNull
private Component currentComponent;
private boolean fullView;
public LevelEditor() {
this.addListener( this );
ViewManager.setMainView( this );
gameView = new Frame( WINDOW_TITLE );
gameView.setSize( DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT );
gameView.setLocationRelativeTo( null );
gameView.setLayout( null );
GraphicsEnv.getInstance().setWindowSize( gameView.getWidth(), gameView.getHeight() );
GraphicsEnv.getInstance().setGraphicsConfiguration( gameView.getGraphicsConfiguration() );
GraphicsEnv.getInstance().setTextureLoader( new ResolutionTextureLoader() );
this.fullView = false;
this.currentComponent = new Label( "Select level" );
try {
GraphicsEnv.getInstance().getTextureLoader().loadTexture( HEADER_TEXTURE, false );
} catch ( Exception e ) {
final TextureNotFoundException textureException = new TextureNotFoundException( e );
new ErrorDialog( this.gameView, LanguageDictionary.ERROR_STRING, LanguageDictionary.ERROR_LOAD_HEADER, textureException, true );
throw textureException;
}
this.header = new Label() {
@Override
public void paint( Graphics g ) {
try {
Image img = GraphicsEnv.getInstance().getTextureLoader().loadTexture( HEADER_TEXTURE, false );/*NON-NLS*/
g.drawImage( img.getScaledInstance( - 1, getHeight(), Image.SCALE_SMOOTH ), 0, 0, getWidth(), getHeight(), null );
} catch ( Exception e ) {
final TextureNotFoundException textureException = new TextureNotFoundException( e );
new ErrorDialog( gameView, LanguageDictionary.ERROR_STRING, LanguageDictionary.ERROR_LOAD_HEADER, textureException, true );
}
}
};
this.holder = new Panel();
this.holder.setLayout( null );
this.holder.add( header );
this.holder.add( this.currentComponent );
gameView.add( this.holder );
gameView.setBackground( Color.BLACK );
gameView.setFont( GraphicsEnv.getInstance().getBigElementsFont() );
gameView.setSize( DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT );
gameView.addWindowListener( this );
gameView.addComponentListener( new ComponentAdapter() {
@Override
public void componentResized( final ComponentEvent e ) {
super.componentResized( e );
final Insets frameInsets = gameView.getInsets();
final Dimension realDimension = gameView.getSize();
final int viewHeight = realDimension.height - ( frameInsets.bottom + frameInsets.top );
final int viewWidth = realDimension.width - ( frameInsets.left + frameInsets.right );
GraphicsEnv.getInstance().setWindowSize( viewWidth, viewHeight );
GraphicsEnv.getInstance().getTextureLoader().reloadTextures();
adaptComponentsSizes();
}
} );
adaptComponentsSizes();
gameView.setVisible( true );
this.currentComponent.setFocusable( true );
EventQueue.invokeLater( new Runnable() {
@Override
public void run() {
final String selection = JOptionPane.showInputDialog( gameView,
"Enter the level to edit", "" );
if( selection == null ) {
ViewManager.getMainView().triggerExit( LevelEditor.this );
} else {
try {
ViewManager.getMainView().swapToFullView( new DummyCanvas( selection ) );
} catch ( Exception e ) {
e.printStackTrace();
ViewManager.getMainView().swapToMenuView( new Label( "Fail" ) );
}
}
}
} );
}
public static void main( String[] args ) {
try {
GameSettings.loadInstance();
System.setProperty("com.apple.mrj.application.apple.menu.about.name",WINDOW_TITLE);
if ( GameSettings.getInstance().getInstanceValue( GameSettings.INTEGER_SETTINGS_TYPE.OPENGL ) != 0 ) {
System.setProperty( "sun.java2d.opengl", "true" );
}
if ( GameSettings.getInstance().getInstanceValue( GameSettings.INTEGER_SETTINGS_TYPE.ANTIALIAS ) != 0 ) {
System.setProperty( "awt.useSystemAAFontSettings", "on" );
}
new LevelEditor();
} catch ( Throwable e ) {
StringBuilder builder = new StringBuilder();
builder.append( LanguageDictionary.EXCEPTION_AT_INITIALIZATION );
builder.append( System.lineSeparator() );
Throwable cause = e;
while ( cause != null ) {
builder.append( cause.getMessage() );
builder.append( System.lineSeparator() );
for (StackTraceElement stackTraceElement : cause.getStackTrace()) {
builder.append( stackTraceElement );
builder.append( System.lineSeparator() );
}
cause = cause.getCause();
if ( cause != null ) {
builder.append( "-----------------------------------------" );
builder.append( System.lineSeparator() );
}
}
System.exit( 1 );
}
}
@Override
public void exitTriggered( @Nullable final Object source ) {
EventQueue.invokeLater( new Runnable() {
@Override
public void run() {
gameView.dispose();
GraphicsEnv.getInstance().getTextureLoader().clear();
}
} );
}
@Override
public void windowClosing( final WindowEvent e ) {
this.triggerExit( this );
}
@Override
public synchronized void swap( @NotNull final Component newComponent ) {
if ( isFullView() ) {
swapToFullView( newComponent );
} else {
swapToMenuView( newComponent );
}
}
@Override
public synchronized void swapToFullView( @NotNull final Component newComponent ) {
final Component previousComponent = getCurrentComponent();
this.currentComponent = newComponent;
if ( ! isFullView() ) {
holder.remove( previousComponent );
gameView.remove( holder );
fullView = true;
} else {
gameView.remove( previousComponent );
}
gameView.add( this.currentComponent );
this.currentComponent.setFocusable( true );
this.currentComponent.setVisible( true );
adaptComponentsSizes();
}
@Override
public synchronized void swapToMenuView( @NotNull final Component newComponent ) {
final Component previousComponent = getCurrentComponent();
currentComponent = newComponent;
currentComponent.setVisible( false );
if ( isFullView() ) {
fullView = false;
gameView.remove( previousComponent );
holder.add( currentComponent );
gameView.add( holder );
} else {
holder.remove( previousComponent );
holder.add( currentComponent );
}
currentComponent.setFocusable( true );
currentComponent.setVisible( true );
adaptComponentsSizes();
}
@NotNull
@Override
public synchronized Component getCurrentComponent() {
return this.currentComponent;
}
@Override
public synchronized void addListener( @NotNull ExitListener listener ) {
this.listeners.add( listener );
}
@Override
public synchronized boolean removeListener( @NotNull ExitListener listener ) {
return this.listeners.remove( listener );
}
@Override
public synchronized void triggerExit( @Nullable Object source ) {
for (ExitListener listener : this.listeners) {
listener.exitTriggered( source );
}
}
@Override
public synchronized void switchToFullView( final boolean fullView ) {
this.fullView = fullView;
}
@Override
public synchronized boolean isFullView() {
return this.fullView;
}
@GuardedBy ( "this" )
protected void adaptComponentsSizes() {
final Insets frameInsets = gameView.getInsets();
final Dimension realDimension = gameView.getSize();
final int viewHeight = realDimension.height - ( frameInsets.bottom + frameInsets.top );
final int viewWidth = realDimension.width - ( frameInsets.left + frameInsets.right );
realDimension.width = viewWidth;
realDimension.height = viewHeight;
if ( isFullView() ) {
final Dimension componentDimension = new Dimension( viewWidth, viewHeight );
currentComponent.setPreferredSize( componentDimension );
currentComponent.setBounds( frameInsets.left, frameInsets.top, componentDimension.width, componentDimension.height );
} else {
final int headerHeight = (int) ( viewHeight * PERCENTAGE_HEADER_HEIGHT );
final int componentHeight = viewHeight - headerHeight;
final Dimension headerDimension = new Dimension( viewWidth, headerHeight );
final Dimension componentDimension = new Dimension( viewWidth, componentHeight );
holder.setPreferredSize( realDimension );
holder.setMaximumSize( realDimension );
holder.setBounds( frameInsets.left, frameInsets.top, realDimension.width, realDimension.height );
header.setPreferredSize( headerDimension );
header.setBounds( 0, 0, headerDimension.width, headerDimension.height );
currentComponent.setPreferredSize( componentDimension );
currentComponent.setBounds( 0, headerHeight, componentDimension.width, componentDimension.height );
}
gameView.revalidate();
gameView.repaint();
}
@NotNull
@Override
public Frame getFrame() {
return this.gameView;
}
}