-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_view.js
93 lines (80 loc) · 2.38 KB
/
map_view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
define([
'jquery',
'canvas_renderer',
'text_renderer',
'reducer/selection'
],
function($, canvasRenderer, textRenderer, Selection) {
/*
* Generates toolbar buttons (which apply to both the text and HTML view).
*/
var buildToolbar = function() {
var toolbarHtml = '<div id="toolbar">' +
'<button id="delete_selection">Delete</button>' +
'<button id="add_room">Add Room</button>' +
'</div>';
return $(toolbarHtml);
};
/*
* Adds event listeners for the toolbar buttons.
*
*@param $toolbar The toolbar div, as a jQuery object (hence the $).
*/
var addToolbarListeners = function($toolbar, store) {
$toolbar.find('#delete_selection').on(
'click',
dispatchDelete.bind(this, store)
);
$toolbar.find('#add_room').on(
'click',
store.dispatch.bind(store, {
type: 'action.stage',
payload: {
type: 'map.rooms.add',
payload: {
x: 0,
y: 0,
width: 1,
height: 1
}
}
})
);
};
// TODO: consider converting to a Redux thunk?
var dispatchDelete = function(store) {
var state = store.getState();
var roomIdsToDelete = Selection.selectedIds(state.selection, 'room');
store.dispatch({
type: 'map.rooms.remove',
payload: { roomIds: roomIdsToDelete }
});
var doorIdsToDelete = Selection.selectedIds(state.selection, 'door');
store.dispatch({
type: 'map.doors.remove',
payload: { doorIds: doorIdsToDelete }
});
// Clear selection now we've deleted everything that was in it.
store.dispatch({ type: 'selection.clear' });
}
// MVC implementation based on example from <https://alexatnet.com/articles/model-view-controller-mvc-javascript>
var MapView = function(store, canvas) {
var graphicsContext = canvas.getContext('2d');
var toolbar = buildToolbar();
var textContext = $('<div>');
$(canvas).before(toolbar);
$(canvas).after(textContext);
addToolbarListeners(toolbar, store);
textRenderer.addListeners(textContext, store);
var render = function(store) {
canvasRenderer.render(store, graphicsContext);
textRenderer.render(store, textContext);
canvasRenderer.addListeners(canvas, store);
}
store.subscribe(function() {
render(store);
});
render(store);
};
return MapView;
});