TUIO Map View with events
TUIO Map View with events import java.util.Map;
int ROWS_OF_TEXT = 4; int TEXT_LINE_HEIGHT = 14; int TEXT_OFFSET_TOP = 20; int TEXT_OFFSET_RIGHT = 140;
class MapEvent { String action; int id; float x, y; MapEvent(String _action, int _id, float _x, float _y) { action = _action; id = _id; x = _x; y = _y; } }
class TUIOMapView {
int cols, rows;
Frame camera_frame, map_frame, text_frame;
MapCellView[][] cell_views;
PImage[] tiles;
HashMap<Integer,MapCellModel> fiducials;
ArrayList
cell_views = new MapCellView[rows][cols]; for (int j = 0; j < rows; j++) { for (int i = 0; i < cols; i++) { cell_views[j][i] = new MapCellView( map_frame.get_x(i), map_frame.get_y(j), map_frame.cell_width, map_frame.cell_height, tiles ); } } } void render(MapModel model) { for (int j = 0; j < rows; j++) { for (int i = 0; i < cols; i++) { cell_views[j][i].render(model.cell_models[j][i]); } } fill(0); text(“Place the hospitals”, text_frame.x, text_frame.get_y(0)); text(“so they serve the towns”, text_frame.x, text_frame.get_y(1)); text(“Score: " + model.score(), text_frame.x, text_frame.get_y(2)); if (model.hospitals_left() > 0) { text(“Hospitals left: " + model.hospitals_left(), text_frame.x, text_frame.get_y(3)); } } void handle_add_fiducial(int id, float x, float y, MapModel model) { create_event(“ADD”, id, x, y, model); } void handle_remove_fiducial(int id, float x, float y, MapModel model) { create_event(“REMOVE”, id, x, y, model); } void handle_move_fiducial(int id, float x, float y, MapModel model) { create_event(“MOVE”, id, x, y, model); } void create_event(String action, int id, float x, float y, MapModel model) { MapEvent new_event = new MapEvent(action, id, x, y); events.add(new_event); process_events(model); } void process_events(MapModel model) { if (!processing_events) { processing_events = true; while (!events.isEmpty()) { MapEvent event = events.remove(0); if (event.action == “ADD”) { add_fiducial(event, model); } else if (event.action == “MOVE”) { move_fiducial(event, model); } else if (event.action == “REMOVE”) { remove_fiducial(event, model); } } processing_events = false; } } void add_fiducial(MapEvent event, MapModel model) { if (camera_frame.is_in_frame(event.x, event.y)) { int col = camera_frame.get_col(event.x); int row = camera_frame.get_row(event.y); MapCellModel cell_model = model.cell_models[row][col]; if (cell_model.is_free()) { fiducials.put(event.id, cell_model); cell_model.add_hospital(); model.update_cell_distances(); } } } void remove_fiducial(MapEvent event, MapModel model) { fiducials.get(event.id).remove_hospital(); fiducials.remove(event.id); model.update_cell_distances(); } void move_fiducial(MapEvent event, MapModel model) { if (camera_frame.is_in_frame(event.x, event.y)) { int col = camera_frame.get_col(event.x); int row = camera_frame.get_row(event.y); MapCellModel new_cell_model = model.cell_models[row][col]; MapCellModel old_cell_model = fiducials.get(event.id); if (new_cell_model != old_cell_model && new_cell_model.is_free()) { fiducials.put(event.id, new_cell_model); old_cell_model.remove_hospital(); new_cell_model.add_hospital(); model.update_cell_distances(); } } } }