-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.cpp
312 lines (284 loc) · 9.92 KB
/
edit.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
/*****************************************************************
* edit.cpp
*
* Copyright 2012-2014, Hendrik Radke
* All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the license contained in the
* COPYING file.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Edit XML files with comic bubbles.
* Features:
* - [ ] Add / delete comic bubbles in elliptical and rectangular form
* - [ ] Edit coordinates / size of comic bubbles
* - [ ] Edit color / text of comic bubbles
*/
#include "comicfile.hpp"
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Input.H>
#include <FL/fl_draw.H>
#include <assert.h>
Fl_Window* mainWindow;
Bubble* current;
void cb_drawRect(Fl_Widget* widget, void* data);
void cb_drawCirc(Fl_Widget* widget, void* data);
void cb_delShape(Fl_Widget* widget, void* data);
void cb_setText(Fl_Widget* widget, void* data);
void cb_save(Fl_Widget* widget, void* data);
class MyBox : public Fl_Box {
public:
enum EditMode { DM_HOVER, DM_DRAW, DM_MOVE, DM_RESIZE };
enum EditSubMode { DSM_RECT, DSM_CIRC };
uchar* img_original; // unchanged image buffer
uchar* img_display; // displayed image buffer
Comicfile* comic;
Fl_Input* text_bar;
EditMode editmode = DM_HOVER;
EditSubMode submode = DSM_RECT;
int oldx, oldy;
// FLTK DRAW METHOD
void draw() {
load_image();
size_t img_size = imlib_image_get_width() * imlib_image_get_height() * 4;
fl_draw_image(img_display, x(), y(), w(), h(), 4, 0); // todo: sometimes segfaults
}
void load_image() {
size_t img_size = imlib_image_get_width() * imlib_image_get_height() * 4;
if (img_original == nullptr) img_original = (uchar*)malloc(img_size);
if (img_display == nullptr) img_display = (uchar*)malloc(img_size);
DATA32* data = imlib_image_get_data();
for (int i=0; i<w()*h(); i+=1) {
img_original[4*i+0] = (data[i]>>16) & 0xFF; // b
img_original[4*i+1] = (data[i]>> 8) & 0xFF; // g
img_original[4*i+2] = (data[i]>> 0) & 0xFF; // r
img_original[4*i+3] = (data[i]>>24) & 0xFF; // a
}
memcpy(img_display, img_original, img_size);
}
MyBox(int x0, int y0)
: Fl_Box(x0,y0, imlib_image_get_width(), imlib_image_get_height()) {
// Create GUI
auto bRect = new Fl_Button( 0,0, 40,30, "rect");
auto bCirc = new Fl_Button(40,0, 40,30, "circ");
auto bDel = new Fl_Button(80,0, 40,30, "del");
text_bar = new Fl_Input(120,0, w()-200,30, "");
// TODO: maybe replace this with an "edit" window, where you can set text, size, font, color?
auto bSet = new Fl_Button(w()-80,0, 40,30, "set");
auto bSave = new Fl_Button(w()-40,0, 40,30, "save");
// set callbacks
bRect->callback(cb_drawRect, this);
bCirc->callback(cb_drawCirc, this);
bDel->callback(cb_delShape, this);
bSet->callback(cb_setText, this);
bSave->callback(cb_save, this);
}
~MyBox() {
free(img_original);
free(img_display);
}
void setComic(Comicfile* _comic) {
comic = _comic;
load_image();
}
Bubble* bubbleAt(int x, int y) const {
for (Bubble* bubble : comic->bubbles ) {
if (bubble->contains(x,y)) {
return bubble;
}
}
return nullptr;
}
int handle_hover(int event, int cx, int cy) {
Bubble* bubble = bubbleAt(cx, cy);
if (bubble == nullptr) {
// todo: this still redraws every time the cursor moves
if (current != nullptr) current->draw(); // redraw last selected bubble
return 1;
}
switch(editmode) {
case DM_HOVER:
if (current != nullptr && current != bubble) current->draw(); // redraw last selected bubble
text_bar->value(bubble->getText().c_str());
current = bubble;
bubble->draw(Bubble::OUTLINE);
redraw();
break;
case DM_MOVE:
// todo: should never happen
break;
}
return 1;
}
int handle_press(int event, int cx, int cy) {
Bubble* bubble = bubbleAt(cx, cy);
switch (editmode) {
case DM_HOVER:
if (bubble != NULL) {
text_bar->value(bubble->getText().c_str());
current = bubble;
bubble->draw(Bubble::OUTLINE);
redraw();
}
break;
case DM_DRAW: // create rectangle/ellipse
oldx = cx;
oldy = cy;
break;
}
return 1;
}
int handle_release(int event, int cx, int cy) {
Bubble* bubble;
switch(editmode) {
case DM_HOVER: return 1;
case DM_MOVE:
break;
case DM_DRAW: {
if (cx < oldx) std::swap(oldx, cx);
if (cy < oldy) std::swap(oldy, cy);
assert(submode == DSM_RECT || submode == DSM_CIRC);
if (submode == DSM_RECT) {
bubble = new BubbleRectangle(oldx, oldy, cx-oldx, cy-oldy, comic->getFont("default"), comic->getColor("default"));
} else if (submode == DSM_CIRC) {
int radx = (cx-oldx)/2, rady = (cy-oldy)/2;
bubble = new BubbleEllipse(oldx+radx, oldy+rady, radx, rady, comic->getFont("default"), comic->getColor("default"));
}
comic->add(bubble);
bubble->draw(Bubble::FILL);
bubble->setText("noi");
current = bubble;
redraw();
break;
}
default:
std::cerr<< "Invalid shape creation\n";
exit(-1);
}
editmode = DM_HOVER;
mainWindow->cursor(FL_CURSOR_DEFAULT);
return 1;
}
int handle_drag(int event, int cx, int cy) {
switch(editmode) {
case DM_DRAW:
if (submode == DSM_RECT) {
fl_color(255, 0, 255);
fl_line_style(FL_DASH, 1, const_cast<char*>("\x04\x04"));
fl_rect(oldx, oldy+30, abs(cx-oldx), abs(cy-oldy));
} else if (submode == DSM_CIRC) {
//fl_pie(centerx-radiusx, centery-radiusy+30, 2*radiusx, 2*radiusy, 0, 360);
fl_rect(oldx, oldy+30, abs(cx-oldx), abs(cy-oldy));
}
redraw();
break;
case DM_HOVER:
current = bubbleAt(cx, cy);
if (current == nullptr) return 1;
current->setPosition(cx, cy);
current->draw(Bubble::OUTLINE);
redraw();
break;
}
return 1;
}
virtual int handle(int event) {
int cx = Fl::event_x() - x();
int cy = Fl::event_y() - y();
switch(event) {
case FL_MOVE: return handle_hover (event, cx, cy);
case FL_PUSH: return handle_press (event, cx, cy);
case FL_RELEASE: return handle_release(event, cx, cy);
case FL_DRAG: return handle_drag (event, cx, cy);
case FL_SHORTCUT: // fallthrough
case FL_HIDE:
// makes ESC terminate the program, but everything else stops working :-(
return 1;
default: return 1;
}
}
};
void cb_drawRect(Fl_Widget* widget, void* data) {
MyBox* my_box = static_cast<MyBox*>(data);
assert( my_box != nullptr );
mainWindow->cursor(FL_CURSOR_CROSS);
my_box->editmode = MyBox::DM_DRAW;
my_box->submode = MyBox::DSM_RECT;
}
void cb_drawCirc(Fl_Widget* widget, void* data) {
MyBox* my_box = static_cast<MyBox*>(data);
assert( my_box != nullptr );
mainWindow->cursor(FL_CURSOR_CROSS);
my_box->editmode = MyBox::DM_DRAW;
my_box->submode = MyBox::DSM_CIRC;
}
void cb_delShape(Fl_Widget* widget, void* data) {
// TODO: a bit unintuitive maybe. should i change cursor shape and delete on click?
if ( !current ) return;
MyBox* my_box = static_cast<MyBox*>(data);
assert( my_box != nullptr );
my_box->comic->del( current );
current = nullptr;
my_box->comic->draw();
}
void cb_setText(Fl_Widget* widget, void* data) {
if ( !current ) return;
MyBox* my_box = static_cast<MyBox*>(data);
assert( my_box != nullptr );
current->setText( my_box->text_bar->value() );
}
void cb_save(Fl_Widget* widget, void* data) {
MyBox* my_box = static_cast<MyBox*>(data);
assert( my_box != nullptr );
// todo: write to file instead of cout
my_box->comic->writeXML(std::cout);
}
int main(int argc, char **argv) {
Comicfile::addFontpath("./fonts");
Comicfile* comic;
if(argc == 2) {
comic = parse_XML(argv[1]);
} else {
std::cerr<< "usage: "<< argv[0] <<" <XML file>\n";
exit(-1);
}
comic->draw();
// Create GUI
mainWindow = new Fl_Window(imlib_image_get_width(), imlib_image_get_height()+30);
MyBox* box = new MyBox(0,30);
box->setComic(comic);
mainWindow->end();
mainWindow->show();
mainWindow->redraw();
return Fl::run();
}
void Comicfile::draw() const {
// Load the original image
Imlib_Load_Error err;
Imlib_Image image = imlib_load_image_with_error_return(imgfile.c_str(), &err);
if (err != 0) {
std::cerr<< "Error loading image '"<< imgfile <<"', error code "<< err <<"\n";
exit(-1);
}
imlib_context_set_image(image);
// Derive filename and format for output file
string filename_ext = imgfile.substr(imgfile.rfind('.')+1);
imlib_image_set_format(filename_ext.c_str());
string filename_out = imgfile.substr(0, imgfile.rfind('.')+1) + language + "." + filename_ext;
imlib_context_set_image(image);
// Draw all the bubbles
for (const auto& b : bubbles) {
b->draw();
}
}