Skip to content
This repository has been archived by the owner on Oct 1, 2022. It is now read-only.

Fix #506: Allow soldiers to be force-moved somewhere. #709

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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.awt.event.ComponentListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
Expand All @@ -30,9 +31,12 @@
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.lang.reflect.Field;
import java.util.EnumSet;
import java.util.Set;

import go.graphics.UIPoint;
import go.graphics.event.GOEventHandlerProvider;
import go.graphics.event.command.EModifier;
import go.graphics.event.interpreter.AbstractEventConverter;

/**
Expand All @@ -54,6 +58,8 @@ public class GOSwingEventConverter extends AbstractEventConverter

private int scaleFactor = 1;

private int modifiers;

/**
* Creates a new event converter, that converts swing events to go events.
*
Expand Down Expand Up @@ -108,25 +114,30 @@ private void updateScaleFactor(Component component) {

@Override
public void mouseEntered(MouseEvent e) {
updateModifiers(e);
startHover(convertToLocal(e));
}

@Override
public void mouseMoved(MouseEvent e) {
updateModifiers(e);
updateHoverPosition(convertToLocal(e));
}

@Override
public void mouseExited(MouseEvent e) {
updateModifiers(e);
endHover(convertToLocal(e));
}

@Override
public void mouseClicked(MouseEvent e) {
updateModifiers(e);
}

@Override
public void mousePressed(MouseEvent e) {
updateModifiers(e);
int mouseButton = e.getButton();
UIPoint local = convertToLocal(e);
if (mouseButton == MouseEvent.BUTTON1) {
Expand All @@ -142,6 +153,7 @@ public void mousePressed(MouseEvent e) {

@Override
public void mouseDragged(MouseEvent e) {
updateModifiers(e);
UIPoint local = convertToLocal(e);
updateDrawPosition(local);
updatePanPosition(local);
Expand All @@ -150,6 +162,7 @@ public void mouseDragged(MouseEvent e) {

@Override
public void mouseReleased(MouseEvent e) {
updateModifiers(e);
UIPoint local = convertToLocal(e);
if (e.getButton() == MouseEvent.BUTTON1) {
endDraw(local);
Expand All @@ -164,6 +177,7 @@ public void mouseReleased(MouseEvent e) {

@Override
public void keyPressed(KeyEvent e) {
updateModifiers(e);
String text = getKeyName(e);
startKeyEvent(text);
/*
Expand Down Expand Up @@ -261,6 +275,7 @@ private String getKeyName(KeyEvent e) {

@Override
public void keyReleased(KeyEvent e) {
updateModifiers(e);
endKeyEvent(getKeyName(e));
}

Expand All @@ -270,6 +285,7 @@ public void keyTyped(KeyEvent e) {

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
updateModifiers(e);
float factor = (float) Math.exp(-e.getUnitsToScroll() / 20.0);
startZoom();
endZoomEvent(factor, convertToLocal(e));
Expand Down Expand Up @@ -310,4 +326,23 @@ void privateRegisterComponentListenerToParentWindowOf(Component component, Compo
privateRegisterComponentListenerToParentWindowOf(component.getParent(), childComponent);
}
}

private void updateModifiers(InputEvent e) {
modifiers = e.getModifiers();
}

@Override
protected Set<EModifier> getCurrentModifiers() {
EnumSet<EModifier> set = EnumSet.noneOf(EModifier.class);
if ((modifiers & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
set.add(EModifier.CTRL);
}
if ((modifiers & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
set.add(EModifier.ALT);
}
if ((modifiers & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
set.add(EModifier.SHIFT);
}
return set;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package go.graphics.event.command;

public enum EModifier {
SHIFT,
CTRL,
ALT
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
*******************************************************************************/
package go.graphics.event.command;

import java.util.Set;

import go.graphics.UIPoint;
import go.graphics.event.GOEvent;

public interface GOCommandEvent extends GOEvent {
UIPoint getCommandPosition();

boolean isSelecting();

Set<EModifier> getModifiers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*******************************************************************************/
package go.graphics.event.command;

import java.util.Set;

import go.graphics.UIPoint;
import go.graphics.event.mouse.GOEventProxy;

Expand Down Expand Up @@ -48,4 +50,9 @@ public UIPoint getCommandPosition() {
public boolean isSelecting() {
return baseEvent.isSelecting();
}

@Override
public Set<EModifier> getModifiers() {
return baseEvent.getModifiers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
*******************************************************************************/
package go.graphics.event.interpreter;

import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.Set;

import go.graphics.UIPoint;
import go.graphics.event.GOEvent;
import go.graphics.event.GOEventHandlerProvider;
import go.graphics.event.GOKeyEvent;
import go.graphics.event.SingleHandlerGoEvent;
import go.graphics.event.command.EModifier;
import go.graphics.event.command.GOCommandEvent;

/**
Expand Down Expand Up @@ -189,7 +193,7 @@ protected void abortHover() {
}

protected boolean fireCommandEvent(UIPoint point, boolean isSelect) {
ConvertedCommandEvent commandEvent = new ConvertedCommandEvent(point, isSelect);
ConvertedCommandEvent commandEvent = new ConvertedCommandEvent(point, isSelect, getCurrentModifiers());

handleEvent(commandEvent);

Expand All @@ -198,6 +202,10 @@ protected boolean fireCommandEvent(UIPoint point, boolean isSelect) {
return commandEvent.getHandler() != null;
}

protected Set<EModifier> getCurrentModifiers() {
return EnumSet.noneOf(EModifier.class);
}

protected synchronized void startKeyEvent(String string) {
if (ongoingKeyEvent == null) {
ongoingKeyEvent = new GOKeyEvent(string);
Expand Down Expand Up @@ -250,10 +258,12 @@ private class ConvertedCommandEvent extends SingleHandlerGoEvent implements

private final UIPoint position;
private final boolean selecting;
private final Set<EModifier> modifiers;

public ConvertedCommandEvent(UIPoint position, boolean selecting) {
public ConvertedCommandEvent(UIPoint position, boolean selecting, Set<EModifier> modifiers) {
this.position = position;
this.selecting = selecting;
this.modifiers = modifiers;
}

public void initialized() {
Expand All @@ -271,6 +281,11 @@ public UIPoint getCommandPosition() {
public boolean isSelecting() {
return selecting;
}

@Override
public Set<EModifier> getModifiers() {
return Collections.unmodifiableSet(modifiers);
}

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package jsettlers.common.action;

public enum EMoveToType {
DEFAULT(true, false), FORCED(false, false),
/**
* Work at the target or patrol (for soldiers)
*/
WORK(true, true);

public static EMoveToType[] VALUES = values();

private final boolean attackOnTheWay;
private final boolean workOnDestination;

EMoveToType(boolean attackOnTheWay, boolean workOnDestination) {
this.attackOnTheWay = attackOnTheWay;
this.workOnDestination = workOnDestination;
}

public boolean isAttackOnTheWay() {
return attackOnTheWay;
}

public boolean isWorkOnDestination() {
return workOnDestination;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package jsettlers.common.action;

import jsettlers.common.position.ShortPoint2D;

/**
* Action for {@link EActionType#MOVE_TO}
* @author Michael Zangl
*/
public class MoveToAction extends PointAction {

private final EMoveToType moveToType;

public MoveToAction(EMoveToType moveToType, ShortPoint2D position) {
super(EActionType.MOVE_TO, position);
this.moveToType = moveToType;
}

public EMoveToType getMoveToType() {
return moveToType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import go.graphics.event.GOEvent;
import go.graphics.event.GOEventHandler;
import java8.util.Optional;
import jsettlers.common.action.Action;

/**
Expand All @@ -25,7 +26,7 @@
*/
public class ActionHandler implements GOEventHandler {

private final Action action;
private final Optional<Action> action;
private final ActionFireable connector;

/**
Expand All @@ -36,7 +37,7 @@ public class ActionHandler implements GOEventHandler {
* @param connector
* The event to fire the action for.
*/
public ActionHandler(Action action, ActionFireable connector) {
public ActionHandler(Optional<Action> action, ActionFireable connector) {
this.action = action;
this.connector = connector;
}
Expand All @@ -47,9 +48,7 @@ public void aborted(GOEvent event) {

@Override
public void finished(GOEvent event) {
if (action != null) {
this.connector.fireAction(this.action);
}
action.ifPresent(this.connector::fireAction);
}

@Override
Expand Down
Loading