forked from NetsBlox/Snap--Build-Your-Own-Blocks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages.js
209 lines (169 loc) · 5.43 KB
/
messages.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*globals DialogBoxMorph, Point, fontHeight, HandleMorph, BlockEditorMorph, SpriteMorph,
ReporterBlockMorph, modules*/
// Global settings /////////////////////////////////////////////////////
modules.messages = '2015-October-02';
// Message /////////////////////////////////////////////////////////////
/*
This is a Message object for NetsBlox. It contains a number of
predefined fields
*/
function MessageType(name, fields) {
this.name = name;
this.fields = fields;
}
function Message(type) {
this.type = type;
this.contents = {};
this.init();
}
Message.prototype.init = function() {
for (var i = this.type.fields.length; i--;) {
this.set(this.type.fields[i], 0);
}
};
Message.prototype.set = function(field, value) {
this.contents[field] = value;
};
Message.prototype.get = function(field) {
return this.contents[field];
};
Message.prototype.getFieldNames = function() {
return this.type.fields.slice();
};
Message.prototype.toString = function() {
// FIXME: Better representation would be good...
// Can I reuse the List graphic?
return this.type.name + ' Message';
};
// MessageFrame
function MessageFrame() {
this.msgTypes = {};
this.version = Date.now();
}
MessageFrame.prototype.addMsgType = function(messageType) {
this.msgTypes[messageType.name] = messageType;
this.version = Date.now();
};
MessageFrame.prototype.getMsgType = function(name) {
return this.msgTypes[name];
};
MessageFrame.prototype.deleteMsgType = function(name) {
delete this.msgTypes[name];
this.version = Date.now();
};
MessageFrame.prototype.names = function() {
return Object.keys(this.msgTypes);
};
// MessageCreatorMorph ///////////////////////////////////////////
// A MessageCreatorMorph is used to create custom message types.
MessageCreatorMorph.prototype = new DialogBoxMorph();
MessageCreatorMorph.prototype.constructor = MessageCreatorMorph;
MessageCreatorMorph.uber = DialogBoxMorph.prototype;
function MessageCreatorMorph(target, action) {
this.init(target, action);
}
MessageCreatorMorph.prototype.fixLayout = function() {
var th = fontHeight(this.titleFontSize) + this.titlePadding * 2;
if (this.body) {
this.body.setPosition(this.position().add(new Point(
this.padding,
th + this.padding
)));
this.minWidth = this.minWidth || 0;
var originalWidth = this.body.width() + this.padding * 2;
this.silentSetWidth(Math.max(originalWidth, this.minWidth));
this.silentSetHeight(
this.body.height()
+ this.padding * 2
+ th
);
this.body.setLeft(this.left() + Math.round((this.width() - this.body.width()) / 2));
}
if (this.label) {
this.label.setCenter(this.center());
this.label.setTop(this.top() + (th - this.label.height()) / 2);
}
if (this.buttons && (this.buttons.children.length > 0)) {
this.buttons.fixLayout();
this.silentSetHeight(
this.height()
+ this.buttons.height()
+ this.padding
);
this.silentSetWidth(Math.max(
this.width(),
this.buttons.width()
+ (2 * this.padding)
)
);
this.buttons.setCenter(this.center());
this.buttons.setBottom(this.bottom() - this.padding);
}
};
MessageCreatorMorph.prototype.init = function(target, action) {
var myself = this;
this.minWidth = 0;
MessageCreatorMorph.uber.init.call(this, target);
this.key = 'createNewMsgType';
this.labelString = 'Create Message Type';
this.createLabel();
// Create message definition area
var messageBlock = new MessageDefinitionBlock();
this.addBody(messageBlock);
var fixLayout = messageBlock.fixLayout;
messageBlock.fixLayout = function() {
this.parent.drawNew();
fixLayout.call(this);
myself.fixLayout();
myself.handle.drawNew(); // Should this be automatic?
myself.drawNew();
};
this.accept = function() {
// Get the info for the message type
var desc = {
name: messageBlock.messageName(),
fields: messageBlock.fields()
};
if (desc.name) {
action(desc);
}
this.destroy();
};
this.addButton('ok', 'OK');
this.addButton('cancel', 'Cancel');
this.fixLayout();
this.drawNew();
this.minWidth = this.width();
};
MessageCreatorMorph.prototype.popUp = function () {
var world = this.target.world();
if (world) {
BlockEditorMorph.uber.popUp.call(this, world);
this.handle = new HandleMorph(
this,
280,
220,
this.corner,
this.corner
);
}
};
MessageDefinitionBlock.prototype = new ReporterBlockMorph();
MessageDefinitionBlock.prototype.constructor = MessageDefinitionBlock;
MessageDefinitionBlock.uber = ReporterBlockMorph.prototype;
function MessageDefinitionBlock() {
this.init();
}
MessageDefinitionBlock.prototype.init = function() {
MessageDefinitionBlock.uber.init.call(this);
this.color = SpriteMorph.prototype.blockColor.network;
this.category = 'network';
this.setSpec('name: %hintname fields: %mhintfield');
this.drawNew();
};
MessageDefinitionBlock.prototype.messageName = function() {
return this.inputs()[0].evaluate();
};
MessageDefinitionBlock.prototype.fields = function() {
return this.inputs()[1].evaluate();
};