2025-07-06 20:43:53 +03:00
|
|
|
#include "RectSelectTool.hpp"
|
2025-12-26 10:40:58 +03:00
|
|
|
#include "../DrawingProgram.hpp"
|
|
|
|
|
#include "../../MainProgram.hpp"
|
|
|
|
|
#include "../../DrawData.hpp"
|
2025-08-02 20:10:33 +03:00
|
|
|
#include "DrawingProgramToolBase.hpp"
|
2025-07-06 20:43:53 +03:00
|
|
|
#include "Helpers/MathExtras.hpp"
|
|
|
|
|
#include "Helpers/SCollision.hpp"
|
2025-12-26 10:40:58 +03:00
|
|
|
#include "../../CoordSpaceHelper.hpp"
|
2025-07-06 20:43:53 +03:00
|
|
|
|
2026-03-30 12:32:41 +03:00
|
|
|
#include "../../GUIStuff/ElementHelpers/TextLabelHelpers.hpp"
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
RectSelectTool::RectSelectTool(DrawingProgram& initDrawP):
|
2025-08-02 20:10:33 +03:00
|
|
|
DrawingProgramToolBase(initDrawP)
|
2025-07-06 20:43:53 +03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 20:10:33 +03:00
|
|
|
DrawingProgramToolType RectSelectTool::get_type() {
|
|
|
|
|
return DrawingProgramToolType::RECTSELECT;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
void RectSelectTool::gui_toolbox() {
|
2025-12-29 15:04:32 +03:00
|
|
|
auto& t = drawP.world.main.toolbar;
|
|
|
|
|
t.gui.push_id("rectangle select tool");
|
2026-03-30 12:32:41 +03:00
|
|
|
GUIStuff::ElementHelpers::text_label_centered(t.gui, "Rectangle Select");
|
2026-01-27 16:55:09 +03:00
|
|
|
drawP.selection.selection_gui();
|
2025-12-29 15:04:32 +03:00
|
|
|
t.gui.pop_id();
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
2026-03-02 16:36:34 +03:00
|
|
|
void RectSelectTool::input_key_callback(const InputManager::KeyCallbackArgs& key) {
|
|
|
|
|
drawP.selection.input_key_callback_modify_selection(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RectSelectTool::input_mouse_button_on_canvas_callback(const InputManager::MouseButtonCallbackArgs& button) {
|
|
|
|
|
drawP.selection.input_mouse_button_on_canvas_callback_modify_selection(button);
|
2026-03-02 23:09:48 +03:00
|
|
|
if(!controls.isSelecting && button.button == InputManager::MouseButton::LEFT && button.down && !drawP.selection.is_being_transformed()) {
|
2026-03-02 16:36:34 +03:00
|
|
|
controls = RectSelectControls();
|
|
|
|
|
controls.coords = drawP.world.drawData.cam.c;
|
|
|
|
|
controls.selectStartAt = controls.selectEndAt = button.pos;
|
|
|
|
|
controls.isSelecting = true;
|
|
|
|
|
}
|
2026-03-02 23:09:48 +03:00
|
|
|
else if(controls.isSelecting && button.button == InputManager::MouseButton::LEFT && !button.down) {
|
2026-03-02 16:36:34 +03:00
|
|
|
using namespace SCollision;
|
2026-03-03 10:18:47 +03:00
|
|
|
controls.selectEndAt = controls.coords.from_cam_space_to_this(drawP.world, button.pos);
|
|
|
|
|
Vector2f newP1 = controls.coords.from_this_to_cam_space(drawP.world, cwise_vec_min(controls.selectStartAt, controls.selectEndAt));
|
|
|
|
|
Vector2f newP2 = controls.coords.from_this_to_cam_space(drawP.world, cwise_vec_max(controls.selectStartAt, controls.selectEndAt));
|
2026-03-02 16:36:34 +03:00
|
|
|
std::array<Vector2f, 4> newT = triangle_from_rect_points(newP1, newP2);
|
|
|
|
|
|
|
|
|
|
ColliderCollection<float> cC;
|
|
|
|
|
cC.triangle.emplace_back(newT[0], newT[1], newT[2]);
|
|
|
|
|
cC.triangle.emplace_back(newT[2], newT[3], newT[0]);
|
|
|
|
|
cC.recalculate_bounds();
|
|
|
|
|
|
|
|
|
|
if(drawP.world.main.input.key(InputManager::KEY_GENERIC_LSHIFT).held)
|
|
|
|
|
drawP.selection.add_from_cam_coord_collider_to_selection(cC, drawP.controls.layerSelector, false);
|
|
|
|
|
else if(drawP.world.main.input.key(InputManager::KEY_GENERIC_LALT).held)
|
|
|
|
|
drawP.selection.remove_from_cam_coord_collider_to_selection(cC, drawP.controls.layerSelector, false);
|
|
|
|
|
else {
|
|
|
|
|
drawP.selection.deselect_all();
|
|
|
|
|
drawP.selection.add_from_cam_coord_collider_to_selection(cC, drawP.controls.layerSelector, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
controls.isSelecting = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RectSelectTool::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) {
|
|
|
|
|
if(controls.isSelecting)
|
2026-03-03 10:18:47 +03:00
|
|
|
controls.selectEndAt = controls.coords.from_cam_space_to_this(drawP.world, motion.pos);
|
2026-03-02 16:36:34 +03:00
|
|
|
drawP.selection.input_mouse_motion_callback_modify_selection(motion);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 19:46:08 +03:00
|
|
|
void RectSelectTool::erase_component(CanvasComponentContainer::ObjInfo* erasedComp) {
|
2025-12-15 15:36:42 +03:00
|
|
|
}
|
|
|
|
|
|
2026-03-30 12:32:41 +03:00
|
|
|
void RectSelectTool::right_click_popup_gui(Vector2f popupPos) {
|
|
|
|
|
drawP.selection_action_menu(popupPos);
|
2025-12-01 17:33:14 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-02 20:39:21 +03:00
|
|
|
void RectSelectTool::switch_tool(DrawingProgramToolType newTool) {
|
2025-12-29 15:04:32 +03:00
|
|
|
if(!drawP.is_selection_allowing_tool(newTool))
|
|
|
|
|
drawP.selection.deselect_all();
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RectSelectTool::tool_update() {
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-27 16:53:22 +03:00
|
|
|
bool RectSelectTool::prevent_undo_or_redo() {
|
2026-03-02 16:36:34 +03:00
|
|
|
return drawP.selection.is_something_selected() || controls.isSelecting;
|
2025-07-27 16:53:22 +03:00
|
|
|
}
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
void RectSelectTool::draw(SkCanvas* canvas, const DrawData& drawData) {
|
2026-03-02 16:36:34 +03:00
|
|
|
if(controls.isSelecting) {
|
|
|
|
|
Vector2f mPos = controls.selectEndAt;
|
|
|
|
|
Vector2f newP1 = cwise_vec_min(controls.selectStartAt, mPos);
|
|
|
|
|
Vector2f newP2 = cwise_vec_max(controls.selectStartAt, mPos);
|
|
|
|
|
std::array<Vector2f, 4> newT = triangle_from_rect_points(newP1, newP2);
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
canvas->save();
|
|
|
|
|
controls.coords.transform_sk_canvas(canvas, drawData);
|
|
|
|
|
|
2026-03-02 16:36:34 +03:00
|
|
|
SkRect r = SkRect::MakeLTRB(newT[0].x(), newT[0].y(), newT[2].x(), newT[2].y());
|
2026-03-20 20:53:59 +03:00
|
|
|
canvas->drawRect(r, drawP.select_tool_line_paint(drawData));
|
2025-07-06 20:43:53 +03:00
|
|
|
|
2025-07-24 16:35:48 +03:00
|
|
|
canvas->restore();
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
}
|