core_bind.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*************************************************************************/
  2. /* core_bind.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef CORE_BIND_H
  31. #define CORE_BIND_H
  32. #include "io/resource_loader.h"
  33. #include "io/resource_saver.h"
  34. #include "os/dir_access.h"
  35. #include "os/file_access.h"
  36. #include "os/semaphore.h"
  37. #include "os/thread.h"
  38. class _ResourceLoader : public Object {
  39. OBJ_TYPE(_ResourceLoader, Object);
  40. protected:
  41. static void _bind_methods();
  42. static _ResourceLoader *singleton;
  43. public:
  44. static _ResourceLoader *get_singleton() { return singleton; }
  45. Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_type_hint = "");
  46. RES load(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false);
  47. DVector<String> get_recognized_extensions_for_type(const String &p_type);
  48. void set_abort_on_missing_resources(bool p_abort);
  49. StringArray get_dependencies(const String &p_path);
  50. bool has(const String &p_path);
  51. Ref<ResourceImportMetadata> load_import_metadata(const String &p_path);
  52. _ResourceLoader();
  53. };
  54. class _ResourceSaver : public Object {
  55. OBJ_TYPE(_ResourceSaver, Object);
  56. protected:
  57. static void _bind_methods();
  58. static _ResourceSaver *singleton;
  59. public:
  60. enum SaverFlags {
  61. FLAG_RELATIVE_PATHS = 1,
  62. FLAG_BUNDLE_RESOURCES = 2,
  63. FLAG_CHANGE_PATH = 4,
  64. FLAG_OMIT_EDITOR_PROPERTIES = 8,
  65. FLAG_SAVE_BIG_ENDIAN = 16,
  66. FLAG_COMPRESS = 32,
  67. };
  68. static _ResourceSaver *get_singleton() { return singleton; }
  69. Error save(const String &p_path, const RES &p_resource, uint32_t p_flags);
  70. DVector<String> get_recognized_extensions(const RES &p_resource);
  71. _ResourceSaver();
  72. };
  73. class MainLoop;
  74. class _OS : public Object {
  75. OBJ_TYPE(_OS, Object);
  76. protected:
  77. static void _bind_methods();
  78. static _OS *singleton;
  79. public:
  80. enum Weekday {
  81. DAY_SUNDAY,
  82. DAY_MONDAY,
  83. DAY_TUESDAY,
  84. DAY_WEDNESDAY,
  85. DAY_THURSDAY,
  86. DAY_FRIDAY,
  87. DAY_SATURDAY
  88. };
  89. enum Month {
  90. /// Start at 1 to follow Windows SYSTEMTIME structure
  91. /// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspx
  92. MONTH_JANUARY = 1,
  93. MONTH_FEBRUARY,
  94. MONTH_MARCH,
  95. MONTH_APRIL,
  96. MONTH_MAY,
  97. MONTH_JUNE,
  98. MONTH_JULY,
  99. MONTH_AUGUST,
  100. MONTH_SEPTEMBER,
  101. MONTH_OCTOBER,
  102. MONTH_NOVEMBER,
  103. MONTH_DECEMBER
  104. };
  105. Point2 get_mouse_pos() const;
  106. void set_window_title(const String &p_title);
  107. int get_mouse_button_state() const;
  108. void set_clipboard(const String &p_text);
  109. String get_clipboard() const;
  110. void set_video_mode(const Size2 &p_size, bool p_fullscreen, bool p_resizeable, int p_screen = 0);
  111. Size2 get_video_mode(int p_screen = 0) const;
  112. bool is_video_mode_fullscreen(int p_screen = 0) const;
  113. bool is_video_mode_resizable(int p_screen = 0) const;
  114. Array get_fullscreen_mode_list(int p_screen = 0) const;
  115. virtual int get_screen_count() const;
  116. virtual int get_current_screen() const;
  117. virtual void set_current_screen(int p_screen);
  118. virtual Point2 get_screen_position(int p_screen = 0) const;
  119. virtual Size2 get_screen_size(int p_screen = 0) const;
  120. virtual int get_screen_dpi(int p_screen = 0) const;
  121. virtual Point2 get_window_position() const;
  122. virtual void set_window_position(const Point2 &p_position);
  123. virtual Size2 get_window_size() const;
  124. virtual Size2 get_real_window_size() const;
  125. virtual void set_window_size(const Size2 &p_size);
  126. virtual void set_window_fullscreen(bool p_enabled);
  127. virtual bool is_window_fullscreen() const;
  128. virtual void set_window_resizable(bool p_enabled);
  129. virtual bool is_window_resizable() const;
  130. virtual void set_window_minimized(bool p_enabled);
  131. virtual bool is_window_minimized() const;
  132. virtual void set_window_maximized(bool p_enabled);
  133. virtual bool is_window_maximized() const;
  134. virtual void set_window_always_on_top(bool p_enabled);
  135. virtual bool is_window_always_on_top() const;
  136. virtual void request_attention();
  137. virtual void center_window();
  138. virtual void set_borderless_window(bool p_borderless);
  139. virtual bool get_borderless_window() const;
  140. Error native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track);
  141. bool native_video_is_playing();
  142. void native_video_pause();
  143. void native_video_unpause();
  144. void native_video_stop();
  145. void set_iterations_per_second(int p_ips);
  146. int get_iterations_per_second() const;
  147. void set_target_fps(int p_fps);
  148. float get_target_fps() const;
  149. void set_low_processor_usage_mode(bool p_enabled);
  150. bool is_in_low_processor_usage_mode() const;
  151. String get_executable_path() const;
  152. int execute(const String &p_path, const Vector<String> &p_arguments, bool p_blocking, Array p_output = Array());
  153. Error kill(int p_pid);
  154. Error shell_open(String p_uri);
  155. int get_process_ID() const;
  156. bool has_environment(const String &p_var) const;
  157. String get_environment(const String &p_var) const;
  158. String get_name() const;
  159. Vector<String> get_cmdline_args();
  160. String get_locale() const;
  161. String get_latin_keyboard_variant() const;
  162. String get_model_name() const;
  163. MainLoop *get_main_loop() const;
  164. String get_custom_level() const;
  165. float get_frames_per_second() const;
  166. void dump_memory_to_file(const String &p_file);
  167. void dump_resources_to_file(const String &p_file);
  168. bool has_virtual_keyboard() const;
  169. void show_virtual_keyboard(const String &p_existing_text = "");
  170. void hide_virtual_keyboard();
  171. void print_resources_in_use(bool p_short = false);
  172. void print_all_resources(const String &p_to_file);
  173. void print_all_textures_by_size();
  174. void print_resources_by_type(const Vector<String> &p_types);
  175. bool has_touchscreen_ui_hint() const;
  176. bool is_debug_build() const;
  177. String get_unique_ID() const;
  178. String get_scancode_string(uint32_t p_code) const;
  179. bool is_scancode_unicode(uint32_t p_unicode) const;
  180. int find_scancode_from_string(const String &p_code) const;
  181. /*
  182. struct Date {
  183. int year;
  184. Month month;
  185. int day;
  186. Weekday weekday;
  187. bool dst;
  188. };
  189. struct Time {
  190. int hour;
  191. int min;
  192. int sec;
  193. };
  194. */
  195. void set_use_file_access_save_and_swap(bool p_enable);
  196. void set_icon(const Image &p_icon);
  197. int get_exit_code() const;
  198. void set_exit_code(int p_code);
  199. Dictionary get_date(bool utc) const;
  200. Dictionary get_time(bool utc) const;
  201. Dictionary get_datetime(bool utc) const;
  202. Dictionary get_datetime_from_unix_time(uint64_t unix_time_val) const;
  203. uint64_t get_unix_time_from_datetime(Dictionary datetime) const;
  204. Dictionary get_time_zone_info() const;
  205. uint64_t get_unix_time() const;
  206. uint64_t get_system_time_secs() const;
  207. int get_static_memory_usage() const;
  208. int get_static_memory_peak_usage() const;
  209. int get_dynamic_memory_usage() const;
  210. void delay_usec(uint32_t p_usec) const;
  211. void delay_msec(uint32_t p_msec) const;
  212. uint32_t get_ticks_msec() const;
  213. uint32_t get_splash_tick_msec() const;
  214. bool can_use_threads() const;
  215. bool can_draw() const;
  216. int get_frames_drawn();
  217. bool is_stdout_verbose() const;
  218. int get_processor_count() const;
  219. enum SystemDir {
  220. SYSTEM_DIR_DESKTOP,
  221. SYSTEM_DIR_DCIM,
  222. SYSTEM_DIR_DOCUMENTS,
  223. SYSTEM_DIR_DOWNLOADS,
  224. SYSTEM_DIR_MOVIES,
  225. SYSTEM_DIR_MUSIC,
  226. SYSTEM_DIR_PICTURES,
  227. SYSTEM_DIR_RINGTONES,
  228. };
  229. enum ScreenOrientation {
  230. SCREEN_ORIENTATION_LANDSCAPE,
  231. SCREEN_ORIENTATION_PORTRAIT,
  232. SCREEN_ORIENTATION_REVERSE_LANDSCAPE,
  233. SCREEN_ORIENTATION_REVERSE_PORTRAIT,
  234. SCREEN_ORIENTATION_SENSOR_LANDSCAPE,
  235. SCREEN_ORIENTATION_SENSOR_PORTRAIT,
  236. SCREEN_ORIENTATION_SENSOR,
  237. };
  238. String get_system_dir(SystemDir p_dir) const;
  239. String get_data_dir() const;
  240. void alert(const String &p_alert, const String &p_title = "ALERT!");
  241. void set_screen_orientation(ScreenOrientation p_orientation);
  242. ScreenOrientation get_screen_orientation() const;
  243. void set_keep_screen_on(bool p_enabled);
  244. bool is_keep_screen_on() const;
  245. void set_time_scale(float p_scale);
  246. float get_time_scale();
  247. bool is_ok_left_and_cancel_right() const;
  248. Error set_thread_name(const String &p_name);
  249. void set_use_vsync(bool p_enable);
  250. bool is_vsync_enabled() const;
  251. Dictionary get_engine_version() const;
  252. static _OS *get_singleton() { return singleton; }
  253. _OS();
  254. };
  255. VARIANT_ENUM_CAST(_OS::SystemDir);
  256. VARIANT_ENUM_CAST(_OS::ScreenOrientation);
  257. class _Geometry : public Object {
  258. OBJ_TYPE(_Geometry, Object);
  259. static _Geometry *singleton;
  260. protected:
  261. static void _bind_methods();
  262. public:
  263. static _Geometry *get_singleton();
  264. DVector<Plane> build_box_planes(const Vector3 &p_extents);
  265. DVector<Plane> build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis = Vector3::AXIS_Z);
  266. DVector<Plane> build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis = Vector3::AXIS_Z);
  267. Variant segment_intersects_segment_2d(const Vector2 &p_from_a, const Vector2 &p_to_a, const Vector2 &p_from_b, const Vector2 &p_to_b);
  268. DVector<Vector2> get_closest_points_between_segments_2d(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2);
  269. DVector<Vector3> get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2);
  270. Vector2 get_closest_point_to_segment_2d(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b);
  271. Vector3 get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b);
  272. Vector2 get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b);
  273. Vector3 get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b);
  274. Variant ray_intersects_triangle(const Vector3 &p_from, const Vector3 &p_dir, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2);
  275. Variant segment_intersects_triangle(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2);
  276. bool point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const;
  277. DVector<Vector3> segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius);
  278. DVector<Vector3> segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius);
  279. DVector<Vector3> segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Vector<Plane> &p_planes);
  280. real_t segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius);
  281. int get_uv84_normal_bit(const Vector3 &p_vector);
  282. Vector<int> triangulate_polygon(const Vector<Vector2> &p_polygon);
  283. Dictionary make_atlas(const Vector<Size2> &p_rects);
  284. _Geometry();
  285. };
  286. class _File : public Reference {
  287. OBJ_TYPE(_File, Reference);
  288. FileAccess *f;
  289. bool eswap;
  290. protected:
  291. static void _bind_methods();
  292. public:
  293. enum ModeFlags {
  294. READ = 1,
  295. WRITE = 2,
  296. READ_WRITE = 3,
  297. WRITE_READ = 7,
  298. };
  299. Error open_encrypted(const String &p_path, int p_mode_flags, const Vector<uint8_t> &p_key);
  300. Error open_encrypted_pass(const String &p_path, int p_mode_flags, const String &p_pass);
  301. Error open(const String &p_path, int p_mode_flags); ///< open a file
  302. void close(); ///< close a file
  303. bool is_open() const; ///< true when file is open
  304. void seek(int64_t p_position); ///< seek to a given position
  305. void seek_end(int64_t p_position = 0); ///< seek from the end of file
  306. int64_t get_pos() const; ///< get position in the file
  307. int64_t get_len() const; ///< get size of the file
  308. bool eof_reached() const; ///< reading passed EOF
  309. uint8_t get_8() const; ///< get a byte
  310. uint16_t get_16() const; ///< get 16 bits uint
  311. uint32_t get_32() const; ///< get 32 bits uint
  312. uint64_t get_64() const; ///< get 64 bits uint
  313. float get_float() const;
  314. double get_double() const;
  315. real_t get_real() const;
  316. Variant get_var() const;
  317. DVector<uint8_t> get_buffer(int p_length) const; ///< get an array of bytes
  318. String get_line() const;
  319. String get_as_text() const;
  320. String get_md5(const String &p_path) const;
  321. String get_sha256(const String &p_path) const;
  322. /**< use this for files WRITTEN in _big_ endian machines (ie, amiga/mac)
  323. * It's not about the current CPU type but file formats.
  324. * this flags get reset to false (little endian) on each open
  325. */
  326. void set_endian_swap(bool p_swap);
  327. bool get_endian_swap();
  328. Error get_error() const; ///< get last error
  329. void store_8(uint8_t p_dest); ///< store a byte
  330. void store_16(uint16_t p_dest); ///< store 16 bits uint
  331. void store_32(uint32_t p_dest); ///< store 32 bits uint
  332. void store_64(uint64_t p_dest); ///< store 64 bits uint
  333. void store_float(float p_dest);
  334. void store_double(double p_dest);
  335. void store_real(real_t p_real);
  336. void store_string(const String &p_string);
  337. void store_line(const String &p_string);
  338. virtual void store_pascal_string(const String &p_string);
  339. virtual String get_pascal_string();
  340. Vector<String> get_csv_line(String delim = ",") const;
  341. void store_buffer(const DVector<uint8_t> &p_buffer); ///< store an array of bytes
  342. void store_var(const Variant &p_var);
  343. bool file_exists(const String &p_name) const; ///< return true if a file exists
  344. uint64_t get_modified_time(const String &p_file) const;
  345. _File();
  346. virtual ~_File();
  347. };
  348. class _Directory : public Reference {
  349. OBJ_TYPE(_Directory, Reference);
  350. DirAccess *d;
  351. protected:
  352. static void _bind_methods();
  353. public:
  354. Error open(const String &p_path);
  355. bool list_dir_begin(); ///< This starts dir listing
  356. String get_next();
  357. bool current_is_dir() const;
  358. void list_dir_end(); ///<
  359. int get_drive_count();
  360. String get_drive(int p_drive);
  361. Error change_dir(String p_dir); ///< can be relative or absolute, return false on success
  362. String get_current_dir(); ///< return current dir location
  363. Error make_dir(String p_dir);
  364. Error make_dir_recursive(String p_dir);
  365. bool file_exists(String p_file);
  366. bool dir_exists(String p_dir);
  367. int get_space_left();
  368. Error copy(String p_from, String p_to);
  369. Error rename(String p_from, String p_to);
  370. Error remove(String p_name);
  371. _Directory();
  372. virtual ~_Directory();
  373. };
  374. class _Marshalls : public Reference {
  375. OBJ_TYPE(_Marshalls, Reference);
  376. protected:
  377. static void _bind_methods();
  378. public:
  379. String variant_to_base64(const Variant &p_var);
  380. Variant base64_to_variant(const String &p_str);
  381. String raw_to_base64(const DVector<uint8_t> &p_arr);
  382. DVector<uint8_t> base64_to_raw(const String &p_str);
  383. String utf8_to_base64(const String &p_str);
  384. String base64_to_utf8(const String &p_str);
  385. _Marshalls(){};
  386. };
  387. class _Mutex : public Reference {
  388. OBJ_TYPE(_Mutex, Reference);
  389. Mutex *mutex;
  390. static void _bind_methods();
  391. public:
  392. void lock();
  393. Error try_lock();
  394. void unlock();
  395. _Mutex();
  396. ~_Mutex();
  397. };
  398. class _Semaphore : public Reference {
  399. OBJ_TYPE(_Semaphore, Reference);
  400. Semaphore *semaphore;
  401. static void _bind_methods();
  402. public:
  403. Error wait();
  404. Error post();
  405. _Semaphore();
  406. ~_Semaphore();
  407. };
  408. class _Thread : public Reference {
  409. OBJ_TYPE(_Thread, Reference);
  410. protected:
  411. Variant ret;
  412. Variant userdata;
  413. volatile bool active;
  414. Object *target_instance;
  415. StringName target_method;
  416. Thread *thread;
  417. static void _bind_methods();
  418. static void _start_func(void *ud);
  419. public:
  420. enum Priority {
  421. PRIORITY_LOW,
  422. PRIORITY_NORMAL,
  423. PRIORITY_HIGH
  424. };
  425. Error start(Object *p_instance, const StringName &p_method, const Variant &p_userdata = Variant(), int p_priority = PRIORITY_NORMAL);
  426. String get_id() const;
  427. bool is_active() const;
  428. Variant wait_to_finish();
  429. _Thread();
  430. ~_Thread();
  431. };
  432. #endif // CORE_BIND_H