-
Notifications
You must be signed in to change notification settings - Fork 0
/
SKButtonNode.cs
53 lines (45 loc) · 1.41 KB
/
SKButtonNode.cs
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
using System;
using SpriteKit;
using UIKit;
namespace Gladius
{
public class SKButtonNode
{
public static SKNode WithImage(string imageName, Action actionOnTouch) {
return new SKSpriteButtonNode(imageName, actionOnTouch);
}
public static SKNode WithText(string labelText, Action actionOnTouch) {
return new SKLabelButtonNode(labelText, actionOnTouch);
}
}
public class SKSpriteButtonNode : SKSpriteNode
{
private readonly Action _actionOnTouch;
public SKSpriteButtonNode(string imageName, Action actionOnTouch) : base(SKTexture.FromImageNamed(imageName)) {
_actionOnTouch = actionOnTouch;
UserInteractionEnabled = true;
}
public override void TouchesBegan(Foundation.NSSet touches, UIEvent evt) {
var touch = touches.AnyObject as UITouch;
if (ContainsPoint(touch.LocationInNode(Parent)))
_actionOnTouch();
}
}
public class SKLabelButtonNode : SKLabelNode
{
private readonly Action _actionOnTouch;
public SKLabelButtonNode(string labelText, Action actionOnTouch) {
Text = labelText;
FontColor = UIColor.Black;
FontSize = 10;
HorizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left;
_actionOnTouch = actionOnTouch;
UserInteractionEnabled = true;
}
public override void TouchesBegan(Foundation.NSSet touches, UIEvent evt) {
var touch = touches.AnyObject as UITouch;
if (ContainsPoint(touch.LocationInNode(Parent)))
_actionOnTouch();
}
}
}