1 /*
2 * ppui/Menu.cpp
3 *
4 * Copyright 2009 Peter Barth
5 *
6 * This file is part of Milkytracker.
7 *
8 * Milkytracker is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Milkytracker is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Milkytracker. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 /*
24 * PPMenu.cpp
25 * MilkyTracker
26 *
27 * Created by Peter Barth on Fri Mar 04 2005.
28 *
29 */
30
31 #include "Menu.h"
32 #include "Font.h"
33 #include "GraphicsAbstract.h"
34
35 // ------------- menu --------------------------------------------
PPMenu(PPFont * aFont,const PPColor & selectionColor,const PPColor & bgColor,bool bSubMenu)36 PPMenu::PPMenu(PPFont* aFont, const PPColor& selectionColor, const PPColor& bgColor, bool bSubMenu/* = false*/) :
37 font(aFont),
38 backColor(&bgColor),
39 borderColor(&bgColor),
40 selectionColor(&selectionColor),
41 textBrightColor(NULL),
42 textDarkColor(NULL),
43 subMenu(bSubMenu),
44 parentMenu(NULL)
45 //borderColor(128, 128, 128)
46 {
47 }
48
getMaxWidth() const49 pp_uint32 PPMenu::getMaxWidth() const
50 {
51 pp_uint32 maxWidth = 0;
52
53 for (pp_int32 i = 0; i < items.size(); i++)
54 {
55 pp_uint32 len = items.get(i)->name.length();
56 if (len > maxWidth)
57 maxWidth = len;
58 }
59
60 return maxWidth;
61 }
62
getEntryHeight() const63 pp_uint32 PPMenu::getEntryHeight() const
64 {
65 #ifndef __LOWRES__
66 return font->getCharHeight()+2;
67 #else
68 return font->getCharHeight();
69 #endif
70 }
71
getBoundingRect() const72 PPRect PPMenu::getBoundingRect() const
73 {
74 PPRect r;
75 r.x1 = 0;
76 r.y1 = 0;
77 r.y2 = items.size()*getEntryHeight()+4;
78 r.x2 = getMaxWidth()*font->getCharWidth()+5;
79 return r;
80 }
81
setState(pp_int32 theId,pp_uint32 newState)82 bool PPMenu::setState(pp_int32 theId, pp_uint32 newState)
83 {
84 bool b = false;
85 for (pp_int32 i = 0; i < items.size(); i++)
86 {
87 if (items.get(i)->identifier == theId)
88 {
89 items.get(i)->state = newState;
90 b = true;
91 }
92 }
93
94 return b;
95 }
96
paint(PPGraphicsAbstract * g,pp_int32 px,pp_int32 py,pp_int32 menuSelection)97 void PPMenu::paint(PPGraphicsAbstract* g, pp_int32 px, pp_int32 py, pp_int32 menuSelection)
98 {
99 pp_int32 i;
100
101 PPRect r = getBoundingRect();
102
103 #ifndef __LOWRES__
104 const pp_int32 yOffset = 1;
105 #else
106 const pp_int32 yOffset = 0;
107 #endif
108
109 {
110 PPColor color = *backColor;
111
112 g->setRect(px, py, px + r.x2 + 2, py + r.y2 + 2);
113
114 g->setColor(color);
115
116 static pp_uint8 pattern[] = {224, 210, 198, 210, 224};
117
118 for (pp_int32 i = py; i < py + r.y2; i++)
119 {
120 pp_int32 c = (pattern[(i-py)%5]-64);
121 g->setColor(c,c,c);
122 g->drawHLine(px, px + r.x2, i);
123 }
124
125 // border
126
127 color = *borderColor;
128 PPColor bColor = color;
129 // adjust bright color
130 bColor.scaleFixed(87163);
131 PPPoint location(px,py);
132 PPSize size(r.x2, r.y2);
133
134 g->setColor(bColor);
135
136 g->drawHLine(location.x, location.x + size.width, location.y);
137 g->drawVLine(location.y, location.y + size.height, location.x);
138
139 // adjust dark color
140 bColor = color;
141 bColor.scaleFixed(20000);
142
143 g->setColor(bColor);
144
145 g->drawHLine(location.x, location.x + size.width, location.y + size.height - 1);
146 g->drawVLine(location.y, location.y + size.height, location.x + size.width - 1);
147
148 g->setColor(0,0,0);
149 g->drawHLine(location.x+1, location.x + size.width+1, location.y + size.height);
150 g->drawVLine(location.y+1, location.y + size.height+1, location.x + size.width);
151 g->drawHLine(location.x+1, location.x + size.width+2, location.y + size.height+1);
152 g->drawVLine(location.y+1, location.y + size.height+2, location.x + size.width+1);
153
154
155 }
156
157 g->setFont(font);
158
159 pp_int32 they = 0;
160 for (i = 0; i < items.size(); i++)
161 {
162 if (i == menuSelection)
163 {
164 PPColor color = *selectionColor;
165 PPColor nsdColor = color, nsbColor = color;
166
167 nsdColor.scaleFixed(45000);
168
169 // adjust bright color
170 nsbColor.scaleFixed(75000);
171
172 pp_int32 y = py + 2 + i*(getEntryHeight()) - 1;
173 pp_int32 h = (getEntryHeight()+1);
174 PPRect rect(px+1, y, px + 1 + r.x2 - 3, y + h/2);
175 g->fillVerticalShaded(rect, nsbColor, nsdColor, true);
176 rect.y1 += h/2;
177 rect.y2 += h/2 + 1;
178 g->fillVerticalShaded(rect, nsbColor, nsdColor, false);
179
180 g->setColor(*textBrightColor);
181 }
182 else
183 g->setColor(*textDarkColor);
184
185 if (items.get(i)->state & 1)
186 {
187 g->setColor(*borderColor);
188 g->drawString(items.get(i)->name, px + 2, py + 2 + they+yOffset);
189 }
190 else
191 {
192 PPColor curCol = g->getColor();
193 g->setColor((textBrightColor->r-curCol.r)>>1, (textBrightColor->g-curCol.g)>>1, (textBrightColor->b-curCol.b)>>1);
194 g->drawString(items.get(i)->name, px + 3, py + 3 + they+yOffset);
195 g->setColor(curCol);
196 g->drawString(items.get(i)->name, px + 2, py + 2 + they+yOffset);
197 }
198
199 they+=getEntryHeight();
200 }
201 }
202