diff options
author | Michael Lutz <michi@icosahedron.de> | 2021-12-31 01:18:12 +0100 |
---|---|---|
committer | Michael Lutz <michi@icosahedron.de> | 2022-01-01 12:19:30 +0100 |
commit | 69c8ed9965e2b2bfd6bc04627ca07f78dbcbe2ba (patch) | |
tree | 35cb437e12cba45cebede0556b30ab931df23f5e | |
parent | b351cbe4902269590ef5a51cc93c56b52541ba52 (diff) | |
download | openttd-69c8ed9965e2b2bfd6bc04627ca07f78dbcbe2ba.tar.xz |
Codechange: [OSX] Simplify touchbar button data definition.
-rw-r--r-- | src/video/cocoa/cocoa_wnd.h | 54 | ||||
-rw-r--r-- | src/video/cocoa/cocoa_wnd.mm | 57 |
2 files changed, 50 insertions, 61 deletions
diff --git a/src/video/cocoa/cocoa_wnd.h b/src/video/cocoa/cocoa_wnd.h index 2b29dff42..94bff1e1f 100644 --- a/src/video/cocoa/cocoa_wnd.h +++ b/src/video/cocoa/cocoa_wnd.h @@ -11,8 +11,6 @@ #define COCOA_WND_H #import <Cocoa/Cocoa.h> -#include "toolbar_gui.h" -#include "table/sprites.h" #ifdef MAC_OS_X_VERSION_10_12_2 # define HAVE_TOUCHBAR_SUPPORT @@ -34,58 +32,6 @@ extern NSString *OTTDMainLaunchGameEngine; + (NSCursor *) clearCocoaCursor; @end -#ifdef HAVE_TOUCHBAR_SUPPORT -/* 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 */ @interface OTTD_CocoaWindow : NSWindow #ifdef HAVE_TOUCHBAR_SUPPORT diff --git a/src/video/cocoa/cocoa_wnd.mm b/src/video/cocoa/cocoa_wnd.mm index c240e64ed..95ac3feb1 100644 --- a/src/video/cocoa/cocoa_wnd.mm +++ b/src/video/cocoa/cocoa_wnd.mm @@ -32,7 +32,11 @@ #include "../../gfx_func.h" #include "../../window_func.h" #include "../../window_gui.h" -#include "spritecache.h" +#include "../../spritecache.h" +#include "../../toolbar_gui.h" +#include <array> + +#include "table/sprites.h" /* Table data for key mapping. */ #include "cocoa_keys.h" @@ -56,6 +60,31 @@ * Read http://developer.apple.com/releasenotes/Cocoa/Objective-C++.html for more information. */ +#ifdef HAVE_TOUCHBAR_SUPPORT +struct TouchBarButton { + NSTouchBarItemIdentifier key; + SpriteID sprite; + MainToolbarHotkeys hotkey; + NSString *fallback_text; + + bool operator ==(const NSTouchBarItemIdentifier other) const { return this->key == other; } +}; + +/* 9 items can be displayed on the touch bar when using default buttons. */ +static const std::array<TouchBarButton, 9> _touchbar_buttons{{ + { @"openttd.pause", SPR_IMG_PAUSE, MTHK_PAUSE, @"Pause" }, + { @"openttd.fastforward", SPR_IMG_FASTFORWARD, MTHK_FASTFORWARD, @"Fast Forward" }, + { @"openttd.zoom_in", SPR_IMG_ZOOMIN, MTHK_ZOOM_IN, @"Zoom In" }, + { @"openttd.zoom_out", SPR_IMG_ZOOMOUT, MTHK_ZOOM_OUT, @"Zoom Out" }, + { @"openttd.build_rail", SPR_IMG_BUILDRAIL, MTHK_BUILD_RAIL, @"Rail" }, + { @"openttd.build_road", SPR_IMG_BUILDROAD, MTHK_BUILD_ROAD, @"Road" }, + { @"openttd.build_tram", SPR_IMG_BUILDTRAMS, MTHK_BUILD_TRAM, @"Tram" }, + { @"openttd.build_docks", SPR_IMG_BUILDWATER, MTHK_BUILD_DOCKS, @"Docks" }, + { @"openttd.build_airport", SPR_IMG_BUILDAIR, MTHK_BUILD_AIRPORT, @"Airport" } +}}; + +#endif + bool _allow_hidpi_window = true; // Referenced from table/misc_settings.ini @interface OTTDMain : NSObject <NSApplicationDelegate> @@ -453,15 +482,24 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel - (void)touchBarButtonAction:(id)sender { NSButton *btn = (NSButton *)sender; - NSNumber *hotkeyIndex = [ touchBarButtonActions objectForKey:btn.identifier ]; - if (hotkeyIndex != nil) HandleToolbarHotkey(hotkeyIndex.intValue); + if (auto item = std::find(_touchbar_buttons.cbegin(), _touchbar_buttons.cend(), (NSTouchBarItemIdentifier)btn.identifier); item != _touchbar_buttons.cend()) { + HandleToolbarHotkey(item->hotkey); + } } - (nullable NSTouchBar *)makeTouchBar { + /* Make button identifier array. */ + NSMutableArray<NSTouchBarItemIdentifier> *button_ids = [ [ NSMutableArray alloc ] init ]; + for (const auto &button : _touchbar_buttons) { + [ button_ids addObject:button.key ]; + } + [ button_ids addObject:NSTouchBarItemIdentifierOtherItemsProxy ]; + NSTouchBar *bar = [ [ NSTouchBar alloc ] init ]; bar.delegate = self; - bar.defaultItemIdentifiers = touchBarButtonIdentifiers; + bar.defaultItemIdentifiers = button_ids; + [ button_ids release ]; self->touchbar_created = true; @@ -470,7 +508,10 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel - (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { - NSButton *button = [ NSButton buttonWithTitle:touchBarFallbackText[identifier] target:self action:@selector(touchBarButtonAction:) ]; + auto item = std::find(_touchbar_buttons.cbegin(), _touchbar_buttons.cend(), identifier); + assert(item != _touchbar_buttons.cend()); + + NSButton *button = [ NSButton buttonWithTitle:item->fallback_text target:self action:@selector(touchBarButtonAction:) ]; button.identifier = identifier; button.imageScaling = NSImageScaleProportionallyDown; @@ -488,11 +529,13 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel /* Re-create button images from OTTD sprites. */ for (NSTouchBarItemIdentifier ident in self.touchBar.itemIdentifiers) { + auto item = std::find(_touchbar_buttons.cbegin(), _touchbar_buttons.cend(), ident); + if (item == _touchbar_buttons.cend()) continue; + NSCustomTouchBarItem *tb_item = [ self.touchBar itemForIdentifier:ident ]; NSButton *button = tb_item.view; - NSNumber *num = touchBarButtonSprites[ident]; - NSImage *image = NSImageFromSprite(num.unsignedIntValue, _settings_client.gui.zoom_min); + NSImage *image = NSImageFromSprite(item->sprite, _settings_client.gui.zoom_min); if (image != nil) { /* Human Interface Guidelines: Maximum touch bar glyph size 22 pt. */ CGFloat max_dim = std::max(image.size.width, image.size.height); |