-
Notifications
You must be signed in to change notification settings - Fork 57
/
counters.c
1413 lines (1191 loc) · 39.5 KB
/
counters.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
#include <stdlib.h>
#include <ctype.h>
#include "counters.h"
#include "context_menu.h"
#include "actors_list.h"
#include "asc.h"
#include "elconfig.h"
#include "elwindows.h"
#include "errors.h"
#include "hud.h"
#include "init.h"
#include "loginwin.h"
#ifdef JSON_FILES
#include "json_io.h"
#endif
#include "manufacture.h"
#include "multiplayer.h"
#include "named_colours.h"
#include "sound.h"
#include "spells.h"
#include "tabs.h"
#include "io/elpathwrapper.h"
#ifdef OPENGL_TRACE
#include "gl_init.h"
#endif
#include "translate.h"
#define NUM_COUNTERS 15
/* Counter IDs */
enum {
KILLS = 1,
DEATHS,
HARVESTS,
ALCHEMY,
CRAFTING,
MANUFACTURING,
POTIONS,
SPELLS,
SUMMONS,
ENGINEERING,
BREAKS,
MISC_EVENTS,
TAILORING,
CRIT_FAILS,
USED_ITEMS
};
/* Columns IDs */
enum {
NAME = 1,
SESSION,
TOTAL
};
static struct Counter *counters[NUM_COUNTERS];
static int counters_initialized = 0;
static int selected_counter_id = KILLS;
static int last_selected_counter_id = KILLS;
static int sort_counter_id = KILLS;
static int entries[NUM_COUNTERS];
static int sort_by[NUM_COUNTERS];
static int multiselect_id = -1;
static int NUM_LINES = 0;
static int mouseover_name = 0;
static int mouseover_session = 0;
static int mouseover_total = 0;
static int mouseover_entry_y = -1;
static int mouse_clicked = 0;
static int selected_entry = -1;
static int counters_show_win_help = 0;
static int product_count = 0;
static char product_name[128];
static char to_count_name[128];
static char *spell_names[128] = { NULL };
static int requested_spell_id = -1;
static int margin_y_len = 0;
static int left_panel_width = 0;
static int name_x_start = 0;
static int name_x_end = 0;
static int session_x_start = 0;
static int session_x_end = 0;
static int total_x_start = 0;
static int total_x_end = 0;
static int space_y = 0;
static int step_y = 0;
// put these in translate module
static const unsigned char *name_str = (const unsigned char*)"Name";
static const unsigned char *session_str = (const unsigned char*)"This Session";
static const unsigned char *total_str = (const unsigned char*)"Total";
static const unsigned char *totals_str = (const unsigned char*)"Totals:";
static const char *cat_str[NUM_COUNTERS] = { "Kills", "Deaths", "Harvests", "Alchemy", "Crafting", "Manufacturing",
"Potions", "Spells", "Summons", "Engineering", "Breakages", "Events", "Tailoring", "Crit Fails", "Used Items" };
static const char *temp_event_string[] =
{ "%s found a", /* keep this one first in the list as its different from the rest*/
"%s was blessed by the Queen of Nature with ", /* referenced laters as second in the list */
"A cavern wall collapsed on %s",
"Mother Nature got pissed off at %s",
"%s was stung by a bee, losing ",
"%s just hit a teleport nexus, ",
"While harvesting, %s upset a radon pouch, ",
"%s found Joker and got ",
"%s found Joker and failed to get",
"%s found 1 Pear.",
"While harvesting, %s lit up a match to check the dung level, ",
"While trying to harvest, the outhouse lid fell on %s, ",
"While harvesting some dung, Mother Nature was nearby taking a dump. Unfortunately %s ",
"You just got food poisoned!" };
static const char *count_str[] =
{ "dummy",
"Blessed by the Queen of Nature",
"A cavern wall collapsed",
"Mother Nature got pissed off",
"Stung by a bee",
"Hit a teleport nexus",
"Upset a radon pouch",
"Gift from Joker",
"Gift from Joker (lost)",
"Found a Pear",
"Explosion while harvesting dung",
"Outhouse lid accident",
"Mother Nature taking a dump",
"Food poisoned" };
static const int num_search_str = sizeof(count_str)/sizeof(char *);
static char **search_str = NULL;
static size_t *search_len = NULL;
static Uint32 misc_event_time = 0;
static int harvesting_flag = 0;
int now_harvesting(void) { return harvesting_flag; }
void clear_now_harvesting(void) { harvesting_flag = 0; }
void set_now_harvesting(void) { harvesting_flag = 1; }
Uint32 disconnect_time;
char harvest_name[32] = {0};
int killed_by_player = 0;
char last_spell_name[60] = {0};
int counters_scroll_id = 16;
void increment_counter(int counter_id, const char *name, int quantity, int extra);
static int display_counters_handler(window_info *win);
static int click_counters_handler(window_info *win, int mx, int my, Uint32 extra);
static int mouseover_counters_handler(window_info *win, int mx, int my);
static size_t cm_counters = CM_INIT_VALUE;
static int cm_selected_entry = -1;
static int cm_selected_id = -1;
static int cm_entry_count = -1;
static int cm_floating_flag = 0;
unsigned int floating_counter_flags = 0; /* persisted in cfg file */
int floating_session_counters = 0; /* persisted in ini file */
int sort_counter_func(const void *a, const void *b)
{
const struct Counter *ca, *cb;
if (sort_by[sort_counter_id-1] < 0) {
ca = b;
cb = a;
} else {
ca = a;
cb = b;
}
switch (abs(sort_by[sort_counter_id-1])) {
case TOTAL:
if (ca->n_total < cb->n_total)
return -1;
if (ca->n_total > cb->n_total)
return 1;
// fallthrough
case SESSION:
if (ca->n_session < cb->n_session)
return -1;
if (ca->n_session > cb->n_session)
return 1;
// fallthrough
case NAME:
return strcasecmp(ca->name, cb->name);
}
return 0;
}
void sort_counter(int counter_id)
{
if (sort_by[counter_id-1]) {
sort_counter_id = counter_id;
qsort(counters[counter_id-1], entries[counter_id-1], sizeof(struct Counter), sort_counter_func);
}
}
void load_counters(void)
{
FILE *f;
int i, j;
Uint8 io_counter_id;
Uint8 io_name_len;
Uint32 io_extra;
Uint32 io_n_total;
char io_name[64];
int fread_ok = 1;
char filename[256];
if (counters_initialized) {
/*
* save eny existing counters before reloading
* this will take place when relogging after disconnection
*/
flush_counters();
return;
}
ENTER_DEBUG_MARK("load counters");
for (i = 0; i < NUM_COUNTERS; i++) {
counters[i] = NULL;
entries[i] = 0;
sort_by[i] = 0;
}
/* allocate and set misc event matching strings */
search_str = malloc (sizeof (char *) * num_search_str);
search_len = malloc (sizeof (size_t) * num_search_str);
for (i=0; i<num_search_str; i++)
{
size_t max_len = strlen (get_username()) + strlen (temp_event_string[i]) + 1;
search_str[i] = malloc (max_len);
safe_snprintf (search_str[i], max_len, temp_event_string[i], get_username());
search_len[i] = strlen (search_str[i]);
}
if (!spell_names[0]) {
memset(&spell_names, 0, sizeof(spell_names));
}
counters_initialized = 1;
#ifdef JSON_FILES
if (get_use_json_user_files())
{
USE_JSON_DEBUG("Loading json file");
// try to load the counters json file
safe_snprintf(filename, sizeof(filename), "%scounters_%s.json", get_path_config(), get_lowercase_username());
if (json_load_counters(filename, cat_str, entries, NUM_COUNTERS, counters) >= 0)
{
LEAVE_DEBUG_MARK("load counters");
return;
}
}
USE_JSON_DEBUG("Loading binary file");
#endif
// if there is no json file, or json use disabled, try to load the old binary format
safe_snprintf(filename, sizeof(filename), "counters_%s.dat", get_lowercase_username());
LOG_DEBUG("Open counters file '%s'", filename);
if (!(f = open_file_config(filename, "rb"))) {
LEAVE_DEBUG_MARK("load counters");
return;
}
while (fread(&io_counter_id, sizeof(io_counter_id), 1, f) > 0) {
fread_ok = 0;
if (fread(&io_name_len, sizeof(io_name_len), 1, f) != 1)
break;
if (fread(io_name, io_name_len, 1, f) != 1)
break;
io_name[io_name_len] = '\0';
LOG_DEBUG("Reading counter '%s'", io_name);
if (fread(&io_extra, sizeof(io_extra), 1, f) != 1)
break;
if (fread(&io_n_total, sizeof(io_n_total), 1, f) != 1)
break;
fread_ok = 1;
if(strlen(io_name)<1 || strlen(io_name)>100){
//doesn't seem to have a real name, so we don't want it
continue;
}
i = io_counter_id - 1;
if (i >= NUM_COUNTERS)
{
LOG_ERROR("Counter ID %d out of bounds. NUM_COUNTERS %d", io_counter_id, NUM_COUNTERS);
break;
}
j = entries[i]++;
counters[i] = realloc(counters[i], entries[i] * sizeof(struct Counter));
counters[i][j].name = strdup(io_name);
counters[i][j].n_session = 0;
counters[i][j].n_total = io_n_total;
counters[i][j].extra = io_extra;
}
if (!fread_ok)
LOG_ERROR("%s error reading counters\n", __FUNCTION__);
fclose(f);
LEAVE_DEBUG_MARK("load counters");
}
void flush_counters(void)
{
FILE *f;
int i, j;
Uint8 io_counter_id;
Uint8 io_name_len;
char filename[256];
if (!counters_initialized) {
return;
}
#ifdef JSON_FILES
if (get_use_json_user_files())
{
USE_JSON_DEBUG("Saving json file");
// save the json file
safe_snprintf(filename, sizeof(filename), "%scounters_%s.json", get_path_config(), get_lowercase_username());
if (json_save_counters(filename, cat_str, entries, NUM_COUNTERS, (const struct Counter **)counters) < 0)
LOG_ERROR("%s: %s \"%s\"\n", reg_error_str, cant_open_file, filename);
return;
}
USE_JSON_DEBUG("Saving binary file");
#endif
safe_snprintf(filename, sizeof(filename), "counters_%s.dat", get_lowercase_username());
LOG_DEBUG("Open counters file '%s'", filename);
if (!(f = open_file_config(filename, "wb"))) {
return;
}
ENTER_DEBUG_MARK("flush counters");
for (i = 0; i < NUM_COUNTERS; i++) {
io_counter_id = i + 1;
for (j = 0; j < entries[i]; j++) {
io_name_len = strlen(counters[i][j].name);
LOG_DEBUG("Writing counter '%s'",
counters[i][j].name);
fwrite(&io_counter_id, sizeof(io_counter_id), 1, f);
fwrite(&io_name_len, sizeof(io_name_len), 1, f);
fwrite(counters[i][j].name, io_name_len, 1, f);
fwrite(&counters[i][j].extra, sizeof(counters[i][j].extra), 1, f);
fwrite(&counters[i][j].n_total, sizeof(counters[i][j].n_total), 1, f);
}
}
fclose(f);
LEAVE_DEBUG_MARK("flush counters");
}
void cleanup_counters(void)
{
int i, j;
for (i = 0; i < NUM_COUNTERS; i++) {
if (counters[i]) {
for (j = 0; j < entries[i]; ++j) {
free(counters[i][j].name);
}
free(counters[i]);
counters[i] = NULL;
entries[i] = 0;
}
}
/* free memory used by misc event matching strings */
if (search_str != NULL)
{
for (i=0; i<num_search_str; i++)
free(search_str[i]);
free(search_str);
free(search_len);
search_str = NULL;
search_len = NULL;
}
clear_now_harvesting();
counters_initialized = 0;
}
static void increment_product_counter(int counter_id, const char *name, int quantity, int extra)
{
increment_counter(counter_id, name, quantity, extra);
/* make sure the name/quanity is not used again */
counters_set_product_info("",0);
}
void increment_counter(int counter_id, const char *name, int quantity, int extra)
{
int i, j;
int new_entry = 1;
//printf("%s: counter_id=%u name=[%s] quantity=%u extra=%d\n", __FUNCTION__, counter_id, name, quantity, extra);
if(name == 0 || strlen(name)<1 || strlen(name)>100){
//doesn't seem to have a real name, so no point saving it
return;
}
i = counter_id - 1;
if (i >= NUM_COUNTERS)
{
LOG_ERROR("Counter ID %d out of bounds. NUM_COUNTERS %d", counter_id, NUM_COUNTERS);
return;
}
/* Look for an existing entry. */
for (j = 0; j < entries[i]; j++) {
if ((name && strcasecmp(counters[i][j].name, name)) || counters[i][j].extra != extra) {
continue;
}
counters[i][j].n_session += quantity;
counters[i][j].n_total += quantity;
new_entry = 0;
break;
}
if (new_entry) {
/* Create a new entry. */
j = entries[i]++;
last_selected_counter_id = -1; /* force recalculation of the scrollbar */
counters[i] = realloc(counters[i], entries[i] * sizeof(struct Counter));
counters[i][j].name = strdup(name);
counters[i][j].n_session = quantity;
counters[i][j].n_total = quantity;
counters[i][j].extra = extra;
}
if (floating_session_counters && (floating_counter_flags & (1 << i)))
{
size_t buf_len = strlen(name) + 20; // room for " : [+/-]4294967296\0"
char *str = malloc(buf_len);
safe_snprintf(str, buf_len, "%s: %u", name, counters[i][j].n_session);
add_floating_message(yourself, str, FLOATINGMESSAGE_NORTH, 0.3, 0.3, 1.0, 1500);
free(str);
}
sort_counter(counter_id);
}
void decrement_counter(int counter_id, char *name, int quantity, int extra)
{
int i, j;
i = counter_id - 1;
for (j = 0; j < entries[i]; j++) {
if (strcasecmp(counters[i][j].name, name) || counters[i][j].extra != extra) {
continue;
}
counters[i][j].n_session -= quantity;
counters[i][j].n_total -= quantity;
return;
}
}
static void cm_counters_pre_show_handler(window_info *win, int widget_id, int mx, int my, window_info *cm_win)
{
// get the counter id and entry indices
cm_selected_id = multiselect_get_selected(win->window_id, multiselect_id);
cm_entry_count = entries[cm_selected_id];
cm_selected_entry = (my - (margin_y_len + space_y)) / step_y;
if (counters_scroll_id != -1)
cm_selected_entry += vscrollbar_get_pos(win->window_id, counters_scroll_id);
// if not a valid entry, the menu options are greyed out
{
int is_grey = ((cm_selected_entry <0) || (cm_selected_entry >= entries[cm_selected_id]));
cm_grey_line(cm_counters, 0, is_grey);
cm_grey_line(cm_counters, 2, is_grey);
}
// set the control var from floating flags
cm_floating_flag = (floating_counter_flags & (1 << cm_selected_id)) ?1 :0;
}
// Prints the entries (total and name) in the specified category.
// If just_session is true, then session infomation is also
// included but only non-zero entries are included.
//
static void print_category(size_t cat, int just_session)
{
int i;
char buf[256];
if (cat >= NUM_COUNTERS)
return;
if (just_session)
{
int has_session = 0;
for (i = 0; i < entries[cat]; i++)
if (counters[cat][i].n_session > 0)
{
has_session = 1;
break;
}
if (!has_session)
return;
}
safe_snprintf(buf, sizeof(buf), "%s:", cat_str[cat]);
LOG_TO_CONSOLE(c_green2, buf);
for (i = 0; i < entries[cat]; i++)
{
if (just_session)
{
if (counters[cat][i].n_session <= 0)
continue;
safe_snprintf(buf, sizeof(buf), "%11u %11u %s",
counters[cat][i].n_session, counters[cat][i].n_total, counters[cat][i].name);
}
else
safe_snprintf(buf, sizeof(buf), "%11u %s", counters[cat][i].n_total, counters[cat][i].name);
LOG_TO_CONSOLE(c_grey1,buf);
}
}
// The #session_counters command and menu option
//
void print_session_counters(const char *category)
{
int i;
if ((category == NULL) || !(*category))
for (i = 0; i < NUM_COUNTERS; i++)
print_category(i, 1);
else
{
for (i = 0; i < NUM_COUNTERS; i++)
if (strncasecmp(category, cat_str[i], strlen(category)) == 0)
{
print_category(i, 1);
break;
}
}
}
static int cm_counters_handler(window_info *win, int widget_id, int mx, int my, int option)
{
struct Counter *the_entry = NULL;
// if the number of entries has changed, we could be about to use the wrong entry, don't use it
// if a entry index is invalid, don't use it
if ((cm_entry_count == entries[cm_selected_id]) &&
(cm_selected_entry >= 0) && (cm_selected_entry < entries[cm_selected_id]))
the_entry = &counters[cm_selected_id][cm_selected_entry];
switch (option)
{
case 0: // delete entry
if (the_entry != NULL)
{
int i;
if (the_entry->name != NULL)
free(the_entry->name);
// move the entries up, replacing the deleted one
for (i=cm_selected_entry+1; i<entries[cm_selected_id]; i++)
{
the_entry->name = (the_entry+1)->name;
the_entry->n_session = (the_entry+1)->n_session;
the_entry->n_total = (the_entry+1)->n_total;
the_entry->extra = (the_entry+1)->extra;
the_entry++;
}
entries[cm_selected_id]--;
}
else
return 0;
break;
case 2: // reset session total for entry
if (the_entry != NULL)
the_entry->n_session = 0;
else
return 0;
break;
case 4: // set the floating flag from the control var
{
int flagbit = multiselect_get_selected(win->window_id, multiselect_id);
if (cm_floating_flag)
{
floating_counter_flags |= 1 << flagbit;
if (!floating_session_counters)
toggle_OPT_BOOL_by_name("floating_session_counters");
}
else
floating_counter_flags &= ~(1 << flagbit);
}
break;
case 6: // print the category to console
print_category(cm_selected_id, 0);
break;
case 7: // print all categories to console
{
int i;
for (i = 0; i < NUM_COUNTERS; i++)
print_category(i, 0);
}
break;
case 8: // print session information to console
print_session_counters(NULL);
break;
default:
return 0;
}
return 1;
}
static void set_content_widths(window_info *win)
{
float zoom = win->current_scale_small;
int gap_x = win->small_font_max_len_x / 2;
int max_button_width = 0;
int button_height = (int)(0.5 + win->current_scale_small * 2*BUTTONRADIUS);
int button_sep = (int)(0.5 + win->current_scale_small * 2);
int i;
for (i=0; i<NUM_COUNTERS; i++)
{
int width = calc_button_width((const unsigned char*)cat_str[i], win->font_category, zoom);
if (width > max_button_width)
max_button_width = width;
}
left_panel_width = 2 * gap_x + max_button_width;
name_x_start = left_panel_width + gap_x;
name_x_end = name_x_start + get_string_width_zoom(name_str, win->font_category, zoom);
total_x_end = win->len_x - win->box_size - gap_x;
total_x_start = total_x_end - get_string_width_zoom(total_str, win->font_category, zoom);
session_x_end = total_x_end - 12 * win->small_font_max_len_x;
session_x_start = session_x_end - get_string_width_zoom(session_str, win->font_category, zoom);
win->min_len_x = name_x_end + 2 * 12 * win->small_font_max_len_x + win->box_size + gap_x;
win->min_len_y = 2 * gap_x + (NUM_COUNTERS - 1) * button_sep + NUM_COUNTERS * button_height;
}
static int resize_counters_handler(window_info *win, int new_width, int new_height)
{
int gap_x = win->small_font_max_len_x / 2;
size_t i;
int current_selected;
int butt_y[NUM_COUNTERS] = {0, 1, 6, 7, 8, 9, 10, 11, 12, 13, 2, 5, 14, 3, 4 };
set_content_widths(win);
step_y = get_line_height(win->font_category, win->current_scale_small);
margin_y_len = 1.5 * step_y;
space_y = 0.5 * step_y;
NUM_LINES = (int)((new_height - 2 * margin_y_len - 2 * space_y) / step_y);
// for now, destroy then re-create the buttons as there is no clear way to resize them
if (multiselect_id >= 0)
{
current_selected = multiselect_get_selected(win->window_id, multiselect_id);
widget_destroy(win->window_id, multiselect_id);
multiselect_id = -1;
}
else
current_selected = 0;
widget_resize(win->window_id, counters_scroll_id, win->box_size, win->len_y - 2 * margin_y_len);
widget_move(win->window_id, counters_scroll_id, win->len_x - win->box_size, margin_y_len);
vscrollbar_set_bar_len(win->window_id, counters_scroll_id, max2i(0, entries[current_selected] - NUM_LINES));
cm_remove_regions(win->window_id);
cm_add_region(cm_counters, win->window_id, left_panel_width, (margin_y_len + space_y),
win->len_x - win->box_size - left_panel_width, NUM_LINES * step_y);
multiselect_id = multiselect_add(win->window_id, NULL, gap_x, gap_x, left_panel_width - 2 * gap_x);
for (i=0; i<NUM_COUNTERS; i++)
{
multiselect_button_add_extended(win->window_id, multiselect_id,
0, (int)((new_height - gap_x) / NUM_COUNTERS) * butt_y[i], 0, cat_str[i], win->current_scale_small, i==0);
}
multiselect_set_selected(win->window_id, multiselect_id, current_selected);
return 0;
}
int ui_scale_font_handler(window_info *win)
{
set_content_widths(win);
return 1;
}
int change_counters_font_handler(window_info *win, font_cat cat)
{
if (cat != win->font_category)
return 0;
set_content_widths(win);
return 1;
}
void fill_counters_win(int window_id)
{
set_window_custom_scale(window_id, MW_STATS);
set_window_handler(window_id, ELW_HANDLER_DISPLAY, &display_counters_handler);
set_window_handler(window_id, ELW_HANDLER_CLICK, &click_counters_handler);
set_window_handler(window_id, ELW_HANDLER_MOUSEOVER, &mouseover_counters_handler);
set_window_handler(window_id, ELW_HANDLER_RESIZE, &resize_counters_handler);
set_window_handler(window_id, ELW_HANDLER_UI_SCALE, &ui_scale_font_handler);
set_window_handler(window_id, ELW_HANDLER_FONT_CHANGE, &change_counters_font_handler);
counters_scroll_id = vscrollbar_add_extended(window_id, counters_scroll_id, NULL, 0, 0, 0, 0, 0,
1.0f, 0, 1, 0);
if (window_id >= 0 && window_id < windows_list.num_windows)
set_content_widths(&windows_list.window[window_id]);
if (cm_counters == CM_INIT_VALUE)
{
cm_counters = cm_create(cm_counters_menu_str, cm_counters_handler);
cm_set_pre_show_handler(cm_counters, cm_counters_pre_show_handler);
cm_bool_line(cm_counters, 4, &cm_floating_flag, NULL);
}
}
static int display_counters_handler(window_info *win)
{
int i, j, n, x, y;
int scroll;
int total, session_total;
unsigned char buffer[32];
i = multiselect_get_selected(win->window_id, multiselect_id);
selected_counter_id = i + 1;
if (selected_counter_id != last_selected_counter_id) {
vscrollbar_set_bar_len(win->window_id, counters_scroll_id, max2i(0, entries[i] - NUM_LINES));
vscrollbar_set_pos(win->window_id, counters_scroll_id, 0);
last_selected_counter_id = selected_counter_id;
selected_entry = -1;
}
x = left_panel_width;
glDisable(GL_TEXTURE_2D);
glColor3fv(gui_color);
glBegin(GL_LINES);
glVertex3i(x, 0, 0);
glVertex3i(x, win->len_y, 0);
glVertex3i(x, margin_y_len, 0);
glVertex3i(win->len_x, margin_y_len, 0);
glVertex3i(x, win->len_y-margin_y_len, 0);
glVertex3i(win->len_x, win->len_y-margin_y_len, 0);
glEnd();
glEnable(GL_TEXTURE_2D);
x = name_x_start;
if (mouseover_name) glColor3f(0.6f, 0.6f, 0.6f);
else glColor3f(1.0f, 1.0f, 1.0f);
draw_text(x, margin_y_len, name_str, strlen((const char*)name_str), win->font_category,
TDO_ZOOM, win->current_scale_small, TDO_VERTICAL_ALIGNMENT, BOTTOM_LINE, TDO_END);
if (mouseover_session) glColor3f(0.6f, 0.6f, 0.6f);
else glColor3f(1.0f, 1.0f, 1.0f);
draw_text(session_x_end, margin_y_len, session_str, strlen((const char*)session_str),
win->font_category, TDO_ZOOM, win->current_scale_small, TDO_ALIGNMENT, RIGHT,
TDO_VERTICAL_ALIGNMENT, BOTTOM_LINE, TDO_END);
if (mouseover_total) glColor3f(0.6f, 0.6f, 0.6f);
else glColor3f(1.0f, 1.0f, 1.0f);
draw_text(total_x_end, margin_y_len, total_str, strlen((const char*)total_str), win->font_category,
TDO_ZOOM, win->current_scale_small, TDO_ALIGNMENT, RIGHT, TDO_VERTICAL_ALIGNMENT, BOTTOM_LINE,
TDO_END);
if (counters_scroll_id != -1) {
scroll = vscrollbar_get_pos(win->window_id, counters_scroll_id);
} else {
scroll = 0;
}
if (cm_window_shown() != cm_counters)
cm_selected_entry = -1;
for (j = scroll, n = 0, y = margin_y_len + space_y; j < entries[i]; j++, n++) {
int mouse_over_this_entry = ((mouseover_entry_y >= y) && (mouseover_entry_y < y+step_y));
if (n == NUM_LINES)
break;
if (mouse_over_this_entry && mouse_clicked) {
selected_entry = j;
mouse_clicked = 0;
}
if (cm_selected_entry == j)
glColor3fv(gui_color);
else if ((selected_counter_id == KILLS || selected_counter_id == DEATHS) && counters[i][j].extra)
glColor3f(0.8f, 0.2f, 0.2f);
else if (selected_entry == j)
elglColourN("global.mouseselected");
else if (mouse_over_this_entry)
elglColourN("global.mousehighlight");
else
glColor3f(1.0f, 1.0f, 1.0f);
/* draw first so left padding does not overwrite name */
safe_snprintf((char*)buffer, sizeof(buffer), "%u", counters[i][j].n_session);
draw_string_small_zoomed_right(session_x_end, y, buffer, 1, win->current_scale);
safe_snprintf((char*)buffer, sizeof(buffer), "%u", counters[i][j].n_total);
draw_string_small_zoomed_right(total_x_end, y, buffer, 1, win->current_scale);
if (counters[i][j].name)
{
float max_width;
float zoom = win->current_scale_small;
safe_snprintf((char*)buffer, sizeof(buffer), "%u", counters[i][j].n_session);
max_width = session_x_end - name_x_start
- get_string_width_zoom(buffer, win->font_category, zoom);
draw_string_zoomed_ellipsis_font(x, y, (const unsigned char*)counters[i][j].name,
max_width, 1, win->font_category, win->current_scale_small);
if (get_string_width_zoom((unsigned char*)counters[i][j].name, win->font_category, zoom) > max_width)
{
/* if the mouse is over this line and its truncated, tooltip to full name */
if (mouseover_entry_y >= y && mouseover_entry_y < y+step_y) {
show_help(counters[i][j].name, -TAB_MARGIN, win->len_y+10+TAB_MARGIN, win->current_scale);
counters_show_win_help = 0;
}
}
}
y += step_y;
}
if (counters_show_win_help) {
#ifdef ANDROID
show_help(long_touch_cm_options_str, -TAB_MARGIN, win->len_y+10+TAB_MARGIN, win->current_scale);
#else
show_help(cm_help_options_str, -TAB_MARGIN, win->len_y+10+TAB_MARGIN, win->current_scale);
#endif
counters_show_win_help = 0;
}
glColor3f(1.0f, 1.0f, 1.0f);
draw_text(x, win->len_y - margin_y_len/2, totals_str, strlen((const char*)totals_str),
win->font_category, TDO_ZOOM, win->current_scale_small, TDO_VERTICAL_ALIGNMENT, CENTER_LINE,
TDO_END);
for (j = 0, total = 0, session_total = 0; j < entries[i]; j++) {
total += counters[i][j].n_total;
session_total += counters[i][j].n_session;
}
safe_snprintf((char*)buffer, sizeof(buffer), "%u", session_total);
draw_text(session_x_end, win->len_y - margin_y_len/2, buffer, strlen((const char*)buffer),
win->font_category, TDO_ZOOM, win->current_scale_small, TDO_ALIGNMENT, RIGHT,
TDO_VERTICAL_ALIGNMENT, CENTER_LINE, TDO_END);
safe_snprintf((char*)buffer, sizeof(buffer), "%u", total);
draw_text(total_x_end, win->len_y - margin_y_len/2, buffer, strlen((const char*)buffer),
win->font_category, TDO_ZOOM, win->current_scale_small, TDO_ALIGNMENT, RIGHT,
TDO_VERTICAL_ALIGNMENT, CENTER_LINE, TDO_END);
#ifdef OPENGL_TRACE
CHECK_GL_ERRORS();
#endif //OPENGL_TRACE
return 1;
}
static int click_counters_handler(window_info *win, int mx, int my, Uint32 extra)
{
if (mx > left_panel_width && my > margin_y_len && my < win->len_y - margin_y_len) {
if (extra&ELW_WHEEL_UP) {
vscrollbar_scroll_up(win->window_id, counters_scroll_id);
return 1;
}
if (extra&ELW_WHEEL_DOWN) {
vscrollbar_scroll_down(win->window_id, counters_scroll_id);
return 1;
}
mouse_clicked = 1;
selected_entry = -1;
do_click_sound();
} else {
if (mouseover_name) {
if (sort_by[selected_counter_id-1] == NAME) {
sort_by[selected_counter_id-1] = -NAME;
} else {
sort_by[selected_counter_id-1] = NAME;
}
}
if (mouseover_session) {
if (sort_by[selected_counter_id-1] == SESSION) {
sort_by[selected_counter_id-1] = -SESSION;
} else {
sort_by[selected_counter_id-1] = SESSION;
}
}
if (mouseover_total) {
if (sort_by[selected_counter_id-1] == TOTAL) {
sort_by[selected_counter_id-1] = -TOTAL;
} else {
sort_by[selected_counter_id-1] = TOTAL;
}
}
sort_counter(selected_counter_id);
selected_entry = -1;
}
return 1;
}
static int mouseover_counters_handler(window_info *win, int mx, int my)
{
mouseover_name = mouseover_session = mouseover_total = 0;
mouseover_entry_y = -1;
if ((mx > left_panel_width) && (mx < (win->len_x - win->box_size)) &&
(my > (margin_y_len + space_y)) && (my < ((margin_y_len + space_y) + NUM_LINES * step_y )))
counters_show_win_help = 1;
if (my > margin_y_len){
if (my > (margin_y_len + space_y) && my < ((margin_y_len + space_y) + NUM_LINES * step_y)
&& mx >= left_panel_width && mx <= (win->len_x - win->box_size)) {
mouseover_entry_y = my;
}
return 0;
}
if (mx >= name_x_start && mx <= name_x_end) {
mouseover_name = 1;
return 0;
}
if (mx >= session_x_start && mx <= session_x_end) {
mouseover_session = 1;
return 0;
}
if (mx >= total_x_start && mx <= total_x_end) {
mouseover_total = 1;
return 0;
}
return 0;
}
/*
* Remove colors and guild tag from an actor's name.
*/
const char *strip_actor_name (const char *actor_name)
{
static char buf[32];
int i;
/* strip a leading color code */
if (is_color (actor_name[0])) {
actor_name++;
}
/* copy the name minus the guild tag */
for (i = 0; actor_name[i] && i < sizeof(buf); i++) {
if (is_color (actor_name[i]))
break;
buf[i] = actor_name[i];
}