Debian Codegraph @

src/window/castlewindow_cocoa_colordialog.inc

raw
1 {%MainUnit castlewindow.pas}
2 {
3 Copyright 2022-2022 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Implementation of CastleWindow.ColorDialog.
18
19 Lots of inspiration from LCL Cocoa widgetset code.
20 }
21
22 type
23 TColorPanelDelegate = objcclass(NSObject, NSWindowDelegateProtocol)
24 public
25 colorPanel: NSColorPanel;
26 didPickColor: Boolean;
27 // NSWindowDelegateProtocol
28 procedure windowWillClose(notification: NSNotification); message 'windowWillClose:';
29 //
30 procedure pickColor; message 'pickColor'; // button action
31 procedure exit; message 'exit'; // button action
32 end;
33
34 { TColorPanelDelegate }
35
36 procedure TColorPanelDelegate.windowWillClose(notification: NSNotification);
37 begin
38 NSApp.stopModal();
39 end;
40
41 procedure TColorPanelDelegate.pickColor();
42 begin
43 didPickColor := true;
44 exit();
45 end;
46
47 procedure TColorPanelDelegate.exit();
48 begin
49 colorPanel.close();
50 end;
51
52 function TCastleWindow.ColorDialog(var Color: TCastleColor): boolean;
53
54 function GetNSRect(x, y, width, height: Integer): NSRect;
55 begin
56 Result.origin.x := x;
57 Result.origin.y := y;
58 Result.size.width := width;
59 Result.size.height := height;
60 end;
61
62 function NSColorFromCastle(const Color: TCastleColor): NSColor;
63 begin
64 Result := NSColor.colorWithDeviceRed_green_blue_alpha(
65 Color.X,
66 Color.Y,
67 Color.Z,
68 Color.W
69 );
70 end;
71
72 function NSColorToCastle(Color: NSColor): TCastleColor;
73 begin
74 // We must convert color to device RGB to be able to read it,
75 // otherwise reading e.g. grayscale results in crash.
76 if Color.colorSpace <> NSColorSpace.deviceRGBColorSpace then
77 begin
78 Color := Color.colorUsingColorSpace(NSColorSpace.deviceRGBColorSpace);
79 if Color = nil then
80 begin
81 WritelnWarning('Cannot convert color to RGB');
82 Exit(Black);
83 end;
84 end;
85
86 Result := Vector4(
87 Color.redComponent,
88 Color.greenComponent,
89 Color.blueComponent,
90 Color.alphaComponent
91 );
92 end;
93
94 { Implementation based on LCL TCocoaWSColorDialog.ShowModal }
95
96 var
97 colorDelegate: TColorPanelDelegate;
98 colorPanel: NSColorPanel;
99 // accessory view
100 accessoryView: NSView;
101 lRect: NSRect;
102 okButton, cancelButton: NSButton;
103 begin
104 colorPanel := NSColorPanel.sharedColorPanel();
105 if (colorPanel.respondsToSelector(ObjCSelector('setRestorable:'))) then
106 colorPanel.setRestorable(false);
107 colorPanel.setColor(NSColorFromCastle(Color));
108
109 colorDelegate := TColorPanelDelegate.alloc.init();
110 colorDelegate.colorPanel := colorPanel;
111
112 // setup panel and its accessory view
113 lRect := GetNSRect(0, 0, 220, 30);
114 accessoryView := NSView.alloc.initWithFrame(lRect);
115
116 lRect := GetNSRect(110, 4, 110-8, 24);
117 okButton := NSButton.alloc.initWithFrame(lRect);
118 okButton.setButtonType(NSMomentaryPushInButton);
119 okButton.setBezelStyle(NSRoundedBezelStyle);
120 okButton.setTitle(NSSTR('Pick'));
121 okButton.setAction(objcselector('pickColor'));
122 okButton.setTarget(colorDelegate);
123
124 lRect := GetNSRect(8, 4, 110-8, 24);
125 cancelButton := NSButton.alloc.initWithFrame(lRect);
126 cancelButton.setButtonType(NSMomentaryPushInButton);
127 cancelButton.setBezelStyle(NSRoundedBezelStyle);
128 cancelButton.setTitle(NSSTR('Cancel'));
129 cancelButton.SetAction(objcselector('exit'));
130 cancelButton.setTarget(colorDelegate);
131
132 accessoryView.addSubview(okButton.autorelease);
133 accessoryView.addSubview(cancelButton.autorelease);
134
135 colorPanel.setDelegate(colorDelegate);
136 colorPanel.setAccessoryView(accessoryView.autorelease);
137 colorPanel.setShowsAlpha(true);
138 colorPanel.setDefaultButtonCell(okButton.cell);
139
140 MenuForcefullyDisabled := true;
141 try
142 colorPanel.makeKeyAndOrderFront(colorDelegate);
143 NSApp.runModalForWindow(colorPanel);
144
145 Result := colorDelegate.didPickColor;
146 if Result then
147 Color := NSColorToCastle(colorPanel.color);
148 finally
149 MenuForcefullyDisabled := false;
150 end;
151
152 ref.makeKeyAndOrderFront(nil);
153 end;