godot_button_view.mm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include "godot_button_view.h"
  31. @implementation GodotButtonView
  32. - (id)initWithFrame:(NSRect)frame {
  33. self = [super initWithFrame:frame];
  34. tracking_area = nil;
  35. offset = NSMakePoint(8, 8);
  36. spacing = 20;
  37. mouse_in_group = false;
  38. rtl = false;
  39. close_button = nullptr;
  40. miniaturize_button = nullptr;
  41. zoom_button = nullptr;
  42. return self;
  43. }
  44. - (void)initButtons:(CGFloat)button_spacing offset:(NSPoint)button_offset rtl:(bool)is_rtl {
  45. spacing = button_spacing;
  46. rtl = is_rtl;
  47. close_button = [NSWindow standardWindowButton:NSWindowCloseButton forStyleMask:NSWindowStyleMaskTitled];
  48. [close_button setFrameOrigin:NSMakePoint(rtl ? spacing * 2 : 0, 0)];
  49. [self addSubview:close_button];
  50. miniaturize_button = [NSWindow standardWindowButton:NSWindowMiniaturizeButton forStyleMask:NSWindowStyleMaskTitled];
  51. [miniaturize_button setFrameOrigin:NSMakePoint(spacing, 0)];
  52. [self addSubview:miniaturize_button];
  53. zoom_button = [NSWindow standardWindowButton:NSWindowZoomButton forStyleMask:NSWindowStyleMaskTitled];
  54. [zoom_button setFrameOrigin:NSMakePoint(rtl ? 0 : spacing * 2, 0)];
  55. [self addSubview:zoom_button];
  56. offset.y = button_offset.y - zoom_button.frame.size.height / 2;
  57. offset.x = button_offset.x - zoom_button.frame.size.width / 2;
  58. if (rtl) {
  59. [self setFrameSize:NSMakeSize(close_button.frame.origin.x + close_button.frame.size.width, close_button.frame.size.height)];
  60. } else {
  61. [self setFrameSize:NSMakeSize(zoom_button.frame.origin.x + zoom_button.frame.size.width, zoom_button.frame.size.height)];
  62. }
  63. [self displayButtons];
  64. }
  65. - (void)setOffset:(NSPoint)button_offset {
  66. if (zoom_button) {
  67. offset.y = button_offset.y - zoom_button.frame.size.height / 2;
  68. offset.x = button_offset.x - zoom_button.frame.size.width / 2;
  69. [self viewDidMoveToWindow];
  70. }
  71. }
  72. - (NSPoint)getOffset {
  73. return offset;
  74. }
  75. - (void)viewDidMoveToWindow {
  76. if (!self.window) {
  77. return;
  78. }
  79. if (rtl) {
  80. [self setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
  81. [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)];
  82. } else {
  83. [self setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];
  84. [self setFrameOrigin:NSMakePoint(offset.x, self.window.frame.size.height - self.frame.size.height - offset.y)];
  85. }
  86. }
  87. - (BOOL)_mouseInGroup:(NSButton *)button {
  88. return mouse_in_group;
  89. }
  90. - (void)updateTrackingAreas {
  91. if (tracking_area != nil) {
  92. [self removeTrackingArea:tracking_area];
  93. }
  94. NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect;
  95. tracking_area = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:options owner:self userInfo:nil];
  96. [self addTrackingArea:tracking_area];
  97. }
  98. - (void)mouseEntered:(NSEvent *)event {
  99. [super mouseEntered:event];
  100. mouse_in_group = true;
  101. [self displayButtons];
  102. }
  103. - (void)mouseExited:(NSEvent *)event {
  104. [super mouseExited:event];
  105. mouse_in_group = false;
  106. [self displayButtons];
  107. }
  108. - (void)displayButtons {
  109. for (NSView *subview in self.subviews) {
  110. [subview setNeedsDisplay:YES];
  111. }
  112. }
  113. @end