]> git.tdb.fi Git - r2c2.git/blob - source/engineer/trainview.cpp
Complete rewrite of the engineer UI
[r2c2.git] / source / engineer / trainview.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/gl/tests.h>
3 #include <msp/gltk/button.h>
4 #include <msp/gltk/image.h>
5 #include <msp/gltk/label.h>
6 #include <msp/strings/format.h>
7 #include "libr2c2/vehicle.h"
8 #include "libr2c2/vehicletype.h"
9 #include "engineer.h"
10 #include "trainview.h"
11
12 using namespace Msp;
13 using namespace R2C2;
14
15 TrainView::TrainView(Engineer &e, const Train &t):
16         engineer(e),
17         train(t),
18         mode(SIDE),
19         forward(true),
20         pipeline(400, 300, false)
21 {
22         Loader::WidgetMap widgets;
23         DataFile::load(*this, "data/trainview.ui", widgets);
24
25         dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_title"))->set_text(format("View of %s", train.get_name()));
26         dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_forward"))->signal_toggled.connect(sigc::mem_fun(this, &TrainView::set_forward));
27         dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_roof"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainView::ui_mode_toggled), ROOF));
28         dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_side"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainView::ui_mode_toggled), SIDE));
29         dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_head"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainView::ui_mode_toggled), HEAD));
30
31         tex.set_min_filter(GL::LINEAR);
32         tex.storage(GL::RGB, 400, 300);
33         tex.image(0, GL::RGB, GL::UNSIGNED_BYTE, 0);
34         fbo.attach(GL::COLOR_ATTACHMENT0, tex, 0);
35         depth.storage(GL::DEPTH_COMPONENT, 400, 300);
36         fbo.attach(GL::DEPTH_ATTACHMENT, depth);
37
38         camera.set_up_direction(GL::Vector3(0, 0, 1));
39         camera.set_depth_clip(0.01, 10);
40         camera.set_aspect(1);
41         pipeline.set_camera(&camera);
42
43         pipeline.add_renderable(engineer.get_layout_3d().get_scene());
44
45         GL::Pipeline::Pass *pass = &pipeline.add_pass(0);
46         pass->set_depth_test(&GL::DepthTest::lequal());
47         pass->set_lighting(&engineer.get_lighting());
48
49         dynamic_cast<GLtk::Image *>(get_item(widgets, "img_view"))->set_image(&tex);
50
51         engineer.add_train_view(*this);
52 }
53
54 TrainView::~TrainView()
55 {
56         engineer.remove_train_view(*this);
57 }
58
59 void TrainView::set_mode(Mode m)
60 {
61         mode = m;
62 }
63
64 void TrainView::ui_mode_toggled(bool v, Mode m)
65 {
66         if(v)
67                 set_mode(m);
68 }
69
70 void TrainView::set_forward(bool f)
71 {
72         forward = f;
73 }
74
75 void TrainView::prepare()
76 {
77         const Vehicle &veh = train.get_vehicle(0);
78         const Vector &pos = veh.get_position();
79         Angle angle = veh.get_rotation();
80         if(!forward)
81                 angle += Angle::half_turn();
82         Vector fwd_vec = rotated_vector(Vector(1, 0, 0), angle);
83         Vector side_vec = rotated_vector(Vector(0, -1, 0), angle);
84         float l = veh.get_type().get_length();
85
86         switch(mode)
87         {
88         case ROOF:
89                 camera.set_position(pos-l*fwd_vec+Vector(0, 0, 0.07));
90                 camera.set_look_direction(fwd_vec-Vector(0, 0, -0.2));
91                 break;
92         case SIDE:
93                 camera.set_position(pos-0.8f*fwd_vec+0.05f*side_vec+Vector(0, 0, 0.03));
94                 camera.set_look_direction(fwd_vec-side_vec*0.2f);
95                 break;
96         case HEAD:
97                 camera.set_position(pos+fwd_vec*(l*0.55f)+Vector(0, 0, 0.03));
98                 camera.set_look_direction(fwd_vec);
99                 break;
100         }
101
102         GL::Bind _bind_fbo(fbo);
103         fbo.clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
104         pipeline.render();
105 }