💿🐜 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.

192 lines
5.3 KiB

  1. /*
  2. * Copyright (C) 2021 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.hpp"
  20. #include "resources/resource-manager.hpp"
  21. namespace entity {
  22. namespace system {
  23. ui::ui(::resource_manager* resource_manager):
  24. resource_manager(resource_manager),
  25. tool_menu_control(nullptr),
  26. camera(nullptr),
  27. scene_collection(nullptr)
  28. {
  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<gl::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 scene
  46. //scene.add_object(&camera);
  47. //scene.add_object(&indirect_light);
  48. //scene.add_object(&direct_light);
  49. }
  50. void ui::update(float dt)
  51. {}
  52. void ui::set_viewport(const float4& viewport)
  53. {
  54. this->viewport = viewport;
  55. // Calculate viewport center
  56. viewport_center[0] = (viewport[2] - viewport[0]) * 0.5f;
  57. viewport_center[1] = (viewport[3] - viewport[1]) * 0.5f;
  58. // Resize modal BG
  59. modal_bg.set_scale({viewport[2] * 0.5f, viewport[3] * 0.5f, 1.0f});
  60. modal_bg.update_tweens();
  61. update_projection();
  62. }
  63. void ui::set_tool_menu_control(input::control* control)
  64. {
  65. tool_menu_control = control;
  66. if (tool_menu_control)
  67. {
  68. tool_menu_control->set_activated_callback(std::bind(&ui::open_tool_menu, this));
  69. tool_menu_control->set_deactivated_callback(std::bind(&ui::close_tool_menu, this));
  70. }
  71. }
  72. void ui::set_camera(scene::camera* camera)
  73. {
  74. this->camera = camera;
  75. if (camera)
  76. {
  77. camera->look_at({0.0f, 0.0f, 500.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f});
  78. update_projection();
  79. }
  80. }
  81. void ui::set_scene(scene::collection* collection)
  82. {
  83. this->scene_collection = collection;
  84. }
  85. void ui::handle_event(const mouse_moved_event& event)
  86. {
  87. if (tool_menu_control && tool_menu_control->is_active())
  88. {
  89. tool_selection_vector.x += event.dx;
  90. tool_selection_vector.y += event.dy;
  91. float max_length = 200.0f;
  92. float selection_threshold = 20.0f;
  93. int sector_count = 6;
  94. float sector_angle = math::two_pi<float> / static_cast<float>(sector_count);
  95. float length_squared = math::length_squared(tool_selection_vector);
  96. // Select tool if length of selection vector within threshold
  97. if (length_squared >= selection_threshold * selection_threshold)
  98. {
  99. // Limit length of tool selection vector
  100. if (length_squared > max_length * max_length)
  101. {
  102. tool_selection_vector = (tool_selection_vector / std::sqrt(length_squared)) * max_length;
  103. }
  104. float2 selection_direction = tool_selection_vector / std::sqrt(length_squared);
  105. float selection_angle = std::atan2(-selection_direction.y, selection_direction.x) - math::radians(90.0f);
  106. selection_angle = (selection_angle >= 0.0f ? selection_angle : (math::two_pi<float> + selection_angle));
  107. int sector = static_cast<int>((selection_angle + sector_angle * 0.5f) / sector_angle) % sector_count;
  108. float rotation_angle = static_cast<float>(sector) * sector_angle;
  109. tool_selector_bg.set_rotation(math::angle_axis(rotation_angle, {0, 0, 1}));
  110. tool_selector_bg.update_tweens();
  111. }
  112. }
  113. mouse_position[0] = event.x;
  114. mouse_position[1] = event.y;
  115. }
  116. void ui::handle_event(const window_resized_event& event)
  117. {
  118. set_viewport({0.0f, 0.0f, static_cast<float>(event.w), static_cast<float>(event.h)});
  119. }
  120. void ui::update_projection()
  121. {
  122. if (camera)
  123. {
  124. float clip_left = -viewport[2] * 0.5f;
  125. float clip_right = viewport[2] * 0.5f;
  126. float clip_top = -viewport[3] * 0.5f;
  127. float clip_bottom = viewport[3] * 0.5f;
  128. float clip_near = 0.0f;
  129. float clip_far = 1000.0f;
  130. camera->set_orthographic(clip_left, clip_right, clip_top, clip_bottom, clip_near, clip_far);
  131. }
  132. }
  133. void ui::open_tool_menu()
  134. {
  135. if (scene_collection)
  136. {
  137. scene_collection->add_object(&modal_bg);
  138. scene_collection->add_object(&tool_selector_bg);
  139. }
  140. tool_selection_vector = {0, 0};
  141. }
  142. void ui::close_tool_menu()
  143. {
  144. if (scene_collection)
  145. {
  146. scene_collection->remove_object(&modal_bg);
  147. scene_collection->remove_object(&tool_selector_bg);
  148. }
  149. }
  150. void ui::open_elevator_menu()
  151. {
  152. }
  153. void ui::close_elevator_menu()
  154. {
  155. }
  156. } // namespace system
  157. } // namespace entity