Debian Codegraph @

platform/macos/godot_button_view.mm

raw
1 /**************************************************************************/
2 /* godot_button_view.mm */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #import "godot_button_view.h"
32
33 @implementation GodotButtonView
34
35 - (id)initWithFrame:(NSRect)frame {
36 self = [super initWithFrame:frame];
37
38 tracking_area = nil;
39 offset = NSMakePoint(8, 8);
40 spacing = 20;
41 mouse_in_group = false;
42 rtl = false;
43 close_button = nullptr;
44 miniaturize_button = nullptr;
45 zoom_button = nullptr;
46
47 return self;
48 }
49
50 - (void)initButtons:(CGFloat)button_spacing offset:(NSPoint)button_offset rtl:(bool)is_rtl {
51 spacing = button_spacing;
52 rtl = is_rtl;
53
54 close_button = [NSWindow standardWindowButton:NSWindowCloseButton forStyleMask:NSWindowStyleMaskTitled];
55 [close_button setFrameOrigin:NSMakePoint(rtl ? spacing * 2 : 0, 0)];
56 [self addSubview:close_button];
57
58 miniaturize_button = [NSWindow standardWindowButton:NSWindowMiniaturizeButton forStyleMask:NSWindowStyleMaskTitled];
59 [miniaturize_button setFrameOrigin:NSMakePoint(spacing, 0)];
60 [self addSubview:miniaturize_button];
61
62 zoom_button = [NSWindow standardWindowButton:NSWindowZoomButton forStyleMask:NSWindowStyleMaskTitled];
63 [zoom_button setFrameOrigin:NSMakePoint(rtl ? 0 : spacing * 2, 0)];
64 [self addSubview:zoom_button];
65
66 offset.y = button_offset.y - zoom_button.frame.size.height / 2;
67 offset.x = button_offset.x - zoom_button.frame.size.width / 2;
68
69 if (rtl) {
70 [self setFrameSize:NSMakeSize(close_button.frame.origin.x + close_button.frame.size.width, close_button.frame.size.height)];
71 } else {
72 [self setFrameSize:NSMakeSize(zoom_button.frame.origin.x + zoom_button.frame.size.width, zoom_button.frame.size.height)];
73 }
74 [self displayButtons];
75 }
76
77 - (void)setOffset:(NSPoint)button_offset {
78 if (zoom_button) {
79 offset.y = button_offset.y - zoom_button.frame.size.height / 2;
80 offset.x = button_offset.x - zoom_button.frame.size.width / 2;
81
82 [self viewDidMoveToWindow];
83 }
84 }
85
86 - (NSPoint)getOffset {
87 return offset;
88 }
89
90 - (void)viewDidMoveToWindow {
91 if (!self.window) {
92 return;
93 }
94
95 if (rtl) {
96 [self setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
97 [self setFrameOrigin:NSMakePoint(self.window.frame.size.width - self.frame.size.width - offset.x, self.window.frame.size.height - self.frame.size.height - offset.y)];
98 } else {
99 [self setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];
100 [self setFrameOrigin:NSMakePoint(offset.x, self.window.frame.size.height - self.frame.size.height - offset.y)];
101 }
102 }
103
104 - (BOOL)_mouseInGroup:(NSButton *)button {
105 return mouse_in_group;
106 }
107
108 - (void)updateTrackingAreas {
109 if (tracking_area != nil) {
110 [self removeTrackingArea:tracking_area];
111 }
112
113 NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect;
114 tracking_area = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:options owner:self userInfo:nil];
115
116 [self addTrackingArea:tracking_area];
117 }
118
119 - (void)mouseEntered:(NSEvent *)event {
120 [super mouseEntered:event];
121
122 mouse_in_group = true;
123 [self displayButtons];
124 }
125
126 - (void)mouseExited:(NSEvent *)event {
127 [super mouseExited:event];
128
129 mouse_in_group = false;
130 [self displayButtons];
131 }
132
133 - (void)displayButtons {
134 for (NSView *subview in self.subviews) {
135 [subview setNeedsDisplay:YES];
136 }
137 }
138
139 @end