level_editor.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. /*
  2. * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #if CROWN_TOOLS
  6. #include <imgui.h>
  7. #include <iconfontheaders/icons_material_design.h>
  8. #include <time.h>
  9. #include "core/containers/vector.h"
  10. #include "core/filesystem/filesystem_disk.h"
  11. #include "core/filesystem/path.h"
  12. #include "core/filesystem/file.h"
  13. #include "core/json/json.h"
  14. #include "core/json/sjson.h"
  15. #include "core/json/json_object.h"
  16. #include "core/network/socket.h"
  17. #include "core/strings/dynamic_string.h"
  18. #include "device/device.h"
  19. #include "device/device_event_queue.h"
  20. #include "device/input_device.h"
  21. #include "device/input_manager.h"
  22. #include "device/input_types.h"
  23. #include "device/device_options.h"
  24. #include "device/log.h"
  25. #include "resource/resource_manager.h"
  26. #include "resource/texture_resource.h"
  27. #include "device/pipeline.h"
  28. #include "imgui_context.h"
  29. #include "tool_api.h"
  30. #include <sys/time.h>
  31. namespace { const crown::log_internal::System LEVEL_EDITOR = { "LevelEditor" }; }
  32. namespace crown
  33. {
  34. static u16 _width = 1280;
  35. static u16 _height = 720;
  36. //-----------------------------------------------------------------------------
  37. struct Inspector
  38. {
  39. // Inspector
  40. char _name[1024];
  41. float _position[3];
  42. float _rotation[3];
  43. float _scale[3];
  44. char _sprite[1024];
  45. char _material[1024];
  46. bool _visible;
  47. char _state_machine[1024];
  48. bool _open;
  49. Inspector()
  50. : _visible(true)
  51. , _open(true)
  52. {
  53. memset(_name, 0, sizeof(_name));
  54. memset(_sprite, 0, sizeof(_sprite));
  55. memset(_material, 0, sizeof(_material));
  56. memset(_position, 0, sizeof(_position));
  57. memset(_rotation, 0, sizeof(_rotation));
  58. memset(_scale, 0, sizeof(_scale));
  59. memset(_state_machine, 0, sizeof(_state_machine));
  60. }
  61. void draw()
  62. {
  63. if (!_open) return;
  64. if (ImGui::BeginDock("Inspector", &_open))
  65. {
  66. ImGui::SetNextTreeNodeOpen(true);
  67. if (ImGui::TreeNode("Unit"))
  68. {
  69. ImGui::InputText("Name", _name, sizeof(_name));
  70. ImGui::TreePop();
  71. }
  72. ImGui::SetNextTreeNodeOpen(true);
  73. if (ImGui::TreeNode("Transform"))
  74. {
  75. ImGui::InputFloat3("Position", _position, ImGuiInputTextFlags_CharsDecimal);
  76. ImGui::InputFloat3("Rotation", _rotation, ImGuiInputTextFlags_CharsDecimal);
  77. ImGui::InputFloat3("Scale", _scale, ImGuiInputTextFlags_CharsDecimal);
  78. ImGui::TreePop();
  79. }
  80. ImGui::SetNextTreeNodeOpen(true);
  81. if (ImGui::TreeNode("Renderer"))
  82. {
  83. ImGui::InputText("Sprite", _sprite, sizeof(_sprite));
  84. ImGui::InputText("Material", _material, sizeof(_material));
  85. ImGui::Checkbox("Visible", &_visible);
  86. ImGui::TreePop();
  87. }
  88. ImGui::SetNextTreeNodeOpen(true);
  89. if (ImGui::TreeNode("Animation"))
  90. {
  91. ImGui::InputText("State Machine", _state_machine, sizeof(_state_machine));
  92. ImGui::TreePop();
  93. }
  94. }
  95. ImGui::EndDock();
  96. }
  97. };
  98. //-----------------------------------------------------------------------------
  99. struct SceneView
  100. {
  101. ImVec2 _pos;
  102. ImVec2 _size;
  103. ImVec2 _mouse_curr;
  104. ImVec2 _mouse_last;
  105. bool _open;
  106. SceneView()
  107. : _open(true)
  108. {
  109. }
  110. void draw()
  111. {
  112. if (!_open) return;
  113. ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
  114. ImGui::SetNextWindowPos(ImVec2(0, 25));
  115. if (ImGui::BeginDock("Scene View"
  116. , &_open
  117. , ImGuiWindowFlags_NoScrollbar
  118. | ImGuiWindowFlags_NoScrollWithMouse
  119. | ImGuiWindowFlags_NoTitleBar))
  120. {
  121. uint16_t w, h;
  122. device()->resolution(w, h);
  123. bgfx::TextureHandle txh = device()->_pipeline->_buffers[0];
  124. if (bgfx::isValid(txh))
  125. {
  126. ImTextureID tex_id = (void*)(uintptr_t)txh.idx;
  127. ImGui::Image(tex_id
  128. , ImVec2(w, h)
  129. #if CROWN_PLATFORM_WINDOWS
  130. , ImVec2(0, 0)
  131. , ImVec2(1, 1)
  132. #else
  133. , ImVec2(0, 1)
  134. , ImVec2(1, 0)
  135. #endif // CROWN_PLATFORM_WINDOWS
  136. , ImColor(255,255,255,255)
  137. , ImColor(255,255,255,128)
  138. );
  139. }
  140. if (ImGui::IsWindowHovered())
  141. {
  142. // send all input to engine
  143. ImGui::CaptureMouseFromApp(false);
  144. ImGui::CaptureKeyboardFromApp(false);
  145. }
  146. else
  147. {
  148. // send all input to imgui
  149. ImGui::CaptureMouseFromApp(true);
  150. ImGui::CaptureKeyboardFromApp(true);
  151. }
  152. }
  153. _pos = ImGui::GetWindowPos();
  154. _size = ImGui::GetWindowSize();
  155. ImGui::EndDock();
  156. ImGui::PopStyleVar();
  157. }
  158. };
  159. //-----------------------------------------------------------------------------
  160. struct UnitList
  161. {
  162. bool _open;
  163. UnitList()
  164. : _open(true)
  165. {
  166. }
  167. void draw()
  168. {
  169. if (!_open) return;
  170. if (ImGui::BeginDock("Unit List", &_open))
  171. {
  172. ImGui::SetNextTreeNodeOpen(true);
  173. if (ImGui::TreeNode("Units"))
  174. {
  175. if (ImGui::TreeNode("Objects"))
  176. {
  177. for (int i = 0; i < 5; i++)
  178. if (ImGui::TreeNode((void*)(intptr_t)i, "Child %d", i))
  179. {
  180. ImGui::Text("blah blah");
  181. ImGui::SameLine();
  182. if (ImGui::SmallButton("print")) printf("Child %d pressed", i);
  183. ImGui::TreePop();
  184. }
  185. ImGui::TreePop();
  186. }
  187. if (ImGui::TreeNode("Lights"))
  188. {
  189. // 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.");
  190. static bool align_label_with_current_x_position = false;
  191. ImGui::Checkbox("Align label with current X position)", &align_label_with_current_x_position);
  192. ImGui::Text("Hello!");
  193. if (align_label_with_current_x_position)
  194. ImGui::Unindent(ImGui::GetTreeNodeToLabelSpacing());
  195. 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.
  196. 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.
  197. ImGui::PushStyleVar(ImGuiStyleVar_IndentSpacing, ImGui::GetFontSize()*3); // Increase spacing to differentiate leaves from expanded contents.
  198. for (int i = 0; i < 6; i++)
  199. {
  200. // Disable the default open on single-click behavior and pass in Selected flag according to our selection state.
  201. ImGuiTreeNodeFlags node_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick | ((selection_mask & (1 << i)) ? ImGuiTreeNodeFlags_Selected : 0);
  202. if (i < 3)
  203. {
  204. // Node
  205. bool node_open = ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, "Selectable Node %d", i);
  206. if (ImGui::IsItemClicked())
  207. node_clicked = i;
  208. if (node_open)
  209. {
  210. ImGui::Text("Blah blah\nBlah Blah");
  211. ImGui::TreePop();
  212. }
  213. }
  214. else
  215. {
  216. // 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().
  217. ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen, "Selectable Leaf %d", i);
  218. if (ImGui::IsItemClicked())
  219. node_clicked = i;
  220. }
  221. }
  222. if (node_clicked != -1)
  223. {
  224. // Update selection state. Process outside of tree loop to avoid visual inconsistencies during the clicking-frame.
  225. if (ImGui::GetIO().KeyCtrl)
  226. selection_mask ^= (1 << node_clicked); // Ctrl+click to toggle
  227. 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
  228. selection_mask = (1 << node_clicked); // Click to single-select
  229. }
  230. ImGui::PopStyleVar();
  231. if (align_label_with_current_x_position)
  232. ImGui::Indent(ImGui::GetTreeNodeToLabelSpacing());
  233. ImGui::TreePop();
  234. }
  235. ImGui::TreePop();
  236. }
  237. }
  238. ImGui::EndDock(); // End Object List
  239. }
  240. };
  241. //-----------------------------------------------------------------------------
  242. struct SpriteAnimator
  243. {
  244. bool _open;
  245. bool _add_animation_popup_open;
  246. Array<const char*> _entities;
  247. s32 _cur_entity;
  248. TextureResource* _texture;
  249. u32 _texture_width;
  250. u32 _texture_height;
  251. struct Frame
  252. {
  253. char name[512];
  254. ImVec2 pivot;
  255. ImVec4 region;
  256. };
  257. Array<Frame> _frames;
  258. char _anim_name[512];
  259. f32 _anim_time;
  260. Array<const char*> _listbox_items;
  261. s32 _listbox_item_current;
  262. Array<Frame> _anim_preview_frames;
  263. f32 _delta;
  264. u32 current;
  265. Vector<DynamicString> file_list_sprites;
  266. FilesystemDisk* _fs;
  267. SpriteAnimator(const DynamicString& src_dir)
  268. : _open(true)
  269. , _add_animation_popup_open(false)
  270. , _entities(default_allocator())
  271. , _cur_entity(0)
  272. , _texture(nullptr)
  273. , _texture_width(0)
  274. , _texture_height(0)
  275. , _frames(default_allocator())
  276. , _anim_time(0.1f)
  277. , _listbox_items(default_allocator())
  278. , _listbox_item_current(0)
  279. , _anim_preview_frames(default_allocator())
  280. , _delta(0.0f)
  281. , current(0)
  282. , file_list_sprites(default_allocator())
  283. {
  284. memset(_anim_name, 0, sizeof(_anim_name));
  285. _fs = CE_NEW(default_allocator(), FilesystemDisk)(default_allocator());
  286. _fs->set_prefix(src_dir.c_str());
  287. get_sprites_list();
  288. }
  289. ImVec2 pixel_to_uv(uint32_t tex_w, uint32_t tex_h, float x, float y)
  290. {
  291. ImVec2 uv;
  292. uv.x = (float)x / (float)tex_w;
  293. uv.y = (float)y / (float)tex_h;
  294. return uv;
  295. }
  296. void get_directory(DynamicString& dir, const char* path)
  297. {
  298. CE_ENSURE(NULL != path);
  299. const char* ls = strrchr(path, '/');
  300. u32 file_len = strlen(ls+1);
  301. u32 dir_len = strlen(path) - file_len;
  302. char buff[1024];
  303. memcpy(buff, path, dir_len);
  304. buff[dir_len] = '\0';
  305. path::reduce(dir, buff);
  306. }
  307. void save_sprite_animation()
  308. {
  309. TempAllocator4096 ta;
  310. StringStream ss(ta);
  311. ss << "frames = [ ";
  312. for (u32 i = 0; i < array::size(_listbox_items); i++)
  313. {
  314. ss << _listbox_items[i] + 7 << " ";
  315. }
  316. ss << "]\n";
  317. ss << "total_time = ";
  318. ss << _anim_time;
  319. DynamicString dir(ta);
  320. get_directory(dir, _entities[_cur_entity]);
  321. DynamicString file_name(ta);
  322. path::join(file_name, dir.c_str(), _anim_name);
  323. file_name += ".sprite_animation";
  324. File* f = _fs->open(file_name.c_str(), FileOpenMode::WRITE);
  325. f->write(string_stream::c_str(ss), strlen(string_stream::c_str(ss)));
  326. f->close();
  327. }
  328. void get_files_list(Vector<DynamicString>& out, const char* path="")
  329. {
  330. TempAllocator4096 ta;
  331. Vector<DynamicString> files(ta);
  332. _fs->list_files(path, files);
  333. for (u32 i = 0; i < vector::size(files); i++)
  334. {
  335. DynamicString join(ta);
  336. path::join(join, path, files[i].c_str());
  337. join = join.c_str()[0] == '/' ? join.c_str()+1 : join.c_str();
  338. if (_fs->is_directory(join.c_str()))
  339. {
  340. get_files_list(out, join.c_str());
  341. }
  342. else
  343. {
  344. vector::push_back(out, join);
  345. }
  346. }
  347. }
  348. void get_sprites_list()
  349. {
  350. TempAllocator4096 ta;
  351. Vector<DynamicString> files(ta);
  352. get_files_list(files);
  353. for (DynamicString* f = vector::begin(files); f != vector::end(files); f++)
  354. if (f->has_suffix(".sprite"))
  355. array::push_back(_entities, (const char*) strdup(f->c_str()));
  356. }
  357. void draw()
  358. {
  359. if (!_open) return;
  360. if (ImGui::BeginDock("Animator", &_open))
  361. {
  362. if (_texture)
  363. {
  364. Frame f = _frames[0];
  365. ImVec2 start = pixel_to_uv(_texture_width, _texture_height, f.region.x, f.region.y);
  366. ImVec2 end = pixel_to_uv(_texture_width, _texture_height, f.region.x+f.region.z, f.region.y+f.region.w);
  367. ImGui::Image((void*)(uintptr_t) _texture->handle.idx
  368. , ImVec2(f.region.z, f.region.w)
  369. , start
  370. , end
  371. , ImColor(255, 255, 255, 55)
  372. );
  373. }
  374. if (ImGui::Combo("Entities", &_cur_entity, (const char* const*) array::begin(_entities), array::size(_entities)))
  375. {
  376. array::clear(_frames);
  377. const char* sprite = _entities[_cur_entity];
  378. u32 sprite_len = strlen(sprite);
  379. char entity[1024];
  380. strncpy(entity, sprite, strlen(sprite));
  381. entity[sprite_len-7] = '\0'; // remove ".sprite"
  382. ResourceManager* resman = device()->_resource_manager;
  383. _texture = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64(entity));
  384. File* file = _fs->open(sprite, FileOpenMode::READ);
  385. const u32 size = file->size();
  386. Buffer buf(default_allocator());
  387. array::resize(buf, size);
  388. file->read(array::begin(buf), size);
  389. _fs->close(*file);
  390. JsonObject obj(default_allocator());
  391. JsonArray list(default_allocator());
  392. sjson::parse(buf, obj);
  393. sjson::parse_array(obj["frames"], list);
  394. _texture_width = sjson::parse_int(obj["width"]);
  395. _texture_height = sjson::parse_int(obj["height"]);
  396. for (uint32_t i = 0; i < array::size(list); i++)
  397. {
  398. JsonObject frame(default_allocator());
  399. DynamicString name(default_allocator());
  400. JsonArray pivot(default_allocator());
  401. JsonArray region(default_allocator());
  402. sjson::parse_object(list[i], frame);
  403. sjson::parse_array(frame["pivot"], pivot);
  404. sjson::parse_array(frame["region"], region);
  405. Frame f;
  406. sjson::parse_string(frame["name"], name);
  407. strncpy(f.name, name.c_str(), name.length());
  408. f.name[name.length()] = '\0';
  409. f.pivot.x = sjson::parse_float(pivot[0]);
  410. f.pivot.y = sjson::parse_float(pivot[1]);
  411. f.region.x = sjson::parse_float(region[0]);
  412. f.region.y = sjson::parse_float(region[1]);
  413. f.region.z = sjson::parse_float(region[2]);
  414. f.region.w = sjson::parse_float(region[3]);
  415. array::push_back(_frames, f);
  416. }
  417. }
  418. if (ImGui::Button("Add animation", ImVec2(100, 25)))
  419. {
  420. _add_animation_popup_open = true;
  421. }
  422. if (_add_animation_popup_open)
  423. {
  424. ImGui::OpenPopup("Add animation");
  425. }
  426. if (ImGui::BeginPopup("Add animation"))
  427. {
  428. ImGui::InputText("Name", _anim_name, sizeof(_anim_name));
  429. ImGui::InputFloat("Time", &_anim_time, 0.1f, 0.1f, 1);
  430. ImGui::ListBox("Animation Frames", &_listbox_item_current, (const char* const*)array::begin(_listbox_items), array::size(_listbox_items));
  431. if (ImGui::Button("Clear Frames", ImVec2(100.0f, 25.0f)))
  432. {
  433. array::clear(_listbox_items);
  434. array::clear(_anim_preview_frames);
  435. _delta = 0.0f;
  436. current = 0;
  437. }
  438. if (array::size(_anim_preview_frames) > 0)
  439. {
  440. _delta += 1.0f/60.0f;
  441. if (_delta >= _anim_time/array::size(_anim_preview_frames))
  442. {
  443. _delta = 0;
  444. current++;
  445. if (current >= array::size(_anim_preview_frames))
  446. current = 0;
  447. }
  448. Frame f = _anim_preview_frames[current];
  449. ImVec2 start = pixel_to_uv(_texture_width, _texture_height, f.region.x, f.region.y);
  450. ImVec2 end = pixel_to_uv(_texture_width, _texture_height, f.region.x+f.region.z, f.region.y+f.region.w);
  451. ImGui::Image(
  452. (void*)(uintptr_t) _texture->handle.idx
  453. , ImVec2(f.region.z, f.region.w)
  454. , start
  455. , end
  456. , ImColor(255, 255, 255, 55)
  457. );
  458. }
  459. ImGui::Separator();
  460. for (uint32_t i = 0; i < array::size(_frames); i++)
  461. {
  462. Frame f = _frames[i];
  463. ImVec2 start = pixel_to_uv(_texture_width, _texture_height, f.region.x, f.region.y);
  464. ImVec2 end = pixel_to_uv(_texture_width, _texture_height, f.region.x+f.region.z, f.region.y+f.region.w);
  465. ImGui::SameLine();
  466. if (i % 9 == 0) ImGui::NewLine();
  467. ImGui::BeginGroup();
  468. ImGui::Image(
  469. (void*)(uintptr_t) _texture->handle.idx
  470. , ImVec2(f.region.z, f.region.w)
  471. , start
  472. , end
  473. , ImColor(255, 255, 255, 55)
  474. );
  475. ImGui::NewLine();
  476. if (ImGui::Button(_frames[i].name, ImVec2(100.0f, 25.0f)))
  477. {
  478. array::push_back(_listbox_items, (const char*) strdup(_frames[i].name));
  479. array::push_back(_anim_preview_frames, _frames[i]);
  480. }
  481. ImGui::EndGroup();
  482. }
  483. if (ImGui::Button("Save", ImVec2(100, 25)))
  484. {
  485. save_sprite_animation();
  486. ImGui::CloseCurrentPopup();
  487. _add_animation_popup_open = false;
  488. }
  489. ImGui::SameLine();
  490. if (ImGui::Button("Cancel", ImVec2(100, 25)))
  491. {
  492. ImGui::CloseCurrentPopup();
  493. _add_animation_popup_open = false;
  494. }
  495. ImGui::EndPopup();
  496. }
  497. }
  498. ImGui::EndDock();
  499. }
  500. };
  501. //-----------------------------------------------------------------------------
  502. struct LevelEditor
  503. {
  504. DynamicString _source_dir;
  505. // FX
  506. TextureResource* tex_move;
  507. TextureResource* tex_place;
  508. TextureResource* tex_rotate;
  509. TextureResource* tex_scale;
  510. TextureResource* tex_ref_world;
  511. TextureResource* tex_ref_local;
  512. TextureResource* tex_axis_local;
  513. TextureResource* tex_axis_world;
  514. TextureResource* tex_snap_grid;
  515. // State
  516. float _grid_size;
  517. int32_t _rotation_snap;
  518. bool _show_grid;
  519. bool _snap_to_grid;
  520. bool _debug_render_world;
  521. bool _debug_physics_world;
  522. tool::ToolType::Enum _tool_type;
  523. tool::SnapMode::Enum _snap_mode;
  524. tool::ReferenceSystem::Enum _reference_system;
  525. ImVec2 _main_menu_pos;
  526. ImVec2 _main_menu_size;
  527. ImVec2 _toolbar_pos;
  528. ImVec2 _toolbar_size;
  529. Console _console;
  530. Inspector _inspector;
  531. SceneView _scene_view;
  532. UnitList _unit_list;
  533. SpriteAnimator _animator;
  534. LevelEditor(const DynamicString& source_dir)
  535. : _source_dir(default_allocator())
  536. , _grid_size(1.0f)
  537. , _rotation_snap(15)
  538. , _show_grid(true)
  539. , _snap_to_grid(true)
  540. , _debug_render_world(false)
  541. , _debug_physics_world(false)
  542. , _tool_type(tool::ToolType::MOVE)
  543. , _snap_mode(tool::SnapMode::RELATIVE)
  544. , _reference_system(tool::ReferenceSystem::LOCAL)
  545. , _main_menu_pos(0, 0)
  546. , _main_menu_size(0, 0)
  547. , _toolbar_pos(0, 0)
  548. , _toolbar_size(0, 0)
  549. , _animator(source_dir)
  550. {
  551. ResourceManager* resman = device()->_resource_manager;
  552. tex_move = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/tool-move"));
  553. tex_place = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/tool-place"));
  554. tex_rotate = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/tool-rotate"));
  555. tex_scale = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/tool-scale"));
  556. tex_ref_world = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/reference-world"));
  557. tex_ref_local = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/reference-local"));
  558. tex_axis_local = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/axis-local"));
  559. tex_axis_world = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/axis-world"));
  560. tex_snap_grid = (TextureResource*)resman->get(RESOURCE_TYPE_TEXTURE, StringId64("core/editors/gui/snap-to-grid"));
  561. imgui_create();
  562. ImGui::LoadDock();
  563. }
  564. ~LevelEditor()
  565. {
  566. ImGui::SaveDock();
  567. imgui_destroy();
  568. }
  569. void update(float dt)
  570. {
  571. CE_UNUSED(dt);
  572. static f32 last_w = 0.0f;
  573. static f32 last_h = 0.0f;
  574. if (last_w != _scene_view._size.x || last_h != _scene_view._size.y)
  575. {
  576. last_w = _scene_view._size.x;
  577. last_h = _scene_view._size.y;
  578. device()->_width = _scene_view._size.x != 0.0f ? _scene_view._size.x : 128.0f;
  579. device()->_height = _scene_view._size.y != 0.0f ? _scene_view._size.y : 128.0f;
  580. }
  581. TempAllocator4096 ta;
  582. u32 message_count = 0;
  583. // Receive response from engine
  584. for (;;)
  585. {
  586. uint32_t msg_len = 0;
  587. ReadResult rr = _console._client.read_nonblock(&msg_len, sizeof(msg_len));
  588. if (rr.error == ReadResult::WOULDBLOCK)
  589. break;
  590. if (ReadResult::SUCCESS == rr.error)
  591. {
  592. char msg[8192];
  593. rr = _console._client.read(msg, msg_len);
  594. msg[msg_len] = '\0';
  595. message_count++;
  596. // logi(LEVEL_EDITOR, "count: %d", message_count);
  597. if (ReadResult::SUCCESS == rr.error)
  598. {
  599. JsonObject obj(ta);
  600. DynamicString type(ta);
  601. json::parse(msg, obj);
  602. json::parse_string(obj["type"], type);
  603. if (type == "message")
  604. {
  605. DynamicString severity(ta);
  606. DynamicString message(ta);
  607. json::parse_string(obj["severity"], severity);
  608. json::parse_string(obj["message"], message);
  609. LogSeverity::Enum ls = LogSeverity::COUNT;
  610. if (strcmp("info", severity.c_str()) == 0)
  611. ls = LogSeverity::LOG_INFO;
  612. else if (strcmp("warning", severity.c_str()) == 0)
  613. ls = LogSeverity::LOG_WARN;
  614. else if (strcmp("error", severity.c_str()) == 0)
  615. ls = LogSeverity::LOG_ERROR;
  616. else
  617. CE_FATAL("Unknown severity");
  618. ConsoleLog log(ls, message.c_str());
  619. vector::push_back(_console._console_items, log);
  620. }
  621. else
  622. {
  623. ConsoleLog log(LogSeverity::LOG_ERROR, "Unknown message type");
  624. vector::push_back(_console._console_items, log);
  625. }
  626. console_scroll_to_bottom();
  627. }
  628. }
  629. }
  630. imgui_begin_frame(VIEW_IMGUI, _width, _height);
  631. float offset_y = _main_menu_size.y;
  632. ImGui::RootDock(ImVec2(0, offset_y), ImVec2(_width, _height-offset_y));
  633. main_menu_bar();
  634. // toolbar();
  635. _scene_view.draw();
  636. console_draw(_console);
  637. _unit_list.draw();
  638. _inspector.draw();
  639. _animator.draw();
  640. imgui_end_frame();
  641. }
  642. void send_command(StringStream& ss)
  643. {
  644. TempAllocator4096 ta;
  645. StringStream out(ta);
  646. out << "{\"type\":\"script\",\"script\":\"";
  647. out << string_stream::c_str(ss);
  648. out << "\"}";
  649. const char* cmd = string_stream::c_str(out);
  650. const uint32_t size = strlen32(cmd);
  651. _console._client.write(&size, sizeof(uint32_t));
  652. _console._client.write(cmd, size);
  653. }
  654. void tool_send_state()
  655. {
  656. TempAllocator256 ta;
  657. StringStream ss(ta);
  658. tool::set_grid_size(ss, _grid_size);
  659. tool::set_rotation_snap(ss, _rotation_snap);
  660. tool::enable_show_grid(ss, _show_grid);
  661. tool::enable_snap_to_grid(ss, _snap_to_grid);
  662. tool::enable_debug_render_world(ss, _debug_render_world);
  663. tool::enable_debug_physics_world(ss, _debug_physics_world);
  664. tool::set_tool_type(ss, _tool_type);
  665. tool::set_snap_mode(ss, _snap_mode);
  666. tool::set_reference_system(ss, _reference_system);
  667. send_command(ss);
  668. }
  669. void format_time(uint64_t time, char* str, size_t size)
  670. {
  671. #if CROWN_PLATFORM_WINDOWS
  672. // SYSTEMTIME st;
  673. // FileTimeToSystemTime(&time, &st);
  674. // sprintf(str, "%d/%d/%d %d:%d"
  675. // , st.wDay
  676. // , st.wMonth
  677. // , st.wYear
  678. // , st.wHour
  679. // , st.wMinute);
  680. #else
  681. struct tm lt;
  682. localtime_r((time_t*)&time, &lt);
  683. strftime(str, size, "%d/%m/%Y %H:%M", &lt);
  684. #endif
  685. }
  686. void main_menu_bar()
  687. {
  688. // Main Menu
  689. if (ImGui::BeginMainMenuBar())
  690. {
  691. if (ImGui::BeginMenu("File"))
  692. {
  693. if (ImGui::MenuItem("New"))
  694. {
  695. }
  696. if (ImGui::MenuItem("Open", "Ctrl+O"))
  697. {
  698. }
  699. if (ImGui::MenuItem("Save", "Ctrl+S"))
  700. {
  701. }
  702. if (ImGui::MenuItem("Save As..."))
  703. {
  704. }
  705. ImGui::Separator();
  706. if (ImGui::MenuItem("Quit", "Ctrl+Q", false, true))
  707. {
  708. TempAllocator64 ta;
  709. StringStream ss(ta);
  710. tool::device_quit(ss);
  711. send_command(ss);
  712. }
  713. ImGui::EndMenu();
  714. }
  715. if (ImGui::BeginMenu("Edit"))
  716. {
  717. if (ImGui::MenuItem("Undo", "Ctrl+Z")) {}
  718. if (ImGui::MenuItem("Redo", "Ctrl+Y")) {}
  719. ImGui::EndMenu();
  720. }
  721. if (ImGui::BeginMenu("Create"))
  722. {
  723. TempAllocator256 ta;
  724. StringStream ss(ta);
  725. if (ImGui::BeginMenu("Primitives"))
  726. {
  727. if (ImGui::MenuItem("Cube", NULL, false, true))
  728. {
  729. _tool_type = tool::ToolType::PLACE;
  730. tool_send_state();
  731. tool::set_placeable(ss, "unit", "core/units/primitives/cube");
  732. send_command(ss);
  733. }
  734. if (ImGui::MenuItem("Sphere"))
  735. {
  736. _tool_type = tool::ToolType::PLACE;
  737. tool_send_state();
  738. tool::set_placeable(ss, "unit", "core/units/primitives/sphere");
  739. send_command(ss);
  740. }
  741. if (ImGui::MenuItem("Cone"))
  742. {
  743. _tool_type = tool::ToolType::PLACE;
  744. tool_send_state();
  745. tool::set_placeable(ss, "unit", "core/units/primitives/cone");
  746. send_command(ss);
  747. }
  748. if (ImGui::MenuItem("Cylinder"))
  749. {
  750. _tool_type = tool::ToolType::PLACE;
  751. tool_send_state();
  752. tool::set_placeable(ss, "unit", "core/units/primitives/cylinder");
  753. send_command(ss);
  754. }
  755. if (ImGui::MenuItem("Plane"))
  756. {
  757. _tool_type = tool::ToolType::PLACE;
  758. tool_send_state();
  759. tool::set_placeable(ss, "unit", "core/units/primitives/plane");
  760. send_command(ss);
  761. }
  762. ImGui::EndMenu();
  763. }
  764. if (ImGui::MenuItem("Camera"))
  765. {
  766. _tool_type = tool::ToolType::PLACE;
  767. tool_send_state();
  768. tool::set_placeable(ss, "unit", "core/units/camera");
  769. send_command(ss);
  770. }
  771. if (ImGui::MenuItem("Light"))
  772. {
  773. _tool_type = tool::ToolType::PLACE;
  774. tool_send_state();
  775. tool::set_placeable(ss, "unit", "core/units/light");
  776. send_command(ss);
  777. }
  778. if (ImGui::MenuItem("Sound"))
  779. {
  780. _tool_type = tool::ToolType::PLACE;
  781. tool_send_state();
  782. tool::set_placeable(ss, "sound", "core/units/camera");
  783. send_command(ss);
  784. }
  785. ImGui::EndMenu();
  786. }
  787. if (ImGui::BeginMenu("Scene View"))
  788. {
  789. TempAllocator256 ta;
  790. StringStream ss(ta);
  791. if (ImGui::BeginMenu("Camera View"))
  792. {
  793. if (ImGui::MenuItem("Perspective"))
  794. {
  795. tool::camera_view_perspective(ss);
  796. send_command(ss);
  797. }
  798. if (ImGui::MenuItem("Front"))
  799. {
  800. tool::camera_view_front(ss);
  801. send_command(ss);
  802. }
  803. if (ImGui::MenuItem("Back"))
  804. {
  805. tool::camera_view_back(ss);
  806. send_command(ss);
  807. }
  808. if (ImGui::MenuItem("Right"))
  809. {
  810. tool::camera_view_right(ss);
  811. send_command(ss);
  812. }
  813. if (ImGui::MenuItem("Left"))
  814. {
  815. tool::camera_view_left(ss);
  816. send_command(ss);
  817. }
  818. if (ImGui::MenuItem("Top"))
  819. {
  820. tool::camera_view_top(ss);
  821. send_command(ss);
  822. }
  823. if (ImGui::MenuItem("Bottom"))
  824. {
  825. tool::camera_view_bottom(ss);
  826. send_command(ss);
  827. }
  828. ImGui::EndMenu();
  829. }
  830. ImGui::Separator();
  831. if (ImGui::MenuItem("Show Grid", NULL, _show_grid))
  832. {
  833. _show_grid = !_show_grid;
  834. tool_send_state();
  835. }
  836. if (ImGui::SliderFloat("Grid Size", &_grid_size, 0.1f, 5.0f))
  837. {
  838. tool_send_state();
  839. }
  840. if (ImGui::SliderInt("Snap Rot", &_rotation_snap, 1, 180))
  841. {
  842. tool_send_state();
  843. }
  844. ImGui::EndMenu();
  845. }
  846. if (ImGui::BeginMenu("Windows"))
  847. {
  848. if (ImGui::MenuItem("Scene"))
  849. {
  850. _scene_view._open = true;
  851. }
  852. if (ImGui::MenuItem("Unit List"))
  853. {
  854. _unit_list._open = true;
  855. }
  856. if (ImGui::MenuItem("Inspector"))
  857. {
  858. _inspector._open = true;
  859. }
  860. if (ImGui::MenuItem("Console"))
  861. {
  862. _console._open = true;
  863. }
  864. if (ImGui::MenuItem("Animator"))
  865. {
  866. _animator._open = true;
  867. }
  868. ImGui::EndMenu();
  869. }
  870. if (ImGui::BeginMenu("Help"))
  871. {
  872. ImGui::MenuItem("About", "");
  873. ImGui::EndMenu();
  874. }
  875. _main_menu_pos = ImGui::GetWindowPos();
  876. _main_menu_size = ImGui::GetWindowSize();
  877. ImGui::EndMainMenuBar();
  878. }
  879. }
  880. void toolbar()
  881. {
  882. if (ImGui::BeginToolbar("Toolbar", _toolbar_pos, _toolbar_size))
  883. {
  884. if (ImGui::ToolbarButton((void*)(uintptr_t) tex_place->handle.idx, ImVec4(0, 0, 0, 0), "Place"))
  885. {
  886. _tool_type = tool::ToolType::PLACE;
  887. tool_send_state();
  888. }
  889. if (ImGui::ToolbarButton((void*)(uintptr_t) tex_move->handle.idx, ImVec4(0, 0, 0, 0), "Move"))
  890. {
  891. _tool_type = tool::ToolType::MOVE;
  892. tool_send_state();
  893. }
  894. if (ImGui::ToolbarButton((void*)(uintptr_t) tex_rotate->handle.idx, ImVec4(0, 0, 0, 0), "Rotate"))
  895. {
  896. _tool_type = tool::ToolType::ROTATE;
  897. tool_send_state();
  898. }
  899. if (ImGui::ToolbarButton((void*)(uintptr_t) tex_scale->handle.idx, ImVec4(0, 0, 0, 0), "Scale"))
  900. {
  901. _tool_type = tool::ToolType::SCALE;
  902. tool_send_state();
  903. }
  904. if (ImGui::ToolbarButton((void*)(uintptr_t) tex_axis_local->handle.idx, ImVec4(0, 0, 0, 0), "Reference System: Local"))
  905. {
  906. _reference_system = tool::ReferenceSystem::LOCAL;
  907. tool_send_state();
  908. }
  909. if (ImGui::ToolbarButton((void*)(uintptr_t) tex_axis_world->handle.idx, ImVec4(0, 0, 0, 0), "Reference System: World"))
  910. {
  911. _reference_system = tool::ReferenceSystem::WORLD;
  912. tool_send_state();
  913. }
  914. if (ImGui::ToolbarButton((void*)(uintptr_t) tex_axis_local->handle.idx, ImVec4(0, 0, 0, 0), "Snap Mode: Relative"))
  915. {
  916. _snap_mode = tool::SnapMode::RELATIVE;
  917. tool_send_state();
  918. }
  919. if (ImGui::ToolbarButton((void*)(uintptr_t) tex_ref_world->handle.idx, ImVec4(0, 0, 0, 0), "Snap Mode: Absolute"))
  920. {
  921. _snap_mode = tool::SnapMode::ABSOLUTE;
  922. tool_send_state();
  923. }
  924. _toolbar_pos = ImGui::GetWindowPos();
  925. _toolbar_size = ImGui::GetWindowSize();
  926. ImGui::EndToolbar();
  927. }
  928. }
  929. };
  930. LevelEditor* s_editor;
  931. void tool_init()
  932. {
  933. const DeviceOptions& opt = device()->_device_options;
  934. s_editor = CE_NEW(default_allocator(), LevelEditor)(opt._source_dir);
  935. }
  936. void tool_update(float dt)
  937. {
  938. s_editor->update(dt);
  939. }
  940. void tool_shutdown()
  941. {
  942. CE_DELETE(default_allocator(), s_editor);
  943. }
  944. extern bool next_event(OsEvent& ev);
  945. bool tool_process_events()
  946. {
  947. ImGuiIO& io = ImGui::GetIO();
  948. bool exit = false;
  949. bool reset = false;
  950. TempAllocator4096 ta;
  951. StringStream ss(ta);
  952. OsEvent event;
  953. while (next_event(event))
  954. {
  955. if (event.type == OsEventType::NONE)
  956. continue;
  957. switch (event.type)
  958. {
  959. case OsEventType::BUTTON:
  960. {
  961. const ButtonEvent& ev = event.button;
  962. switch (ev.device_id)
  963. {
  964. case crown::InputDeviceType::KEYBOARD:
  965. io.KeyCtrl = ((ev.button_num == crown::KeyboardButton::CTRL_LEFT)
  966. || (ev.button_num == crown::KeyboardButton::CTRL_RIGHT)) && ev.pressed;
  967. io.KeyShift = ((ev.button_num == crown::KeyboardButton::SHIFT_LEFT)
  968. || (ev.button_num == crown::KeyboardButton::SHIFT_RIGHT)) && ev.pressed;
  969. io.KeyAlt = ((ev.button_num == crown::KeyboardButton::ALT_LEFT)
  970. || (ev.button_num == crown::KeyboardButton::ALT_RIGHT)) && ev.pressed;
  971. io.KeySuper = ((ev.button_num == crown::KeyboardButton::SUPER_LEFT)
  972. || (ev.button_num == crown::KeyboardButton::SUPER_RIGHT)) && ev.pressed;
  973. io.KeysDown[crown::KeyboardButton::TAB] = (ev.button_num == crown::KeyboardButton::TAB) && ev.pressed;
  974. io.KeysDown[crown::KeyboardButton::LEFT] = (ev.button_num == crown::KeyboardButton::LEFT) && ev.pressed;
  975. io.KeysDown[crown::KeyboardButton::RIGHT] = (ev.button_num == crown::KeyboardButton::RIGHT) && ev.pressed;
  976. io.KeysDown[crown::KeyboardButton::UP] = (ev.button_num == crown::KeyboardButton::UP) && ev.pressed;
  977. io.KeysDown[crown::KeyboardButton::DOWN] = (ev.button_num == crown::KeyboardButton::DOWN) && ev.pressed;
  978. io.KeysDown[crown::KeyboardButton::PAGE_UP] = (ev.button_num == crown::KeyboardButton::PAGE_UP) && ev.pressed;
  979. io.KeysDown[crown::KeyboardButton::PAGE_DOWN] = (ev.button_num == crown::KeyboardButton::PAGE_DOWN) && ev.pressed;
  980. io.KeysDown[crown::KeyboardButton::HOME] = (ev.button_num == crown::KeyboardButton::HOME) && ev.pressed;
  981. io.KeysDown[crown::KeyboardButton::END] = (ev.button_num == crown::KeyboardButton::END) && ev.pressed;
  982. io.KeysDown[crown::KeyboardButton::DEL] = (ev.button_num == crown::KeyboardButton::DEL) && ev.pressed;
  983. io.KeysDown[crown::KeyboardButton::BACKSPACE] = (ev.button_num == crown::KeyboardButton::BACKSPACE) && ev.pressed;
  984. io.KeysDown[crown::KeyboardButton::ENTER] = (ev.button_num == crown::KeyboardButton::ENTER) && ev.pressed;
  985. io.KeysDown[crown::KeyboardButton::ESCAPE] = (ev.button_num == crown::KeyboardButton::ESCAPE) && ev.pressed;
  986. io.KeysDown[crown::KeyboardButton::A] = (ev.button_num == crown::KeyboardButton::A) && ev.pressed;
  987. io.KeysDown[crown::KeyboardButton::C] = (ev.button_num == crown::KeyboardButton::C) && ev.pressed;
  988. io.KeysDown[crown::KeyboardButton::V] = (ev.button_num == crown::KeyboardButton::V) && ev.pressed;
  989. io.KeysDown[crown::KeyboardButton::X] = (ev.button_num == crown::KeyboardButton::X) && ev.pressed;
  990. io.KeysDown[crown::KeyboardButton::Y] = (ev.button_num == crown::KeyboardButton::Y) && ev.pressed;
  991. io.KeysDown[crown::KeyboardButton::Z] = (ev.button_num == crown::KeyboardButton::Z) && ev.pressed;
  992. if (!io.WantCaptureKeyboard)
  993. {
  994. if (ev.pressed)
  995. {
  996. if (ev.button_num == crown::KeyboardButton::W)
  997. tool::keyboard_pressed(ss, 'w');
  998. if (ev.button_num == crown::KeyboardButton::A)
  999. tool::keyboard_pressed(ss, 'a');
  1000. if (ev.button_num == crown::KeyboardButton::S)
  1001. tool::keyboard_pressed(ss, 's');
  1002. if (ev.button_num == crown::KeyboardButton::D)
  1003. tool::keyboard_pressed(ss, 'd');
  1004. }
  1005. else
  1006. {
  1007. if (ev.button_num == crown::KeyboardButton::W)
  1008. tool::keyboard_released(ss, 'w');
  1009. if (ev.button_num == crown::KeyboardButton::A)
  1010. tool::keyboard_released(ss, 'a');
  1011. if (ev.button_num == crown::KeyboardButton::S)
  1012. tool::keyboard_released(ss, 's');
  1013. if (ev.button_num == crown::KeyboardButton::D)
  1014. tool::keyboard_released(ss, 'd');
  1015. }
  1016. }
  1017. break;
  1018. case crown::InputDeviceType::MOUSE:
  1019. io.MouseDown[0] = (ev.button_num == crown::MouseButton::LEFT) && ev.pressed;
  1020. io.MouseDown[1] = (ev.button_num == crown::MouseButton::RIGHT) && ev.pressed;
  1021. io.MouseDown[2] = (ev.button_num == crown::MouseButton::MIDDLE) && ev.pressed;
  1022. if (!io.WantCaptureMouse)
  1023. {
  1024. ImVec2& mouse_curr = s_editor->_scene_view._mouse_curr;
  1025. mouse_curr.x = io.MousePos.x - s_editor->_scene_view._pos.x;
  1026. mouse_curr.y = io.MousePos.y - s_editor->_scene_view._pos.y;
  1027. tool::set_mouse_state(ss
  1028. , mouse_curr.x
  1029. , mouse_curr.y
  1030. , io.MouseDown[0]
  1031. , io.MouseDown[2]
  1032. , io.MouseDown[1]
  1033. );
  1034. if (ev.button_num == crown::MouseButton::LEFT)
  1035. {
  1036. if (ev.pressed)
  1037. tool::mouse_down(ss, mouse_curr.x, mouse_curr.y);
  1038. else
  1039. tool::mouse_up(ss, mouse_curr.x, mouse_curr.y);
  1040. }
  1041. }
  1042. break;
  1043. }
  1044. }
  1045. break;
  1046. case OsEventType::AXIS:
  1047. {
  1048. const AxisEvent& ev = event.axis;
  1049. switch(ev.device_id)
  1050. {
  1051. case InputDeviceType::MOUSE:
  1052. {
  1053. switch(ev.axis_num)
  1054. {
  1055. case crown::MouseAxis::CURSOR:
  1056. io.MousePos = ImVec2(ev.axis_x, ev.axis_y);
  1057. if (!io.WantCaptureMouse)
  1058. {
  1059. ImVec2& mouse_curr = s_editor->_scene_view._mouse_curr;
  1060. ImVec2& mouse_last = s_editor->_scene_view._mouse_last;
  1061. mouse_curr.x = io.MousePos.x - s_editor->_scene_view._pos.x;
  1062. mouse_curr.y = io.MousePos.y - s_editor->_scene_view._pos.y;
  1063. float delta_x = mouse_curr.x - mouse_last.x;
  1064. float delta_y = mouse_curr.y - mouse_last.y;
  1065. tool::mouse_move(ss, mouse_curr.x, mouse_curr.y, delta_x, delta_y);
  1066. mouse_last = mouse_curr;
  1067. }
  1068. break;
  1069. case crown::MouseAxis::WHEEL:
  1070. io.MouseWheel += ev.axis_y;
  1071. if (!io.WantCaptureMouse)
  1072. {
  1073. tool::mouse_wheel(ss, io.MouseWheel);
  1074. }
  1075. break;
  1076. }
  1077. }
  1078. }
  1079. }
  1080. break;
  1081. case OsEventType::TEXT:
  1082. {
  1083. const TextEvent& ev = event.text;
  1084. io.AddInputCharactersUTF8((const char*) ev.utf8);
  1085. }
  1086. break;
  1087. case OsEventType::RESOLUTION:
  1088. {
  1089. const ResolutionEvent& ev = event.resolution;
  1090. _width = ev.width;
  1091. _height = ev.height;
  1092. reset = true;
  1093. }
  1094. break;
  1095. case OsEventType::EXIT:
  1096. exit = true;
  1097. break;
  1098. default:
  1099. break;
  1100. }
  1101. }
  1102. s_editor->send_command(ss);
  1103. bool vsync = true;
  1104. if (reset)
  1105. bgfx::reset(_width, _height, (vsync ? BGFX_RESET_VSYNC : BGFX_RESET_NONE));
  1106. return exit;
  1107. }
  1108. } // namespace crown
  1109. #endif // CROWN_TOOLS