Skip to content

Commit

Permalink
Had some fun with the Level Editor. Removed the dependency on MiGLayo…
Browse files Browse the repository at this point in the history
…ut by creating my own Panel instead. Tried to change as little as possible of the existing code, there should be no functionality changes except for one small thing: The warning text in the population tab did not update when you removed an entry, now it does. Also added what to write in the command line to compile and run the Level Editor, and added some more files to the ignore file
  • Loading branch information
Lego3 committed Sep 7, 2013
1 parent 1258b8b commit 3ff011d
Show file tree
Hide file tree
Showing 26 changed files with 666 additions and 390 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ RelWithDebInfo/
!LevelEdit/src/com/MinSizeRel
!LevelEdit/src/com/RelWithDebInfo
!LevelEdit/src/com/.DS_Store

# This is for the CMake-generated Visual Studio project
*.vcxproj*
CorsixTH_Top_Level*

# For the LevelEdit project
*.class
1 change: 0 additions & 1 deletion LevelEdit/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="miglayout-4.0-swing.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
27 changes: 0 additions & 27 deletions LevelEdit/MiGLayout-License.txt

This file was deleted.

12 changes: 9 additions & 3 deletions LevelEdit/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ editing a text file, which can be quite error prone the level creator can use
this tool to set all available variables used in CorsixTH and then save it
as a .level file.

The tool is written in Java, and a project for Eclipse is supplied here, though
it should work just as well to use any other IDE or command line tool to
compile it.
The tool is written in Java. The supplied project for Eclipse can be used
to compile. An alternative is to issue the following commands while in this
folder:

Compile:
javac -d bin -sourcepath src src/com/corsixth/leveledit/Main.java

Run:
java -cp bin com.corsixth.leveledit.Main

------------------------------------------------------------------------------
-- Contact Details
Expand Down
Binary file removed LevelEdit/miglayout-4.0-swing.jar
Binary file not shown.
4 changes: 1 addition & 3 deletions LevelEdit/src/com/corsixth/leveledit/Emergency.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ this software and associated documentation files (the "Software"), to deal in
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class Emergency {
static int emergencyMode = 2; // 0 = random;
// 1 = semi-random
Expand All @@ -53,7 +51,7 @@ public class Emergency {
"Spare Ribs", "Sweaty Palms", "The Squits", "Transparency",
"Uncommon Cold", "Unexpected Swelling" };

JPanel emergencyPanel = new JPanel(new MigLayout("insets 1"));
JPanel emergencyPanel = new JPanel();

JComboBox illnessCombo = new JComboBox(diseases);
JLabel minPatientsLabel = new JLabel("Patients min:");
Expand Down
22 changes: 17 additions & 5 deletions LevelEdit/src/com/corsixth/leveledit/FileChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,42 @@ this software and associated documentation files (the "Software"), to deal in

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

//creates JFileChooser
/**
* A custom JFileChooser
*
* @author Koanxd
*
*/
public class FileChooser extends JFileChooser {

// this is insignificant but apparently its needed because JFrame is
// serializable
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 2153326271573582591L;

ReaderWriter readerWriter = new ReaderWriter();

FileNameExtensionFilter corsixTH = new FileNameExtensionFilter(
"CorsixTH level files (.level)", "level");
FileNameExtensionFilter originalTH = new FileNameExtensionFilter(
"Theme Hospital Level files (.SAM)", "SAM");

public FileChooser() {
// Used to get the CorsixTH icon in the top bar of the dialog.
JFrame mainFrame;

public FileChooser(JFrame frame) {
setCurrentDirectory(readerWriter.getLastFilePath());
setFileFilter(corsixTH);
mainFrame = frame;
}

public File open() {
addChoosableFileFilter(originalTH);
setAcceptAllFileFilterUsed(true);
int returnVal = showOpenDialog(null);
int returnVal = showOpenDialog(mainFrame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
readerWriter.saveLastFilePath(getSelectedFile().getPath());
return getSelectedFile();
Expand All @@ -63,7 +75,7 @@ public File saveAs() throws IOException {
removeChoosableFileFilter(originalTH);
setAcceptAllFileFilterUsed(false);
setFileFilter(corsixTH);
int returnVal = showSaveDialog(null);
int returnVal = showSaveDialog(mainFrame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = getSelectedFile();
readerWriter.saveLastFilePath(file.getPath());
Expand Down
232 changes: 232 additions & 0 deletions LevelEdit/src/com/corsixth/leveledit/GridPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
/*
Copyright (c) 2013 Edvin "Lego3" Linge
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package com.corsixth.leveledit;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

/**
* A panel that uses the GridBagLayout to layout its components. This is a
* rudimentary implementation tailored for the LevelEdit project. It supplies
* similar functionality as MigLayout, but only the parts we need. The
* constructor takes a column argument that specifies the number of columns used
* in the grid. Each added component is put at the end of the grid left to
* right, top to bottom.
*
* @author Edvin "Lego3" Linge
*
*/
public class GridPanel extends JPanel {

private static final long serialVersionUID = 6388650541048165610L;
private static final int INSETS = 2;
private static final int BORDER = 3;

private int columns = 0;
private int rows = 0;

private GridBagConstraints c = new GridBagConstraints();

// Used to add a dummy JPanel to the end so that we can achieve top left
// alignment.
private GridBagConstraints fillerC = new GridBagConstraints();

private JPanel panelFiller = new JPanel();

private ArrayList<Component>[] contents;

@SuppressWarnings("unchecked")
public GridPanel(int noColumns) {
c.insets = new Insets(INSETS, INSETS, INSETS, INSETS);
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.WEST;
columns = noColumns;

fillerC.gridx = 1;
fillerC.gridy = 1;
fillerC.weighty = 1;
fillerC.weightx = 1;

setBorder(BorderFactory.createEmptyBorder(BORDER, BORDER, BORDER,
BORDER));
setLayout(new GridBagLayout());

contents = (ArrayList<Component>[]) new ArrayList[noColumns];
for (int i = 0; i < noColumns; i++) {
contents[i] = new ArrayList<Component>();
}
}

/**
* Set insets in all directions to 'inset' all for each object added in the
* future.
*
* @param inset
* Inset to use in all directions.
*/
public void setInsets(int inset) {
setInsets(inset, inset, inset, inset);
}

/**
* Set insets in each direction according to the parameters, will be used
* for all objects added to the JGridPanel from this point onwards.
*
* @param top
* Top inset value
* @param left
* Left inset value
* @param bottom
* Bottom inset value
* @param right
* Right inset value
*/
public void setInsets(int top, int left, int bottom, int right) {
c.insets = new Insets(top, left, bottom, right);
}

@Override
public Component add(Component comp) {
super.remove(panelFiller);
contents[c.gridx].add(comp);
add(comp, c);
next();
add(panelFiller, fillerC);

return comp;
}

/**
* Adds a new component to the container and advances one row i newRow is
* true. If the component is added to the last column in the current row the
* advancement is still only one row.
*
* @param comp
* Component to add.
* @param newRow
* Whether to advance one row after adding the new component.
* @return
*/
public Component add(Component comp, boolean newRow) {
int row = rows;
add(comp);
if (row == rows && newRow) {
nextRow();
}
return comp;
}

@Override
public void remove(Component comp) {
boolean found = false;
for (int i = 0; i < columns; i++) {
int index = contents[i].indexOf(comp);
if (index != -1) {
super.remove(comp);
contents[i].remove(comp);
found = true;
break;
}
}
if (found) {
int max = 0;
for (int i = 0; i < columns; i++) {
max = Math.max(max, contents[i].size());
}
if (max <= rows) {
removeRow();
}
}
}

/**
* Finds the oldComp in the grid of added components and swaps it for the
* newComp.
*
* @param oldComp
* @param newComp
*/
public void swap(Component oldComp, Component newComp) {
for (int i = 0; i < columns; i++) {
int index = contents[i].indexOf(oldComp);
if (index != -1) {
contents[i].remove(index);
contents[i].add(index, newComp);
super.remove(oldComp);
int tempX = c.gridx;
int tempY = c.gridy;
c.gridx = i;
c.gridy = index;
add(newComp);
c.gridx = tempX;
c.gridy = tempY;
break;
}
}
}

/**
* Skips 'times' cells in the grid so that the next component will be added furhter down.
* @param times how many cells to skip.
*/
public void next(int times) {
for (int i = 0; i < times; i++) {
next();
}
}

/**
* Skips one cell in the grid.
*/
public void next() {
c.gridx++;
if (c.gridx >= columns) {
nextRow();
}
fillerC.gridx = columns + 1;
fillerC.gridy = c.gridy + 1;
}

/**
* Advances the layout one row so that the next added component will be on a new one.
*/
public void nextRow() {
c.gridx = 0;
c.gridy++;
rows++;
}

private void removeRow() {
c.gridx = 0;
c.gridy--;
rows--;
}

}
Loading

0 comments on commit 3ff011d

Please sign in to comment.