Button

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
 +
Obsessively create a hierarchical menu from a directory listing:
 +
 
<c>
 
<c>
// Use this script on a guiTexture object for regular push buttons
+
- (void)addItemsFromPath:(NSString*)path toMenu:(NSMenu*)menu
// that behave properly like Mac OS X.
+
 
+
var normalMaterial : Material;
+
var hoverMaterial : Material;
+
var pressedMaterial : Material;
+
var messagee : GameObject;
+
var message = "ButtonPress";
+
 
+
private var state = 0;
+
 
+
function OnMouseEnter()
+
 
{
 
{
state++;
+
NSArray *puzzles;
if (state == 1)
+
NSString *puzz;
guiText.material = hoverMaterial;
+
NSMenuItem *item;
}
+
BOOL dir;
 
+
int i;
function OnMouseDown()
+
{
+
puzzles = [[NSFileManager defaultManager] directoryContentsAtPath:[puzzlePath stringByAppendingString:path]];
state++;
+
for(i = 0; i < [puzzles count]; i++) {
if (state == 2)
+
puzz = [puzzles objectAtIndex:i];
guiText.material = pressedMaterial;
+
if([[puzz substringFromIndex:[puzz length]-4] isEqualToString:@".tsu"]) {
}
+
boardData = [NSString stringWithContentsOfFile:[puzzlePath stringByAppendingString:[path stringByAppendingString:puzz]]];
 
+
[self getBoardSize];
function OnMouseUp()
+
item = [[[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"%@ (%d, %d)", puzz, boardwidth, boardheight] action:@selector(loadLevelFromMenu:) keyEquivalent:@""] autorelease];
{
+
[item setTarget:self];
if (state == 2)
+
[item setRepresentedObject:[[path stringByAppendingString:@"/"] stringByAppendingString:puzz]];
{
+
[menu addItem:item];
state--;
+
}
if (messagee)
+
else {
messagee.SendMessage(message, gameObject);
+
[[NSFileManager defaultManager] fileExistsAtPath:[[puzzlePath stringByAppendingString:path] stringByAppendingString:puzz] isDirectory:&dir];
 +
if(dir) {
 +
NSMenu *submenu = [[NSMenu alloc] initWithTitle:@""];
 +
item = [[[NSMenuItem alloc] initWithTitle:puzz action:NULL keyEquivalent:@""] autorelease];
 +
[self addItemsFromPath:[[path stringByAppendingString:puzz] stringByAppendingString:@"/"] toMenu:submenu];
 +
[item setSubmenu:submenu];
 +
[menu addItem:item];
 +
}
 +
}
 
}
 
}
else
 
{
 
state --;
 
if (state < 0)
 
state = 0;
 
}
 
guiText.material = normalMaterial;
 
 
}
 
}
 
function OnMouseExit()
 
{
 
if (state > 0)
 
state--;
 
if (state == 0)
 
guiText.material = normalMaterial;
 
}
 
 
 
</c>
 
</c>

Revision as of 01:44, 1 December 2005

Obsessively create a hierarchical menu from a directory listing:

<c> - (void)addItemsFromPath:(NSString*)path toMenu:(NSMenu*)menu { NSArray *puzzles; NSString *puzz; NSMenuItem *item; BOOL dir; int i;

puzzles = [[NSFileManager defaultManager] directoryContentsAtPath:[puzzlePath stringByAppendingString:path]]; for(i = 0; i < [puzzles count]; i++) { puzz = [puzzles objectAtIndex:i]; if([[puzz substringFromIndex:[puzz length]-4] isEqualToString:@".tsu"]) { boardData = [NSString stringWithContentsOfFile:[puzzlePath stringByAppendingString:[path stringByAppendingString:puzz]]]; [self getBoardSize]; item = [[[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"%@ (%d, %d)", puzz, boardwidth, boardheight] action:@selector(loadLevelFromMenu:) keyEquivalent:@""] autorelease]; [item setTarget:self]; [item setRepresentedObject:[[path stringByAppendingString:@"/"] stringByAppendingString:puzz]]; [menu addItem:item]; } else { [[NSFileManager defaultManager] fileExistsAtPath:[[puzzlePath stringByAppendingString:path] stringByAppendingString:puzz] isDirectory:&dir]; if(dir) { NSMenu *submenu = [[NSMenu alloc] initWithTitle:@""]; item = [[[NSMenuItem alloc] initWithTitle:puzz action:NULL keyEquivalent:@""] autorelease]; [self addItemsFromPath:[[path stringByAppendingString:puzz] stringByAppendingString:@"/"] toMenu:submenu]; [item setSubmenu:submenu]; [menu addItem:item]; } } } } </c>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox