summaryrefslogtreecommitdiffstats
path: root/Project/Bus.cpp
blob: 847308da6e94ff94af9c758587a5598b882e4d81 (plain)
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
#include "Bus.h"

Bus::Bus() : Element() {}
Bus::Bus(wxPoint2DDouble position) : Element()
{
    m_width = 100.0;
    m_height = 5.0;
    SetPosition(position);
}

Bus::~Bus() {}
void Bus::Draw(wxPoint2DDouble translation, double scale) const
{
    // Draw selection (layer 1)
    if(m_selected) {
	    // If the object is selected, the matrix is reset to remove scale effects applied to it, thus keeping the
	    // edges with fixed sizes for all zoom levels.
	    glPushMatrix();
	    glLoadIdentity();
	    // The matrix was reset, so we must use screen coordinates (WorldToScreen).
	    wxPoint2DDouble screenPt = WorldToScreen(translation, scale);
	    glTranslated(screenPt.m_x, screenPt.m_y, 0.0);
	    glRotated(m_angle, 0.0, 0.0, 1.0);
	    glTranslated(-screenPt.m_x, -screenPt.m_y, 0.0);

	    glColor4d(0.0, 0.5, 1.0, 0.5);

	    wxPoint2DDouble pts[4] = {WorldToScreen(translation, scale, -(m_width / 2.0), -(m_height / 2.0)) -
	                                  wxPoint2DDouble(m_borderSize, m_borderSize),
	                              WorldToScreen(translation, scale, -(m_width / 2.0), (m_height / 2.0)) -
	                                  wxPoint2DDouble(m_borderSize, -m_borderSize),
	                              WorldToScreen(translation, scale, (m_width / 2.0), (m_height / 2.0)) -
	                                  wxPoint2DDouble(-m_borderSize, -m_borderSize),
	                              WorldToScreen(translation, scale, (m_width / 2.0), -(m_height / 2.0)) -
	                                  wxPoint2DDouble(-m_borderSize, m_borderSize)};
	    DrawRectangle(pts);
	    glPopMatrix();
	}
    // Draw element (layer 2)
    // Push the current matrix on stack.
    glPushMatrix();
    // Rotate the matrix around the object position.
    glTranslated(m_position.m_x, m_position.m_y, 0.0);
    glRotated(m_angle, 0.0, 0.0, 1.0);
    glTranslated(-m_position.m_x, -m_position.m_y, 0.0);

    glColor4d(0.0, 0.3, 1.0, 1.0);
    DrawRectangle(m_position, m_width, m_height);
    // Pop the old matrix back.
    glPopMatrix();

    // Draw pickbox (layer 3)
    if(m_showPickbox) {
	    glPushMatrix();
	    glLoadIdentity();

	    wxPoint2DDouble screenPt = WorldToScreen(translation, scale);
	    glTranslated(screenPt.m_x, screenPt.m_y, 0.0);
	    glRotated(m_angle, 0.0, 0.0, 1.0);
	    glTranslated(-screenPt.m_x, -screenPt.m_y, 0.0);

	    wxPoint2DDouble pbPosition[2] = {WorldToScreen(translation, scale, m_width / 2.0),
	                                     WorldToScreen(translation, scale, -m_width / 2.0)};
	    DrawPickbox(pbPosition[0]);
	    DrawPickbox(pbPosition[1]);

	    glPopMatrix();
	}
}

bool Bus::Contains(wxPoint2DDouble position) const
{
    wxPoint2DDouble ptR = RotateAtPosition(position, -m_angle);
    return m_rect.Contains(ptR);
}

bool Bus::Intersects(wxRect2DDouble rect) const
{
    if(m_angle == 0.0 || m_angle == 180.0) return m_rect.Intersects(rect);

    wxPoint2DDouble m_rectCorners[4] = {m_rect.GetLeftTop(), m_rect.GetLeftBottom(), m_rect.GetRightBottom(),
                                        m_rect.GetRightTop()};
    wxPoint2DDouble rectCorners[4] = {rect.GetLeftTop(), rect.GetLeftBottom(), rect.GetRightBottom(),
                                      rect.GetRightTop()};

    // Rotate the m_rect corners
    for(int i = 0; i < 4; i++) {
	    m_rectCorners[i] = RotateAtPosition(m_rectCorners[i], m_angle);
	}

    //[Ref] http://www.gamedev.net/page/resources/_/technical/game-programming/2d-rotated-rectangle-collision-r2604

    // Find the rectangles axis to project
    wxPoint2DDouble axis[4] = {m_rectCorners[3] - m_rectCorners[0], m_rectCorners[3] - m_rectCorners[2],
                               rectCorners[3] - rectCorners[0], rectCorners[3] - rectCorners[2]};

    // Calculate the projected points to each axis
    wxPoint2DDouble m_rectProjPts[4][4];  // [axis][corner]
    wxPoint2DDouble rectProjPts[4][4];    // [axis][corner]
    for(int i = 0; i < 4; i++) {
	    double den = axis[i].m_x * axis[i].m_x + axis[i].m_y * axis[i].m_y;
	    for(int j = 0; j < 4; j++) {
		    double m_rectProj = (m_rectCorners[j].m_x * axis[i].m_x + m_rectCorners[j].m_y * axis[i].m_y) / den;
		    double rectProj = (rectCorners[j].m_x * axis[i].m_x + rectCorners[j].m_y * axis[i].m_y) / den;

		    m_rectProjPts[i][j] = wxPoint2DDouble(m_rectProj * axis[i].m_x, m_rectProj * axis[i].m_y);
		    rectProjPts[i][j] = wxPoint2DDouble(rectProj * axis[i].m_x, rectProj * axis[i].m_y);
		}
	}

    // Calculate the scalar value to identify the max and min values on projections
    double m_rectScalar[4][4];  //[axis][corner]
    double rectScalar[4][4];    //[axis][corner]
    for(int i = 0; i < 4; i++) {
	    for(int j = 0; j < 4; j++) {
		    m_rectScalar[i][j] = m_rectProjPts[i][j].m_x * axis[i].m_x + m_rectProjPts[i][j].m_y * axis[i].m_y;
		    rectScalar[i][j] = rectProjPts[i][j].m_x * axis[i].m_x + rectProjPts[i][j].m_y * axis[i].m_y;
		}
	}
    // Identify the max and min values
    double m_rectMin[4];
    double m_rectMax[4];
    double rectMin[4];
    double rectMax[4];

    for(int i = 0; i < 4; i++) {
	    m_rectMax[i] = m_rectScalar[i][0];
	    rectMax[i] = rectScalar[i][0];
	    m_rectMin[i] = m_rectScalar[i][0];
	    rectMin[i] = rectScalar[i][0];

	    for(int j = 1; j < 4; j++) {
		    if(m_rectMax[i] < m_rectScalar[i][j]) m_rectMax[i] = m_rectScalar[i][j];
		    if(rectMax[i] < rectScalar[i][j]) rectMax[i] = rectScalar[i][j];

		    if(m_rectMin[i] > m_rectScalar[i][j]) m_rectMin[i] = m_rectScalar[i][j];
		    if(rectMin[i] > rectScalar[i][j]) rectMin[i] = rectScalar[i][j];
		}
	}

    // Check if any segment don't overlap
    for(int i = 0; i < 4; i++) {
	    if(!(rectMin[i] <= m_rectMax[i] && rectMax[i] >= m_rectMin[i])) return false;
	}

    return true;
    // return rect.Intersects(m_rect);
}
bool Bus::PickboxContains(wxPoint2DDouble position)
{
    m_activePickboxID = ID_PB_NONE;

    wxPoint2DDouble ptR = RotateAtPosition(position, -m_angle);

    wxPoint2DDouble center(m_position.m_x + m_width / 2.0, m_position.m_y);
    wxRect2DDouble rectRight(center.m_x - 5.0, center.m_y - 5.0, 10.0, 10.0);

    center = wxPoint2DDouble(m_position.m_x - m_width / 2.0, m_position.m_y);
    wxRect2DDouble rectLeft(center.m_x - 5.0, center.m_y - 5.0, 10.0, 10.0);

    if(rectRight.Contains(ptR)) {
	    m_activePickboxID = ID_PB_RIGHT;
	    return true;
	}
    if(rectLeft.Contains(ptR)) {
	    m_activePickboxID = ID_PB_LEFT;
	    return true;
	}

    return false;
}

wxCursor Bus::GetBestPickboxCursor() const
{
    double angle = m_angle;
    while(angle >= 157.5) angle -= 180.0;

    if(angle >= -22.5 && angle < 22.5)
	return wxCursor(wxCURSOR_SIZEWE);
    else if(angle >= 22.5 && angle < 67.5)
	return wxCursor(wxCURSOR_SIZENWSE);
    else if(angle >= 67.5 && angle < 112.5)
	return wxCursor(wxCURSOR_SIZENS);
    else if(angle >= 112.5 && angle < 157.5)
	return wxCursor(wxCURSOR_SIZENESW);

    return wxCursor(wxCURSOR_ARROW);
}

void Bus::MovePickbox(wxPoint2DDouble position)
{
    if(m_activePickboxID == ID_PB_NONE) return;

    wxPoint2DDouble ptR = RotateAtPosition(position, -m_angle);

    double dx = 0.0;
    if(m_activePickboxID == ID_PB_RIGHT)
	dx = ptR.m_x - m_position.m_x - m_width / 2.0;
    else if(m_activePickboxID == ID_PB_LEFT)
	dx = m_position.m_x - m_width / 2.0 - ptR.m_x;

    if(m_width + dx < 20.0) return;

    if(m_activePickboxID == ID_PB_RIGHT) {
	    m_position.m_x += (dx / 2.0) * std::cos(wxDegToRad(m_angle));
	    m_position.m_y += (dx / 2.0) * std::sin(wxDegToRad(m_angle));
	}
    else if(m_activePickboxID == ID_PB_LEFT)
	{
	    m_position.m_x -= (dx / 2.0) * std::cos(wxDegToRad(m_angle));
	    m_position.m_y -= (dx / 2.0) * std::sin(wxDegToRad(m_angle));
	}
    m_width += dx;

    SetPosition(m_position);
}

void Bus::Rotate()
{
    m_angle += m_rotationAngle;
    if(m_angle >= 360.0) m_angle = 0.0;
}

bool Bus::GetContextMenu(wxMenu& menu)
{
    menu.Append(ID_EDIT_BUS, _("Edit bus"));
    menu.Append(ID_ROTATE, _("Rotate"));
    return true;
}