Skip to content

fix #53: enable auto goto #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion micropolis-java/src/micropolisj/engine/Micropolis.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public class Micropolis

public int gameLevel;

boolean autoGo;
public boolean autoGo = true;

// census numbers, reset in phase 0 of each cycle, summed during map scan
int poweredZoneCount;
Expand Down Expand Up @@ -2169,6 +2169,12 @@ public void toggleAutoBulldoze()
fireOptionsChanged();
}

public void toggleAutoGo()
{
autoGo = !autoGo;
fireOptionsChanged();
}

public void toggleDisasters()
{
noDisasters = !noDisasters;
Expand Down
40 changes: 39 additions & 1 deletion micropolis-java/src/micropolisj/gui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public MainWindow(Micropolis engine)

c.gridy = 3;
c.weighty = 0.0;
notificationPane = new NotificationPane(engine);
notificationPane = new NotificationPane(engine, this);
leftPane.add(notificationPane, c);

pack();
Expand Down Expand Up @@ -545,6 +545,17 @@ public void actionPerformed(ActionEvent ev)
}));
optionsMenu.add(autoBulldozeMenuItem);

autoGoMenuItem = new JCheckBoxMenuItem(strings.getString("menu.options.auto_go"));
setupKeys(autoGoMenuItem, "menu.options.auto_go");
autoGoMenuItem.addActionListener(wrapActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev)
{
onAutoGoClicked();
}
}));
optionsMenu.add(autoGoMenuItem);

disastersMenuItem = new JCheckBoxMenuItem(strings.getString("menu.options.disasters"));
setupKeys(disastersMenuItem, "menu.options.disasters");
disastersMenuItem.addActionListener(wrapActionListener(
Expand Down Expand Up @@ -793,6 +804,7 @@ private Micropolis getEngine()

JMenuItem autoBudgetMenuItem;
JMenuItem autoBulldozeMenuItem;
JMenuItem autoGoMenuItem;
JMenuItem disastersMenuItem;
JMenuItem soundsMenuItem;
Map<Speed,JMenuItem> priorityMenuItems;
Expand All @@ -810,6 +822,12 @@ private void onAutoBulldozeClicked()
getEngine().toggleAutoBulldoze();
}

private void onAutoGoClicked()
{
dirty1 = true;
getEngine().toggleAutoGo();
}

private void onDisastersClicked()
{
dirty1 = true;
Expand Down Expand Up @@ -1109,6 +1127,21 @@ void doQueryTool(int xpos, int ypos)
notificationPane.showZoneStatus(engine, xpos, ypos, z);
}

public void centering(CityLocation loc)
{
Dimension d = drawingAreaScroll.getViewport().getExtentSize();
Dimension mapSize = drawingAreaScroll.getViewport().getViewSize();

Point np = new Point(
loc.x * drawingArea.getTileSize() - d.width / 2,
loc.y * drawingArea.getTileSize() - d.height / 2
);
np.x = Math.max(0, Math.min(np.x, mapSize.width - d.width));
np.y = Math.max(0, Math.min(np.y, mapSize.height - d.height));

drawingAreaScroll.getViewport().setViewPosition(np);
}

private void doZoom(int dir, Point mousePt)
{
int oldZoom = drawingArea.getTileSize();
Expand Down Expand Up @@ -1562,6 +1595,10 @@ public void cityMessage(MicropolisMessage m, CityLocation p)
{
notificationPane.showMessage(engine, m, p.x, p.y);
}
if (getEngine().autoGo && p != null)
{
centering(p);
}
}

//implements Micropolis.Listener
Expand All @@ -1580,6 +1617,7 @@ private void reloadOptions()
{
autoBudgetMenuItem.setSelected(getEngine().autoBudget);
autoBulldozeMenuItem.setSelected(getEngine().autoBulldoze);
autoGoMenuItem.setSelected(getEngine().autoGo);
disastersMenuItem.setSelected(!getEngine().noDisasters);
soundsMenuItem.setSelected(doSounds);
for (Speed spd : priorityMenuItems.keySet())
Expand Down
25 changes: 23 additions & 2 deletions micropolis-java/src/micropolisj/gui/NotificationPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,36 @@ public class NotificationPane extends JPanel
MicropolisDrawingArea mapView;
JPanel mainPane;
JComponent infoPane;
MainWindow mainWindow;
CityLocation loc = null;

static final Dimension VIEWPORT_SIZE = new Dimension(160,160);
static final Color QUERY_COLOR = new Color(255,165,0);
static final ResourceBundle strings = MainWindow.strings;
static final ResourceBundle mstrings = ResourceBundle.getBundle("micropolisj.CityMessages");
static final ResourceBundle s_strings = ResourceBundle.getBundle("micropolisj.StatusMessages");

public NotificationPane(Micropolis engine)
public NotificationPane(Micropolis engine, MainWindow mainWindow)
{
super(new BorderLayout());
this.mainWindow = mainWindow;
setVisible(false);

headerLbl = new JLabel();
headerLbl.setOpaque(true);
headerLbl.setHorizontalAlignment(SwingConstants.CENTER);
headerLbl.setBorder(BorderFactory.createRaisedBevelBorder());
add(headerLbl, BorderLayout.NORTH);

JButton goBtn = new JButton(strings.getString("notification.go_btn"));
goBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
onGoClicked();
}});

JPanel headerPane = new JPanel(new BorderLayout());
headerPane.add(headerLbl, BorderLayout.CENTER);
headerPane.add(goBtn, BorderLayout.EAST);
add(headerPane, BorderLayout.NORTH);

JButton dismissBtn = new JButton(strings.getString("notification.dismiss"));
dismissBtn.addActionListener(new ActionListener() {
Expand Down Expand Up @@ -74,8 +87,16 @@ private void onDismissClicked()
setVisible(false);
}

private void onGoClicked()
{
if (loc != null) {
mainWindow.centering(loc);
}
}

void setPicture(Micropolis engine, int xpos, int ypos)
{
loc = new CityLocation(xpos, ypos);
Dimension sz = VIEWPORT_SIZE;

mapView.setEngine(engine);
Expand Down
3 changes: 3 additions & 0 deletions micropolis-java/strings/GuiStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ menu.options.auto_budget = Auto Budget
menu.options.auto_budget.key = A
menu.options.auto_bulldoze = Auto Bulldoze
menu.options.auto_bulldoze.key = Z
menu.options.auto_go = Auto Goto
menu.options.auto_go.key = G
menu.options.disasters = Disasters
menu.options.disasters.key = D
menu.options.sound = Sound
Expand Down Expand Up @@ -372,3 +374,4 @@ notification.value_lbl = Value:
notification.crime_lbl = Crime:
notification.pollution_lbl = Pollution:
notification.growth_lbl = Growth:
notification.go_btn = GO!