File size: 4,125 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
/* -*- 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-2012 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 <cisstConfig.h>
#include <cisstCommon/cmnPath.h>
#include <cisst3DUserInterface/ui3MenuButton.h>

#include <vtkPNGReader.h>
#include <vtkTexture.h>
#include <vtkPlaneSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkImageData.h>

ui3MenuButton::~ui3MenuButton()
{
}


bool ui3MenuButton::CreateVTKObjects(void)
{
    this->PNGReader = vtkPNGReader::New();
    CMN_ASSERT(this->PNGReader);
    cmnPath path;
    // this is were the icons have been copied by CMake post build rule
    path.AddRelativeToCisstShare("cisst3DUserInterface/icons");
    // in case the user provided a full path, search from / as well
    path.Add("/", cmnPath::TAIL);
    std::string iconFullName = path.Find(this->IconFile, cmnPath::READ);
    if (iconFullName == "") {
        CMN_LOG_INIT_WARNING << "CreateVTKObjects: can find \"" << this->IconFile
                             << "\" in path: " << path << std::endl;
    }
    this->PNGReader->SetFileName(iconFullName.c_str());

    // Create a texture object for the button.
    this->Texture = vtkTexture::New();
    CMN_ASSERT(this->Texture);
    this->Texture->SetInput(this->PNGReader->GetOutput());
    this->Texture->InterpolateOn();

    // Create the button plane
    this->PlaneSource = vtkPlaneSource::New();
    CMN_ASSERT(this->PlaneSource);
    double halfSize = this->ButtonSize / 2.0;
    this->PlaneSource->SetOrigin(-halfSize, -halfSize, 0.0);
    this->PlaneSource->SetPoint1(+halfSize, -halfSize, 0.0);
    this->PlaneSource->SetPoint2(-halfSize, +halfSize, 0.0);
    this->PlaneSource->SetCenter(0.0, 0.0, 0.0);
    this->PlaneSource->SetXResolution(1.0);
    this->PlaneSource->SetYResolution(1.0);

    // Create a polygon mapper for the button.
    this->Mapper = vtkPolyDataMapper::New();
    CMN_ASSERT(this->Mapper);
    this->Mapper->SetInput(this->PlaneSource->GetOutput());
    this->Mapper->ImmediateModeRenderingOn();

    // Create an actor for the button.
    this->Actor = vtkActor::New();
    CMN_ASSERT(this->Actor);
    this->Actor->SetMapper(this->Mapper);
    this->Actor->SetTexture(this->Texture);
    this->Actor->GetProperty()->SetOpacity(0.5);
    this->Actor->VisibilityOn();
	return true;
}


vtkProp3D * ui3MenuButton::GetVTKProp(void)
{
    return this->Actor;
}


void ui3MenuButton::SetCheck(bool state)
{
}

void ui3MenuButton::SetPush(bool state)
{
}

void ui3MenuButton::SetHighlight(bool state)
{
    if (state) {
        this->Actor->GetProperty()->SetOpacity(1.0);
    } else {
        this->Actor->GetProperty()->SetOpacity(0.5);
    }
}

void ui3MenuButton::SetEnable(bool state)
{
}

bool ui3MenuButton::GetCheck()
{
    return CheckState;
}

bool ui3MenuButton::GetPush()
{
    return PushState;
}

bool ui3MenuButton::GetHighlight()
{
    return HighlightState;
}

bool ui3MenuButton::GetEnable()
{
    return EnableState;
}

vtkActor * ui3MenuButton::VTKPointer(void)
{
    return this->Actor;
}

void ui3MenuButton::SetPosition(vct3 & position)
{
    // compute bounding box
    double halfSize = this->ButtonSize / 2.0;
    this->BottomLeft2D.X() = position.X() - halfSize ;
    this->BottomLeft2D.Y() = -halfSize;
    this->TopRight2D.X() = position.X() + halfSize;
    this->TopRight2D.Y() = +halfSize;
    // position actor wrt menu reference frame
    this->Actor->SetPosition(position.Pointer());
}

bool ui3MenuButton::IsCursorOver(const vct2 & cursor2D)
{
    return (cursor2D.GreaterOrEqual(this->BottomLeft2D)
            && cursor2D.LesserOrEqual(this->TopRight2D));
}