Commit 65563542 authored by Clemens Krug's avatar Clemens Krug
Browse files

Icons visualisation dependent on host states

+ ModularMovementIconVis can now visualise nodes dependent on their states too
+ Implemented visualisation for "walking" state.
parent cf246933
......@@ -10,7 +10,23 @@ import de.tudarmstadt.maki.simonstrator.api.Binder;
import java.util.HashMap;
/**
* Created by Clemens on 01.02.2017.
* This class is meant to be used with the RealWorldStreetsMovement
* if different movement types should be used simultaneously. It acts
* like the {@link ModularMovementModel}, but each of the {@link SimLocationActuator}s
* can have a different movement type and the movement route will be calculated according
* to this type. <BR><BR>
*
* USAGE:
* So basically if you want to use this movement model, all you have to do is
* (besides specifying it in your config) call the {@link #setMovementType(SimLocationActuator, String)}
* for each of your components.<BR><BR>
*
* NOTE: All the movement types you are using need to be specified in you config for the
* {@link RealWorldStreetsMovement}. E.g if you are using 'car' and 'foot' movement types,
* in you config the MovementType for the {@link RealWorldStreetsMovement} should be specified as 'car,foot'.
* If not done properly there won't be an error, but the movement behaviour will be strange.
*
* @author Clemens Krug
*/
public class ModularMultiTypeMovementModel extends ModularMovementModel
{
......@@ -54,4 +70,9 @@ public class ModularMultiTypeMovementModel extends ModularMovementModel
{
movementTypes.put(ms, movementType);
}
public String getMovementType(SimLocationActuator ms)
{
return movementTypes.get(ms);
}
}
package de.tud.kom.p2psim.impl.topology.movement.modularosm;
package de.tud.kom.p2psim.impl.topology.movement.modularosm.movementIconVisualisation;
/**
* Created by Clemens on 04.01.2017.
* Mapping of objects to icons, for usage with the {@link ModularMovementIconVis}, especially
* if the config files.
*
* @author Clemens Krug
*/
public class ClassIconMapping {
private String classname;
public class IconMapping
{
private String object;
private String iconpath;
public void setClassname(String icon)
public void setObject(String icon)
{
classname = icon;
object = icon;
}
public void setIconPath(String path)
......@@ -17,8 +21,8 @@ public class ClassIconMapping {
iconpath = path;
}
public String getClassname() {
return classname;
public String getObject() {
return object;
}
public String getIconpath() {
......
package de.tud.kom.p2psim.impl.topology.movement.modularosm;
package de.tud.kom.p2psim.impl.topology.movement.modularosm.movementIconVisualisation;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.ModularMovementModelViz;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView;
import de.tudarmstadt.maki.simonstrator.api.*;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
......@@ -14,19 +15,44 @@ import java.util.HashMap;
import java.util.Map;
/**
* Created by Clemens on 04.01.2017.
* Visualises the different nodes as icons instead of these lame dots.
* The icons are configured entirely in the config files. To use this visualisation
* the first thing to do is specify this {@link ModularMovementIconVis} as visualisation for
* your modular movement model. To add icons to the visualisation the class {@link IconMapping} is used within the tags.
* You have two tags available for different kind of icons:<BR><BR>
*
* 1. Class icons: These icons are used dependent on the class of the components the host includes. You can
* add a class icon by using the &lt;ClassIcon> tag you config and setting the fully qualified classname
* of you components as object. If a host has multiple components with different specified icons, the first one will be used.<BR><BR>
*
* 2. State icons: These icons are used dependent on the state of a host and take precedence over the class icons. You
* can add them via the &lt;StateIcon> tag. The object should be a String representing the state. When
* using these types of icons, you need to set a {@link StateSelector} for this class in the config.<BR><BR>
*
* For example usage, see the movement_social.xml config for the resourceAllocation overlay.
*
* @author Clemens Krug
*/
public class ModularMovementIconVis extends ModularMovementModelViz implements EventHandler
{
private HashMap<Class, String> pathMap;
private HashMap<Class, Image> iconMap;
private HashMap<Class, Image> classIconMap;
private HashMap<String, String> stateMap;
private HashMap<String, Image> stateIconMap;
private StateSelector stateSelector;
private boolean init = false;
public ModularMovementIconVis()
{
init();
pathMap = new HashMap<>();
iconMap = new HashMap<>();
classIconMap = new HashMap<>();
stateMap = new HashMap<>();
stateIconMap = new HashMap<>();
// scheduling initalization!
......@@ -43,11 +69,24 @@ public class ModularMovementIconVis extends ModularMovementModelViz implements E
{
try {
Image icon = ImageIO.read(new File(e.getValue())).getScaledInstance(-1, 15, Image.SCALE_FAST);
iconMap.put(e.getKey(), icon);
classIconMap.put(e.getKey(), icon);
} catch (IOException ioe) {
Monitor.log(ModularMovementIconVis.class, Monitor.Level.WARN, "Could not load icon from path %s", e.getValue());
}
}
for(Map.Entry<String, String> e : stateMap.entrySet())
{
assert stateSelector != null : "There must be a state selector specified when using state icons!";
try {
Image icon = ImageIO.read(new File(e.getValue())).getScaledInstance(-1, 15, Image.SCALE_FAST);
stateIconMap.put(e.getKey(), icon);
} catch (IOException ioe) {
Monitor.log(ModularMovementIconVis.class, Monitor.Level.WARN, "Could not load icon from path %s", e.getValue());
}
}
init = true;
}
......@@ -80,7 +119,16 @@ public class ModularMovementIconVis extends ModularMovementModelViz implements E
Point2D pt = comp.getRealPosition().asPoint();
g2.setColor(Color.GRAY);
for(Map.Entry<Class, Image> e : iconMap.entrySet())
//Check if the component has a specified state, as this would have precedence over the classIcon.
String state = stateSelector.getState(comp);
if(stateIconMap.containsKey(state))
{
g2.drawImage(stateIconMap.get(state), (int) pt.getX(), (int) pt.getY(), null);
return;
}
// If not, check if there's a class icon available
for(Map.Entry<Class, Image> e : classIconMap.entrySet())
{
try {
host.getComponent(e.getKey());
......@@ -92,6 +140,7 @@ public class ModularMovementIconVis extends ModularMovementModelViz implements E
}
}
// If not, use standard visualisation
if(iconNotFound) super.drawNodePosition(g2, comp);
}
......@@ -102,12 +151,22 @@ public class ModularMovementIconVis extends ModularMovementModelViz implements E
}
}
public void setClassIcon(ClassIconMapping classIcon)
public void setClassIcon(IconMapping classIcon)
{
try {
pathMap.put(Class.forName(classIcon.getClassname()), classIcon.getIconpath());
pathMap.put(Class.forName(classIcon.getObject()), classIcon.getIconpath());
} catch (ClassNotFoundException e) {
Monitor.log(ModularMovementIconVis.class, Monitor.Level.WARN, "Class %s not found!", classIcon.getClassname());
Monitor.log(ModularMovementIconVis.class, Monitor.Level.WARN, "Class %s not found!", classIcon.getObject());
}
}
public void setStateIcon(IconMapping stateIcon)
{
stateMap.put(stateIcon.getObject(), stateIcon.getIconpath());
}
public void setStateSelector(StateSelector selector)
{
this.stateSelector = selector;
}
}
package de.tud.kom.p2psim.impl.topology.movement.modularosm.movementIconVisualisation;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
/**
* Interface for the state selector which shall be used in conjunction with the
* {@link ModularMovementIconVis} in order to use state icons.
*
* @author Clemens Krug
*/
public interface StateSelector
{
/**
* Returns the state of the host as string. The returned state
* must be equal to the one specified in the state-icon mapping to have any effect.
* The state of a host can not be null! In case there is no state matched which has an icon
* specified, you can return whatever string you like.
*
* @param ms The host of whom the state should be determined.
* @return The state of the host as string, not null.
*/
String getState(SimLocationActuator ms);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment