summaryrefslogtreecommitdiffstats
path: root/Project/Workspace.cpp
blob: ec27039c746e9599361c76737da12fa9bc53786a (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
#include "Workspace.h"
#include "MouseEventsHandler.h"

Camera::Camera()
{
	m_translation = wxPoint2DDouble(0,0);
	m_scale = 1.0;
}

Camera::~Camera()
{
}

wxPoint2DDouble Camera::ScreenToWorld(wxPoint2DDouble screenCoords)
{
	return wxPoint2DDouble(screenCoords.m_x / m_scale - m_translation.m_x,
                           screenCoords.m_y / m_scale - m_translation.m_y);
}

Workspace::Workspace(wxWindow* parent, wxString name /*, int workspaceID*/) : WorkspaceBase(parent)
{
    m_name = name;
    // m_workspaceID = workspaceID;
    m_glContext = new wxGLContext(m_glCanvas);
	m_mouseEventsHandler = new MouseEventsHandler(this);
	m_camera = new Camera();
}

Workspace::~Workspace()
{
    std::vector<Element*>::iterator it = m_elementList.begin();
    while(it != m_elementList.end()) {
	    if(!(*it)) delete *it;
	    it++;
	}
	
	delete m_camera;
	delete m_mouseEventsHandler;
}

void Workspace::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(m_glCanvas);
    m_glContext->SetCurrent(*m_glCanvas);
    SetViewport();

    // desenhar
    std::vector<Element*>::iterator it = m_elementList.begin();
    while(it != m_elementList.end()) {
	    Element* element = *it;
	    element->Draw(m_camera->GetTranslation(), m_camera->GetScale());
	    it++;
	}

    glFlush();
    m_glCanvas->SwapBuffers();
	event.Skip();
}

void Workspace::SetViewport()
{
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_LINE_SMOOTH);

    double width = m_glCanvas->GetSize().x - 1;
    double height = m_glCanvas->GetSize().y - 1;

    glViewport(0, 0, width, height);  // viewport cobrindo toda a tela

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, width, height, 0.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void Workspace::OnLeftClickDown(wxMouseEvent& event)
{
    if(m_insertMode) m_insertMode = false;
	event.Skip();
}

/*
void Workspace::OnKeyDown(wxKeyEvent& event)
{
    if(event.GetKeyCode() == 'B' && !m_insertMode) {
	    Bus* newBus = new Bus(m_camera->ScreenToWorld(event.GetPosition()));
	    m_elementList.push_back(newBus);
	    m_insertMode = true;
	    Redraw();
	}
	event.Skip();
}*/

void Workspace::OnMouseMotion(wxMouseEvent& event)
{
	if(m_insertMode) {
		std::vector<Element*>::iterator it = m_elementList.end() - 1;
		Element* element = *it;
		element->SetPosition(m_camera->ScreenToWorld(event.GetPosition()));
		Redraw();
	}
	event.Skip();
}