g4tools  5.4.0
view.hm
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_metal_view_hm
5 #define tools_metal_view_hm
6 
7 #import <MetalKit/MetalKit.h>
8 
9 #include "ui_viewer"
10 
11 @interface tools_metal_view : MTKView {
12  @public tools::metal::ui_viewer* m_viewer;
13  NSCursor* m_cursor_default;
14  NSCursor* m_cursor_target;
15  NSCursor* m_cursor_current;
16 }
17 - (BOOL)acceptsFirstResponder;
18 - (nonnull id)init:(NSRect)a_rect device:(nonnull id<MTLDevice>)a_device viewer:(nonnull tools::metal::ui_viewer*)a_viewer;
19 - (void)dealloc;
20 #ifdef TOOLS_METAL_DRAW_WHEN_NEEDED
21 - (void)drawRect:(NSRect)a_rect;
22 #endif
23 - (void)resetCursorRects;
24 - (void)set_cursor:(tools::sg::cursor_shape)a_cursor;
25 - (void)mouseDown:(nonnull NSEvent*)a_event;
26 - (void)mouseUp:(nonnull NSEvent*)a_event;
27 - (void)mouseDragged:(nonnull NSEvent*)a_event;
28 @end
29 
30 @implementation tools_metal_view
31 - (BOOL)acceptsFirstResponder {return YES;}
32 - (nonnull id)init:(NSRect)a_rect device:(nonnull id<MTLDevice>)a_device viewer:(nonnull tools::metal::ui_viewer*)a_viewer {
33  //::printf("debug : tools::metal::tools_metal_view::init\n");
34  self = [super initWithFrame:a_rect device:a_device];
35  if(!self) return self;
36  m_viewer = a_viewer;
37  m_cursor_default = [NSCursor arrowCursor];
38  m_cursor_target = [NSCursor crosshairCursor];
39  m_cursor_current = m_cursor_default;
40 
41  NSTrackingAreaOptions _options = NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect;
42  NSTrackingArea* tracking_area =
43  [[NSTrackingArea alloc] initWithRect:NSZeroRect
44  options:_options
45  owner:self userInfo:nil];
46  [self addTrackingArea:tracking_area];
47 
48  return self;
49 }
50 
51 - (void)dealloc {[super dealloc];}
52 
53 #ifdef TOOLS_METAL_DRAW_WHEN_NEEDED
54 - (void)drawRect:(NSRect)a_rect {
55  [super drawRect:a_rect];
56  if(!m_viewer) return;
57  int w = a_rect.size.width;
58  int h = a_rect.size.height;
59  ::printf("debug : tools::metal::view::drawRect : %d %d\n",w,h);
60  m_viewer->set_size(w,h);
61  m_viewer->win_render();
62 }
63 #endif
64 
65 - (void)mouseDown:(nonnull NSEvent*)a_event {
66  [super mouseDown:a_event];
67  ::printf("debug : tools::metal::view::mouse_down : begin :\n");
68  //NSPoint p = [a_event locationInWindow];
69  //(0,0) = bottom left of window.
70  //NSLog(@"debug : tools_metal_view::mouseDown %g %g",p.x,p.y);
71  if(!m_viewer) return;
72  /*
73  bool to_render = m_viewer->touch_down(p.x,p.y);
74  if(m_viewer->do_works()) to_render = true;
75  if(to_render) {
76 #ifdef TOOLS_METAL_DRAW_WHEN_NEEDED
77  ::printf("debug : tools::metal::view::mouse_down : to render...\n");
78  m_viewer->win_render();
79 #else
80  [self setNeedsDisplay:YES];
81 #endif
82  }
83  ::printf("debug : tools::metal::view::mouse_down : end.\n");
84  */
85 }
86 
87 - (void)mouseUp:(nonnull NSEvent*)a_event {
88  [super mouseUp:a_event];
89  ::printf("debug : tools::metal::view::mouse_up : begin :\n");
90  //NSPoint p = [a_event locationInWindow];
91  //(0,0) = bottom left of window.
92  //NSLog(@"debug : tools_metal_view::mouseUp %g %g",p.x,p.y);
93  if(!m_viewer) return;
94  /*
95  m_viewer->add_work_check_arm_buttons();
96  bool to_render = m_viewer->touch_up(p.x,p.y);
97  //if(m_viewer->do_works()) to_render = true;
98  if(to_render) {
99 #ifdef TOOLS_METAL_DRAW_WHEN_NEEDED
100  ::printf("debug : tools::metal::view::mouse_up : to render...\n");
101  m_viewer->win_render();
102 #else
103  [self setNeedsDisplay:YES];
104 #endif
105  }
106  */
107  NSRect rect;
108  rect.origin.x = 0;
109  rect.origin.y = 0;
110  rect.size.width = 700;
111  rect.size.height = 500;
112  // [self setNeedsDisplayInRect:rect];
113  [self display];
114  /*
115  if(m_viewer->touch_up(p.x,p.y)) {
116  //m_viewer->win_render();
117  [self setNeedsDisplay:YES];
118  }
119  */
120  ::printf("debug : tools::metal::view::mouse_up : end.\n");
121 }
122 
123 - (void)mouseDragged:(nonnull NSEvent*)a_event {
124  [super mouseDragged:a_event];
125  ::printf("debug : tools::metal::view::mouse_dragged\n");
126  //NSPoint p = [a_event locationInWindow];
127  //(0,0) = bottom left of window.
128  //NSLog(@"debug : tools_metal_view::mouseDragged %g %g",p.x,p.y);
129  if(!m_viewer) return;
130  //if(m_viewer->touch_move(p.x,p.y)) m_viewer->win_render();
131 }
132 
133 - (void)resetCursorRects {
134  [super resetCursorRects];
135  //NSLog(@"debug : resetCursorRects");
136  [self discardCursorRects];
137  [self addCursorRect:self.bounds cursor:m_cursor_default];
138  [self addCursorRect:self.bounds cursor:m_cursor_target];
139  [m_cursor_current set];
140 }
141 
142 - (void)mouseEntered:(nonnull NSEvent*)a_event {
143  [super mouseEntered:a_event];
144  [m_cursor_current set];
145 }
146 
147 - (void)set_cursor:(tools::sg::cursor_shape)a_cursor {
148  if(a_cursor==tools::sg::cursor_default) {
149  [m_cursor_default set];
150  m_cursor_current = m_cursor_default;
151  } else if(a_cursor==tools::sg::cursor_target) {
152  [m_cursor_target set];
153  m_cursor_current = m_cursor_target;
154  }
155 }
156 @end
157 
158 @interface tools_metal_view_controller : NSViewController<MTKViewDelegate>
159 @end
160 
161 @implementation tools_metal_view_controller {
162 }
163 -(void)drawInMTKView:(nonnull MTKView*)a_view {
164  //static unsigned int s_count = 0;
165  //::printf("debug : tools::metal::tools_metal_view_controller::drawInMtkView : %d\n",s_count);s_count++;
166  tools_metal_view* _view = static_cast<tools_metal_view*>(a_view);
167  @autoreleasepool {
168  _view->m_viewer->win_render();
169  }
170 }
171 - (void)mtkView:(nonnull MTKView*)a_view drawableSizeWillChange:(CGSize)a_size {
172  int w = a_size.width*0.5;
173  int h = a_size.height*0.5;
174 //static unsigned int s_count = 0;
175 //::printf("debug : tools::metal::tools_metal_view_controller::drawableSizeWillChange : %d :%d %d : %d %d\n",
176 // s_count,(int)a_size.width,(int)a_size.height,w,h);
177  tools_metal_view* _view = static_cast<tools_metal_view*>(a_view);
178  @autoreleasepool {
179  _view->m_viewer->set_size(w,h);
180  //_view->m_viewer->win_render(); //no.
181  }
182 }
183 @end
184 
185 #endif