-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
206 lines (201 loc) · 6.23 KB
/
Copy pathindex.js
File metadata and controls
206 lines (201 loc) · 6.23 KB
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
const React = require('react');
const Reconciler = require('react-reconciler');
const muray = require('muray');
const TRACE = false;
const hostConfig = {
noTimeout: -1,
isPrimaryRenderer: true,
supportsMutation: true,
supportsPersistence: false,
supportsHydration: false,
getRootHostContext: (...args) => {
if (TRACE) console.log('getRootHostContext', args);
return "urmom";
},
prepareForCommit: (...args) => {
if (TRACE) console.log('prepareForCommit', args);
return null;
},
resetAfterCommit: (...args) => {
if (TRACE) console.log('resetAfterCommit', args);
},
getChildHostContext: (...args) => {
if (TRACE) console.log('getChildHostContext', args);
return "urchild"
},
shouldSetTextContent(type, props) {
if (TRACE) console.log('shouldSetTextContent', type, props);
// Documentation for shouldSetTextContent says you have to manage creating the text nodes in createInstance when this returns true.
// const childrenType = typeof props.children;
// if (childrenType === 'string' || childrenType == 'number') return true;
return false;
},
createTextInstance(text, _rootContainerInstance, _hostContext) {
if (TRACE) console.log('createTextInstance');
return {
type: 'text',
text
};
},
createInstance(
type,
props,
rootContainerInstance,
_hostContext,
) {
if (TRACE) console.log("createInstance");
const elementProps = { ...props };
const element = { type, ...elementProps, children: [] };
if (type === 'Text') {
throw 'TODO';
}
return element;
},
appendInitialChild(parentInstance, child) {
if (TRACE) console.log('appendInitialChild');
parentInstance.children.push(child);
},
finalizeInitialChildren(...args) {
if (TRACE) console.log('finalizeInitialChildren', args);
return true;
},
clearContainer(rootContainerInstance) {
if (TRACE) console.log('clearContainer', rootContainerInstance);
rootContainerInstance.children = [];
},
appendChildToContainer(rootContainerInstance, child) {
if (TRACE) console.log('appendChildToContainer', rootContainerInstance, child);
rootContainerInstance.children.push(child);
},
commitMount(instance, type, newProps) {
if (TRACE) console.log('commitMount', instance, type, newProps);
},
prepareUpdate(
instance,
type,
oldProps,
newProps,
_rootContainerInstance,
_hostContext,
) {
if (TRACE) console.log('prepareUpdate');
const changes = {
props: [],
style: [],
};
for (let key in { ...oldProps, ...newProps }) {
if (oldProps[key] !== newProps[key]) {
changes.props.push(key);
}
}
for (let key in { ...oldProps.style, ...newProps.style }) {
if (oldProps.style[key] !== newProps.style[key]) {
changes.style.push(key);
}
}
// const updatePayload = changes.props.length || changes.style.length ? { changes } : null;
return changes;
},
commitTextUpdate(textInstance, oldText, newText) {
textInstance.text = newText;
if (TRACE) console.log('commitTextUpdate', textInstance, oldText, newText);
},
commitUpdate(
instance,
updatePayload,
type,
oldProps,
newProps,
) {
if (TRACE) console.log('commitUpdate', instance, oldProps, newProps);
for (let prop of updatePayload.props) {
if (prop !== 'children') {
instance[prop] = newProps[prop];
}
}
},
};
const MurayRenderer = Reconciler(hostConfig);
function renderTextElement(element) {
if (element.type === 'text') {
return element.text;
}
console.log(element);
throw 'Not text!';
}
function renderElement(element) {
switch (element.type) {
case 'window':
{
muray.mu_begin_window();
for (let child of element.children) {
renderElement(child);
}
muray.mu_end_window();
} break;
case 'div':
{
for (let child of element.children) {
renderElement(child);
}
} break;
case 'button':
{
let label = "";
for (let child of element.children) {
label += renderTextElement(child);
}
if (muray.mu_button(label)) {
element.onClick();
}
} break;
case 'label':
{
let label = "";
for (let child of element.children) {
label += renderTextElement(child);
}
muray.mu_label(label);
} break;
case 'input':
{
let placeholder = element.placeholder || "";
muray.mu_label(placeholder);
if (element.id == undefined || element.id == "") throw "MISSING ID FOR TEXT INPUT";
let value = muray.mu_input(element.id);
if (value) {
const evt = { target: { value } };
element.onChange(evt);
}
} break;
default:
throw 'TODO';
}
}
exports.render = (element) => {
const container = MurayRenderer.createContainer(
{ type: 'window' },
0,
// null,
// false,
// null,
// 'react_raylib_',
// (_err) => undefined,
// null
);
MurayRenderer.updateContainer(element, container);
muray.InitWindow(800, 600, "Hello from JavaScript");
while (!muray.WindowShouldClose()) {
muray.mu_update_input();
muray.BeginDrawing();
{
muray.ClearBackground(0xFF181818);
muray.mu_begin();
{
renderElement(container.containerInfo);
}
muray.mu_end();
}
muray.EndDrawing();
}
}