26e95072d005c1442a1c6e918aadd92b2388a653
[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         Debug("View Bounds => %s", m_bounds.Str().c_str());
15 }
16
17 void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
18 {
19         // x and y are coordinates in the window
20         // Convert to local coords.
21         x *= m_bounds.w;
22         y *= m_bounds.h;
23         x += m_bounds.x;
24         y += m_bounds.y;
25         
26         //Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
27         
28         Real top = y - m_bounds.y;
29         Real left = x - m_bounds.x;
30         
31         top *= scaleAmt;
32         left *= scaleAmt;
33         
34         m_bounds.x = x - left;
35         m_bounds.y = y - top;
36         m_bounds.w *= scaleAmt;
37         m_bounds.h *= scaleAmt;
38         Debug("View Bounds => %s", m_bounds.Str().c_str());
39 }
40
41 Rect View::TransformToViewCoords(const Rect& inp) const
42 {
43         Rect out;
44         out.x = (inp.x - m_bounds.x) / m_bounds.w;
45         out.y = (inp.y - m_bounds.y) / m_bounds.h;
46
47         out.w = inp.w / m_bounds.w;
48         out.h = inp.h / m_bounds.h;
49         return out;
50 }
51
52 void View::DrawGrid()
53 {
54         // Draw some grid lines at fixed pixel positions
55         glMatrixMode(GL_PROJECTION);
56         glLoadIdentity();
57         glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
58         glMatrixMode(GL_MODELVIEW);
59         glLoadIdentity();
60
61         glColor4f(0.9,0.9,0.9,0.1);
62         const float num_lines = 50.0;
63         for (float i = 0; i < num_lines; ++i)
64         {
65                 glBegin(GL_LINES);
66                 glVertex2f(i*(1.0/num_lines), 0.0);
67                 glVertex2f(i*(1.0/num_lines), 1.0);
68                 glEnd();
69                 glBegin(GL_LINES);
70                 glVertex2f(0.0,i*(1.0/num_lines));
71                 glVertex2f(1.0,i*(1.0/num_lines));
72                 glEnd();
73         
74         }
75 }
76
77 void View::Render()
78 {
79         static bool debug_output_done = false;
80         if (!debug_output_done)
81         {
82                 m_document.DebugDumpObjects();
83                 debug_output_done = true;
84         }
85
86
87         //DrawGrid(); // Draw the gridlines
88
89         glMatrixMode(GL_PROJECTION);
90         glLoadIdentity();
91         if (m_use_gpu_transform)
92         {
93                 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);
94         }
95         else
96         {
97                 glOrtho(0,1,1,0,-1,1);
98         }
99         glMatrixMode(GL_MODELVIEW);
100         glLoadIdentity();
101
102         if (m_colour.a < 1.0f)
103         {
104                 glEnable(GL_BLEND);
105                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
106         }
107         glColor4f(m_colour.r, m_colour.g, m_colour.b, m_colour.a);
108         glBegin(GL_QUADS);
109         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
110         {
111                 if (m_document.m_objects.types[id] != RECT_FILLED)
112                         continue;
113                 Rect obj_bounds;
114                 if (m_use_gpu_transform)
115                 {
116                         obj_bounds = m_document.m_objects.bounds[id];
117                 }
118                 else
119                 {
120                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
121                 }
122                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y));
123                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y));
124                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h));
125                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
126         }
127         glEnd();
128
129         
130         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
131         {
132                 if (m_document.m_objects.types[id] != RECT_OUTLINE)
133                         continue;
134                 Rect obj_bounds;
135                 if (m_use_gpu_transform)
136                 {
137                         obj_bounds = m_document.m_objects.bounds[id];
138                 }
139                 else
140                 {
141                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
142                 }
143                 glBegin(GL_LINE_LOOP);
144                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y));
145                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y));
146                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h));
147                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
148                 glEnd();
149         }
150
151         if (m_colour.a < 1.0f)
152         {
153                 glDisable(GL_BLEND);
154         }
155
156 }

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