Skip to content

Commit 6705c24

Browse files
authored
Merge pull request #1 from pavanvo/feat/hotkeys
Sorted commands (in standard positions) for the command grid
2 parents 9ba6455 + 7eeff4b commit 6705c24

File tree

6 files changed

+131
-26
lines changed

6 files changed

+131
-26
lines changed

source/glest_game/gui/gui.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ Gui::Gui(){
9797
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] START\n",__FILE__,__FUNCTION__);
9898

9999
lastGroupRecall = -1;
100-
numberCommands=0;
101100
posObjWorld= Vec2i(54, 14);
102101
validPosObjWorld= false;
103102
activeCommandType= NULL;
@@ -454,15 +453,11 @@ void Gui::hotKey(SDL_KeyboardEvent key) {
454453
if(isKeyPressed(configKeys.getSDLKey(name.c_str()),key) == true) {
455454
if(activeCommandType != NULL && activeCommandType->getClass() == ccBuild) {
456455
mouseDownDisplayUnitBuild(i);
457-
computeDisplay();
458-
break;
459456
} else {
460-
if (i < numberCommands) {
461-
mouseDownDisplayUnitSkills(i);
462-
computeDisplay();
463-
}
464-
break;
457+
mouseDownDisplayUnitSkills(i);
465458
}
459+
computeDisplay();
460+
break;
466461
}
467462
}
468463
}
@@ -716,7 +711,7 @@ void Gui::mouseDownDisplayUnitSkills(int posDisplay) {
716711
}
717712

718713
//give orders depending on command type
719-
if(!selection.isEmpty()){
714+
if(activeCommandClass != ccNull){
720715
const CommandType *ct= selection.getUnit(0)->getType()->getFirstCtOfClass(activeCommandClass);
721716
if(activeCommandType!=NULL && activeCommandType->getClass()==ccBuild){
722717
assert(selection.isUniform());
@@ -730,7 +725,7 @@ void Gui::mouseDownDisplayUnitSkills(int posDisplay) {
730725
selectingPos= true;
731726
activePos= posDisplay;
732727
}
733-
}
728+
} else { posDisplay= invalidPos;}
734729
}
735730
else{
736731
activePos= posDisplay;
@@ -982,10 +977,11 @@ void Gui::computeDisplay(){
982977
if(u->isBuilt()){
983978
//printf("u->isBuilt()\n");
984979

985-
int morphPos= 8;
986-
for(int i= 0; i < ut->getCommandTypeCount(); ++i){
980+
int morphPos= CommandHelper::getMorphPos();
981+
for(int i= 0; i < ut->getCommandTypeSortedCount(); ++i){
987982
int displayPos= i;
988-
const CommandType *ct= ut->getCommandType(i);
983+
const CommandType *ct= ut->getCommandTypeSorted(i);
984+
if(ct == NULL) continue;
989985
if(ct->getClass() == ccMorph) {
990986
displayPos= morphPos++;
991987
}
@@ -1028,28 +1024,25 @@ void Gui::computeDisplay(){
10281024
}
10291025
}
10301026
}
1031-
numberCommands = displayPos;
10321027
}
1033-
numberCommands++;
10341028
}
10351029
}
10361030
else{
10371031
//printf("selection.isUniform() == FALSE\n");
10381032
//non uniform selection
1039-
int lastCommand= 0;
1040-
for(int i= 0; i < ccCount; ++i){
1041-
CommandClass cc= static_cast<CommandClass> (i);
1033+
int basicPos= CommandHelper::getBasicPos();
1034+
// only basics can be shared
1035+
for(auto &&cc : CommandHelper::getBasicsCC()){
10421036

1043-
//printf("computeDisplay i = %d cc = %d isshared = %d lastCommand = %d\n",i,cc,isSharedCommandClass(cc),lastCommand);
1037+
//printf("computeDisplay i = %d cc = %d isshared = %d basicPos = %d\n",i,cc,isSharedCommandClass(cc),basicPos);
10441038

1045-
if(isSharedCommandClass(cc) && cc != ccBuild){
1046-
display.setDownLighted(lastCommand, true);
1047-
display.setDownImage(lastCommand, ut->getFirstCtOfClass(cc)->getImage());
1048-
display.setCommandClass(lastCommand, cc);
1049-
lastCommand++;
1039+
auto ccPos = CommandHelper::getBasicPos(cc);
1040+
if(isSharedCommandClass(cc)){
1041+
display.setDownLighted(basicPos + ccPos, true);
1042+
display.setDownImage(basicPos + ccPos, ut->getFirstCtOfClass(cc)->getImage());
1043+
display.setCommandClass(basicPos + ccPos, cc);
10501044
}
10511045
}
1052-
numberCommands = lastCommand;
10531046
}
10541047
}
10551048
else if (activeCommandType != NULL && activeCommandType->getClass() == ccBuild) {

source/glest_game/gui/gui.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ class Gui {
125125
CommandClass activeCommandClass;
126126
int activePos;
127127
int lastPosDisplay;
128-
int numberCommands;
129128

130129
//composite
131130
Display display;

source/glest_game/types/command_type.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ using namespace Shared::Util;
3131

3232
namespace Glest{ namespace Game{
3333

34+
// =====================================================
35+
// class CommandHelper
36+
// =====================================================
37+
38+
int CommandHelper::getBasicPos(CommandClass cc){
39+
switch(cc) {
40+
case ccAttack: return 0; break;
41+
case ccStop: return 1; break;
42+
case ccMove: return 2; break;
43+
case ccAttackStopped: return 3; break;
44+
default:
45+
return ccNull;
46+
}
47+
}
3448

3549
// =====================================================
3650
// class CommandType

source/glest_game/types/command_type.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ enum Queueability {
6666
qAlways
6767
};
6868

69+
class CommandHelper {// TODO put magic numbers to settings
70+
public:
71+
inline static int getCorePos() { return 0; }
72+
inline static int getBasicPos() { return 4; }
73+
inline static int getMorphPos() { return 8; }
74+
static int getBasicPos(CommandClass cc);
75+
inline static vector<CommandClass> getBasicsCC() { return { ccAttack, ccStop, ccMove, ccAttackStopped }; }
76+
77+
private:
78+
CommandHelper(){ }
79+
};
80+
6981
// =====================================================
7082
// class CommandType
7183
//

source/glest_game/types/unit_type.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ void UnitType::loaddd(int id,const string &dir, const TechTree *techTree,
880880
}
881881
}
882882
}
883+
sortCommandTypes(commandTypes);
883884

884885
computeFirstStOfClass();
885886
computeFirstCtOfClass();
@@ -1235,6 +1236,78 @@ void UnitType::computeFirstCtOfClass() {
12351236
}
12361237
}
12371238

1239+
void UnitType::sortCommandTypes(CommandTypes cts){
1240+
CommandTypes ctBuf(cts);
1241+
1242+
CommandTypes ctCores;
1243+
CommandTypes ctBasics = {NULL,NULL,NULL,NULL};
1244+
CommandTypes ctMorphs;
1245+
1246+
CommandTypes ctAttack;
1247+
CommandTypes ctBuild;
1248+
1249+
//Morphs
1250+
for(int i = (int)ctBuf.size(); i --> 0; ) {
1251+
if(ctBuf[i]->getClass() == ccMorph) {
1252+
ctMorphs.insert(ctMorphs.begin(), ctBuf[i]);
1253+
ctBuf.erase(ctBuf.begin() + i);
1254+
}
1255+
}
1256+
1257+
//Attacks
1258+
CommandTypeFilter(ctBuf, ctAttack, ccAttack);
1259+
if(ctAttack.size() > 0) {
1260+
ctBasics[0] = ctAttack[0];// first attack to basics
1261+
ctAttack.erase(ctAttack.begin());// another to cores
1262+
ctCores.insert(ctCores.end(), ctAttack.begin(), ctAttack.end());
1263+
}
1264+
auto basicsCC = CommandHelper::getBasicsCC();
1265+
// removing attack cuz we catch all attacks above
1266+
basicsCC.erase( basicsCC.begin() + CommandHelper::getBasicPos(ccAttack));
1267+
1268+
//Basics
1269+
for(int i = (int)ctBuf.size(); i --> 0; ) {
1270+
for(auto &&cc : basicsCC ){
1271+
if(ctBuf[i]->getClass() == cc) {
1272+
auto ccPos = CommandHelper::getBasicPos(cc);
1273+
ctBasics[ccPos] = ctBuf[i];
1274+
ctBuf.erase(ctBuf.begin() + i);
1275+
}
1276+
}
1277+
}
1278+
1279+
// //Cores
1280+
CommandTypeFilter(ctBuf, ctCores, ccProduce);
1281+
CommandTypeFilter(ctBuf, ctCores, ccUpgrade);
1282+
CommandTypeFilter(ctBuf, ctCores, ccSwitchTeam);
1283+
CommandTypeFilter(ctBuf, ctCores, ccHarvest);
1284+
CommandTypeFilter(ctBuf, ctCores, ccRepair);
1285+
1286+
//Build
1287+
CommandTypeFilter(ctBuf, ctBuild, ccBuild);// Build position always 4 in cores
1288+
if(ctCores.size() == 4) {/*do nothing*/ }
1289+
else if(ctCores.size() < 4) {
1290+
int nullCount = 4 - ctCores.size();
1291+
for(int i=0; i<nullCount; i++){
1292+
ctCores.push_back(NULL);
1293+
}
1294+
if(ctBuild.size() > 0){
1295+
ctCores[3] = ctBuild[0];
1296+
}
1297+
}
1298+
commandTypesSorted.insert(commandTypesSorted.end(), ctCores.begin(), ctCores.end());
1299+
commandTypesSorted.insert(commandTypesSorted.end(), ctBasics.begin(), ctBasics.end());
1300+
commandTypesSorted.insert(commandTypesSorted.end(), ctMorphs.begin(), ctMorphs.end());
1301+
}
1302+
1303+
void UnitType::CommandTypeFilter(CommandTypes &input, CommandTypes &output, CommandClass cc){
1304+
std::copy_if(input.begin(), input.end(), std::back_inserter(output), [cc](CommandType* i) {
1305+
if(i->getClass() == cc)
1306+
return true;
1307+
else return false;
1308+
});
1309+
}
1310+
12381311
const CommandType* UnitType::findCommandTypeById(int id) const{
12391312
const HarvestEmergencyReturnCommandType *result = dynamic_cast<const HarvestEmergencyReturnCommandType *>(ctHarvestEmergencyReturnCommandType.get());
12401313
if(result != NULL && id == result->getId()) {
@@ -1259,6 +1332,15 @@ const CommandType *UnitType::getCommandType(int i) const {
12591332
return commandTypes[i];
12601333
}
12611334

1335+
const CommandType *UnitType::getCommandTypeSorted(int i) const {
1336+
if(i >= (int)commandTypesSorted.size()) {
1337+
char szBuf[8096]="";
1338+
snprintf(szBuf,8096,"In [%s::%s Line: %d] i >= commandTypesSorted.size(), i = %d, commandTypesSorted.size() = " MG_SIZE_T_SPECIFIER "",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,commandTypesSorted.size());
1339+
throw megaglest_runtime_error(szBuf);
1340+
}
1341+
return commandTypesSorted[i];
1342+
}
1343+
12621344
string UnitType::getCommandTypeListDesc() const {
12631345
string desc = "Commands: ";
12641346
for(int i=0; i<getCommandTypeCount(); ++i){

source/glest_game/types/unit_type.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ class UnitType: public ProducibleType, public ValueCheckerVault {
215215
//info
216216
SkillTypes skillTypes;
217217
CommandTypes commandTypes;
218+
CommandTypes commandTypesSorted;
218219
StoredResources storedResources;
219220
Levels levels;
220221
LootableResources lootableResources;
@@ -273,10 +274,12 @@ class UnitType: public ProducibleType, public ValueCheckerVault {
273274
inline const ArmorType *getArmorType() const {return armorType;}
274275
inline const SkillType *getSkillType(int i) const {return skillTypes[i];}
275276
const CommandType *getCommandType(int i) const;
277+
const CommandType *getCommandTypeSorted(int i) const;
276278
inline const Level *getLevel(int i) const {return &levels[i];}
277279
const Level *getLevel(string name) const;
278280
inline int getSkillTypeCount() const {return (int)skillTypes.size();}
279281
inline int getCommandTypeCount() const {return (int)commandTypes.size();}
282+
inline int getCommandTypeSortedCount() const {return (int)commandTypesSorted.size();}
280283
inline int getLevelCount() const {return (int)levels.size();}
281284
inline bool getLight() const {return light;}
282285
inline bool getRotationAllowed() const {return rotationAllowed;}
@@ -361,6 +364,8 @@ class UnitType: public ProducibleType, public ValueCheckerVault {
361364
private:
362365
void computeFirstStOfClass();
363366
void computeFirstCtOfClass();
367+
void sortCommandTypes(CommandTypes cts);
368+
void CommandTypeFilter(CommandTypes &input, CommandTypes &output, CommandClass cc);
364369
};
365370

366371
/**

0 commit comments

Comments
 (0)