summaryrefslogtreecommitdiff
path: root/src/video
diff options
context:
space:
mode:
Diffstat (limited to 'src/video')
-rw-r--r--src/video/cocoa/cocoa_wnd.h61
-rw-r--r--src/video/cocoa/cocoa_wnd.mm83
2 files changed, 143 insertions, 1 deletions
diff --git a/src/video/cocoa/cocoa_wnd.h b/src/video/cocoa/cocoa_wnd.h
index 3ddd5a4f0..0b5c51b99 100644
--- a/src/video/cocoa/cocoa_wnd.h
+++ b/src/video/cocoa/cocoa_wnd.h
@@ -11,6 +11,8 @@
#define COCOA_WND_H
#import <Cocoa/Cocoa.h>
+#include "toolbar_gui.h"
+#include "table/sprites.h"
class VideoDriver_Cocoa;
@@ -28,8 +30,67 @@ extern NSString *OTTDMainLaunchGameEngine;
+ (NSCursor *) clearCocoaCursor;
@end
+#ifdef HAVE_OSX_1015_SDK
+/* 9 items can be displayed on the touch bar when using default buttons. */
+static NSArray *touchBarButtonIdentifiers = @[
+ @"openttd.pause",
+ @"openttd.fastforward",
+ @"openttd.zoom_in",
+ @"openttd.zoom_out",
+ @"openttd.build_rail",
+ @"openttd.build_road",
+ @"openttd.build_tram",
+ @"openttd.build_docks",
+ @"openttd.build_airport",
+ NSTouchBarItemIdentifierOtherItemsProxy
+];
+
+static NSDictionary *touchBarButtonSprites = @{
+ @"openttd.pause": [NSNumber numberWithInt:SPR_IMG_PAUSE],
+ @"openttd.fastforward": [NSNumber numberWithInt:SPR_IMG_FASTFORWARD],
+ @"openttd.zoom_in": [NSNumber numberWithInt:SPR_IMG_ZOOMIN],
+ @"openttd.zoom_out": [NSNumber numberWithInt:SPR_IMG_ZOOMOUT],
+ @"openttd.build_rail": [NSNumber numberWithInt:SPR_IMG_BUILDRAIL],
+ @"openttd.build_road": [NSNumber numberWithInt:SPR_IMG_BUILDROAD],
+ @"openttd.build_tram": [NSNumber numberWithInt:SPR_IMG_BUILDTRAMS],
+ @"openttd.build_docks": [NSNumber numberWithInt:SPR_IMG_BUILDWATER],
+ @"openttd.build_airport": [NSNumber numberWithInt:SPR_IMG_BUILDAIR],
+};
+
+static NSDictionary *touchBarButtonActions = @{
+ @"openttd.pause": [NSNumber numberWithInt:MTHK_PAUSE],
+ @"openttd.fastforward": [NSNumber numberWithInt:MTHK_FASTFORWARD],
+ @"openttd.zoom_in": [NSNumber numberWithInt:MTHK_ZOOM_IN],
+ @"openttd.zoom_out": [NSNumber numberWithInt:MTHK_ZOOM_OUT],
+ @"openttd.build_rail": [NSNumber numberWithInt:MTHK_BUILD_RAIL],
+ @"openttd.build_road": [NSNumber numberWithInt:MTHK_BUILD_ROAD],
+ @"openttd.build_tram": [NSNumber numberWithInt:MTHK_BUILD_TRAM],
+ @"openttd.build_docks": [NSNumber numberWithInt:MTHK_BUILD_DOCKS],
+ @"openttd.build_airport": [NSNumber numberWithInt:MTHK_BUILD_AIRPORT],
+};
+
+static NSDictionary *touchBarFallbackText = @{
+ @"openttd.pause": @"Pause",
+ @"openttd.fastforward": @"Fast Forward",
+ @"openttd.zoom_in": @"Zoom In",
+ @"openttd.zoom_out": @"Zoom Out",
+ @"openttd.build_rail": @"Rail",
+ @"openttd.build_road": @"Road",
+ @"openttd.build_tram": @"Tram",
+ @"openttd.build_docks": @"Docks",
+ @"openttd.build_airport": @"Airport",
+};
+#endif
+
/** Subclass of NSWindow to cater our special needs */
+#ifdef HAVE_OSX_1015_SDK
+@interface OTTD_CocoaWindow : NSWindow <NSTouchBarDelegate>
+@property (strong) NSSet *touchbarItems;
+- (NSImage*)generateImage:(int)spriteId;
+#else
@interface OTTD_CocoaWindow : NSWindow
+#endif
+
- (instancetype)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag driver:(VideoDriver_Cocoa *)drv;
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag;
diff --git a/src/video/cocoa/cocoa_wnd.mm b/src/video/cocoa/cocoa_wnd.mm
index 05b55e2a8..9ded8c80d 100644
--- a/src/video/cocoa/cocoa_wnd.mm
+++ b/src/video/cocoa/cocoa_wnd.mm
@@ -32,7 +32,7 @@
#include "../../gfx_func.h"
#include "../../window_func.h"
#include "../../window_gui.h"
-
+#include "spritecache.h"
/* Table data for key mapping. */
#include "cocoa_keys.h"
@@ -405,6 +405,87 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
return self;
}
+#ifdef HAVE_OSX_1015_SDK
+
+- (void)touchBarButtonAction:(id)sender
+{
+ if (@available(macOS 10.15, *)) {
+ NSButtonTouchBarItem *btn = (NSButtonTouchBarItem *)sender;
+ NSNumber *hotkeyIndex = [ touchBarButtonActions objectForKey:btn.identifier ];
+ HandleToolbarHotkey(hotkeyIndex.intValue);
+ }
+}
+
+#pragma mark NSTouchBarProvider
+- (nullable NSTouchBar *)makeTouchBar
+{
+ NSTouchBar *bar = [ [ NSTouchBar alloc ] init ];
+ bar.delegate = self;
+ bar.defaultItemIdentifiers = touchBarButtonIdentifiers;
+
+ return bar;
+}
+
+-(NSImage *)generateImage:(int)spriteId
+{
+ if (!SpriteExists(spriteId)) {
+ return nullptr;
+ }
+
+ /* Fetch the sprite and create a new bitmap */
+ const Sprite *fullspr = GetSprite(spriteId, ST_NORMAL);
+ const std::unique_ptr<uint32[]> buffer = DrawSpriteToRgbaBuffer(spriteId);
+ if (!buffer) {
+ return nullptr; // failed to blit sprite or we're using an 8bpp blitter.
+ }
+
+ NSBitmapImageRep *bitmap = [ [ NSBitmapImageRep alloc ] initWithBitmapDataPlanes:nil pixelsWide:fullspr->width pixelsHigh:fullspr->height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:0 bitsPerPixel:0 ];
+
+ /* Copy the sprite to the NSBitmapImageRep image buffer */
+ const Colour *src = (const Colour *)buffer.get();
+ for (int y = 0; y < fullspr->height; y++) {
+ for (int x = 0; x < fullspr->width; x++) {
+ NSUInteger pixel[4];
+ pixel[0] = src->r;
+ pixel[1] = src->g;
+ pixel[2] = src->b;
+ pixel[3] = src->a;
+ [ bitmap setPixel:pixel atX:x y:y ];
+
+ src += 1;
+ }
+ }
+
+ /* Finally, convert the NSBitmapImageRep we created to a NSimage we can put on the button and clean up. */
+ NSImage *outImage = [ [ NSImage alloc ] initWithSize:NSMakeSize(fullspr->width, fullspr->height) ];
+ [ outImage addRepresentation:bitmap ];
+ [ bitmap release ];
+
+ return outImage;
+}
+
+#pragma mark NSTouchBarDelegate
+- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
+{
+ if (@available(macOS 10.15, *)) {
+ NSButtonTouchBarItem *button = [ [ NSButtonTouchBarItem alloc ] initWithIdentifier:identifier ];
+ button.target = self;
+ button.action = @selector(touchBarButtonAction:);
+
+ NSNumber *num = touchBarButtonSprites[identifier];
+ NSImage *generatedImage = [ self generateImage:num.unsignedIntValue ];
+ if (generatedImage != nullptr) {
+ button.image = generatedImage;
+ } else {
+ button.title = NSLocalizedString(touchBarFallbackText[identifier], @"");
+ }
+ return button;
+ } else {
+ return nullptr;
+ }
+}
+#endif
+
/**
* Define the rectangle we draw our window in
*/