Window.hx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package dx;
  2. import haxe.EntryPoint;
  3. private typedef WinPtr = hl.Abstract<"dx_window">;
  4. typedef MonitorHandle = String;
  5. typedef Monitor = {
  6. name : MonitorHandle,
  7. left : Int,
  8. right : Int,
  9. top : Int,
  10. bottom : Int
  11. }
  12. typedef DisplaySetting = {
  13. width : Int,
  14. height : Int,
  15. framerate : Int
  16. }
  17. enum abstract DisplayMode(Int) {
  18. var Windowed = 0;
  19. var Fullscreen = 1;
  20. var Borderless = 2;
  21. }
  22. @:hlNative("directx")
  23. class Window {
  24. static var windows : Array<Window> = [];
  25. public static inline var CW_USEDEFAULT = 0x80000000;
  26. public static inline var HIDDEN = 0x000001;
  27. public static inline var RESIZABLE = 0x000002;
  28. static var _UID = 0;
  29. var win : WinPtr;
  30. public var id(default,null) : Int;
  31. public var title(default, set) : String;
  32. public var width(get, never) : Int;
  33. public var height(get, never) : Int;
  34. public var minWidth(get, never) : Int;
  35. public var minHeight(get, never) : Int;
  36. public var maxWidth(get, never) : Int;
  37. public var maxHeight(get, never) : Int;
  38. public var x(get, never) : Int;
  39. public var y(get, never) : Int;
  40. public var displayMode(default, set) : DisplayMode;
  41. public var visible(default, set) : Bool = true;
  42. public var opacity(get, set) : Float;
  43. public var displaySetting : DisplaySetting;
  44. public var selectedMonitor : MonitorHandle;
  45. public var vsync : Bool;
  46. public var dragAndDropEnabled(default, set) : Bool;
  47. public function new( title : String, width : Int, height : Int, x : Int = CW_USEDEFAULT, y : Int = CW_USEDEFAULT, windowFlags : Int = RESIZABLE ) {
  48. win = winCreateEx(x, y, width, height, windowFlags);
  49. this.title = title;
  50. windows.push(this);
  51. vsync = true;
  52. id = ++_UID;
  53. }
  54. function set_title(name:String) {
  55. winSetTitle(win, @:privateAccess name.bytes);
  56. return title = name;
  57. }
  58. function set_displayMode(mode) {
  59. displayMode = mode;
  60. if(mode == Windowed) {
  61. dx.Window.winChangeDisplaySetting(selectedMonitor != null ? @:privateAccess selectedMonitor.bytes : null, null);
  62. winSetFullscreen(win, false);
  63. }
  64. else if(mode == Borderless) {
  65. dx.Window.winChangeDisplaySetting(selectedMonitor != null ? @:privateAccess selectedMonitor.bytes : null, null);
  66. winSetFullscreen(win,true);
  67. }
  68. else {
  69. var r = dx.Window.winChangeDisplaySetting(selectedMonitor != null ? @:privateAccess selectedMonitor.bytes : null, displaySetting);
  70. winSetFullscreen(win,true);
  71. }
  72. return mode;
  73. }
  74. function set_visible(b) {
  75. if( visible == b )
  76. return b;
  77. winResize(win, b ? 4 : 3);
  78. return visible = b;
  79. }
  80. public function resize( width : Int, height : Int ) {
  81. winSetSize(win, width, height);
  82. }
  83. public function setMinSize( width : Int, height : Int ) {
  84. winSetMinSize(win, width, height);
  85. }
  86. public function setMaxSize( width : Int, height : Int ) {
  87. winSetMaxSize(win, width, height);
  88. }
  89. public function setPosition( x : Int, y : Int ) {
  90. winSetPosition(win, x, y);
  91. }
  92. public function setCursorPosition( x : Int, y : Int ) {
  93. return winSetCursorPos(win, x, y);
  94. }
  95. public static function setCursorPositionGlobal( x : Int, y : Int ) {
  96. return setCursorPos(x, y);
  97. }
  98. public function setRelativeMouseMode( enabled : Bool ) : Bool {
  99. return winSetRelativeMouseMode(win, enabled);
  100. }
  101. public function getRelativeMouseMode() : Bool {
  102. return winGetRelativeMouseMode();
  103. }
  104. public function center( centerPrimary : Bool = true ) {
  105. winCenter(win, centerPrimary);
  106. }
  107. public function destroy() {
  108. winDestroy(win);
  109. win = null;
  110. windows.remove(this);
  111. }
  112. public function maximize() {
  113. winResize(win, 0);
  114. }
  115. public function minimize() {
  116. winResize(win, 1);
  117. }
  118. public function restore() {
  119. winResize(win, 2);
  120. }
  121. public function focus() {
  122. winSetFocus(win);
  123. }
  124. public function getNextEvent( e : Event ) : Bool {
  125. return winGetNextEvent(win, e);
  126. }
  127. public function clipCursor( enable : Bool ) : Void {
  128. winClipCursor(win, enable);
  129. }
  130. public static function getDisplaySettings(monitor : MonitorHandle) : Array<DisplaySetting> {
  131. var a : Array<DisplaySetting> = [for(s in winGetDisplaySettings(monitor != null ? @:privateAccess monitor.bytes : null)) s];
  132. a.sort((a, b) -> {
  133. if(b.width > a.width) 1;
  134. else if(b.width < a.width) -1;
  135. else if(b.height > a.height) 1;
  136. else if(b.height < a.height) -1;
  137. else if(b.framerate > a.framerate) 1;
  138. else if(b.framerate < a.framerate) -1;
  139. else 0;
  140. });
  141. var last = null;
  142. return a.filter(function(e) {
  143. if(last == null) {
  144. last = e;
  145. return true;
  146. }
  147. else if(last.width == e.width && last.height == e.height && last.framerate == e.framerate) {
  148. return false;
  149. }
  150. last = e;
  151. return true;
  152. });
  153. }
  154. public static function getCurrentDisplaySetting(monitor : MonitorHandle, registry : Bool = false) : DisplaySetting {
  155. return winGetCurrentDisplaySetting(monitor != null ? @:privateAccess monitor.bytes : null, registry);
  156. }
  157. public static function getMonitors() : Array<Monitor> {
  158. var last = null;
  159. return [for(m in winGetMonitors()) @:privateAccess { name: String.fromUCS2(m.name), left: m.left, right: m.right, top: m.top, bottom: m.bottom } ];
  160. }
  161. public function getCurrentMonitor() : MonitorHandle {
  162. return @:privateAccess String.fromUCS2(winGetMonitorFromWindow(win));
  163. }
  164. function get_width() {
  165. var w = 0;
  166. winGetSize(win, w, null);
  167. return w;
  168. }
  169. function get_height() {
  170. var h = 0;
  171. winGetSize(win, null, h);
  172. return h;
  173. }
  174. function get_minWidth() {
  175. var w = 0;
  176. winGetMinSize(win, w, null);
  177. return w;
  178. }
  179. function get_minHeight() {
  180. var h = 0;
  181. winGetMinSize(win, null, h);
  182. return h;
  183. }
  184. function get_maxWidth() {
  185. var w = 0;
  186. winGetMaxSize(win, w, null);
  187. return w;
  188. }
  189. function get_maxHeight() {
  190. var h = 0;
  191. winGetMaxSize(win, null, h);
  192. return h;
  193. }
  194. function get_x() {
  195. var x = 0;
  196. winGetPosition(win, x, null);
  197. return x;
  198. }
  199. function get_y() {
  200. var y = 0;
  201. winGetPosition(win, null, y);
  202. return y;
  203. }
  204. function get_opacity() {
  205. return winGetOpacity(win);
  206. }
  207. function set_opacity(v) {
  208. winSetOpacity(win, v);
  209. return v;
  210. }
  211. function set_dragAndDropEnabled(v) {
  212. winSetDragAcceptFiles(win, v);
  213. return dragAndDropEnabled = v;
  214. }
  215. @:hlNative("?directx", "win_get_display_settings")
  216. static function winGetDisplaySettings(monitor : hl.Bytes) : hl.NativeArray<Dynamic> {
  217. return null;
  218. }
  219. @:hlNative("?directx", "win_get_current_display_setting")
  220. static function winGetCurrentDisplaySetting(monitor : hl.Bytes, registry : Bool) : Dynamic {
  221. return null;
  222. }
  223. @:hlNative("?directx", "win_change_display_setting")
  224. public static function winChangeDisplaySetting(monitor : hl.Bytes, ds : Dynamic) : Int {
  225. return 0;
  226. }
  227. @:hlNative("?directx", "win_get_monitors")
  228. static function winGetMonitors() : hl.NativeArray<Dynamic> {
  229. return null;
  230. }
  231. @:hlNative("?directx", "win_get_monitor_from_window")
  232. static function winGetMonitorFromWindow( win : WinPtr ) : hl.Bytes {
  233. return null;
  234. }
  235. static function winCreateEx( x : Int, y : Int, width : Int, height : Int, windowFlags : Int ) : WinPtr {
  236. return null;
  237. }
  238. static function winCreate( width : Int, height : Int ) : WinPtr {
  239. return null;
  240. }
  241. static function winSetTitle( win : WinPtr, title : hl.Bytes ) {
  242. }
  243. static function winSetFullscreen( win : WinPtr, fs : Bool ) {
  244. }
  245. static function winSetSize( win : WinPtr, width : Int, height : Int ) {
  246. }
  247. static function winSetMinSize( win : WinPtr, width : Int, height : Int ) {
  248. }
  249. static function winSetMaxSize( win : WinPtr, width : Int, height : Int ) {
  250. }
  251. static function winSetPosition( win : WinPtr, x : Int, y : Int ) {
  252. }
  253. static function winCenter( win : WinPtr, centerPrimary : Bool ) {
  254. }
  255. static function winResize( win : WinPtr, mode : Int ) {
  256. }
  257. static function winGetSize( win : WinPtr, width : hl.Ref<Int>, height : hl.Ref<Int> ) {
  258. }
  259. static function winGetMinSize( win : WinPtr, width : hl.Ref<Int>, height : hl.Ref<Int> ) {
  260. }
  261. static function winGetMaxSize( win : WinPtr, width : hl.Ref<Int>, height : hl.Ref<Int> ) {
  262. }
  263. static function winGetPosition( win : WinPtr, x : hl.Ref<Int>, y : hl.Ref<Int> ) {
  264. }
  265. @:hlNative("?directx", "win_set_focus")
  266. static function winSetFocus( win: WinPtr ) {
  267. }
  268. static function winGetOpacity( win : WinPtr ) : Float {
  269. return 0.0;
  270. }
  271. static function winSetOpacity( win : WinPtr, opacity : Float ) : Bool {
  272. return false;
  273. }
  274. static function winDestroy( win : WinPtr ) {
  275. }
  276. public static function getScreenWidth() {
  277. return 0;
  278. }
  279. public static function getScreenHeight() {
  280. return 0;
  281. }
  282. static function winGetNextEvent( win : WinPtr, event : Dynamic ) : Bool {
  283. return false;
  284. }
  285. static function winClipCursor( win : WinPtr, enable : Bool ) : Void {
  286. }
  287. static function setCursorPos( x : Int, y : Int ) : Bool {
  288. return false;
  289. }
  290. static function winSetCursorPos( win : WinPtr, x : Int, y : Int ) : Bool {
  291. return false;
  292. }
  293. static function winSetRelativeMouseMode( win : WinPtr, enable : Bool ) : Bool {
  294. return false;
  295. }
  296. static function winGetRelativeMouseMode() : Bool {
  297. return false;
  298. }
  299. @:hlNative("?directx", "win_set_drag_accept_files")
  300. static function winSetDragAcceptFiles( win : WinPtr, enable: Bool ) : Void {
  301. }
  302. public static function detectKeyboardLayout() @:privateAccess {
  303. return String.fromUTF8( dxDetectKeyboardLayout() );
  304. }
  305. @:hlNative("directx", "detect_keyboard_layout")
  306. static function dxDetectKeyboardLayout() : hl.Bytes {
  307. return null;
  308. }
  309. }