UI.hx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C)2005-2018 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package hl;
  23. typedef SentinelHandle = hl.Abstract<"ui_sentinel">;
  24. abstract Sentinel(SentinelHandle) {
  25. public var pause(get,set) : Bool;
  26. public function new( timeout, callback ) {
  27. this = create_sentinel(timeout,callback);
  28. }
  29. function get_pause() {
  30. return is_paused(this);
  31. }
  32. function set_pause( p : Bool ) {
  33. _pause(this, p);
  34. return p;
  35. }
  36. public function tick() {
  37. _tick(this);
  38. }
  39. @:hlNative("ui", "ui_start_sentinel") static function create_sentinel( timeout : Float, callb : Void -> Void ) : SentinelHandle {
  40. return null;
  41. }
  42. @:hlNative("ui","ui_sentinel_tick") static function _tick( h : SentinelHandle ) : Void {}
  43. @:hlNative("ui","ui_sentinel_pause") static function _pause( h : SentinelHandle, b : Bool ) : Void {}
  44. @:hlNative("ui","ui_sentinel_is_paused") static function is_paused( h : SentinelHandle ) : Bool { return false; }
  45. }
  46. typedef WinHandle = hl.Abstract<"ui_window">;
  47. class Window {
  48. var h : WinHandle;
  49. public function setText( text : String ) {
  50. win_set_text(h, @:privateAccess text.bytes);
  51. }
  52. public function setEnable( b : Bool ) {
  53. win_set_enable(h, b);
  54. }
  55. public function destroy() {
  56. win_destroy(h);
  57. }
  58. @:hlNative("ui","ui_win_destroy")
  59. static function win_destroy( win : WinHandle ) : Void {
  60. }
  61. @:hlNative("ui","ui_win_set_text")
  62. static function win_set_text( win : WinHandle, text : hl.Bytes ) : Void {
  63. }
  64. @:hlNative("ui","ui_win_set_enable")
  65. static function win_set_enable( win : WinHandle, enable : Bool ) : Void {
  66. }
  67. }
  68. class Button extends Window {
  69. public function new( parent : Window, text : String ) {
  70. h = button_new(parent.h, @:privateAccess text.bytes, function() this.onClick());
  71. }
  72. public dynamic function onClick() {
  73. }
  74. @:hlNative("ui", "ui_button_new")
  75. static function button_new( parent : WinHandle, text : hl.Bytes, onClick : Void -> Void ) : WinHandle {
  76. return null;
  77. }
  78. }
  79. class WinLog extends Window {
  80. public function new( title : String, width, height ) {
  81. h = winlog_new(@:privateAccess title.bytes,width, height);
  82. }
  83. public function setTextContent( text : String, autoScroll = false ) {
  84. winlog_set_text(h, @:privateAccess text.bytes,autoScroll);
  85. }
  86. @:hlNative("ui","ui_winlog_new")
  87. static function winlog_new( text : hl.Bytes, width : Int, height : Int ) : WinHandle {
  88. return null;
  89. }
  90. @:hlNative("ui","ui_winlog_set_text")
  91. static function winlog_set_text( win : WinHandle, text : hl.Bytes, autoScroll : Bool ) : Void {
  92. }
  93. }
  94. enum DialogFlags {
  95. YesNo;
  96. IsError;
  97. }
  98. enum abstract LoopResult(Int) {
  99. var NoMessage = 0;
  100. var HandledMessage = 1;
  101. var Quit = 2;
  102. }
  103. typedef FileOptions = {
  104. @:optional var window : Window;
  105. @:optional var filters : Array<{ name : String, exts : Array<String> }>;
  106. @:optional var filterIndex : Int;
  107. @:optional var fileName : String;
  108. @:optional var title : String;
  109. }
  110. /**
  111. These are the bindings for the HL `ui.hdll` library, which contains some low level system access.
  112. **/
  113. class UI {
  114. @:hlNative("ui","ui_init") static function init() {}
  115. static function __init__() {
  116. init();
  117. }
  118. @:hlNative("ui","ui_dialog")
  119. static function _dialog( title : hl.Bytes, text : hl.Bytes, flags : Int ) : Int {
  120. return 0;
  121. }
  122. public static function dialog( title : String, text : String, flags : haxe.EnumFlags<DialogFlags> ) {
  123. @:privateAccess _dialog(title.bytes,text.bytes,flags.toInt());
  124. }
  125. @:hlNative("ui","ui_loop")
  126. public static function loop( blocking : Bool ) : LoopResult {
  127. return Quit;
  128. }
  129. @:hlNative("ui","ui_stop_loop")
  130. public static function stopLoop() : Void {
  131. }
  132. @:hlNative("ui","ui_close_console")
  133. public static function closeConsole() : Void {
  134. }
  135. public static function loadFile( opts : FileOptions ) {
  136. return chooseFile(false, opts);
  137. }
  138. public static function saveFile( opts : FileOptions ) {
  139. return chooseFile(true, opts);
  140. }
  141. static function chooseFile( save : Bool, opts : FileOptions ) @:privateAccess {
  142. var out : Dynamic = {
  143. };
  144. if( opts.fileName != null ) {
  145. var file = sys.FileSystem.absolutePath(opts.fileName);
  146. if( Sys.systemName() == "Windows" )
  147. file = file.split("/").join("\\");
  148. var dir = null;
  149. if( sys.FileSystem.isDirectory(file) ) {
  150. dir = file;
  151. file = null;
  152. } else {
  153. var path = new haxe.io.Path(file);
  154. dir = path.dir;
  155. file = path.file + (path.ext == null ? "" : "." + path.ext);
  156. }
  157. if( file != null )
  158. out.fileName = file.bytes;
  159. if( dir != null )
  160. out.directory = dir.bytes;
  161. }
  162. if( opts.title != null )
  163. out.title = opts.title.bytes;
  164. if( opts.filters != null ) {
  165. var filters = new hl.NativeArray<hl.Bytes>(opts.filters.length * 2);
  166. var i = 0;
  167. for( f in opts.filters ) {
  168. filters[i++] = f.name.bytes;
  169. filters[i++] = [for( e in f.exts ) "*."+e].join(";").bytes;
  170. }
  171. out.filters = filters;
  172. out.filterIndex = opts.filterIndex;
  173. }
  174. if( opts.window != null )
  175. out.window = opts.window.h;
  176. var str = _chooseFile(save, out);
  177. return str == null ? null : String.fromUCS2(str);
  178. }
  179. @:hlNative("ui","ui_choose_file")
  180. static function _chooseFile( forSave : Bool, obj : Dynamic ) : hl.Bytes {
  181. return null;
  182. }
  183. }