macOS Cocoa improvements.

This commit is contained in:
byuu 2019-07-27 00:52:29 +09:00
parent 3934be89ff
commit cdf4784468
15 changed files with 177 additions and 44 deletions

View File

@ -31,7 +31,7 @@ using namespace nall;
namespace Emulator {
static const string Name = "bsnes";
static const string Version = "107.15";
static const string Version = "107.16";
static const string Author = "byuu";
static const string License = "GPLv3";
static const string Website = "https://byuu.org";

View File

@ -39,7 +39,10 @@ ifeq ($(shell id -un),root)
else ifeq ($(platform),windows)
else ifeq ($(platform),macos)
mkdir -p ~/Library/Application\ Support/$(name)/
mkdir -p ~/Library/Application\ Support/$(name)/Database/
cp -R out/$(name).app /Applications/$(name).app
cp Database/* ~/Library/Application\ Support/$(name)/Database/
cp ../icarus/Database/* ~/Library/Application\ Support/$(name)/Database/
else ifneq ($(filter $(platform),linux bsd),)
mkdir -p $(prefix)/bin/
mkdir -p $(prefix)/share/applications/

View File

@ -22,6 +22,7 @@ auto Program::create() -> void {
presentation.create();
presentation.setVisible();
presentation.viewport.setFocused();
settingsWindow.create();
videoSettings.create();

View File

@ -137,7 +137,7 @@ auto EmulatorSettings::create() -> void {
settings.emulator.hack.fastSuperFX = superFXClock.position() * 10 + 100;
superFXValue.setText({settings.emulator.hack.fastSuperFX, "%"});
}).doChange();
hacksNote.setForegroundColor({224, 0, 0}).setText("Note: some hack setting changes do not take effect until after reloading games.");
hacksNote.setText("Note: some hack setting changes do not take effect until after reloading games.");
}
auto EmulatorSettings::updateConfiguration() -> void {

View File

@ -33,7 +33,7 @@ auto HotkeySettings::reloadMappings() -> void {
mappingList.append(TableViewColumn().setText("Mapping").setExpandable());
for(auto& hotkey : inputManager.hotkeys) {
mappingList.append(TableViewItem()
.append(TableViewCell().setText(hotkey.name).setFont(Font().setBold()).setBackgroundColor({240, 240, 255}))
.append(TableViewCell().setText(hotkey.name).setFont(Font().setBold()))
.append(TableViewCell())
);
}

View File

@ -97,7 +97,7 @@ auto InputSettings::reloadMappings() -> void {
mappingList.append(TableViewColumn().setText("Mapping").setExpandable());
for(auto& mapping : activeDevice().mappings) {
mappingList.append(TableViewItem()
.append(TableViewCell().setText(mapping.name).setFont(Font().setBold()).setBackgroundColor({240, 240, 255}))
.append(TableViewCell().setText(mapping.name).setFont(Font().setBold()))
.append(TableViewCell())
);
}

View File

@ -2,6 +2,13 @@ auto NSMakeColor(const hiro::Color& color) -> NSColor* {
return [NSColor colorWithRed:(color.red() / 255.0) green:(color.green() / 255.0) blue:(color.blue() / 255.0) alpha:(color.alpha() / 255.0)];
}
auto NSMakeCursor(const hiro::MouseCursor& mouseCursor) -> NSCursor* {
if(mouseCursor == MouseCursor::Hand) return [NSCursor pointingHandCursor];
if(mouseCursor == MouseCursor::HorizontalResize) return [NSCursor resizeLeftRightCursor];
if(mouseCursor == MouseCursor::VerticalResize) return [NSCursor resizeUpDownCursor];
return nil;
}
auto NSMakeImage(image icon, uint scaleWidth = 0, uint scaleHeight = 0) -> NSImage* {
if(!icon) return nil;
@ -14,7 +21,7 @@ auto NSMakeImage(image icon, uint scaleWidth = 0, uint scaleHeight = 0) -> NSIma
initWithBitmapDataPlanes:nil
pixelsWide:icon.width() pixelsHigh:icon.height()
bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES
isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace
isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
bytesPerRow:(4 * icon.width()) bitsPerPixel:32
] autorelease];

View File

@ -15,6 +15,12 @@
return self;
}
-(void) resetCursorRects {
if(auto mouseCursor = NSMakeCursor(label->mouseCursor())) {
[self addCursorRect:self.bounds cursor:mouseCursor];
}
}
-(NSDragOperation) draggingEntered:(id<NSDraggingInfo>)sender {
return DropPathsOperation(sender);
}
@ -42,6 +48,10 @@
}
}
-(void) mouseEntered:(NSEvent*)event {
canvas->doMouseEnter();
}
-(void) mouseExited:(NSEvent*)event {
canvas->doMouseLeave();
}
@ -183,7 +193,7 @@ auto pCanvas::_rasterize() -> void {
initWithBitmapDataPlanes:nil
pixelsWide:width pixelsHigh:height
bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES
isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace
isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
bytesPerRow:(width * 4) bitsPerPixel:32
] autorelease];

View File

@ -5,9 +5,11 @@
hiro::mCanvas* canvas;
}
-(id) initWith:(hiro::mCanvas&)canvas;
-(void) resetCursorRects;
-(NSDragOperation) draggingEntered:(id<NSDraggingInfo>)sender;
-(BOOL) performDragOperation:(id<NSDraggingInfo>)sender;
-(void) mouseButton:(NSEvent*)event down:(BOOL)isDown;
-(void) mouseEntered:(NSEvent*)event;
-(void) mouseExited:(NSEvent*)event;
-(void) mouseMove:(NSEvent*)event;
-(void) mouseDown:(NSEvent*)event;

View File

@ -1,22 +1,123 @@
#if defined(Hiro_Label)
//todo:
//* Label::onButtonPress()
//* Label::onButtonRelease()
@implementation CocoaLabel : NSTextView
@implementation CocoaLabel : NSView
-(id) initWith:(hiro::mLabel&)labelReference {
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
label = &labelReference;
[self setDrawsBackground:NO];
[self setEditable:NO];
[self setRichText:NO];
}
return self;
}
-(void) resetCursorRects {
if(auto mouseCursor = NSMakeCursor(label->mouseCursor())) {
[self addCursorRect:self.bounds cursor:mouseCursor];
}
}
-(void) drawRect:(NSRect)dirtyRect {
auto geometry = label->geometry();
NSRect rect = {{geometry.x(), geometry.y()}, {geometry.width(), geometry.height()}};
if(auto backgroundColor = label->backgroundColor()) {
NSColor* color = NSMakeColor(backgroundColor);
[color setFill];
NSRectFill(rect);
}
NSFont* font = hiro::pFont::create(label->font(true));
NSColor* color = [NSColor textColor];
if(auto foregroundColor = label->foregroundColor()) {
color = NSMakeColor(foregroundColor);
}
NSMutableParagraphStyle* paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
paragraphStyle.alignment = NSTextAlignmentLeft;
paragraphStyle.lineBreakMode = NSLineBreakByClipping;
NSDictionary* attributes = @{
NSFontAttributeName:font,
NSForegroundColorAttributeName:color,
NSParagraphStyleAttributeName:paragraphStyle
};
auto size = hiro::pFont::size(label->font(true), label->text());
auto alignment = label->alignment();
if(!alignment) alignment = {0.0, 0.5};
rect.origin.x = max(0, (geometry.width() - size.width()) * alignment.horizontal());
rect.origin.y = max(0, (geometry.height() - size.height()) * alignment.vertical());
rect.size.width = min(geometry.width(), size.width());
rect.size.height = min(geometry.height(), size.height());
NSString* string = [NSString stringWithUTF8String:label->text()];
[string drawInRect:rect withAttributes:attributes];
}
-(void) mouseButton:(NSEvent*)event down:(BOOL)isDown {
if(isDown) {
switch([event buttonNumber]) {
case 0: return label->doMousePress(hiro::Mouse::Button::Left);
case 1: return label->doMousePress(hiro::Mouse::Button::Right);
case 2: return label->doMousePress(hiro::Mouse::Button::Middle);
}
} else {
switch([event buttonNumber]) {
case 0: return label->doMouseRelease(hiro::Mouse::Button::Left);
case 1: return label->doMouseRelease(hiro::Mouse::Button::Right);
case 2: return label->doMouseRelease(hiro::Mouse::Button::Middle);
}
}
}
-(void) mouseEntered:(NSEvent*)event {
label->doMouseEnter();
}
-(void) mouseExited:(NSEvent*)event {
label->doMouseLeave();
}
-(void) mouseMove:(NSEvent*)event {
if([event window] == nil) return;
NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil];
label->doMouseMove({(int)location.x, (int)([self frame].size.height - 1 - location.y)});
}
-(void) mouseDown:(NSEvent*)event {
[self mouseButton:event down:YES];
}
-(void) mouseUp:(NSEvent*)event {
[self mouseButton:event down:NO];
}
-(void) mouseDragged:(NSEvent*)event {
[self mouseMove:event];
}
-(void) rightMouseDown:(NSEvent*)event {
[self mouseButton:event down:YES];
}
-(void) rightMouseUp:(NSEvent*)event {
[self mouseButton:event down:NO];
}
-(void) rightMouseDragged:(NSEvent*)event {
[self mouseMove:event];
}
-(void) otherMouseDown:(NSEvent*)event {
[self mouseButton:event down:YES];
}
-(void) otherMouseUp:(NSEvent*)event {
[self mouseButton:event down:NO];
}
-(void) otherMouseDragged:(NSEvent*)event {
[self mouseMove:event];
}
@end
namespace hiro {
@ -27,6 +128,8 @@ auto pLabel::construct() -> void {
pWidget::construct();
setAlignment(state().alignment);
setBackgroundColor(state().backgroundColor());
setForegroundColor(state().foregroundColor());
setText(state().text);
}
}
@ -44,43 +147,25 @@ auto pLabel::minimumSize() const -> Size {
auto pLabel::setAlignment(Alignment alignment) -> void {
@autoreleasepool {
NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;
if(alignment.horizontal() < 0.333) paragraphStyle.alignment = NSTextAlignmentLeft;
if(alignment.horizontal() > 0.666) paragraphStyle.alignment = NSTextAlignmentRight;
[cocoaView setDefaultParagraphStyle:paragraphStyle];
[cocoaView setNeedsDisplay:YES];
}
}
auto pLabel::setBackgroundColor(Color color) -> void {
//todo
@autoreleasepool {
[cocoaView setNeedsDisplay:YES];
}
}
auto pLabel::setForegroundColor(Color color) -> void {
//todo
@autoreleasepool {
[cocoaView setNeedsDisplay:YES];
}
auto pLabel::setGeometry(Geometry geometry) -> void {
//NSTextView does not support vertical text centering:
//simulate this by adjusting the geometry placement (reduce height, move view down)
uint height = pFont::size(self().font(true), state().text).height();
auto offset = geometry;
if(geometry.height() > height) {
uint diff = geometry.height() - height;
offset.setY(offset.y() + (diff >> 1));
offset.setHeight(offset.height() - (diff >> 1));
}
pWidget::setGeometry({
offset.x() - 6, offset.y(),
offset.width() + 12, offset.height()
});
}
auto pLabel::setText(const string& text) -> void {
@autoreleasepool {
[cocoaView setString:[NSString stringWithUTF8String:text]];
[cocoaView setNeedsDisplay:YES];
}
}

View File

@ -1,10 +1,25 @@
#if defined(Hiro_Label)
@interface CocoaLabel : NSTextView {
@interface CocoaLabel : NSView {
@public
hiro::mLabel* label;
}
-(id) initWith:(hiro::mLabel&)label;
-(void) resetCursorRects;
-(void) drawRect:(NSRect)dirtyRect;
-(void) mouseButton:(NSEvent*)event down:(BOOL)isDown;
-(void) mouseEntered:(NSEvent*)event;
-(void) mouseExited:(NSEvent*)event;
-(void) mouseMove:(NSEvent*)event;
-(void) mouseDown:(NSEvent*)event;
-(void) mouseUp:(NSEvent*)event;
-(void) mouseDragged:(NSEvent*)event;
-(void) rightMouseDown:(NSEvent*)event;
-(void) rightMouseUp:(NSEvent*)event;
-(void) rightMouseDragged:(NSEvent*)event;
-(void) otherMouseDown:(NSEvent*)event;
-(void) otherMouseUp:(NSEvent*)event;
-(void) otherMouseDragged:(NSEvent*)event;
@end
namespace hiro {
@ -16,7 +31,6 @@ struct pLabel : pWidget {
auto setAlignment(Alignment alignment) -> void;
auto setBackgroundColor(Color color) -> void;
auto setForegroundColor(Color color) -> void;
auto setGeometry(Geometry geometry) -> void override;
auto setText(const string& text) -> void;
CocoaLabel* cocoaLabel = nullptr;

View File

@ -365,7 +365,6 @@ auto pTableView::setForegroundColor(Color color) -> void {
auto pTableView::setHeadered(bool headered) -> void {
@autoreleasepool {
if(headered == state().headered) return;
if(headered) {
[[cocoaView content] setHeaderView:[[[NSTableHeaderView alloc] init] autorelease]];
} else {

View File

@ -9,6 +9,12 @@
return self;
}
-(void) resetCursorRects {
if(auto mouseCursor = NSMakeCursor(label->mouseCursor())) {
[self addCursorRect:self.bounds cursor:mouseCursor];
}
}
-(void) drawRect:(NSRect)rect {
[[NSColor blackColor] setFill];
NSRectFillUsingOperation(rect, NSCompositeSourceOver);

View File

@ -5,6 +5,7 @@
hiro::mViewport* viewport;
}
-(id) initWith:(hiro::mViewport&)viewport;
-(void) resetCursorRects;
-(void) drawRect:(NSRect)rect;
-(BOOL) acceptsFirstResponder;
-(NSDragOperation) draggingEntered:(id<NSDraggingInfo>)sender;

View File

@ -137,7 +137,11 @@ auto AboutDialog::show() -> void {
websiteValue.setAlignment(0.0);
websiteValue.setFont(Font().setBold());
websiteValue.setForegroundColor({0, 0, 240});
websiteValue.setText(string{state.website}.trimLeft("http://", 1L).trimLeft("https://", 1L));
auto website = state.website;
if(0); //remove protocol prefix from displayed label:
else if(website.beginsWith("http://" )) website.trimLeft("http://" , 1L);
else if(website.beginsWith("https://")) website.trimLeft("https://", 1L);
websiteValue.setText(website);
//so that the hand cursor is only visible when overing over the link.
websiteValue.setMouseCursor(MouseCursor::Hand);
websiteValue.onMouseRelease([&](auto button) {
@ -153,6 +157,7 @@ auto AboutDialog::show() -> void {
window.setDismissable();
window.setVisible();
window.setModal();
window.setVisible(false);
}
#endif