base.ts 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  1. let base_ui_enabled: bool = true;
  2. let base_view3d_show: bool = true;
  3. let base_is_dragging: bool = false;
  4. let base_is_resizing: bool = false;
  5. let base_drag_asset: asset_t = null;
  6. let base_drag_swatch: swatch_color_t = null;
  7. let base_drag_file: string = null;
  8. let base_drag_file_icon: gpu_texture_t = null;
  9. let base_drag_tint: i32 = 0xffffffff;
  10. let base_drag_size: i32 = -1;
  11. let base_drag_rect: rect_t = null;
  12. let base_drag_off_x: f32 = 0.0;
  13. let base_drag_off_y: f32 = 0.0;
  14. let base_drag_start: f32 = 0.0;
  15. let base_drop_x: f32 = 0.0;
  16. let base_drop_y: f32 = 0.0;
  17. let base_font: draw_font_t = null;
  18. let base_theme: ui_theme_t;
  19. let base_color_wheel: gpu_texture_t;
  20. let base_color_wheel_gradient: gpu_texture_t;
  21. let base_default_element_w: i32 = 100;
  22. let base_default_element_h: i32 = 28;
  23. let base_default_font_size: i32 = 13;
  24. let base_res_handle: ui_handle_t = ui_handle_create();
  25. let base_bits_handle: ui_handle_t = ui_handle_create();
  26. let base_drop_paths: string[] = [];
  27. let base_appx: i32 = 0;
  28. let base_appy: i32 = 0;
  29. let base_last_window_width: i32 = 0;
  30. let base_last_window_height: i32 = 0;
  31. let base_drag_material: slot_material_t = null;
  32. let base_drag_layer: slot_layer_t = null;
  33. let _base_material_count: i32;
  34. let ui: ui_t;
  35. let ui_base_show: bool = true;
  36. let ui_base_border_started: i32 = 0;
  37. let ui_base_border_handle: ui_handle_t = null;
  38. let ui_base_action_paint_remap: string = "";
  39. let ui_base_operator_search_offset: i32 = 0;
  40. let ui_base_undo_tap_time: f32 = 0.0;
  41. let ui_base_redo_tap_time: f32 = 0.0;
  42. let ui_base_viewport_col: i32;
  43. let _ui_base_operator_search_first: bool;
  44. type tab_draw_t = {
  45. f: (h: ui_handle_t) => void;
  46. };
  47. type tab_draw_array_t = tab_draw_t[];
  48. let ui_base_hwnds: ui_handle_t[] = ui_base_init_hwnds();
  49. let ui_base_htabs: ui_handle_t[] = ui_base_init_htabs();
  50. let ui_base_hwnd_tabs: tab_draw_array_t[] = ui_base_init_hwnd_tabs();
  51. function base_init() {
  52. base_last_window_width = iron_window_width();
  53. base_last_window_height = iron_window_height();
  54. sys_notify_on_drop_files(function(drop_path: string) {
  55. drop_path = trim_end(drop_path);
  56. array_push(base_drop_paths, drop_path);
  57. });
  58. sys_notify_on_app_state(
  59. function() { // Foreground
  60. context_raw.foreground_event = true;
  61. context_raw.last_paint_x = -1;
  62. context_raw.last_paint_y = -1;
  63. },
  64. function() {}, // Resume
  65. function() {}, // Pause
  66. function() { // Background
  67. // Release keys after alt-tab / win-tab
  68. _key_up(key_code_t.ALT, null);
  69. _key_up(key_code_t.WIN, null);
  70. },
  71. function() { // Shutdown
  72. /// if (arm_android || arm_ios)
  73. project_save();
  74. /// end
  75. config_save();
  76. });
  77. iron_set_save_and_quit_callback(base_save_and_quit_callback);
  78. base_font = data_get_font("font.ttf");
  79. base_color_wheel = data_get_image("color_wheel.k");
  80. base_color_wheel_gradient = data_get_image("color_wheel_gradient.k");
  81. config_load_theme(config_raw.theme, false);
  82. base_default_element_w = base_theme.ELEMENT_W;
  83. base_default_element_h = base_theme.ELEMENT_H;
  84. base_default_font_size = base_theme.FONT_SIZE;
  85. translator_load_translations(config_raw.locale);
  86. ui_files_filename = tr("untitled");
  87. /// if (arm_android || arm_ios)
  88. sys_title_set(tr("untitled"));
  89. /// end
  90. // Baked font for fast startup
  91. if (config_raw.locale == "en") {
  92. draw_font_13(base_font);
  93. }
  94. else {
  95. draw_font_init(base_font);
  96. }
  97. ui_nodes_enum_texts = base_enum_texts;
  98. // Init plugins
  99. if (config_raw.plugins != null) {
  100. for (let i: i32 = 0; i < config_raw.plugins.length; ++i) {
  101. let plugin: string = config_raw.plugins[i];
  102. plugin_start(plugin);
  103. }
  104. }
  105. args_parse();
  106. camera_init();
  107. ui_base_init();
  108. ui_viewnodes_init();
  109. ui_view2d_init();
  110. sys_notify_on_update(base_update);
  111. sys_notify_on_update(ui_view2d_update);
  112. sys_notify_on_update(ui_nodes_update);
  113. sys_notify_on_update(ui_base_update);
  114. sys_notify_on_update(camera_update);
  115. sys_notify_on_render(ui_view2d_render);
  116. sys_notify_on_render(ui_base_render_cursor);
  117. sys_notify_on_render(ui_nodes_render);
  118. sys_notify_on_render(ui_base_render);
  119. sys_notify_on_render(base_render);
  120. base_appx = ui_toolbar_w(true);
  121. base_appy = 0;
  122. if (config_raw.layout[layout_size_t.HEADER] == 1) {
  123. base_appy = ui_header_h * 2;
  124. }
  125. scene_camera.data.fov = math_floor(scene_camera.data.fov * 100) / 100;
  126. camera_object_build_proj(scene_camera);
  127. args_run();
  128. let has_projects: bool = config_raw.recent_projects.length > 0;
  129. if (config_raw.splash_screen && has_projects) {
  130. box_projects_show();
  131. }
  132. }
  133. function base_save_and_quit_callback(save: bool) {
  134. base_save_window_rect();
  135. if (save) {
  136. project_save(true);
  137. }
  138. else {
  139. iron_stop();
  140. }
  141. }
  142. function base_w(): i32 {
  143. // Drawing material preview
  144. if (context_raw.material_preview) {
  145. return util_render_material_preview_size;
  146. }
  147. // Drawing decal preview
  148. if (context_raw.decal_preview) {
  149. return util_render_decal_preview_size;
  150. }
  151. if (context_raw.paint2d_view) {
  152. return ui_view2d_ww;
  153. }
  154. // 3D view is hidden
  155. if (!base_view3d_show) {
  156. return 1;
  157. }
  158. let res: i32 = base_view3d_w();
  159. return res > 0 ? res : 1; // App was minimized, force render path resize
  160. }
  161. function base_view3d_w(): i32 {
  162. let res: i32 = 0;
  163. if (config_raw.layout == null) {
  164. let sidebarw: i32 = ui_sidebar_default_w;
  165. res = iron_window_width() - sidebarw - ui_toolbar_default_w;
  166. }
  167. else if (ui_nodes_show || ui_view2d_show) {
  168. res = iron_window_width() - config_raw.layout[layout_size_t.SIDEBAR_W] - config_raw.layout[layout_size_t.NODES_W] - ui_toolbar_w(true);
  169. }
  170. else if (ui_base_show) {
  171. res = iron_window_width() - config_raw.layout[layout_size_t.SIDEBAR_W] - ui_toolbar_w(true);
  172. }
  173. else { // Distract free
  174. res = iron_window_width();
  175. }
  176. if (context_raw.view_index > -1) {
  177. res = math_ceil(res / 2);
  178. }
  179. if (context_raw.paint2d_view) {
  180. res = ui_view2d_ww;
  181. }
  182. return res;
  183. }
  184. function base_h(): i32 {
  185. // Drawing material preview
  186. if (context_raw.material_preview) {
  187. return util_render_material_preview_size;
  188. }
  189. // Drawing decal preview
  190. if (context_raw.decal_preview) {
  191. return util_render_decal_preview_size;
  192. }
  193. let res: i32 = iron_window_height();
  194. if (config_raw.layout == null) {
  195. res -= ui_header_default_h * 2 + ui_statusbar_default_h;
  196. /// if (arm_android || arm_ios)
  197. res += ui_header_h;
  198. /// end
  199. }
  200. else if (ui_base_show && res > 0) {
  201. let statush: i32 = config_raw.layout[layout_size_t.STATUS_H];
  202. res -= math_floor(ui_header_default_h * 2 * config_raw.window_scale) + statush;
  203. if (config_raw.layout[layout_size_t.HEADER] == 0) {
  204. res += ui_header_h * 2;
  205. }
  206. }
  207. return res > 0 ? res : 1; // App was minimized, force render path resize
  208. }
  209. function base_x(): i32 {
  210. return context_raw.view_index == 1 ? base_appx + base_w() : base_appx;
  211. }
  212. function base_y(): i32 {
  213. return base_appy;
  214. }
  215. function base_on_resize() {
  216. if (iron_window_width() == 0 || iron_window_height() == 0) {
  217. return;
  218. }
  219. let ratio_w: f32 = iron_window_width() / base_last_window_width;
  220. base_last_window_width = iron_window_width();
  221. let ratio_h: f32 = iron_window_height() / base_last_window_height;
  222. base_last_window_height = iron_window_height();
  223. config_raw.layout[layout_size_t.NODES_W] = math_floor(config_raw.layout[layout_size_t.NODES_W] * ratio_w);
  224. config_raw.layout[layout_size_t.SIDEBAR_H0] = math_floor(config_raw.layout[layout_size_t.SIDEBAR_H0] * ratio_h);
  225. config_raw.layout[layout_size_t.SIDEBAR_H1] = iron_window_height() - config_raw.layout[layout_size_t.SIDEBAR_H0];
  226. base_resize();
  227. base_save_window_rect();
  228. }
  229. function base_save_window_rect() {
  230. config_raw.window_w = iron_window_width();
  231. config_raw.window_h = iron_window_height();
  232. config_raw.window_x = iron_window_x();
  233. config_raw.window_y = iron_window_y();
  234. config_save();
  235. }
  236. function base_resize() {
  237. if (iron_window_width() == 0 || iron_window_height() == 0) {
  238. return;
  239. }
  240. let cam: camera_object_t = scene_camera;
  241. if (cam.data.ortho != null) {
  242. cam.data.ortho[2] = -2 * (sys_h() / sys_w());
  243. cam.data.ortho[3] = 2 * (sys_h() / sys_w());
  244. }
  245. camera_object_build_proj(cam);
  246. render_path_base_taa_frame = 0;
  247. if (context_raw.camera_type == camera_type_t.ORTHOGRAPHIC) {
  248. viewport_update_camera_type(context_raw.camera_type);
  249. }
  250. context_raw.ddirty = 2;
  251. if (ui_base_show && base_view3d_show) {
  252. base_appx = ui_toolbar_w(true);
  253. base_appy = 0;
  254. if (config_raw.layout[layout_size_t.HEADER] == 1) {
  255. base_appy = ui_header_h * 2;
  256. }
  257. }
  258. else {
  259. base_appx = -1;
  260. base_appy = 0;
  261. }
  262. ui_nodes_grid_redraw = true;
  263. ui_view2d_grid_redraw = true;
  264. base_redraw_ui();
  265. }
  266. function base_update() {
  267. if (mouse_movement_x != 0 || mouse_movement_y != 0) {
  268. iron_mouse_set_cursor(iron_cursor_t.ARROW);
  269. }
  270. let has_drag: bool = base_drag_asset != null || base_drag_material != null || base_drag_layer != null || base_drag_file != null || base_drag_swatch != null;
  271. if (config_raw.touch_ui) {
  272. // Touch and hold to activate dragging
  273. if (base_drag_start < 0.2) {
  274. if (has_drag && mouse_down()) {
  275. base_drag_start += sys_real_delta();
  276. }
  277. else {
  278. base_drag_start = 0;
  279. }
  280. has_drag = false;
  281. }
  282. if (mouse_released()) {
  283. base_drag_start = 0;
  284. }
  285. let moved: bool = math_abs(mouse_movement_x) > 1 && math_abs(mouse_movement_y) > 1;
  286. if ((mouse_released() || moved) && !has_drag) {
  287. base_drag_asset = null;
  288. base_drag_swatch = null;
  289. base_drag_file = null;
  290. base_drag_file_icon = null;
  291. base_is_dragging = false;
  292. base_drag_material = null;
  293. base_drag_layer = null;
  294. }
  295. // Disable touch scrolling while dragging is active
  296. ui_touch_scroll = !base_is_dragging;
  297. }
  298. if (has_drag && (mouse_movement_x != 0 || mouse_movement_y != 0)) {
  299. base_is_dragging = true;
  300. }
  301. if (mouse_released() && has_drag) {
  302. if (base_drag_asset != null) {
  303. // Create image texture
  304. if (context_in_nodes()) {
  305. ui_nodes_accept_asset_drop(array_index_of(project_assets, base_drag_asset));
  306. }
  307. else if (context_in_3d_view()) {
  308. if (ends_with(to_lower_case(base_drag_asset.file), ".hdr")) {
  309. let image: gpu_texture_t = project_get_image(base_drag_asset);
  310. import_envmap_run(base_drag_asset.file, image);
  311. }
  312. }
  313. // Create mask
  314. else if (context_in_layers() || context_in_2d_view()) {
  315. layers_create_image_mask(base_drag_asset);
  316. }
  317. base_drag_asset = null;
  318. }
  319. else if (base_drag_swatch != null) {
  320. // Create RGB node
  321. if (context_in_nodes()) {
  322. ui_nodes_accept_swatch_drop(base_drag_swatch);
  323. }
  324. else if (context_in_swatches()) {
  325. tab_swatches_accept_swatch_drop(base_drag_swatch);
  326. }
  327. else if (context_in_materials()) {
  328. tab_materials_accept_swatch_drop(base_drag_swatch);
  329. }
  330. else if (context_in_3d_view()) {
  331. let color: i32 = base_drag_swatch.base;
  332. color = color_set_ab(color, base_drag_swatch.opacity * 255);
  333. layers_create_color_layer(color, base_drag_swatch.occlusion, base_drag_swatch.roughness, base_drag_swatch.metallic);
  334. }
  335. else if (context_in_layers() && tab_layers_can_drop_new_layer(context_raw.drag_dest)) {
  336. let color: i32 = base_drag_swatch.base;
  337. color = color_set_ab(color, base_drag_swatch.opacity * 255);
  338. layers_create_color_layer(color, base_drag_swatch.occlusion, base_drag_swatch.roughness, base_drag_swatch.metallic, context_raw.drag_dest);
  339. }
  340. base_drag_swatch = null;
  341. }
  342. else if (base_drag_file != null) {
  343. if (!context_in_browser()) {
  344. base_drop_x = mouse_x;
  345. base_drop_y = mouse_y;
  346. _base_material_count = project_materials.length;
  347. import_asset_run(base_drag_file, base_drop_x, base_drop_y, true, true, function() {
  348. // Asset was material
  349. if (project_materials.length > _base_material_count) {
  350. base_drag_material = context_raw.material;
  351. base_material_dropped();
  352. }
  353. });
  354. }
  355. base_drag_file = null;
  356. base_drag_file_icon = null;
  357. }
  358. else if (base_drag_material != null) {
  359. base_material_dropped();
  360. }
  361. else if (base_drag_layer != null) {
  362. if (context_in_nodes()) {
  363. ui_nodes_accept_layer_drop(array_index_of(project_layers, base_drag_layer));
  364. }
  365. else if (context_in_layers() && base_is_dragging) {
  366. slot_layer_move(base_drag_layer, context_raw.drag_dest);
  367. make_material_parse_mesh_material();
  368. }
  369. base_drag_layer = null;
  370. }
  371. iron_mouse_set_cursor(iron_cursor_t.ARROW);
  372. base_is_dragging = false;
  373. }
  374. if (context_raw.color_picker_callback != null && (mouse_released() || mouse_released("right"))) {
  375. context_raw.color_picker_callback = null;
  376. context_select_tool(context_raw.color_picker_previous_tool);
  377. }
  378. base_handle_drop_paths();
  379. if (context_raw.ddirty < 0) {
  380. context_raw.ddirty = 0;
  381. }
  382. if (context_raw.tool == tool_type_t.GIZMO) {
  383. if (keyboard_down("control") && keyboard_started("d")) {
  384. sim_duplicate();
  385. }
  386. if (keyboard_started("delete")) {
  387. sim_delete();
  388. }
  389. }
  390. compass_update();
  391. }
  392. function base_material_dropped() {
  393. // Material drag and dropped onto viewport or layers tab
  394. if (context_in_3d_view()) {
  395. let uv_type: uv_type_t = keyboard_down("control") ? uv_type_t.PROJECT : uv_type_t.UVMAP;
  396. let decal_mat: mat4_t = uv_type == uv_type_t.PROJECT ? util_render_get_decal_mat() : mat4_nan();
  397. layers_create_fill_layer(uv_type, decal_mat);
  398. }
  399. if (context_in_layers() && tab_layers_can_drop_new_layer(context_raw.drag_dest)) {
  400. let uv_type: uv_type_t = keyboard_down("control") ? uv_type_t.PROJECT : uv_type_t.UVMAP;
  401. let decal_mat: mat4_t = uv_type == uv_type_t.PROJECT ? util_render_get_decal_mat() : mat4_nan();
  402. layers_create_fill_layer(uv_type, decal_mat, context_raw.drag_dest);
  403. }
  404. else if (context_in_nodes()) {
  405. ui_nodes_accept_material_drop(array_index_of(project_materials, base_drag_material));
  406. }
  407. base_drag_material = null;
  408. }
  409. function base_handle_drop_paths() {
  410. if (base_drop_paths.length > 0) {
  411. let wait: bool = false;
  412. /// if (arm_linux || arm_macos)
  413. wait = !mouse_moved; // Mouse coords not updated during drag
  414. /// end
  415. if (!wait) {
  416. base_drop_x = mouse_x;
  417. base_drop_y = mouse_y;
  418. let drop_path: string = array_shift(base_drop_paths);
  419. import_asset_run(drop_path, base_drop_x, base_drop_y);
  420. }
  421. }
  422. }
  423. function base_get_drag_background(): rect_t {
  424. let icons: gpu_texture_t = resource_get("icons.k");
  425. if (base_drag_layer != null && !slot_layer_is_group(base_drag_layer) && base_drag_layer.fill_layer == null) {
  426. return resource_tile50(icons, 4, 1);
  427. }
  428. return null;
  429. }
  430. function base_get_drag_image(): gpu_texture_t {
  431. base_drag_tint = 0xffffffff;
  432. base_drag_size = -1;
  433. base_drag_rect = null;
  434. if (base_drag_asset != null) {
  435. return project_get_image(base_drag_asset);
  436. }
  437. if (base_drag_swatch != null) {
  438. base_drag_tint = base_drag_swatch.base;
  439. base_drag_size = 26;
  440. return tab_swatches_empty_get();
  441. }
  442. if (base_drag_file != null) {
  443. if (base_drag_file_icon != null) {
  444. return base_drag_file_icon;
  445. }
  446. let icons: gpu_texture_t = resource_get("icons.k");
  447. base_drag_rect = string_index_of(base_drag_file, ".") > 0 ? resource_tile50(icons, 3, 1) : resource_tile50(icons, 2, 1);
  448. base_drag_tint = ui.ops.theme.HIGHLIGHT_COL;
  449. return icons;
  450. }
  451. if (base_drag_material != null) {
  452. return base_drag_material.image_icon;
  453. }
  454. if (base_drag_layer != null && slot_layer_is_group(base_drag_layer)) {
  455. let icons: gpu_texture_t = resource_get("icons.k");
  456. let folder_closed: rect_t = resource_tile50(icons, 2, 1);
  457. let folder_open: rect_t = resource_tile50(icons, 8, 1);
  458. base_drag_rect = base_drag_layer.show_panel ? folder_open : folder_closed;
  459. base_drag_tint = ui.ops.theme.LABEL_COL - 0x00202020;
  460. return icons;
  461. }
  462. if (base_drag_layer != null && slot_layer_is_mask(base_drag_layer) && base_drag_layer.fill_layer == null) {
  463. tab_layers_make_mask_preview_rgba32(base_drag_layer);
  464. return context_raw.mask_preview_rgba32;
  465. }
  466. if (base_drag_layer != null) {
  467. return base_drag_layer.fill_layer != null ? base_drag_layer.fill_layer.image_icon : base_drag_layer.texpaint_preview;
  468. }
  469. return null;
  470. }
  471. function base_render() {
  472. if (context_raw.frame == 2) {
  473. util_render_make_material_preview();
  474. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  475. base_init_undo_layers();
  476. }
  477. if (context_raw.tool == tool_type_t.GIZMO) {
  478. sim_init();
  479. sim_update();
  480. }
  481. if (context_raw.frame == 2) {
  482. make_material_parse_mesh_material();
  483. make_material_parse_paint_material();
  484. context_raw.ddirty = 0;
  485. // Default camera controls
  486. context_raw.camera_controls = config_raw.camera_controls;
  487. }
  488. else if (context_raw.frame == 3) {
  489. context_raw.ddirty = 3;
  490. }
  491. context_raw.frame++;
  492. if (base_is_dragging) {
  493. iron_mouse_set_cursor(iron_cursor_t.HAND);
  494. let img: gpu_texture_t = base_get_drag_image();
  495. let scale_factor: f32 = UI_SCALE();
  496. let size: f32 = (base_drag_size == -1 ? 50 : base_drag_size) * scale_factor;
  497. let ratio: f32 = size / img.width;
  498. let h: f32 = img.height * ratio;
  499. let inv: i32 = 0;
  500. draw_begin();
  501. draw_set_color(base_drag_tint);
  502. let bg_rect: rect_t = base_get_drag_background();
  503. if (bg_rect != null) {
  504. draw_scaled_sub_image(resource_get("icons.k"), bg_rect.x, bg_rect.y, bg_rect.w, bg_rect.h, mouse_x + base_drag_off_x,
  505. mouse_y + base_drag_off_y + inv, size, h - inv * 2);
  506. }
  507. base_drag_rect == null ? draw_scaled_image(img, mouse_x + base_drag_off_x, mouse_y + base_drag_off_y + inv, size, h - inv * 2)
  508. : draw_scaled_sub_image(img, base_drag_rect.x, base_drag_rect.y, base_drag_rect.w, base_drag_rect.h, mouse_x + base_drag_off_x,
  509. mouse_y + base_drag_off_y + inv, size, h - inv * 2);
  510. draw_set_color(0xffffffff);
  511. draw_end();
  512. }
  513. let using_menu: bool = ui_menu_show && mouse_y > ui_header_h;
  514. base_ui_enabled = !ui_box_show && !using_menu && ui.combo_selected_handle == null;
  515. if (ui_box_show) {
  516. ui_box_render();
  517. }
  518. if (ui_menu_show) {
  519. ui_menu_render();
  520. }
  521. // Save last pos for continuos paint
  522. context_raw.last_paint_vec_x = context_raw.paint_vec.x;
  523. context_raw.last_paint_vec_y = context_raw.paint_vec.y;
  524. /// if (arm_android || arm_ios)
  525. // No mouse move events for touch, re-init last paint position on touch start
  526. if (!mouse_down()) {
  527. context_raw.last_paint_x = -1;
  528. context_raw.last_paint_y = -1;
  529. }
  530. /// end
  531. }
  532. function base_enum_texts(node_type: string): string[] {
  533. if (node_type == "TEX_IMAGE") {
  534. if (project_asset_names.length > 0) {
  535. return project_asset_names;
  536. }
  537. else {
  538. let empty: string[] = [ "" ];
  539. return empty;
  540. }
  541. }
  542. if (node_type == "LAYER" || node_type == "LAYER_MASK") {
  543. let layer_names: string[] = [];
  544. for (let i: i32 = 0; i < project_layers.length; ++i) {
  545. let l: slot_layer_t = project_layers[i];
  546. array_push(layer_names, l.name);
  547. }
  548. return layer_names;
  549. }
  550. if (node_type == "MATERIAL") {
  551. let material_names: string[] = [];
  552. for (let i: i32 = 0; i < project_materials.length; ++i) {
  553. let m: slot_material_t = project_materials[i];
  554. array_push(material_names, m.canvas.name);
  555. }
  556. return material_names;
  557. }
  558. if (node_type == "image_texture_node") {
  559. if (project_asset_names.length > 0) {
  560. return project_asset_names;
  561. }
  562. else {
  563. let empty: string[] = [ "" ];
  564. return empty;
  565. }
  566. }
  567. return null;
  568. }
  569. function base_get_asset_index(file_name: string): i32 {
  570. let i: i32 = array_index_of(project_asset_names, file_name);
  571. return i >= 0 ? i : 0;
  572. }
  573. function base_toggle_fullscreen() {
  574. if (iron_window_get_mode() == iron_window_mode_t.WINDOW) {
  575. config_raw.window_w = iron_window_width();
  576. config_raw.window_h = iron_window_height();
  577. config_raw.window_x = iron_window_x();
  578. config_raw.window_y = iron_window_y();
  579. iron_window_change_mode(iron_window_mode_t.FULLSCREEN);
  580. }
  581. else {
  582. iron_window_change_mode(iron_window_mode_t.WINDOW);
  583. iron_window_resize(config_raw.window_w, config_raw.window_h);
  584. iron_window_move(config_raw.window_x, config_raw.window_y);
  585. }
  586. }
  587. function base_is_decal_layer(): bool {
  588. let is_painting: bool = context_raw.tool != tool_type_t.MATERIAL && context_raw.tool != tool_type_t.BAKE;
  589. return is_painting && context_raw.layer.fill_layer != null && context_raw.layer.uv_type == uv_type_t.PROJECT;
  590. }
  591. function base_redraw_status() {
  592. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  593. }
  594. function base_redraw_console() {
  595. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  596. }
  597. function base_init_undo_layers() {
  598. if (history_undo_layers == null) {
  599. history_undo_layers = [];
  600. for (let i: i32 = 0; i < config_raw.undo_steps; ++i) {
  601. let len: i32 = history_undo_layers.length;
  602. let ext: string = "_undo" + len;
  603. let l: slot_layer_t = slot_layer_create(ext);
  604. array_push(history_undo_layers, l);
  605. }
  606. }
  607. }
  608. function ui_base_init_hwnds(): ui_handle_t[] {
  609. let hwnds: ui_handle_t[] = [ ui_handle_create(), ui_handle_create(), ui_handle_create() ];
  610. return hwnds;
  611. }
  612. function ui_base_init_htabs(): ui_handle_t[] {
  613. let htabs: ui_handle_t[] = [ ui_handle_create(), ui_handle_create(), ui_handle_create() ];
  614. return htabs;
  615. }
  616. function _draw_callback_create(f: (h: ui_handle_t) => void): tab_draw_t {
  617. let cb: tab_draw_t = {f : f};
  618. return cb;
  619. }
  620. function ui_base_init_hwnd_tabs(): tab_draw_array_t[] {
  621. let a0: tab_draw_array_t = [ _draw_callback_create(tab_layers_draw), _draw_callback_create(tab_history_draw), _draw_callback_create(tab_plugins_draw) ];
  622. let a1: tab_draw_array_t = [
  623. _draw_callback_create(tab_materials_draw), _draw_callback_create(tab_brushes_draw), _draw_callback_create(tab_scripts_draw)
  624. ];
  625. let a2: tab_draw_array_t = [
  626. _draw_callback_create(tab_browser_draw), _draw_callback_create(tab_meshes_draw), _draw_callback_create(tab_textures_draw),
  627. _draw_callback_create(tab_fonts_draw), _draw_callback_create(tab_swatches_draw), _draw_callback_create(tab_console_draw),
  628. _draw_callback_create(ui_statusbar_draw_version_tab)
  629. ];
  630. let r: tab_draw_array_t[] = [];
  631. array_push(r, a0);
  632. array_push(r, a1);
  633. array_push(r, a2);
  634. return r;
  635. }
  636. function ui_base_init() {
  637. ui_toolbar_init();
  638. context_raw.text_tool_text = tr("Text");
  639. ui_header_init();
  640. ui_statusbar_init();
  641. ui_menubar_init();
  642. ui_header_h = math_floor(ui_header_default_h * config_raw.window_scale);
  643. ui_menubar_w = math_floor(ui_menubar_default_w * config_raw.window_scale);
  644. if (context_raw.empty_envmap == null) {
  645. ui_base_make_empty_envmap(base_theme.VIEWPORT_COL);
  646. }
  647. if (context_raw.preview_envmap == null) {
  648. let b: u8_array_t = u8_array_create(4);
  649. b[0] = 0;
  650. b[1] = 0;
  651. b[2] = 0;
  652. b[3] = 255;
  653. context_raw.preview_envmap = gpu_create_texture_from_bytes(b, 1, 1);
  654. }
  655. if (context_raw.saved_envmap == null) {
  656. // raw.saved_envmap = scene_world._envmap;
  657. context_raw.default_irradiance = scene_world._.irradiance;
  658. context_raw.default_radiance = scene_world._.radiance;
  659. context_raw.default_radiance_mipmaps = scene_world._.radiance_mipmaps;
  660. }
  661. scene_world._.envmap = context_raw.show_envmap ? context_raw.saved_envmap : context_raw.empty_envmap;
  662. context_raw.ddirty = 1;
  663. let resources: string[] = [ "cursor.k", "icons.k" ];
  664. resource_load(resources);
  665. let scale: f32 = config_raw.window_scale;
  666. let ops: ui_options_t =
  667. {theme : base_theme, font : base_font, scale_factor : scale, color_wheel : base_color_wheel, black_white_gradient : base_color_wheel_gradient};
  668. ui = ui_create(ops);
  669. ui_on_border_hover = ui_base_on_border_hover;
  670. ui_on_tab_drop = ui_base_on_tab_drop;
  671. if (UI_SCALE() > 1) {
  672. ui_base_set_icon_scale();
  673. }
  674. context_raw.gizmo = scene_get_child(".Gizmo");
  675. context_raw.gizmo_translate_x = object_get_child(context_raw.gizmo, ".TranslateX");
  676. context_raw.gizmo_translate_y = object_get_child(context_raw.gizmo, ".TranslateY");
  677. context_raw.gizmo_translate_z = object_get_child(context_raw.gizmo, ".TranslateZ");
  678. context_raw.gizmo_scale_x = object_get_child(context_raw.gizmo, ".ScaleX");
  679. context_raw.gizmo_scale_y = object_get_child(context_raw.gizmo, ".ScaleY");
  680. context_raw.gizmo_scale_z = object_get_child(context_raw.gizmo, ".ScaleZ");
  681. context_raw.gizmo_rotate_x = object_get_child(context_raw.gizmo, ".RotateX");
  682. context_raw.gizmo_rotate_y = object_get_child(context_raw.gizmo, ".RotateY");
  683. context_raw.gizmo_rotate_z = object_get_child(context_raw.gizmo, ".RotateZ");
  684. project_new(false);
  685. if (project_filepath == "") {
  686. sys_notify_on_next_frame(layers_init);
  687. }
  688. context_raw.project_objects = [];
  689. for (let i: i32 = 0; i < scene_meshes.length; ++i) {
  690. let m: mesh_object_t = scene_meshes[i];
  691. array_push(context_raw.project_objects, m);
  692. }
  693. operator_register("view_top", ui_base_view_top);
  694. }
  695. function ui_base_update() {
  696. ui_base_update_ui();
  697. operator_update();
  698. let keys: string[] = map_keys(plugin_map);
  699. for (let i: i32 = 0; i < keys.length; ++i) {
  700. let p: plugin_t = map_get(plugin_map, keys[i]);
  701. if (p.on_update != null) {
  702. js_call(p.on_update);
  703. }
  704. }
  705. if (!base_ui_enabled) {
  706. return;
  707. }
  708. if (!ui.is_typing) {
  709. if (operator_shortcut(map_get(config_keymap, "toggle_node_editor"))) {
  710. ui_nodes_canvas_type == canvas_type_t.MATERIAL ? ui_base_show_material_nodes() : ui_base_show_brush_nodes();
  711. }
  712. else if (operator_shortcut(map_get(config_keymap, "toggle_browser"))) {
  713. ui_base_toggle_browser();
  714. }
  715. else if (operator_shortcut(map_get(config_keymap, "toggle_2d_view"))) {
  716. ui_base_show_2d_view(view_2d_type_t.LAYER);
  717. }
  718. }
  719. if (operator_shortcut(map_get(config_keymap, "file_save_as"))) {
  720. project_save_as();
  721. }
  722. else if (operator_shortcut(map_get(config_keymap, "file_save"))) {
  723. project_save();
  724. }
  725. else if (operator_shortcut(map_get(config_keymap, "file_open"))) {
  726. project_open();
  727. }
  728. else if (operator_shortcut(map_get(config_keymap, "file_open_recent"))) {
  729. box_projects_show();
  730. }
  731. else if (operator_shortcut(map_get(config_keymap, "file_reimport_mesh"))) {
  732. project_reimport_mesh();
  733. }
  734. else if (operator_shortcut(map_get(config_keymap, "file_reimport_textures"))) {
  735. project_reimport_textures();
  736. }
  737. else if (operator_shortcut(map_get(config_keymap, "file_new"))) {
  738. project_new_box();
  739. }
  740. else if (operator_shortcut(map_get(config_keymap, "file_export_textures"))) {
  741. if (context_raw.texture_export_path == "") { // First export, ask for path
  742. context_raw.layers_export = export_mode_t.VISIBLE;
  743. box_export_show_textures();
  744. }
  745. else {
  746. sys_notify_on_next_frame(function() {
  747. export_texture_run(context_raw.texture_export_path);
  748. });
  749. }
  750. }
  751. else if (operator_shortcut(map_get(config_keymap, "file_export_textures_as"))) {
  752. context_raw.layers_export = export_mode_t.VISIBLE;
  753. box_export_show_textures();
  754. }
  755. else if (operator_shortcut(map_get(config_keymap, "file_import_assets"))) {
  756. project_import_asset();
  757. }
  758. else if (operator_shortcut(map_get(config_keymap, "edit_prefs"))) {
  759. box_preferences_show();
  760. }
  761. if (keyboard_started(map_get(config_keymap, "view_distract_free")) || (keyboard_started("escape") && !ui_base_show && !ui_box_show)) {
  762. ui_base_toggle_distract_free();
  763. }
  764. /// if arm_linux
  765. if (operator_shortcut("alt+enter", shortcut_type_t.STARTED)) {
  766. base_toggle_fullscreen();
  767. }
  768. /// end
  769. let decal_mask: bool = context_is_decal_mask();
  770. if (context_raw.brush_locked && mouse_moved) {
  771. if (operator_shortcut(map_get(config_keymap, "brush_radius"), shortcut_type_t.DOWN) ||
  772. operator_shortcut(map_get(config_keymap, "brush_opacity"), shortcut_type_t.DOWN) ||
  773. operator_shortcut(map_get(config_keymap, "brush_angle"), shortcut_type_t.DOWN) ||
  774. (decal_mask && operator_shortcut(map_get(config_keymap, "decal_mask") + "+" + map_get(config_keymap, "brush_radius"), shortcut_type_t.DOWN))) {
  775. if (context_raw.brush_locked) {
  776. if (operator_shortcut(map_get(config_keymap, "brush_opacity"), shortcut_type_t.DOWN)) {
  777. context_raw.brush_opacity += mouse_movement_x / 500;
  778. context_raw.brush_opacity = math_max(0.0, math_min(1.0, context_raw.brush_opacity));
  779. context_raw.brush_opacity = math_round(context_raw.brush_opacity * 100) / 100;
  780. context_raw.brush_opacity_handle.f = context_raw.brush_opacity;
  781. }
  782. else if (operator_shortcut(map_get(config_keymap, "brush_angle"), shortcut_type_t.DOWN)) {
  783. context_raw.brush_angle += mouse_movement_x / 5;
  784. let i: i32 = math_floor(context_raw.brush_angle);
  785. context_raw.brush_angle = i % 360;
  786. if (context_raw.brush_angle < 0)
  787. context_raw.brush_angle += 360;
  788. context_raw.brush_angle_handle.f = context_raw.brush_angle;
  789. make_material_parse_paint_material();
  790. }
  791. else if (decal_mask &&
  792. operator_shortcut(map_get(config_keymap, "decal_mask") + "+" + map_get(config_keymap, "brush_radius"), shortcut_type_t.DOWN)) {
  793. context_raw.brush_decal_mask_radius += mouse_movement_x / 150;
  794. context_raw.brush_decal_mask_radius = math_max(0.01, math_min(4.0, context_raw.brush_decal_mask_radius));
  795. context_raw.brush_decal_mask_radius = math_round(context_raw.brush_decal_mask_radius * 100) / 100;
  796. context_raw.brush_decal_mask_radius_handle.f = context_raw.brush_decal_mask_radius;
  797. }
  798. else {
  799. context_raw.brush_radius += mouse_movement_x / 150;
  800. context_raw.brush_radius = math_max(0.01, math_min(4.0, context_raw.brush_radius));
  801. context_raw.brush_radius = math_round(context_raw.brush_radius * 100) / 100;
  802. context_raw.brush_radius_handle.f = context_raw.brush_radius;
  803. }
  804. ui_header_handle.redraws = 2;
  805. }
  806. }
  807. }
  808. let is_typing: bool = ui.is_typing;
  809. if (!is_typing) {
  810. if (operator_shortcut(map_get(config_keymap, "select_material"), shortcut_type_t.DOWN)) {
  811. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  812. for (let i: i32 = 1; i < 10; ++i) {
  813. if (keyboard_started(i + "")) {
  814. context_select_material(i - 1);
  815. }
  816. }
  817. }
  818. else if (operator_shortcut(map_get(config_keymap, "select_layer"), shortcut_type_t.DOWN)) {
  819. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  820. for (let i: i32 = 1; i < 10; ++i) {
  821. if (keyboard_started(i + "")) {
  822. context_select_layer(i - 1);
  823. }
  824. }
  825. }
  826. }
  827. // Viewport shortcuts
  828. if (context_in_paint_area() && !is_typing) {
  829. if (!mouse_down("right")) { // Fly mode off
  830. if (operator_shortcut(map_get(config_keymap, "tool_brush"))) {
  831. context_select_tool(tool_type_t.BRUSH);
  832. }
  833. else if (operator_shortcut(map_get(config_keymap, "tool_eraser"))) {
  834. context_select_tool(tool_type_t.ERASER);
  835. }
  836. else if (operator_shortcut(map_get(config_keymap, "tool_fill"))) {
  837. context_select_tool(tool_type_t.FILL);
  838. }
  839. else if (operator_shortcut(map_get(config_keymap, "tool_colorid"))) {
  840. context_select_tool(tool_type_t.COLORID);
  841. }
  842. else if (operator_shortcut(map_get(config_keymap, "tool_decal"))) {
  843. context_select_tool(tool_type_t.DECAL);
  844. }
  845. else if (operator_shortcut(map_get(config_keymap, "tool_text"))) {
  846. context_select_tool(tool_type_t.TEXT);
  847. }
  848. else if (operator_shortcut(map_get(config_keymap, "tool_clone"))) {
  849. context_select_tool(tool_type_t.CLONE);
  850. }
  851. else if (operator_shortcut(map_get(config_keymap, "tool_blur"))) {
  852. context_select_tool(tool_type_t.BLUR);
  853. }
  854. else if (operator_shortcut(map_get(config_keymap, "tool_smudge"))) {
  855. context_select_tool(tool_type_t.SMUDGE);
  856. }
  857. else if (operator_shortcut(map_get(config_keymap, "tool_particle"))) {
  858. context_select_tool(tool_type_t.PARTICLE);
  859. }
  860. else if (operator_shortcut(map_get(config_keymap, "tool_picker"))) {
  861. context_select_tool(tool_type_t.PICKER);
  862. }
  863. else if (operator_shortcut(map_get(config_keymap, "tool_bake"))) {
  864. context_select_tool(tool_type_t.BAKE);
  865. }
  866. else if (operator_shortcut(map_get(config_keymap, "tool_gizmo"))) {
  867. context_select_tool(tool_type_t.GIZMO);
  868. }
  869. else if (operator_shortcut(map_get(config_keymap, "tool_material"))) {
  870. context_select_tool(tool_type_t.MATERIAL);
  871. }
  872. else if (operator_shortcut(map_get(config_keymap, "swap_brush_eraser"))) {
  873. context_select_tool(context_raw.tool == tool_type_t.BRUSH ? tool_type_t.ERASER : tool_type_t.BRUSH);
  874. }
  875. }
  876. // Radius
  877. if (context_raw.tool == tool_type_t.BRUSH || context_raw.tool == tool_type_t.ERASER || context_raw.tool == tool_type_t.DECAL ||
  878. context_raw.tool == tool_type_t.TEXT || context_raw.tool == tool_type_t.CLONE || context_raw.tool == tool_type_t.BLUR ||
  879. context_raw.tool == tool_type_t.SMUDGE || context_raw.tool == tool_type_t.PARTICLE) {
  880. if (operator_shortcut(map_get(config_keymap, "brush_radius")) || operator_shortcut(map_get(config_keymap, "brush_opacity")) ||
  881. operator_shortcut(map_get(config_keymap, "brush_angle")) ||
  882. (decal_mask && operator_shortcut(map_get(config_keymap, "decal_mask") + "+" + map_get(config_keymap, "brush_radius")))) {
  883. context_raw.brush_locked = true;
  884. if (!pen_connected) {
  885. iron_mouse_lock();
  886. }
  887. }
  888. else if (operator_shortcut(map_get(config_keymap, "brush_radius_decrease"), shortcut_type_t.REPEAT)) {
  889. context_raw.brush_radius -= ui_base_get_radius_increment();
  890. context_raw.brush_radius = math_max(math_round(context_raw.brush_radius * 100) / 100, 0.01);
  891. context_raw.brush_radius_handle.f = context_raw.brush_radius;
  892. ui_header_handle.redraws = 2;
  893. }
  894. else if (operator_shortcut(map_get(config_keymap, "brush_radius_increase"), shortcut_type_t.REPEAT)) {
  895. context_raw.brush_radius += ui_base_get_radius_increment();
  896. context_raw.brush_radius = math_round(context_raw.brush_radius * 100) / 100;
  897. context_raw.brush_radius_handle.f = context_raw.brush_radius;
  898. ui_header_handle.redraws = 2;
  899. }
  900. else if (decal_mask) {
  901. if (operator_shortcut(map_get(config_keymap, "decal_mask") + "+" + map_get(config_keymap, "brush_radius_decrease"), shortcut_type_t.REPEAT)) {
  902. context_raw.brush_decal_mask_radius -= ui_base_get_radius_increment();
  903. context_raw.brush_decal_mask_radius = math_max(math_round(context_raw.brush_decal_mask_radius * 100) / 100, 0.01);
  904. context_raw.brush_decal_mask_radius_handle.f = context_raw.brush_decal_mask_radius;
  905. ui_header_handle.redraws = 2;
  906. }
  907. else if (operator_shortcut(map_get(config_keymap, "decal_mask") + "+" + map_get(config_keymap, "brush_radius_increase"),
  908. shortcut_type_t.REPEAT)) {
  909. context_raw.brush_decal_mask_radius += ui_base_get_radius_increment();
  910. context_raw.brush_decal_mask_radius = math_round(context_raw.brush_decal_mask_radius * 100) / 100;
  911. context_raw.brush_decal_mask_radius_handle.f = context_raw.brush_decal_mask_radius;
  912. ui_header_handle.redraws = 2;
  913. }
  914. }
  915. }
  916. if (decal_mask && (operator_shortcut(map_get(config_keymap, "decal_mask"), shortcut_type_t.STARTED) ||
  917. operator_shortcut(map_get(config_keymap, "decal_mask"), shortcut_type_t.RELEASED))) {
  918. ui_header_handle.redraws = 2;
  919. }
  920. // Viewpoint
  921. if (mouse_view_x() < sys_w()) {
  922. if (operator_shortcut(map_get(config_keymap, "view_reset"))) {
  923. viewport_reset();
  924. viewport_scale_to_bounds();
  925. }
  926. else if (operator_shortcut(map_get(config_keymap, "view_back"))) {
  927. viewport_set_view(0, 1, 0, math_pi() / 2, 0, math_pi());
  928. }
  929. else if (operator_shortcut(map_get(config_keymap, "view_front"))) {
  930. viewport_set_view(0, -1, 0, math_pi() / 2, 0, 0);
  931. }
  932. else if (operator_shortcut(map_get(config_keymap, "view_left"))) {
  933. viewport_set_view(-1, 0, 0, math_pi() / 2, 0, -math_pi() / 2);
  934. }
  935. else if (operator_shortcut(map_get(config_keymap, "view_right"))) {
  936. viewport_set_view(1, 0, 0, math_pi() / 2, 0, math_pi() / 2);
  937. }
  938. else if (operator_shortcut(map_get(config_keymap, "view_bottom"))) {
  939. viewport_set_view(0, 0, -1, math_pi(), 0, math_pi());
  940. }
  941. else if (operator_shortcut(map_get(config_keymap, "view_camera_type"))) {
  942. context_raw.camera_type = context_raw.camera_type == camera_type_t.PERSPECTIVE ? camera_type_t.ORTHOGRAPHIC : camera_type_t.PERSPECTIVE;
  943. context_raw.cam_handle.i = context_raw.camera_type;
  944. viewport_update_camera_type(context_raw.camera_type);
  945. }
  946. else if (operator_shortcut(map_get(config_keymap, "view_orbit_left"), shortcut_type_t.REPEAT)) {
  947. viewport_orbit(-math_pi() / 12, 0);
  948. }
  949. else if (operator_shortcut(map_get(config_keymap, "view_orbit_right"), shortcut_type_t.REPEAT)) {
  950. viewport_orbit(math_pi() / 12, 0);
  951. }
  952. else if (operator_shortcut(map_get(config_keymap, "view_orbit_up"), shortcut_type_t.REPEAT)) {
  953. viewport_orbit(0, -math_pi() / 12);
  954. }
  955. else if (operator_shortcut(map_get(config_keymap, "view_orbit_down"), shortcut_type_t.REPEAT)) {
  956. viewport_orbit(0, math_pi() / 12);
  957. }
  958. else if (operator_shortcut(map_get(config_keymap, "view_orbit_opposite"))) {
  959. viewport_orbit_opposite();
  960. }
  961. else if (operator_shortcut(map_get(config_keymap, "view_zoom_in"), shortcut_type_t.REPEAT)) {
  962. viewport_zoom(0.2);
  963. }
  964. else if (operator_shortcut(map_get(config_keymap, "view_zoom_out"), shortcut_type_t.REPEAT)) {
  965. viewport_zoom(-0.2);
  966. }
  967. else if (operator_shortcut(map_get(config_keymap, "viewport_mode"))) {
  968. ui.is_key_pressed = false;
  969. ui_menu_draw(function() {
  970. let mode_handle: ui_handle_t = ui_handle(__ID__);
  971. mode_handle.i = context_raw.viewport_mode;
  972. ui_text(tr("Viewport Mode"), ui_align_t.RIGHT);
  973. let modes: string[] = [
  974. tr("Lit"), tr("Base Color"), tr("Normal"), tr("Occlusion"), tr("Roughness"), tr("Metallic"), tr("Opacity"), tr("Height"),
  975. tr("Emission"), tr("Subsurface"), tr("TexCoord"), tr("Object Normal"), tr("Material ID"), tr("Object ID"), tr("Mask")
  976. ];
  977. let shortcuts: string[] = [ "l", "b", "n", "o", "r", "m", "a", "h", "e", "s", "t", "1", "2", "3", "4" ];
  978. if (gpu_raytrace_supported()) {
  979. array_push(modes, tr("Path Traced"));
  980. array_push(shortcuts, "p");
  981. }
  982. for (let i: i32 = 0; i < modes.length; ++i) {
  983. ui_radio(mode_handle, i, modes[i], shortcuts[i]);
  984. }
  985. let index: i32 = array_index_of(shortcuts, keyboard_key_code(ui.key_code));
  986. if (ui.is_key_pressed && index != -1) {
  987. mode_handle.i = index;
  988. ui.changed = true;
  989. context_set_viewport_mode(mode_handle.i);
  990. }
  991. else if (mode_handle.changed) {
  992. context_set_viewport_mode(mode_handle.i);
  993. ui.changed = true;
  994. }
  995. });
  996. }
  997. }
  998. if (operator_shortcut(map_get(config_keymap, "operator_search"))) {
  999. ui_base_operator_search();
  1000. }
  1001. }
  1002. if (context_raw.brush_locked) {
  1003. let b: bool =
  1004. context_raw.brush_locked && !operator_shortcut(map_get(config_keymap, "brush_radius"), shortcut_type_t.DOWN) &&
  1005. !operator_shortcut(map_get(config_keymap, "brush_opacity"), shortcut_type_t.DOWN) &&
  1006. !operator_shortcut(map_get(config_keymap, "brush_angle"), shortcut_type_t.DOWN) &&
  1007. !(decal_mask && operator_shortcut(map_get(config_keymap, "decal_mask") + "+" + map_get(config_keymap, "brush_radius"), shortcut_type_t.DOWN));
  1008. if (b) {
  1009. iron_mouse_unlock();
  1010. context_raw.last_paint_x = -1;
  1011. context_raw.last_paint_y = -1;
  1012. context_raw.brush_locked = false;
  1013. }
  1014. }
  1015. // Resizing
  1016. if (ui_base_border_handle != null) {
  1017. if (ui_base_border_handle == ui_nodes_hwnd || ui_base_border_handle == ui_view2d_hwnd) {
  1018. if (ui_base_border_started == border_side_t.LEFT) {
  1019. config_raw.layout[layout_size_t.NODES_W] -= math_floor(mouse_movement_x);
  1020. if (config_raw.layout[layout_size_t.NODES_W] < 32) {
  1021. config_raw.layout[layout_size_t.NODES_W] = 32;
  1022. }
  1023. else if (config_raw.layout[layout_size_t.NODES_W] > iron_window_width() * 0.7) {
  1024. config_raw.layout[layout_size_t.NODES_W] = math_floor(iron_window_width() * 0.7);
  1025. }
  1026. }
  1027. else { // UINodes / UIView2D ratio
  1028. config_raw.layout[layout_size_t.NODES_H] -= math_floor(mouse_movement_y);
  1029. if (config_raw.layout[layout_size_t.NODES_H] < 32) {
  1030. config_raw.layout[layout_size_t.NODES_H] = 32;
  1031. }
  1032. else if (config_raw.layout[layout_size_t.NODES_H] > sys_h() * 0.95) {
  1033. config_raw.layout[layout_size_t.NODES_H] = math_floor(sys_h() * 0.95);
  1034. }
  1035. }
  1036. }
  1037. else if (ui_base_border_handle == ui_base_hwnds[tab_area_t.STATUS]) {
  1038. let my: i32 = math_floor(mouse_movement_y);
  1039. if (config_raw.layout[layout_size_t.STATUS_H] - my >= ui_statusbar_default_h * config_raw.window_scale &&
  1040. config_raw.layout[layout_size_t.STATUS_H] - my < iron_window_height() * 0.7) {
  1041. config_raw.layout[layout_size_t.STATUS_H] -= my;
  1042. }
  1043. }
  1044. else { // Sidebar
  1045. if (ui_base_border_started == border_side_t.LEFT) {
  1046. config_raw.layout[layout_size_t.SIDEBAR_W] -= math_floor(mouse_movement_x);
  1047. if (config_raw.layout[layout_size_t.SIDEBAR_W] < ui_sidebar_w_mini) {
  1048. config_raw.layout[layout_size_t.SIDEBAR_W] = ui_sidebar_w_mini;
  1049. }
  1050. else if (config_raw.layout[layout_size_t.SIDEBAR_W] > iron_window_width() - ui_sidebar_w_mini * 2) {
  1051. config_raw.layout[layout_size_t.SIDEBAR_W] = iron_window_width() - ui_sidebar_w_mini * 2;
  1052. }
  1053. if (ui_nodes_show || ui_base_show) {
  1054. // Scale down node view if viewport is already at minimal size
  1055. if (config_raw.layout[layout_size_t.NODES_W] + config_raw.layout[layout_size_t.SIDEBAR_W] + ui_sidebar_w_mini > iron_window_width()) {
  1056. config_raw.layout[layout_size_t.NODES_W] = iron_window_width() - config_raw.layout[layout_size_t.SIDEBAR_W] - ui_sidebar_w_mini;
  1057. }
  1058. }
  1059. }
  1060. else {
  1061. let my: i32 = math_floor(mouse_movement_y);
  1062. if (ui_base_border_handle == ui_base_hwnds[tab_area_t.SIDEBAR1] && ui_base_border_started == border_side_t.TOP) {
  1063. if (config_raw.layout[layout_size_t.SIDEBAR_H0] + my > 32 && config_raw.layout[layout_size_t.SIDEBAR_H1] - my > 32) {
  1064. config_raw.layout[layout_size_t.SIDEBAR_H0] += my;
  1065. config_raw.layout[layout_size_t.SIDEBAR_H1] -= my;
  1066. }
  1067. }
  1068. }
  1069. }
  1070. base_resize();
  1071. }
  1072. if (!mouse_down()) {
  1073. ui_base_border_handle = null;
  1074. base_is_resizing = false;
  1075. }
  1076. if (context_raw.tool == tool_type_t.PARTICLE && context_in_paint_area() && !context_raw.paint2d) {
  1077. util_particle_init_physics();
  1078. let world: physics_world_t = physics_world_active;
  1079. physics_world_update(world);
  1080. context_raw.ddirty = 2;
  1081. context_raw.rdirty = 2;
  1082. if (mouse_started()) {
  1083. if (context_raw.particle_timer != null) {
  1084. tween_stop(context_raw.particle_timer);
  1085. let timer: tween_anim_t = context_raw.particle_timer;
  1086. timer.done(timer.done_data);
  1087. context_raw.particle_timer = null;
  1088. }
  1089. history_push_undo = true;
  1090. context_raw.particle_hit_x = context_raw.particle_hit_y = context_raw.particle_hit_z = 0;
  1091. let o: object_t = scene_spawn_object(".Sphere");
  1092. let mo: mesh_object_t = o.ext;
  1093. mo.base.name = ".Bullet";
  1094. mo.base.visible = true;
  1095. let camera: camera_object_t = scene_camera;
  1096. let ct: transform_t = camera.base.transform;
  1097. mo.base.transform.loc = vec4_create(transform_world_x(ct), transform_world_y(ct), transform_world_z(ct));
  1098. mo.base.transform.scale = vec4_create(context_raw.brush_radius * 0.2, context_raw.brush_radius * 0.2, context_raw.brush_radius * 0.2);
  1099. transform_build_matrix(mo.base.transform);
  1100. let body: physics_body_t = physics_body_create();
  1101. body.shape = physics_shape_t.SPHERE;
  1102. body.mass = 1.0;
  1103. physics_body_init(body, mo.base);
  1104. let ray: ray_t = raycast_get_ray(mouse_view_x(), mouse_view_y(), camera);
  1105. physics_body_apply_impulse(body, vec4_mult(ray.dir, 0.15));
  1106. context_raw.particle_timer = tween_timer(5, function(mo: mesh_object_t) {
  1107. mesh_object_remove(mo);
  1108. }, mo);
  1109. }
  1110. let pairs: physics_pair_t[] = physics_world_get_contact_pairs(world, context_raw.paint_body);
  1111. if (pairs != null) {
  1112. for (let i: i32 = 0; i < pairs.length; ++i) {
  1113. let p: physics_pair_t = pairs[i];
  1114. context_raw.last_particle_hit_x = context_raw.particle_hit_x != 0 ? context_raw.particle_hit_x : p.pos_a_x;
  1115. context_raw.last_particle_hit_y = context_raw.particle_hit_y != 0 ? context_raw.particle_hit_y : p.pos_a_y;
  1116. context_raw.last_particle_hit_z = context_raw.particle_hit_z != 0 ? context_raw.particle_hit_z : p.pos_a_z;
  1117. context_raw.particle_hit_x = p.pos_a_x;
  1118. context_raw.particle_hit_y = p.pos_a_y;
  1119. context_raw.particle_hit_z = p.pos_a_z;
  1120. context_raw.pdirty = 1;
  1121. break; // 1 pair for now
  1122. }
  1123. }
  1124. }
  1125. }
  1126. function ui_base_view_top() {
  1127. let is_typing: bool = ui.is_typing;
  1128. if (context_in_paint_area() && !is_typing) {
  1129. if (mouse_view_x() < sys_w()) {
  1130. viewport_set_view(0, 0, 1, 0, 0, 0);
  1131. }
  1132. }
  1133. }
  1134. function ui_base_operator_search() {
  1135. _ui_base_operator_search_first = true;
  1136. ui_menu_draw(function() {
  1137. ui_menu_h = UI_ELEMENT_H() * 8;
  1138. let search_handle: ui_handle_t = ui_handle(__ID__);
  1139. let search: string = ui_text_input(search_handle, "", ui_align_t.LEFT, true, true);
  1140. ui.changed = false;
  1141. if (_ui_base_operator_search_first) {
  1142. _ui_base_operator_search_first = false;
  1143. search_handle.text = "";
  1144. ui_start_text_edit(search_handle); // Focus search bar
  1145. }
  1146. if (search_handle.changed) {
  1147. ui_base_operator_search_offset = 0;
  1148. }
  1149. if (ui.is_key_pressed) { // Move selection
  1150. if (ui.key_code == key_code_t.DOWN && ui_base_operator_search_offset < 6) {
  1151. ui_base_operator_search_offset++;
  1152. }
  1153. if (ui.key_code == key_code_t.UP && ui_base_operator_search_offset > 0) {
  1154. ui_base_operator_search_offset--;
  1155. }
  1156. }
  1157. let enter: bool = keyboard_down("enter");
  1158. let count: i32 = 0;
  1159. let BUTTON_COL: i32 = ui.ops.theme.BUTTON_COL;
  1160. let keys: string[] = map_keys(config_keymap);
  1161. for (let i: i32 = 0; i < keys.length; ++i) {
  1162. let n: string = keys[i];
  1163. if (string_index_of(n, search) >= 0) {
  1164. ui.ops.theme.BUTTON_COL = count == ui_base_operator_search_offset ? ui.ops.theme.HIGHLIGHT_COL : ui.ops.theme.SEPARATOR_COL;
  1165. if (ui_button(n, ui_align_t.LEFT, map_get(config_keymap, n)) || (enter && count == ui_base_operator_search_offset)) {
  1166. if (enter) {
  1167. ui.changed = true;
  1168. count = 6; // Trigger break
  1169. }
  1170. operator_run(n);
  1171. }
  1172. if (++count > 6) {
  1173. break;
  1174. }
  1175. }
  1176. }
  1177. if (enter && count == 0) { // Hide popup on enter when command is not found
  1178. ui.changed = true;
  1179. search_handle.text = "";
  1180. }
  1181. ui.ops.theme.BUTTON_COL = BUTTON_COL;
  1182. });
  1183. }
  1184. function ui_base_toggle_distract_free() {
  1185. ui_base_show = !ui_base_show;
  1186. base_resize();
  1187. }
  1188. function ui_base_get_radius_increment(): f32 {
  1189. return 0.1;
  1190. }
  1191. function ui_base_hit_rect(mx: f32, my: f32, x: i32, y: i32, w: i32, h: i32): bool {
  1192. return mx > x && mx < x + w && my > y && my < y + h;
  1193. }
  1194. function ui_base_get_brush_stencil_rect(): rect_t {
  1195. let w: i32 = math_floor(context_raw.brush_stencil_image.width * (base_h() / context_raw.brush_stencil_image.height) * context_raw.brush_stencil_scale);
  1196. let h: i32 = math_floor(base_h() * context_raw.brush_stencil_scale);
  1197. let x: i32 = math_floor(base_x() + context_raw.brush_stencil_x * base_w());
  1198. let y: i32 = math_floor(base_y() + context_raw.brush_stencil_y * base_h());
  1199. let r: rect_t = {w : w, h : h, x : x, y : y};
  1200. return r;
  1201. }
  1202. function ui_base_update_ui() {
  1203. if (console_message_timer > 0) {
  1204. console_message_timer -= sys_delta();
  1205. if (console_message_timer <= 0) {
  1206. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  1207. }
  1208. }
  1209. ui_sidebar_w_mini = math_floor(ui_sidebar_default_w_mini * UI_SCALE());
  1210. if (!base_ui_enabled) {
  1211. return;
  1212. }
  1213. // Same mapping for paint and rotate (predefined in touch keymap)
  1214. if (context_in_3d_view()) {
  1215. let paint_key: string = map_get(config_keymap, "action_paint");
  1216. let rotate_key: string = map_get(config_keymap, "action_rotate");
  1217. if (mouse_started() && paint_key == rotate_key) {
  1218. ui_base_action_paint_remap = paint_key;
  1219. util_render_pick_pos_nor_tex();
  1220. let is_mesh: bool = math_abs(context_raw.posx_picked) < 50 && math_abs(context_raw.posy_picked) < 50 && math_abs(context_raw.posz_picked) < 50;
  1221. /// if arm_android
  1222. // Allow rotating with both pen and touch, because hovering a pen prevents touch input on android
  1223. let pen_only: bool = false;
  1224. /// else
  1225. let pen_only: bool = context_raw.pen_painting_only;
  1226. /// end
  1227. let is_pen: bool = pen_only && pen_down();
  1228. // Mesh picked - disable rotate
  1229. // Pen painting only - rotate with touch, paint with pen
  1230. if ((is_mesh && !pen_only) || is_pen) {
  1231. map_set(config_keymap, "action_rotate", "");
  1232. map_set(config_keymap, "action_paint", ui_base_action_paint_remap);
  1233. }
  1234. // World sphere picked - disable paint
  1235. else {
  1236. map_set(config_keymap, "action_paint", "");
  1237. map_set(config_keymap, "action_rotate", ui_base_action_paint_remap);
  1238. }
  1239. }
  1240. else if (!mouse_down() && ui_base_action_paint_remap != "") {
  1241. map_set(config_keymap, "action_rotate", ui_base_action_paint_remap);
  1242. map_set(config_keymap, "action_paint", ui_base_action_paint_remap);
  1243. ui_base_action_paint_remap = "";
  1244. }
  1245. }
  1246. if (context_raw.brush_stencil_image != null && operator_shortcut(map_get(config_keymap, "stencil_transform"), shortcut_type_t.DOWN)) {
  1247. let r: rect_t = ui_base_get_brush_stencil_rect();
  1248. if (mouse_started("left")) {
  1249. context_raw.brush_stencil_scaling =
  1250. ui_base_hit_rect(mouse_x, mouse_y, r.x - 8, r.y - 8, 16, 16) || ui_base_hit_rect(mouse_x, mouse_y, r.x - 8, r.h + r.y - 8, 16, 16) ||
  1251. ui_base_hit_rect(mouse_x, mouse_y, r.w + r.x - 8, r.y - 8, 16, 16) || ui_base_hit_rect(mouse_x, mouse_y, r.w + r.x - 8, r.h + r.y - 8, 16, 16);
  1252. let cosa: f32 = math_cos(-context_raw.brush_stencil_angle);
  1253. let sina: f32 = math_sin(-context_raw.brush_stencil_angle);
  1254. let ox: f32 = 0;
  1255. let oy: f32 = -r.h / 2;
  1256. let x: f32 = ox * cosa - oy * sina;
  1257. let y: f32 = ox * sina + oy * cosa;
  1258. x += r.x + r.w / 2;
  1259. y += r.y + r.h / 2;
  1260. context_raw.brush_stencil_rotating = ui_base_hit_rect(mouse_x, mouse_y, math_floor(x - 16), math_floor(y - 16), 32, 32);
  1261. }
  1262. let _scale: f32 = context_raw.brush_stencil_scale;
  1263. if (mouse_down("left")) {
  1264. if (context_raw.brush_stencil_scaling) {
  1265. let mult: i32 = mouse_x > r.x + r.w / 2 ? 1 : -1;
  1266. context_raw.brush_stencil_scale += mouse_movement_x / 400 * mult;
  1267. }
  1268. else if (context_raw.brush_stencil_rotating) {
  1269. let gizmo_x: f32 = r.x + r.w / 2;
  1270. let gizmo_y: f32 = r.y + r.h / 2;
  1271. context_raw.brush_stencil_angle = -math_atan2(mouse_y - gizmo_y, mouse_x - gizmo_x) - math_pi() / 2;
  1272. }
  1273. else {
  1274. context_raw.brush_stencil_x += mouse_movement_x / base_w();
  1275. context_raw.brush_stencil_y += mouse_movement_y / base_h();
  1276. }
  1277. }
  1278. else {
  1279. context_raw.brush_stencil_scaling = false;
  1280. }
  1281. if (mouse_wheel_delta != 0) {
  1282. context_raw.brush_stencil_scale -= mouse_wheel_delta / 10;
  1283. }
  1284. // Center after scale
  1285. let ratio: f32 = base_h() / context_raw.brush_stencil_image.height;
  1286. let old_w: f32 = _scale * context_raw.brush_stencil_image.width * ratio;
  1287. let new_w: f32 = context_raw.brush_stencil_scale * context_raw.brush_stencil_image.width * ratio;
  1288. let old_h: f32 = _scale * base_h();
  1289. let new_h: f32 = context_raw.brush_stencil_scale * base_h();
  1290. context_raw.brush_stencil_x += (old_w - new_w) / base_w() / 2;
  1291. context_raw.brush_stencil_y += (old_h - new_h) / base_h() / 2;
  1292. }
  1293. let set_clone_source: bool =
  1294. context_raw.tool == tool_type_t.CLONE &&
  1295. operator_shortcut(map_get(config_keymap, "set_clone_source") + "+" + map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN);
  1296. let decal_mask: bool = context_is_decal_mask_paint();
  1297. let down: bool = operator_shortcut(map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN) || decal_mask || set_clone_source ||
  1298. operator_shortcut(map_get(config_keymap, "brush_ruler") + "+" + map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN) ||
  1299. (pen_down() && !keyboard_down("alt"));
  1300. if (config_raw.touch_ui) {
  1301. if (pen_down()) {
  1302. context_raw.pen_painting_only = true;
  1303. }
  1304. else if (context_raw.pen_painting_only) {
  1305. down = false;
  1306. }
  1307. }
  1308. if (context_raw.tool == tool_type_t.PARTICLE) {
  1309. down = false;
  1310. }
  1311. if (down) {
  1312. let mx: i32 = mouse_view_x();
  1313. let my: i32 = mouse_view_y();
  1314. let ww: i32 = sys_w();
  1315. if (context_raw.paint2d) {
  1316. mx -= sys_w();
  1317. ww = ui_view2d_ww;
  1318. }
  1319. if (mx < ww && mx > sys_x() && my < sys_h() && my > sys_y()) {
  1320. if (set_clone_source) {
  1321. context_raw.clone_start_x = mx;
  1322. context_raw.clone_start_y = my;
  1323. }
  1324. else {
  1325. if (context_raw.brush_time == 0 && !base_is_dragging && !base_is_resizing && ui.combo_selected_handle == null) { // Paint started
  1326. // Draw line
  1327. if (operator_shortcut(map_get(config_keymap, "brush_ruler") + "+" + map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN)) {
  1328. context_raw.last_paint_vec_x = context_raw.last_paint_x;
  1329. context_raw.last_paint_vec_y = context_raw.last_paint_y;
  1330. }
  1331. history_push_undo = true;
  1332. if (context_raw.tool == tool_type_t.CLONE && context_raw.clone_start_x >= 0.0) { // Clone delta
  1333. context_raw.clone_delta_x = (context_raw.clone_start_x - mx) / ww;
  1334. context_raw.clone_delta_y = (context_raw.clone_start_y - my) / sys_h();
  1335. context_raw.clone_start_x = -1;
  1336. }
  1337. else if (context_raw.tool == tool_type_t.FILL && context_raw.fill_type_handle.i == fill_type_t.UV_ISLAND) {
  1338. util_uv_uvislandmap_cached = false;
  1339. }
  1340. }
  1341. context_raw.brush_time += sys_delta();
  1342. if (context_raw.run_brush != null) {
  1343. context_raw.run_brush(context_raw.brush_output_node_inst, 0);
  1344. }
  1345. }
  1346. }
  1347. }
  1348. else if (context_raw.brush_time > 0) { // Brush released
  1349. context_raw.brush_time = 0;
  1350. context_raw.prev_paint_vec_x = -1;
  1351. context_raw.prev_paint_vec_y = -1;
  1352. /// if (arm_opengl || arm_direct3d11) // Keep accumulated samples for D3D12
  1353. context_raw.ddirty = 3;
  1354. /// end
  1355. context_raw.brush_blend_dirty = true; // Update brush mask
  1356. context_raw.layer_preview_dirty = true; // Update layer preview
  1357. // New color id picked, update fill layer
  1358. if (context_raw.tool == tool_type_t.COLORID && context_raw.layer.fill_layer != null) {
  1359. sys_notify_on_next_frame(function() {
  1360. layers_update_fill_layer();
  1361. make_material_parse_paint_material(false);
  1362. });
  1363. }
  1364. }
  1365. if (context_raw.layers_preview_dirty) {
  1366. context_raw.layers_preview_dirty = false;
  1367. context_raw.layer_preview_dirty = false;
  1368. context_raw.mask_preview_last = null;
  1369. // Update all layer previews
  1370. for (let i: i32 = 0; i < project_layers.length; ++i) {
  1371. let l: slot_layer_t = project_layers[i];
  1372. if (slot_layer_is_group(l)) {
  1373. continue;
  1374. }
  1375. let target: gpu_texture_t = l.texpaint_preview;
  1376. if (target == null) {
  1377. continue;
  1378. }
  1379. let source: gpu_texture_t = l.texpaint;
  1380. draw_begin(target, true, 0x00000000);
  1381. // draw_set_pipeline(l.is_mask() ? pipes_copy8 : pipes_copy);
  1382. draw_set_pipeline(pipes_copy); // texpaint_preview is always RGBA32 for now
  1383. draw_scaled_image(source, 0, 0, target.width, target.height);
  1384. draw_set_pipeline(null);
  1385. draw_end();
  1386. }
  1387. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  1388. }
  1389. if (context_raw.layer != null && context_raw.layer_preview_dirty && !slot_layer_is_group(context_raw.layer)) {
  1390. context_raw.layer_preview_dirty = false;
  1391. context_raw.mask_preview_last = null;
  1392. // Update layer preview
  1393. let l: slot_layer_t = context_raw.layer;
  1394. let target: gpu_texture_t = l.texpaint_preview;
  1395. if (target != null) {
  1396. let source: gpu_texture_t = l.texpaint;
  1397. draw_begin(target, true, 0x00000000);
  1398. // draw_set_pipeline(raw.layer.is_mask() ? pipes_copy8 : pipes_copy);
  1399. draw_set_pipeline(pipes_copy); // texpaint_preview is always RGBA32 for now
  1400. draw_scaled_image(source, 0, 0, target.width, target.height);
  1401. draw_set_pipeline(null);
  1402. draw_end();
  1403. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  1404. }
  1405. }
  1406. let undo_pressed: bool = operator_shortcut(map_get(config_keymap, "edit_undo"));
  1407. let redo_pressed: bool = operator_shortcut(map_get(config_keymap, "edit_redo")) || (keyboard_down("control") && keyboard_started("y"));
  1408. // Two-finger tap to undo, three-finger tap to redo
  1409. if (context_in_3d_view() && config_raw.touch_ui) {
  1410. if (mouse_started("middle")) {
  1411. ui_base_redo_tap_time = sys_time();
  1412. }
  1413. else if (mouse_started("right")) {
  1414. ui_base_undo_tap_time = sys_time();
  1415. }
  1416. else if (mouse_released("middle") && sys_time() - ui_base_redo_tap_time < 0.1) {
  1417. ui_base_redo_tap_time = ui_base_undo_tap_time = 0;
  1418. redo_pressed = true;
  1419. }
  1420. else if (mouse_released("right") && sys_time() - ui_base_undo_tap_time < 0.1) {
  1421. ui_base_redo_tap_time = ui_base_undo_tap_time = 0;
  1422. undo_pressed = true;
  1423. }
  1424. }
  1425. if (undo_pressed) {
  1426. history_undo();
  1427. }
  1428. else if (redo_pressed) {
  1429. history_redo();
  1430. }
  1431. gizmo_update();
  1432. }
  1433. function ui_base_render() {
  1434. if (!ui_base_show && config_raw.touch_ui) {
  1435. ui.input_enabled = true;
  1436. ui_begin(ui);
  1437. if (ui_window(ui_handle(__ID__), 0, 0, 150, math_floor(UI_ELEMENT_H() + UI_ELEMENT_OFFSET() + 1))) {
  1438. if (ui_button(tr("Close"))) {
  1439. ui_base_toggle_distract_free();
  1440. }
  1441. }
  1442. ui_end();
  1443. }
  1444. if (!ui_base_show) {
  1445. return;
  1446. }
  1447. ui.input_enabled = base_ui_enabled;
  1448. // Remember last tab positions
  1449. for (let i: i32 = 0; i < ui_base_htabs.length; ++i) {
  1450. if (ui_base_htabs[i].changed) {
  1451. config_raw.layout_tabs[i] = ui_base_htabs[i].i;
  1452. config_save();
  1453. }
  1454. }
  1455. // Set tab positions
  1456. for (let i: i32 = 0; i < ui_base_htabs.length; ++i) {
  1457. ui_base_htabs[i].i = config_raw.layout_tabs[i];
  1458. }
  1459. // Nothing to display in the main area
  1460. if (!base_view3d_show && !ui_nodes_show && !ui_view2d_show) {
  1461. draw_begin(null, true, base_theme.SEPARATOR_COL);
  1462. let img: gpu_texture_t = data_get_image("badge_bw.k");
  1463. draw_set_color(0x22ffffff);
  1464. draw_image(img, base_view3d_w() / 2 - img.width / 2, base_h() / 2 - img.height / 2);
  1465. draw_end();
  1466. }
  1467. ui_begin(ui);
  1468. ui_toolbar_render_ui();
  1469. ui_menubar_render_ui();
  1470. ui_header_render_ui();
  1471. ui_statusbar_render_ui();
  1472. ui_sidebar_render_ui();
  1473. ui_end();
  1474. ui.input_enabled = true;
  1475. }
  1476. function ui_base_render_cursor() {
  1477. if (!base_ui_enabled) {
  1478. return;
  1479. }
  1480. if (context_raw.tool == tool_type_t.MATERIAL || context_raw.tool == tool_type_t.BAKE) {
  1481. return;
  1482. }
  1483. draw_begin();
  1484. draw_set_color(0xffffffff);
  1485. context_raw.view_index = context_raw.view_index_last;
  1486. let mx: i32 = base_x() + context_raw.paint_vec.x * base_w();
  1487. let my: i32 = base_y() + context_raw.paint_vec.y * base_h();
  1488. context_raw.view_index = -1;
  1489. if (context_raw.brush_stencil_image != null && context_raw.tool != tool_type_t.PICKER && context_raw.tool != tool_type_t.COLORID) {
  1490. let r: rect_t = ui_base_get_brush_stencil_rect();
  1491. if (!operator_shortcut(map_get(config_keymap, "stencil_hide"), shortcut_type_t.DOWN)) {
  1492. draw_set_color(0x88ffffff);
  1493. let angle: f32 = context_raw.brush_stencil_angle;
  1494. draw_set_transform(mat3_multmat(mat3_multmat(mat3_translation(0.5, 0.5), mat3_rotation(-angle)), mat3_translation(-0.5, -0.5)));
  1495. draw_scaled_image(context_raw.brush_stencil_image, r.x, r.y, r.w, r.h);
  1496. draw_set_transform(mat3_nan());
  1497. draw_set_color(0xffffffff);
  1498. }
  1499. let transform: bool = operator_shortcut(map_get(config_keymap, "stencil_transform"), shortcut_type_t.DOWN);
  1500. if (transform) {
  1501. // Outline
  1502. draw_rect(r.x, r.y, r.w, r.h);
  1503. // Scale
  1504. draw_rect(r.x - 8, r.y - 8, 16, 16);
  1505. draw_rect(r.x - 8 + r.w, r.y - 8, 16, 16);
  1506. draw_rect(r.x - 8, r.y - 8 + r.h, 16, 16);
  1507. draw_rect(r.x - 8 + r.w, r.y - 8 + r.h, 16, 16);
  1508. // Rotate
  1509. let cosa: f32 = math_cos(-context_raw.brush_stencil_angle);
  1510. let sina: f32 = math_sin(-context_raw.brush_stencil_angle);
  1511. let ox: f32 = 0;
  1512. let oy: f32 = -r.h / 2;
  1513. let x: f32 = ox * cosa - oy * sina;
  1514. let y: f32 = ox * sina + oy * cosa;
  1515. x += r.x + r.w / 2;
  1516. y += r.y + r.h / 2;
  1517. draw_filled_circle(x, y, 8);
  1518. }
  1519. }
  1520. // Show picked material next to cursor
  1521. if (context_raw.tool == tool_type_t.PICKER && context_raw.picker_select_material && context_raw.color_picker_callback == null) {
  1522. let img: gpu_texture_t = context_raw.material.image_icon;
  1523. draw_image(img, mx + 10, my + 10);
  1524. }
  1525. if (context_raw.tool == tool_type_t.PICKER && context_raw.color_picker_callback != null) {
  1526. let img: gpu_texture_t = resource_get("icons.k");
  1527. let rect: rect_t = resource_tile50(img, tool_type_t.PICKER, 0);
  1528. draw_sub_image(img, mx + 10, my + 10, rect.x, rect.y, rect.w, rect.h);
  1529. }
  1530. let cursor_img: gpu_texture_t = resource_get("cursor.k");
  1531. let psize: i32 = math_floor(182 * (context_raw.brush_radius * context_raw.brush_nodes_radius) * UI_SCALE());
  1532. // Clone source cursor
  1533. if (context_raw.tool == tool_type_t.CLONE && !keyboard_down("alt") && (mouse_down() || pen_down())) {
  1534. draw_set_color(0x66ffffff);
  1535. draw_scaled_image(cursor_img, mx + context_raw.clone_delta_x * sys_w() - psize / 2, my + context_raw.clone_delta_y * sys_h() - psize / 2, psize, psize);
  1536. draw_set_color(0xffffffff);
  1537. }
  1538. let decal: bool = context_is_decal();
  1539. if (context_in_2d_view() || decal) {
  1540. let decal_mask: bool = context_is_decal_mask();
  1541. if (decal && !context_in_nodes()) {
  1542. let decal_alpha: f32 = 0.5;
  1543. if (!decal_mask) {
  1544. context_raw.decal_x = context_raw.paint_vec.x;
  1545. context_raw.decal_y = context_raw.paint_vec.y;
  1546. decal_alpha = context_raw.brush_opacity;
  1547. }
  1548. if (!config_raw.brush_live) {
  1549. let psizex: i32 = math_floor(256 * UI_SCALE() * (context_raw.brush_radius * context_raw.brush_nodes_radius * context_raw.brush_scale_x));
  1550. let psizey: i32 = math_floor(256 * UI_SCALE() * (context_raw.brush_radius * context_raw.brush_nodes_radius));
  1551. context_raw.view_index = context_raw.view_index_last;
  1552. let decalx: f32 = base_x() + context_raw.decal_x * base_w() - psizex / 2;
  1553. let decaly: f32 = base_y() + context_raw.decal_y * base_h() - psizey / 2;
  1554. context_raw.view_index = -1;
  1555. draw_set_color(color_from_floats(1, 1, 1, decal_alpha));
  1556. let angle: f32 = (context_raw.brush_angle + context_raw.brush_nodes_angle) * (math_pi() / 180);
  1557. draw_set_transform(mat3_multmat(mat3_multmat(mat3_translation(0.5, 0.5), mat3_rotation(angle)), mat3_translation(-0.5, -0.5)));
  1558. draw_scaled_image(context_raw.decal_image, decalx, decaly, psizex, psizey);
  1559. draw_set_transform(mat3_nan());
  1560. draw_set_color(0xffffffff);
  1561. }
  1562. }
  1563. if (context_raw.tool == tool_type_t.BRUSH || context_raw.tool == tool_type_t.ERASER || context_raw.tool == tool_type_t.CLONE ||
  1564. context_raw.tool == tool_type_t.BLUR || context_raw.tool == tool_type_t.SMUDGE || context_raw.tool == tool_type_t.PARTICLE ||
  1565. (decal_mask && context_in_2d_view())) {
  1566. if (decal_mask) {
  1567. psize = math_floor(cursor_img.width * (context_raw.brush_decal_mask_radius * context_raw.brush_nodes_radius) * UI_SCALE());
  1568. }
  1569. if (context_in_2d_view()) {
  1570. psize = math_floor(psize * ui_view2d_pan_scale);
  1571. }
  1572. draw_scaled_image(cursor_img, mx - psize / 2, my - psize / 2, psize, psize);
  1573. }
  1574. }
  1575. if (context_raw.brush_lazy_radius > 0 && !context_raw.brush_locked &&
  1576. (context_raw.tool == tool_type_t.BRUSH || context_raw.tool == tool_type_t.ERASER || context_raw.tool == tool_type_t.DECAL ||
  1577. context_raw.tool == tool_type_t.TEXT || context_raw.tool == tool_type_t.CLONE || context_raw.tool == tool_type_t.BLUR ||
  1578. context_raw.tool == tool_type_t.SMUDGE || context_raw.tool == tool_type_t.PARTICLE)) {
  1579. draw_filled_rect(mx - 1, my - 1, 2, 2);
  1580. mx = context_raw.brush_lazy_x * base_w() + base_x();
  1581. my = context_raw.brush_lazy_y * base_h() + base_y();
  1582. let radius: f32 = context_raw.brush_lazy_radius * 180;
  1583. draw_set_color(0xff666666);
  1584. draw_scaled_image(cursor_img, mx - radius / 2, my - radius / 2, radius, radius);
  1585. draw_set_color(0xffffffff);
  1586. }
  1587. draw_end();
  1588. }
  1589. function ui_base_show_material_nodes() {
  1590. // Clear input state as ui receives input events even when not drawn
  1591. ui_end_input();
  1592. ui_nodes_show = !ui_nodes_show || ui_nodes_canvas_type != canvas_type_t.MATERIAL;
  1593. ui_nodes_canvas_type = canvas_type_t.MATERIAL;
  1594. /// if (arm_ios || arm_android)
  1595. if (ui_view2d_show) {
  1596. ui_view2d_show = false;
  1597. }
  1598. /// end
  1599. base_resize();
  1600. }
  1601. function ui_base_show_brush_nodes() {
  1602. // Clear input state as ui receives input events even when not drawn
  1603. ui_end_input();
  1604. ui_nodes_show = !ui_nodes_show || ui_nodes_canvas_type != canvas_type_t.BRUSH;
  1605. ui_nodes_canvas_type = canvas_type_t.BRUSH;
  1606. /// if (arm_ios || arm_android)
  1607. if (ui_view2d_show) {
  1608. ui_view2d_show = false;
  1609. }
  1610. /// end
  1611. base_resize();
  1612. }
  1613. function ui_base_show_2d_view(type: view_2d_type_t) {
  1614. // Clear input state as ui receives input events even when not drawn
  1615. ui_end_input();
  1616. if (ui_view2d_type != type) {
  1617. ui_view2d_show = true;
  1618. }
  1619. else {
  1620. ui_view2d_show = !ui_view2d_show;
  1621. }
  1622. ui_view2d_type = type;
  1623. ui_view2d_hwnd.redraws = 2;
  1624. /// if (arm_ios || arm_android)
  1625. if (ui_nodes_show) {
  1626. ui_nodes_show = false;
  1627. }
  1628. /// end
  1629. base_resize();
  1630. }
  1631. function ui_base_toggle_browser() {
  1632. let minimized: bool = config_raw.layout[layout_size_t.STATUS_H] <= (ui_statusbar_default_h * config_raw.window_scale);
  1633. config_raw.layout[layout_size_t.STATUS_H] = minimized ? 240 : ui_statusbar_default_h;
  1634. config_raw.layout[layout_size_t.STATUS_H] = math_floor(config_raw.layout[layout_size_t.STATUS_H] * config_raw.window_scale);
  1635. base_resize();
  1636. }
  1637. function ui_base_set_icon_scale() {
  1638. if (UI_SCALE() > 1) {
  1639. let res: string[] = [ "icons2x.k" ];
  1640. resource_load(res);
  1641. map_set(resource_bundled, "icons.k", resource_get("icons2x.k"));
  1642. }
  1643. else {
  1644. let res: string[] = [ "icons.k" ];
  1645. resource_load(res);
  1646. }
  1647. }
  1648. function ui_base_on_border_hover(handle: ui_handle_t, side: i32) {
  1649. if (!base_ui_enabled) {
  1650. return;
  1651. }
  1652. if (handle != ui_base_hwnds[tab_area_t.SIDEBAR0] && handle != ui_base_hwnds[tab_area_t.SIDEBAR1] && handle != ui_base_hwnds[tab_area_t.STATUS] &&
  1653. handle != ui_nodes_hwnd && handle != ui_view2d_hwnd) {
  1654. return; // Scalable handles
  1655. }
  1656. if (handle == ui_view2d_hwnd && side != border_side_t.LEFT) {
  1657. return;
  1658. }
  1659. if (handle == ui_nodes_hwnd && side == border_side_t.TOP && !ui_view2d_show) {
  1660. return;
  1661. }
  1662. if (handle == ui_base_hwnds[tab_area_t.SIDEBAR0] && side == border_side_t.TOP) {
  1663. return;
  1664. }
  1665. if (handle == ui_nodes_hwnd && side != border_side_t.LEFT && side != border_side_t.TOP) {
  1666. return;
  1667. }
  1668. if (handle == ui_base_hwnds[tab_area_t.STATUS] && side != border_side_t.TOP) {
  1669. return;
  1670. }
  1671. if (side == border_side_t.RIGHT) {
  1672. return; // UI is snapped to the right side
  1673. }
  1674. side == border_side_t.LEFT || side == border_side_t.RIGHT ? iron_mouse_set_cursor(iron_cursor_t.SIZEWE) : iron_mouse_set_cursor(iron_cursor_t.SIZENS);
  1675. if (ui.input_started) {
  1676. ui_base_border_started = side;
  1677. ui_base_border_handle = handle;
  1678. base_is_resizing = true;
  1679. }
  1680. }
  1681. function ui_base_on_tab_drop(to: ui_handle_t, to_position: i32, from: ui_handle_t, from_position: i32) {
  1682. let i: i32 = -1;
  1683. let j: i32 = -1;
  1684. for (let k: i32 = 0; k < ui_base_htabs.length; ++k) {
  1685. if (ui_base_htabs[k] == to) {
  1686. i = k;
  1687. }
  1688. if (ui_base_htabs[k] == from) {
  1689. j = k;
  1690. }
  1691. }
  1692. if (i == j && to_position == from_position) {
  1693. return;
  1694. }
  1695. if (i > -1 && j > -1) {
  1696. let tabsi: tab_draw_t[] = ui_base_hwnd_tabs[i];
  1697. let tabsj: tab_draw_t[] = ui_base_hwnd_tabs[j];
  1698. if (tabsj.length == 1) {
  1699. return; // Keep at least one tab in place
  1700. }
  1701. let element: tab_draw_t = tabsj[from_position];
  1702. array_splice(tabsj, from_position, 1);
  1703. array_insert(tabsi, to_position, element);
  1704. ui_base_hwnds[i].redraws = 2;
  1705. ui_base_hwnds[j].redraws = 2;
  1706. }
  1707. }
  1708. function base_redraw_ui() {
  1709. ui_header_handle.redraws = 2;
  1710. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  1711. ui_menubar_menu_handle.redraws = 2;
  1712. ui_menubar_hwnd.redraws = 2;
  1713. ui_nodes_hwnd.redraws = 2;
  1714. ui_box_hwnd.redraws = 2;
  1715. ui_view2d_hwnd.redraws = 2;
  1716. // Redraw viewport
  1717. if (context_raw.ddirty < 0) {
  1718. context_raw.ddirty = 0;
  1719. }
  1720. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  1721. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  1722. ui_toolbar_handle.redraws = 2;
  1723. if (context_raw.split_view) {
  1724. context_raw.ddirty = 1;
  1725. }
  1726. }
  1727. function ui_base_make_empty_envmap(col: i32) {
  1728. ui_base_viewport_col = col;
  1729. let b: u8_array_t = u8_array_create(4);
  1730. b[0] = color_get_rb(col);
  1731. b[1] = color_get_gb(col);
  1732. b[2] = color_get_bb(col);
  1733. b[3] = 255;
  1734. context_raw.empty_envmap = gpu_create_texture_from_bytes(b, 1, 1);
  1735. }
  1736. function ui_base_set_viewport_col(col: i32) {
  1737. ui_base_make_empty_envmap(col);
  1738. context_raw.ddirty = 2;
  1739. if (!context_raw.show_envmap) {
  1740. scene_world._.envmap = context_raw.empty_envmap;
  1741. }
  1742. }