💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.6 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "ui-system.hpp"
  20. #include "input/control.hpp"
  21. #include "resources/resource-manager.hpp"
  22. using namespace vmq::operators;
  23. ui_system::ui_system(::resource_manager* resource_manager):
  24. resource_manager(resource_manager),
  25. tool_menu_control(nullptr)
  26. {
  27. // Setup camera
  28. camera.look_at({0.0f, 0.0f, 500.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f});
  29. // Setup lighting
  30. indirect_light.set_intensity(0.25f);
  31. indirect_light.update_tweens();
  32. direct_light.look_at({-0.1f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f});
  33. direct_light.set_intensity(1.0f);
  34. direct_light.update_tweens();
  35. // Setup modal background
  36. modal_bg_material.set_shader_program(resource_manager->load<shader_program>("ui-element-untextured.glsl"));
  37. modal_bg_material.set_flags(1);
  38. modal_bg_material.add_property<float4>("tint")->set_value({0, 0, 0, 0.25f});
  39. modal_bg.set_material(&modal_bg_material);
  40. modal_bg.set_translation({0, 0, -10.0f});
  41. // Setup tool selector background
  42. tool_selector_bg.set_material(resource_manager->load<material>("tool-selector.mtl"));
  43. tool_selector_bg.set_translation({0, 0, -4.0f});
  44. tool_selector_bg.set_scale({270, 270, 270});
  45. // Setup tool selector ant
  46. tool_selector_ant.set_model(resource_manager->load<model>("worker-ant.obj"));
  47. tool_selector_ant.set_scale({350, 350, 350});
  48. tool_selector_ant.set_rotation(vmq::angle_axis(vmq::radians(180.0f), {0, 0, 1}) * vmq::angle_axis(vmq::radians(90.0f), {1, 0, 0}));
  49. tool_selector_ant.update_tweens();
  50. // Setup energy symbol
  51. energy_symbol.set_model(resource_manager->load<model>("energy.obj"));
  52. energy_symbol.set_scale({30, 30, 30});
  53. energy_symbol.update_tweens();
  54. energy_symbol.set_active(false);
  55. // Setup scene
  56. scene.add_object(&camera);
  57. scene.add_object(&indirect_light);
  58. scene.add_object(&direct_light);
  59. scene.add_object(&energy_symbol);
  60. }
  61. void ui_system::update(float dt)
  62. {}
  63. void ui_system::set_viewport(const float4& viewport)
  64. {
  65. this->viewport = viewport;
  66. // Calculate viewport center
  67. viewport_center[0] = (viewport[2] - viewport[0]) * 0.5f;
  68. viewport_center[1] = (viewport[3] - viewport[1]) * 0.5f;
  69. // Recalculate orthographic projection
  70. float clip_left = -viewport[2] * 0.5f;
  71. float clip_right = viewport[2] * 0.5f;
  72. float clip_top = -viewport[3] * 0.5f;
  73. float clip_bottom = viewport[3] * 0.5f;
  74. float clip_near = 0.0f;
  75. float clip_far = 1000.0f;
  76. camera.set_orthographic(clip_left, clip_right, clip_top, clip_bottom, clip_near, clip_far);
  77. energy_symbol.set_translation({viewport[2] * 0.25f, 0.0f, 0.0f});
  78. energy_symbol.update_tweens();
  79. // Resize modal BG
  80. modal_bg.set_scale({viewport[2] * 0.5f, viewport[3] * 0.5f, 1.0f});
  81. modal_bg.update_tweens();
  82. }
  83. void ui_system::set_tool_menu_control(control* control)
  84. {
  85. tool_menu_control = control;
  86. tool_menu_control->set_activated_callback(std::bind(&ui_system::open_tool_menu, this));
  87. tool_menu_control->set_deactivated_callback(std::bind(&ui_system::close_tool_menu, this));
  88. }
  89. void ui_system::handle_event(const mouse_moved_event& event)
  90. {
  91. if (tool_menu_control->is_active())
  92. {
  93. tool_selection_vector.x += event.dx;
  94. tool_selection_vector.y += event.dy;
  95. float max_length = 200.0f;
  96. float selection_threshold = 20.0f;
  97. int sector_count = 6;
  98. float sector_angle = vmq::two_pi<float> / static_cast<float>(sector_count);
  99. float length_squared = vmq::length_squared(tool_selection_vector);
  100. // Select tool if length of selection vector within threshold
  101. if (length_squared >= selection_threshold * selection_threshold)
  102. {
  103. // Limit length of tool selection vector
  104. if (length_squared > max_length * max_length)
  105. {
  106. tool_selection_vector = (tool_selection_vector / std::sqrt(length_squared)) * max_length;
  107. }
  108. float2 selection_direction = tool_selection_vector / std::sqrt(length_squared);
  109. float selection_angle = std::atan2(-selection_direction.y, selection_direction.x) - vmq::radians(90.0f);
  110. selection_angle = (selection_angle >= 0.0f ? selection_angle : (vmq::two_pi<float> + selection_angle));
  111. int sector = static_cast<int>((selection_angle + sector_angle * 0.5f) / sector_angle) % sector_count;
  112. float rotation_angle = static_cast<float>(sector) * sector_angle;
  113. tool_selector_bg.set_rotation(vmq::angle_axis(rotation_angle, {0, 0, 1}));
  114. tool_selector_bg.update_tweens();
  115. tool_selector_ant.set_rotation(vmq::angle_axis(rotation_angle + vmq::radians(180.0f), {0, 0, 1}) * vmq::angle_axis(vmq::radians(90.0f), {1, 0, 0}));
  116. }
  117. }
  118. mouse_position[0] = event.x;
  119. mouse_position[1] = event.y;
  120. }
  121. void ui_system::open_tool_menu()
  122. {
  123. scene.add_object(&modal_bg);
  124. scene.add_object(&tool_selector_bg);
  125. scene.add_object(&tool_selector_ant);
  126. tool_selection_vector = {0, 0};
  127. }
  128. void ui_system::close_tool_menu()
  129. {
  130. scene.remove_object(&modal_bg);
  131. scene.remove_object(&tool_selector_bg);
  132. scene.remove_object(&tool_selector_ant);
  133. }
  134. void ui_system::open_elevator_menu()
  135. {
  136. }
  137. void ui_system::close_elevator_menu()
  138. {
  139. }