/
SJMessageNode.m
119 lines (95 loc) · 3.07 KB
/
SJMessageNode.m
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
//
// SJMessageNode.m
// SJRolePlaying
//
// Created by Tatsuya Tobioka on 2013/10/31.
// Copyright (c) 2013年 tnantoka. All rights reserved.
//
#import "SJMessageNode.h"
#import "SJComponents.h"
static const CGFloat MARGIN = 10.0f;
static const CGFloat PADDING = 10.0f;
static const CGFloat LINE_HEIGHT = 18.0f;
static const CGFloat FONT_SIZE = 16.0f;
NSString * const kMessageName = @"message";
@implementation SJMessageNode {
CGSize _size;
NSMutableArray *_labels;
NSInteger _lines;
NSInteger _chars;
NSInteger _page;
NSInteger _pages;
}
- (id)initWithSize:(CGSize)size {
if (self = [super init]) {
_size = size;
[self createNodeContents];
}
return self;
}
- (void)createNodeContents {
self.name = kMessageName;
SKShapeNode *boxNode = [[SKShapeNode alloc] init];
CGMutablePathRef path = CGPathCreateMutable();
CGRect rect = CGRectMake(MARGIN, MARGIN, _size.width - MARGIN * 2.0f, _size.height - MARGIN * 2.0f);
CGPathAddRoundedRect(path, NULL, rect, 1.0f, 1.0f);
//CGPathAddRect(path, NULL, rect);
boxNode.path = path;
boxNode.lineWidth = 1.0f;
boxNode.fillColor = [SKColor blueColor];
boxNode.strokeColor = [SKColor whiteColor];
boxNode.glowWidth = 0;
boxNode.alpha = 0.7f;
[self addChild:boxNode];
CGPathRelease(path);
_lines = round((rect.size.height - PADDING * 2.0f) / LINE_HEIGHT);
_chars = floor((rect.size.width - PADDING * 2.0f) / FONT_SIZE);
_labels = @[].mutableCopy;
for (int i = 0; i < _lines; i++) {
SKLabelNode *labelNode = [SKLabelNode labelNodeWithFontNamed:FONT_NORMAL];
labelNode.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
labelNode.fontSize = FONT_SIZE;
CGPoint position = CGPointMake(rect.origin.x + PADDING, rect.origin.y + PADDING + LINE_HEIGHT * i);
if (i == 0) {
position.x += FONT_SIZE * (_chars - 1);
}
labelNode.position = position;
[self addChild:labelNode];
[_labels insertObject:labelNode atIndex:0];
if (i == 0) {
}
}
self.message = @" "; // For performance
}
- (void)setMessage:(NSString *)message {
_message = message;
_page = -1;
_pages = message.length / _chars / (_lines - 1);
[self next];
}
- (void)next {
if (![self hasNext]) {
return;
}
_page++;
for (SKLabelNode *labelNode in _labels) {
labelNode.text = @"";
}
for (int i = 0; i < _lines - 1; i++) {
NSInteger loc = i *_chars + (_page * (_lines - 1)) * _chars;
NSInteger len = loc + _chars < _message.length ? _chars : _message.length - loc;
NSRange range = NSMakeRange(loc, len);
NSString *m = [_message substringWithRange:range];
[(SKLabelNode *)_labels[i] setText:m];
if (range.location + range.length >= _message.length) {
break;
}
}
if ([self hasNext]) {
[(SKLabelNode *)_labels.lastObject setText:@"▼"];
}
}
- (BOOL)hasNext {
return _page < _pages ;
}
@end