-
Notifications
You must be signed in to change notification settings - Fork 0
/
VmMap.cc
975 lines (866 loc) · 27.1 KB
/
VmMap.cc
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
/* -*-c++-*-
* This file is part of vice-mapper.
*
* vice-mapper is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* vice-mapper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with vice-mapper. If not, see <https://www.gnu.org/licenses/>.
*
* $Id$
*
* File: map-window.cc
* Date: Tue May 5 22:43:21 2020
* Author: pottendo (pottendo)
*
* Abstract:
* map-window
*
* Modifications:
* $Log$
*/
#include <gtkmm/button.h>
#include <gtkmm/widget.h>
#include <gtkmm/frame.h>
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/pagesetup.h>
#include <glibmm/fileutils.h>
#include <glibmm/miscutils.h>
#include <cairomm/context.h>
#include <gdkmm/general.h> // set_source_pixbuf()
#include <iostream>
#include <regex>
#include "VmMap.h"
#include "VmMapControls.h"
#include "dialogs.h"
using namespace::std;
Glib::RefPtr<Gdk::Pixbuf> VmMap::empty_image;
double VmMap::scale_factor_x = def_zoom;
double VmMap::scale_factor_y = def_zoom;
int VmMap::nr_tiles = 0;
string VmMap::current_path="";
string VmMap::current_ext="";
string VmMap::current_basename=def_basename;
VmMap::VmMap()
: dirty(false),
tiles_placed(false)
{
//set_title("Map");
//map_grid.attach_next_to(ma, m_button_quit, Gtk::POS_BOTTOM, 1, 1);
//empty_image = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, TRUE, 8, def_resX, def_resY);
//empty_image->fill(0x0000001f);
// ctrls = new VmMapControls(*this, "Controls");
mw_ctrls = ctrls = new VmMapControls(*this);
//set_default_size(400,400);
set_border_width(4);
map_grid.set_hexpand(TRUE);
map_grid.set_vexpand(TRUE);
scw.mc = ctrls;
scw.set_border_width(4);
//scw.set_size_request(100, 100);
scw.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
scw.add(map_grid);
scw.set_hexpand(TRUE);
scw.set_vexpand(TRUE);
preview_win = nullptr;
builder->get_widget("VmMapPreview", preview_win);
file_chooser = nullptr;
builder->get_widget("VmMapPreviewFileChooser", file_chooser);
Gtk::Frame *prev_frame = nullptr;
builder->get_widget("VmMapPreviewFrame", prev_frame);
map_preview = new MapPreview();
map_preview->show();
prev_frame->add(*map_preview);
prev_frame->show();
Gtk::Frame *map_frame = nullptr;
builder->get_widget("VmMap", map_frame);
map_frame->add(scw);
show_all_children();
memset(tiles, 0, 101*101*sizeof(VmTile*));
pr_settings = Gtk::PrintSettings::create();
pr_pagesettings = Gtk::PageSetup::create();
}
VmMap::~VmMap()
{
mw_out << __FUNCTION__ << ": main map delete." << endl;
delete map_preview;
delete mw_ctrls;
mw_debug->save();
delete mw_out_stream; // a bit ugly to delete here not in main. TODO: Fixme.
delete mw_err_stream;
delete mw_debug;
delete mw_debug2;
delete mw_status;
}
VmMap::MyScw::MyScw()
{
}
/*
* this only gets GDK_SCROLL_SMOOTH events in ScrolledWindow
*/
bool
VmMap::MyScw::on_scroll_event(GdkEventScroll *scroll_event)
{
if (scroll_event->direction == GDK_SCROLL_SMOOTH) {
GdkScrollDirection dir;
gdouble dx, dy;
if (gdk_event_get_scroll_direction((GdkEvent *)scroll_event, &dir)) {
// mw_out << __FUNCTION__ << ": direction = " << dir << endl;
switch (dir) {
case GDK_SCROLL_UP:
scale_factor_x *= 1.05;
scale_factor_y *= 1.05;
break;
case GDK_SCROLL_DOWN:
scale_factor_x /= 1.05;
scale_factor_y /= 1.05;
break;
default:
;
mw_out << __FUNCTION__ << ": unknown direction: " << scroll_event->direction << endl;
}
}
else if (gdk_event_get_scroll_deltas((GdkEvent *)scroll_event, &dx, &dy)) {
if (dx > 0) scale_factor_x *= 1.05;
if (dx < 0) scale_factor_x /= 1.05;
if (dy > 0) scale_factor_y *= 1.05;
if (dy < 0) scale_factor_y /= 1.05;
//mw_out << __FUNCTION__ << ": dx = " << dx << ", dy = " << dy << endl;
}
}
scale_factor_x = MIN(scale_factor_x, 6.0);
scale_factor_y = MIN(scale_factor_y, 6.0);
mc->set_zoom(scale_factor_x, scale_factor_y);
return TRUE;
}
bool
VmMap::MyScw::on_motion_notify_event(GdkEventMotion* motion_event)
{
static gdouble xp, yp;
if (space_modifier /* && button_press*/) {
gdouble dx, dy, xa, ya;
dx = (motion_event->x - xp);
dy = (motion_event->y - yp);
/*
mw_out << __FUNCTION__ << ": space+mouse-move: hadj = " << get_hadjustment()->get_value()
<< "- dx: " << dx << ",dy: " << dy << ", xp: " << xp << ",yp: " << yp << endle;
*/
xa = get_hadjustment()->get_value() - dx;
ya = get_vadjustment()->get_value() - dy;
get_hadjustment()->set_value(xa);
get_vadjustment()->set_value(ya);
}
xp = motion_event->x; // track last location even if not panning
yp = motion_event->y;
mw_status->status();
return Gtk::ScrolledWindow::on_motion_notify_event(motion_event);
}
bool
VmMap::MyScw::on_enter_notify_event(GdkEventCrossing* crossing_event)
{
grab_focus(); // needed if e.g. controls took the focus
space_modifier = false;
return Gtk::ScrolledWindow::on_enter_notify_event(crossing_event);
}
bool
VmMap::MyScw::on_key_press_event(GdkEventKey *key_event)
{
if (key_event->keyval == GDK_KEY_space) {
space_modifier = true;
if (!move_cursor) // cannot initialize in constructor, as window is not realized at the time
move_cursor = Gdk::Cursor::create(this->get_window()->get_display(),Gdk::CROSSHAIR);
this->get_window()->set_cursor(move_cursor);
}
return Gtk::ScrolledWindow::on_key_press_event(key_event);
}
bool
VmMap::MyScw::on_key_release_event(GdkEventKey *key_event)
{
if (key_event->keyval == GDK_KEY_space) {
space_modifier = false;
this->get_window()->set_cursor();
}
return Gtk::ScrolledWindow::on_key_release_event(key_event);
}
/*
bool
map_window::MyScw::on_button_press_event(GdkEventButton* button_event)
{
button_press = TRUE;
mw_out << "button press" << endl;
Gtk::ScrolledWindow::on_button_press_event(button_event);
return TRUE;
}
bool
map_window::MyScw::on_button_release_event(GdkEventButton* button_event)
{
button_press = FALSE;
mw_out << "button rel" << endl;
Gtk::ScrolledWindow::on_button_release_event(button_event);
return TRUE;
}
*/
void
VmMap::update_state(void)
{
tiles_placed = dirty = false;
for (auto it = begin(VmTile::all_tiles);
(!dirty || !tiles_placed) && it != end(VmTile::all_tiles);
++it) {
if ((*it)->is_empty()) continue;
dirty |= (*it)->is_dirty();
tiles_placed |= ((*it)->getX() > 0) ? true : false;
}
set_dirty(dirty);
}
void
VmMap::add_tile(VmTile *a)
{
map_grid.attach(*a, a->getX(), a->getY());
tiles[a->getX()][a->getY()] = a;
a->scale(scale_factor_x, scale_factor_y); // make sure tile adjusts to current scaling
map_grid.show_all_children();
}
void
VmMap::remove_tile(VmTile *a, bool map_remove)
{
// mw_out << __FUNCTION__ << ": " << map_remove << " called for " << *a << endl;
if (a->getX() < 0) {
ctrls->remove_tile(a);
}
else {
map_grid.remove(*a);
if (!map_remove) {
// placed tile
VmTile *new_empty = new VmTile(*this, NULL, a->getX(), a->getY());
add_tile(new_empty);
mw_out << __FUNCTION__ << ": attached new " << *new_empty << endl;
resize_map();
}
else
tiles[a->getX()][a->getY()] = nullptr;
}
auto e = VmTile::all_tiles.erase(a);
if (e > 1) {
mw_err << __FUNCTION__ << ": erased " << e << " tiles." << endl;
}
nr_tiles = VmTile::all_tiles.size();
update_state();
show_all_children();
}
void
VmMap::reload_unplaced_tiles(char *path)
{
string cp = (path != NULL) ? string(path) : current_path;
if (cp == "") return;
Glib::Dir dir(cp);
std::list<std::string> entries (dir.begin(), dir.end());
for (auto e = entries.begin(); e != entries.end(); ++e) {
string fp = cp + G_DIR_SEPARATOR_S + *e;
VmTile *t = VmTile::lookup_by_name(fp);
if (t) continue; // skip if a tile with the filename exists
mw_out << __FUNCTION__ << ": new tile: " << fp << endl;
try {
t = new VmTile(*this, fp.c_str());
}
catch (...) {
continue; // ignore non-tiles
}
}
if (!empty_image ||
((empty_image->get_width() != VmTile::max_resX) || (empty_image->get_height() != VmTile::max_resY))) {
// create a new empty_image if not yet done or if a new max has been detected
empty_image = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, TRUE, 8, VmTile::max_resX, VmTile::max_resY);
empty_image->fill(0x0000001f);
}
ctrls->update_ctrls();
VmTile::refresh_minmax();
fill_empties();
mw_status->show(VmStatus::STATM, current_path);
update_state();
if (!tiles_placed && tiles[map_max / 2][map_max / 2] == nullptr)
add_tile(new VmTile(*this, NULL, map_max / 2, map_max / 2));
}
void
VmMap::save_settings(void)
{
string &cp = current_path;
if (cp == "") return;
size_t ext_pos = def_basename.find_last_of(".");
string cfg_fname = cp + G_DIR_SEPARATOR_S + def_basename.substr(0, ext_pos) + def_cfg_ext;
mw_out << __FUNCTION__ << ": cfg file = " << cfg_fname << endl;
Glib::RefPtr<Gio::File> cfg_file = Gio::File::create_for_path(cfg_fname);
try {
cfg_file->remove();
}
catch (...) {}; // ignore if not exists
Glib::RefPtr<Gio::FileOutputStream> cfg_stream;
try {
cfg_stream = cfg_file->create_file();
}
catch (Gio::Error &e) {
mw_err << __FUNCTION__ << ": *** cfg file not written: " << e.what() << endl;
return;
}
string head = string("# vice-mapper settings for for '" + def_basename + "'\n" +
"MAPV=" + mapper_version + "\n");
cfg_stream->write(head.c_str(), head.size());
string lines=
string("CRUP=" + to_string(VmTile::cr_up) + "\n"
"CRDO=" + to_string(VmTile::cr_do) + "\n"
"CRLE=" + to_string(VmTile::cr_le) + "\n"
"CRRI=" + to_string(VmTile::cr_ri) + "\n"
"ZOOMX=" + to_string(scale_factor_x) + "\n"
"ZOOMY=" + to_string(scale_factor_y) + "\n"
"#END - DONT_REMOVE_THIS_LINE\n"
);
cfg_stream->write(lines.c_str(), lines.size());
cfg_stream->flush();
}
bool
VmMap::process_line(string l)
{
//mw_out << __FUNCTION__ << ": processing line '" << l << "'" << endl;
if (l.find("#END") < l.size())
return false;
try {
if (l.find("CRUP") < l.size()) VmTile::cr_up = stoi(l.substr(5));
if (l.find("CRDO") < l.size()) VmTile::cr_do = stoi(l.substr(5));
if (l.find("CRLE") < l.size()) VmTile::cr_le = stoi(l.substr(5));
if (l.find("CRRI") < l.size()) VmTile::cr_ri = stoi(l.substr(5));
if (l.find("ZOOMX") < l.size()) { scale_factor_x = stod(l.substr(6)); }
if (l.find("ZOOMY") < l.size()) { scale_factor_y = stod(l.substr(6)); }
}
catch (std::invalid_argument &e) {
mw_err << e.what() << endl;
}
return true;
}
bool
VmMap::load_settings(void)
{
string &cp = current_path;
if (cp == "") return false;
size_t ext_pos = def_basename.find_last_of(".");
string cfg_fname = cp + G_DIR_SEPARATOR_S + def_basename.substr(0, ext_pos) + def_cfg_ext;
mw_out << __FUNCTION__ << ": cfg file = " << cfg_fname << endl;
Glib::RefPtr<Gio::File> cfg_file = Gio::File::create_for_path(cfg_fname);
if (!cfg_file) {
mw_out << __FUNCTION__ << ": can't open cfg file for map." << endl;
return false;
}
Glib::RefPtr<Gio::FileInputStream> cfg_stream;
try {
cfg_stream = cfg_file->read();
char cfg_buffer[1024];
gsize bytes_read;
if (cfg_stream->read_all(cfg_buffer, 1024, bytes_read) == false) {
mw_err << __FUNCTION__ << ": read error." << endl;
return false;
}
string cfg(cfg_buffer);
int lstart = 0;
int lend;
do {
lend = cfg.substr(lstart).find_first_of("\n");
if (!process_line(cfg.substr(lstart, lend)))
break;
lstart = lstart + lend + 1;
} while(true);
ctrls->set_zoom(scale_factor_x, scale_factor_y, false);
ctrls->set_crop(VmTile::cr_up, VmTile::cr_do, VmTile::cr_le, VmTile::cr_ri, false);
}
catch (Gio::Error &e) {
mw_err << __FUNCTION__ << ": *** cfg file not read: " << e.what() << endl;
return false;
}
return true;
}
void
VmMap::fill_empties()
{
int x, y;
/*
mw_out << "X:" << VmTile::xmin << "-" << VmTile::xmax
<< "Y:" << VmTile::ymin << "-" << VmTile::ymax << endl;
*/
try {
for (x = VmTile::xmin; x <= VmTile::xmax; x++) {
for (y = VmTile::ymin; y <= VmTile::ymax; y++) {
if (tiles[x][y] == NULL) {
add_tile(new VmTile(*this, NULL, x, y));
}
}
}
}
catch (std::exception &e) {
mw_out << __FUNCTION__ << ": failed with exception " << e.what() << endl;
return;
}
}
void
VmMap::scale_all(void)
{
int x, y;
for (x = VmTile::xmin; x <= VmTile::xmax; x++) {
for (y = VmTile::ymin; y <= VmTile::ymax; y++) {
if (tiles[x][y] != NULL) {
tiles[x][y]->scale(scale_factor_x, scale_factor_y);
}
}
}
}
int
VmMap::get_empty_area(int from_x, int from_y, int to_x, int to_y)
{
int x, y;
int do_scratch = 0;
VmTile *t;
std::vector<VmTile *> scratch;
for (y = from_y; y < to_y; y++) {
for (x = from_x; x < to_x; x++) {
if (tiles[x][y]) {
if (!(t = tiles[x][y])->is_empty()) {
break;
}
scratch.push_back(t);
do_scratch++;
}
}
}
if (do_scratch) {
mw_out << "found " << do_scratch << " empty tiles to remove." << endl;
std::for_each(scratch.begin(), scratch.end(),
[](VmTile *x)->void {
if (x) {
x->mw.map_grid.remove(*x);
x->mw.tiles[x->getX()][x->getY()] = NULL;
delete x;
}
});
}
return do_scratch;
}
void
VmMap::xchange_tiles(VmTile *s, VmTile *d)
{
int tsx, tsy, tdx, tdy;
VmTile *t;
s->getXY(tsx, tsy); // get x,y
d->getXY(tdx, tdy);
/*
if ((tsx < 0) && (tsy < 0)) {
mw_out << "*** shoud not happen !!! - placed from/to unplaced tiles." << endl;
return;
}
*/
if (tdx < 0) {
mw_out << "*** should not happen !!! - move to unplaced tiles";
//special case - add to list of unplace tiles
return;
}
if (tsx < 0) {
//mw_out << "remove from unplaced tiles";
mw_out << __FUNCTION__ << ": removing (unplaced) " << *s << endl;
ctrls->remove_tile(s);
}
if ((t = (VmTile *) map_grid.get_child_at(tsx, tsy)) != NULL) {
mw_out << __FUNCTION__ << ": removing (placed) " << *t << endl;
map_grid.remove(*t);
}
if ((t = (VmTile *) map_grid.get_child_at(tdx, tdy)) != NULL) {
mw_out << __FUNCTION__ << ": removing (placed) " << *t << endl;
map_grid.remove(*t);
}
s->setXY(tdx, tdy);
add_tile(s);
s->sync_tile();
tiles_placed=true;
d->setXY(tsx, tsy);
if (tsx > 0) {
add_tile(d);
d->sync_tile();
}
else {
if (!d->is_empty()) { // pushback if we placed on occupied tile
ctrls->add_tile(d);
d->sync_tile();
}
else {
delete d; // dispose unused empty tile
}
}
// check if we need to grow/shrink
if (s->update_minmax() || d->update_minmax())
fill_empties(); // already placed therefore s(ource)!
resize_map();
}
/*
* map functions
*/
void
VmMap::resize_map(void)
{
int do_scratch = 0;
VmTile::refresh_minmax();
do_scratch += get_empty_area(0, 0, map_max+1, VmTile::ymin);
do_scratch += get_empty_area(0, VmTile::ymax+1, map_max+1, map_max+1);
do_scratch += get_empty_area(0, 0, VmTile::xmin, map_max+1);
do_scratch += get_empty_area(VmTile::xmax+1, 0, map_max+1, map_max+1);
if (do_scratch) {
mw_out << "found " << do_scratch << " empty tiles to remove." << endl;
}
}
void
VmMap::remove_map(void)
{
mw_out << __FUNCTION__ << ": called." << endl;
std::vector<Gtk::Widget *> to_scratch = map_grid.get_children();
std::for_each(to_scratch.begin(), to_scratch.end(),
[this](Gtk::Widget *t)->void {
remove_tile(static_cast<VmTile*>(t), true);
delete static_cast<VmTile*>(t);
});
for (auto t : VmTile::all_tiles) {
delete t;
VmTile::all_tiles.erase(t);
nr_tiles--;
}
//nr_tiles = VmTile::all_tiles.size();
mw_out << __FUNCTION__ << ": pending tiles = " << nr_tiles
<< ", alloc_count = " << VmTile::alloc_count << endl;
mw_status->clear();
set_dirty(false);
tiles_placed = false;
VmTile::min_resX = 100000;
VmTile::min_resY = 100000;
VmTile::max_resX = -1;
VmTile::max_resY = -1;
show_all_children();
}
void
VmMap::open_map(void)
{
Gtk::FileChooserDialog d("Select map from folder", Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
d.set_transient_for(*mainWindow);
d.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
d.add_button("Select", Gtk::RESPONSE_OK);
if (d.run() != Gtk::RESPONSE_OK)
return;
mw_out << __FUNCTION__ << ": load map from '" << d.get_filename() << "'." << endl;
remove_map();
current_path = d.get_filename();
reload_unplaced_tiles();
if (!load_settings()) {
ctrls->set_crop(def_cry, def_cry, def_crx, def_crx);
ctrls->set_zoom(def_zoom, def_zoom);
}
}
void
VmMap::export_map(void)
{
//mw_out << __FUNCTION__ << ": called." << endl;
update_state();
if (!tiles_placed) {
mw_out << __FUNCTION__ << ": no tiles placed, nothing to export." << endl;
return;
}
map_preview->set_header("Map: " + current_path);
auto t = std::chrono::system_clock::now();
time_t tt = chrono::system_clock::to_time_t(t);
struct tm * p = localtime(&tt);
char ttc[256];
strftime(ttc, 256, "%c", p);
map_preview->set_footer(string("mapped on ") +
ttc + " by " +
Glib::get_user_name());
map_preview->render_preview();
preview_win->show_all_children();
preview_win->show();
Glib::RefPtr<Gio::File> fnp = Gio::File::create_for_path(current_path);
string s = string(current_path) + G_DIR_SEPARATOR_S + fnp->get_basename() + ".png";
mw_out << __FUNCTION__ << ": " << s << endl;
file_chooser->set_current_folder(current_path);
file_chooser->set_current_name(s);
}
void
VmMap::export_map_updatefn(void)
{
/*
mw_out << __FUNCTION__ << ": cf=" << file_chooser->get_current_folder() << endl;
mw_out << __FUNCTION__ << ": fn=" << file_chooser->get_filename() << endl;
*/
string f = string(file_chooser->get_filename());
if (f == "") f = "vice-map.png"; // if user has deleted the entry field
Glib::RefPtr<Gio::File> fnp = Gio::File::create_for_path(f);
string s = string(file_chooser->get_current_folder()) + G_DIR_SEPARATOR_S + fnp->get_basename();
mw_out << __FUNCTION__ << ": " << s << endl;
file_chooser->set_current_name(s);
}
void
VmMap::export_map_commit(bool save)
{
if (!save) {
preview_win->hide();
return;
}
string fn = file_chooser->get_filename();
if (fn == "")
return;
std::cmatch cm;
std::regex re_ext("(.*)\\.(png|PNG)");
std::regex_match(fn.c_str(), cm, re_ext, std::regex_constants::match_default);
if (cm.size() <= 0) {
fn += ".png";
}
Glib::RefPtr<Gio::File> fnp = Gio::File::create_for_path(fn);
if (fnp->query_exists()) {
if (VmMsg("Overwrite existing file?", fn).run() != Gtk::RESPONSE_OK)
return;
}
map_preview->save(fn);
preview_win->hide();
}
void
VmMap::print(void)
{
po = VmMap::MapPrint::create();
po->set_default_page_setup(pr_pagesettings);
po->set_print_settings(pr_settings);
switch(po->run()) {
case Gtk::PRINT_OPERATION_RESULT_APPLY:
mw_out << __FUNCTION__ << ": printing finished successfully." << endl;
break;
case Gtk::PRINT_OPERATION_RESULT_ERROR:
mw_err << __FUNCTION__ << ": printing failed." << endl;
break;
default:
break;
}
pr_settings = po->get_print_settings();
}
VmMap::MapPreview::MapPreview()
{
set_hexpand(TRUE);
set_vexpand(TRUE);
header = footer = nullptr;
builder->get_widget("VmMapPreviewHeader", header);
builder->get_widget("VmMapPreviewFooter", footer);
Gtk::Button *p = nullptr;
builder->get_widget("VmMapPreviewCancel", p);
p->set_image_from_icon_name("window-close-symbolic");
p = nullptr;
builder->get_widget("VmMapPreviewSave", p);
p->set_image_from_icon_name("document-save-symbolic");
p = nullptr;
builder->get_widget("VmMapPreviewPrint", p);
p->set_image_from_icon_name("document-print-symbolic");
}
void
VmMap::MapPreview::render_preview(void)
{
Glib::RefPtr<Gdk::Pixbuf> tile_img;
int x, y, dxk, dyk, out_xres, out_yres;
out_xres = out_yres = 0;
// calculate width of output image
for (x = VmTile::xmin+1; x < VmTile::xmax; x++) {
mw_map->get_tile(x, VmTile::ymin)->get_cropped_dimensions(dxk, dyk); // dyk not used
out_xres += dxk;
}
// calculate height of output image
for (y = VmTile::ymin; y <= VmTile::ymax; y++) {
mw_map->get_tile(VmTile::xmin, y)->get_cropped_dimensions(dxk, dyk); // dxk not used
out_yres += dyk;
}
out_image = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, TRUE, 8, out_xres, out_yres);
dyk = 0;
for (y = VmTile::ymin; y <= VmTile::ymax; y++) {
dxk = 0;
for (x = VmTile::xmin+1; x < VmTile::xmax; x++) {
tile_img = mw_map->get_tile(x, y)->get_cropped_image();
tile_img->copy_area(0, 0, tile_img->get_width(), tile_img->get_height(), out_image, dxk, dyk);
dxk += tile_img->get_width();
}
dyk += tile_img->get_height();
}
Cairo::RefPtr<Cairo::Surface> imgs = Cairo::ImageSurface::create(Cairo::FORMAT_RGB24, out_xres, 100);
Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(imgs);
Glib::RefPtr<Gdk::Pixbuf> text;
Pango::FontDescription font;
font.set_family("Arial Rounded Mt Bold");
font.set_size(32 * PANGO_SCALE);
// header
cr->set_source_rgb(0.8, 0.8, 0.8);
cr->rectangle(0, 0, out_xres, 100);
cr->fill();
cr->set_source_rgb(0.2, 0.2, 0.2);
auto headerlayout = create_pango_layout(header->get_text());
headerlayout->set_font_description(font);
cr->move_to(1, 1);
headerlayout->show_in_cairo_context(cr);
cr->set_source_rgb(0.2, 0.2, 0.2);
cr->rectangle(0, 0, out_xres, 100);
cr->stroke();
imgs = cr->get_target();
text = Gdk::Pixbuf::create(imgs, 0, 0, out_xres, 100);
text->copy_area(0, 0, out_xres, 100, out_image, 0, 0);
// footer
font.set_size(20 * PANGO_SCALE);
cr->set_source_rgb(0.8, 0.8, 0.8);
cr->rectangle(0, 0, out_xres, 80);
cr->fill();
cr->set_source_rgb(0.2, 0.2, 0.2);
cr->move_to(1, 1);
auto footerlayout = create_pango_layout(footer->get_text());
footerlayout->set_font_description(font);
footerlayout->show_in_cairo_context(cr);
auto vm_text = create_pango_layout(" --- powered by vice-mapper");
vm_text->set_font_description(font);
int w, h;
vm_text->get_pixel_size(w, h);
cr->move_to(out_xres - w - 1, 1);
vm_text->show_in_cairo_context(cr);
cr->set_source_rgb(0.2, 0.2, 0.2);
cr->rectangle(0, 0, out_xres, 80);
cr->stroke();
imgs = cr->get_target();
text = Gdk::Pixbuf::create(imgs, 0, 0, out_xres, 80);
text->copy_area(0, 0, out_xres, 80, out_image, 0, out_image->get_height() - 80);
// mw_out << __FUNCTION__ << ": out_image = " << out_image->get_width() << "x" << out_image->get_height() << endl;
queue_draw();
}
void
VmMap::MapPreview::save(std::string name)
{
try {
mw_out << __FUNCTION__ << ": " << name << endl;
out_image->save(name, "png");
}
catch (Gdk::PixbufError &ex) {
mw_out << __FUNCTION__ << ": failed - " << ex.what() << endl;
}
}
bool
VmMap::MapPreview::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
//mw_out << __FUNCTION__ << ": called." << endl;
Glib::RefPtr<Gdk::Pixbuf> scaled =
Gdk::Pixbuf::create(out_image->get_colorspace(),
out_image->get_has_alpha(),
out_image->get_bits_per_sample(),
out_image->get_width(),
out_image->get_height());
out_image->copy_area(0, 0,
out_image->get_width(),
out_image->get_height(),
scaled, 0, 0);
scaled = scaled->scale_simple(get_allocated_width(), get_allocated_height(),
Gdk::INTERP_BILINEAR);
Gdk::Cairo::set_source_pixbuf(cr, scaled);
cr->paint();
return TRUE;
}
VmMap::MapPrint::MapPrint()
{
mw_out << __FUNCTION__ << ": created." << endl;
}
VmMap::MapPrint::~MapPrint()
{
// no mw_out possible, as stream has already being destroyed.
}
Glib::RefPtr<VmMap::MapPrint> VmMap::MapPrint::create()
{
return Glib::RefPtr<VmMap::MapPrint> (new VmMap::MapPrint());
}
void
VmMap::MapPrint::on_begin_print(const Glib::RefPtr<Gtk::PrintContext>& context)
{
mw_out << __FUNCTION__ << ": called." << endl;
set_n_pages(1);
}
void
VmMap::MapPrint::on_request_page_setup(const Glib::RefPtr<Gtk::PrintContext>& context,
int page_no, const Glib::RefPtr<Gtk::PageSetup>& psetup)
{
mw_out << __FUNCTION__ << ": called." << endl;
Gtk::PaperSize ps = psetup->get_paper_size();
double w, h, to, bo, le, ri;
/*
w = ps.get_width(Gtk::UNIT_POINTS);
h = ps.get_height(Gtk::UNIT_POINTS);
*/
w = context->get_width();
h = context->get_height();
to = psetup->get_top_margin(Gtk::UNIT_POINTS);
bo = psetup->get_bottom_margin(Gtk::UNIT_POINTS);
le = psetup->get_left_margin(Gtk::UNIT_POINTS);
ri = psetup->get_right_margin(Gtk::UNIT_POINTS);
mw_out << __FUNCTION__ << ": paper wxh=" << w << "x" << h
<< ", to/bo/le/ri=" << to << "/" << bo << "/" << le << "/" << ri
<< endl;
Glib::RefPtr<Gdk::Pixbuf> tmp = mw_map->get_out_image();
print_image = Gdk::Pixbuf::create(tmp->get_colorspace(),
tmp->get_has_alpha(),
tmp->get_bits_per_sample(),
tmp->get_width(),
tmp->get_height());
tmp->copy_area(0, 0, tmp->get_width(), tmp->get_height(), print_image, 0, 0);
print_image = print_image->scale_simple(w-le-ri, h-to-bo, Gdk::INTERP_BILINEAR);
}
void
VmMap::MapPrint::on_draw_page(const Glib::RefPtr<Gtk::PrintContext>& context, int page_nr)
{
mw_out << __FUNCTION__ << ": called." << endl;
Cairo::RefPtr<Cairo::Context>cr = context->get_cairo_context();
mw_out << __FUNCTION__ << ": wxh=" << print_image->get_width() << "x" << print_image->get_height() << endl;
mw_debug->put_pixbuf(print_image);
Gdk::Cairo::set_source_pixbuf(cr, print_image);
cr->paint();
}
extern "C"
{
G_MODULE_EXPORT void
on_VmMapPreviewHeader_activate(void)
{
// mw_out << __FUNCTION__ << ": called." << endl;
mw_map->update_preview();
}
G_MODULE_EXPORT void
on_VmMapPreviewFooter_activate(void)
{
// mw_out << __FUNCTION__ << ": called." << endl;
mw_map->update_preview();
}
G_MODULE_EXPORT void
on_VmMapPreviewFileChooser_current_folder_changed(void)
{
mw_out << __FUNCTION__ << ": called." << endl;
mw_map->export_map_updatefn();
}
G_MODULE_EXPORT void
on_VmMapPreviewSave_clicked(void)
{
// mw_out << __FUNCTION__ << ": called." << endl;
mw_map->export_map_commit(true);
}
G_MODULE_EXPORT void
on_VmMapPreviewPrint_clicked(void)
{
mw_out << __FUNCTION__ << ": called." << endl;
//VmMsg("Print not yet implemented!", ":-(").run();
mw_map->print();
}
G_MODULE_EXPORT void
on_VmMapPreviewCancel_clicked(void)
{
// mw_out << __FUNCTION__ << ": called." << endl;
mw_map->export_map_commit(false);
}
}