-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSACharacterStatusView.m
More file actions
242 lines (195 loc) · 7.91 KB
/
DSACharacterStatusView.m
File metadata and controls
242 lines (195 loc) · 7.91 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
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
/*
Project: DSA-SpielHelfer
Copyright (C) 2025 Free Software Foundation
Author: Sebastian Reitenbach
Created: 2025-02-21 20:57:22 +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 "DSACharacterStatusView.h"
#import "Utils.h"
@implementation DSACharacterStatusView {
NSArray *_colors;
NSArray *_stateKeys;
NSInteger _hoveredCircleIndex;
CGFloat _dotSize;
CGFloat _minSpacing;
CGFloat _startX;
CGFloat _startY;
}
- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
_hoveredCircleIndex = -1;
[self addTrackingRect:self.bounds owner:self userData:NULL assumeInside:NO];
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
//[self registerForDraggedTypes:@[]]; // Register for tooltips here! This makes that all elements are empty...
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
if (!self.character) {
return;
}
_stateKeys = @[
@(DSACharacterStateWounded),
@(DSACharacterStateSick),
@(DSACharacterStateDrunken),
@(DSACharacterStatePoisoned),
@(DSACharacterStateDead),
@(DSACharacterStateUnconscious),
@(DSACharacterStateSpellbound)
];
NSMutableArray *colorsMutable = [NSMutableArray array];
for (NSNumber *stateKey in _stateKeys) {
id stateValue = [self.character.statesDict objectForKey:stateKey];
NSColor *color;
if ([stateKey isEqualToNumber:@(DSACharacterStatePoisoned)] ||
[stateKey isEqualToNumber:@(DSACharacterStateDead)] ||
[stateKey isEqualToNumber:@(DSACharacterStateUnconscious)] ||
[stateKey isEqualToNumber:@(DSACharacterStateSpellbound)]) {
color = [Utils colorForBooleanState:[stateValue boolValue]];
} else {
color = [Utils colorForDSASeverity:[stateValue integerValue]];
}
[colorsMutable addObject:color];
}
_colors = [colorsMutable copy];
NSUInteger dotCount = _colors.count;
_minSpacing = 5.0;
CGFloat availableWidth = dirtyRect.size.width - (_minSpacing * (dotCount - 1));
_dotSize = MIN(availableWidth / dotCount, dirtyRect.size.height - 10.0);
CGFloat totalContentWidth = (_dotSize * dotCount) + (_minSpacing * (dotCount - 1));
_startX = (dirtyRect.size.width - totalContentWidth) / 2;
_startY = (dirtyRect.size.height - _dotSize) / 2;
CGFloat x = _startX;
CGFloat y = _startY;
for (NSColor *color in _colors) {
NSBezierPath *circle = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(x, y, _dotSize, _dotSize)];
[color setFill];
[circle fill];
x += _dotSize + _minSpacing;
}
// [self registerForDraggedTypes:@[]]; // Register for tooltips here! This makes the UI stuck
}
- (void)setCharacter:(DSACharacter *)character {
_character = character;
[self setNeedsDisplay:YES];
[self displayIfNeeded];
}
- (void)mouseMoved:(NSEvent *)event {
NSPoint mouseLocation = [self convertPoint:[event locationInWindow] fromView:nil];
NSInteger hoveredIndex = -1;
CGFloat x = _startX;
for (NSInteger i = 0; i < _colors.count; i++) {
NSRect circleRect = NSMakeRect(x, _startY, _dotSize, _dotSize);
if (NSPointInRect(mouseLocation, circleRect)) {
hoveredIndex = i;
break;
}
x += _dotSize + _minSpacing;
}
if (hoveredIndex != _hoveredCircleIndex) {
_hoveredCircleIndex = hoveredIndex;
[self setToolTip:[self tooltipForIndex:hoveredIndex]];
}
}
- (NSString *)tooltipForIndex:(NSInteger)index {
if (index == -1) {
return nil;
}
NSNumber *stateKey = _stateKeys[index];
NSString *stateName;
switch ([stateKey integerValue]) {
case DSACharacterStateWounded:
stateName = @"Wounded";
break;
case DSACharacterStateSick:
stateName = @"Sick";
break;
case DSACharacterStateDrunken:
stateName = @"Drunken";
break;
case DSACharacterStatePoisoned:
stateName = @"Poisoned";
break;
case DSACharacterStateDead:
stateName = @"Dead";
break;
case DSACharacterStateUnconscious:
stateName = @"Unconscious";
break;
case DSACharacterStateSpellbound:
stateName = @"Spellbound";
break;
default:
stateName = @"Unknown";
break;
}
return stateName;
}
- (void)mouseExited:(NSEvent *)event {
_hoveredCircleIndex = -1;
[self setToolTip:nil];
}
@end
/*
@implementation DSACharacterStatusView
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
NSLog(@"DSACharacterStatusView drawRect %@", [self.character.statesDict objectForKey:@(DSACharacterStateDead)]);
if (!self.character) {
return; // Avoid drawing if character is not set
}
NSArray *colors = @[
[Utils colorForDSASeverity:[[self.character.statesDict objectForKey:@(DSACharacterStateWounded)] integerValue]],
[Utils colorForDSASeverity:[[self.character.statesDict objectForKey:@(DSACharacterStateSick)] integerValue]],
[Utils colorForDSASeverity:[[self.character.statesDict objectForKey:@(DSACharacterStateDrunken)] integerValue]],
[Utils colorForBooleanState:[[self.character.statesDict objectForKey:@(DSACharacterStatePoisoned)] boolValue]],
[Utils colorForBooleanState:[[self.character.statesDict objectForKey:@(DSACharacterStateDead)] boolValue]],
[Utils colorForBooleanState:[[self.character.statesDict objectForKey:@(DSACharacterStateUnconscious)] boolValue]],
[Utils colorForBooleanState:[[self.character.statesDict objectForKey:@(DSACharacterStateSpellbound)] boolValue]]
];
for (NSInteger i = 0; i < colors.count; i++) {
NSLog(@"Dot %ld Color: %@", (long)i, colors[i]);
}
NSUInteger dotCount = colors.count;
CGFloat minSpacing = 5.0;
// Get the available width
CGFloat availableWidth = dirtyRect.size.width - (minSpacing * (dotCount - 1));
// Calculate the maximum possible dot size
CGFloat maxDotSize = availableWidth / dotCount;
// Ensure dots do not exceed the view height
CGFloat dotSize = MIN(maxDotSize, dirtyRect.size.height - 10.0); // Leave some padding at the top/bottom
// Calculate the total width occupied by dots including spacing
CGFloat totalContentWidth = (dotSize * dotCount) + (minSpacing * (dotCount - 1));
// Compute starting X position to center dots horizontally
CGFloat x = (dirtyRect.size.width - totalContentWidth) / 2;
CGFloat y = (dirtyRect.size.height - dotSize) / 2; // Center vertically
for (NSColor *color in colors) {
NSBezierPath *circle = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(x, y, dotSize, dotSize)];
[color setFill];
[circle fill];
x += dotSize + minSpacing;
}
}
- (void)setCharacter:(DSACharacter *)character {
_character = character;
NSLog(@"DSACharacterStatusView THE CHARACTER STATES DICT: %@", character.statesDict);
// Ensure the view updates once character is assigned
[self setNeedsDisplay:YES];
[self displayIfNeeded];
}
@end */