level_editor.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. /*
  2. * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #if CROWN_TOOLS
  6. #include "core/containers/vector.h"
  7. #include "core/filesystem/file.h"
  8. #include "core/filesystem/filesystem_disk.h"
  9. #include "core/filesystem/path.h"
  10. #include "core/json/json.h"
  11. #include "core/json/json_object.h"
  12. #include "core/json/sjson.h"
  13. #include "core/math/vector2.h"
  14. #include "core/network/ip_address.h"
  15. #include "core/network/socket.h"
  16. #include "core/process.h"
  17. #include "core/strings/dynamic_string.h"
  18. #include "device/device.h"
  19. #include "device/device_event_queue.h"
  20. #include "device/device_options.h"
  21. #include "device/input_device.h"
  22. #include "device/input_manager.h"
  23. #include "device/input_types.h"
  24. #include "device/log.h"
  25. #include "device/pipeline.h"
  26. #include "imgui_context.h"
  27. #include "resource/resource_manager.h"
  28. #include "resource/texture_resource.h"
  29. #include "tool_api.h"
  30. #include <iconfontheaders/icons_material_design.h>
  31. #include <imgui.h>
  32. #include <nfd.h>
  33. #include <time.h>
  34. #if CROWN_PLATFORM_POSIX
  35. #include <sys/time.h>
  36. #endif
  37. LOG_SYSTEM(LEVEL_EDITOR, "level_editor")
  38. namespace crown
  39. {
  40. struct Project
  41. {
  42. DynamicString _data_dir;
  43. Project(Allocator& a)
  44. : _data_dir(a)
  45. {
  46. }
  47. };
  48. static u16 _width = 1280;
  49. static u16 _height = 720;
  50. static struct LevelEditor* _editor;
  51. static struct Project* _project;
  52. static TCPSocket _client;
  53. static Process _game_process;
  54. struct StartGame
  55. {
  56. enum Enum
  57. {
  58. NORMAL,
  59. TEST
  60. };
  61. };
  62. static void stop_game()
  63. {
  64. if (_game_process.spawned())
  65. {
  66. _game_process.force_exit();
  67. _game_process.wait();
  68. }
  69. }
  70. static void start_game(StartGame::Enum sg, const char* data_dir)
  71. {
  72. // Stop any previously launched game
  73. stop_game();
  74. const char* argv[] =
  75. {
  76. "./crown-debug",
  77. "--data-dir", data_dir,
  78. "--console-port", "12345",
  79. // "--wait-console",
  80. "--lua-string", sg == StartGame::TEST ? "TEST=true" : "",
  81. NULL
  82. };
  83. _game_process.spawn(argv);
  84. }
  85. struct Pivot
  86. {
  87. enum Enum
  88. {
  89. TOP_LEFT,
  90. TOP_CENTER,
  91. TOP_RIGHT,
  92. LEFT,
  93. CENTER,
  94. RIGHT,
  95. BOTTOM_LEFT,
  96. BOTTOM_CENTER,
  97. BOTTOM_RIGHT,
  98. COUNT
  99. };
  100. };
  101. static const char* pivot_names[] =
  102. {
  103. "Top Left", // Pivot::TOP_LEFT
  104. "Top Right", // Pivot::TOP_CENTER
  105. "Top Center", // Pivot::TOP_RIGHT
  106. "Left", // Pivot::LEFT
  107. "Center", // Pivot::CENTER
  108. "Right", // Pivot::RIGHT
  109. "Bottom Left", // Pivot::BOTTOM_LEFT
  110. "Bottom Center", // Pivot::BOTTOM_CENTER
  111. "Bottom Right" // Pivot::BOTTOM_RIGHT
  112. };
  113. CE_STATIC_ASSERT(countof(pivot_names) == Pivot::COUNT);
  114. Vector2 sprite_cell_xy(int r, int c, int offset_x, int offset_y, int cell_w, int cell_h, int spacing_x, int spacing_y)
  115. {
  116. int x0 = offset_x + c*cell_w + c*spacing_x;
  117. int y0 = offset_y + r*cell_h + r*spacing_y;
  118. return vector2((f32)x0, (f32)y0);
  119. }
  120. Vector2 sprite_cell_pivot_xy(int cell_w, int cell_h, int pivot)
  121. {
  122. int pivot_x = 0;
  123. int pivot_y = 0;
  124. switch (pivot)
  125. {
  126. case Pivot::TOP_LEFT:
  127. pivot_x = 0;
  128. pivot_y = 0;
  129. break;
  130. case Pivot::TOP_CENTER:
  131. pivot_x = cell_w / 2;
  132. pivot_y = 0;
  133. break;
  134. case Pivot::TOP_RIGHT:
  135. pivot_x = cell_w;
  136. pivot_y = 0;
  137. break;
  138. case Pivot::BOTTOM_LEFT:
  139. pivot_x = 0;
  140. pivot_y = cell_h;
  141. break;
  142. case Pivot::BOTTOM_CENTER:
  143. pivot_x = cell_w / 2;
  144. pivot_y = cell_h;
  145. break;
  146. case Pivot::BOTTOM_RIGHT:
  147. pivot_x = cell_w;
  148. pivot_y = cell_h;
  149. break;
  150. case Pivot::LEFT:
  151. pivot_x = 0;
  152. pivot_y = cell_h / 2;
  153. break;
  154. case Pivot::CENTER:
  155. pivot_x = cell_w / 2;
  156. pivot_y = cell_h / 2;
  157. break;
  158. case Pivot::RIGHT:
  159. pivot_x = cell_w;
  160. pivot_y = cell_h / 2;
  161. break;
  162. default:
  163. CE_FATAL("Unknown pivot");
  164. break;
  165. }
  166. return vector2((f32)pivot_x, (f32)pivot_y);
  167. }
  168. struct SpriteImporter
  169. {
  170. int width;
  171. int height;
  172. int cells_h;
  173. int cells_v;
  174. bool cell_wh_auto;
  175. int cell_w;
  176. int cell_h;
  177. int offset_x;
  178. int offset_y;
  179. int spacing_x;
  180. int spacing_y;
  181. int pivot;
  182. int layer;
  183. int depth;
  184. SpriteImporter()
  185. : width(128)
  186. , height(128)
  187. , cells_h(4)
  188. , cells_v(4)
  189. , cell_wh_auto(false)
  190. , cell_w(16)
  191. , cell_h(16)
  192. , offset_x(0)
  193. , offset_y(0)
  194. , spacing_x(0)
  195. , spacing_y(0)
  196. , pivot(Pivot::CENTER)
  197. , layer(0)
  198. , depth(0)
  199. {
  200. }
  201. void draw()
  202. {
  203. ImGui::Columns(2);
  204. #if 1
  205. ImDrawList* draw_list = ImGui::GetWindowDrawList();
  206. {
  207. // Here we are using InvisibleButton() as a convenience to 1) advance the cursor and 2) allows us to use IsItemHovered()
  208. // However you can draw directly and poll mouse/keyboard by yourself. You can manipulate the cursor using GetCursorPos() and SetCursorPos().
  209. // If you only use the ImDrawList API, you can notify the owner window of its extends by using SetCursorPos(max).
  210. ImVec2 canvas_pos = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates!
  211. ImVec2 canvas_size = ImGui::GetContentRegionAvail(); // Resize canvas to what's available
  212. if (canvas_size.x < 50.0f) canvas_size.x = 10.0f;
  213. if (canvas_size.y < 50.0f) canvas_size.y = 50.0f;
  214. draw_list->AddRectFilledMultiColor(canvas_pos, ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + canvas_size.y), ImColor(50,50,50), ImColor(50,50,60), ImColor(60,60,70), ImColor(50,50,60));
  215. // Pivot is relative to the top-left corner of the cell
  216. Vector2 pivot_xy = sprite_cell_pivot_xy(cell_w
  217. , cell_h
  218. , pivot
  219. );
  220. int num_v = cells_v;
  221. int num_h = cells_h;
  222. for (int h = 0; h < num_v; ++h)
  223. {
  224. for (int w = 0; w < num_h; ++w)
  225. {
  226. Vector2 cell = sprite_cell_xy(h
  227. , w
  228. , offset_x
  229. , offset_y
  230. , cell_w
  231. , cell_h
  232. , spacing_x
  233. , spacing_y
  234. );
  235. const int x0 = (int)cell.x;
  236. const int y0 = (int)cell.y;
  237. const int x1 = x0+(int)cell_w;
  238. const int y1 = y0+(int)cell_h;
  239. draw_list->AddRect(ImVec2(canvas_pos.x + x0, canvas_pos.y + y0)
  240. , ImVec2(canvas_pos.x + x1, canvas_pos.y + y1)
  241. , ImColor(230, 26, 26, 153)
  242. );
  243. draw_list->AddCircleFilled(ImVec2(x0 + canvas_pos.x + pivot_xy.x, y0 + canvas_pos.y + pivot_xy.y)
  244. , 5.0f
  245. , ImColor(26, 26, 230, 153)
  246. );
  247. }
  248. }
  249. ImGui::InvisibleButton("canvas", canvas_size);
  250. draw_list->PushClipRect(canvas_pos, ImVec2(canvas_pos.x+canvas_size.x, canvas_pos.y+canvas_size.y)); // clip lines within the canvas (if we resize it, etc.)
  251. }
  252. #endif
  253. ImGui::NextColumn();
  254. ImGui::BeginGroup();
  255. ImGui::LabelText("Resolution", "%d x %d", width, height);
  256. ImGui::InputInt("Cells H", &cells_h);
  257. cells_h = clamp(cells_h, 1, 256);
  258. ImGui::InputInt("Cells V", &cells_v);
  259. cells_v = clamp(cells_v, 1, 256);
  260. ImGui::Checkbox("Cell WH auto", &cell_wh_auto);
  261. ImGui::InputInt("Cell W", &cell_w);
  262. cell_w = clamp(cell_w, 1, 4096);
  263. ImGui::InputInt("Cell H", &cell_h);
  264. cell_h = clamp(cell_h, 1, 4096);
  265. ImGui::InputInt("Offset X", &offset_x);
  266. offset_x = clamp(offset_x, 0, 128);
  267. ImGui::InputInt("Offset Y", &offset_y);
  268. offset_y = clamp(offset_y, 0, 128);
  269. ImGui::InputInt("Spacing X", &spacing_x);
  270. spacing_x = clamp(spacing_x, 0, 128);
  271. ImGui::InputInt("Spacing Y", &spacing_y);
  272. spacing_y = clamp(spacing_y, 0, 128);
  273. ImGui::Combo("Pivot", &pivot, pivot_names, Pivot::COUNT);
  274. ImGui::InputInt("Layer", &layer);
  275. layer = clamp(layer, 0, 7);
  276. ImGui::InputInt("Depth", &depth);
  277. depth = clamp(depth, 0, 9999);
  278. ImGui::EndGroup();
  279. }
  280. };
  281. //-----------------------------------------------------------------------------
  282. struct Inspector
  283. {
  284. // Inspector
  285. char _name[1024];
  286. f32 _position[3];
  287. f32 _rotation[3];
  288. f32 _scale[3];
  289. char _sprite[1024];
  290. char _material[1024];
  291. bool _visible;
  292. char _state_machine[1024];
  293. bool _open;
  294. Inspector()
  295. : _visible(true)
  296. , _open(true)
  297. {
  298. memset(_name, 0, sizeof(_name));
  299. memset(_sprite, 0, sizeof(_sprite));
  300. memset(_material, 0, sizeof(_material));
  301. memset(_position, 0, sizeof(_position));
  302. memset(_rotation, 0, sizeof(_rotation));
  303. memset(_scale, 0, sizeof(_scale));
  304. memset(_state_machine, 0, sizeof(_state_machine));
  305. }
  306. void draw()
  307. {
  308. ImGui::Begin("Inspector", &_open);
  309. if (ImGui::TreeNodeEx("Unit", ImGuiTreeNodeFlags_DefaultOpen))
  310. {
  311. ImGui::InputText("Name", _name, sizeof(_name));
  312. ImGui::TreePop();
  313. }
  314. if (ImGui::TreeNodeEx("Transform", ImGuiTreeNodeFlags_DefaultOpen))
  315. {
  316. ImGui::InputFloat3("Position", _position);
  317. ImGui::InputFloat3("Rotation", _rotation);
  318. ImGui::InputFloat3("Scale", _scale);
  319. ImGui::TreePop();
  320. }
  321. if (ImGui::TreeNodeEx("Renderer", ImGuiTreeNodeFlags_DefaultOpen))
  322. {
  323. ImGui::InputText("Sprite", _sprite, sizeof(_sprite));
  324. ImGui::InputText("Material", _material, sizeof(_material));
  325. ImGui::Checkbox("Visible", &_visible);
  326. ImGui::TreePop();
  327. }
  328. if (ImGui::TreeNodeEx("Animation", ImGuiTreeNodeFlags_DefaultOpen))
  329. {
  330. ImGui::InputText("State Machine", _state_machine, sizeof(_state_machine));
  331. ImGui::TreePop();
  332. }
  333. ImGui::End();
  334. }
  335. };
  336. //-----------------------------------------------------------------------------
  337. struct SceneView
  338. {
  339. ImVec2 _origin;
  340. ImVec2 _size;
  341. ImVec2 _cursor;
  342. bool _open;
  343. SceneView()
  344. : _open(true)
  345. {
  346. }
  347. void draw()
  348. {
  349. ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
  350. ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
  351. ImGui::Begin("Scene View"
  352. , &_open
  353. , ImGuiWindowFlags_NoScrollbar
  354. | ImGuiWindowFlags_NoScrollWithMouse
  355. );
  356. _origin = ImGui::GetCursorScreenPos();
  357. uint16_t w, h;
  358. device()->resolution(w, h);
  359. bgfx::TextureHandle txh = device()->_pipeline->_buffers[0];
  360. CE_ENSURE(bgfx::isValid(txh));
  361. ImGui::Image((void*)(uintptr_t)txh.idx
  362. , ImVec2(w, h)
  363. #if CROWN_PLATFORM_WINDOWS
  364. , ImVec2(0, 0)
  365. , ImVec2(1, 1)
  366. #else
  367. , ImVec2(0, 1)
  368. , ImVec2(1, 0)
  369. #endif // CROWN_PLATFORM_WINDOWS
  370. );
  371. ImVec2 mouse_pos_in_view = ImVec2(ImGui::GetIO().MousePos.x - _origin.x
  372. , ImGui::GetIO().MousePos.y - _origin.y
  373. );
  374. if (ImGui::IsWindowHovered()
  375. && mouse_pos_in_view.x > 0
  376. && mouse_pos_in_view.x < w
  377. && mouse_pos_in_view.y > 0
  378. && mouse_pos_in_view.y < h
  379. )
  380. {
  381. // Send all input to engine
  382. ImGui::CaptureMouseFromApp(false);
  383. ImGui::CaptureKeyboardFromApp(false);
  384. }
  385. else
  386. {
  387. // Send all input to imgui
  388. ImGui::CaptureMouseFromApp(true);
  389. ImGui::CaptureKeyboardFromApp(true);
  390. }
  391. _size = ImGui::GetWindowSize();
  392. _size.y -= ImGui::GetFrameHeight();
  393. ImGui::End();
  394. ImGui::PopStyleVar(2);
  395. }
  396. };
  397. //-----------------------------------------------------------------------------
  398. struct SceneTree
  399. {
  400. bool _open;
  401. SceneTree()
  402. : _open(true)
  403. {
  404. }
  405. void draw()
  406. {
  407. ImGui::Begin("Scene Tree", &_open);
  408. if (ImGui::TreeNodeEx("Units", ImGuiTreeNodeFlags_DefaultOpen))
  409. {
  410. if (ImGui::TreeNodeEx("Objects", ImGuiTreeNodeFlags_DefaultOpen))
  411. {
  412. for (int i = 0; i < 5; i++)
  413. if (ImGui::TreeNode((void*)(intptr_t)i, "Child %d", i))
  414. {
  415. ImGui::Text("blah blah");
  416. ImGui::SameLine();
  417. if (ImGui::SmallButton("print")) printf("Child %d pressed", i);
  418. ImGui::TreePop();
  419. }
  420. ImGui::TreePop();
  421. }
  422. if (ImGui::TreeNodeEx("Lights", ImGuiTreeNodeFlags_DefaultOpen))
  423. {
  424. // ShowHelpMarker("This is a more standard looking tree with selectable nodes.\nClick to select, Ctrl+Click to toggle, click on arrows or double-click to open.");
  425. static bool align_label_with_current_x_position = false;
  426. ImGui::Checkbox("Align label with current X position)", &align_label_with_current_x_position);
  427. ImGui::Text("Hello!");
  428. if (align_label_with_current_x_position)
  429. ImGui::Unindent(ImGui::GetTreeNodeToLabelSpacing());
  430. static int selection_mask = (1 << 2); // Dumb representation of what may be user-side selection state. You may carry selection state inside or outside your objects in whatever format you see fit.
  431. int node_clicked = -1; // Temporary storage of what node we have clicked to process selection at the end of the loop. May be a pointer to your own node type, etc.
  432. ImGui::PushStyleVar(ImGuiStyleVar_IndentSpacing, ImGui::GetFontSize()*3); // Increase spacing to differentiate leaves from expanded contents.
  433. for (int i = 0; i < 6; i++)
  434. {
  435. // Disable the default open on single-click behavior and pass in Selected flag according to our selection state.
  436. ImGuiTreeNodeFlags node_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick | ((selection_mask & (1 << i)) ? ImGuiTreeNodeFlags_Selected : 0);
  437. if (i < 3)
  438. {
  439. // Node
  440. bool node_open = ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, "Selectable Node %d", i);
  441. if (ImGui::IsItemClicked())
  442. node_clicked = i;
  443. if (node_open)
  444. {
  445. ImGui::Text("Blah blah\nBlah Blah");
  446. ImGui::TreePop();
  447. }
  448. }
  449. else
  450. {
  451. // Leaf: The only reason we have a TreeNode at all is to allow selection of the leaf. Otherwise we can use BulletText() or TreeAdvanceToLabelPos()+Text().
  452. ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen, "Selectable Leaf %d", i);
  453. if (ImGui::IsItemClicked())
  454. node_clicked = i;
  455. }
  456. }
  457. if (node_clicked != -1)
  458. {
  459. // Update selection state. Process outside of tree loop to avoid visual inconsistencies during the clicking-frame.
  460. if (ImGui::GetIO().KeyCtrl)
  461. selection_mask ^= (1 << node_clicked); // Ctrl+click to toggle
  462. else //if (!(selection_mask & (1 << node_clicked))) // Depending on selection behavior you want, this commented bit preserve selection when clicking on item that is part of the selection
  463. selection_mask = (1 << node_clicked); // Click to single-select
  464. }
  465. ImGui::PopStyleVar();
  466. if (align_label_with_current_x_position)
  467. ImGui::Indent(ImGui::GetTreeNodeToLabelSpacing());
  468. ImGui::TreePop();
  469. }
  470. ImGui::TreePop();
  471. }
  472. ImGui::End();
  473. }
  474. };
  475. //-----------------------------------------------------------------------------
  476. struct SpriteAnimator
  477. {
  478. bool _open;
  479. bool _add_animation_popup_open;
  480. Array<const char*> _entities;
  481. s32 _cur_entity;
  482. TextureResource* _texture;
  483. u32 _texture_width;
  484. u32 _texture_height;
  485. struct Frame
  486. {
  487. char name[512];
  488. ImVec2 pivot;
  489. ImVec4 region;
  490. };
  491. Array<Frame> _frames;
  492. char _anim_name[512];
  493. f32 _anim_time;
  494. Array<const char*> _listbox_items;
  495. s32 _listbox_item_current;
  496. Array<Frame> _anim_preview_frames;
  497. f32 _delta;
  498. u32 current;
  499. Vector<DynamicString> file_list_sprites;
  500. FilesystemDisk* _fs;
  501. SpriteAnimator(const DynamicString& src_dir)
  502. : _open(false)
  503. , _add_animation_popup_open(false)
  504. , _entities(default_allocator())
  505. , _cur_entity(0)
  506. , _texture(nullptr)
  507. , _texture_width(0)
  508. , _texture_height(0)
  509. , _frames(default_allocator())
  510. , _anim_time(0.1f)
  511. , _listbox_items(default_allocator())
  512. , _listbox_item_current(0)
  513. , _anim_preview_frames(default_allocator())
  514. , _delta(0.0f)
  515. , current(0)
  516. , file_list_sprites(default_allocator())
  517. {
  518. memset(_anim_name, 0, sizeof(_anim_name));
  519. _fs = CE_NEW(default_allocator(), FilesystemDisk)(default_allocator());
  520. _fs->set_prefix(src_dir.c_str());
  521. get_sprites_list();
  522. }
  523. ~SpriteAnimator()
  524. {
  525. CE_DELETE(default_allocator(), _fs);
  526. }
  527. ImVec2 pixel_to_uv(u32 tex_w, u32 tex_h, f32 x, f32 y)
  528. {
  529. ImVec2 uv;
  530. uv.x = (f32)x / (f32)tex_w;
  531. uv.y = (f32)y / (f32)tex_h;
  532. return uv;
  533. }
  534. void get_directory(DynamicString& dir, const char* path)
  535. {
  536. CE_ENSURE(NULL != path);
  537. const char* ls = strrchr(path, '/');
  538. u32 file_len = strlen32(ls+1);
  539. u32 dir_len = strlen32(path) - file_len;
  540. char buff[1024];
  541. memcpy(buff, path, dir_len);
  542. buff[dir_len] = '\0';
  543. path::reduce(dir, buff);
  544. }
  545. void save_sprite_animation()
  546. {
  547. TempAllocator4096 ta;
  548. StringStream ss(ta);
  549. ss << "frames = [ ";
  550. for (u32 i = 0; i < array::size(_listbox_items); i++)
  551. {
  552. ss << _listbox_items[i] + 7 << " ";
  553. }
  554. ss << "]\n";
  555. ss << "total_time = ";
  556. ss << _anim_time;
  557. DynamicString dir(ta);
  558. get_directory(dir, _entities[_cur_entity]);
  559. DynamicString file_name(ta);
  560. path::join(file_name, dir.c_str(), _anim_name);
  561. file_name += ".sprite_animation";
  562. File* f = _fs->open(file_name.c_str(), FileOpenMode::WRITE);
  563. f->write(string_stream::c_str(ss), strlen32(string_stream::c_str(ss)));
  564. f->close();
  565. }
  566. void get_files_list(Vector<DynamicString>& out, const char* path="")
  567. {
  568. TempAllocator4096 ta;
  569. Vector<DynamicString> files(ta);
  570. _fs->list_files(path, files);
  571. for (u32 i = 0; i < vector::size(files); i++)
  572. {
  573. DynamicString join(ta);
  574. path::join(join, path, files[i].c_str());
  575. join = join.c_str()[0] == '/' ? join.c_str()+1 : join.c_str();
  576. if (_fs->is_directory(join.c_str()))
  577. {
  578. get_files_list(out, join.c_str());
  579. }
  580. else
  581. {
  582. vector::push_back(out, join);
  583. }
  584. }
  585. }
  586. void get_sprites_list()
  587. {
  588. TempAllocator4096 ta;
  589. Vector<DynamicString> files(ta);
  590. get_files_list(files);
  591. for (DynamicString* f = vector::begin(files); f != vector::end(files); f++)
  592. if (f->has_suffix(".sprite"))
  593. array::push_back(_entities, (const char*) strdup(f->c_str()));
  594. }
  595. void draw()
  596. {
  597. ImGui::Begin("Animator", &_open);
  598. if (_texture)
  599. {
  600. Frame f = _frames[0];
  601. ImVec2 start = pixel_to_uv(_texture_width, _texture_height, f.region.x, f.region.y);
  602. ImVec2 end = pixel_to_uv(_texture_width, _texture_height, f.region.x+f.region.z, f.region.y+f.region.w);
  603. ImGui::Image((void*)(uintptr_t)_texture->handle.idx
  604. , ImVec2(f.region.z, f.region.w)
  605. , start
  606. , end
  607. , ImColor(255, 255, 255, 55)
  608. );
  609. }
  610. if (ImGui::Combo("Entities", &_cur_entity, (const char* const*) array::begin(_entities), array::size(_entities)))
  611. {
  612. array::clear(_frames);
  613. const char* sprite = _entities[_cur_entity];
  614. u32 sprite_len = strlen32(sprite);
  615. char entity[1024];
  616. strncpy(entity, sprite, sizeof(entity)-1);
  617. entity[sprite_len-7] = '\0'; // remove ".sprite"
  618. ResourceManager* resman = device()->_resource_manager;
  619. _texture = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64(entity));
  620. File* file = _fs->open(sprite, FileOpenMode::READ);
  621. const u32 size = file->size();
  622. Buffer buf(default_allocator());
  623. array::resize(buf, size);
  624. file->read(array::begin(buf), size);
  625. _fs->close(*file);
  626. JsonObject obj(default_allocator());
  627. JsonArray list(default_allocator());
  628. sjson::parse(buf, obj);
  629. sjson::parse_array(obj["frames"], list);
  630. _texture_width = sjson::parse_int(obj["width"]);
  631. _texture_height = sjson::parse_int(obj["height"]);
  632. for (u32 i = 0; i < array::size(list); i++)
  633. {
  634. JsonObject frame(default_allocator());
  635. DynamicString name(default_allocator());
  636. JsonArray pivot(default_allocator());
  637. JsonArray region(default_allocator());
  638. sjson::parse_object(list[i], frame);
  639. sjson::parse_array(frame["pivot"], pivot);
  640. sjson::parse_array(frame["region"], region);
  641. Frame f;
  642. sjson::parse_string(frame["name"], name);
  643. strncpy(f.name, name.c_str(), name.length());
  644. f.name[name.length()] = '\0';
  645. f.pivot.x = sjson::parse_float(pivot[0]);
  646. f.pivot.y = sjson::parse_float(pivot[1]);
  647. f.region.x = sjson::parse_float(region[0]);
  648. f.region.y = sjson::parse_float(region[1]);
  649. f.region.z = sjson::parse_float(region[2]);
  650. f.region.w = sjson::parse_float(region[3]);
  651. array::push_back(_frames, f);
  652. }
  653. }
  654. if (ImGui::Button("Add animation", ImVec2(100, 25)))
  655. {
  656. _add_animation_popup_open = true;
  657. }
  658. if (_add_animation_popup_open)
  659. {
  660. ImGui::OpenPopup("Add animation");
  661. }
  662. if (ImGui::BeginPopup("Add animation"))
  663. {
  664. ImGui::InputText("Name", _anim_name, sizeof(_anim_name));
  665. ImGui::InputFloat("Time", &_anim_time, 0.1f, 0.1f);
  666. ImGui::ListBox("Animation Frames", &_listbox_item_current, (const char* const*)array::begin(_listbox_items), array::size(_listbox_items));
  667. if (ImGui::Button("Clear Frames", ImVec2(100.0f, 25.0f)))
  668. {
  669. array::clear(_listbox_items);
  670. array::clear(_anim_preview_frames);
  671. _delta = 0.0f;
  672. current = 0;
  673. }
  674. if (array::size(_anim_preview_frames) > 0)
  675. {
  676. _delta += 1.0f/60.0f;
  677. if (_delta >= _anim_time/array::size(_anim_preview_frames))
  678. {
  679. _delta = 0;
  680. current++;
  681. if (current >= array::size(_anim_preview_frames))
  682. current = 0;
  683. }
  684. Frame f = _anim_preview_frames[current];
  685. ImVec2 start = pixel_to_uv(_texture_width, _texture_height, f.region.x, f.region.y);
  686. ImVec2 end = pixel_to_uv(_texture_width, _texture_height, f.region.x+f.region.z, f.region.y+f.region.w);
  687. ImGui::Image(
  688. (void*)(uintptr_t)_texture->handle.idx
  689. , ImVec2(f.region.z, f.region.w)
  690. , start
  691. , end
  692. , ImColor(255, 255, 255, 55)
  693. );
  694. }
  695. ImGui::Separator();
  696. for (u32 i = 0; i < array::size(_frames); i++)
  697. {
  698. Frame f = _frames[i];
  699. ImVec2 start = pixel_to_uv(_texture_width, _texture_height, f.region.x, f.region.y);
  700. ImVec2 end = pixel_to_uv(_texture_width, _texture_height, f.region.x+f.region.z, f.region.y+f.region.w);
  701. ImGui::SameLine();
  702. if (i % 9 == 0) ImGui::NewLine();
  703. ImGui::BeginGroup();
  704. ImGui::Image(
  705. (void*)(uintptr_t)_texture->handle.idx
  706. , ImVec2(f.region.z, f.region.w)
  707. , start
  708. , end
  709. , ImColor(255, 255, 255, 55)
  710. );
  711. ImGui::NewLine();
  712. if (ImGui::Button(_frames[i].name, ImVec2(100.0f, 25.0f)))
  713. {
  714. array::push_back(_listbox_items, (const char*) strdup(_frames[i].name));
  715. array::push_back(_anim_preview_frames, _frames[i]);
  716. }
  717. ImGui::EndGroup();
  718. }
  719. if (ImGui::Button("Save", ImVec2(100, 25)))
  720. {
  721. save_sprite_animation();
  722. ImGui::CloseCurrentPopup();
  723. _add_animation_popup_open = false;
  724. }
  725. ImGui::SameLine();
  726. if (ImGui::Button("Cancel", ImVec2(100, 25)))
  727. {
  728. ImGui::CloseCurrentPopup();
  729. _add_animation_popup_open = false;
  730. }
  731. ImGui::EndPopup();
  732. }
  733. ImGui::End();
  734. }
  735. };
  736. //-----------------------------------------------------------------------------
  737. struct LevelEditor
  738. {
  739. DynamicString _source_dir;
  740. // FX
  741. TextureResource* tool_move_texture;
  742. TextureResource* tool_place_texture;
  743. TextureResource* tool_rotate_texture;
  744. TextureResource* tool_scale_texture;
  745. TextureResource* reference_world_texture;
  746. TextureResource* reference_local_texture;
  747. TextureResource* axis_local_texture;
  748. TextureResource* axis_world_texture;
  749. TextureResource* snap_to_grid_texture;
  750. // State
  751. f32 _grid_size;
  752. f32 _rotation_snap;
  753. bool _show_grid;
  754. bool _snap_to_grid;
  755. bool _debug_render_world;
  756. bool _debug_physics_world;
  757. tool::ToolType::Enum _tool_type;
  758. tool::SnapMode::Enum _snap_mode;
  759. tool::ReferenceSystem::Enum _reference_system;
  760. ImVec2 _main_menu_pos;
  761. ImVec2 _main_menu_size;
  762. Console _console;
  763. Inspector _inspector;
  764. SceneView _scene_view;
  765. SceneTree _scene_tree;
  766. SpriteAnimator _animator;
  767. SpriteImporter _sprite_importer;
  768. LevelEditor(const DynamicString& source_dir)
  769. : _source_dir(default_allocator())
  770. , _grid_size(1.0f)
  771. , _rotation_snap(15)
  772. , _show_grid(true)
  773. , _snap_to_grid(true)
  774. , _debug_render_world(false)
  775. , _debug_physics_world(false)
  776. , _tool_type(tool::ToolType::MOVE)
  777. , _snap_mode(tool::SnapMode::RELATIVE)
  778. , _reference_system(tool::ReferenceSystem::LOCAL)
  779. , _main_menu_pos(0, 0)
  780. , _main_menu_size(0, 0)
  781. , _animator(source_dir)
  782. {
  783. ResourceManager* resman = device()->_resource_manager;
  784. tool_move_texture = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/tool-move"));
  785. tool_place_texture = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/tool-place"));
  786. tool_rotate_texture = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/tool-rotate"));
  787. tool_scale_texture = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/tool-scale"));
  788. reference_world_texture = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/reference-world"));
  789. reference_local_texture = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/reference-local"));
  790. axis_local_texture = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/axis-local"));
  791. axis_world_texture = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/axis-world"));
  792. snap_to_grid_texture = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/snap-to-grid"));
  793. imgui_create();
  794. }
  795. ~LevelEditor()
  796. {
  797. imgui_destroy();
  798. }
  799. void update(f32 dt)
  800. {
  801. CE_UNUSED(dt);
  802. static f32 last_w = 0.0f;
  803. static f32 last_h = 0.0f;
  804. if (last_w != _scene_view._size.x || last_h != _scene_view._size.y)
  805. {
  806. last_w = _scene_view._size.x;
  807. last_h = _scene_view._size.y;
  808. device()->_width = u16(_scene_view._size.x != 0.0f ? _scene_view._size.x : 128.0f);
  809. device()->_height = u16(_scene_view._size.y != 0.0f ? _scene_view._size.y : 128.0f);
  810. }
  811. u32 message_count = 0;
  812. // Receive response from engine
  813. for (;;)
  814. {
  815. u32 msg_len = 0;
  816. ReadResult rr = _client.read_nonblock(&msg_len, sizeof(msg_len));
  817. if (rr.error == ReadResult::WOULDBLOCK)
  818. break;
  819. if (ReadResult::SUCCESS == rr.error)
  820. {
  821. char msg[8192];
  822. rr = _client.read(msg, msg_len);
  823. msg[msg_len] = '\0';
  824. message_count++;
  825. // logi(LEVEL_EDITOR, "count: %d", message_count);
  826. if (ReadResult::SUCCESS == rr.error)
  827. {
  828. TempAllocator4096 ta;
  829. JsonObject obj(ta);
  830. DynamicString type(ta);
  831. json::parse(msg, obj);
  832. json::parse_string(obj["type"], type);
  833. if (type == "message")
  834. {
  835. DynamicString severity(ta);
  836. DynamicString message(ta);
  837. json::parse_string(obj["severity"], severity);
  838. json::parse_string(obj["message"], message);
  839. LogSeverity::Enum ls = LogSeverity::COUNT;
  840. if (strcmp("info", severity.c_str()) == 0)
  841. ls = LogSeverity::LOG_INFO;
  842. else if (strcmp("warning", severity.c_str()) == 0)
  843. ls = LogSeverity::LOG_WARN;
  844. else if (strcmp("error", severity.c_str()) == 0)
  845. ls = LogSeverity::LOG_ERROR;
  846. else
  847. CE_FATAL("Unknown severity");
  848. _console.add_log(ls, message.c_str());
  849. }
  850. else if (type == "error")
  851. {
  852. DynamicString message(ta);
  853. json::parse_string(obj["message"], message);
  854. _console.add_log(LogSeverity::LOG_ERROR, message.c_str());
  855. }
  856. else
  857. {
  858. _console.add_log(LogSeverity::LOG_ERROR, "Unknown message type");
  859. }
  860. console_scroll_to_bottom(_console);
  861. }
  862. }
  863. }
  864. imgui_begin_frame(VIEW_IMGUI, _width, _height);
  865. ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_None;
  866. // We are using the ImGuiWindowFlags_NoDocking flag to make the parent window not dockable into,
  867. // because it would be confusing to have two docking targets within each others.
  868. ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;
  869. ImGuiViewport* viewport = ImGui::GetMainViewport();
  870. ImGui::SetNextWindowPos(viewport->Pos);
  871. ImGui::SetNextWindowSize(viewport->Size);
  872. ImGui::SetNextWindowViewport(viewport->ID);
  873. ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
  874. ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
  875. window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
  876. window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
  877. // When using ImGuiDockNodeFlags_PassthruCentralNode, DockSpace() will render our background and handle the pass-thru hole, so we ask Begin() to not render a background.
  878. if (dockspace_flags & ImGuiDockNodeFlags_PassthruCentralNode)
  879. window_flags |= ImGuiWindowFlags_NoBackground;
  880. ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
  881. ImGui::Begin("DockSpace Demo", NULL, window_flags);
  882. ImGui::PopStyleVar(3);
  883. // DockSpace
  884. ImGuiID dockspace_id = ImGui::GetID("MyDockSpace");
  885. ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
  886. main_menu_bar();
  887. ImGui::End();
  888. if (_scene_view._open)
  889. {
  890. _scene_view.draw();
  891. // Draw toolbar overlay
  892. ImVec2 window_pos;
  893. window_pos.x = _scene_view._origin.x + 6.0f;
  894. window_pos.y = _scene_view._origin.y + 6.0f;
  895. ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always);
  896. ImGui::SetNextWindowBgAlpha(0.35f);
  897. if (ImGui::Begin("Toobar Overlay"
  898. , NULL
  899. , ImGuiWindowFlags_NoMove
  900. | ImGuiWindowFlags_NoDecoration
  901. | ImGuiWindowFlags_AlwaysAutoResize
  902. | ImGuiWindowFlags_NoSavedSettings
  903. | ImGuiWindowFlags_NoFocusOnAppearing
  904. | ImGuiWindowFlags_NoNav
  905. ))
  906. {
  907. ImGui::BeginGroup();
  908. if (ImGui::ImageButton(tool_place_texture->handle, ImVec2(16, 16)))
  909. {
  910. _tool_type = tool::ToolType::PLACE;
  911. tool_send_state();
  912. }
  913. if (ImGui::ImageButton(tool_move_texture->handle, ImVec2(16, 16)))
  914. {
  915. _tool_type = tool::ToolType::MOVE;
  916. tool_send_state();
  917. }
  918. if (ImGui::ImageButton(tool_rotate_texture->handle, ImVec2(16, 16)))
  919. {
  920. _tool_type = tool::ToolType::ROTATE;
  921. tool_send_state();
  922. }
  923. if (ImGui::ImageButton(tool_scale_texture->handle, ImVec2(16, 16)))
  924. {
  925. _tool_type = tool::ToolType::SCALE;
  926. tool_send_state();
  927. }
  928. ImGui::Separator();
  929. if (ImGui::ImageButton(axis_local_texture->handle, ImVec2(16, 16)))
  930. {
  931. _reference_system = tool::ReferenceSystem::LOCAL;
  932. tool_send_state();
  933. }
  934. if (ImGui::ImageButton(axis_world_texture->handle, ImVec2(16, 16)))
  935. {
  936. _reference_system = tool::ReferenceSystem::WORLD;
  937. tool_send_state();
  938. }
  939. ImGui::Separator();
  940. if (ImGui::ImageButton(reference_world_texture->handle, ImVec2(16, 16)))
  941. {
  942. _snap_mode = tool::SnapMode::RELATIVE;
  943. tool_send_state();
  944. }
  945. if (ImGui::ImageButton(reference_local_texture->handle, ImVec2(16, 16)))
  946. {
  947. _snap_mode = tool::SnapMode::ABSOLUTE;
  948. tool_send_state();
  949. }
  950. ImGui::Separator();
  951. if (ImGui::ImageButton(snap_to_grid_texture->handle, ImVec2(16, 16)))
  952. {
  953. _snap_to_grid = !_snap_to_grid;
  954. tool_send_state();
  955. }
  956. ImGui::EndGroup();
  957. }
  958. ImGui::End();
  959. }
  960. if (_scene_tree._open)
  961. _scene_tree.draw();
  962. if (_inspector._open)
  963. _inspector.draw();
  964. if (_console._open)
  965. console_draw(_console, _client);
  966. if (_animator._open)
  967. _animator.draw();
  968. // _sprite_importer.draw();
  969. imgui_end_frame();
  970. }
  971. void send_command(StringStream& ss)
  972. {
  973. console_send_script(_client, string_stream::c_str(ss));
  974. }
  975. void tool_send_state()
  976. {
  977. TempAllocator256 ta;
  978. StringStream ss(ta);
  979. tool::set_grid_size(ss, _grid_size);
  980. tool::set_rotation_snap(ss, _rotation_snap);
  981. tool::enable_show_grid(ss, _show_grid);
  982. tool::enable_snap_to_grid(ss, _snap_to_grid);
  983. tool::enable_debug_render_world(ss, _debug_render_world);
  984. tool::enable_debug_physics_world(ss, _debug_physics_world);
  985. tool::set_tool_type(ss, _tool_type);
  986. tool::set_snap_mode(ss, _snap_mode);
  987. tool::set_reference_system(ss, _reference_system);
  988. send_command(ss);
  989. }
  990. void format_time(uint64_t time, char* str, size_t size)
  991. {
  992. #if CROWN_PLATFORM_WINDOWS
  993. // SYSTEMTIME st;
  994. // FileTimeToSystemTime(&time, &st);
  995. // sprintf(str, "%d/%d/%d %d:%d"
  996. // , st.wDay
  997. // , st.wMonth
  998. // , st.wYear
  999. // , st.wHour
  1000. // , st.wMinute);
  1001. #else
  1002. struct tm lt;
  1003. localtime_r((time_t*)&time, &lt);
  1004. strftime(str, size, "%d/%m/%Y %H:%M", &lt);
  1005. #endif
  1006. }
  1007. void main_menu_bar()
  1008. {
  1009. // Main Menu
  1010. if (ImGui::BeginMainMenuBar())
  1011. {
  1012. if (ImGui::BeginMenu("File"))
  1013. {
  1014. if (ImGui::MenuItem("New"))
  1015. {
  1016. }
  1017. if (ImGui::MenuItem("Open", "Ctrl+O"))
  1018. {
  1019. nfdchar_t *out_path = NULL;
  1020. nfdresult_t result = NFD_OpenDialog(NULL, NULL, &out_path);
  1021. if ( result == NFD_OKAY )
  1022. {
  1023. logi(LEVEL_EDITOR, "Success!");
  1024. logi(LEVEL_EDITOR, out_path);
  1025. free(out_path);
  1026. }
  1027. else if ( result == NFD_CANCEL )
  1028. {
  1029. logi(LEVEL_EDITOR, "User pressed cancel.");
  1030. }
  1031. else
  1032. {
  1033. loge(LEVEL_EDITOR, "Error: %s\n", NFD_GetError());
  1034. }
  1035. }
  1036. if (ImGui::MenuItem("Save", "Ctrl+S"))
  1037. {
  1038. }
  1039. if (ImGui::MenuItem("Save As..."))
  1040. {
  1041. }
  1042. ImGui::Separator();
  1043. if (ImGui::MenuItem("Quit", "Ctrl+Q", false, true))
  1044. {
  1045. TempAllocator64 ta;
  1046. StringStream ss(ta);
  1047. tool::device_quit(ss);
  1048. send_command(ss);
  1049. }
  1050. ImGui::EndMenu();
  1051. }
  1052. if (ImGui::BeginMenu("Edit"))
  1053. {
  1054. if (ImGui::MenuItem("Undo", "Ctrl+Z")) {}
  1055. if (ImGui::MenuItem("Redo", "Ctrl+Y")) {}
  1056. ImGui::EndMenu();
  1057. }
  1058. if (ImGui::BeginMenu("Create"))
  1059. {
  1060. TempAllocator256 ta;
  1061. StringStream ss(ta);
  1062. if (ImGui::BeginMenu("Primitives"))
  1063. {
  1064. if (ImGui::MenuItem("Cube", NULL, false, true))
  1065. {
  1066. _tool_type = tool::ToolType::PLACE;
  1067. tool_send_state();
  1068. tool::set_placeable(ss, "unit", "core/units/primitives/cube");
  1069. send_command(ss);
  1070. }
  1071. if (ImGui::MenuItem("Sphere"))
  1072. {
  1073. _tool_type = tool::ToolType::PLACE;
  1074. tool_send_state();
  1075. tool::set_placeable(ss, "unit", "core/units/primitives/sphere");
  1076. send_command(ss);
  1077. }
  1078. if (ImGui::MenuItem("Cone"))
  1079. {
  1080. _tool_type = tool::ToolType::PLACE;
  1081. tool_send_state();
  1082. tool::set_placeable(ss, "unit", "core/units/primitives/cone");
  1083. send_command(ss);
  1084. }
  1085. if (ImGui::MenuItem("Cylinder"))
  1086. {
  1087. _tool_type = tool::ToolType::PLACE;
  1088. tool_send_state();
  1089. tool::set_placeable(ss, "unit", "core/units/primitives/cylinder");
  1090. send_command(ss);
  1091. }
  1092. if (ImGui::MenuItem("Plane"))
  1093. {
  1094. _tool_type = tool::ToolType::PLACE;
  1095. tool_send_state();
  1096. tool::set_placeable(ss, "unit", "core/units/primitives/plane");
  1097. send_command(ss);
  1098. }
  1099. ImGui::EndMenu();
  1100. }
  1101. if (ImGui::MenuItem("Camera"))
  1102. {
  1103. _tool_type = tool::ToolType::PLACE;
  1104. tool_send_state();
  1105. tool::set_placeable(ss, "unit", "core/units/camera");
  1106. send_command(ss);
  1107. }
  1108. if (ImGui::MenuItem("Light"))
  1109. {
  1110. _tool_type = tool::ToolType::PLACE;
  1111. tool_send_state();
  1112. tool::set_placeable(ss, "unit", "core/units/light");
  1113. send_command(ss);
  1114. }
  1115. if (ImGui::MenuItem("Sound"))
  1116. {
  1117. _tool_type = tool::ToolType::PLACE;
  1118. tool_send_state();
  1119. tool::set_placeable(ss, "sound", "core/units/camera");
  1120. send_command(ss);
  1121. }
  1122. ImGui::EndMenu();
  1123. }
  1124. if (ImGui::BeginMenu("Scene View"))
  1125. {
  1126. TempAllocator256 ta;
  1127. StringStream ss(ta);
  1128. if (ImGui::BeginMenu("Camera View"))
  1129. {
  1130. if (ImGui::MenuItem("Perspective"))
  1131. {
  1132. tool::camera_view_perspective(ss);
  1133. send_command(ss);
  1134. }
  1135. if (ImGui::MenuItem("Front"))
  1136. {
  1137. tool::camera_view_front(ss);
  1138. send_command(ss);
  1139. }
  1140. if (ImGui::MenuItem("Back"))
  1141. {
  1142. tool::camera_view_back(ss);
  1143. send_command(ss);
  1144. }
  1145. if (ImGui::MenuItem("Right"))
  1146. {
  1147. tool::camera_view_right(ss);
  1148. send_command(ss);
  1149. }
  1150. if (ImGui::MenuItem("Left"))
  1151. {
  1152. tool::camera_view_left(ss);
  1153. send_command(ss);
  1154. }
  1155. if (ImGui::MenuItem("Top"))
  1156. {
  1157. tool::camera_view_top(ss);
  1158. send_command(ss);
  1159. }
  1160. if (ImGui::MenuItem("Bottom"))
  1161. {
  1162. tool::camera_view_bottom(ss);
  1163. send_command(ss);
  1164. }
  1165. ImGui::EndMenu();
  1166. }
  1167. ImGui::Separator();
  1168. if (ImGui::MenuItem("Show Grid", NULL, _show_grid))
  1169. {
  1170. _show_grid = !_show_grid;
  1171. tool_send_state();
  1172. }
  1173. if (ImGui::SliderFloat("Grid Size", &_grid_size, 0.1f, 5.0f))
  1174. {
  1175. tool_send_state();
  1176. }
  1177. if (ImGui::SliderFloat("Snap Rot", &_rotation_snap, 1, 180))
  1178. {
  1179. tool_send_state();
  1180. }
  1181. ImGui::EndMenu();
  1182. }
  1183. if (ImGui::BeginMenu("Windows"))
  1184. {
  1185. if (ImGui::MenuItem("Scene"))
  1186. {
  1187. _scene_view._open = true;
  1188. }
  1189. if (ImGui::MenuItem("Scene Tree"))
  1190. {
  1191. _scene_tree._open = true;
  1192. }
  1193. if (ImGui::MenuItem("Inspector"))
  1194. {
  1195. _inspector._open = true;
  1196. }
  1197. if (ImGui::MenuItem("Console"))
  1198. {
  1199. _console._open = true;
  1200. }
  1201. if (ImGui::MenuItem("Animator"))
  1202. {
  1203. _animator._open = true;
  1204. }
  1205. ImGui::EndMenu();
  1206. }
  1207. if (ImGui::BeginMenu("Test"))
  1208. {
  1209. if (ImGui::MenuItem("Test Level"))
  1210. start_game(StartGame::TEST, _project->_data_dir.c_str());
  1211. if (ImGui::MenuItem("Test Game"))
  1212. start_game(StartGame::NORMAL, _project->_data_dir.c_str());
  1213. if (ImGui::MenuItem("Stop"))
  1214. stop_game();
  1215. ImGui::EndMenu();
  1216. }
  1217. if (ImGui::BeginMenu("Help"))
  1218. {
  1219. ImGui::MenuItem("About", "");
  1220. ImGui::EndMenu();
  1221. }
  1222. _main_menu_pos = ImGui::GetWindowPos();
  1223. _main_menu_size = ImGui::GetWindowSize();
  1224. ImGui::EndMainMenuBar();
  1225. }
  1226. }
  1227. };
  1228. void tool_init()
  1229. {
  1230. const DeviceOptions& opt = device()->_device_options;
  1231. _project = CE_NEW(default_allocator(), Project)(default_allocator());
  1232. _project->_data_dir = opt._data_dir;
  1233. _editor = CE_NEW(default_allocator(), LevelEditor)(opt._source_dir);
  1234. _client.connect(IP_ADDRESS_LOOPBACK, CROWN_DEFAULT_CONSOLE_PORT);
  1235. }
  1236. void tool_update(f32 dt)
  1237. {
  1238. _editor->update(dt);
  1239. }
  1240. void tool_shutdown()
  1241. {
  1242. _client.close();
  1243. CE_DELETE(default_allocator(), _editor);
  1244. CE_DELETE(default_allocator(), _project);
  1245. stop_game();
  1246. }
  1247. extern bool next_event(OsEvent& ev);
  1248. bool tool_process_events()
  1249. {
  1250. ImGuiIO& io = ImGui::GetIO();
  1251. bool exit = false;
  1252. bool reset = false;
  1253. TempAllocator4096 ta;
  1254. StringStream ss(ta);
  1255. OsEvent event;
  1256. while (next_event(event))
  1257. {
  1258. if (event.type == OsEventType::NONE)
  1259. continue;
  1260. switch (event.type)
  1261. {
  1262. case OsEventType::BUTTON:
  1263. switch (event.button.device_id)
  1264. {
  1265. case crown::InputDeviceType::KEYBOARD:
  1266. io.KeysDown[event.button.button_num] = event.button.pressed;
  1267. io.KeyCtrl = io.KeysDown[crown::KeyboardButton::CTRL_LEFT] || io.KeysDown[crown::KeyboardButton::CTRL_RIGHT];
  1268. io.KeyShift = io.KeysDown[crown::KeyboardButton::SHIFT_LEFT] || io.KeysDown[crown::KeyboardButton::SHIFT_RIGHT];
  1269. io.KeyAlt = io.KeysDown[crown::KeyboardButton::ALT_LEFT] || io.KeysDown[crown::KeyboardButton::ALT_RIGHT];
  1270. io.KeySuper = io.KeysDown[crown::KeyboardButton::SUPER_LEFT] || io.KeysDown[crown::KeyboardButton::SUPER_RIGHT];
  1271. if (!io.WantCaptureKeyboard)
  1272. {
  1273. if (event.button.pressed)
  1274. {
  1275. if (event.button.button_num == crown::KeyboardButton::W)
  1276. tool::key_down(ss, "w");
  1277. if (event.button.button_num == crown::KeyboardButton::A)
  1278. tool::key_down(ss, "a");
  1279. if (event.button.button_num == crown::KeyboardButton::S)
  1280. tool::key_down(ss, "s");
  1281. if (event.button.button_num == crown::KeyboardButton::D)
  1282. tool::key_down(ss, "d");
  1283. if (event.button.button_num == crown::KeyboardButton::CTRL_LEFT)
  1284. tool::key_down(ss, "ctrl_left");
  1285. if (event.button.button_num == crown::KeyboardButton::SHIFT_LEFT)
  1286. tool::key_down(ss, "shift_left");
  1287. if (event.button.button_num == crown::KeyboardButton::ALT_LEFT)
  1288. tool::key_down(ss, "alt_left");
  1289. }
  1290. else
  1291. {
  1292. if (event.button.button_num == crown::KeyboardButton::W)
  1293. tool::key_up(ss, "w");
  1294. if (event.button.button_num == crown::KeyboardButton::A)
  1295. tool::key_up(ss, "a");
  1296. if (event.button.button_num == crown::KeyboardButton::S)
  1297. tool::key_up(ss, "s");
  1298. if (event.button.button_num == crown::KeyboardButton::D)
  1299. tool::key_up(ss, "d");
  1300. if (event.button.button_num == crown::KeyboardButton::CTRL_LEFT)
  1301. tool::key_up(ss, "ctrl_left");
  1302. if (event.button.button_num == crown::KeyboardButton::SHIFT_LEFT)
  1303. tool::key_up(ss, "shift_left");
  1304. if (event.button.button_num == crown::KeyboardButton::ALT_LEFT)
  1305. tool::key_up(ss, "alt_left");
  1306. }
  1307. }
  1308. break;
  1309. case crown::InputDeviceType::MOUSE:
  1310. io.MouseDown[event.button.button_num] = event.button.pressed;
  1311. if (!io.WantCaptureMouse)
  1312. {
  1313. ImVec2& cursor = _editor->_scene_view._cursor;
  1314. cursor.x = io.MousePos.x - _editor->_scene_view._origin.x;
  1315. cursor.y = io.MousePos.y - _editor->_scene_view._origin.y;
  1316. tool::set_mouse_state(ss
  1317. , cursor.x
  1318. , cursor.y
  1319. , io.MouseDown[MouseButton::LEFT]
  1320. , io.MouseDown[MouseButton::MIDDLE]
  1321. , io.MouseDown[MouseButton::RIGHT]
  1322. );
  1323. if (event.button.button_num == crown::MouseButton::LEFT)
  1324. {
  1325. if (event.button.pressed)
  1326. tool::mouse_down(ss, cursor.x, cursor.y);
  1327. else
  1328. tool::mouse_up(ss, cursor.x, cursor.y);
  1329. }
  1330. }
  1331. break;
  1332. }
  1333. break;
  1334. case OsEventType::AXIS:
  1335. switch(event.axis.device_id)
  1336. {
  1337. case InputDeviceType::MOUSE:
  1338. switch(event.axis.axis_num)
  1339. {
  1340. case crown::MouseAxis::CURSOR:
  1341. io.MousePos = ImVec2(event.axis.axis_x, event.axis.axis_y);
  1342. if (!io.WantCaptureMouse)
  1343. {
  1344. ImVec2& cursor = _editor->_scene_view._cursor;
  1345. cursor.x = io.MousePos.x - _editor->_scene_view._origin.x;
  1346. cursor.y = io.MousePos.y - _editor->_scene_view._origin.y;
  1347. tool::mouse_move(ss, cursor.x, cursor.y);
  1348. }
  1349. break;
  1350. case crown::MouseAxis::WHEEL:
  1351. io.MouseWheel += event.axis.axis_y;
  1352. if (!io.WantCaptureMouse)
  1353. tool::mouse_wheel(ss, io.MouseWheel);
  1354. break;
  1355. }
  1356. }
  1357. break;
  1358. case OsEventType::TEXT:
  1359. io.AddInputCharactersUTF8((const char*) event.text.utf8);
  1360. break;
  1361. case OsEventType::RESOLUTION:
  1362. _width = event.resolution.width;
  1363. _height = event.resolution.height;
  1364. reset = true;
  1365. break;
  1366. case OsEventType::EXIT:
  1367. exit = true;
  1368. break;
  1369. default:
  1370. break;
  1371. }
  1372. }
  1373. _editor->send_command(ss);
  1374. bool vsync = true;
  1375. if (reset)
  1376. bgfx::reset(_width, _height, (vsync ? BGFX_RESET_VSYNC : BGFX_RESET_NONE));
  1377. return exit;
  1378. }
  1379. } // namespace crown
  1380. #endif // CROWN_TOOLS