Пример #1
0
/**
**  Center on group.
**
**  @param group  Group number to center on.
**
**  @todo Improve this function, try to show all selected units
**        or the most possible units.
*/
static void UiCenterOnGroup(unsigned group, GroupSelectionMode mode = SELECTABLE_BY_RECTANGLE_ONLY)
{
	int n = GetNumberUnitsOfGroup(group, SELECT_ALL);

	if (n--) {
		CUnit **units = GetUnitsOfGroup(group);
		PixelPos pos(-1, -1);

		// FIXME: what should we do with the removed units? ignore?
		if (units[n]->Type && units[n]->Type->CanSelect(mode)) {
			pos = units[n]->GetMapPixelPosCenter();
		}
		while (n--) {
			if (units[n]->Type && units[n]->Type->CanSelect(mode)) {
				if (pos.x != -1) {
					pos += (units[n]->GetMapPixelPosCenter() - pos) / 2;
				} else {
					pos = units[n]->GetMapPixelPosCenter();
				}
			}
		}
		if (pos.x != -1) {
			UI.SelectedViewport->Center(pos);
		}
	}
}
Пример #2
0
/**
**  Add group to current selection.
**
**  @param group  Group number to add.
*/
static void UiAddGroupToSelection(unsigned group)
{
	CUnit **units;
	int n;

	if (!(n = GetNumberUnitsOfGroup(group, SELECT_ALL))) {
		return;
	}

	//
	//  Don't allow to mix units and buildings
	//
	if (NumSelected && Selected[0]->Type->Building) {
		return;
	}

	units = GetUnitsOfGroup(group);

	while (n--) {
		if (!(units[n]->Removed || units[n]->Type->Building)) {
			SelectUnit(*units[n]);
		}
	}

	SelectionChanged();
}
Пример #3
0
/**
**  Change selected units to units from group group_number
**  Doesn't change the selection if the group has no unit.
**
**  @param group_number  number of the group to be selected.
**
**  @return              number of units in the group.
*/
int SelectGroup(int group_number, GroupSelectionMode mode)
{
	int nunits = GetNumberUnitsOfGroup(group_number, SELECT_ALL);
	if (nunits) {
		if (mode == SELECT_ALL || !IsGroupTainted(group_number)) {
			ChangeSelectedUnits(GetUnitsOfGroup(group_number), nunits);
			return NumSelected;
		} else {
			std::vector<CUnit *> table;
			CUnit **group = GetUnitsOfGroup(group_number);

			for (int i = 0; i < nunits; ++i) {
				const CUnitType *type = group[i]->Type;
				if (type && type->CanSelect(mode)) {
					table.push_back(group[i]);
				}
			}
			if (table.empty() == false) {
				ChangeSelectedUnits(&table[0], static_cast<int>(table.size()));
				return NumSelected;
			}
		}
	}
	return 0;
}