source: abuse/tags/pd/macabuse/src/key_cfg.c @ 49

Last change on this file since 49 was 49, checked in by Sam Hocevar, 12 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1#include "lisp.hpp"
2#include "jwindow.hpp"
3#include "image.hpp"
4#include "specs.hpp"
5#include "clisp.hpp"
6#include "input.hpp"
7#include "cache.hpp"
8
9char *symbol_str(char *name);
10#ifdef __MAC__
11extern char *macify_name(char *s);
12#endif
13
14void load_image_into_screen(char *filename, char *name, int &x_loaded, int &y_loaded)
15{
16  bFILE *fp=open_file(filename,"rb");
17  if (fp->open_failure())
18  {
19    lbreak("file not found %s\n",filename);
20    exit(0);
21  }
22  spec_directory sd(fp);
23  spec_entry *se=sd.find(name);
24  if (!se)
25  {
26    lbreak("Unable to find %s in %s\n",name,filename);
27  }
28  fp->seek(se->offset,0);
29  unsigned short w,h;
30  w=fp->read_short();
31  h=fp->read_short();
32
33  screen->clear();
34  int x,y,ytot;
35  int read_width,skip_width;
36  if (w>screen->width())
37  {
38    read_width=screen->width();
39    skip_width=screen->width()-w;
40    x_loaded=0;
41  } else
42  {
43    read_width=w;
44    skip_width=0;
45    x_loaded=screen->width()/2-w/2;
46  }
47
48  if (h>screen->height())
49  {
50    ytot=screen->height();
51    y_loaded=0;
52  } else
53  {
54    ytot=h;
55    y_loaded=screen->height()/2-h/2;
56  }
57
58  unsigned char *sl=screen->scan_line(y_loaded)+x_loaded;
59
60  for (y=0;y<ytot;y++)
61  {
62    fp->read(sl,read_width);
63    if (skip_width)
64      fp->seek(skip_width,SEEK_CUR);
65    sl+=screen->width();
66  }
67
68  delete fp; 
69}
70
71class key_field : public ifield
72{
73  int key;
74public:
75
76  virtual void area(int &x1, int &y1, int &x2, int &y2, window_manager *wm)
77  {
78    x1=x;
79    y1=y;
80    x2=x+wm->font()->width()*11;
81    y2=y+wm->font()->height()+2;
82  }
83
84  virtual void draw_first(image *screen, window_manager *wm)
85  {
86    draw(0,screen,wm);
87  }
88
89  virtual void draw(int active, image *screen, window_manager *wm)
90  {
91    int x1,y1,x2,y2;
92    area(x1,y1,x2,y2,wm);
93    int bc,tc;
94
95    if (active)
96    {
97      bc=wm->bright_color();
98      tc=wm->black();
99    } else
100    {
101      tc=wm->bright_color();
102      bc=wm->black();
103    }
104
105    screen->bar(x1,y1,x2,y2,bc);
106    screen->rectangle(x1,y1,x2,y2,wm->medium_color());
107    char st[50];
108    key_name(key,st);
109
110    wm->font()->put_string(screen,(x1+x2)/2-wm->font()->width()*strlen(st)/2,y1+2,st,tc);
111  }
112
113  virtual void handle_event(event &ev, image *screen, window_manager *wm, input_manager *im)
114  {
115    if (ev.type==EV_KEY)
116    {
117      key=ev.key;
118      im->next_active(screen,wm);
119    }
120  }
121
122  virtual int selectable() { return 1; }
123  virtual char *read() { return (char *)(&key); }
124  key_field(int X, int Y, int Id, int Key, ifield *Next)
125  {
126    x=X; y=Y; id=Id; key=Key; next=Next;
127  }
128 
129} ;
130                               
131
132enum { field_total=7 } ;
133
134void do_key_config(window_manager *wm)
135{
136  int xp,yp;
137  load_image_into_screen("art/keys.spe","key_config",xp,yp);
138
139  ifield *first,**p;
140  p=&first;
141
142  int field_pos[field_total*2+2*2],*fieldp;
143  fieldp=field_pos;
144  for (int y=0;y<screen->height();y++)
145  {
146    unsigned char *sl=screen->scan_line(y);
147    for (int x=0;x<screen->width();x++,sl++)
148      if (*sl==242)
149      {
150        *fieldp=x; fieldp++;
151        *fieldp=y; fieldp++;
152      }
153  }
154 
155  image *ok_image=cash.img(cash.reg("art/frame.spe","dev_ok",SPEC_IMAGE,1))->copy(),
156    *cancel_image=cash.img(cash.reg("art/frame.spe","cancel",SPEC_IMAGE,1))->copy();
157
158
159  int key_defaults[field_total]= { lnumber_value(symbol_value(l_up_key)),
160                                   lnumber_value(symbol_value(l_left_key)),
161                                   lnumber_value(symbol_value(l_right_key)),
162                                   lnumber_value(symbol_value(l_down_key)),
163                                   lnumber_value(symbol_value(l_special_key)),
164                                   lnumber_value(symbol_value(l_weapon_left_key)),
165                                   lnumber_value(symbol_value(l_weapon_right_key)) };
166                                   
167                                   
168
169  for (int i=0;i<field_total;i++)
170  {
171    *p=new key_field(field_pos[i*2],field_pos[i*2+1],i,key_defaults[i],0);
172    p=&((*p)->next);
173  }
174
175  *p=new button(field_pos[field_total*2],field_pos[field_total*2+1],20,ok_image,0);
176  p=&((*p)->next); 
177  *p=new button(field_pos[field_total*2+2],field_pos[field_total*2+3],21,cancel_image,0);
178  p=&((*p)->next); 
179
180  input_manager inm(screen,wm,first);
181  int done=0;
182  do
183  {
184    event ev;
185    wm->flush_screen();
186    wm->get_event(ev);
187    if (ev.type==EV_MESSAGE)
188    {
189      if (ev.message.id==20)
190        done=1;
191      else if (ev.message.id==21)
192        done=2;
193    }
194
195    if (ev.type==EV_KEY && ev.key==JK_ESC)
196      done=2;
197
198    inm.handle_event(ev,0,wm);
199   
200  } while (!done);
201  if (done==1)
202  {
203    for (int i=0;i<field_total;i++)
204    {
205      key_defaults[i]=*((int *)first->read());
206      first=(key_field *)first->next;
207    }
208   
209    set_symbol_number(l_up_key,key_defaults[0]);
210    set_symbol_number(l_left_key,key_defaults[1]);
211    set_symbol_number(l_right_key,key_defaults[2]);
212    set_symbol_number(l_down_key,key_defaults[3]);
213    set_symbol_number(l_special_key,key_defaults[4]);
214    set_symbol_number(l_weapon_left_key,key_defaults[5]);
215    set_symbol_number(l_weapon_right_key,key_defaults[6]);
216
217                char *st = "lisp/input.lsp";
218
219#ifdef __MAC__
220                FILE *fp=fopen(macify_name(st),"wb");
221#else
222    FILE *fp=fopen(st,"wb");
223#endif
224    if (fp)
225    {
226      int *key=key_defaults;
227      enum { u,l,r,d,sp,wl,wr };
228      fprintf(fp,"(setq up_key %d)\n",key[u]);
229      fprintf(fp,"(setq left_key %d)\n",key[l]);
230      fprintf(fp,"(setq right_key %d)\n",key[r]);
231      fprintf(fp,"(setq down_key %d)\n",key[d]);
232      fprintf(fp,"(setq special_key %d)\n",key[sp]);
233      fprintf(fp,"(setq weapon_left_key %d)\n",key[wl]);
234      fprintf(fp,"(setq weapon_right_key %d)\n",key[wr]);
235      fclose(fp);
236    }                                                           
237  }
238
239  delete ok_image;
240  delete cancel_image;
241
242  screen->clear();
243  wm->flush_screen();
244}
245
Note: See TracBrowser for help on using the repository browser.