source: abuse/branches/lol/src/imlib/pmenu.cpp @ 732

Last change on this file since 732 was 732, checked in by Sam Hocevar, 8 years ago

build: SDL2 compilation fixes.

File size: 10.6 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2013 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <string.h>
16
17#include "common.h"
18
19#include "imlib/pmenu.h"
20
21void pmenu::move(int new_x, int new_y)
22{
23  wm->move_window(bar,new_x,new_y);
24}
25
26pmenu::pmenu(int X, int Y, pmenu_item *first, AImage *screen)
27{
28  top=first;
29  active=NULL;
30
31  ivec2 caa, cbb;
32  screen->GetClip(caa, cbb);
33  if (caa.x<X) caa.x=X;
34  int w = cbb.x - caa.x - AWindow::left_border() - AWindow::right_border();
35  int h = AWindow::top_border() + AWindow::bottom_border();
36
37  bar = wm->CreateWindow(ivec2(X, Y), ivec2(w, 0));
38  bar->freeze();  // can't drag this window
39  bar->m_surf->WidgetBar(ivec2(0, 0), ivec2(w - 1, h - 1),
40                         wm->bright_color(), wm->medium_color(),
41                         wm->dark_color());
42
43  int total=0,tx,tw;
44  pmenu_item *p=top;
45  for (; p; p=p->next) total++;
46
47  tw=w/(total+1);
48  tx=tw/2;
49
50  for (p=top; p; p=p->next,tx+=tw)
51    p->draw_self(bar,itemx(p),1,itemw(p),1,p==active);
52/*  }
53  else
54  {
55    for (p=top; p; p=p->next,tx+=tw)
56      p->draw(bar,itemx(p),1,itemw(p),1,p==active);
57  }*/
58
59}
60
61pmenu_item::pmenu_item(int ID, char const *Name, char const *on_off_flag, int Hotkey, pmenu_item *Next)
62{
63  xp=-1;
64  id=ID;
65  hotkey=Hotkey;
66  on_off=on_off_flag;
67  if (Name)
68    n = strdup(Name);
69  else n=NULL;
70  next=Next;
71  sub=NULL;
72}
73
74pmenu_item::pmenu_item(char const *Name, psub_menu *Sub, pmenu_item *Next, int xpos)
75{
76  xp=xpos;
77  id=0; hotkey=-1;
78  next=Next;
79  on_off=NULL;
80  ASSERT(Name, "Sub menu cannot have a NULL name");
81  n = strdup(Name);
82  sub=Sub;
83}
84
85pmenu_item *pmenu_item::find_id(int search_id)
86{
87  if (id==search_id) return this;
88  else if (sub) return sub->find_id(search_id);
89  else return NULL;
90}
91
92pmenu_item *pmenu_item::find_key(int key)
93{
94  if (key==hotkey && hotkey!=-1) return this;
95  else if (sub) return sub->find_key(key);
96  else return NULL;
97}
98
99pmenu::~pmenu()
100{
101  while (top)
102  {
103    pmenu_item *p=top;
104    top=top->next;
105    delete p;
106  }
107  if (bar) wm->close_window(bar);
108}
109
110psub_menu::~psub_menu()
111{
112  if (win)
113    wm->close_window(win);
114
115  while (first)
116  {
117    pmenu_item *tmp=first;
118    first=first->next;
119    delete tmp;
120  }
121}
122
123pmenu_item *psub_menu::find_id(int search_id)
124{
125  for (pmenu_item *f=first; f; f=f->next)
126  {
127    pmenu_item *ret=f->find_id(search_id);
128    if (ret) return ret;
129  }
130  return NULL;
131}
132
133pmenu_item *psub_menu::find_key(int key)
134{
135  for (pmenu_item *f=first; f; f=f->next)
136  {
137    pmenu_item *ret=f->find_key(key);
138    if (ret) return ret;
139  }
140  return NULL;
141}
142
143
144void psub_menu::hide(AWindow *parent, int x, int y)
145{
146  int w,h;
147  calc_size(w,h);
148  ivec2 caa, cbb;
149  main_screen->GetClip(caa, cbb);
150  // FIXME: is this correct? it looks like it used to be incorrect
151  // before the GetClip refactoring...
152  if (w+x>cbb.x-1)
153    x=cbb.x-1-w;
154
155  if (win)
156  {
157    if (active!=-1)
158    {
159      int w,h;
160      calc_size(w,h);
161      item_num(active)->draw(win,x+3,y+3+active*(wm->font()->Size().y+1),w-6,0,0);
162    }
163    wm->close_window(win);
164    win=NULL;
165  }
166}
167
168void psub_menu::calc_size(int &w, int &h)
169{
170    ivec2 ts = wm->font()->Size();
171  w=h=0;
172  for (pmenu_item *p=first; p; p=p->next)
173  {
174    if (p->name())
175    {
176      int l=strlen(p->name())*ts.x+8;
177      if (p->on_off) l+=ts.x*4;
178      if (l>w) w=l;
179    }
180    h++;
181  }
182  h=h*(ts.y+1)+8;
183}
184
185void psub_menu::draw(AWindow *parent, int x, int y)
186{
187  if (win) wm->close_window(win);
188
189  int w,h,i=0;
190  calc_size(w,h);
191  ivec2 caa, cbb;
192  main_screen->GetClip(caa, cbb);
193  if (parent->m_pos.x + w + x >= cbb.x)
194    x=cbb.x-1-w-parent->m_pos.x;
195  if (h+y+parent->m_pos.y>=cbb.y)
196  {
197    if (parent->m_pos.y+parent->m_size.y+wm->font()->Size().y>=cbb.y)
198      y=-h;
199    else y=y-h+wm->font()->Size().y+5;
200  }
201
202
203  win=wm->CreateWindow(parent->m_pos + ivec2(x, y),
204             ivec2(w - AWindow::left_border() - AWindow::right_border(),
205                   h - AWindow::top_border() - AWindow::bottom_border()));
206  win->freeze();
207  win->m_surf->WidgetBar(ivec2(0, 0), ivec2(w - 1, h - 1),
208                         wm->bright_color(), wm->medium_color(),
209                         wm->dark_color());
210
211  int has_flags=0;
212  pmenu_item *p=first;
213  for (; p; p=p->next) if (p->on_off) has_flags=1;
214  x = has_flags ? 3 + wm->font()->Size().x : 3;
215  y = 3;
216
217  for (p=first; p; p=p->next,i++,y+=wm->font()->Size().y+1)
218    p->draw(win,x,y,w-6,0,i==active);
219
220}
221
222void pmenu_item::draw_self(AWindow *parent, int x, int y, int w, int top, int active)
223{
224  int bx=x;
225  if (on_off) bx=x-wm->font()->Size().x;
226
227  if (!n)
228  {
229    int h=wm->font()->Size().y;
230    parent->m_surf->WidgetBar(ivec2(x, y + h / 2 - 1),
231                              ivec2(x + w - 1, y + h / 2), wm->dark_color(),
232                              wm->medium_color(), wm->bright_color());
233  } else
234  {
235    if (active)
236    {
237      if (xp!=-1)
238        parent->m_surf->xor_bar(bx,y,x+w-1,y+wm->font()->Size().y+1,wm->dark_color());
239      else
240      {
241    parent->m_surf->Bar(ivec2(bx, y),
242                        ivec2(x + w - 1, y + wm->font()->Size().y + 1),
243                        wm->dark_color());
244    wm->font()->PutString(parent->m_surf, ivec2(x+1, y+1), n, wm->medium_color());
245    if (on_off && *on_off) wm->font()->PutString(parent->m_surf, ivec2(bx+1, y+1), "*", wm->medium_color());
246      }
247    } else
248    {
249      if (xp!=-1)
250        parent->m_surf->xor_bar(bx,y,x+w-1,y+wm->font()->Size().y+1,wm->dark_color());
251      else
252      {
253    parent->m_surf->Bar(ivec2(bx, y),
254                        ivec2(x + w - 1, y + wm->font()->Size().y + 1),
255                        wm->medium_color());
256    wm->font()->PutString(parent->m_surf, ivec2(x + 1, y + 1), n, wm->bright_color());
257    if (on_off && *on_off) wm->font()->PutString(parent->m_surf, ivec2(bx + 1, y + 1), "*", wm->bright_color());
258      }
259    }
260  }
261}
262
263void pmenu_item::draw(AWindow *parent, int x, int y, int w, int top,
264              int active)
265{
266  if (n)
267  {
268    if (active)
269    {
270      draw_self(parent,x,y,w,top,active);
271      if (sub)
272      {
273    if (top)
274          sub->draw(parent,x,y+wm->font()->Size().y+2);
275    else
276      sub->draw(parent,x+w,y);
277      }
278    }
279    else
280    {
281      if (sub)
282      {
283    if (top)
284          sub->hide(parent,x,y+wm->font()->Size().y+2);
285    else
286      sub->hide(parent,x+w,y);
287      }
288      draw_self(parent,x,y,w,top,active);
289
290    }
291
292  } else draw_self(parent,x,y,w,top,active);
293}
294
295int pmenu::itemx(pmenu_item *p)
296{
297  if (p->xp!=-1) return p->xp;
298  int w=bar->m_surf->Size().x;
299
300
301  int total=0,tw,i=0,x=0;
302  for (pmenu_item *pp=top; pp; pp=pp->next,i++)
303  { if (pp==p) x=i;
304    total++;
305  }
306
307
308  tw=w/(total+1);
309  return tw/2+x*tw;
310}
311
312
313void pmenu::draw(AImage *screen, int top_only)
314{
315
316}
317
318
319int psub_menu::handle_event(AWindow *parent, int x, int y, Event &ev)
320{
321  int w,h;
322  calc_size(w,h);
323
324  x=win->m_pos.x;
325  y=win->m_pos.y;
326
327  int has_flags=0,dx=3;
328  for (pmenu_item *p=first; p; p=p->next) if (p->on_off) has_flags=1;
329  if (has_flags) dx+=wm->font()->Size().x;
330
331  int th=wm->font()->Size().y;
332  if (ev.mouse_move.x>=x && ev.mouse_move.y>=y && ev.mouse_move.x<x+w && ev.mouse_move.y<y+h)
333  {
334    int new_active=(ev.mouse_move.y-y-3)/(th+1);
335    if (item_num(new_active)==NULL) new_active=-1;
336
337    if (new_active!=active)
338    {
339      if (active!=-1)
340        item_num(active)->draw(win,dx,3+active*(th+1),w-6,0,0);
341      active=new_active;
342      if (active!=-1)
343        item_num(active)->draw(win,dx,3+active*(th+1),w-6,0,1);
344    }
345    if (ev.type==EV_MOUSE_BUTTON)
346    {
347      if (active!=-1)
348        return item_num(active)->handle_event(win,dx,3+active*(th+1),w-6,0,ev);
349      else return 0;
350    } else return 1;
351  } else if (active!=-1)
352    return item_num(active)->handle_event(win,win->m_pos.x+dx,win->m_pos.y+3+active*(th+1),w-6,0,ev);
353  else return 0;
354
355
356}
357
358int pmenu_item::handle_event(AWindow *parent, int x, int y, int w, int top,
359                 Event &ev)
360{
361  x+=parent->m_pos.x;
362  y+=parent->m_pos.y;
363  if (ev.mouse_move.x>=x && ev.mouse_move.y>=y && ev.mouse_move.x<x+w &&
364      ev.mouse_move.y<y+wm->font()->Size().y+2)
365  {
366    if (sub) return 1;
367    else
368    {
369      if (ev.type==EV_MOUSE_BUTTON &&n)
370        wm->Push(Event(id, (char *)this));
371      return 1;
372    }
373  } else if (sub)
374  {
375    if (top)
376      return sub->handle_event(parent,x,y+wm->font()->Size().y+2,ev);
377    else return sub->handle_event(parent,x+w,y,ev);
378  } else return 0;
379}
380
381pmenu_item *pmenu::inarea(int mx, int my, AImage *screen)
382{
383  mx-=bar->m_pos.x;
384  my-=bar->m_pos.y;
385  if (mx<0 || my<0 || mx>=bar->m_surf->Size().x || my>=bar->m_surf->Size().y) return NULL;
386  else
387  {
388    for (pmenu_item *p=top; p; p=p->next)
389    {
390      if (!p->next) return p;
391      else if (itemx(p->next)>mx) return p;
392    }
393    return NULL;
394  }
395}
396
397int psub_menu::own_event(Event &ev)
398{
399  if (win && ev.window==win) return 1; else
400    for (pmenu_item *p=first; p; p=p->next)
401      if (p->own_event(ev))
402        return 1;
403  return 0;
404}
405
406int pmenu_item::own_event(Event &ev)
407{
408  if (sub)
409    return sub->own_event(ev);
410  else return 0;
411}
412
413pmenu_item::~pmenu_item()
414{ if (n) free(n); if (sub) delete sub;
415}
416
417int pmenu::handle_event(Event &ev, AImage *screen)
418{
419  if (!active && ev.window!=bar) return 0;
420/*
421    int yes=0;
422    if (ev.window==bar) yes=1;    // event in top bar?
423    else
424    {
425      for (pmenu_item *p=top; p && !yes; p=p->next)  // event in submenu?
426      if (p->own_event(ev)) yes=1;
427    }
428    if (!yes) return 0;        // event is not for us...
429  }*/
430
431  switch (ev.type)
432  {
433    case EV_KEY :
434    {
435      for (pmenu_item *p=top; p; p=p->next)
436      {
437    pmenu_item *r=p->find_key(ev.key);
438    if (r)
439    {
440      wm->Push(Event(r->id, (char *)r));
441      return 1;
442    }
443      }
444      return 0;
445    } break;
446    case EV_MOUSE_MOVE :
447    {
448      pmenu_item *new_selection=inarea(ev.mouse_move.x,ev.mouse_move.y,screen);
449      if (!new_selection && active &&
450      active->handle_event(bar,itemx(active),1,itemw(active),1,ev))
451    return 1;
452      else if (active!=new_selection)
453      {
454    if (active)
455      active->draw(bar,itemx(active),1,itemw(active),1,0);
456    active=new_selection;
457    if (active)
458      active->draw(bar,itemx(active),1,itemw(active),1,1);
459      }
460      if (active) return 1;
461      else return 0;
462    } break;
463    case EV_MOUSE_BUTTON :
464    {
465      if (active)
466      {
467        if (active->handle_event(bar,itemx(active),1,itemw(active),1,ev))
468    {
469      active->draw(bar,itemx(active),1,itemw(active),1,0);
470      active=NULL;
471      return 1;
472    } else return 0;
473      }
474      else return 0;
475    } break;
476  }
477  return 0;
478}
479
480
Note: See TracBrowser for help on using the repository browser.