-
Notifications
You must be signed in to change notification settings - Fork 0
/
ONScripterLabel_event.cpp
1596 lines (1429 loc) · 53 KB
/
ONScripterLabel_event.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
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
/* -*- C++ -*-
*
* ONScripterLabel_event.cpp - Event handler of ONScripter-EN
*
* Copyright (c) 2001-2009 Ogapee. All rights reserved.
* (original ONScripter, of which this is a fork).
*
* ogapee@aqua.dti2.ne.jp
*
* Copyright (c) 2007-2010 "Uncle" Mion Sonozaki
*
* UncleMion@gmail.com
*
* 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, see <http://www.gnu.org/licenses/>
* or write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Modified by Mion, November 2009, to update from
// Ogapee's 20091115 release source code.
#include "ONScripterLabel.h"
#ifdef LINUX
#include <sys/types.h>
#include <sys/wait.h>
#endif
#ifdef WIN32
#include <windows.h>
#include "SDL_syswm.h"
#endif
#define ONS_TIMER_EVENT (SDL_USEREVENT)
#define ONS_SOUND_EVENT (SDL_USEREVENT+1)
#define ONS_CDAUDIO_EVENT (SDL_USEREVENT+2)
#define ONS_SEQMUSIC_EVENT (SDL_USEREVENT+3)
#define ONS_WAVE_EVENT (SDL_USEREVENT+4)
#define ONS_MUSIC_EVENT (SDL_USEREVENT+5)
#define ONS_BREAK_EVENT (SDL_USEREVENT+6)
#define ONS_ANIM_EVENT (SDL_USEREVENT+7)
// This sets up the fade event flag for use in bgm fadeout and fadein.
#define BGM_FADEOUT 0
#define BGM_FADEIN 1
#define ONS_BGMFADE_EVENT (SDL_USEREVENT+8)
#define EDIT_MODE_PREFIX "[EDIT MODE] "
#define EDIT_SELECT_STRING "Music vol (m) SE vol (s) Voice vol (v) Numeric variable (n) Exit (Esc)"
#define EDIT_VOLUME_STRING "Music vol (m) SE vol (s) Voice vol (v) Exit (Esc)"
static SDL_TimerID timer_id = NULL;
static SDL_TimerID break_id = NULL;
SDL_TimerID timer_cdaudio_id = NULL;
SDL_TimerID anim_timer_id = NULL;
SDL_TimerID timer_bgmfade_id = NULL;
SDL_TimerID timer_silentmovie_id = NULL;
// The reason we have a separate midi loop timer id here is that on Mac OS X, looping midis via SDL will cause SDL itself
// to hard crash after the first play. So, we work around that by manually causing the midis to loop. This OS X midi
// workaround is the work of Ben Carter. Recommend for integration. [Seung Park, 20060621]
#ifdef MACOSX
SDL_TimerID timer_seqmusic_id = NULL;
#endif
bool ext_music_play_once_flag = false;
static inline void clearTimer(SDL_TimerID &timer_id)
{
if (timer_id != NULL ) {
SDL_RemoveTimer( timer_id );
timer_id = NULL;
}
}
extern long decodeOggVorbis(ONScripterLabel::MusicStruct *music_struct, Uint8 *buf_dst, long len, bool do_rate_conversion);
/* **************************************** *
* Callback functions
* **************************************** */
extern "C" void mp3callback( void *userdata, Uint8 *stream, int len )
{
if ( SMPEG_playAudio( (SMPEG*)userdata, stream, len ) == 0 ){
SDL_Event event;
event.type = ONS_SOUND_EVENT;
SDL_PushEvent(&event);
}
}
extern "C" void oggcallback( void *userdata, Uint8 *stream, int len )
{
if (decodeOggVorbis((ONScripterLabel::MusicStruct*)userdata, stream, len, true) == 0){
SDL_Event event;
event.type = ONS_SOUND_EVENT;
SDL_PushEvent(&event);
}
}
extern "C" Uint32 SDLCALL animCallback( Uint32 interval, void *param )
{
clearTimer( anim_timer_id );
SDL_Event event;
event.type = ONS_ANIM_EVENT;
SDL_PushEvent( &event );
return 0;
}
extern "C" Uint32 SDLCALL breakCallback(Uint32 interval, void *param)
{
clearTimer(break_id);
SDL_Event event;
event.type = ONS_BREAK_EVENT;
SDL_PushEvent(&event);
return 0;
}
extern "C" Uint32 SDLCALL timerCallback( Uint32 interval, void *param )
{
clearTimer( timer_id );
SDL_Event event;
event.type = ONS_TIMER_EVENT;
SDL_PushEvent( &event );
return 0;
}
extern "C" Uint32 cdaudioCallback( Uint32 interval, void *param )
{
clearTimer( timer_cdaudio_id );
SDL_Event event;
event.type = ONS_CDAUDIO_EVENT;
SDL_PushEvent( &event );
return 0;
}
extern "C" Uint32 SDLCALL bgmfadeCallback( Uint32 interval, void *param )
{
SDL_Event event;
event.type = ONS_BGMFADE_EVENT;
event.user.code = (param == NULL) ? 0 : 1;
SDL_PushEvent( &event );
return interval;
}
extern "C" Uint32 SDLCALL silentmovieCallback( Uint32 interval, void *param )
{
SMPEG **mpeg = (SMPEG **)param;
if (*mpeg && (SMPEG_status(*mpeg) != SMPEG_PLAYING)){
SMPEG_play( *mpeg );
} else if (*mpeg == NULL){
clearTimer( timer_silentmovie_id );
return 0;
}
return interval;
}
// Pushes the midi loop event onto the stack. Part of a workaround for ONScripter
// crashing in Mac OS X after a midi is looped for the first time. Recommend for
// integration. This is the work of Ben Carter. [Seung Park, 20060621]
#if defined(MACOSX)
extern "C" Uint32 seqmusicSDLCallback( Uint32 interval, void *param )
{
SDL_Event event;
event.type = ONS_SEQMUSIC_EVENT;
SDL_PushEvent( &event );
return interval;
}
#endif
void seqmusicCallback( int sig )
{
#ifdef LINUX
int status;
wait( &status );
#endif
if ( !ext_music_play_once_flag ){
SDL_Event event;
event.type = ONS_SEQMUSIC_EVENT;
SDL_PushEvent(&event);
}
}
void musicCallback( int sig )
{
#ifdef LINUX
int status;
wait( &status );
#endif
if ( !ext_music_play_once_flag ){
SDL_Event event;
event.type = ONS_MUSIC_EVENT;
SDL_PushEvent(&event);
}
}
extern "C" void waveCallback( int channel )
{
SDL_Event event;
event.type = ONS_WAVE_EVENT;
event.user.code = channel;
SDL_PushEvent(&event);
}
/* **************************************** *
* OS Dependent Input Translation
* **************************************** */
#ifndef IPODLINUX
struct keychk {
bool set;
Uint16 unicode;
keychk(): set(false), unicode(0) {};
};
static keychk unikey[SDLK_LAST+1];
#endif
SDL_keysym ONScripterLabel::transKey(SDL_keysym key, bool isdown)
{
#ifdef IPODLINUX
switch(key.sym){
case SDLK_m: key.sym = SDLK_UP; break; /* Menu */
case SDLK_d: key.sym = SDLK_DOWN; break; /* Play/Pause */
case SDLK_f: key.sym = SDLK_RIGHT; break; /* Fast forward */
case SDLK_w: key.sym = SDLK_LEFT; break; /* Rewind */
case SDLK_RETURN: key.sym = SDLK_RETURN; break; /* Action */
case SDLK_h: key.sym = SDLK_ESCAPE; break; /* Hold */
case SDLK_r: key.sym = SDLK_UNKNOWN; break; /* Wheel clockwise */
case SDLK_l: key.sym = SDLK_UNKNOWN; break; /* Wheel ctrclockwise */
default: break;
}
#else
//printf("got key: %d (unicode %d)\n", event.key.keysym.sym, event.key.keysym.unicode);
// check against unicode
if (isdown) { // unicode field only available for keydown; save for keyup
unikey[key.sym].unicode = key.unicode;
unikey[key.sym].set = true;
}
else if (unikey[key.sym].set)
key.unicode = unikey[key.sym].unicode;
//account for switched-around keys in some layouts
if ((key.unicode & 0xFF80) == 0){
// ASCII
if ((key.unicode >= '0') && (key.unicode <= '9'))
key.sym = SDLKey(SDLK_0 + (int)key.unicode - '0');
else if ((key.unicode >= 'a') && (key.unicode <= 'z'))
key.sym = SDLKey(SDLK_a + (int)key.unicode - 'a');
else if ((key.unicode >= 'A') && (key.unicode <= 'Z'))
key.sym = SDLKey(SDLK_a + (int)key.unicode - 'A');
else if (key.unicode == ',')
key.sym = SDLK_COMMA;
}
#endif
return key;
}
SDLKey transJoystickButton(Uint8 button)
{
#ifdef PSP
SDLKey button_map[] = { SDLK_ESCAPE, /* TRIANGLE */
SDLK_RETURN, /* CIRCLE */
SDLK_SPACE, /* CROSS */
SDLK_RCTRL, /* SQUARE */
SDLK_o, /* LTRIGGER */
SDLK_s, /* RTRIGGER */
SDLK_DOWN, /* DOWN */
SDLK_LEFT, /* LEFT */
SDLK_UP, /* UP */
SDLK_RIGHT, /* RIGHT */
SDLK_0, /* SELECT */
SDLK_a, /* START */
SDLK_UNKNOWN,/* HOME */ /* kernel mode only */
SDLK_UNKNOWN,/* HOLD */};
return button_map[button];
#endif
return SDLK_UNKNOWN;
}
SDL_KeyboardEvent transJoystickAxis(SDL_JoyAxisEvent &jaxis)
{
static int old_axis=-1;
SDL_KeyboardEvent event;
SDLKey axis_map[] = {SDLK_LEFT, /* AL-LEFT */
SDLK_RIGHT, /* AL-RIGHT */
SDLK_UP, /* AL-UP */
SDLK_DOWN /* AL-DOWN */};
int axis = -1;
/* rerofumi: Jan.15.2007 */
/* ps3's pad has 0x1b axis (with analog button) */
if (jaxis.axis < 2){
axis = ((3200 > jaxis.value) && (jaxis.value > -3200) ? -1 :
(jaxis.axis * 2 + (jaxis.value>0 ? 1 : 0) ));
}
if (axis != old_axis){
if (axis == -1){
event.type = SDL_KEYUP;
event.keysym.sym = axis_map[old_axis];
}
else{
event.type = SDL_KEYDOWN;
event.keysym.sym = axis_map[axis];
}
old_axis = axis;
}
else{
event.keysym.sym = SDLK_UNKNOWN;
}
return event;
}
void ONScripterLabel::flushEventSub( SDL_Event &event )
{
//event related to streaming media
if ( event.type == ONS_SOUND_EVENT ){
if (async_movie) {
if ((SMPEG_status(async_movie) != SMPEG_PLAYING) && (movie_loop_flag))
SMPEG_play( async_movie );
} else if ( music_play_loop_flag ||
(cd_play_loop_flag && !cdaudio_flag ) ){
stopBGM( true );
if (music_file_name)
playSound(music_file_name, SOUND_OGG_STREAMING|SOUND_MP3, true);
else
playCDAudio();
}
else{
stopBGM( false );
}
}
// Mion: integrating insani's fadeout code & adding fadein, support for non-mp3 bgm
// The event handler for the mp3 fadeout event itself.
// Simply sets the volume of the mp3 being played lower and lower until it's 0,
// and until the requisite mp3 fadeout time has passed. [Seung Park, 20060621]
else if ((event.type == ONS_BGMFADE_EVENT) &&
(event.user.code == BGM_FADEOUT)){
Uint32 cur_fade_duration = mp3fadeout_duration;
if (skip_mode & (SKIP_NORMAL | SKIP_TO_EOP | SKIP_TO_WAIT) ||
ctrl_pressed_status) {
cur_fade_duration = 0;
setCurMusicVolume( 0 );
}
Uint32 tmp = SDL_GetTicks() - mp3fade_start;
if ( tmp < cur_fade_duration ) {
tmp = cur_fade_duration - tmp;
tmp *= music_volume;
tmp /= cur_fade_duration;
setCurMusicVolume( tmp );
} else {
clearTimer( timer_bgmfade_id );
event_mode &= ~WAIT_TIMER_MODE;
stopBGM( false );
//set break event to return to script processing
clearTimer(break_id);
SDL_Event event;
event.type = ONS_BREAK_EVENT;
SDL_PushEvent( &event );
}
}
else if ((event.type == ONS_BGMFADE_EVENT) &&
(event.user.code == BGM_FADEIN)){
Uint32 cur_fade_duration = mp3fadein_duration;
if (skip_mode & (SKIP_NORMAL | SKIP_TO_EOP | SKIP_TO_WAIT) ||
ctrl_pressed_status) {
cur_fade_duration = 0;
setCurMusicVolume( music_volume );
}
Uint32 tmp = SDL_GetTicks() - mp3fade_start;
if ( tmp < cur_fade_duration ) {
tmp *= music_volume;
tmp /= cur_fade_duration;
setCurMusicVolume( tmp );
} else {
clearTimer( timer_bgmfade_id );
event_mode &= ~WAIT_TIMER_MODE;
//set break event to return to script processing
clearTimer(break_id);
SDL_Event event;
event.type = ONS_BREAK_EVENT;
SDL_PushEvent( &event );
}
}
else if ( event.type == ONS_CDAUDIO_EVENT ){
if ( cd_play_loop_flag ){
stopBGM( true );
playCDAudio();
}
else{
stopBGM( false );
}
}
else if ( event.type == ONS_SEQMUSIC_EVENT ){
#if defined(MACOSX) //insani
if (!Mix_PlayingMusic())
{
ext_music_play_once_flag = !seqmusic_play_loop_flag;
Mix_FreeMusic( seqmusic_info );
playSequencedMusic(seqmusic_play_loop_flag);
}
#else
ext_music_play_once_flag = !seqmusic_play_loop_flag;
Mix_FreeMusic( seqmusic_info );
playSequencedMusic(seqmusic_play_loop_flag);
#endif
}
else if ( event.type == ONS_MUSIC_EVENT ){
ext_music_play_once_flag = !music_play_loop_flag;
Mix_FreeMusic(music_info);
playExternalMusic(music_play_loop_flag);
}
else if ( event.type == ONS_WAVE_EVENT ){ // for processing btntime2 and automode correctly
int ch = event.user.code;
if ( wave_sample[ch] ){
if ( (ch >= ONS_MIX_CHANNELS) || (ch == 0) ||
!channel_preloaded[ch] ) {
//don't free preloaded channels, _except_:
//always free voice channel, for now - could be
//messy for bgmdownmode and/or voice-waiting FIXME
Mix_FreeChunk( wave_sample[ch] );
wave_sample[ch] = NULL;
}
if (ch == MIX_LOOPBGM_CHANNEL0 &&
loop_bgm_name[1] &&
wave_sample[MIX_LOOPBGM_CHANNEL1])
Mix_PlayChannel(MIX_LOOPBGM_CHANNEL1,
wave_sample[MIX_LOOPBGM_CHANNEL1], -1);
if (ch == 0) {
channel_preloaded[ch] = false;
if (bgmdownmode_flag)
setCurMusicVolume( music_volume );
}
}
}
}
void ONScripterLabel::flushEvent()
{
SDL_Event event;
while( SDL_PollEvent( &event ) )
flushEventSub( event );
}
void ONScripterLabel::advancePhase( int count )
{
clearTimer(timer_id);
resetCursorTime( count );
timer_id = SDL_AddTimer( count, timerCallback, NULL );
}
void ONScripterLabel::advanceAnimPhase( int count )
{
if ( anim_timer_id == NULL ){
resetRemainingTime(count);
anim_timer_id = SDL_AddTimer( count, animCallback, NULL );
}
}
void ONScripterLabel::waitEventSub(int count)
{
if (break_id != NULL){ // already in wait queue
return;
}
//use WAIT_NO_ANIM_MODE to avoid animation refresh (e.g. in effect mode)
//use count<0 to prevent generating an automatic break event (i.e. run until "done")
int no_anim = false;
if (event_mode & (WAIT_INPUT_MODE | WAIT_TEXTBTN_MODE)) {
if ( (skip_mode & SKIP_NORMAL) || ctrl_pressed_status ||
(event_mode & WAIT_NO_ANIM_MODE) )
no_anim = true;
}
if (event_mode & (WAIT_TIMER_MODE | WAIT_TEXTOUT_MODE)){
if (no_anim){
clearTimer(anim_timer_id);
} else {
int duration = proceedCursorAnimation();
if (duration >= 0){
advancePhase();
}
duration = proceedAnimation();
if (duration >= 0){
advanceAnimPhase();
}
}
if (count > 0){
break_id = SDL_AddTimer(count, breakCallback, NULL);
}
}
if ((count >= 0) && (break_id == NULL)){
SDL_Event event;
event.type = ONS_BREAK_EVENT;
SDL_PushEvent( &event );
}
runEventLoop();
clearTimer(break_id);
}
bool ONScripterLabel::waitEvent( int count )
{
while(1){
waitEventSub( count );
if ( system_menu_mode == SYSTEM_NULL ) break;
if ( rgosub_label ) {
system_menu_mode = SYSTEM_NULL;
char *tmp = script_h.rgosub_wait_pos[script_h.cur_rgosub_wait];
gosubReal( rgosub_label, tmp, false, clickstr_state,
script_h.rgosub_wait_1byte[script_h.cur_rgosub_wait]);
script_h.cur_rgosub_wait = script_h.num_rgosub_waits = 0;
return true;
}
if ( executeSystemCall() ) return true;
}
return false;
}
void ONScripterLabel::trapHandler()
{
trap_mode = TRAP_NONE;
stopCursorAnimation( clickstr_state );
setCurrentLabel( trap_dest );
}
/* **************************************** *
* Event handlers
* **************************************** */
bool ONScripterLabel::mouseMoveEvent( SDL_MouseMotionEvent *event )
{
current_button_state.x = event->x;
current_button_state.y = event->y;
if ( event_mode & WAIT_BUTTON_MODE ){
mouseOverCheck( current_button_state.x, current_button_state.y );
if (getmouseover_flag &&
(current_over_button >= getmouseover_min) &&
(current_over_button <= getmouseover_max)){
current_button_state.set(current_over_button);
volatile_button_state.set(current_over_button);
playClickVoice();
stopCursorAnimation( clickstr_state );
return true;
}
else if (btnarea_flag &&
( ((btnarea_pos < 0) && (event->y > -btnarea_pos)) ||
((btnarea_pos > 0) && (event->y < btnarea_pos)) )){
current_button_state.set(-4);
volatile_button_state.set(-4);
playClickVoice();
stopCursorAnimation( clickstr_state );
return true;
}
}
return false;
}
bool ONScripterLabel::mousePressEvent( SDL_MouseButtonEvent *event )
// returns true if should break out of the event loop
{
if ( variable_edit_mode ) return false;
if (event_mode & WAIT_BUTTON_MODE)
last_keypress = KEYPRESS_NULL;
//any mousepress clears automode, on the release
if ( automode_flag ){
if ( event->type == SDL_MOUSEBUTTONUP ){
automode_flag = false;
if (getskipoff_flag && (event_mode & WAIT_BUTTON_MODE)){
current_button_state.set(-61);
volatile_button_state.set(-61);
return true;
}
}
return false;
}
//trap that mouseclick!
if ( ((event->button == SDL_BUTTON_RIGHT) && (trap_mode & TRAP_RIGHT_CLICK)) ||
((event->button == SDL_BUTTON_LEFT) && (trap_mode & TRAP_LEFT_CLICK)) ){
trapHandler();
return true;
}
current_button_state.reset();
current_button_state.x = event->x;
current_button_state.y = event->y;
current_button_state.down_flag = false;
if (getskipoff_flag && (skip_mode & SKIP_NORMAL) &&
(event_mode & WAIT_BUTTON_MODE)){
skip_mode &= ~SKIP_NORMAL;
current_button_state.set(-60);
volatile_button_state.set(-60);
return true;
}
skip_mode &= ~SKIP_NORMAL;
//right-click
if ((event->button == SDL_BUTTON_RIGHT) &&
(event->type == SDL_MOUSEBUTTONUP) &&
( (rmode_flag && (event_mode & WAIT_TEXT_MODE)) ||
(event_mode & (WAIT_BUTTON_MODE | WAIT_RCLICK_MODE)) )){
current_button_state.set(-1);
if (rmode_flag && (event_mode & WAIT_TEXT_MODE)){
if (root_rmenu_link.next)
system_menu_mode = SYSTEM_MENU;
else
system_menu_mode = SYSTEM_WINDOWERASE;
}
}
//left-click
else if ( (event->button == SDL_BUTTON_LEFT) &&
((event->type == SDL_MOUSEBUTTONUP) || btndown_flag) ){
current_button_state.set(current_over_button);
if ( event_mode & WAIT_TEXTOUT_MODE) {
skip_mode |= (SKIP_TO_WAIT | SKIP_TO_EOL);
}
skip_effect = true;
if ( event->type == SDL_MOUSEBUTTONDOWN )
current_button_state.down_flag = true;
}
//middle-click
else if ( (event->button == SDL_BUTTON_MIDDLE) &&
((event->type == SDL_MOUSEBUTTONUP) || btndown_flag) ){
if (!getmclick_flag)
return false;
current_button_state.set(-70);
if ( event->type == SDL_MOUSEBUTTONDOWN )
current_button_state.down_flag = true;
}
#if SDL_VERSION_ATLEAST(1, 2, 5)
else if ((event->button == SDL_BUTTON_WHEELUP) &&
((event_mode & WAIT_TEXT_MODE) ||
(usewheel_flag && (event_mode & WAIT_BUTTON_MODE)) ||
(system_menu_mode == SYSTEM_LOOKBACK))){
current_button_state.set(-2);
if (event_mode & WAIT_TEXT_MODE) system_menu_mode = SYSTEM_LOOKBACK;
}
else if ( (event->button == SDL_BUTTON_WHEELDOWN) &&
((enable_wheeldown_advance_flag && (event_mode & WAIT_TEXT_MODE)) ||
(usewheel_flag && (event_mode & WAIT_BUTTON_MODE)) ||
(system_menu_mode == SYSTEM_LOOKBACK) ) ){
if (event_mode & WAIT_TEXT_MODE){
current_button_state.set(0);
}
else{
current_button_state.set(-3);
}
}
#endif
else return false;
if (current_button_state.valid_flag)
volatile_button_state.set(current_button_state.button);
if (event_mode & (WAIT_INPUT_MODE | WAIT_BUTTON_MODE)){
if (system_menu_mode == SYSTEM_NULL) playClickVoice();
stopCursorAnimation( clickstr_state );
return true;
} else
return false;
}
void ONScripterLabel::variableEditMode( SDL_KeyboardEvent *event )
{
if (event_mode & WAIT_BUTTON_MODE)
last_keypress = KEYPRESS_NULL;
int i;
const char* var_name;
char var_index[12];
switch ( event->keysym.sym ) {
case SDLK_m:
if ( (variable_edit_mode != EDIT_SELECT_MODE) &&
(variable_edit_mode != EDIT_VOLUME_MODE) )
return;
variable_edit_mode = EDIT_MP3_VOLUME_MODE;
variable_edit_num = music_volume;
break;
case SDLK_s:
if ( (variable_edit_mode != EDIT_SELECT_MODE) &&
(variable_edit_mode != EDIT_VOLUME_MODE) )
return;
variable_edit_mode = EDIT_SE_VOLUME_MODE;
variable_edit_num = se_volume;
break;
case SDLK_v:
if ( (variable_edit_mode != EDIT_SELECT_MODE) &&
(variable_edit_mode != EDIT_VOLUME_MODE) )
return;
variable_edit_mode = EDIT_VOICE_VOLUME_MODE;
variable_edit_num = voice_volume;
break;
case SDLK_n:
if ( variable_edit_mode != EDIT_SELECT_MODE ) return;
variable_edit_mode = EDIT_VARIABLE_INDEX_MODE;
variable_edit_num = 0;
break;
case SDLK_9: case SDLK_KP9: variable_edit_num = variable_edit_num * 10 + 9; break;
case SDLK_8: case SDLK_KP8: variable_edit_num = variable_edit_num * 10 + 8; break;
case SDLK_7: case SDLK_KP7: variable_edit_num = variable_edit_num * 10 + 7; break;
case SDLK_6: case SDLK_KP6: variable_edit_num = variable_edit_num * 10 + 6; break;
case SDLK_5: case SDLK_KP5: variable_edit_num = variable_edit_num * 10 + 5; break;
case SDLK_4: case SDLK_KP4: variable_edit_num = variable_edit_num * 10 + 4; break;
case SDLK_3: case SDLK_KP3: variable_edit_num = variable_edit_num * 10 + 3; break;
case SDLK_2: case SDLK_KP2: variable_edit_num = variable_edit_num * 10 + 2; break;
case SDLK_1: case SDLK_KP1: variable_edit_num = variable_edit_num * 10 + 1; break;
case SDLK_0: case SDLK_KP0: variable_edit_num = variable_edit_num * 10 + 0; break;
case SDLK_MINUS: case SDLK_KP_MINUS:
if ( (variable_edit_mode == EDIT_VARIABLE_NUM_MODE) &&
(variable_edit_num == 0) )
variable_edit_sign = -1;
break;
case SDLK_BACKSPACE:
if ( variable_edit_num ) variable_edit_num /= 10;
else if ( variable_edit_sign == -1 ) variable_edit_sign = 1;
break;
case SDLK_RETURN: case SDLK_KP_ENTER:
switch( variable_edit_mode ){
case EDIT_VARIABLE_INDEX_MODE:
variable_edit_index = variable_edit_num;
variable_edit_num = script_h.getVariableData(variable_edit_index).num;
if ( variable_edit_num < 0 ){
variable_edit_num = -variable_edit_num;
variable_edit_sign = -1;
}
else{
variable_edit_sign = 1;
}
break;
case EDIT_VARIABLE_NUM_MODE:
script_h.setNumVariable( variable_edit_index, variable_edit_sign * variable_edit_num );
break;
case EDIT_MP3_VOLUME_MODE:
music_volume = variable_edit_num;
setCurMusicVolume(music_volume);
break;
case EDIT_SE_VOLUME_MODE:
se_volume = variable_edit_num;
for ( i=1 ; i<ONS_MIX_CHANNELS ; i++ )
if ( wave_sample[i] )
Mix_Volume( i, !volume_on_flag? 0 : se_volume * 128 / 100 );
if ( wave_sample[MIX_LOOPBGM_CHANNEL0] )
Mix_Volume( MIX_LOOPBGM_CHANNEL0, !volume_on_flag? 0 : se_volume * 128 / 100 );
if ( wave_sample[MIX_LOOPBGM_CHANNEL1] )
Mix_Volume( MIX_LOOPBGM_CHANNEL1, !volume_on_flag? 0 : se_volume * 128 / 100 );
break;
case EDIT_VOICE_VOLUME_MODE:
voice_volume = variable_edit_num;
if ( wave_sample[0] )
Mix_Volume( 0, !volume_on_flag? 0 : voice_volume * 128 / 100 );
default:
break;
}
if ( variable_edit_mode == EDIT_VARIABLE_INDEX_MODE )
variable_edit_mode = EDIT_VARIABLE_NUM_MODE;
else if (edit_flag)
variable_edit_mode = EDIT_SELECT_MODE;
else
variable_edit_mode = EDIT_VOLUME_MODE;
break;
case SDLK_ESCAPE:
if ( (variable_edit_mode == EDIT_SELECT_MODE) ||
(variable_edit_mode == EDIT_VOLUME_MODE) ){
variable_edit_mode = NOT_EDIT_MODE;
SDL_WM_SetCaption( DEFAULT_WM_TITLE, DEFAULT_WM_ICON );
SDL_Delay( 100 );
SDL_WM_SetCaption( wm_title_string, wm_icon_string );
return;
}
if (edit_flag)
variable_edit_mode = EDIT_SELECT_MODE;
else
variable_edit_mode = EDIT_VOLUME_MODE;
default:
break;
}
if ( variable_edit_mode == EDIT_SELECT_MODE ){
sprintf( wm_edit_string, "%s%s", EDIT_MODE_PREFIX, EDIT_SELECT_STRING );
}
else if ( variable_edit_mode == EDIT_VOLUME_MODE ){
sprintf( wm_edit_string, "%s%s", EDIT_MODE_PREFIX, EDIT_VOLUME_STRING );
}
else if ( variable_edit_mode == EDIT_VARIABLE_INDEX_MODE ) {
sprintf( wm_edit_string, "%s%s%d", EDIT_MODE_PREFIX, "Variable Index? %", variable_edit_sign * variable_edit_num );
}
else if ( variable_edit_mode >= EDIT_VARIABLE_NUM_MODE ){
int p=0;
switch( variable_edit_mode ){
case EDIT_VARIABLE_NUM_MODE:
sprintf( var_index, "%%%d", variable_edit_index );
var_name = var_index; p = script_h.getVariableData(variable_edit_index).num; break;
case EDIT_MP3_VOLUME_MODE:
var_name = "MP3 Volume"; p = music_volume; break;
case EDIT_VOICE_VOLUME_MODE:
var_name = "Voice Volume"; p = voice_volume; break;
case EDIT_SE_VOLUME_MODE:
var_name = "Sound effect Volume"; p = se_volume; break;
default:
var_name = "";
}
sprintf( wm_edit_string, "%sCurrent %s=%d New value? %s%d",
EDIT_MODE_PREFIX, var_name, p, (variable_edit_sign==1)?"":"-", variable_edit_num );
}
SDL_WM_SetCaption( wm_edit_string, wm_icon_string );
}
void ONScripterLabel::shiftCursorOnButton( int diff )
//moves the mouse cursor to the new button "moused-over" via keystroke
{
int num = 0;
ButtonLink *button = root_button_link.next;
while (button) {
button = button->next;
++num;
}
shortcut_mouse_line += diff;
if (shortcut_mouse_line < 0) shortcut_mouse_line = num - 1;
else if (shortcut_mouse_line >= num) shortcut_mouse_line = 0;
button = root_button_link.next;
for (int i = 0; i < shortcut_mouse_line; ++i)
button = button->next;
if (button) {
SDL_Rect clip = {0, 0, button->select_rect.w, button->select_rect.h};
int x = button->select_rect.x;
int y = button->select_rect.y;
if (x < 0) clip.x -= x;
else if (x > screen_width){
clip.w = 0;
x = screen_width - 1;
}
else if (x+clip.w > screen_width) clip.w = screen_width - x;
if (y < 0) clip.y -= y;
else if (x > screen_width){
clip.h = 0;
y = screen_height - 1;
}
else if (y+clip.h > screen_height) clip.h = screen_height - y;
if (transbtn_flag && (clip.x < (Sint16) clip.w) && (clip.y < (Sint16) clip.h)){
AnimationInfo *anim = NULL;
if ( button->button_type == ButtonLink::SPRITE_BUTTON ||
button->button_type == ButtonLink::EX_SPRITE_BUTTON )
anim = &sprite_info[ button->sprite_no ];
else
anim = button->anim[0];
SDL_Rect pos = anim->findOpaquePoint(&clip);
x += pos.x;
y += pos.y;
} else {
x += clip.x;
y += clip.y;
}
SDL_WarpMouse(x, y);
}
}
bool ONScripterLabel::keyDownEvent( SDL_KeyboardEvent *event )
// returns true if should break out of the event loop
{
if (event_mode & WAIT_BUTTON_MODE)
last_keypress = event->keysym.sym;
int last_ctrl_status = ctrl_pressed_status;
switch ( event->keysym.sym ) {
case SDLK_RCTRL:
ctrl_pressed_status |= 0x01;
case SDLK_LCTRL:
if (event->keysym.sym == SDLK_LCTRL)
ctrl_pressed_status |= 0x02;
if (last_ctrl_status != ctrl_pressed_status)
skip_effect = true; // allow short-circuiting the current effect with ctrl
//Ctrl key: do skip in text
if (event_mode & (WAIT_INPUT_MODE | WAIT_TEXTOUT_MODE | WAIT_TEXTBTN_MODE)){
current_button_state.set(0);
volatile_button_state.set(0);
playClickVoice();
stopCursorAnimation( clickstr_state );
return true;
}
if (event_mode & (WAIT_SLEEP_MODE)){
stopCursorAnimation( clickstr_state );
return true;
}
break;
case SDLK_RSHIFT:
shift_pressed_status |= 0x01;
break;
case SDLK_LSHIFT:
shift_pressed_status |= 0x02;
break;
#ifdef MACOSX
case SDLK_LMETA:
apple_pressed_status |= 1;
break;
case SDLK_RMETA:
apple_pressed_status |= 1;
break;
#endif
default:
break;
}
return false;
}
void ONScripterLabel::keyUpEvent( SDL_KeyboardEvent *event )
{
if (event_mode & WAIT_BUTTON_MODE)
last_keypress = event->keysym.sym;
switch ( event->keysym.sym ) {
case SDLK_RCTRL:
ctrl_pressed_status &= ~0x01;
break;
case SDLK_LCTRL:
ctrl_pressed_status &= ~0x02;
break;
case SDLK_RSHIFT:
shift_pressed_status &= ~0x01;
break;
case SDLK_LSHIFT:
shift_pressed_status &= ~0x02;
break;
#ifdef MACOSX
case SDLK_LMETA:
apple_pressed_status &= ~1;
break;
case SDLK_RMETA:
apple_pressed_status &= ~2;
break;
#endif
default: