Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clickable unit portraits #231

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions source/glest_game/gui/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ int Display::computeDownIndex(int x, int y) const {
return index;
}

int Display::computeUpIndex(int x, int y) const {
y= y-(Metrics::getInstance().getDisplayH()-upCellSideCount*upImageSize);

if(y>imageSize*upCellSideCount || y < 0){
return invalidPos;
}

int cellX= x/upImageSize;
int cellY= (y/upImageSize) % upCellSideCount;
int index= (upCellSideCount-cellY-1)*upCellSideCount+cellX;;

if(index<0 || index>=upCellCount || upImages[index]==NULL){
index= invalidPos;
}

return index;
}

int Display::computeDownX(int index) const{
return (index % cellSideCount) * imageSize;
}
Expand Down
1 change: 1 addition & 0 deletions source/glest_game/gui/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Display{
void clear();
void switchColor();
int computeDownIndex(int x, int y) const;
int computeUpIndex(int x, int y) const;
int computeDownX(int index) const;
int computeDownY(int index) const;
int computeUpX(int index) const;
Expand Down
27 changes: 25 additions & 2 deletions source/glest_game/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Gui::Gui(){
activeCommandType= NULL;
activeCommandClass= ccStop;
selectingBuilding= false;
hoveringUnitPortrait= false;
selectedBuildingFacing = CardinalDir(CardinalDir::NORTH);
selectingPos= false;
selectingMeetingPoint= false;
Expand Down Expand Up @@ -203,6 +204,7 @@ void Gui::invalidatePosObjWorld(){

void Gui::resetState(){
selectingBuilding= false;
hoveringUnitPortrait= false;
selectedBuildingFacing = CardinalDir(CardinalDir::NORTH);
selectingPos= false;
selectingMeetingPoint= false;
Expand Down Expand Up @@ -237,7 +239,10 @@ void Gui::mouseDownLeftDisplay(int x, int y) {
int posDisplay = computePosDisplay(x, y);

if(posDisplay != invalidPos) {
if(selection.isCommandable()) {
if (hoveringUnitPortrait) {
mouseDownPortrait(posDisplay);
} else {
if(selection.isCommandable()) {

if(selectingBuilding) {
mouseDownDisplayUnitBuild(posDisplay);
Expand All @@ -249,6 +254,7 @@ void Gui::mouseDownLeftDisplay(int x, int y) {
else {
resetState();
}
}
}
computeDisplay();
}
Expand Down Expand Up @@ -646,6 +652,17 @@ void Gui::clickCommonCommand(CommandClass commandClass) {
computeDisplay();
}

void Gui::mouseDownPortrait(int posDisplay) {
Unit *unit = selection.getUnitPtr(posDisplay);
if (isKeyDown(vkControl)) {
selection.selectType(unit);
} else if (!isKeyDown(vkShift)) {
selection.clear();
selection.select(unit, false);
} else {
selection.unSelect(posDisplay);
}
}
void Gui::mouseDownDisplayUnitSkills(int posDisplay) {
if(selection.isEmpty() == false) {
if(posDisplay != cancelPos) {
Expand Down Expand Up @@ -778,7 +795,7 @@ void Gui::computeInfoString(int posDisplay){

display.setInfoText(computeDefaultInfoString());

if(posDisplay!=invalidPos && selection.isCommandable()){
if(!hoveringUnitPortrait && posDisplay!=invalidPos && selection.isCommandable()){
if(!selectingBuilding){
if(posDisplay==cancelPos){
display.setInfoText(lang.getString("Cancel"));
Expand Down Expand Up @@ -1113,6 +1130,12 @@ int Gui::computePosDisplay(int x, int y){
}

//printf("computePosDisplay returning = %d\n",posDisplay);
if (posDisplay == invalidPos) {
posDisplay = display.computeUpIndex(x, y);
if (posDisplay != invalidPos) {
hoveringUnitPortrait = true;
}
}

return posDisplay;
}
Expand Down
2 changes: 2 additions & 0 deletions source/glest_game/gui/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class Gui {

//states
bool selectingBuilding;
bool hoveringUnitPortrait;
bool selectingPos;
bool selectingMeetingPoint;

Expand Down Expand Up @@ -222,6 +223,7 @@ class Gui {
int computePosDisplay(int x, int y);
void computeDisplay();
void resetState();
void mouseDownPortrait(int posDisplay);
void mouseDownDisplayUnitSkills(int posDisplay);
void mouseDownDisplayUnitBuild(int posDisplay);
void computeInfoString(int posDisplay);
Expand Down
10 changes: 10 additions & 0 deletions source/glest_game/gui/selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ void Selection::select(const UnitContainer &units, bool addToSelection){
}
}

void Selection::selectType(Unit *unit) {
UnitContainer units;
for (int i = 0; i < (int)selectedUnits.size(); i++) {
if (selectedUnits[i]->getType() == unit->getType() && unit->isOperative() == selectedUnits[i]->isOperative() ) {
units.push_back(selectedUnits[i]);
}
}
selectedUnits = units;
}

void Selection::unSelect(const UnitContainer &units) {

//add units to gui
Expand Down
1 change: 1 addition & 0 deletions source/glest_game/gui/selection.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Selection: public UnitObserver {
virtual ~Selection();

bool select(Unit *unit, bool addToSelection);
void selectType(Unit *unit);
void select(const UnitContainer &units, bool addToSelection);
void unSelect(const UnitContainer &units);
void unSelect(int unitIndex);
Expand Down