File: clipping_box_interactor.cpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (234 lines) | stat: -rw-r--r-- 7,017 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/************************************************************************
 *
 * Copyright (C) 2019-2023 IRCAD France
 * Copyright (C) 2019-2021 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "viz/scene3d/interactor/clipping_box_interactor.hpp"

#include "viz/scene3d/layer.hpp"

namespace sight::viz::scene3d::interactor
{

//------------------------------------------------------------------------------

clipping_box_interactor::clipping_box_interactor(
    layer::sptr _layer,
    bool _layer_order_dependant,
    const std::string& _id,
    Ogre::SceneNode* _parent_scene_node,
    const Ogre::Matrix4& _clipping_matrix,
    const widget::clipping_box::clipping_update_callback_t& _clipping_update_cb,
    const std::string& _box_mtl_name,
    const std::string& _handle_mtl_name
) noexcept :
    base(_layer, _layer_order_dependant),
    m_widget(_id, _parent_scene_node, _layer->get_default_camera(), _layer->get_scene_manager(),
             _clipping_matrix, _clipping_update_cb, _box_mtl_name, _handle_mtl_name)
{
    SIGHT_ASSERT("This interactor must know its layer.", _layer);
}

//------------------------------------------------------------------------------

Ogre::MovableObject* clipping_box_interactor::pick_object(int _x, int _y)
{
    if(auto layer = m_layer.lock())
    {
        const auto result = viz::scene3d::utils::pick_object(_x, _y, 0xFFFFFFFF, *layer->get_scene_manager());

        return result.has_value() ? result->first : nullptr;
    }

    return nullptr;
}

//------------------------------------------------------------------------------

void clipping_box_interactor::mouse_move_event(
    mouse_button _button,
    modifier /*_mods*/,
    int _x,
    int _y,
    int _dx,
    int _dy
)
{
    if(m_widget.get_visibility()) // If a widget is present in the scene.
    {
        bool interacted = m_picked_object != nullptr;

        if(_button == left && interacted)
        {
            m_widget.widget_picked(m_picked_object, _x, _y);
        }
        else if(_button == middle)
        {
            interacted = m_widget.move_clipping_box(_x, _y, -_dx, -_dy);
        }
        else if(_button == right)
        {
            interacted = m_widget.scale_clipping_box(_x, _y, _dy);
        }

        if(interacted)
        {
            this->cancel_further_layer_interactions();
            m_layer.lock()->request_render();
        }
    }
}

//------------------------------------------------------------------------------

void clipping_box_interactor::button_release_event(mouse_button /*_button*/, modifier /*_mods*/, int /*_x*/, int /*_y*/)
{
    if(m_widget.get_visibility())
    {
        m_widget.widget_released();
        m_picked_object = nullptr;
        m_layer.lock()->request_render();
    }
}

//------------------------------------------------------------------------------

void clipping_box_interactor::button_press_event(mouse_button _button, modifier /*_mods*/, int _x, int _y)
{
    if(m_widget.get_visibility())
    {
        bool interacted = false;
        if(_button == left)
        {
            m_picked_object = pick_object(_x, _y);

            interacted = m_widget.belongs_to_widget(m_picked_object);
            if(interacted)
            {
                m_widget.widget_picked(m_picked_object, _x, _y);
            }
            else
            {
                m_picked_object = nullptr;
            }
        }
        else if(_button == middle)
        {
            interacted = m_widget.move_clipping_box(_x, _y, 0, 0);
        }
        else if(_button == right)
        {
            interacted = m_widget.scale_clipping_box(_x, _y, 0);
        }

        if(interacted)
        {
            this->cancel_further_layer_interactions();
            m_layer.lock()->request_render();
        }
    }
}

//------------------------------------------------------------------------------

void clipping_box_interactor::pinch_gesture_event(double _scale_factor, int _center_x, int _center_y)
{
    if(m_widget.get_visibility())
    {
        // Convert back the scale from "delta"
        if(_scale_factor < 0)
        {
            _scale_factor = -1.0 / _scale_factor;
        }

        const auto dy = int(((_center_y * _scale_factor) - _center_y) * 0.5);

        if(m_widget.scale_clipping_box(_center_x, _center_y, dy) || m_picked_object != nullptr)
        {
            cancel_further_layer_interactions();
        }
    }
}

//------------------------------------------------------------------------------

void clipping_box_interactor::pan_gesture_move_event(int _x, int _y, int _dx, int _dy)
{
    if(m_widget.get_visibility())
    {
        if(m_widget.move_clipping_box(_x, _y, -_dx, -_dy) || m_picked_object != nullptr)
        {
            cancel_further_layer_interactions();
        }
    }
}

//------------------------------------------------------------------------------

void clipping_box_interactor::pan_gesture_release_event(int /*_x*/, int /*_y*/, int /*_dx*/, int /*_dy*/)
{
    if(m_widget.get_visibility())
    {
        m_widget.widget_released();
        m_picked_object = nullptr;
    }
}

//------------------------------------------------------------------------------

void clipping_box_interactor::set_box_visibility(bool _visibility)
{
    m_widget.set_visibility(_visibility);
    m_layer.lock()->request_render();
}

//------------------------------------------------------------------------------

Ogre::AxisAlignedBox clipping_box_interactor::get_clipping_box() const
{
    return m_widget.get_clipping_box();
}

//------------------------------------------------------------------------------

Ogre::Matrix4 clipping_box_interactor::get_clipping_transform() const
{
    return m_widget.get_clipping_transform();
}

//------------------------------------------------------------------------------

void clipping_box_interactor::update_from_transform(const Ogre::Matrix4& _clipping_trf)
{
    m_widget.update_from_transform(_clipping_trf);
}

//------------------------------------------------------------------------------

void clipping_box_interactor::cancel_further_layer_interactions()
{
    const auto layer = m_layer.lock();
    if(layer)
    {
        layer->cancel_further_interaction();
    }
}

} // namespace sight::viz::scene3d::interactor