level_editor.cpp 38 KB

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