forked from abeisgoat/IsHTML
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsHTMLBehavior.ts
More file actions
278 lines (236 loc) · 8.12 KB
/
IsHTMLBehavior.ts
File metadata and controls
278 lines (236 loc) · 8.12 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const getRandomID = () =>
Math.random()
.toString(36)
.replace(".", "");
namespace game {
export class IsHTMLBehaviorFilter extends ut.EntityFilter {
entity: ut.Entity;
rect: ut.UILayout.RectTransform;
is_html: game.IsHTML;
sprite_renderer: ut.Core2D.Sprite2DRenderer;
}
export class IsHTMLBehavior extends ut.ComponentBehaviour {
data: IsHTMLBehaviorFilter;
elements: { [element_id: string]: HTMLElement } = {};
OnEntityEnable(): void {
let style = document.querySelector("style.is_html_styles");
if (!style) {
style = document.createElement("style");
style.setAttribute("class", "is_html_styles");
document.body.appendChild(style);
}
style.innerHTML = this.world.getConfigData(game.IsHTMLConfiguration).css;
const tag = document.createElement("div");
const id = `isHTML_${getRandomID()}`;
tag.setAttribute("id", id);
tag.setAttribute("class", "is_html");
tag.setAttribute(
"style",
"top: 0px; left; 0px; width: 0px; 0px; position: absolute; display: grid;"
);
document.body.appendChild(tag);
const options = [
{
text: "Yes",
action: () => {
alert("You saidy yes");
}
},
{
text: "No",
action: () => {
alert("You said nah");
}
}
];
this.data.is_html._selector = `#${id}`;
IsHTMLService.SetHTML(this.data.is_html, this.data.is_html.html);
this.elements[this.data.is_html._selector] = tag;
if (!localStorage.getItem("IS_HTML_DEBUG")) {
this.data.sprite_renderer.color.a = 0;
}
}
OnEntityUpdate(): void {
let size = ut.UILayout.UILayoutService.getRectTransformSizeOfEntity(
this.world,
this.data.entity
);
let displayInfo = this.world.getConfigData(ut.Core2D.DisplayInfo);
let displaySize = new Vector2(displayInfo.width, displayInfo.height);
// Todo: Take into account sizeDelta
let multiple = displaySize.y / 1080 / window.devicePixelRatio;
if (multiple == Infinity) return;
const el = this.elements[this.data.is_html._selector];
if (!el) throw this.data.is_html._selector;
const offset = new Vector2(
(size.x / (1 / this.data.rect.pivot.x)) * multiple,
(size.y / (1 / this.data.rect.pivot.y)) * multiple
);
const topLeftAnchorMin = new Vector2(
(this.data.rect.anchorMin.x + this.data.rect.anchorMax.x) / 2,
Math.abs(
(this.data.rect.anchorMin.y + this.data.rect.anchorMax.y) / 2 - 1
)
);
if (((window as any).DEBUG || "") == this.data.is_html._selector) {
console.log(this.data.rect.sizeDelta, size);
}
el.style.width = `${size.x * multiple}px`;
el.style.height = `${size.y * multiple}px`;
el.style.left = `${(displaySize.x / window.devicePixelRatio) *
topLeftAnchorMin.x +
this.data.rect.anchoredPosition.x * multiple -
offset.x}px`;
el.style.top = `${(displaySize.y / window.devicePixelRatio) *
topLeftAnchorMin.y -
this.data.rect.anchoredPosition.y * multiple -
offset.y}px`;
}
// this method is called for each entity matching the IsHTMLBehaviorFilter signature, once when disabled
//OnEntityDisable():void { }
}
export class IsHTMLService {
static addEventListener(
is_html: game.IsHTML,
selector: string,
event: string,
callback: EventListener
) {
const el = document.querySelector(`${is_html._selector} ${selector}`);
el.addEventListener(event, callback);
}
static SetHTML(is_html: game.IsHTML, html: string) {
document.querySelector(is_html._selector).innerHTML = html;
}
static CreateElement(tag, args, ...children) {
const IsHTMLEvents = (window as any).IsHTMLEvents || {};
const stringed_args = Object.keys(args || [])
.map(key => {
let value = args[key];
if (typeof value == "function" && key.indexOf("on") == 0) {
const callbackId = "_" + getRandomID();
IsHTMLEvents[callbackId] = value;
value = `IsHTMLEvents.${callbackId}(event)`;
} else if (typeof value == "object") {
value = JSON.stringify(value);
}
return `${key}="${value}"`;
})
.join(" ");
(window as any).IsHTMLEvents = IsHTMLEvents;
return `<${tag} ${stringed_args}>
${children
.map(child => {
if (Array.isArray(child)) {
return child.join("\n");
} else {
return child;
}
})
.join("\n")}
</${tag}>`;
}
}
}
/*
This is a hack to work around https://forum.unity.com/threads/bug-renderer-fails-to-take-into-account-screen-dpi.601087/
Instructions:
Put the following code in a Behavior / System which gets included in your game.
Place the code NEXT to your namespace definition i.e...
(function HDPI_Hacks_By_abeisgreat() {
...
})();
namespace game {
...
}
It should not go inside the namespace.
*/
(function HDPI_Hacks_By_abeisgreat() {
const w = window as any;
const initialize_hack = () => {
console.log("Initializing HDPI hacks v6 by @abeisgreat");
const fakeMouseEventFn = ev => {
const ut_HTML = w.ut._HTML;
const fakeEvent = {
type: ev.type,
pageX: ev.pageX * window.devicePixelRatio,
pageY: ev.pageY * window.devicePixelRatio,
preventDefault: () => {},
stopPropagation: () => {}
};
ut_HTML.mouseEventFn(fakeEvent);
ev.preventDefault();
ev.stopPropagation();
};
const fakeTouchEventFn = ev => {
const ut_HTML = w.ut._HTML;
const changedTouches = [];
for (var index = 0; index < ev.changedTouches.length; index++) {
const touch = ev.changedTouches[index];
changedTouches.push({
identifier: touch.identifier,
pageX: touch.pageX * window.devicePixelRatio,
pageY: touch.pageY * window.devicePixelRatio
});
}
const fakeEvent = {
type: ev.type,
changedTouches,
preventDefault: () => {},
stopPropagation: () => {}
};
ut_HTML.touchEventFn(fakeEvent);
ev.preventDefault();
ev.stopPropagation();
};
window.addEventListener("resize", function() {
const ut = w.ut;
ut._HTML.onDisplayUpdated(
window.innerWidth * window.devicePixelRatio,
window.innerHeight * window.devicePixelRatio,
window.screen.width * window.devicePixelRatio,
window.screen.height * window.devicePixelRatio,
-1
);
ut._HTML.canvasElement.style.width = `${window.innerWidth}px`;
ut._HTML.canvasElement.style.height = `${window.innerHeight}px`;
ut._HTML.stopResizeListening();
const mouseEvents = ["down", "move", "up"];
const touchEvents = ["touch", "cancel", "move", "start"];
mouseEvents.forEach(type => {
const eventType = `mouse${type}`;
ut._HTML.canvasElement.removeEventListener(eventType, fakeMouseEventFn);
ut._HTML.canvasElement.addEventListener(eventType, fakeMouseEventFn);
});
touchEvents.forEach(type => {
const eventType = `touch${type}`;
ut._HTML.canvasElement.removeEventListener(eventType, fakeTouchEventFn);
ut._HTML.canvasElement.addEventListener(eventType, fakeTouchEventFn);
});
setTimeout(function() {
mouseEvents.forEach(type => {
ut._HTML.canvasElement.removeEventListener(
`mouse${type}`,
ut._HTML.mouseEventFn
);
});
touchEvents.forEach(type => {
ut._HTML.canvasElement.removeEventListener(
`touch${type}`,
ut._HTML.touchEventFn
);
});
}, 100);
});
window.dispatchEvent(new Event("resize"));
};
const intervalID = setInterval(() => {
const w = window as any;
const ut = w.ut;
if (ut._HTML.canvasElement && w.known_ut_HTML !== ut._HTML) {
w.known_ut_HTML = ut._HTML;
clearInterval(intervalID);
initialize_hack();
}
}, 10);
})();