mirror of
https://github.com/Drezil/imgui.git
synced 2024-11-22 20:07:01 +00:00
Examples: Bring code of example_apple_metal and example_apple_opengl2 closer to each other. (#1873, #3543)
This commit is contained in:
parent
0aeb978e61
commit
88f4c1321a
@ -15,26 +15,28 @@
|
|||||||
|
|
||||||
#include "imgui.h"
|
#include "imgui.h"
|
||||||
#include "imgui_impl_metal.h"
|
#include "imgui_impl_metal.h"
|
||||||
|
|
||||||
#if TARGET_OS_OSX
|
#if TARGET_OS_OSX
|
||||||
#include "imgui_impl_osx.h"
|
#include "imgui_impl_osx.h"
|
||||||
|
@interface AppViewController : NSViewController
|
||||||
@interface ViewController : NSViewController
|
|
||||||
@end
|
@end
|
||||||
#else
|
#else
|
||||||
@interface ViewController : UIViewController
|
@interface AppViewController : UIViewController
|
||||||
@end
|
@end
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@interface ViewController () <MTKViewDelegate>
|
@interface AppViewController () <MTKViewDelegate>
|
||||||
@property (nonatomic, readonly) MTKView *mtkView;
|
@property (nonatomic, readonly) MTKView *mtkView;
|
||||||
@property (nonatomic, strong) id <MTLDevice> device;
|
@property (nonatomic, strong) id <MTLDevice> device;
|
||||||
@property (nonatomic, strong) id <MTLCommandQueue> commandQueue;
|
@property (nonatomic, strong) id <MTLCommandQueue> commandQueue;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation ViewController
|
//-----------------------------------------------------------------------------------
|
||||||
|
// AppViewController
|
||||||
|
//-----------------------------------------------------------------------------------
|
||||||
|
|
||||||
- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil
|
@implementation AppViewController
|
||||||
|
|
||||||
|
-(instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil
|
||||||
{
|
{
|
||||||
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
|
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
|
||||||
|
|
||||||
@ -80,17 +82,17 @@
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (MTKView *)mtkView
|
-(MTKView *)mtkView
|
||||||
{
|
{
|
||||||
return (MTKView *)self.view;
|
return (MTKView *)self.view;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)loadView
|
-(void)loadView
|
||||||
{
|
{
|
||||||
self.view = [[MTKView alloc] initWithFrame:CGRectMake(0, 0, 1200, 720)];
|
self.view = [[MTKView alloc] initWithFrame:CGRectMake(0, 0, 1200, 720)];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)viewDidLoad
|
-(void)viewDidLoad
|
||||||
{
|
{
|
||||||
[super viewDidLoad];
|
[super viewDidLoad];
|
||||||
|
|
||||||
@ -110,7 +112,7 @@
|
|||||||
// we receive events for all controls, not just Dear ImGui widgets. If we had native controls in our
|
// we receive events for all controls, not just Dear ImGui widgets. If we had native controls in our
|
||||||
// window, we'd want to be much more careful than just ingesting the complete event stream.
|
// window, we'd want to be much more careful than just ingesting the complete event stream.
|
||||||
// To match the behavior of other backends, we pass every event down to the OS.
|
// To match the behavior of other backends, we pass every event down to the OS.
|
||||||
NSEventMask eventMask = NSEventMaskKeyDown | NSEventMaskKeyUp | NSEventMaskFlagsChanged | NSEventTypeScrollWheel;
|
NSEventMask eventMask = NSEventMaskKeyDown | NSEventMaskKeyUp | NSEventMaskFlagsChanged;
|
||||||
[NSEvent addLocalMonitorForEventsMatchingMask:eventMask handler:^NSEvent * _Nullable(NSEvent *event)
|
[NSEvent addLocalMonitorForEventsMatchingMask:eventMask handler:^NSEvent * _Nullable(NSEvent *event)
|
||||||
{
|
{
|
||||||
ImGui_ImplOSX_HandleEvent(event, self.view);
|
ImGui_ImplOSX_HandleEvent(event, self.view);
|
||||||
@ -122,105 +124,7 @@
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Interaction
|
-(void)drawInMTKView:(MTKView*)view
|
||||||
|
|
||||||
#if TARGET_OS_OSX
|
|
||||||
|
|
||||||
- (void)mouseMoved:(NSEvent *)event {
|
|
||||||
ImGui_ImplOSX_HandleEvent(event, self.view);
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)mouseDown:(NSEvent *)event {
|
|
||||||
ImGui_ImplOSX_HandleEvent(event, self.view);
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)rightMouseDown:(NSEvent *)event {
|
|
||||||
ImGui_ImplOSX_HandleEvent(event, self.view);
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)otherMouseDown:(NSEvent *)event {
|
|
||||||
ImGui_ImplOSX_HandleEvent(event, self.view);
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)mouseUp:(NSEvent *)event {
|
|
||||||
ImGui_ImplOSX_HandleEvent(event, self.view);
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)rightMouseUp:(NSEvent *)event {
|
|
||||||
ImGui_ImplOSX_HandleEvent(event, self.view);
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)otherMouseUp:(NSEvent *)event {
|
|
||||||
ImGui_ImplOSX_HandleEvent(event, self.view);
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)mouseDragged:(NSEvent *)event {
|
|
||||||
ImGui_ImplOSX_HandleEvent(event, self.view);
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)rightMouseDragged:(NSEvent *)event {
|
|
||||||
ImGui_ImplOSX_HandleEvent(event, self.view);
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)otherMouseDragged:(NSEvent *)event {
|
|
||||||
ImGui_ImplOSX_HandleEvent(event, self.view);
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)scrollWheel:(NSEvent *)event {
|
|
||||||
ImGui_ImplOSX_HandleEvent(event, self.view);
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
// This touch mapping is super cheesy/hacky. We treat any touch on the screen
|
|
||||||
// as if it were a depressed left mouse button, and we don't bother handling
|
|
||||||
// multitouch correctly at all. This causes the "cursor" to behave very erratically
|
|
||||||
// when there are multiple active touches. But for demo purposes, single-touch
|
|
||||||
// interaction actually works surprisingly well.
|
|
||||||
- (void)updateIOWithTouchEvent:(UIEvent *)event
|
|
||||||
{
|
|
||||||
UITouch *anyTouch = event.allTouches.anyObject;
|
|
||||||
CGPoint touchLocation = [anyTouch locationInView:self.view];
|
|
||||||
ImGuiIO &io = ImGui::GetIO();
|
|
||||||
io.MousePos = ImVec2(touchLocation.x, touchLocation.y);
|
|
||||||
|
|
||||||
BOOL hasActiveTouch = NO;
|
|
||||||
for (UITouch *touch in event.allTouches)
|
|
||||||
{
|
|
||||||
if (touch.phase != UITouchPhaseEnded && touch.phase != UITouchPhaseCancelled)
|
|
||||||
{
|
|
||||||
hasActiveTouch = YES;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
io.MouseDown[0] = hasActiveTouch;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
|
|
||||||
{
|
|
||||||
[self updateIOWithTouchEvent:event];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
|
|
||||||
{
|
|
||||||
[self updateIOWithTouchEvent:event];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
|
|
||||||
{
|
|
||||||
[self updateIOWithTouchEvent:event];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
|
|
||||||
{
|
|
||||||
[self updateIOWithTouchEvent:event];
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#pragma mark - MTKViewDelegate
|
|
||||||
|
|
||||||
- (void)drawInMTKView:(MTKView*)view
|
|
||||||
{
|
{
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
io.DisplaySize.x = view.bounds.size.width;
|
io.DisplaySize.x = view.bounds.size.width;
|
||||||
@ -237,20 +141,12 @@
|
|||||||
|
|
||||||
id<MTLCommandBuffer> commandBuffer = [self.commandQueue commandBuffer];
|
id<MTLCommandBuffer> commandBuffer = [self.commandQueue commandBuffer];
|
||||||
|
|
||||||
// Our state (make them static = more or less global) as a convenience to keep the example terse.
|
|
||||||
static bool show_demo_window = true;
|
|
||||||
static bool show_another_window = false;
|
|
||||||
static float clear_color[4] = { 0.28f, 0.36f, 0.5f, 1.0f };
|
|
||||||
|
|
||||||
MTLRenderPassDescriptor* renderPassDescriptor = view.currentRenderPassDescriptor;
|
MTLRenderPassDescriptor* renderPassDescriptor = view.currentRenderPassDescriptor;
|
||||||
if (renderPassDescriptor != nil)
|
if (renderPassDescriptor == nil)
|
||||||
{
|
{
|
||||||
renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(clear_color[0] * clear_color[3], clear_color[1] * clear_color[3], clear_color[2] * clear_color[3], clear_color[3]);
|
[commandBuffer commit];
|
||||||
|
return;
|
||||||
// Here, you could do additional rendering work, including other passes as necessary.
|
}
|
||||||
|
|
||||||
id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
|
|
||||||
[renderEncoder pushDebugGroup:@"ImGui demo"];
|
|
||||||
|
|
||||||
// Start the Dear ImGui frame
|
// Start the Dear ImGui frame
|
||||||
ImGui_ImplMetal_NewFrame(renderPassDescriptor);
|
ImGui_ImplMetal_NewFrame(renderPassDescriptor);
|
||||||
@ -259,6 +155,11 @@
|
|||||||
#endif
|
#endif
|
||||||
ImGui::NewFrame();
|
ImGui::NewFrame();
|
||||||
|
|
||||||
|
// Our state (make them static = more or less global) as a convenience to keep the example terse.
|
||||||
|
static bool show_demo_window = true;
|
||||||
|
static bool show_another_window = false;
|
||||||
|
static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
||||||
|
|
||||||
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
|
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
|
||||||
if (show_demo_window)
|
if (show_demo_window)
|
||||||
ImGui::ShowDemoWindow(&show_demo_window);
|
ImGui::ShowDemoWindow(&show_demo_window);
|
||||||
@ -299,24 +200,83 @@
|
|||||||
// Rendering
|
// Rendering
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
ImDrawData* draw_data = ImGui::GetDrawData();
|
ImDrawData* draw_data = ImGui::GetDrawData();
|
||||||
ImGui_ImplMetal_RenderDrawData(draw_data, commandBuffer, renderEncoder);
|
|
||||||
|
|
||||||
|
renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
|
||||||
|
id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
|
||||||
|
[renderEncoder pushDebugGroup:@"Dear ImGui rendering"];
|
||||||
|
ImGui_ImplMetal_RenderDrawData(draw_data, commandBuffer, renderEncoder);
|
||||||
[renderEncoder popDebugGroup];
|
[renderEncoder popDebugGroup];
|
||||||
[renderEncoder endEncoding];
|
[renderEncoder endEncoding];
|
||||||
|
|
||||||
|
// Present
|
||||||
[commandBuffer presentDrawable:view.currentDrawable];
|
[commandBuffer presentDrawable:view.currentDrawable];
|
||||||
}
|
|
||||||
|
|
||||||
[commandBuffer commit];
|
[commandBuffer commit];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)mtkView:(MTKView*)view drawableSizeWillChange:(CGSize)size
|
-(void)mtkView:(MTKView*)view drawableSizeWillChange:(CGSize)size
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------------
|
||||||
|
// Input processing
|
||||||
|
//-----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#if TARGET_OS_OSX
|
||||||
|
|
||||||
|
// Forward Mouse/Keyboard events to Dear ImGui OSX backend.
|
||||||
|
// Other events are registered via addLocalMonitorForEventsMatchingMask()
|
||||||
|
-(void)mouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
-(void)rightMouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
-(void)otherMouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
-(void)mouseUp:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
-(void)rightMouseUp:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
-(void)otherMouseUp:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
-(void)mouseMoved:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
-(void)mouseDragged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
-(void)rightMouseMoved:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
-(void)rightMouseDragged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
-(void)otherMouseMoved:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
-(void)otherMouseDragged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
-(void)scrollWheel:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self.view); }
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
// This touch mapping is super cheesy/hacky. We treat any touch on the screen
|
||||||
|
// as if it were a depressed left mouse button, and we don't bother handling
|
||||||
|
// multitouch correctly at all. This causes the "cursor" to behave very erratically
|
||||||
|
// when there are multiple active touches. But for demo purposes, single-touch
|
||||||
|
// interaction actually works surprisingly well.
|
||||||
|
-(void)updateIOWithTouchEvent:(UIEvent *)event
|
||||||
|
{
|
||||||
|
UITouch *anyTouch = event.allTouches.anyObject;
|
||||||
|
CGPoint touchLocation = [anyTouch locationInView:self.view];
|
||||||
|
ImGuiIO &io = ImGui::GetIO();
|
||||||
|
io.MousePos = ImVec2(touchLocation.x, touchLocation.y);
|
||||||
|
|
||||||
|
BOOL hasActiveTouch = NO;
|
||||||
|
for (UITouch *touch in event.allTouches)
|
||||||
|
{
|
||||||
|
if (touch.phase != UITouchPhaseEnded && touch.phase != UITouchPhaseCancelled)
|
||||||
|
{
|
||||||
|
hasActiveTouch = YES;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
io.MouseDown[0] = hasActiveTouch;
|
||||||
|
}
|
||||||
|
|
||||||
|
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self updateIOWithTouchEvent:event]; }
|
||||||
|
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self updateIOWithTouchEvent:event]; }
|
||||||
|
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self updateIOWithTouchEvent:event]; }
|
||||||
|
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self updateIOWithTouchEvent:event]; }
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#pragma mark - Application Delegate
|
//-----------------------------------------------------------------------------------
|
||||||
|
// AppDelegate
|
||||||
|
//-----------------------------------------------------------------------------------
|
||||||
|
|
||||||
#if TARGET_OS_OSX
|
#if TARGET_OS_OSX
|
||||||
|
|
||||||
@ -326,11 +286,16 @@
|
|||||||
|
|
||||||
@implementation AppDelegate
|
@implementation AppDelegate
|
||||||
|
|
||||||
- (instancetype)init
|
-(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
|
||||||
|
{
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
|
||||||
|
-(instancetype)init
|
||||||
{
|
{
|
||||||
if (self = [super init])
|
if (self = [super init])
|
||||||
{
|
{
|
||||||
NSViewController *rootViewController = [[ViewController alloc] initWithNibName:nil bundle:nil];
|
NSViewController *rootViewController = [[AppViewController alloc] initWithNibName:nil bundle:nil];
|
||||||
self.window = [[NSWindow alloc] initWithContentRect:NSZeroRect
|
self.window = [[NSWindow alloc] initWithContentRect:NSZeroRect
|
||||||
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable
|
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable
|
||||||
backing:NSBackingStoreBuffered
|
backing:NSBackingStoreBuffered
|
||||||
@ -343,11 +308,6 @@
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
|
|
||||||
{
|
|
||||||
return YES;
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#else
|
#else
|
||||||
@ -358,10 +318,10 @@
|
|||||||
|
|
||||||
@implementation AppDelegate
|
@implementation AppDelegate
|
||||||
|
|
||||||
- (BOOL)application:(UIApplication *)application
|
-(BOOL)application:(UIApplication *)application
|
||||||
didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions
|
didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions
|
||||||
{
|
{
|
||||||
UIViewController *rootViewController = [[ViewController alloc] init];
|
UIViewController *rootViewController = [[AppViewController alloc] init];
|
||||||
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
|
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
|
||||||
self.window.rootViewController = rootViewController;
|
self.window.rootViewController = rootViewController;
|
||||||
[self.window makeKeyAndVisible];
|
[self.window makeKeyAndVisible];
|
||||||
@ -372,7 +332,9 @@
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#pragma mark - main()
|
//-----------------------------------------------------------------------------------
|
||||||
|
// Application main() function
|
||||||
|
//-----------------------------------------------------------------------------------
|
||||||
|
|
||||||
#if TARGET_OS_OSX
|
#if TARGET_OS_OSX
|
||||||
|
|
||||||
|
@ -2,30 +2,25 @@
|
|||||||
// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
|
// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
|
||||||
// Read online: https://github.com/ocornut/imgui/tree/master/docs
|
// Read online: https://github.com/ocornut/imgui/tree/master/docs
|
||||||
|
|
||||||
#include "imgui.h"
|
|
||||||
#include "../../backends/imgui_impl_osx.h"
|
|
||||||
#include "../../backends/imgui_impl_opengl2.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#import <Cocoa/Cocoa.h>
|
#import <Cocoa/Cocoa.h>
|
||||||
#import <OpenGL/gl.h>
|
#import <OpenGL/gl.h>
|
||||||
#import <OpenGL/glu.h>
|
#import <OpenGL/glu.h>
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#include "imgui_impl_opengl2.h"
|
||||||
|
#include "imgui_impl_osx.h"
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------------
|
||||||
// ImGuiExampleView
|
// AppView
|
||||||
//-----------------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------------
|
||||||
|
|
||||||
@interface ImGuiExampleView : NSOpenGLView
|
@interface AppView : NSOpenGLView
|
||||||
{
|
{
|
||||||
NSTimer* animationTimer;
|
NSTimer* animationTimer;
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation ImGuiExampleView
|
@implementation AppView
|
||||||
|
|
||||||
-(void)animationTimerFired:(NSTimer*)timer
|
|
||||||
{
|
|
||||||
[self setNeedsDisplay:YES];
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void)prepareOpenGL
|
-(void)prepareOpenGL
|
||||||
{
|
{
|
||||||
@ -39,6 +34,49 @@
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-(void)initialize
|
||||||
|
{
|
||||||
|
// Some events do not raise callbacks of AppView in some circumstances (for example when CMD key is held down).
|
||||||
|
// This monitor taps into global event stream and captures these events.
|
||||||
|
NSEventMask eventMask = NSEventMaskKeyDown | NSEventMaskKeyUp | NSEventMaskFlagsChanged;
|
||||||
|
[NSEvent addLocalMonitorForEventsMatchingMask:eventMask handler:^NSEvent * _Nullable(NSEvent *event)
|
||||||
|
{
|
||||||
|
ImGui_ImplOSX_HandleEvent(event, self);
|
||||||
|
return event;
|
||||||
|
}];
|
||||||
|
|
||||||
|
// Setup Dear ImGui context
|
||||||
|
// FIXME: This example doesn't have proper cleanup...
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
ImGui::CreateContext();
|
||||||
|
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
||||||
|
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
||||||
|
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
||||||
|
|
||||||
|
// Setup Dear ImGui style
|
||||||
|
ImGui::StyleColorsDark();
|
||||||
|
//ImGui::StyleColorsClassic();
|
||||||
|
|
||||||
|
// Setup Platform/Renderer backends
|
||||||
|
ImGui_ImplOSX_Init();
|
||||||
|
ImGui_ImplOpenGL2_Init();
|
||||||
|
|
||||||
|
// Load Fonts
|
||||||
|
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
|
||||||
|
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
|
||||||
|
// - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
|
||||||
|
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
|
||||||
|
// - Read 'docs/FONTS.txt' for more instructions and details.
|
||||||
|
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
|
||||||
|
//io.Fonts->AddFontDefault();
|
||||||
|
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
|
||||||
|
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
|
||||||
|
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
|
||||||
|
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
|
||||||
|
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
|
||||||
|
//IM_ASSERT(font != NULL);
|
||||||
|
}
|
||||||
|
|
||||||
-(void)updateAndDrawDemoView
|
-(void)updateAndDrawDemoView
|
||||||
{
|
{
|
||||||
// Start the Dear ImGui frame
|
// Start the Dear ImGui frame
|
||||||
@ -90,15 +128,15 @@
|
|||||||
|
|
||||||
// Rendering
|
// Rendering
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
[[self openGLContext] makeCurrentContext];
|
|
||||||
|
|
||||||
ImDrawData* draw_data = ImGui::GetDrawData();
|
ImDrawData* draw_data = ImGui::GetDrawData();
|
||||||
|
|
||||||
|
[[self openGLContext] makeCurrentContext];
|
||||||
GLsizei width = (GLsizei)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
|
GLsizei width = (GLsizei)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
|
||||||
GLsizei height = (GLsizei)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
|
GLsizei height = (GLsizei)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
|
|
||||||
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
|
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
ImGui_ImplOpenGL2_RenderDrawData(draw_data);
|
ImGui_ImplOpenGL2_RenderDrawData(draw_data);
|
||||||
|
|
||||||
// Present
|
// Present
|
||||||
@ -108,38 +146,20 @@
|
|||||||
animationTimer = [NSTimer scheduledTimerWithTimeInterval:0.017 target:self selector:@selector(animationTimerFired:) userInfo:nil repeats:YES];
|
animationTimer = [NSTimer scheduledTimerWithTimeInterval:0.017 target:self selector:@selector(animationTimerFired:) userInfo:nil repeats:YES];
|
||||||
}
|
}
|
||||||
|
|
||||||
-(void)reshape
|
-(void)reshape { [[self openGLContext] update]; [self updateAndDrawDemoView]; }
|
||||||
{
|
-(void)drawRect:(NSRect)bounds { [self updateAndDrawDemoView]; }
|
||||||
[[self openGLContext] update];
|
-(void)animationTimerFired:(NSTimer*)timer { [self setNeedsDisplay:YES]; }
|
||||||
[self updateAndDrawDemoView];
|
-(BOOL)acceptsFirstResponder { return (YES); }
|
||||||
}
|
-(BOOL)becomeFirstResponder { return (YES); }
|
||||||
|
-(BOOL)resignFirstResponder { return (YES); }
|
||||||
|
-(void)dealloc { animationTimer = nil; }
|
||||||
|
|
||||||
-(void)drawRect:(NSRect)bounds
|
//-----------------------------------------------------------------------------------
|
||||||
{
|
// Input processing
|
||||||
[self updateAndDrawDemoView];
|
//-----------------------------------------------------------------------------------
|
||||||
}
|
|
||||||
|
|
||||||
-(BOOL)acceptsFirstResponder
|
|
||||||
{
|
|
||||||
return (YES);
|
|
||||||
}
|
|
||||||
|
|
||||||
-(BOOL)becomeFirstResponder
|
|
||||||
{
|
|
||||||
return (YES);
|
|
||||||
}
|
|
||||||
|
|
||||||
-(BOOL)resignFirstResponder
|
|
||||||
{
|
|
||||||
return (YES);
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void)dealloc
|
|
||||||
{
|
|
||||||
animationTimer = nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Forward Mouse/Keyboard events to Dear ImGui OSX backend.
|
// Forward Mouse/Keyboard events to Dear ImGui OSX backend.
|
||||||
|
// Other events are registered via addLocalMonitorForEventsMatchingMask()
|
||||||
-(void)mouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
-(void)mouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
||||||
-(void)rightMouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
-(void)rightMouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
||||||
-(void)otherMouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
-(void)otherMouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
||||||
@ -147,23 +167,24 @@
|
|||||||
-(void)rightMouseUp:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
-(void)rightMouseUp:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
||||||
-(void)otherMouseUp:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
-(void)otherMouseUp:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
||||||
-(void)mouseMoved:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
-(void)mouseMoved:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
||||||
-(void)rightMouseMoved:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
|
||||||
-(void)otherMouseMoved:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
|
||||||
-(void)mouseDragged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
-(void)mouseDragged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
||||||
|
-(void)rightMouseMoved:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
||||||
-(void)rightMouseDragged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
-(void)rightMouseDragged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
||||||
|
-(void)otherMouseMoved:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
||||||
-(void)otherMouseDragged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
-(void)otherMouseDragged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
||||||
|
-(void)scrollWheel:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); }
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------------
|
||||||
// ImGuiExampleAppDelegate
|
// AppDelegate
|
||||||
//-----------------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------------
|
||||||
|
|
||||||
@interface ImGuiExampleAppDelegate : NSObject <NSApplicationDelegate>
|
@interface AppDelegate : NSObject <NSApplicationDelegate>
|
||||||
@property (nonatomic, readonly) NSWindow* window;
|
@property (nonatomic, readonly) NSWindow* window;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation ImGuiExampleAppDelegate
|
@implementation AppDelegate
|
||||||
@synthesize window = _window;
|
@synthesize window = _window;
|
||||||
|
|
||||||
-(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
|
-(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
|
||||||
@ -228,7 +249,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
|
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
|
||||||
ImGuiExampleView* view = [[ImGuiExampleView alloc] initWithFrame:self.window.frame pixelFormat:format];
|
AppView* view = [[AppView alloc] initWithFrame:self.window.frame pixelFormat:format];
|
||||||
format = nil;
|
format = nil;
|
||||||
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
|
||||||
if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
|
if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
|
||||||
@ -239,55 +260,21 @@
|
|||||||
if ([view openGLContext] == nil)
|
if ([view openGLContext] == nil)
|
||||||
NSLog(@"No OpenGL Context!");
|
NSLog(@"No OpenGL Context!");
|
||||||
|
|
||||||
// Some events do not raise callbacks of ImGuiExampleView in some circumstances (for example when CMD key is held down).
|
[view initialize];
|
||||||
// This monitor taps into global event stream and captures these events.
|
|
||||||
NSEventMask eventMask = NSEventMaskKeyDown | NSEventMaskKeyUp | NSEventMaskFlagsChanged | NSEventTypeScrollWheel;
|
|
||||||
[NSEvent addLocalMonitorForEventsMatchingMask:eventMask handler:^NSEvent * _Nullable(NSEvent *event)
|
|
||||||
{
|
|
||||||
ImGui_ImplOSX_HandleEvent(event, view);
|
|
||||||
return event;
|
|
||||||
}];
|
|
||||||
|
|
||||||
// Setup Dear ImGui context
|
|
||||||
// FIXME: This example doesn't have proper cleanup...
|
|
||||||
IMGUI_CHECKVERSION();
|
|
||||||
ImGui::CreateContext();
|
|
||||||
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
|
||||||
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
|
||||||
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
|
||||||
|
|
||||||
// Setup Dear ImGui style
|
|
||||||
ImGui::StyleColorsDark();
|
|
||||||
//ImGui::StyleColorsClassic();
|
|
||||||
|
|
||||||
// Setup Platform/Renderer backends
|
|
||||||
ImGui_ImplOSX_Init();
|
|
||||||
ImGui_ImplOpenGL2_Init();
|
|
||||||
|
|
||||||
// Load Fonts
|
|
||||||
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
|
|
||||||
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
|
|
||||||
// - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
|
|
||||||
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
|
|
||||||
// - Read 'docs/FONTS.txt' for more instructions and details.
|
|
||||||
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
|
|
||||||
//io.Fonts->AddFontDefault();
|
|
||||||
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
|
|
||||||
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
|
|
||||||
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
|
|
||||||
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
|
|
||||||
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
|
|
||||||
//IM_ASSERT(font != NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------------
|
||||||
|
// Application main() function
|
||||||
|
//-----------------------------------------------------------------------------------
|
||||||
|
|
||||||
int main(int argc, const char* argv[])
|
int main(int argc, const char* argv[])
|
||||||
{
|
{
|
||||||
@autoreleasepool
|
@autoreleasepool
|
||||||
{
|
{
|
||||||
NSApp = [NSApplication sharedApplication];
|
NSApp = [NSApplication sharedApplication];
|
||||||
ImGuiExampleAppDelegate* delegate = [[ImGuiExampleAppDelegate alloc] init];
|
AppDelegate* delegate = [[AppDelegate alloc] init];
|
||||||
[[NSApplication sharedApplication] setDelegate:delegate];
|
[[NSApplication sharedApplication] setDelegate:delegate];
|
||||||
[NSApp run];
|
[NSApp run];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user