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
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 " ) ;
t . gui . text_label_centered ( " Rectangle Select " ) ;
t . gui . text_label ( " Select from: " ) ;
if ( t . gui . radio_button_field ( " layer edited " , " Layer being edited " , drawP . controls . layerSelector = = DrawingProgramLayerManager : : LayerSelector : : LAYER_BEING_EDITED ) )
drawP . controls . layerSelector = DrawingProgramLayerManager : : LayerSelector : : LAYER_BEING_EDITED ;
if ( t . gui . radio_button_field ( " all visible " , " All visible layers " , drawP . controls . layerSelector = = DrawingProgramLayerManager : : LayerSelector : : ALL_VISIBLE_LAYERS ) )
drawP . controls . layerSelector = DrawingProgramLayerManager : : LayerSelector : : ALL_VISIBLE_LAYERS ;
t . gui . pop_id ( ) ;
2025-07-06 20:43:53 +03:00
}
2025-12-30 19:46:08 +03:00
void RectSelectTool : : erase_component ( CanvasComponentContainer : : ObjInfo * erasedComp ) {
2025-12-15 15:36:42 +03:00
}
2025-12-01 17:33:14 +03:00
bool RectSelectTool : : right_click_popup_gui ( Vector2f popupPos ) {
return drawP . selection_action_menu ( popupPos ) ;
}
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 ( ) {
switch ( controls . selectionMode ) {
case 0 : {
2025-12-29 15:04:32 +03:00
if ( drawP . controls . leftClick & & ! drawP . selection . is_being_transformed ( ) ) {
controls = RectSelectControls ( ) ;
controls . coords = drawP . world . drawData . cam . c ;
controls . selectStartAt = controls . coords . get_mouse_pos ( drawP . world ) ;
controls . selectionMode = 1 ;
}
2025-07-06 20:43:53 +03:00
break ;
}
case 1 : {
using namespace SCollision ;
Vector2f mPos = controls . coords . get_mouse_pos ( drawP . world ) ;
Vector2f newP1 = cwise_vec_min ( controls . selectStartAt , mPos ) ;
Vector2f newP2 = cwise_vec_max ( controls . selectStartAt , mPos ) ;
controls . newT = triangle_from_rect_points ( newP1 , newP2 ) ;
if ( ! drawP . controls . leftClickHeld ) {
2025-07-24 16:35:48 +03:00
ColliderCollection < float > cC ;
cC . triangle . emplace_back ( controls . newT [ 0 ] , controls . newT [ 1 ] , controls . newT [ 2 ] ) ;
cC . triangle . emplace_back ( controls . newT [ 2 ] , controls . newT [ 3 ] , controls . newT [ 0 ] ) ;
2025-08-02 14:42:53 +03:00
cC . recalculate_bounds ( ) ;
2025-07-06 20:43:53 +03:00
2025-12-29 15:04:32 +03:00
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 ) ;
}
2025-07-06 20:43:53 +03:00
controls . selectionMode = 0 ;
}
break ;
}
}
}
2025-07-27 16:53:22 +03:00
bool RectSelectTool : : prevent_undo_or_redo ( ) {
2025-12-29 15:04:32 +03:00
return drawP . selection . is_something_selected ( ) | | controls . selectionMode = = 1 ;
2025-07-27 16:53:22 +03:00
}
2025-07-06 20:43:53 +03:00
void RectSelectTool : : draw ( SkCanvas * canvas , const DrawData & drawData ) {
2025-07-24 16:35:48 +03:00
if ( controls . selectionMode = = 1 ) {
2025-07-06 20:43:53 +03:00
canvas - > save ( ) ;
controls . coords . transform_sk_canvas ( canvas , drawData ) ;
2025-07-24 16:35:48 +03:00
SkRect r = SkRect : : MakeLTRB ( controls . newT [ 0 ] . x ( ) , controls . newT [ 0 ] . y ( ) , controls . newT [ 2 ] . x ( ) , controls . newT [ 2 ] . y ( ) ) ;
2025-08-02 12:31:16 +03:00
canvas - > drawRect ( r , drawP . select_tool_line_paint ( ) ) ;
2025-07-06 20:43:53 +03:00
canvas - > restore ( ) ;
2025-07-24 16:35:48 +03:00
canvas - > restore ( ) ;
2025-07-06 20:43:53 +03:00
}
}