SameBoy | Accurate GB/GBC emulator |
| download: https://git.y1.nz/archives/sameboy.tar.gz | |
| README | Files | Log | Refs | LICENSE |
Cocoa/GBPaletteView.m
1 #import "GBPaletteView.h"
2
3 @interface GBPaletteViewItem : NSObject
4 @property IBOutlet NSView *view;
5 @property (strong) IBOutlet NSTextField *label;
6 @property (strong) IBOutlet NSTextField *color0;
7 @property (strong) IBOutlet NSTextField *color1;
8 @property (strong) IBOutlet NSTextField *color2;
9 @property (strong) IBOutlet NSTextField *color3;
10 @end
11
12 @implementation GBPaletteViewItem
13 @end
14
15 @implementation GBPaletteView
16 {
17 NSMutableArray<NSTextField *> *_colors;
18 }
19
20 - (instancetype)initWithCoder:(NSCoder *)coder
21 {
22 self = [super initWithCoder:coder];
23 _colors = [NSMutableArray array];
24 CGFloat height = self.frame.size.height;
25 for (unsigned i = 0; i < 16; i++) {
26 GBPaletteViewItem *item = [[GBPaletteViewItem alloc] init];
27 [[NSBundle mainBundle] loadNibNamed:@"GBPaletteViewRow" owner:item topLevelObjects:nil];
28 [self addSubview:item.view];
29 [item.view setFrameOrigin:NSMakePoint(0, height - (i * 24) - 24)];
30 item.label.stringValue = [NSString stringWithFormat:@"%@ %d", i < 8? @"Background" : @"Object", i % 8];
31 item.view.autoresizingMask = NSViewMaxXMargin | NSViewMinYMargin;
32 [_colors addObject:item.color0];
33 [_colors addObject:item.color1];
34 [_colors addObject:item.color2];
35 [_colors addObject:item.color3];
36
37 }
38 return self;
39 }
40
41 - (void)reloadData:(Document *)document
42 {
43 GB_gameboy_t *gb = document.gb;
44 uint8_t *bg = GB_get_direct_access(gb, GB_DIRECT_ACCESS_BGP, NULL, NULL);
45 uint8_t *obj = GB_get_direct_access(gb, GB_DIRECT_ACCESS_OBP, NULL, NULL);
46 NSFont *font = [document debuggerFontOfSize:13];
47
48 for (unsigned i = 0; i < 4 * 8 * 2; i++) {
49 uint8_t index = i % (4 * 8);
50 uint8_t *palette = i >= 4 * 8 ? obj : bg;
51 uint16_t color = (palette[(index << 1) + 1] << 8) | palette[(index << 1)];
52 uint32_t nativeColor = GB_convert_rgb15(gb, color, false);
53
54 uint8_t r = color & 0x1F,
55 g = (color >> 5) & 0x1F,
56 b = (color >> 10) & 0x1F;
57
58 NSTextField *field = _colors[i];
59 field.stringValue = [NSString stringWithFormat:@"$%04X", color];
60 field.textColor = r * 3 + g * 4 + b * 2 > 120? [NSColor blackColor] : [NSColor whiteColor];
61 field.toolTip = [NSString stringWithFormat:@"Red: %d, Green: %d, Blue: %d", r, g, b];
62 field.font = font;
63 field.backgroundColor = [NSColor colorWithRed:(nativeColor & 0xFF) / 255.0
64 green:((nativeColor >> 8) & 0xFF) / 255.0
65 blue:((nativeColor >> 16) & 0xFF) / 255.0
66 alpha:1.0];
67 }
68 }
69
70 - (void)drawRect:(NSRect)dirtyRect
71 {
72 NSRect frame = self.frame;
73 if (@available(macOS 10.14, *)) {
74 [[NSColor alternatingContentBackgroundColors].lastObject setFill];
75 }
76 else {
77 [[NSColor colorWithDeviceWhite:0.96 alpha:1] setFill];
78 }
79 for (unsigned i = 1; i <= 8; i++) {
80 NSRectFill(NSMakeRect(0, frame.size.height - i * 24 * 2, frame.size.width, 24));
81 }
82
83 if (@available(macOS 10.14, *)) {
84 [[NSColor alternatingContentBackgroundColors].firstObject setFill];
85 }
86 else {
87 [[NSColor controlBackgroundColor] setFill];
88 }
89 for (unsigned i = 0; i < 8; i++) {
90 NSRectFill(NSMakeRect(0, frame.size.height - i * 24 * 2 - 24, frame.size.width, 24));
91 }
92 }
93 @end
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.