forked from JohnSundell/SuperSpriteKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSKButtonNode.h
363 lines (325 loc) · 11.7 KB
/
SSKButtonNode.h
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#import <SpriteKit/SpriteKit.h>
#import "SSKMultiplatform.h"
#import "SSKInteractionHandler.h"
#import "SSKStretchableNode.h"
#pragma mark - Enums
/**
* Enum describing various states a SSKButton instance can be in
*/
typedef enum : NSUInteger {
/**
* The normal state, when the button is idle and unselected
*/
SSKButtonStateNormal,
/**
* The state that the button will be in when the user is currently
* interacting with it.
*/
SSKButtonStateHighlighted,
/**
* The state the button will be in when it's currently selected.
* See SSKButtonSelectionStyle for more information about selection.
*/
SSKButtonStateSelected,
/**
* The state the button will be in when it has been disabled when
* setting the "enabled" property to NO.
*/
SSKButtonStateDisabled
} SSKButtonState;
/**
* Enum describing various selection styles a SSKButton instance can use
*/
typedef enum : NSUInteger {
/**
* This style causes the button never to appear in a selected state
* visually. Actions will still be run for the SSKButtonStateSelected
* state though. Whenever selected, the button will simply revert to
* its normal state as soon as interactions on it stopped.
*/
SSKButtonSelectionStyleNone,
/**
* This style will cause the button to remain selected once selected
* by a user interaction. The "selected" property on the button must
* be programmatically set to NO, before the button will return to its
* normal state, or the "state" property set to another value.
*/
SSKButtonSelectionStyleRemainSelected,
/**
* This style will cause the button to toggle between its selected
* and normal state whenever a user interaction ended on it.
*/
SSKButtonSelectionStyleToggle
} SSKButtonSelectionStyle;
#pragma mark - SSKButtonNode
/**
* A node that acts like a button-type control, well suited for
* in-game user interfaces
*
* @discussion It's API is heavily inspired by UI/NSButton.
*
* @note To be able to respond to user interactions, SSKButtonNode
* requires the SKView it's being displayed in to have an SSKInteractionHandler
* attached to it. For more information about interaction handling in
* SuperSpriteKit, see SSKInteractionHandler.
*/
@interface SSKButtonNode : SKNode <SSKInteractiveNode>
/**
* The size of the button
*/
@property (nonatomic) CGSize size;
/**
* The selection style of the button
*
* @discussion See SSKButtonSelectionStyle for more information.
*
* The selection style will cause the button to behave differently
* when moving in between different states as the user interacts
* with it.
*
* Regardless of selection style, the button will always go to
* the SSKButtonStateHighlighted whenever a user interaction
* is occuring on it.
*/
@property (nonatomic) SSKButtonSelectionStyle selectionStyle;
/**
* The current state of the button
*
* @discussion See SSKButtonState for more information.
*
* The state of the button is automatically set as the user interacts
* with it, depending on the chosen selection style. The value of this
* property may also be set programmatically to change the button's state.
*/
@property (nonatomic) SSKButtonState state;
/**
* Whether the button is currently enabled
*
* @discussion Setting this to NO will cause the button to go to the
* SSKButtonStateDisabled state. Setting it back to YES after that will
* cause the button to go to the SSKButtonStateNormal state.
*
* Programmatically setting the button's state to SSKButtonStateDisabled
* will not affect the value of this property.
*/
@property (nonatomic, getter = isEnabled) BOOL enabled;
/**
* Whether the button is currently selected
*
* @discussion The value of this property is automatically changed as
* the button moves in between different states according to its selection style.
*
* Setting this to YES will cause the button to go to the SSKButtonStateSelected
* state. Setting it to NO will cause the button to go the SSKButtonStateNormal state.
*/
@property (nonatomic, getter = isSelected) BOOL selected;
/**
* The margin between the button's icon and title if they both exist
*/
@property (nonatomic) CGFloat iconLabelMargin;
/**
* The label node used to draw the button's title
*/
@property (nonatomic, strong, readonly) SKLabelNode *titleLabelNode;
/**
* Allocate & initialize an instance of SSKButtonNode
*
* @param size The size the button should have
*/
+ (instancetype)buttonNodeWithSize:(CGSize)size;
#pragma mark Targets & actions
/**
* Add a target & action that should be called when the button enters a state
*
* @param target The target to send a message to
* @param action The message to send to the target
* @param state The state to trigger this action for
*/
- (void)addTarget:(id)target
action:(SEL)action
forState:(SSKButtonState)state;
/**
* Remove a target for a state
*
* @param target The target to remove
* @param state The state for which to remove the target
*/
- (void)removeTarget:(id)target
forState:(SSKButtonState)state;
#pragma mark Backgrounds
/**
* Return the background color for a state
*
* @param state The state to get the background color for
*
* @return An SKColor instance representing the background
* color for the specified state, if such a definition exists.
*
* Nil will be returned if no definition exists at all, and
* an instance of NSNull will be returned if nil or NSNull has
* been specified as the color for the state.
*/
- (SKColor *)backgroundColorForState:(SSKButtonState)state;
/**
* Set the background color for a state
*
* @param color The background color to set
* @param state The state to set the background color for
*
* @discussion The background color for a state will only be used if
* that state does not have a background texture assigned to it.
*
* Whenever a state doesn't have a background color, or a background
* texture assigned to it, the background color or texture (depending
* on what's assigned) for the SSKButtonStateNormal state will be used.
*
* To disable this behavior for a state, set the background color for
* that state to nil or NSNull.
*/
- (void)setBackgroundColor:(SKColor *)color
forState:(SSKButtonState)state;
/**
* Return the background texture for a state
*
* @param state The state to get the background texture for
*
* @return An SKTexture instance representing the background
* texture for the specified state, if such a definition exists.
*
* Nil will be returned if no definition exists at all, and
* an instance of NSNull will be returned if nil or NSNull has
* been specified as the background texture for the state.
*/
- (SKTexture *)backgroundTextureForState:(SSKButtonState)state;
/**
* Set the background texture for a state
*
* @param texture The background texture to set
* @param state The state to set the background texture for
*
* @discussion The button will always use the background texture assigned to
* its current state if set, and fall back to any set background color when it's not.
*
* Whenever a state doesn't have a background texture, or a
* background color assigned to it, the background color or texture (depending
* on what's assigned) for the SSKButtonStateNormal state will be used.
*
* To disable this behavior for a state, set the background texture for
* that state to nil or NSNull.
*/
- (void)setBackgroundTexture:(SKTexture *)texture
forState:(SSKButtonState)state;
/**
* Return the cap insets used to stretch the background texture for a state
*
* @param state The state to get the stretchable background cap insets for
*
* @discussion See the documentation for SSKStretchableNode for an explanation
* on how texture streching works in SuperSpriteKit. The default value for every
* state is an edge insets struct with all of its members set to zero.
*/
- (SSKEdgeInsetsType)stretchableBackgoundCapInsetsForState:(SSKButtonState)state;
/**
* Set the cap insets used to stretch the background texture for a state
*
* @param capInsets The cap insets to use for this state
* @param state The state to set the stretchable background cap insets for
*
* @discussion You only need to use this API if you actually want the button's
* background texture to stretch.
*
* See the documentation for SSKStretchableNode for an explanation
* on how texture streching works in SuperSpriteKit.
*/
- (void)setStretchableBackgroundCapInsets:(SSKEdgeInsetsType)capInsets
forState:(SSKButtonState)state;
#pragma mark Icons & titles
/**
* Return the texture used for the button's icon for a state
*
* @param state The state to get the icon texture for
*
* @return An SKTexture instance representing the icon texture
* for the specified state, if such a definition exists.
*
* Nil will be returned if no definition exists at all, and
* an instance of NSNull will be returned if nil or NSNull has
* been specified as the icon texture for the state.
*/
- (SKTexture *)iconTextureForState:(SSKButtonState)state;
/**
* Set the texture used for the button's icon for a state
*
* @param texture The icon texture to set
* @param state The state to set the icon texture for
*
* Whenever a state doesn't have a icon texture, the icon texture for
* the SSKButtonStateNormal state will be used.
*
* To disable this behavior for a state, set the icon texture for
* that state to nil or NSNull.
*/
- (void)setIconTexture:(SKTexture *)texture
forState:(SSKButtonState)state;
/**
* Return the title for a state
*
* @param state The state to get the title for
*
* @return An NSString instance representing the title for the specified
* state, if such a definition exists.
*
* Nil will be returned if no definition exists at all, and
* an instance of NSNull will be returned if nil or NSNull has
* been specified as the icon texture for the state.
*/
- (NSString *)titleForState:(SSKButtonState)state;
/**
* Set the title for a state
*
* @param title The title to set
* @param state The state to set the title for
*
* Whenever a state doesn't have a title assigned to it, the title for
* the SSKButtonStateNormal state will be used.
*
* To disable this behavior for a state, set the title for
* that state to nil or NSNull.
*/
- (void)setTitle:(NSString *)title
forState:(SSKButtonState)state;
/**
* Return the title offset for a state
*
* @param state The state to get the title offset for
*
* @discussion The default value for every state is an edge insets struct
* with all of its members set to zero, meaning that the button's title
* will be displayed in the center (offset by the icon, if any) of the button.
*/
- (SSKEdgeInsetsType)titleOffsetForState:(SSKButtonState)state;
/**
* Set the title offset for a state
*
* @param offset The offset to set
* @param state The state to set the title edge insets for
*
* @discussion Setting any of the edge insets member to non-zero will push the title
* (regardless of if an icon is displayed or not) in that direction. For example,
* setting the top value of the edge insets to 5, will push the title 5 points down.
*/
- (void)setTitleOffset:(SSKEdgeInsetsType)offset
forState:(SSKButtonState)state;
#pragma mark Layout
/**
* Update the button's layout, according to its layout properties
*
* @discussion You don't have to call this method manually, as it gets
* called every time you update a property that requires the button to
* relayout itself.
*
* You may override this method in any SSKButtonNode subclass to
* apply your own layout to the button.
*/
- (void)updateLayout;
@end