82 * @param w Window holding the menu items |
82 * @param w Window holding the menu items |
83 * @param x X coordinate of the position |
83 * @param x X coordinate of the position |
84 * @param y Y coordinate of the position |
84 * @param y Y coordinate of the position |
85 * @return Index number of the menu item, or \c -1 if no valid selection under position |
85 * @return Index number of the menu item, or \c -1 if no valid selection under position |
86 */ |
86 */ |
87 static int GetMenuItemIndex(const Window *w, int x, int y) |
87 static int GetMenuItemIndex(const Window *w) |
88 { |
88 { |
|
89 int x = _cursor.pos.x; |
|
90 int y = _cursor.pos.y; |
|
91 |
89 if ((x -= w->left) >= 0 && x < w->width && (y -= w->top + 1) >= 0) { |
92 if ((x -= w->left) >= 0 && x < w->width && (y -= w->top + 1) >= 0) { |
90 y /= 10; |
93 y /= 10; |
91 |
94 |
92 if (y < WP(w, const menu_d).item_count && |
95 if (y < WP(w, const menu_d).item_count && |
93 !HasBit(WP(w, const menu_d).disabled_items, y)) { |
96 !HasBit(WP(w, const menu_d).disabled_items, y)) { |
1151 v->RaiseWidget(WP(w, menu_d).main_button); |
1154 v->RaiseWidget(WP(w, menu_d).main_button); |
1152 v->SetDirty(); |
1155 v->SetDirty(); |
1153 return; |
1156 return; |
1154 } |
1157 } |
1155 |
1158 |
1156 case WE_POPUPMENU_SELECT: { |
1159 case WE_MOUSELOOP: { |
1157 int index = GetMenuItemIndex(w, e->we.popupmenu.pt.x, e->we.popupmenu.pt.y); |
1160 int index = GetMenuItemIndex(w); |
1158 |
1161 |
1159 if (index < 0) { |
1162 if (_left_button_down) { |
1160 Window *w2 = FindWindowById(WC_MAIN_TOOLBAR,0); |
1163 if (index == -1 || index == WP(w, menu_d).sel_index) return; |
1161 if (GetWidgetFromPos(w2, e->we.popupmenu.pt.x - w2->left, e->we.popupmenu.pt.y - w2->top) == WP(w, menu_d).main_button) |
1164 |
1162 index = WP(w, menu_d).sel_index; |
1165 WP(w, menu_d).sel_index = index; |
1163 } |
1166 w->SetDirty(); |
1164 |
1167 } else { |
1165 int action_id = WP(w, menu_d).action_id; |
1168 if (index < 0) { |
1166 delete w; |
1169 Window *w2 = FindWindowById(WC_MAIN_TOOLBAR,0); |
1167 |
1170 if (GetWidgetFromPos(w2, _cursor.pos.x - w2->left, _cursor.pos.y - w2->top) == WP(w, menu_d).main_button) |
1168 if (index >= 0) { |
1171 index = WP(w, menu_d).sel_index; |
1169 assert((uint)index <= lengthof(_menu_clicked_procs)); |
1172 } |
1170 _menu_clicked_procs[action_id](index); |
1173 |
1171 } |
1174 int action_id = WP(w, menu_d).action_id; |
1172 |
1175 delete w; |
|
1176 |
|
1177 if (index >= 0) { |
|
1178 assert((uint)index <= lengthof(_menu_clicked_procs)); |
|
1179 _menu_clicked_procs[action_id](index); |
|
1180 } |
|
1181 } |
1173 } break; |
1182 } break; |
1174 |
|
1175 case WE_POPUPMENU_OVER: { |
|
1176 int index = GetMenuItemIndex(w, e->we.popupmenu.pt.x, e->we.popupmenu.pt.y); |
|
1177 |
|
1178 if (index == -1 || index == WP(w, menu_d).sel_index) return; |
|
1179 |
|
1180 WP(w, menu_d).sel_index = index; |
|
1181 w->SetDirty(); |
|
1182 return; |
|
1183 } |
|
1184 } |
1183 } |
1185 } |
1184 } |
1186 |
1185 |
1187 /* Dynamic widget length determined by toolbar-string length. |
1186 /* Dynamic widget length determined by toolbar-string length. |
1188 * See PopupMainToolbMenu en MenuWndProc */ |
1187 * See PopupMainToolbMenu en MenuWndProc */ |
1260 WP(w, menu_d).action_id = (GB(parent_button, 8, 8) != 0) ? GB(parent_button, 8, 8) : parent_button; |
1259 WP(w, menu_d).action_id = (GB(parent_button, 8, 8) != 0) ? GB(parent_button, 8, 8) : parent_button; |
1261 WP(w, menu_d).string_id = base_string; |
1260 WP(w, menu_d).string_id = base_string; |
1262 WP(w, menu_d).checked_items = 0; |
1261 WP(w, menu_d).checked_items = 0; |
1263 WP(w, menu_d).disabled_items = disabled_mask; |
1262 WP(w, menu_d).disabled_items = disabled_mask; |
1264 |
1263 |
1265 _popup_menu_active = true; |
|
1266 |
|
1267 SndPlayFx(SND_15_BEEP); |
1264 SndPlayFx(SND_15_BEEP); |
1268 return w; |
1265 return w; |
1269 } |
1266 } |
1270 |
1267 |
1271 /* --- Rendering/drawing the player menu --- */ |
1268 /* --- Rendering/drawing the player menu --- */ |
1348 v->RaiseWidget(WP(w, menu_d).main_button); |
1345 v->RaiseWidget(WP(w, menu_d).main_button); |
1349 v->SetDirty(); |
1346 v->SetDirty(); |
1350 return; |
1347 return; |
1351 } |
1348 } |
1352 |
1349 |
1353 case WE_POPUPMENU_SELECT: { |
1350 case WE_MOUSELOOP: { |
1354 int index = GetMenuItemIndex(w, e->we.popupmenu.pt.x, e->we.popupmenu.pt.y); |
1351 int index = GetMenuItemIndex(w); |
1355 int action_id = WP(w, menu_d).action_id; |
1352 |
1356 |
1353 if (_left_button_down) { |
1357 /* We have a new entry at the top of the list of menu 9 when networking |
1354 UpdatePlayerMenuHeight(w); |
1358 * so keep that in count */ |
1355 /* We have a new entry at the top of the list of menu 9 when networking |
1359 if (_networking && WP(w, menu_d).main_button == 9) { |
1356 * so keep that in count */ |
1360 if (index > 0) index = GetPlayerIndexFromMenu(index - 1) + 1; |
1357 if (_networking && WP(w, menu_d).main_button == 9) { |
|
1358 if (index > 0) index = GetPlayerIndexFromMenu(index - 1) + 1; |
|
1359 } else { |
|
1360 index = GetPlayerIndexFromMenu(index); |
|
1361 } |
|
1362 |
|
1363 if (index == -1 || index == WP(w, menu_d).sel_index) return; |
|
1364 |
|
1365 WP(w, menu_d).sel_index = index; |
|
1366 w->SetDirty(); |
1361 } else { |
1367 } else { |
1362 index = GetPlayerIndexFromMenu(index); |
1368 int action_id = WP(w, menu_d).action_id; |
1363 } |
1369 |
1364 |
1370 /* We have a new entry at the top of the list of menu 9 when networking |
1365 if (index < 0) { |
1371 * so keep that in count */ |
1366 Window *w2 = FindWindowById(WC_MAIN_TOOLBAR,0); |
1372 if (_networking && WP(w, menu_d).main_button == 9) { |
1367 if (GetWidgetFromPos(w2, e->we.popupmenu.pt.x - w2->left, e->we.popupmenu.pt.y - w2->top) == WP(w, menu_d).main_button) |
1373 if (index > 0) index = GetPlayerIndexFromMenu(index - 1) + 1; |
1368 index = WP(w, menu_d).sel_index; |
1374 } else { |
1369 } |
1375 index = GetPlayerIndexFromMenu(index); |
1370 |
1376 } |
1371 delete w; |
1377 |
1372 |
1378 if (index < 0) { |
1373 if (index >= 0) { |
1379 Window *w2 = FindWindowById(WC_MAIN_TOOLBAR,0); |
1374 assert(index >= 0 && index < 30); |
1380 if (GetWidgetFromPos(w2, _cursor.pos.x - w2->left, _cursor.pos.y - w2->top) == WP(w, menu_d).main_button) |
1375 _menu_clicked_procs[action_id](index); |
1381 index = WP(w, menu_d).sel_index; |
|
1382 } |
|
1383 |
|
1384 delete w; |
|
1385 |
|
1386 if (index >= 0) { |
|
1387 assert(index >= 0 && index < 30); |
|
1388 _menu_clicked_procs[action_id](index); |
|
1389 } |
1376 } |
1390 } |
1377 } break; |
1391 } break; |
1378 |
|
1379 case WE_POPUPMENU_OVER: { |
|
1380 int index; |
|
1381 UpdatePlayerMenuHeight(w); |
|
1382 index = GetMenuItemIndex(w, e->we.popupmenu.pt.x, e->we.popupmenu.pt.y); |
|
1383 |
|
1384 /* We have a new entry at the top of the list of menu 9 when networking |
|
1385 * so keep that in count */ |
|
1386 if (_networking && WP(w, menu_d).main_button == 9) { |
|
1387 if (index > 0) index = GetPlayerIndexFromMenu(index - 1) + 1; |
|
1388 } else { |
|
1389 index = GetPlayerIndexFromMenu(index); |
|
1390 } |
|
1391 |
|
1392 if (index == -1 || index == WP(w, menu_d).sel_index) return; |
|
1393 |
|
1394 WP(w, menu_d).sel_index = index; |
|
1395 w->SetDirty(); |
|
1396 return; |
|
1397 } |
|
1398 } |
1392 } |
1399 } |
1393 } |
1400 |
1394 |
1401 static const Widget _player_menu_widgets[] = { |
1395 static const Widget _player_menu_widgets[] = { |
1402 { WWT_PANEL, RESIZE_NONE, 14, 0, 240, 0, 81, 0x0, STR_NULL}, |
1396 { WWT_PANEL, RESIZE_NONE, 14, 0, 240, 0, 81, 0x0, STR_NULL}, |
1424 } |
1418 } |
1425 WP(w, menu_d).action_id = main_button; |
1419 WP(w, menu_d).action_id = main_button; |
1426 WP(w, menu_d).main_button = main_button; |
1420 WP(w, menu_d).main_button = main_button; |
1427 WP(w, menu_d).checked_items = gray; |
1421 WP(w, menu_d).checked_items = gray; |
1428 WP(w, menu_d).disabled_items = 0; |
1422 WP(w, menu_d).disabled_items = 0; |
1429 _popup_menu_active = true; |
1423 |
1430 SndPlayFx(SND_15_BEEP); |
1424 SndPlayFx(SND_15_BEEP); |
1431 return w; |
1425 return w; |
1432 } |
1426 } |
1433 |
1427 |
1434 /* --- Allocating the toolbar --- */ |
1428 /* --- Allocating the toolbar --- */ |