forked from pete-gordon/oricutron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgbox_sdl.c
302 lines (260 loc) · 7.35 KB
/
msgbox_sdl.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
/*
** Oricutron
** Copyright (C) 2009-2014 Peter Gordon
**
** 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, version 2
** of the License.
**
** 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.
**
** SDL based message box
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef _MSC_VER
#include <unistd.h>
#include <dirent.h>
#else
#include "msvc/dirent.h"
#endif
#include <sys/stat.h>
#include "system.h"
#include "6502.h"
#include "via.h"
#include "8912.h"
#include "gui.h"
#include "disk.h"
#include "monitor.h"
#include "6551.h"
#include "machine.h"
#include "msgbox.h"
#ifdef WWW
#include <emscripten.h>
#endif
struct msgboxbut
{
int x, y;
char *str;
};
extern SDL_Surface *screen;
extern struct textzone *tz[NUM_TZ];
static struct msgboxbut *btns;
static int cbtn;
static struct msgboxbut ynbuts[] = { { 2, 5, " YES " },
{ 50, 5, " NO " },
{ -1,-1, NULL } };
static struct msgboxbut ocbuts[] = { { 2, 5, " OK " },
{ 50, 5, " CANCEL " },
{ -1,-1, NULL } };
static struct msgboxbut obuts[] = { { 26, 5, " OK " },
{ -1,-1, NULL } };
SDL_bool init_msgbox( struct machine *oric )
{
if( !alloc_textzone( oric, TZ_MSGBOX, 80, 120, 60, 8, "Oricutron Request" ) ) return SDL_FALSE;
return SDL_TRUE;
}
void shut_msgbox( struct machine *oric )
{
free_textzone( oric, TZ_MSGBOX );
}
// Render the messagebox
static void msgbox_render( struct machine *oric )
{
int i;
for( i=0; btns[i].x!=-1; i++ )
{
if( i==cbtn )
{
tz[TZ_MSGBOX]->cfc = 1;
tz[TZ_MSGBOX]->cbc = 2;
} else {
tz[TZ_MSGBOX]->cfc = 2;
tz[TZ_MSGBOX]->cbc = 5;
}
tzstrpos( tz[TZ_MSGBOX], btns[i].x, btns[i].y, btns[i].str );
}
oric->render_begin( oric );
oric->render_video( oric, SDL_TRUE );
oric->render_textzone( oric, TZ_MSGBOX );
oric->render_end( oric );
}
static int msgbox_checkover( int x, int y )
{
int i;
for( i=0; btns[i].x!=-1; i++ )
{
if( ( y == btns[i].y ) &&
( x >= btns[i].x ) &&
( x < (btns[i].x+strlen(btns[i].str)) ) )
return i;
}
return -1;
}
SDL_bool msgbox( struct machine *oric, int type, char *msg )
{
int i, j, k, mx, my, presson;
int lines[] = { 2*60, 3*60, 5*60 };
char *msg2;
SDL_Event event;
SDL_bool wasunicode;
wasunicode = SDL_COMPAT_EnableUNICODE( SDL_TRUE );
SDL_COMPAT_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
// Clear any old message
for( i=1; i<59; i++ )
{
for( j=0; j<3; j++ )
{
tz[TZ_MSGBOX]->fc[lines[j]+i] = 2;
tz[TZ_MSGBOX]->bc[lines[j]+i] = 3;
tz[TZ_MSGBOX]->tx[lines[j]+i] = 32;
}
}
for( i=0; msg[i]; i++ ) if( (msg[i]=='\r')||(msg[i]=='\n') ) break;
for( j=0, k=30-i/2; j<i; j++, k++ )
tz[TZ_MSGBOX]->tx[lines[0]+k] = msg[j];
while( (msg[i]=='\r')||(msg[i]=='\n') ) i++;
if( msg[i] )
{
msg2 = &msg[i];
i = strlen( msg2 );
for( j=0, k=30-i/2; j<i; j++, k++ )
tz[TZ_MSGBOX]->tx[lines[1]+k] = msg2[j];
}
switch( type )
{
case MSGBOX_YES_NO:
btns = ynbuts;
cbtn = 0;
break;
case MSGBOX_OK_CANCEL:
btns = ocbuts;
cbtn = 1;
break;
case MSGBOX_OK:
btns = obuts;
cbtn = 0;
break;
}
msgbox_render( oric );
presson = -1;
for( ;; )
{
#ifdef WWW
if( !SDL_PollEvent( &event )) {
emscripten_sleep(10);
continue;
}
#else
if( !SDL_WaitEvent( &event ) )
break;
#endif
switch( event.type )
{
case SDL_COMPAT_ACTIVEEVENT:
if(SDL_COMPAT_IsAppFocused( &event ) ) {
msgbox_render( oric );
}
break;
case SDL_MOUSEMOTION:
if( presson != -1 ) break;
mx = (event.motion.x - tz[TZ_MSGBOX]->x)/8;
my = (event.motion.y - tz[TZ_MSGBOX]->y)/12;
i = msgbox_checkover( mx, my );
if( i == -1 ) break;
if( i != cbtn )
{
cbtn = i;
msgbox_render( oric );
}
break;
case SDL_MOUSEBUTTONDOWN:
if( event.button.button == SDL_BUTTON_LEFT )
{
mx = (event.motion.x - tz[TZ_MSGBOX]->x)/8;
my = (event.motion.y - tz[TZ_MSGBOX]->y)/12;
i = msgbox_checkover( mx, my );
if( i == -1 ) break;
cbtn = i;
presson = i;
msgbox_render( oric );
}
break;
case SDL_MOUSEBUTTONUP:
if( event.button.button == SDL_BUTTON_LEFT )
{
if( presson == -1 ) break;
mx = (event.motion.x - tz[TZ_MSGBOX]->x)/8;
my = (event.motion.y - tz[TZ_MSGBOX]->y)/12;
i = msgbox_checkover( mx, my );
if( i == presson )
{
switch( type )
{
case MSGBOX_YES_NO:
case MSGBOX_OK_CANCEL:
SDL_COMPAT_EnableUNICODE( wasunicode );
SDL_COMPAT_EnableKeyRepeat( wasunicode ? SDL_DEFAULT_REPEAT_DELAY : 0, wasunicode ? SDL_DEFAULT_REPEAT_INTERVAL : 0 );
return (presson==0);
default:
SDL_COMPAT_EnableUNICODE( wasunicode );
SDL_COMPAT_EnableKeyRepeat( wasunicode ? SDL_DEFAULT_REPEAT_DELAY : 0, wasunicode ? SDL_DEFAULT_REPEAT_INTERVAL : 0 );
return SDL_TRUE;
}
break;
}
presson = -1;
}
break;
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_TAB:
case SDLK_RIGHT:
cbtn = cbtn+1;
if( btns[cbtn].x == -1 ) cbtn = 0;
msgbox_render( oric );
break;
case SDLK_LEFT:
cbtn--;
if( cbtn < 0 )
{
for( cbtn=0; btns[cbtn].x!=-1; cbtn++ ) ;
cbtn--;
}
msgbox_render( oric );
break;
case SDLK_RETURN:
case SDLK_KP_ENTER:
switch( type )
{
case MSGBOX_YES_NO:
case MSGBOX_OK_CANCEL:
SDL_COMPAT_EnableUNICODE( wasunicode );
SDL_COMPAT_EnableKeyRepeat( wasunicode ? SDL_DEFAULT_REPEAT_DELAY : 0, wasunicode ? SDL_DEFAULT_REPEAT_INTERVAL : 0 );
return (cbtn==0);
default:
SDL_COMPAT_EnableUNICODE( wasunicode );
SDL_COMPAT_EnableKeyRepeat( wasunicode ? SDL_DEFAULT_REPEAT_DELAY : 0, wasunicode ? SDL_DEFAULT_REPEAT_INTERVAL : 0 );
return SDL_TRUE;
}
break;
default:
break;
}
break;
}
}
SDL_COMPAT_EnableUNICODE( wasunicode );
SDL_COMPAT_EnableKeyRepeat( wasunicode ? SDL_DEFAULT_REPEAT_DELAY : 0, wasunicode ? SDL_DEFAULT_REPEAT_INTERVAL : 0 );
return SDL_FALSE;
}