forked from KaixingWU/ImpSkelS20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaintView.cpp
242 lines (190 loc) · 4.36 KB
/
PaintView.cpp
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
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// paintview.cpp
//
// The code maintaining the painting view of the input images
//
#include "impressionist.h"
#include "impressionistDoc.h"
#include "impressionistUI.h"
#include "paintview.h"
#include "ImpBrush.h"
#define LEFT_MOUSE_DOWN 1
#define LEFT_MOUSE_DRAG 2
#define LEFT_MOUSE_UP 3
#define RIGHT_MOUSE_DOWN 4
#define RIGHT_MOUSE_DRAG 5
#define RIGHT_MOUSE_UP 6
#ifndef WIN32
#define min(a, b) ( ( (a)<(b) ) ? (a) : (b) )
#define max(a, b) ( ( (a)>(b) ) ? (a) : (b) )
#endif
static int eventToDo;
static int isAnEvent=0;
static Point coord;
PaintView::PaintView(int x,
int y,
int w,
int h,
const char* l)
: Fl_Gl_Window(x,y,w,h,l)
{
m_nWindowWidth = w;
m_nWindowHeight = h;
}
void PaintView::draw()
{
#ifndef MESA
// To avoid flicker on some machines.
glDrawBuffer(GL_FRONT_AND_BACK);
#endif // !MESA
if(!valid())
{
glClearColor(0.7f, 0.7f, 0.7f, 1.0);
// We're only using 2-D, so turn off depth
glDisable( GL_DEPTH_TEST );
ortho();
glClear( GL_COLOR_BUFFER_BIT );
}
Point scrollpos;// = GetScrollPosition();
scrollpos.x = 0;
scrollpos.y = 0;
m_nWindowWidth = w();
m_nWindowHeight = h();
int drawWidth, drawHeight;
drawWidth = min( m_nWindowWidth, m_pDoc->m_nPaintWidth );
drawHeight = min( m_nWindowHeight, m_pDoc->m_nPaintHeight );
int startrow = m_pDoc->m_nPaintHeight - (scrollpos.y + drawHeight);
if ( startrow < 0 ) startrow = 0;
m_pPaintBitstart = m_pDoc->m_ucPainting +
3 * ((m_pDoc->m_nPaintWidth * startrow) + scrollpos.x);
m_nDrawWidth = drawWidth;
m_nDrawHeight = drawHeight;
m_nStartRow = startrow;
m_nEndRow = startrow + drawHeight;
m_nStartCol = scrollpos.x;
m_nEndCol = m_nStartCol + drawWidth;
if ( m_pDoc->m_ucPainting && !isAnEvent)
{
RestoreContent();
}
if ( m_pDoc->m_ucPainting && isAnEvent)
{
// Clear it after processing.
isAnEvent = 0;
Point source( coord.x + m_nStartCol, m_nEndRow - coord.y );
Point target( coord.x, m_nWindowHeight - coord.y );
// This is the event handler
switch (eventToDo)
{
case LEFT_MOUSE_DOWN:
m_pDoc->m_pCurrentBrush->BrushBegin( source, target );
break;
case LEFT_MOUSE_DRAG:
m_pDoc->m_pCurrentBrush->BrushMove( source, target );
break;
case LEFT_MOUSE_UP:
m_pDoc->m_pCurrentBrush->BrushEnd( source, target );
SaveCurrentContent();
RestoreContent();
break;
case RIGHT_MOUSE_DOWN:
break;
case RIGHT_MOUSE_DRAG:
break;
case RIGHT_MOUSE_UP:
break;
default:
printf("Unknown event!!\n");
break;
}
}
glFlush();
#ifndef MESA
// To avoid flicker on some machines.
glDrawBuffer(GL_BACK);
#endif // !MESA
}
int PaintView::handle(int event)
{
switch(event)
{
case FL_ENTER:
redraw();
break;
case FL_PUSH:
coord.x = Fl::event_x();
coord.y = Fl::event_y();
if (Fl::event_button()>1)
eventToDo=RIGHT_MOUSE_DOWN;
else
eventToDo=LEFT_MOUSE_DOWN;
isAnEvent=1;
redraw();
break;
case FL_DRAG:
coord.x = Fl::event_x();
coord.y = Fl::event_y();
if (Fl::event_button()>1)
eventToDo=RIGHT_MOUSE_DRAG;
else
eventToDo=LEFT_MOUSE_DRAG;
isAnEvent=1;
redraw();
break;
case FL_RELEASE:
coord.x = Fl::event_x();
coord.y = Fl::event_y();
if (Fl::event_button()>1)
eventToDo=RIGHT_MOUSE_UP;
else
eventToDo=LEFT_MOUSE_UP;
isAnEvent=1;
redraw();
break;
case FL_MOVE:
coord.x = Fl::event_x();
coord.y = Fl::event_y();
break;
default:
return 0;
break;
}
return 1;
}
void PaintView::refresh()
{
redraw();
}
void PaintView::resizeWindow(int width, int height)
{
resize(x(), y(), width, height);
}
void PaintView::SaveCurrentContent()
{
// Tell openGL to read from the front buffer when capturing
// out paint strokes
glReadBuffer(GL_FRONT);
glPixelStorei( GL_PACK_ALIGNMENT, 1 );
glPixelStorei( GL_PACK_ROW_LENGTH, m_pDoc->m_nPaintWidth );
glReadPixels( 0,
m_nWindowHeight - m_nDrawHeight,
m_nDrawWidth,
m_nDrawHeight,
GL_RGB,
GL_UNSIGNED_BYTE,
m_pPaintBitstart );
}
void PaintView::RestoreContent()
{
glDrawBuffer(GL_BACK);
glClear( GL_COLOR_BUFFER_BIT );
glRasterPos2i( 0, m_nWindowHeight - m_nDrawHeight );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glPixelStorei( GL_UNPACK_ROW_LENGTH, m_pDoc->m_nPaintWidth );
glDrawPixels( m_nDrawWidth,
m_nDrawHeight,
GL_RGB,
GL_UNSIGNED_BYTE,
m_pPaintBitstart);
// glDrawBuffer(GL_FRONT);
}