-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImpressionistUI.cpp
578 lines (473 loc) · 20 KB
/
ImpressionistUI.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//
// impressionistUI.h
//
// The user interface part for the program.
//
#include <FL/fl_ask.h>
#include <math.h>
#include "impressionistUI.h"
#include "impressionistDoc.h"
#include <string>
/*
//------------------------------ Widget Examples -------------------------------------------------
Here is some example code for all of the widgets that you may need to add to the
project. You can copy and paste these into your code and then change them to
make them look how you want. Descriptions for all of the widgets here can be found
in links on the fltk help session page.
//---------Window/Dialog and Menubar-----------------------------------
//----To install a window--------------------------
Fl_Window* myWindow = new Fl_Window(600, 300, "MyWindow");
myWindow->user_data((void*)(this)); // record self to be used by static callback functions
// install menu bar
myMenubar = new Fl_Menu_Bar(0, 0, 600, 25);
Fl_Menu_Item ImpressionistUI::myMenuItems[] = {
{ "&File", 0, 0, 0, FL_SUBMENU },
{ "&Load...", FL_ALT + 'l', (Fl_Callback *)ImpressionistUI::cb_load },
{ "&Save...", FL_ALT + 's', (Fl_Callback *)ImpressionistUI::cb_save }.
{ "&Quit", FL_ALT + 'q', (Fl_Callback *)ImpressionistUI::cb_exit },
{ 0 },
{ "&Edit", 0, 0, 0, FL_SUBMENU },
{ "&Copy",FL_ALT + 'c', (Fl_Callback *)ImpressionistUI::cb_copy, (void *)COPY },
{ "&Cut", FL_ALT + 'x', (Fl_Callback *)ImpressionistUI::cb_cut, (void *)CUT },
{ "&Paste", FL_ALT + 'v', (Fl_Callback *)ImpressionistUI::cb_paste, (void *)PASTE },
{ 0 },
{ "&Help", 0, 0, 0, FL_SUBMENU },
{ "&About", FL_ALT + 'a', (Fl_Callback *)ImpressionistUI::cb_about },
{ 0 },
{ 0 }
};
myMenubar->menu(myMenuItems);
m_mainWindow->end();
//----The window callback--------------------------
// One of the callbacks
void ImpressionistUI::cb_load(Fl_Menu_* o, void* v)
{
ImpressionistDoc *pDoc=whoami(o)->getDocument();
char* newfile = fl_file_chooser("Open File?", "*.bmp", pDoc->getImageName() );
if (newfile != NULL) {
pDoc->loadImage(newfile);
}
}
//------------Slider---------------------------------------
//----To install a slider--------------------------
Fl_Value_Slider * mySlider = new Fl_Value_Slider(10, 80, 300, 20, "My Value");
mySlider->user_data((void*)(this)); // record self to be used by static callback functions
mySlider->type(FL_HOR_NICE_SLIDER);
mySlider->labelfont(FL_COURIER);
mySlider->labelsize(12);
mySlider->minimum(1);
mySlider->maximum(40);
mySlider->step(1);
mySlider->value(m_nMyValue);
mySlider->align(FL_ALIGN_RIGHT);
mySlider->callback(cb_MyValueSlides);
//----The slider callback--------------------------
void ImpressionistUI::cb_MyValueSlides(Fl_Widget* o, void* v)
{
((ImpressionistUI*)(o->user_data()))->m_nMyValue=int( ((Fl_Slider *)o)->value() ) ;
}
//------------Choice---------------------------------------
//----To install a choice--------------------------
Fl_Choice * myChoice = new Fl_Choice(50,10,150,25,"&myChoiceLabel");
myChoice->user_data((void*)(this)); // record self to be used by static callback functions
Fl_Menu_Item ImpressionistUI::myChoiceMenu[3+1] = {
{"one",FL_ALT+'p', (Fl_Callback *)ImpressionistUI::cb_myChoice, (void *)ONE},
{"two",FL_ALT+'l', (Fl_Callback *)ImpressionistUI::cb_myChoice, (void *)TWO},
{"three",FL_ALT+'c', (Fl_Callback *)ImpressionistUI::cb_myChoice, (void *)THREE},
{0}
};
myChoice->menu(myChoiceMenu);
myChoice->callback(cb_myChoice);
//-----The choice callback-------------------------
void ImpressionistUI::cb_myChoice(Fl_Widget* o, void* v)
{
ImpressionistUI* pUI=((ImpressionistUI *)(o->user_data()));
ImpressionistDoc* pDoc=pUI->getDocument();
int type=(int)v;
pDoc->setMyType(type);
}
//------------Button---------------------------------------
//---To install a button---------------------------
Fl_Button* myButton = new Fl_Button(330,220,50,20,"&myButtonLabel");
myButton->user_data((void*)(this)); // record self to be used by static callback functions
myButton->callback(cb_myButton);
//---The button callback---------------------------
void ImpressionistUI::cb_myButton(Fl_Widget* o, void* v)
{
ImpressionistUI* pUI=((ImpressionistUI*)(o->user_data()));
ImpressionistDoc* pDoc = pUI->getDocument();
pDoc->startPainting();
}
//---------Light Button------------------------------------
//---To install a light button---------------------
Fl_Light_Button* myLightButton = new Fl_Light_Button(240,10,150,25,"&myLightButtonLabel");
myLightButton->user_data((void*)(this)); // record self to be used by static callback functions
myLightButton->callback(cb_myLightButton);
//---The light button callback---------------------
void ImpressionistUI::cb_myLightButton(Fl_Widget* o, void* v)
{
ImpressionistUI *pUI=((ImpressionistUI*)(o->user_data()));
if (pUI->myBool==TRUE) pUI->myBool=FALSE;
else pUI->myBool=TRUE;
}
//----------Int Input--------------------------------------
//---To install an int input-----------------------
Fl_Int_Input* myInput = new Fl_Int_Input(200, 50, 5, 5, "&My Input");
myInput->user_data((void*)(this)); // record self to be used by static callback functions
myInput->callback(cb_myInput);
//---The int input callback------------------------
void ImpressionistUI::cb_myInput(Fl_Widget* o, void* v)
{
((ImpressionistUI*)(o->user_data()))->m_nMyInputValue=int( ((Fl_Int_Input *)o)->value() );
}
//------------------------------------------------------------------------------------------------
*/
//------------------------------------- Help Functions --------------------------------------------
//------------------------------------------------------------
// This returns the UI, given the menu item. It provides a
// link from the menu items to the UI
//------------------------------------------------------------
ImpressionistUI* ImpressionistUI::whoami(Fl_Menu_* o)
{
return ( (ImpressionistUI*)(o->parent()->user_data()) );
}
//--------------------------------- Callback Functions --------------------------------------------
//------------------------------------------------------------------
// Brings up a file chooser and then loads the chosen image
// This is called by the UI when the load image menu item is chosen
//------------------------------------------------------------------
void ImpressionistUI::cb_load_image(Fl_Menu_* o, void* v)
{
ImpressionistDoc *pDoc=whoami(o)->getDocument();
char* newfile = fl_file_chooser("Open File?", "*.bmp", pDoc->getImageName() );
if (newfile != NULL) {
pDoc->loadImage(newfile);
}
}
//------------------------------------------------------------------
// Brings up a file chooser and then saves the painted image
// This is called by the UI when the save image menu item is chosen
//------------------------------------------------------------------
void ImpressionistUI::cb_save_image(Fl_Menu_* o, void* v)
{
ImpressionistDoc *pDoc=whoami(o)->getDocument();
char* newfile = fl_file_chooser("Save File?", "*.bmp", "save.bmp" );
if (newfile != NULL) {
pDoc->saveImage(newfile);
}
}
//-------------------------------------------------------------
// Brings up the paint dialog
// This is called by the UI when the brushes menu item
// is chosen
//-------------------------------------------------------------
void ImpressionistUI::cb_brushes(Fl_Menu_* o, void* v)
{
whoami(o)->m_brushDialog->show();
}
//-------------------------------------------------------------
// Performs a Gaussian Blur on the source image on the left
//-------------------------------------------------------------
void ImpressionistUI::cb_gaussian(Fl_Menu_* o, void* v)
{
ImpressionistDoc* pDoc = whoami(o)->getDocument();
pDoc->gaussianBlur("iname");
}
//------------------------------------------------------------
// Clears the paintview canvas.
// Called by the UI when the clear canvas menu item is chosen
//------------------------------------------------------------
void ImpressionistUI::cb_clear_canvas(Fl_Menu_* o, void* v)
{
ImpressionistDoc* pDoc=whoami(o)->getDocument();
pDoc->clearCanvas();
}
//------------------------------------------------------------
// Causes the Impressionist program to exit
// Called by the UI when the quit menu item is chosen
//------------------------------------------------------------
void ImpressionistUI::cb_exit(Fl_Menu_* o, void* v)
{
whoami(o)->m_mainWindow->hide();
whoami(o)->m_brushDialog->hide();
}
//-----------------------------------------------------------
// Brings up an about dialog box
// Called by the UI when the about menu item is chosen
//-----------------------------------------------------------
void ImpressionistUI::cb_about(Fl_Menu_* o, void* v)
{
fl_message("Impressionist FLTK version for CS 460");
}
//------- UI should keep track of the current for all the controls for answering the query from Doc ---------
//-------------------------------------------------------------
// Sets the type of brush to use to the one chosen in the brush
// choice.
// Called by the UI when a brush is chosen in the brush choice
//-------------------------------------------------------------
void ImpressionistUI::cb_brushChoice(Fl_Widget* o, void* v)
{
ImpressionistUI* pUI=((ImpressionistUI *)(o->user_data()));
ImpressionistDoc* pDoc=pUI->getDocument();
int type=(int)v;
pDoc->setBrushType(type);
}
//------------------------------------------------------------
// Clears the paintview canvas.
// Called by the UI when the clear canvas button is pushed
//------------------------------------------------------------
void ImpressionistUI::cb_clear_canvas_button(Fl_Widget* o, void* v)
{
ImpressionistDoc * pDoc = ((ImpressionistUI*)(o->user_data()))->getDocument();
pDoc->clearCanvas();
}
//-----------------------------------------------------------
// Updates the brush size to use from the value of the size
// slider
// Called by the UI when the size slider is moved
//-----------------------------------------------------------
void ImpressionistUI::cb_sizeSlides(Fl_Widget* o, void* v)
{
((ImpressionistUI*)(o->user_data()))->m_nSize=int( ((Fl_Slider *)o)->value() ) ;
}
//-----------------------------------------------------------
// Updates the brush thickness to use from the value of the thickness
// slider
// Called by the UI when the thickness slider is moved
//-----------------------------------------------------------
void ImpressionistUI::cb_thicknessSlides(Fl_Widget* o, void* v)
{
((ImpressionistUI*)(o->user_data()))->m_nThickness = int(((Fl_Slider*)o)->value()); // used to be m_nSize
}
//-----------------------------------------------------------
// Updates the brush angle to use from the value of the angle
// slider
// Called by the UI when the angle slider is moved
//-----------------------------------------------------------
void ImpressionistUI::cb_angleSlides(Fl_Widget* o, void* v)
{
((ImpressionistUI*)(o->user_data()))->m_nAngle = int(((Fl_Slider*)o)->value()); // used to be m_nSize
}
//-----------------------------------------------------------
// Updates the brush alpha to use from the value of the alpha
// slider
// Called by the UI when the alpha slider is moved
//-----------------------------------------------------------
void ImpressionistUI::cb_alphaSlides(Fl_Widget* o, void* v)
{
((ImpressionistUI*)(o->user_data()))->m_nAlpha = double(((Fl_Slider*)o)->value()); // used to be m_nSize
}
//---------------------------------- per instance functions --------------------------------------
//------------------------------------------------
// Return the ImpressionistDoc used
//------------------------------------------------
ImpressionistDoc* ImpressionistUI::getDocument()
{
return m_pDoc;
}
//------------------------------------------------
// Draw the main window
//------------------------------------------------
void ImpressionistUI::show() {
m_mainWindow->show();
m_paintView->show();
m_origView->show();
}
//------------------------------------------------
// Change the paint and original window sizes to
// w by h
//------------------------------------------------
void ImpressionistUI::resize_windows(int w, int h) {
m_paintView->size(w,h);
m_origView->size(w,h);
}
//------------------------------------------------
// Set the ImpressionistDoc used by the UI to
// communicate with the brushes
//------------------------------------------------
void ImpressionistUI::setDocument(ImpressionistDoc* doc)
{
m_pDoc = doc;
m_origView->m_pDoc = doc;
m_paintView->m_pDoc = doc;
}
//------------------------------------------------
// Return the brush size
//------------------------------------------------
int ImpressionistUI::getSize()
{
return m_nSize;
}
//------------------------------------------------
// Return the brush thickness
//------------------------------------------------
int ImpressionistUI::getThickness()
{
return m_nThickness;
}
//------------------------------------------------
// Return the brush angle
//------------------------------------------------
int ImpressionistUI::getAngle()
{
return m_nAngle;
}
//------------------------------------------------
// Return the brush alpha
//------------------------------------------------
double ImpressionistUI::getAlpha()
{
return m_nAlpha;
}
//-------------------------------------------------
// Set the brush size
//-------------------------------------------------
void ImpressionistUI::setSize( int size )
{
m_nSize=size;
if (size<=40)
m_BrushSizeSlider->value(m_nSize);
}
//-------------------------------------------------
// Set the brush thickness
//-------------------------------------------------
void ImpressionistUI::setThickness(int size)
{
m_nThickness = size;
if (size <= 40)
m_BrushThicknessSlider->value(m_nThickness); // used to be m_BrushSizeSlider
}
//-------------------------------------------------
// Set the brush angle
//-------------------------------------------------
void ImpressionistUI::setAngle(int angle)
{
m_nAngle = angle;
if (angle <= 360)
m_BrushAngleSlider->value(m_nAngle);
}
//-------------------------------------------------
// Set the brush alpha
//-------------------------------------------------
void ImpressionistUI::setAlpha(double alpha)
{
m_nAlpha = alpha;
if (alpha <= 1.0)
m_BrushAlphaSlider->value(m_nAlpha);
}
// Main menu definition
Fl_Menu_Item ImpressionistUI::menuitems[] = {
{ "&File", 0, 0, 0, FL_SUBMENU },
{ "&Load Image...", FL_ALT + 'l', (Fl_Callback *)ImpressionistUI::cb_load_image },
{ "&Save Image...", FL_ALT + 's', (Fl_Callback *)ImpressionistUI::cb_save_image },
{ "&Brushes...", FL_ALT + 'b', (Fl_Callback *)ImpressionistUI::cb_brushes },
{ "&Gaussian Blur...", FL_ALT + 'g', (Fl_Callback*)ImpressionistUI::cb_gaussian },
{ "&Clear Canvas", FL_ALT + 'c', (Fl_Callback *)ImpressionistUI::cb_clear_canvas, 0, FL_MENU_DIVIDER },
{ "&Quit", FL_ALT + 'q', (Fl_Callback *)ImpressionistUI::cb_exit },
{ 0 },
{ "&Help", 0, 0, 0, FL_SUBMENU },
{ "&About", FL_ALT + 'a', (Fl_Callback *)ImpressionistUI::cb_about },
{ 0 },
{ 0 }
};
// Brush choice menu definition
Fl_Menu_Item ImpressionistUI::brushTypeMenu[NUM_BRUSH_TYPE+1] = {
{"Points", FL_ALT+'p', (Fl_Callback *)ImpressionistUI::cb_brushChoice, (void *)BRUSH_POINTS},
{"Lines", FL_ALT+'l', (Fl_Callback *)ImpressionistUI::cb_brushChoice, (void *)BRUSH_LINES},
{"Circles", FL_ALT+'c', (Fl_Callback *)ImpressionistUI::cb_brushChoice, (void *)BRUSH_CIRCLES},
{"Unfilled Circles", FL_ALT+'u', (Fl_Callback *)ImpressionistUI::cb_brushChoice, (void *)BRUSH_UNFILLED_CIRCLES},
{"Scattered Points", FL_ALT+'q', (Fl_Callback *)ImpressionistUI::cb_brushChoice, (void *)BRUSH_SCATTERED_POINTS},
{"Scattered Lines", FL_ALT+'m', (Fl_Callback *)ImpressionistUI::cb_brushChoice, (void *)BRUSH_SCATTERED_LINES},
{"Scattered Circles", FL_ALT+'d', (Fl_Callback *)ImpressionistUI::cb_brushChoice, (void *)BRUSH_SCATTERED_CIRCLES},
{"Scattered Unfilled Circles", FL_ALT + 'j', (Fl_Callback*)ImpressionistUI::cb_brushChoice, (void*)BRUSH_SCATTERED_UNFILLED_CIRCLES},
{0}
};
//----------------------------------------------------
// Constructor. Creates all of the widgets.
// Add new widgets here
//----------------------------------------------------
ImpressionistUI::ImpressionistUI() {
// Create the main window
m_mainWindow = new Fl_Window(600, 300, "Impressionist");
m_mainWindow->user_data((void*)(this)); // record self to be used by static callback functions
// install menu bar
m_menubar = new Fl_Menu_Bar(0, 0, 600, 25);
m_menubar->menu(menuitems);
// Create a group that will hold two sub windows inside the main
// window
Fl_Group* group = new Fl_Group(0, 25, 600, 275);
// install paint view window
m_paintView = new PaintView(300, 25, 300, 275, "This is the paint view");//0jon
m_paintView->box(FL_DOWN_FRAME);
// install original view window
m_origView = new OriginalView(0, 25, 300, 275, "This is the orig view");//300jon
m_origView->box(FL_DOWN_FRAME);
m_origView->deactivate();
group->end();
Fl_Group::current()->resizable(group);
m_mainWindow->end();
// init values
m_nSize = 10;
m_nThickness = 1;
m_nAngle = 0;
m_nAlpha = 1.0;
// brush dialog definition
m_brushDialog = new Fl_Window(400, 325, "Brush Dialog");
// Add a brush type choice to the dialog
m_BrushTypeChoice = new Fl_Choice(50,10,150,25,"&Brush");
m_BrushTypeChoice->user_data((void*)(this)); // record self to be used by static callback functions
m_BrushTypeChoice->menu(brushTypeMenu);
m_BrushTypeChoice->callback(cb_brushChoice);
m_ClearCanvasButton = new Fl_Button(240,10,150,25,"&Clear Canvas");
m_ClearCanvasButton->user_data((void*)(this));
m_ClearCanvasButton->callback(cb_clear_canvas_button);
// Add brush size slider to the dialog
m_BrushSizeSlider = new Fl_Value_Slider(10, 80, 300, 20, "Size");
m_BrushSizeSlider->user_data((void*)(this)); // record self to be used by static callback functions
m_BrushSizeSlider->type(FL_HOR_NICE_SLIDER);
m_BrushSizeSlider->labelfont(FL_HELVETICA);
m_BrushSizeSlider->labelsize(12);
m_BrushSizeSlider->minimum(1);
m_BrushSizeSlider->maximum(40);
m_BrushSizeSlider->step(1);
m_BrushSizeSlider->value(m_nSize);
m_BrushSizeSlider->align(FL_ALIGN_RIGHT);
m_BrushSizeSlider->callback(cb_sizeSlides);
// Add brush thickness slider to the dialog
m_BrushThicknessSlider = new Fl_Value_Slider(10, 100, 300, 20, "Thickness"); //x-axis, y-axis, width, height, "name"
m_BrushThicknessSlider->user_data((void*)(this)); // record self to be used by static callback functions
m_BrushThicknessSlider->type(FL_HOR_NICE_SLIDER);
m_BrushThicknessSlider->labelfont(FL_HELVETICA);
m_BrushThicknessSlider->labelsize(12);
m_BrushThicknessSlider->minimum(1);
m_BrushThicknessSlider->maximum(40);
m_BrushThicknessSlider->step(1);
m_BrushThicknessSlider->value(m_nThickness);
m_BrushThicknessSlider->align(FL_ALIGN_RIGHT);
m_BrushThicknessSlider->callback(cb_thicknessSlides);
// Add brush angle slider to the dialog
m_BrushAngleSlider = new Fl_Value_Slider(10, 120, 300, 20, "Angle"); //x-axis, y-axis, width, height, "name"
m_BrushAngleSlider->user_data((void*)(this)); // record self to be used by static callback functions
m_BrushAngleSlider->type(FL_HOR_NICE_SLIDER);
m_BrushAngleSlider->labelfont(FL_HELVETICA); // FL_COURIER
m_BrushAngleSlider->labelsize(12);
m_BrushAngleSlider->minimum(0);
m_BrushAngleSlider->maximum(360);
m_BrushAngleSlider->step(1);
m_BrushAngleSlider->value(m_nAngle);
m_BrushAngleSlider->align(FL_ALIGN_RIGHT);
m_BrushAngleSlider->callback(cb_angleSlides);
// Add brush alpha slider to the dialog
m_BrushAlphaSlider = new Fl_Value_Slider(10, 160, 300, 20, "Alpha"); //x-axis, y-axis, width, height, "name"
m_BrushAlphaSlider->user_data((void*)(this)); // record self to be used by static callback functions
m_BrushAlphaSlider->type(FL_HOR_NICE_SLIDER);
m_BrushAlphaSlider->labelfont(FL_HELVETICA); // FL_COURIER
m_BrushAlphaSlider->labelsize(12);
m_BrushAlphaSlider->minimum(0.0);
m_BrushAlphaSlider->maximum(1.0);
m_BrushAlphaSlider->step(0.01);
m_BrushAlphaSlider->value(m_nAlpha);
m_BrushAlphaSlider->align(FL_ALIGN_RIGHT);
m_BrushAlphaSlider->callback(cb_alphaSlides);
m_brushDialog->end();
}