Skip to content

Commit c828560

Browse files
authored
Add clickable unit portraits (#231)
* Add clickable unit portraits clicking on a unit portrait selects that unit shift clicking on it, deselects it * Add control+click portrait to select units of the same type
1 parent 34e1a13 commit c828560

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

source/glest_game/gui/display.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ int Display::computeDownIndex(int x, int y) const {
112112
return index;
113113
}
114114

115+
int Display::computeUpIndex(int x, int y) const {
116+
y= y-(Metrics::getInstance().getDisplayH()-upCellSideCount*upImageSize);
117+
118+
if(y>imageSize*upCellSideCount || y < 0){
119+
return invalidPos;
120+
}
121+
122+
int cellX= x/upImageSize;
123+
int cellY= (y/upImageSize) % upCellSideCount;
124+
int index= (upCellSideCount-cellY-1)*upCellSideCount+cellX;;
125+
126+
if(index<0 || index>=upCellCount || upImages[index]==NULL){
127+
index= invalidPos;
128+
}
129+
130+
return index;
131+
}
132+
115133
int Display::computeDownX(int index) const{
116134
return (index % cellSideCount) * imageSize;
117135
}

source/glest_game/gui/display.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class Display{
105105
void clear();
106106
void switchColor();
107107
int computeDownIndex(int x, int y) const;
108+
int computeUpIndex(int x, int y) const;
108109
int computeDownX(int index) const;
109110
int computeDownY(int index) const;
110111
int computeUpX(int index) const;

source/glest_game/gui/gui.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Gui::Gui(){
102102
activeCommandType= NULL;
103103
activeCommandClass= ccStop;
104104
selectingBuilding= false;
105+
hoveringUnitPortrait= false;
105106
selectedBuildingFacing = CardinalDir(CardinalDir::NORTH);
106107
selectingPos= false;
107108
selectingMeetingPoint= false;
@@ -203,6 +204,7 @@ void Gui::invalidatePosObjWorld(){
203204

204205
void Gui::resetState(){
205206
selectingBuilding= false;
207+
hoveringUnitPortrait= false;
206208
selectedBuildingFacing = CardinalDir(CardinalDir::NORTH);
207209
selectingPos= false;
208210
selectingMeetingPoint= false;
@@ -237,7 +239,10 @@ void Gui::mouseDownLeftDisplay(int x, int y) {
237239
int posDisplay = computePosDisplay(x, y);
238240

239241
if(posDisplay != invalidPos) {
240-
if(selection.isCommandable()) {
242+
if (hoveringUnitPortrait) {
243+
mouseDownPortrait(posDisplay);
244+
} else {
245+
if(selection.isCommandable()) {
241246

242247
if(selectingBuilding) {
243248
mouseDownDisplayUnitBuild(posDisplay);
@@ -249,6 +254,7 @@ void Gui::mouseDownLeftDisplay(int x, int y) {
249254
else {
250255
resetState();
251256
}
257+
}
252258
}
253259
computeDisplay();
254260
}
@@ -646,6 +652,17 @@ void Gui::clickCommonCommand(CommandClass commandClass) {
646652
computeDisplay();
647653
}
648654

655+
void Gui::mouseDownPortrait(int posDisplay) {
656+
Unit *unit = selection.getUnitPtr(posDisplay);
657+
if (isKeyDown(vkControl)) {
658+
selection.selectType(unit);
659+
} else if (!isKeyDown(vkShift)) {
660+
selection.clear();
661+
selection.select(unit, false);
662+
} else {
663+
selection.unSelect(posDisplay);
664+
}
665+
}
649666
void Gui::mouseDownDisplayUnitSkills(int posDisplay) {
650667
if(selection.isEmpty() == false) {
651668
if(posDisplay != cancelPos) {
@@ -785,7 +802,7 @@ void Gui::computeInfoString(int posDisplay){
785802

786803
display.setInfoText(computeDefaultInfoString());
787804

788-
if(posDisplay!=invalidPos && selection.isCommandable()){
805+
if(!hoveringUnitPortrait && posDisplay!=invalidPos && selection.isCommandable()){
789806
if(!selectingBuilding){
790807
if(posDisplay==cancelPos){
791808
display.setInfoText(lang.getString("Cancel"));
@@ -1135,6 +1152,12 @@ int Gui::computePosDisplay(int x, int y){
11351152
}
11361153

11371154
//printf("computePosDisplay returning = %d\n",posDisplay);
1155+
if (posDisplay == invalidPos) {
1156+
posDisplay = display.computeUpIndex(x, y);
1157+
if (posDisplay != invalidPos) {
1158+
hoveringUnitPortrait = true;
1159+
}
1160+
}
11381161

11391162
return posDisplay;
11401163
}

source/glest_game/gui/gui.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class Gui {
139139

140140
//states
141141
bool selectingBuilding;
142+
bool hoveringUnitPortrait;
142143
bool selectingPos;
143144
bool selectingMeetingPoint;
144145

@@ -222,6 +223,7 @@ class Gui {
222223
int computePosDisplay(int x, int y);
223224
void computeDisplay();
224225
void resetState();
226+
void mouseDownPortrait(int posDisplay);
225227
void mouseDownDisplayUnitSkills(int posDisplay);
226228
void mouseDownDisplayUnitBuild(int posDisplay);
227229
void computeInfoString(int posDisplay);

source/glest_game/gui/selection.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ void Selection::select(const UnitContainer &units, bool addToSelection){
149149
}
150150
}
151151

152+
void Selection::selectType(Unit *unit) {
153+
UnitContainer units;
154+
for (int i = 0; i < (int)selectedUnits.size(); i++) {
155+
if (selectedUnits[i]->getType() == unit->getType() && unit->isOperative() == selectedUnits[i]->isOperative() ) {
156+
units.push_back(selectedUnits[i]);
157+
}
158+
}
159+
selectedUnits = units;
160+
}
161+
152162
void Selection::unSelect(const UnitContainer &units) {
153163

154164
//add units to gui

source/glest_game/gui/selection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class Selection: public UnitObserver {
6969
virtual ~Selection();
7070

7171
bool select(Unit *unit, bool addToSelection);
72+
void selectType(Unit *unit);
7273
void select(const UnitContainer &units, bool addToSelection);
7374
void unSelect(const UnitContainer &units);
7475
void unSelect(int unitIndex);

0 commit comments

Comments
 (0)