trackballnavigationeventlistener.cpp 6.92 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2013, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22
23
24
// 
// ================================================================================================

25
#include "trackballnavigationeventlistener.h"
26
27
#include "tgt/assert.h"
#include "tgt/event/mouseevent.h"
28
#include "core/datastructures/abstractdata.h"
29
#include "core/properties/cameraproperty.h"
30
#include "core/pipeline/visualizationprocessor.h"
31

schultezub's avatar
schultezub committed
32
namespace campvis {
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

    CamPropNavigationWrapper::CamPropNavigationWrapper(CameraProperty* camProp)
        : _cameraProperty(camProp)
        , _dirty(false)
    {

    }

    CamPropNavigationWrapper::~CamPropNavigationWrapper() {

    }

    tgt::Camera* CamPropNavigationWrapper::getCamera() {
        if (! _dirty) {
            // perform deep copy (hopefully thread-safe...)
            _localCopy = _cameraProperty->getValue();
            _dirty = true;
        }
        return &_localCopy;
    }

    void CamPropNavigationWrapper::update() {
        _cameraProperty->setValue(_localCopy);
        _dirty = false;
    }

59
// = TrackballNavigationEventListener ==============================================================
60

61
    const std::string TrackballNavigationEventListener::loggerCat_ = "CAMPVis.core.eventhandler.TrackballNavigationEventListener";
62

63
    TrackballNavigationEventListener::TrackballNavigationEventListener(CameraProperty* cameraProperty, IVec2Property* viewportSizeProp)
64
        : tgt::EventListener()
65
        , _cameraProperty(cameraProperty)
66
        , _viewportSizeProp(viewportSizeProp)
67
68
        , _cpnw(cameraProperty)
        , _trackball(0)
69
    {
70
71
72
73
74
        tgtAssert(_cameraProperty != 0, "Assigned camera property must not be 0.");
        tgtAssert(_viewportSizeProp != 0, "Assigned viewport size property must not be 0.");

        _trackball = new tgt::Trackball(&_cpnw, viewportSizeProp->getValue());
        _viewportSizeProp->s_changed.connect(this, &TrackballNavigationEventListener::onViewportSizePropChanged);
75
76
    }

77
    TrackballNavigationEventListener::~TrackballNavigationEventListener() {
78
        _viewportSizeProp->s_changed.disconnect(this);
79
        delete _trackball;
80
81
    }

82
    void TrackballNavigationEventListener::onEvent(tgt::Event* e) {
83
84
        if (typeid(*e) == typeid(tgt::MouseEvent)) {
            tgt::MouseEvent* me = static_cast<tgt::MouseEvent*>(e);
85
            if (me->action() == tgt::MouseEvent::PRESSED) {
86
87
                for (std::vector<VisualizationProcessor*>::iterator it = _lqModeProcessors.begin(); it != _lqModeProcessors.end(); ++it)
                    (*it)->p_lqMode.setValue(true);
88
                _trackball->mousePressEvent(me);
89
90
            }
            else if (me->action() == tgt::MouseEvent::RELEASED) {
91
92
                for (std::vector<VisualizationProcessor*>::iterator it = _lqModeProcessors.begin(); it != _lqModeProcessors.end(); ++it)
                    (*it)->p_lqMode.setValue(false);
93
                _trackball->mouseReleaseEvent(me);
94
            }
95
96
97
98
99
100
101
102
            else if (me->action() == tgt::MouseEvent::MOTION)
                _trackball->mouseMoveEvent(me);
            else if (me->action() == tgt::MouseEvent::WHEEL)
                _trackball->wheelEvent(me);
        }
        else if (typeid(*e) == typeid(tgt::KeyEvent)) {
            _trackball->keyEvent(static_cast<tgt::KeyEvent*>(e));
        }
103
104
    }

105
    void TrackballNavigationEventListener::reinitializeCamera(const tgt::vec3& position, const tgt::vec3& focus, const tgt::vec3& upVector) {
106
107
        _trackball->reinitializeCamera(position, focus, upVector);
    }
108

109
110
111
112
113
114
115
116
117
118
119
120
121
    void TrackballNavigationEventListener::reinitializeCamera(const IHasWorldBounds* hwb) {
        reinitializeCamera(hwb->getWorldBounds());
    }

    void TrackballNavigationEventListener::reinitializeCamera(const tgt::Bounds& worldBounds) {
        if (_sceneBounds != worldBounds) {
            tgt::vec3 pos = worldBounds.center() - tgt::vec3(0, 0, tgt::length(worldBounds.diagonal()));

            setSceneBounds(worldBounds);
            setCenter(worldBounds.center());
            reinitializeCamera(pos, worldBounds.center(), _cameraProperty->getValue().getUpVector());
        }
    }
122
    
123
    void TrackballNavigationEventListener::setCenter(const tgt::vec3& center) {
124
        _trackball->setCenter(center);
125
    }
126
    
127
    void TrackballNavigationEventListener::setSceneBounds(const tgt::Bounds& bounds) {
128
        _sceneBounds = bounds;
129
130
131
        _trackball->setSceneBounds(bounds);
    }

132
    const tgt::Bounds& TrackballNavigationEventListener::getSceneBounds() const {
133
134
135
        return _trackball->getSceneBounds();
    }

136
137
138
139
    tgt::Trackball* TrackballNavigationEventListener::getTrackball() {
        return _trackball;
    }

140
141
142
143
144
145
146
147
148
    void TrackballNavigationEventListener::addLqModeProcessor(VisualizationProcessor* vp) {
        tgtAssert(vp != 0, "Pointer to processor must not be 0.");
        tgtAssert(std::find(_lqModeProcessors.begin(), _lqModeProcessors.end(), vp) == _lqModeProcessors.end(), "Processor already in list of LQ mode processors.");

        _lqModeProcessors.push_back(vp);
    }

    void TrackballNavigationEventListener::removeLqModeProcessor(VisualizationProcessor* vp) {
        for (std::vector<VisualizationProcessor*>::iterator it = _lqModeProcessors.begin(); it != _lqModeProcessors.end(); ++it) {
149
            if (*it == vp) {
150
                _lqModeProcessors.erase(it);
151
152
                return;
            }
153
154
155
        }
    }

156
157
158
159
160
161
162
163
    void TrackballNavigationEventListener::onViewportSizePropChanged(const AbstractProperty* p) {
        tgtAssert(p == _viewportSizeProp, "Signal from unexpected property! Expected p == _viewportSizeProp.");

        _trackball->setViewprtSize(_viewportSizeProp->getValue());
        float ratio = static_cast<float>(_viewportSizeProp->getValue().x) / static_cast<float>(_viewportSizeProp->getValue().y);
        _cameraProperty->setWindowRatio(ratio);
    }

164
}