Implement Pointless Broken Things
[ipdf/code.git] / src / view.cpp
1 #include "view.h"
2
3 #include "SDL_opengl.h"
4
5 using namespace IPDF;
6 using namespace std;
7
8 void View::Translate(Real x, Real y)
9 {
10         x *= m_bounds.w;
11         y *= m_bounds.h;
12         m_bounds.x += x;
13         m_bounds.y += y;
14 }
15
16 void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
17 {
18         // Convert to local coords.
19         x *= m_bounds.w;
20         y *= m_bounds.h;
21         x += m_bounds.x;
22         y += m_bounds.y;
23         
24         Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
25         
26         Real top = y - m_bounds.y;
27         Real left = x - m_bounds.x;
28         
29         top *= scaleAmt;
30         left *= scaleAmt;
31         
32         m_bounds.x = x - left;
33         m_bounds.y = y - top;
34         m_bounds.w *= scaleAmt;
35         m_bounds.h *= scaleAmt;
36 }
37
38 void View::DrawGrid()
39 {
40         // Draw some grid lines at fixed pixel positions
41         glMatrixMode(GL_PROJECTION);
42         glLoadIdentity();
43         glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
44         glMatrixMode(GL_MODELVIEW);
45         glLoadIdentity();
46
47         glColor4f(0.9,0.9,0.9,0.1);
48         const float num_lines = 50.0;
49         for (float i = 0; i < num_lines; ++i)
50         {
51                 glBegin(GL_LINES);
52                 glVertex2f(i*(1.0/num_lines), 0.0);
53                 glVertex2f(i*(1.0/num_lines), 1.0);
54                 glEnd();
55                 glBegin(GL_LINES);
56                 glVertex2f(0.0,i*(1.0/num_lines));
57                 glVertex2f(1.0,i*(1.0/num_lines));
58                 glEnd();
59         
60         }
61 }
62
63 void View::Render()
64 {
65         static bool debug_output_done = false;
66         if (!debug_output_done)
67         {
68                 m_document.DebugDumpObjects();
69                 debug_output_done = true;
70         }
71
72         glClearColor(1.f,1.f,1.f,1.f);
73         glClear(GL_COLOR_BUFFER_BIT);
74
75         DrawGrid(); // Draw the gridlines
76
77         glMatrixMode(GL_PROJECTION);
78         glLoadIdentity();
79         glOrtho(Float(m_bounds.x), Float(m_bounds.x)+Float(m_bounds.w), Float(m_bounds.y) + Float(m_bounds.h), Float(m_bounds.y), -1.f, 1.f);
80         glMatrixMode(GL_MODELVIEW);
81         glLoadIdentity();
82
83         glColor4f(m_colour.r, m_colour.g, m_colour.b, m_colour.a);
84         glBegin(GL_QUADS);
85         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
86         {
87                 if (m_document.m_objects.types[id] == RECT_FILLED)
88                         continue;
89                 Rect obj_bounds = m_document.m_objects.bounds[id];
90                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y));
91                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y));
92                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h));
93                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
94         }
95         glEnd();
96
97         
98         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
99         {
100                 if (m_document.m_objects.types[id] == RECT_OUTLINE)
101                         continue;
102                 Rect obj_bounds = m_document.m_objects.bounds[id];
103                 glBegin(GL_LINE_LOOP);
104                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y));
105                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y));
106                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h));
107                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
108                 glEnd();
109         }
110
111 }

UCC git Repository :: git.ucc.asn.au