File: MenuCtrl.cc

package info (click to toggle)
hatman 0.5.0.1-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,416 kB
  • ctags: 1,971
  • sloc: cpp: 7,871; sh: 330; yacc: 322; ansic: 260; lex: 210; makefile: 104; awk: 80
file content (223 lines) | stat: -rw-r--r-- 5,730 bytes parent folder | download | duplicates (2)
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
/*C*

 *
 * Hatman - The Game of Kings
 * Copyright (C) 1997 James Pharaoh & Timothy Fisken
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *C*/

#include "CtrlEvent.h"
#include "Events.h"
#include "MenuCtrl.h"
#include "../gl/Color.h"
#include "../gl/Console.h"
#include "../gl/Font.h"
#include "../gl/Mouse.h"
#include "../gl/VgaBlur.h"
#include "../gl/keycodes.h"
#include "../util/String.h"

//--------------------------------------------------------------------------------------------------------------------------------

class MenuItem : public Object
{
private:
 Events* events;
 Control* dest;

public:
 int id;
 String text;

 MenuItem(Events* _events, Control* _dest, int _id, const char* _text)
  { events = _events; dest = _dest; id = _id; text = _text; }
};

template Collection<MenuItem>;

//--------------------------------------------------------------------------------------------------------------------------------

MenuCtrl::MenuCtrl(Rect _rect, Events* _events, Control* _notify, Font* _font)
 : Control(_rect, _events)
{
 notify = _notify;
 font = _font;
 pSel = 0;
 pTop = 0;
 menuItems = new Collection<MenuItem>;
}

MenuCtrl::~MenuCtrl()
{
 delete menuItems;
}

//--------------------------------------------------------------------------------------------------------------------------------

Rect MenuCtrl::itemRect(int i)
{
 return Rect(rect().topLeft() + Point(0, i * font->h() - top()), Point(rect().w, font->h()));
}

bool MenuCtrl::bringIntoView()
{
 int y = font->h() * sel();

 // scroll up if we have to
 if(y < top())
  {
   top(y);
   return true;
  }

 // scroll down if we have to
 if(y + font->h() > top() + rect().h)
  {
   int newTop;
   if(font->h() > rect().h) newTop = y;
   else newTop = y + font->h() - rect().h;
   if(newTop != top())
    {
     top(newTop);
     return true;
    }
  }

 return false;
}

//--------------------------------------------------------------------------------------------------------------------------------

void MenuCtrl::evLBDown(Point p)
{
 int newSel = (p.y + top() - rect().top()) / font->h(); // confused?
 int oldSel = sel();
 if(newSel == oldSel || newSel >= menuItems->length()) return;

 sel(newSel);

 if(!rect().contains(itemRect(newSel)))
  {
   if(bringIntoView())
    events->redraw(rect());
  }
  /*
  {
   int newTop = font->h() * newSel;
   if(newTop + rect().h > menuItems->length() * font->h())
    newTop = menuItems->length() * font->h() - rect().h;
   if(newTop < 0) newTop = 0; // this should never happen...
   top(newTop);
   events->redraw(rect());
  }
  */
 else
  {
   events->redraw(itemRect(oldSel));
   events->redraw(itemRect(newSel));
  }
}

void MenuCtrl::evDbClick(Point p)
{
 int newSel = (p.y + top() - rect().top()) / font->h(); // confused?
 int oldSel = sel();
 if(newSel >= menuItems->length()) return;
 sel(newSel);

 if(newSel != oldSel)
  {
   events->redraw(itemRect(oldSel).clipTo(rect()));
   events->redraw(itemRect(newSel).clipTo(rect()));
  }

 events->add(new CommandEvent(notify, menuItems->at(sel())->id));
}

//--------------------------------------------------------------------------------------------------------------------------------

void MenuCtrl::evKeyDown(int key)
{
 if(IS_KEY_UP(key))
  {
   if(sel() > 0)
    {
     int oldSel = sel();
     int newSel = sel() - 1;
     sel(newSel);
     if(bringIntoView()) events->redraw(rect());
     else
      {
       events->redraw(itemRect(oldSel));
       events->redraw(itemRect(newSel));
      }
    }
  }
 if(IS_KEY_DOWN(key))
  {
   if(sel() < menuItems->length()-1)
    {
     int oldSel = sel();
     int newSel = sel() + 1;
     sel(newSel);
     if(bringIntoView()) events->redraw(rect());
     else
      {
       events->redraw(itemRect(oldSel));
       events->redraw(itemRect(newSel));
      }
    }
  }
}

void MenuCtrl::evKeyRep(int key)
{
 evKeyDown(key);
}

//--------------------------------------------------------------------------------------------------------------------------------

void MenuCtrl::draw(Rect redrawRect)
{
 font->hAlign = Font::HMiddle;
 font->vAlign = Font::VMiddle;
 // FIXME this should figure out which are on screen first
 for(int i=0; i<menuItems->length(); i++)
  {
   Rect miRect = itemRect(i);
   if(!miRect.meets(rect()) || !miRect.meets(redrawRect)) continue;
   MenuItem* mi = menuItems->at(i);
   if(sel() == i) screen->fillBox(miRect, Color(colorRed, 0x7F));
   font->writef(screen, miRect.middle(), colorWhite, "%s", (const char*) mi->text);
  }
}

//--------------------------------------------------------------------------------------------------------------------------------

void MenuCtrl::add(int id, const char* text)
{
 menuItems->add(new MenuItem(events, notify, id, text));
}

//--------------------------------------------------------------------------------------------------------------------------------

int MenuCtrl::itemId()
{
 return menuItems->at(sel())->id;
}

//--------------------------------------------------------------------------------------------------------------------------------