-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
184 lines (151 loc) · 5.27 KB
/
logic.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"use strict";
// Create a dragstart event handler.
function makeEventListener(type) {
return function (event) {
event.dataTransfer.setData("Text", type);
event.dataTransfer.effectAllowed = 'move'; // only allow moves, what the fuck does that even mean?
}
}
// indexedDB.deleteDatabase("logic");
/********************************************************************************************/
var Pallet = Backbone.View.extend({
/********************************************************************************************/
tag: "div",
initialize: function() {
},
render: function() {
var gate_list = ["AND", "OR", "NOT", "NAND", "NOR", "XOR", "XNOR", "SWITCH", "BULB", "INPUT", "OUTPUT"];
// Create the elements
var html = '<span>';
for (var i = 0; i < gate_list.length; i++) {
var width = 30;
var height = 30;
if (gate_info[gate_list[i]]) {
width *= gate_info[gate_list[i]].size.height;
height *= gate_info[gate_list[i]].size.width;
}
html += '<canvas width="' + width + '" height="' + height +'" draggable="true" id="pallet-item-' + String(i) + '"></canvas>'
}
html += "</span>";
this.$el.html(html);
for (var i = 0; i < gate_list.length; i++) {
var canvas = this.$("#pallet-item-" + String(i))[0];
var type = gate_list[i];
this.drawGate(canvas, type);
canvas.addEventListener("dragstart", makeEventListener(type), true);
}
},
drawGate: function(canvas, type) {
var ctx = canvas.getContext("2d");
if (gate_info[type]) {
ctx.scale(canvas.width / gate_info[type].size.height, canvas.height / gate_info[type].size.width);
ctx.translate(gate_info[type].size.height - 0.5, 0.5);
} else {
ctx.scale(canvas.width, canvas.height);
ctx.translate(0.5, 0.5);
}
ctx.rotate(Math.PI / 2);
ctx.translate(-0.5, -0.5);
var gate = makeGate(type)
gate.draw(ctx);
}
});
/********************************************************************************************/
var MainView = JakeKit.Stack.extend({
/********************************************************************************************/
initialize: function(database) {
this.database = database;
JakeKit.Stack.prototype.initialize.call(this);
_.bindAll(this, "newProject");
// Create the user interface
this.project_view_container = new JakeKit.HBox();
this.addChild(this.project_view_container);
this.makeActive(this.project_view_container);
// Load data
this.project_list = database.getProjectList();
var active_project = this.database.getConfig("active_project");
if (active_project) {
this.openProject(active_project);
} else {
this.newProject();
}
},
newProject: function() {
var project_list = this.database.getProjectList();
var new_project = new this.database.Project({
cdate: Date(),
mdate: Date(),
adate: Date(),
name: "New Project",
project_id: this.database.createID()
});
project_list.add(new_project);
new_project.save();
this.openProject(new_project.get("project_id"));
},
openProject: function(project_id, replay_to) {
if (this.project_view) {
clearInterval(this.project_view.interval_id);
}
var error_at = null;
try {
this.project = new Project(this.database.loadProjectData(project_id, true), this, replay_to);
}
catch (e) { throw(e);
error_at = e.error_at;
}
this.project_view_container.empty()
if (_.isNumber(error_at)) {
var error_handler = new ErrorHandler({}, this, project_id, error_at - 1);
this.project_view_container.addChild(error_handler);
} else {
this.project_view = new ProjectView(this.project);
this.project_view_container.addChild(this.project_view);
// Todo this was never meant to be needed outside of the toolkit :-(
if (this.project_view_container._vivified) {
this.project_view_container._resized();
}
this.database.setConfig("active_project", project_id);
}
}
});
var ErrorHandler = Backbone.View.extend({
initialize: function(none, main_view, project_id, replay_to) {
this.project_id = project_id;
this.replay_to = replay_to;
this.main_view = main_view;
},
render: function() {
var html = '<div style="ErrorHandler">An error has occurred, what do you want to do<br>';
html += '<input type="radio" name="recovery_choice" value="recover" checked>Try and recover the ';
html += 'project. You may loose some of your most recent edits but the project will remain usable.<br>';
html += '<input type="radio" name="recovery_choice" value="new">Forget this project for now and ';
html += 'open a new project<br>';
//html += '<input type="radio" name="recovery_choice" value="existing">Forget this project for now and ';
//html += 'choose a different project to open<br>';
html += '<button id="go" a="Go">Go</button>';
this.$el.html(html);
var that = this;
this.$("#go").click(function() {
console.log();
switch ($("input:radio[name='recovery_choice']:checked").val()) {
case "recover":
that.main_view.openProject(that.project_id, that.replay_to);
break;
case "new":
that.main_view.newProject();
break;
case "existing":
break;
}
});
}
});
var body;
$(document).ready(function() {
getDatabase("logic").done(function(database) {
body = new JakeKit.Wrapper($('body'));
var main_view = new MainView(database);
body.setChild(main_view);
});
});