MyOpenGLView.mm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. #include "MyOpenGLView.h"
  4. #include "MyWindow.h"
  5. namespace EE{
  6. /******************************************************************************/
  7. MyOpenGLView *OpenGLView;
  8. static ImageRTPtr LiveResize;
  9. /******************************************************************************/
  10. }
  11. /******************************************************************************/
  12. @implementation MyOpenGLView
  13. /******************************************************************************/
  14. // DRAG AND DROP
  15. /******************************************************************************/
  16. -(NSDragOperation) draggingEntered:(id<NSDraggingInfo>)sender
  17. {
  18. NSDragOperation drag_mask=[sender draggingSourceOperationMask];
  19. if(NSPasteboard *pasteboard=[sender draggingPasteboard])
  20. if([[pasteboard types] containsObject:NSFilenamesPboardType])
  21. {
  22. if(drag_mask&NSDragOperationLink)return NSDragOperationLink;
  23. if(drag_mask&NSDragOperationCopy)return NSDragOperationCopy;
  24. }
  25. return NSDragOperationNone;
  26. }
  27. -(BOOL) performDragOperation:(id<NSDraggingInfo>)sender
  28. {
  29. if(App.drop)
  30. if(NSPasteboard *pasteboard=[sender draggingPasteboard])
  31. if([[pasteboard types] containsObject:NSFilenamesPboardType])
  32. if(NSArray *files=[pasteboard propertyListForType:NSFilenamesPboardType])
  33. {
  34. Memc<Str> names; FREP([files count])if(NSString *string=[files objectAtIndex: i])names.add(string);
  35. if(names.elms())App.drop(names, Gui.objAtPos(Ms.pos()), Ms.pos()); // don't use 'Gui.msLit' but 'Gui.objAtPos' because this is Apple callback and 'Ms.pos' may be newer than of 'Gui.msLit'
  36. }
  37. return YES;
  38. }
  39. /******************************************************************************/
  40. // INPUT
  41. /******************************************************************************/
  42. -(void) scrollWheel :(NSEvent*)theEvent {Ms._wheel.x-=[theEvent deltaX]; Ms._wheel.y+=[theEvent deltaY];} // deltaY is the normal mouse wheel, deltaX is the horizontal wheel (or vertical with Shift pressed)
  43. -(void) mouseDown :(NSEvent*)theEvent {Ms. push ([theEvent buttonNumber]);}
  44. -(void) mouseUp :(NSEvent*)theEvent {Ms. release([theEvent buttonNumber]);}
  45. -(void) rightMouseDown:(NSEvent*)theEvent {[self mouseDown:theEvent];}
  46. -(void) otherMouseDown:(NSEvent*)theEvent {[self mouseDown:theEvent];}
  47. -(void) rightMouseUp :(NSEvent*)theEvent {[self mouseUp :theEvent];}
  48. -(void) otherMouseUp :(NSEvent*)theEvent {[self mouseUp :theEvent];}
  49. -(void) mouseEntered :(NSEvent*)theEvent {Ms._on_client=true ; Ms.resetVisibility();}
  50. -(void) mouseExited :(NSEvent*)theEvent {Ms._on_client=false; Ms.resetVisibility();}
  51. /******************************************************************************/
  52. // DRAW
  53. /******************************************************************************/
  54. -(void) drawRect:(NSRect)rect
  55. {
  56. if(D.created())
  57. {
  58. if(App.minimized() || (D.full() && !App.active()))return;
  59. SyncLocker locker(D._lock);
  60. if(!D._resetting)
  61. {
  62. if([self inLiveResize]) // resizing
  63. {
  64. if(LiveResize)
  65. {
  66. D.viewReset();
  67. RectI viewport=D._viewport;
  68. D.viewport(RectI(0, 0, RoundPos(rect.size.width), RoundPos(rect.size.height))); // force full window viewport
  69. ALPHA_MODE alpha=D.alpha (ALPHA_NONE); LiveResize->drawFs(FIT_FULL, FILTER_LINEAR);
  70. D.alpha (alpha );
  71. D.viewport(viewport );
  72. goto ok;
  73. }
  74. }else
  75. if(DrawState())return; // 'DrawState' already calls 'D.flip'
  76. }
  77. D.clearCol();
  78. ok:
  79. D.flip(); // flip screen
  80. }
  81. }
  82. /******************************************************************************/
  83. -(void)setLayer:(CALayer*)layer
  84. {
  85. [super setLayer:layer];
  86. [OpenGLContext update]; // this is needed on Xcode 10, which shipped on Mac OS Mojave, without this line, the screen will be black initially, and only appear after resizing window, this line fixes that
  87. }
  88. -(void) setFrameSize:(NSSize)newSize
  89. {
  90. [super setFrameSize:newSize];
  91. [OpenGLContext update]; // without this, client area will be rendered incorrectly (black bars, flickering, offsets)
  92. #if 1
  93. App._window_resized=WindowSize(true);
  94. #else // can't use this because if app was started in fullscreen then it will return incorrect sizes (including the border)
  95. NSRect rect=[OpenGLView bounds]; App._window_resized.set(RoundPos(rect.size.width), RoundPos(rect.size.height));
  96. #endif
  97. }
  98. -(void) viewWillStartLiveResize
  99. {
  100. if(D.created() && !D._resetting && StateActive)
  101. {
  102. DrawState(); // do not surround 'DrawState' with '_lock' lock because it handles it on its own
  103. SyncLocker locker(D._lock);
  104. if(LiveResize.find(ImageRTDesc(Renderer._main.w(), Renderer._main.h(), IMAGERT_RGB)))
  105. {
  106. Renderer._main.copyHw(*LiveResize, true); // doesn't use Alpha
  107. #if GL
  108. glFlush(); // without this 'LiveResize' will be empty or look corrupted
  109. #endif
  110. }
  111. }
  112. [super viewWillStartLiveResize];
  113. }
  114. -(void) viewDidEndLiveResize
  115. {
  116. LiveResize.clear();
  117. [super viewDidEndLiveResize];
  118. #if 1
  119. VecI2 size=WindowSize(true);
  120. #else // can't use this because if app was started in fullscreen then it will return incorrect sizes (including the border)
  121. NSRect rect=[self bounds]; VecI2 size(RoundPos(rect.size.width), RoundPos(rect.size.height));
  122. #endif
  123. D.modeSet(size.x, size.y); // reset here already because draw can be requested before we detect size change in update
  124. }
  125. /******************************************************************************/
  126. // CALLBACKS
  127. /******************************************************************************/
  128. -(BOOL)acceptsFirstResponder {return YES;}
  129. -(BOOL)becomeFirstResponder {return YES;}
  130. -(BOOL)resignFirstResponder {return YES;}
  131. /******************************************************************************/
  132. @end
  133. /******************************************************************************/