/
OnCanvasElementNode.hx
116 lines (102 loc) · 2.74 KB
/
OnCanvasElementNode.hx
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
package armory.logicnode;
import armory.trait.internal.CanvasScript;
import iron.Scene;
#if arm_ui
import armory.ui.Canvas.Anchor;
#end
class OnCanvasElementNode extends LogicNode {
var canvas: CanvasScript;
var element: String;
/**
* The event type this node should react to, can be "click" or "hover".
*/
public var property0: String;
/**
* If the event type is click, this property states whether to check for
* "down", "started" or "released" events.
*/
public var property1: String;
/**
* The mouse button that this node should react to. Only used when listening
* for mouse clicks.
*/
public var property2: String;
public function new(tree: LogicTree) {
super(tree);
tree.notifyOnUpdate(update);
}
#if arm_ui
function update() {
element = inputs[0].get();
// Ensure canvas is ready
if(!Scene.active.ready) return;
canvas = Scene.active.getTrait(CanvasScript);
if (canvas == null) canvas = Scene.active.camera.getTrait(CanvasScript);
if(canvas == null) return;
if (!canvas.ready) return;
if(canvas.getElement(element) == null) return;
if(canvas.getElement(element).visible == false) return;
var mouse = iron.system.Input.getMouse();
var isEvent = false;
if (property0 == "click") {
switch (property1) {
case "started":
isEvent = mouse.started(property2);
case "down":
isEvent = mouse.down(property2);
case "released":
isEvent = mouse.released(property2);
}
}
// Hovered
else {
isEvent = true;
}
if (isEvent)
{
var canvasElem = canvas.getElement(element);
var left = canvasElem.x*canvas.getUiScale();
var top = canvasElem.y*canvas.getUiScale();
var right = left + canvasElem.width*canvas.getUiScale();
var bottom = top + canvasElem.height*canvas.getUiScale();
var anchor = canvasElem.anchor;
var cx = canvas.getCanvas().width;
var cy = canvas.getCanvas().height;
var mouseX = mouse.x;
var mouseY = mouse.y;
switch(anchor)
{
case Top:
mouseX -= cx/2 - canvasElem.width/2;
case TopRight:
mouseX -= cx - canvasElem.width;
case CenterLeft:
mouseY -= cy/2 - canvasElem.height/2;
case Anchor.Center:
mouseX -= cx/2 - canvasElem.width/2;
mouseY -= cy/2 - canvasElem.height/2;
case CenterRight:
mouseX -= cx - canvasElem.width;
mouseY -= cy/2 - canvasElem.height/2;
case BottomLeft:
mouseY -= cy - canvasElem.height;
case Bottom:
mouseX -= cx/2 - canvasElem.width/2;
mouseY -= cy - canvasElem.height;
case BottomRight:
mouseX -= cx - canvasElem.width;
mouseY -= cy - canvasElem.height;
}
if((mouseX >= left) && (mouseX <= right))
{
if((mouseY >= top) && (mouseY <= bottom))
{
runOutput(0);
}
}
}
}
#else
function update() {}
#end
}