-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse.js
More file actions
91 lines (88 loc) · 3.99 KB
/
mouse.js
File metadata and controls
91 lines (88 loc) · 3.99 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
CLOCKWORKRT.components.register([
{
name: "mouse",
description: "This component allows you to incorporate mouse support to your game, via two point collisions for the mouse hover and click. It also generates click events.",
events: [
{
name: "#setup", code: function (event) {
var manifest = CLOCKWORKRT.API.getManifest();
this.var.w = manifest.screenResolution.w;
this.var.h = manifest.screenResolution.h;
this.var.listener_click = this.do.onclick.bind(this);
this.engine.var["#DOM"].addEventListener("mousedown", this.var.listener_click, false);
this.var.listener_move = this.do.onmove.bind(this);
this.engine.var["#DOM"].addEventListener("mousemove", this.var.listener_move, false);
this.var.listener_up = this.do.mouseup.bind(this);
this.engine.var["#DOM"].addEventListener("mouseup", this.var.listener_up, false);
}
},
{
name: "#exit", code: function (event) {
this.engine.var["#DOM"].removeEventListener("mousedown", this.var.listener_click, false);
this.engine.var["#DOM"].removeEventListener("mousemove", this.var.listener_move, false);
this.engine.var["#DOM"].removeEventListener("mouseup", this.var.listener_up, false);
}
},
{
name: "onclick", code: function (e) {
this.var.timer = -1;
this.engine.do.click({ x: this.var.$x, y: this.var.$y, button: e.button });
}
},
{
name: "onmove", code: function (e) {
var tx = e.offsetX == undefined ? e.layerX : e.offsetX;
var ty = e.offsetY == undefined ? e.layerY : e.offsetY;
//Transform the coordinates to the Spritesheet.js canvas
tx = this.var.w * tx / window.innerWidth;
var ypos = (window.innerHeight - this.var.h * window.innerWidth / this.var.w) / 2;
var height = (this.var.h * window.innerWidth / this.var.w);
ty = this.var.h * (ty - ypos) / height;
var camera = this.engine.getRenderingLibrary().getCamera();
this.var.$x = +tx + camera.x;
this.var.$y = +ty + camera.y;
}
},
{
name: "#loop", code: function (event) {
//We wait one iteration before deleting the click coordinates
if (this.var.timer == 1) {
//Remove clic collision
this.setCollider("click", {
x: NaN,
y: NaN
});
this.var.timer = 0;
}
if (this.var.timer == -1) {
//Set click collision
this.setCollider("click", {
x: 0,
y: 0
});
this.var.timer = 1;
}
}
}
],
collision: {
"point": [
//Coordinates of the pointer
{ "#tag": "hover", "x": 0, "y": 0 },
//Coordinates of the click
{ "#tag": "click", "x": NaN, "y": NaN }
]
},
triggers: [
{
"name": "click",
"description": "This event will be triggered once the mouse is clicked",
"dataSchema": {
"x": "<The x coordinate of the mouse>",
"y": "<The y coordinate of the mouse>",
"button":"<Left button=0, middle button=1, right button=2>"
}
}
]
}
]);