-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSAAdventureGenerationController.m
More file actions
138 lines (106 loc) · 4.94 KB
/
DSAAdventureGenerationController.m
File metadata and controls
138 lines (106 loc) · 4.94 KB
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
/*
Project: DSA-SpielHelfer
Copyright (C) 2025 Free Software Foundation
Author: Sebastian Reitenbach
Created: 2025-03-24 20:33:28 +0100 by sebastia
This application is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This application is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#import "DSAAdventureGenerationController.h"
#import "DSALocations.h"
@implementation DSAAdventureGenerationController
- (instancetype)init
{
self = [super initWithWindowNibName:@"DSAAdventureGeneration"];
if (self)
{
}
return self;
}
- (void) startAdventureGeneration:(id)sender {
NSLog(@"DSAAdventureGenerationController: startAdventureGeneration called");
// Load available locations
DSALocations *locations = [DSALocations sharedInstance];
NSLog(@"DSAAdventureGenerationController startAdventureGeneration before getting location names");
self.locationsArray = [locations locationNamesWithTemples];
NSLog(@"DSAAdventureGenerationController startAdventureGeneration after getting location names");
self.filteredLocations = self.locationsArray;
// OK & Cancel buttons
[self.okButton setEnabled: NO];
[[self window] makeKeyAndOrderFront: self];
//NSLog(@"DSAAdventureGenerationController startAdventureGeneration finished");
}
- (void) windowDidLoad
{
[super windowDidLoad];
self.locationField.usesDataSource = YES;
self.locationField.delegate = self;
self.locationField.dataSource = self;
//NSLog(@"DSAAdventureGenerationController: windowDidLoad locationField: %@", self.locationField);
}
- (void)handleOKButton:(NSButton *)sender {
NSString *selectedLocation = self.locationField.stringValue;
if ([self.filteredLocations containsObject:selectedLocation]) { // Ensure it's a valid selection
if (self.completionHandler) {
self.completionHandler(selectedLocation);
}
//NSLog(@"DSAAdventureGenerationController handleOKButton before closing window");
[sender.window close]; // Close panel
}
}
- (void)handleCancelButton:(NSButton *)sender {
if (self.completionHandler) {
self.completionHandler(nil);
}
[sender.window close]; // Close panel
}
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)comboBox {
//NSLog(@"DSAAdventureGenerationController numberOfItemsInComboBox called");
return self.filteredLocations.count;
}
- (id)comboBox:(NSComboBox *)comboBox objectValueForItemAtIndex:(NSInteger)index {
//NSLog(@"DSAAdventureGenerationController objectValueForItemAtIndex called");
return self.filteredLocations[index];
}
- (void)comboBoxWillDismiss:(NSNotification *)notification {
//NSLog(@"DSAAdventureGenerationController comboBoxWillDismiss called");
NSString *text = [self.locationField stringValue];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", text];
self.filteredLocations = [self.locationsArray filteredArrayUsingPredicate:predicate];
[self.locationField reloadData];
}
- (void)comboBoxSelectionDidChange:(NSNotification *)notification {
//NSLog(@"DSAAdventureGenerationController comboBoxSelectionDidChange called");
NSInteger selectedIndex = [self.locationField indexOfSelectedItem];
if (selectedIndex >= 0) {
NSString *selectedItem = self.filteredLocations[selectedIndex]; // Get from filtered list
//NSLog(@"comboBoxSelectionDidChange called: %@", selectedItem);
// Ensure OK button is updated based on selection
BOOL isValid = [self.locationsArray containsObject:selectedItem];
[self.okButton setEnabled:isValid];
}
}
- (void)controlTextDidChange:(NSNotification *)notification {
NSString *input = [self.locationField stringValue];
//DSALocations *locations = [DSALocations sharedInstance];
// Filter locations
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@", input];
self.filteredLocations = [self.locationsArray filteredArrayUsingPredicate:predicate];
// Refresh the combo box data
[self.locationField reloadData];
[self.locationField noteNumberOfItemsChanged]; // Ensure UI refresh
// Enable OK button only if input exactly matches a known location
//NSLog(@"controlTextDidChange: location: %@ ARRAY: %@", input, self.locationsArray);
BOOL isValid = [self.locationsArray containsObject:input];
[self.okButton setEnabled:isValid];
}
@end