1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
4 #ifndef tools_metal_view_hm
5 #define tools_metal_view_hm
7 #import <MetalKit/MetalKit.h>
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;
17 - (BOOL)acceptsFirstResponder;
18 - (nonnull id)init:(NSRect)a_rect device:(nonnull id<MTLDevice>)a_device viewer:(nonnull tools::metal::ui_viewer*)a_viewer;
20 #ifdef TOOLS_METAL_DRAW_WHEN_NEEDED
21 - (void)drawRect:(NSRect)a_rect;
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;
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;
37 m_cursor_default = [NSCursor arrowCursor];
38 m_cursor_target = [NSCursor crosshairCursor];
39 m_cursor_current = m_cursor_default;
41 NSTrackingAreaOptions _options = NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect;
42 NSTrackingArea* tracking_area =
43 [[NSTrackingArea alloc] initWithRect:NSZeroRect
45 owner:self userInfo:nil];
46 [self addTrackingArea:tracking_area];
51 - (void)dealloc {[super dealloc];}
53 #ifdef TOOLS_METAL_DRAW_WHEN_NEEDED
54 - (void)drawRect:(NSRect)a_rect {
55 [super drawRect:a_rect];
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();
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);
73 bool to_render = m_viewer->touch_down(p.x,p.y);
74 if(m_viewer->do_works()) to_render = true;
76 #ifdef TOOLS_METAL_DRAW_WHEN_NEEDED
77 ::printf("debug : tools::metal::view::mouse_down : to render...\n");
78 m_viewer->win_render();
80 [self setNeedsDisplay:YES];
83 ::printf("debug : tools::metal::view::mouse_down : end.\n");
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);
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;
99 #ifdef TOOLS_METAL_DRAW_WHEN_NEEDED
100 ::printf("debug : tools::metal::view::mouse_up : to render...\n");
101 m_viewer->win_render();
103 [self setNeedsDisplay:YES];
110 rect.size.width = 700;
111 rect.size.height = 500;
112 // [self setNeedsDisplayInRect:rect];
115 if(m_viewer->touch_up(p.x,p.y)) {
116 //m_viewer->win_render();
117 [self setNeedsDisplay:YES];
120 ::printf("debug : tools::metal::view::mouse_up : end.\n");
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();
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];
142 - (void)mouseEntered:(nonnull NSEvent*)a_event {
143 [super mouseEntered:a_event];
144 [m_cursor_current set];
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;
158 @interface tools_metal_view_controller : NSViewController<MTKViewDelegate>
161 @implementation tools_metal_view_controller {
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);
168 _view->m_viewer->win_render();
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);
179 _view->m_viewer->set_size(w,h);
180 //_view->m_viewer->win_render(); //no.