File size: 8,822 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*

  Author(s):	Balazs Vagvolgyi, Simon DiMaio, Anton Deguet
  Created on:	2008-06-10

  (C) Copyright 2008 Johns Hopkins University (JHU), All Rights
  Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/

#include <cisstOSAbstraction/osaSleep.h>

#include <cisst3DUserInterface/ui3BehaviorBase.h>
#include <cisst3DUserInterface/ui3SceneManager.h>
#include <cisst3DUserInterface/ui3VTKRenderer.h>
#include <cisst3DUserInterface/ui3VideoInterfaceFilter.h>
#include <cisst3DUserInterface/ui3Manager.h>
#include <cisst3DUserInterface/ui3Widget3D.h>


CMN_IMPLEMENT_SERVICES(ui3BehaviorBase)


ui3BehaviorBase::ui3BehaviorBase(const std::string & name,
                                 ui3Manager * manager):
    mtsTaskContinuous(name, 500), /* all behaviors have a state table of 500, ui3Manager needs at least 3 */
    MenuBar(0),
    State(Background),
    Manager(manager)
{
}


ui3BehaviorBase::~ui3BehaviorBase()
{
    for (unsigned int i = 0; i < Streams.size(); i ++) {
        if (Streams[i]) delete Streams[i];
    }
}


void ui3BehaviorBase::AddWidget3D(ui3Widget3D * widget3D)
{
    this->Widget3Ds.push_back(widget3D);

    unsigned int handleCounter;
    for (handleCounter = 0;
         handleCounter < 4;
         handleCounter++) {
        this->AddSelectable(widget3D->SideHandles[handleCounter]);
        this->AddSelectable(widget3D->CornerHandles[handleCounter]);
    }

}


void ui3BehaviorBase::AddSelectable(ui3Selectable * selectable)
{
    this->Selectables.push_back(selectable);
}


void ui3BehaviorBase::AddMenuBar(bool isManager)
{
    CMN_ASSERT(this->Manager);

    // create the menu bar
    this->MenuBar = new ui3MenuBar(this->GetName());
    CMN_ASSERT(this->MenuBar);

    // add a button in the main menu bar with callback
    if (!isManager) {
        this->MenuBar->AddClickButton("Hide",
                                      0,
                                      "iconify-left.png",
                                      &ui3BehaviorBase::SetStateBackground,
                                      this);
        this->MenuBar->AddClickButton("Close",
                                      99,
                                      "close.png",
                                      &ui3BehaviorBase::SetStateIdle,
                                      this);
    }
}


ui3BehaviorBase::StateType ui3BehaviorBase::GetState(void) const
{
    return this->State;
}

ui3Manager * ui3BehaviorBase::GetManager(void)
{
    return this->Manager;
}

ui3SceneManager * ui3BehaviorBase::GetSceneManager(void)
{
    return this->GetManager()->GetSceneManager();
}

void ui3BehaviorBase::Configure(const std::string & CMN_UNUSED(configFile))
{
    CMN_LOG_CLASS_INIT_WARNING << "Configure: use default configure from base class, doing nothing" << std::endl;
}

bool ui3BehaviorBase::SaveConfiguration(const std::string & CMN_UNUSED(configFile))
{
    CMN_LOG_CLASS_INIT_WARNING << "SaveConfiguration: use default save configuration from base class, doing nothing" << std::endl;
    return true;
}

void ui3BehaviorBase::Run(void)
{
    this->ProcessQueuedEvents();
    if (this->Manager->MastersAsMice()) {
        switch(this->State) {
        case Foreground:
            this->MenuBar->Show();
            this->RunForeground();
            break;
        case Background:
            this->RunBackground();
            break;
        case Idle:
        default:
            break;
        }
    } else {
        switch(this->State) {
        case Foreground:
        case Background:
            this->RunNoInput();
            break;
        case Idle:
        default:
            break;
        }
    }
    osaSleep(50.0 * cmn_ms); // bad, needs to be a variable/data member
}


void ui3BehaviorBase::SetStateForeground(void)
{
    this->SetState(Foreground);
    this->SetStateForegroundCallback();
}


void ui3BehaviorBase::SetStateBackground(void)
{
    this->SetState(Background);
    this->SetStateBackgroundCallback();
}


void ui3BehaviorBase::SetStateIdle(void)
{
    this->SetState(Idle);
    this->SetStateIdleCallback();
}


void ui3BehaviorBase::SetState(const StateType & newState)
{
    if (newState != this->State) {
        switch (newState) {
            case Foreground:
                if (this->Manager->ActiveBehavior) {
                    this->Manager->ActiveBehavior->MenuBar->Hide();
                    this->Manager->ActiveBehavior->State = Background;
                }
                this->MenuBar->Show();
                this->Manager->ActiveBehavior = this;
                if (this->State == Idle) {
                    this->OnStart();
                }
                break;
            case Background:
                this->MenuBar->Hide();
                this->Manager->ActiveBehavior = this->Manager;
                this->Manager->State = Foreground;
                this->Manager->ActiveBehavior->MenuBar->Show();
                break;
            case Idle:
                this->MenuBar->Hide();
                if (this->GetVisibleObject()) {
                    this->GetVisibleObject()->Hide();
                }
                this->Manager->ActiveBehavior = this->Manager;
                this->Manager->State = Foreground;
                this->Manager->ActiveBehavior->MenuBar->Show();
                this->OnQuit();
                break;
        }
        this->State = newState;
    }
}


void ui3BehaviorBase::OnStreamSample(svlSample * CMN_UNUSED(sample), size_t CMN_UNUSED(streamindex))
{
    // Default implementation does nothing
}


size_t ui3BehaviorBase::AddStream(svlStreamType type, const std::string & streamname)
{
    size_t streamindex = Streams.size();
    ui3VideoInterfaceFilter* filter = new ui3VideoInterfaceFilter(type, streamindex, this);
    CMN_ASSERT(filter);

    Streams.resize(streamindex + 1);
    StreamNames.resize(streamindex + 1);

    Streams[streamindex] = filter;
    StreamNames[streamindex] = streamname;

    return streamindex;
}


unsigned int ui3BehaviorBase::GetStreamWidth(const size_t streamindex, unsigned int channel)
{
    if (streamindex < 0 || streamindex >= static_cast<int>(Streams.size())) return 0;
    return Streams[streamindex]->GetWidth(channel);
}


unsigned int ui3BehaviorBase::GetStreamWidth(const std::string & streamname, unsigned int channel)
{
    return GetStreamWidth(GetStreamIndexFromName(streamname), channel);
}


unsigned int ui3BehaviorBase::GetStreamHeight(const int streamindex, unsigned int channel)
{
    if (streamindex < 0 || streamindex >= static_cast<int>(Streams.size())) return 0;
    return Streams[streamindex]->GetHeight(channel);
}


unsigned int ui3BehaviorBase::GetStreamHeight(const std::string & streamname, unsigned int channel)
{
    return GetStreamHeight(GetStreamIndexFromName(streamname), channel);
}


int ui3BehaviorBase::GetStreamIndexFromName(const std::string & streamname)
{
    unsigned int i;
    for (i = 0; i < StreamNames.size(); i ++) {
        if (StreamNames[i] == streamname) break;
    }
    if (i < StreamNames.size()) return i;
    return -1;
}


svlFilterBase * ui3BehaviorBase::GetStreamSamplerFilter(const std::string & streamname)
{
    int streamindex = GetStreamIndexFromName(streamname);
    if (streamindex < 0 || streamindex >= static_cast<int>(Streams.size())) return 0;
    return dynamic_cast<svlFilterBase*>(Streams[streamindex]);
}


void ui3BehaviorBase::PrimaryMasterButtonCallback(const prmEventButton & CMN_UNUSED(event))
{
    CMN_LOG_RUN_VERBOSE << "PrimaryMasterButtonCallback not overloaded for \""
                        << this->GetName() << "\"" << std::endl;
}


void ui3BehaviorBase::SecondaryMasterButtonCallback(const prmEventButton & CMN_UNUSED(event))
{
    CMN_LOG_RUN_VERBOSE << "SecondaryMasterButtonCallback not overloaded for \""
                        << this->GetName() << "\"" << std::endl;
}


void ui3BehaviorBase::SetStateIdleCallback(void)
{
    CMN_LOG_RUN_VERBOSE << "SetStateIdleCallback not overloaded for \""
                        << this->GetName() << "\"" << std::endl;
}


void ui3BehaviorBase::SetStateForegroundCallback(void)
{
    CMN_LOG_RUN_VERBOSE << "SetStateForegroundCallback not overloaded for \""
                        << this->GetName() << "\"" << std::endl;
}


void ui3BehaviorBase::SetStateBackgroundCallback(void)
{
    CMN_LOG_RUN_VERBOSE << "SetStateBackgroundCallback not overloaded for \""
                        << this->GetName() << "\"" << std::endl;
}