-
Notifications
You must be signed in to change notification settings - Fork 20
/
boxedit.c
2446 lines (2046 loc) · 65.1 KB
/
boxedit.c
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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ScummC
* Copyright (C) 2004-2006 Alban Bedel
*
* This program 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 2
* of the License, or (at your option) any later version.
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/**
* @file boxedit.c
* @brief Gtk based box editor
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "scc_fd.h"
#include "scc_util.h"
#include "scc_cost.h"
#include "scc.h"
#include "scc_param.h"
#include "scc_img.h"
#include "scc_box.h"
#include "boxedit_help.h"
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#define SCC_UNDO_NEW_PTS 1
#define SCC_UNDO_SELECT_PTS 2
#define SCC_UNDO_UNSELECT_PTS 3
#define SCC_UNDO_SELECT_NEXT_PTS 4
#define SCC_UNDO_NEW_BOX 5
#define SCC_UNDO_SELECT_BOX 6
#define SCC_UNDO_UNSELECT_BOX 7
#define SCC_UNDO_SELECT_NEXT_BOX 8
#define SCC_UNDO_MOVE 9
#define SCC_UNDO_UNSELECT_ALL 10
#define SCC_UNDO_DESTROY 11
#define SCC_UNDO_FLAG 12
#define SCC_UNDO_NAME 13
#define SCC_UNDO_SCALE 14
#define SCC_UNDO_MASK 15
#define SCC_UNDO_UNDO 16
typedef struct scc_boxedit_undo_st scc_boxedit_undo_t;
struct scc_boxedit_undo_st {
scc_boxedit_undo_t* next;
int op;
scc_box_t* box;
scc_box_t* box_b;
scc_box_t* box_c;
int pts[4];
int undone;
};
#define SCC_NUM_SCALE_SLOT 4
typedef struct scc_boxedit_st {
// box data
scc_box_t* boxes;
// scale slots
scc_scale_slot_t scale_slots[SCC_NUM_SCALE_SLOT];
// room image
scc_img_t* rmim;
// the view
uint8_t* view_data;
int view_w,view_h;
int view_off_x,view_off_y;
int zoom;
// the main gtk window
GtkWidget* win;
// the drawing area
GtkWidget* da;
// the da scrollbox, we need bcs it take the key events
GtkWidget* da_scroll;
// the status bar
GtkWidget* sbar;
unsigned int sbar_ctx_coord;
unsigned int sbar_ctx_msg;
int sbar_have_msg;
// the scale slots window
GtkWidget* scale_win;
// the box entries
scc_box_t* edit_box;
GtkWidget* name_entry;
GtkWidget* mask_entry;
GtkWidget* scale_val;
GtkWidget* scale_slot;
GtkWidget* scale_slot_entries[4*SCC_NUM_SCALE_SLOT];
GtkWidget* scale_entry;
GtkWidget* flag_toggle[8];
int hide_plane[SCC_MAX_IM_PLANES];
// gc stuff
GdkGC* img_gc;
GdkRgbCmap *img_cmap;
GdkColor box_color; // box colors
GdkColor pts_color; // point color
GdkColor sel_color; // selected element color
GdkColor edit_color; // edit box color
int nsel;
int drag,drag_x,drag_y;
int drag_move_x,drag_move_y;
// undo
scc_boxedit_undo_t* undo;
// undo level at last save
scc_boxedit_undo_t* last_save;
int changed;
// current file
char* filename;
int save_as;
int open_img;
} scc_boxedit_t;
static int scc_boxm_size_from_matrix(uint8_t* matrix,int size) {
int i,j,pos = 0;
for(i = 0 ; i < size ; i++) {
pos++;
for(j = 0 ; j < size ; j++) {
uint8_t v = matrix[i*size+j];
if(v == 255) continue;
while(j < size-1 && v == matrix[j]) j++;
pos += 3;
}
}
pos++;
return pos;
}
int scc_boxedit_read_scale(scc_boxedit_t* be,
scc_fd_t* fd) {
uint32_t type,len;
int i;
while(1) {
type = scc_fd_r32(fd);
len = scc_fd_r32be(fd);
if(type == MKID('B','O','X','M') && len > 8) {
scc_fd_seek(fd,len-8,SEEK_CUR);
continue;
}
if(type == MKID('S','C','A','L'))
break;
return 0;
}
if(len != SCC_NUM_SCALE_SLOT*8+8) {
printf("SCAL block has an invalid length: %d, should be 40.\n",len);
return 0;
}
for(i = 0 ; i < SCC_NUM_SCALE_SLOT ; i++) {
be->scale_slots[i].s1 = scc_fd_r16le(fd);
be->scale_slots[i].y1 = scc_fd_r16le(fd);
be->scale_slots[i].s2 = scc_fd_r16le(fd);
be->scale_slots[i].y2 = scc_fd_r16le(fd);
}
return 1;
}
scc_box_t* scc_boxedit_load_box(scc_boxedit_t* be, char* path) {
scc_box_t* list=NULL,*last=NULL,*b;
scc_fd_t* fd = new_scc_fd(path,O_RDONLY,0);
uint32_t type,len,pos = 8;
int nlen,x,y;
if(!fd) {
printf("Failed to open %s.\n",path);
return NULL;
}
type = scc_fd_r32(fd);
len = scc_fd_r32be(fd);
if((type != MKID('B','O','X','D')) && (type != MKID('b','o','x','d'))) {
printf("%s is not a boxd file.\n",path);
scc_fd_close(fd);
return NULL;
}
// skip the first box on the original format
if(type == MKID('B','O','X','D')) {
scc_fd_r16le(fd); pos += 2;
scc_fd_seek(fd,20,SEEK_CUR);
pos += 20;
}
while(pos < len) {
b = calloc(1,sizeof(scc_box_t));
if(type == MKID('b','o','x','d')) {
nlen = scc_fd_r8(fd); pos++;
if(nlen > 0) {
b->name = malloc(nlen+1);
scc_fd_read(fd,b->name,nlen); pos += nlen;
b->name[nlen] = '\0';
}
}
x = (int16_t)scc_fd_r16le(fd); pos += 2;
y = (int16_t)scc_fd_r16le(fd); pos += 2;
scc_box_add_pts(b,x,y);
x = (int16_t)scc_fd_r16le(fd); pos += 2;
y = (int16_t)scc_fd_r16le(fd); pos += 2;
scc_box_add_pts(b,x,y);
x = (int16_t)scc_fd_r16le(fd); pos += 2;
y = (int16_t)scc_fd_r16le(fd); pos += 2;
scc_box_add_pts(b,x,y);
x = (int16_t)scc_fd_r16le(fd); pos += 2;
y = (int16_t)scc_fd_r16le(fd); pos += 2;
scc_box_add_pts(b,x,y);
b->mask = scc_fd_r8(fd); pos++;
b->flags = scc_fd_r8(fd); pos++;
b->scale = scc_fd_r16le(fd); pos += 2;
SCC_LIST_ADD(list,last,b);
#if 0
printf("load box:");
for(x = 0 ; x < b->npts ; x++)
printf(" (%dx%d)",b->pts[x].x,b->pts[x].y);
printf("\n");
#endif
}
// try to read scal, it might be there
scc_boxedit_read_scale(be,fd);
scc_fd_close(fd);
if(be->filename) free(be->filename);
be->filename = strdup(path);
if(be->boxes) scc_box_list_free(be->boxes);
be->boxes = list;
return list;
}
int scc_box_size(scc_box_t* box) {
int size = 0;
while(box) {
size += 1;
if(box->name) size += strlen(box->name);
size += 2*8 + 1 + 1 + 2;
box = box->next;
}
return size;
}
int scc_boxedit_save(scc_boxedit_t* be, char* path) {
scc_fd_t* fd;
scc_box_t* box;
int i,j,up,up2;
int len;
uint8_t* boxm;
if(!be->boxes) {
printf("No box to save.\n");
return 0;
}
fd = new_scc_fd(path,O_WRONLY|O_CREAT|O_TRUNC,0);
if(!fd) {
printf("Failed to open %s\n",path);
return 0;
}
scc_fd_w32(fd,MKID('b','o','x','d'));
scc_fd_w32be(fd,8 + scc_box_size(be->boxes));
for(box = be->boxes ; box ; box = box->next) {
if(!box->name)
scc_fd_w8(fd,0);
else {
scc_fd_w8(fd,strlen(box->name));
scc_fd_write(fd,box->name,strlen(box->name));
}
// find the top point
up = 0;
for(i = 1 ; i < box->npts ; i++)
if(box->pts[i].y < box->pts[up].y) up = i;
// find the 2 top point
up2 = -1;
for(i = 0 ; i < box->npts ; i++) {
if(i == up) continue;
if(up2 == -1 || box->pts[i].y < box->pts[up2].y) up2 = i;
}
// take the leftest, note that if both are equal we still keep the
// highest
if(box->pts[up2].x < box->pts[up].x) up = up2;
if(box->npts == 2) {
for(i = 0 ; i < 2 ; i++) {
scc_fd_w16le(fd,box->pts[up].x);
scc_fd_w16le(fd,box->pts[up].y);
}
up = (up+1)%box->npts;
for(i = 0 ; i < 2 ; i++) {
scc_fd_w16le(fd,box->pts[up].x);
scc_fd_w16le(fd,box->pts[up].y);
}
} else {
for(i = up ; i < up + 4 ; i++) {
scc_fd_w16le(fd,box->pts[i%box->npts].x);
scc_fd_w16le(fd,box->pts[i%box->npts].y);
}
}
scc_fd_w8(fd,box->mask);
scc_fd_w8(fd,box->flags);
scc_fd_w16le(fd,box->scale);
}
// boxm generation
len = scc_box_get_matrix(be->boxes,&boxm);
#if 0
for(i = 0 ; i < len ; i++) {
printf("Box %2d :",i);
for(j = 0 ; j < len ; j++)
printf(" %d",boxm[i*len+j]);
printf("\n");
}
#endif
scc_fd_w32(fd,MKID('B','O','X','M'));
scc_fd_w32be(fd,8 + scc_boxm_size_from_matrix(boxm,len));
for(i = 0 ; i < len ; i++) {
scc_fd_w8(fd,0xFF);
for(j = 0 ; j < len ; j++) {
uint8_t v = boxm[i*len+j];
if(v == 255) continue;
scc_fd_w8(fd,j);
while(j < len-1 && v == boxm[j]) j++;
scc_fd_w8(fd,j);
scc_fd_w8(fd,v);
}
}
scc_fd_w8(fd,0xFF);
// scal
scc_fd_w32(fd,MKID('S','C','A','L'));
scc_fd_w32be(fd,8 + 8*SCC_NUM_SCALE_SLOT);
for(i = 0 ; i < SCC_NUM_SCALE_SLOT ; i++) {
scc_fd_w16le(fd,be->scale_slots[i].s1);
scc_fd_w16le(fd,be->scale_slots[i].y1);
scc_fd_w16le(fd,be->scale_slots[i].s2);
scc_fd_w16le(fd,be->scale_slots[i].y2);
}
scc_fd_close(fd);
return 1;
}
/*
#define SWAP(a,b) t = a; a = b; b = t;
static int is_near_line(scc_box_pts_t* a,scc_box_pts_t* b, int x, int y) {
float s;
int th = 4,r = 0,c,z,w,swap = 0;
if(abs(a->x - b->x) < abs(a->y - b->y)) {
int t;
swap = 1;
SWAP(a->x,a->y);
SWAP(b->x,b->y);
SWAP(x,y);
}
if(a->x > b->x) {
scc_box_pts_t* t;
SWAP(a,b);
}
do {
if(x < a->x - th || x > b->x + th) break;
if(a->x == b->x) {
if(a->y < b->y) {
if(y < a->y - th || y > b->y + th) break;
} else {
if(y < b->y - th || y > a->y + th) break;
}
r = 1;
break;
}
s = ((float)(b->y-a->y))/(b->x-a->x);
c = a->y - s*a->x;
w = x*s+c;
z = sqrt((s*s)+1);
if(abs((y-w)*z) > th) break;
r = 1;
} while(0);
if(swap) {
int t;
SWAP(a->x,a->y);
SWAP(b->x,b->y);
}
return r;
}
*/
static int is_near_point(scc_box_pts_t* a,int x, int y,int th) {
int dx = a->x - x;
int dy = a->y - y;
if(th < 1) th = 1;
return (sqrt((dx*dx)+(dy*dy)) > th) ? 0 : 1;
}
static int is_box_visible(scc_boxedit_t* be, scc_box_t* box) {
int m = box->mask;
if(m < 0) m = 0;
if(m >= SCC_MAX_IM_PLANES) m = SCC_MAX_IM_PLANES-1;
return be->hide_plane[m] ? 0 : 1;
}
static int can_move_box(scc_box_t* box,int x, int y) {
scc_box_pts_t pts[4];
int i;
// it's not working properly and a bit useless currently
// so disable it for now
return 1;
// non-square box can't be bad
if(box->npts < 4) return 1;
for(i = 0 ; i < box->npts ; i++) {
pts[i] = box->pts[i];
if(!box->pts[i].selected) continue;
pts[i].x += x;
pts[i].y += y;
}
if(pts[0].x > pts[1].x || pts[1].y > pts[2].y ||
pts[2].x < pts[3].x || pts[3].y < pts[0].y) return 0;
return 1;
}
static void activate_name_cb(GtkEntry *entry,scc_boxedit_t* be);
static void scc_boxedit_set_edit_box(scc_boxedit_t* be, scc_box_t* box) {
int i;
if(be->edit_box == box) return;
// it's set to null so the callback doesn't touch it
be->edit_box = NULL;
if(!box) {
gtk_widget_set_sensitive(be->name_entry,0);
gtk_entry_set_text(GTK_ENTRY(be->name_entry),"");
gtk_widget_set_sensitive(be->mask_entry,0);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(be->mask_entry),0);
gtk_widget_set_sensitive(be->scale_entry,0);
gtk_widget_set_sensitive(be->scale_val,0);
gtk_widget_set_sensitive(be->scale_slot,0);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(be->scale_entry),0);
for(i = 3 ; i < 8 ; i++) {
gtk_widget_set_sensitive(be->flag_toggle[i],0);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(be->flag_toggle[i]),0);
}
return;
}
gtk_widget_set_sensitive(be->name_entry,1);
gtk_entry_set_text(GTK_ENTRY(be->name_entry),box->name ? box->name : "");
gtk_widget_set_sensitive(be->mask_entry,1);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(be->mask_entry),box->mask);
gtk_widget_set_sensitive(be->scale_entry,1);
gtk_widget_set_sensitive(be->scale_val,1);
gtk_widget_set_sensitive(be->scale_slot,1);
if(box->scale & 0x8000) {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(be->scale_slot),1);
gtk_spin_button_set_range(GTK_SPIN_BUTTON(be->scale_entry),0,3);
} else {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(be->scale_val),1);
gtk_spin_button_set_range(GTK_SPIN_BUTTON(be->scale_entry),1,255);
}
gtk_spin_button_set_value(GTK_SPIN_BUTTON(be->scale_entry),box->scale & 0xFF);
for(i = 3 ; i < 8 ; i++) {
gtk_widget_set_sensitive(be->flag_toggle[i],1);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(be->flag_toggle[i]),box->flags & (1 << i));
}
be->edit_box = box;
}
static void scc_boxedit_status_msg(scc_boxedit_t* be,char* msg, ...) {
char txt[1024];
va_list ap;
va_start(ap, msg);
vsprintf(txt,msg,ap);
va_end(ap);
if(be->sbar_have_msg)
gtk_statusbar_pop(GTK_STATUSBAR(be->sbar),be->sbar_ctx_msg);
gtk_statusbar_push(GTK_STATUSBAR(be->sbar),be->sbar_ctx_msg,txt);
be->sbar_have_msg = 1;
}
static int scc_boxedit_sel_pts(scc_boxedit_t* be,scc_box_t* box,int p) {
if(p < 0 || p > 3) return 0;
if(box->pts[p].selected) return 0;
box->pts[p].selected = 1;
box->nsel++;
be->nsel++;
//if(box->nsel == box->npts || be->edit_box == NULL)
scc_boxedit_set_edit_box(be,box);
return 1;
}
static int scc_boxedit_unsel_pts(scc_boxedit_t* be,scc_box_t* box,int p) {
if(p < 0 || p > 3) return 0;
if(!box->pts[p].selected) return 0;
box->pts[p].selected = 0;
box->nsel--;
be->nsel--;
if(box == be->edit_box && box->nsel == 0)
scc_boxedit_set_edit_box(be,NULL);
return 1;
}
static int scc_boxedit_sel_next_pts(scc_boxedit_t* be,scc_box_t* box_a,int p_a,
scc_box_t* box_b,int p_b) {
if(scc_boxedit_unsel_pts(be,box_a,p_a) &&
scc_boxedit_sel_pts(be,box_b,p_b)) return 1;
return 0;
}
static int scc_boxedit_sel_box(scc_boxedit_t* be,scc_box_t* box) {
int i,r=0;
for(i = 0 ; i < box->npts ; i++) {
if(box->pts[i].selected) continue;
r=1;
box->pts[i].selected = 1;
box->nsel++;
be->nsel++;
}
scc_boxedit_set_edit_box(be,box);
return r;
}
static int scc_boxedit_unsel_box(scc_boxedit_t* be,scc_box_t* box) {
int i,r=0;
for(i = 0 ; i < box->npts ; i++) {
if(!box->pts[i].selected) continue;
r=1;
box->pts[i].selected = 0;
box->nsel--;
be->nsel--;
}
if(box == be->edit_box)
scc_boxedit_set_edit_box(be,NULL);
return r;
}
static int scc_boxedit_sel_next_box(scc_boxedit_t* be,scc_box_t* box_a,
scc_box_t* box_b) {
if(scc_boxedit_unsel_box(be,box_a) &&
scc_boxedit_sel_box(be,box_b)) return 1;
return 0;
}
static int scc_boxedit_sel_move(scc_boxedit_t* be,int x, int y) {
scc_box_t* box;
int i;
// first check if the move is valid
for(box = be->boxes ; box ; box = box->next)
if(!can_move_box(box,x,y)) return 0;
for(box = be->boxes ; box ; box = box->next) {
for(i = 0 ; i < box->npts ; i++) {
if(!box->pts[i].selected) continue;
box->pts[i].x += x;
box->pts[i].y += y;
}
}
return 1;
}
static scc_box_t* scc_boxedit_new_box(scc_boxedit_t* be) {
scc_box_t *b,* box = calloc(1,sizeof(scc_box_t));
if(be->boxes) {
for(b = be->boxes ; b->next ; b = b->next);
b->next = box;
} else
be->boxes = box;
box->npts = 4;
box->pts[1].x = 10;
box->pts[2].x = 10;
box->pts[2].y = 10;
box->pts[3].y = 10;
box->scale = 255;
return box;
}
static scc_boxedit_undo_t* scc_boxedit_get_undo(scc_boxedit_t* be,int op) {
scc_boxedit_undo_t* undo = calloc(1,sizeof(scc_boxedit_undo_t));
be->changed = 1;
undo->op = op;
undo->next = be->undo;
be->undo = undo;
return undo;
}
static int scc_boxedit_undo_action(scc_boxedit_t* be,
scc_boxedit_undo_t* undo) {
int n,end;
scc_boxedit_undo_t* u2;
scc_box_t* b;
char* txt;
switch(undo->op) {
case SCC_UNDO_SELECT_PTS:
if(undo->undone)
scc_boxedit_sel_pts(be,undo->box,undo->pts[0]);
else {
scc_boxedit_unsel_pts(be,undo->box,undo->pts[0]);
scc_boxedit_set_edit_box(be,undo->box_b);
}
break;
case SCC_UNDO_UNSELECT_PTS:
if(undo->undone)
scc_boxedit_unsel_pts(be,undo->box,undo->pts[0]);
else {
scc_boxedit_sel_pts(be,undo->box,undo->pts[0]);
scc_boxedit_set_edit_box(be,undo->box_b);
}
break;
case SCC_UNDO_SELECT_NEXT_PTS:
if(undo->undone)
scc_boxedit_sel_next_pts(be,undo->box,undo->pts[0],
undo->box_b,undo->pts[1]);
else {
scc_boxedit_sel_next_pts(be,undo->box_b,undo->pts[1],
undo->box,undo->pts[0]);
scc_boxedit_set_edit_box(be,undo->box_c);
}
break;
case SCC_UNDO_NEW_BOX:
if(undo->undone) {
// re-add it
if(!be->boxes || be->boxes == undo->box->next)
be->boxes = undo->box;
else {
for(b = be->boxes ; b->next != undo->box->next ; b = b->next);
b->next = undo->box;
}
scc_boxedit_set_edit_box(be,undo->box);
be->nsel += 4;
} else {
// remove it from the list
if(undo->box == be->boxes)
be->boxes = undo->box->next;
else {
for(b = be->boxes ; b->next != undo->box ; b = b->next);
b->next = undo->box->next;
}
scc_boxedit_set_edit_box(be,undo->box_b);
be->nsel -= 4;
//undo->box->next = NULL;
}
break;
case SCC_UNDO_SELECT_BOX:
if(undo->undone)
scc_boxedit_sel_box(be,undo->box);
else {
for(n = 0 ; n < 4 && undo->pts[n] >= 0 ; n++)
scc_boxedit_unsel_pts(be,undo->box,undo->pts[n]);
scc_boxedit_set_edit_box(be,undo->box_b);
}
break;
case SCC_UNDO_UNSELECT_BOX:
if(undo->undone)
scc_boxedit_unsel_box(be,undo->box);
else {
for(n = 0 ; n < 4 && undo->pts[n] >= 0 ; n++)
scc_boxedit_sel_pts(be,undo->box,undo->pts[n]);
scc_boxedit_set_edit_box(be,undo->box_b);
}
break;
case SCC_UNDO_SELECT_NEXT_BOX:
if(undo->undone)
scc_boxedit_sel_next_box(be,undo->box,undo->box_b);
else {
scc_boxedit_sel_next_box(be,undo->box_b,undo->box);
for(n = 0 ; n < 4 && undo->pts[n] >= 0 ; n++)
scc_boxedit_sel_pts(be,undo->box_b,undo->pts[n]);
scc_boxedit_set_edit_box(be,undo->box_c);
}
break;
case SCC_UNDO_MOVE:
if(undo->undone)
scc_boxedit_sel_move(be,undo->pts[0],undo->pts[1]);
else
scc_boxedit_sel_move(be,-undo->pts[0],-undo->pts[1]);
break;
case SCC_UNDO_UNSELECT_ALL:
case SCC_UNDO_DESTROY:
b = be->boxes;
be->boxes = undo->box;
undo->box = b;
n = be->nsel;
be->nsel = undo->pts[0];
undo->pts[0] = n;
if(be->edit_box || undo->box_b) {
b = be->edit_box;
scc_boxedit_set_edit_box(be,undo->box_b);
undo->box_b = b;
}
break;
case SCC_UNDO_NAME:
b = be->edit_box;
be->edit_box = NULL;
txt = b->name;
gtk_entry_set_text(GTK_ENTRY(be->name_entry),(undo->box) ? (char*)undo->box : "");
b->name = (char*)undo->box;
undo->box = (scc_box_t*)txt;
be->edit_box = b;
break;
case SCC_UNDO_FLAG:
b = be->edit_box;
be->edit_box = NULL;
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(be->flag_toggle[undo->pts[0]]),(b->flags & (1<<undo->pts[0])) ? 0 : 1);
if(b->flags & (1<<undo->pts[0]))
b->flags &= ~(1<<undo->pts[0]);
else
b->flags |= (1<<undo->pts[0]);
be->edit_box = b;
break;
case SCC_UNDO_SCALE:
n = be->edit_box->scale;
b = be->edit_box;
be->edit_box = NULL;
b->scale = undo->pts[0];
undo->pts[0] = n;
if(b->scale & 0x8000) {
if(!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(be->scale_slot))) {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(be->scale_slot),1);
gtk_spin_button_set_range(GTK_SPIN_BUTTON(be->scale_entry),0,3);
}
} else {
if(!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(be->scale_val))) {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(be->scale_val),1);
gtk_spin_button_set_range(GTK_SPIN_BUTTON(be->scale_entry),1,255);
}
}
gtk_spin_button_set_value(GTK_SPIN_BUTTON(be->scale_entry),b->scale & 0xFF);
be->edit_box = b;
break;
case SCC_UNDO_MASK:
n = be->edit_box->mask;
b = be->edit_box;
be->edit_box = NULL;
gtk_spin_button_set_value(GTK_SPIN_BUTTON(be->mask_entry),undo->pts[0]);
b->mask = undo->pts[0];
undo->pts[0] = n;
be->edit_box = b;
break;
case SCC_UNDO_UNDO:
end = undo->pts[0];
if(undo->undone) {
for(u2 = undo->next,n = 0 ; u2 && n < end ; n++, u2= u2->next)
scc_boxedit_undo_action(be,u2);
} else {
do {
for(u2 = undo->next,n = 1 ; u2 && n < end ; n++, u2= u2->next);
scc_boxedit_undo_action(be,u2);
end--;
} while(end > 0);
}
break;
}
undo->undone ^= 1;
return 1;
}
static void scc_undo_free(scc_boxedit_undo_t* u) {
scc_boxedit_undo_t* n;
while(u) {
n = u->next;
switch(u->op) {
case SCC_UNDO_NEW_BOX:
if(u->undone) free(u->box);
break;
case SCC_UNDO_UNSELECT_ALL:
case SCC_UNDO_DESTROY:
scc_box_list_free(u->box);
break;
case SCC_UNDO_NAME:
free(u->box);
break;
}
free(u);
u = n;
}
}
static char* scc_undo_name(scc_boxedit_undo_t* u) {
char* name;
switch(u->op) {
case SCC_UNDO_SELECT_PTS:
name = "point selection";
break;
case SCC_UNDO_UNSELECT_PTS:
name = "point unselection";
break;
case SCC_UNDO_SELECT_NEXT_PTS:
name = "next point selection";
break;
case SCC_UNDO_NEW_BOX:
name = "new box creation";
break;
case SCC_UNDO_SELECT_BOX:
name = "box selection";
break;
case SCC_UNDO_UNSELECT_BOX:
name = "box unselection";
break;
case SCC_UNDO_SELECT_NEXT_BOX:
name = "next box selection";
break;
case SCC_UNDO_MOVE:
name = "selection move";
break;
case SCC_UNDO_UNSELECT_ALL:
name = "unselecting all";
break;
case SCC_UNDO_DESTROY:
name = "destroy selection";
break;
case SCC_UNDO_NAME:
name = "name setting";
break;
case SCC_UNDO_FLAG:
name = "flag setting";
break;
case SCC_UNDO_SCALE:
name = "scale setting";
break;
case SCC_UNDO_MASK:
name = "mask setting";
break;
case SCC_UNDO_UNDO:
name = "undo";
break;
default:
name = "some op";
}
return name;
}
static void scc_undo_print(scc_boxedit_undo_t* u) {
printf("%s %d %d\n",scc_undo_name(u),u->undone,u->pts[0]);
}
int scc_boxedit_undo(scc_boxedit_t* be) {
scc_boxedit_undo_t* undo = be->undo,*u2;
int skip = 0;
if(!undo) {
scc_boxedit_status_msg(be,"Nothing to undo");
return 0;
}
// if the last op is an undo, try to add another step
// to it
if(undo->op == SCC_UNDO_UNDO) {
skip = undo->pts[0]+1;
while(undo && skip > 0) {
skip--;
undo = undo->next;
}
if(!undo) {
scc_boxedit_status_msg(be,"Nothing left to undo");
return 0;
}
}
scc_boxedit_status_msg(be,"Undo %s",scc_undo_name(undo));
scc_boxedit_undo_action(be,undo);
if(be->undo->op == SCC_UNDO_UNDO) {
be->undo->pts[0]++;
} else {
u2 = scc_boxedit_get_undo(be,SCC_UNDO_UNDO);
u2->pts[0] = 1;
}
if(undo->next == be->last_save) be->changed = 0;
else be->changed = 1;
return 1;
}
int scc_boxedit_redo(scc_boxedit_t* be) {
scc_boxedit_undo_t* undo = be->undo, *u2;
int n;
if(!undo || undo->op != SCC_UNDO_UNDO) {
scc_boxedit_status_msg(be,"Nothing to redo");
return 0;
}
undo->pts[0]--;
for(u2 = undo->next,n = 0 ; u2 && n < undo->pts[0] ; n++, u2= u2->next);
if(!u2) {
scc_boxedit_status_msg(be,"Hmm, something went wrong with the redo");