yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
5b9931456
master
1// window.h 2#pragma once 3 4#include "core/slang-basic.h" 5#include "core/slang-func-ptr.h" 6#include "platform-api.h" 7#include "slang-com-ptr.h" 8 9namespace platform 10{ 11 12enum class KeyCode :uint32_t 13{ 14None = 0 , 15Left = 0x25 , 16Up = 0x26 , 17Down = 0x28 , 18Right = 0x27 , 19Escape = 0x1B , 20Return = 0x0D , 21Space = 0x20 , 22Shift = 0x10 , 23Ctrl = 0x11 , 24Alt = 0x12 , 25Backspace = 0x08 , 26Delete = 0x2E , 27Home = 0x24 , 28End = 0x23 , 29PageUp = 0x21 , 30PageDown = 0x22 , 31Insert = 0x2D , 32Tab = 0x09 , 33A = 0x41 , 34B = 0x42 , 35C = 0x43 , 36D = 0x44 , 37E = 0x45 , 38F = 0x46 , 39G = 0x47 , 40H = 0x48 , 41I = 0x49 , 42J = 0x4A , 43K = 0x4B , 44L = 0x4C , 45M = 0x4D , 46N = 0x4E , 47O = 0x4F , 48P = 0x50 , 49Q = 0x51 , 50R = 0x52 , 51S = 0x53 , 52T = 0x54 , 53U = 0x55 , 54V = 0x56 , 55W = 0x57 , 56X = 0x58 , 57Y = 0x59 , 58Z = 0x5A , 59Semicolon = 0xBA , 60Comma = 0xBC , 61Dot = 0xBE , 62Slash = 0xBF , 63Quote = 0xDE , 64LBracket = 0xDB , 65RBracket = 0xDD , 66Backslash = 0xDC , 67Minus = 0xBD , 68Plus = 0xBB , 69Tilde = 0xC0 , 70Key0 = 0x30 , 71Key1 = 0x31 , 72Key2 = 0x32 , 73Key3 = 0x33 , 74Key4 = 0x34 , 75Key5 = 0x35 , 76Key6 = 0x36 , 77Key7 = 0x37 , 78Key8 = 0x38 , 79Key9 = 0x39 , 80F1 = 0x70 , 81F2 = 0x71 , 82F3 = 0x72 , 83F4 = 0x73 , 84F5 = 0x74 , 85F6 = 0x75 , 86F7 = 0x76 , 87F8 = 0x77 , 88F9 = 0x78 , 89F10 = 0x79 , 90F11 = 0x7A , 91F12 = 0x7B , 92}; 93 94struct WindowHandle 95{ 96enum class Type 97 { 98Unknown , 99Win32Handle , 100NSWindowHandle , 101XLibHandle , 102 }; 103Type type ; 104intptr_t handleValues [2 ]; 105static WindowHandle fromHwnd (void * hwnd ) 106 { 107WindowHandle handle = {}; 108handle .type = WindowHandle ::Type ::Win32Handle ; 109handle .handleValues [0 ]= (intptr_t )(hwnd ); 110return handle ; 111 } 112static WindowHandle fromNSWindow (void * nswindow ) 113 { 114WindowHandle handle = {}; 115handle .type = WindowHandle ::Type ::NSWindowHandle ; 116handle .handleValues [0 ]= (intptr_t )(nswindow ); 117return handle ; 118 } 119static WindowHandle fromXWindow (void * xdisplay ,uint32_t xwindow ) 120 { 121WindowHandle handle = {}; 122handle .type = WindowHandle ::Type ::XLibHandle ; 123handle .handleValues [0 ]= (intptr_t )(xdisplay ); 124handle .handleValues [1 ]= xwindow ; 125return handle ; 126 } 127template < typename T > 128T convert () 129 { 130T result ; 131result .type = (decltype (result .type ))type ; 132result .handleValues [0 ]= handleValues [0 ]; 133result .handleValues [1 ]= handleValues [1 ]; 134return result ; 135 } 136}; 137 138struct ButtonState 139{ 140enum Enum 141 { 142None = 0 , 143LeftButton = 1 , 144RightButton = 2 , 145MiddleButton = 4 , 146Shift = 8 , 147Control = 16 , 148Alt = 32 149 }; 150}; 151 152struct KeyEventArgs 153{ 154KeyCode key ; 155wchar_t keyChar ;// For KeyPress event 156ButtonState ::Enum buttons ; 157bool cancelEvent ; 158}; 159 160struct MouseEventArgs 161{ 162int x ,y ; 163int delta ; 164ButtonState ::Enum buttons ; 165}; 166 167struct Rect 168{ 169int x ,y ; 170int width ,height ; 171}; 172 173enum class WindowStyle 174{ 175Default , 176FixedSize , 177}; 178 179struct WindowDesc 180{ 181char const * title = nullptr; 182int width = 0 ; 183int height = 0 ; 184WindowStyle style = WindowStyle ::Default ; 185}; 186 187class Window :public Slang ::RefObject 188{ 189public : 190struct Events 191 { 192Slang ::Action <> mainLoop ; 193Slang ::Action <> sizeChanged ; 194Slang ::Action <> focus ; 195Slang ::Action <> lostFocus ; 196Slang ::Action < KeyEventArgs &> keyDown ; 197Slang ::Action < KeyEventArgs &> keyUp ; 198Slang ::Action < KeyEventArgs &> keyPress ; 199Slang ::Action < MouseEventArgs > mouseMove ; 200Slang ::Action < MouseEventArgs > mouseUp ; 201Slang ::Action < MouseEventArgs > mouseDown ; 202Slang ::Action < MouseEventArgs > mouseWheel ; 203 }; 204 205Events events; 206 207virtual void setClientSize ( uint32_t width, uint32_t height) = 0 ; 208virtual Rect getClientRect () = 0 ; 209virtual void centerScreen () = 0 ; 210virtual void close () = 0 ; 211virtual bool getFocused () = 0 ; 212virtual bool getVisible () = 0 ; 213virtual WindowHandle getNativeHandle () = 0 ; 214virtual void setText ( Slang ::String text) = 0 ; 215virtual void show () = 0 ; 216virtual void hide () = 0 ; 217virtual int getCurrentDpi () = 0 ; 218}; 219 220class Application 221{ 222public : 223static SLANG_PLATFORM_API Window * createWindow ( const WindowDesc & desc); 224static SLANG_PLATFORM_API void init (); 225static SLANG_PLATFORM_API void run ( Window * mainWindow, bool waitForEvents = false); 226static SLANG_PLATFORM_API void quit (); 227static SLANG_PLATFORM_API void doEvents (); 228static SLANG_PLATFORM_API void dispose (); 229}; 230 231} // namespace platform 232 233#ifdef _WIN32 234 235#ifdef _MSC_VER 236#ifdef _DEBUG 237#define GFX_DUMP_LEAK _CrtDumpMemoryLeaks(); 238#endif 239#endif 240 241#endif 242 243#ifndef GFX_DUMP_LEAK 244#define GFX_DUMP_LEAK 245#endif 246 247#define PLATFORM_UI_MAIN ( APPLICATION_ENTRY ) \ 248 int exampleMain(int argc, char** argv) \ 249 { \ 250 platform::Application::init(); \ 251 auto rs = APPLICATION_ENTRY(argc, argv); \ 252 platform::Application::dispose(); \ 253 GFX_DUMP_LEAK \ 254 return rs; \ 255 }