2010-03-12 08:42:55 +01:00
// ==============================================================
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2011-12-14 08:40:48 +01:00
// Copyright (C) 2001-2008 Martiño Figueroa
2010-03-12 08:42:55 +01:00
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
# include "gui.h"
# include <cassert>
# include <algorithm>
# include "world.h"
# include "renderer.h"
# include "game.h"
# include "upgrade.h"
# include "unit.h"
# include "metrics.h"
# include "display.h"
# include "platform_util.h"
# include "sound_renderer.h"
# include "util.h"
# include "faction.h"
# include "leak_dumper.h"
2010-03-18 22:26:40 +01:00
# include "network_types.h"
# include "network_manager.h"
2010-03-12 08:42:55 +01:00
using namespace Shared : : Graphics ;
using namespace Shared : : Util ;
namespace Glest { namespace Game {
// =====================================================
// class Mouse3d
// =====================================================
const float Mouse3d : : fadeSpeed = 1.f / 50.f ;
2010-04-18 21:28:52 +02:00
static const int queueCommandKey = vkShift ;
2010-03-12 08:42:55 +01:00
Mouse3d : : Mouse3d ( ) {
enabled = false ;
rot = 0 ;
fade = 0.f ;
}
void Mouse3d : : enable ( ) {
enabled = true ;
fade = 0.f ;
}
void Mouse3d : : update ( ) {
if ( enabled ) {
rot = ( rot + 3 ) % 360 ;
fade + = fadeSpeed ;
if ( fade > 1.f ) fade = 1.f ;
}
}
// ===============================
// class SelectionQuad
// ===============================
SelectionQuad : : SelectionQuad ( ) {
enabled = false ;
posDown = Vec2i ( 0 ) ;
posUp = Vec2i ( 0 ) ;
}
void SelectionQuad : : setPosDown ( const Vec2i & posDown ) {
enabled = true ;
this - > posDown = posDown ;
this - > posUp = posDown ;
}
void SelectionQuad : : setPosUp ( const Vec2i & posUp ) {
this - > posUp = posUp ;
}
void SelectionQuad : : disable ( ) {
enabled = false ;
}
// =====================================================
// class Gui
// =====================================================
//constructor
Gui : : Gui ( ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] START \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-12 08:42:55 +01:00
2011-06-10 18:59:19 +02:00
lastGroupRecall = - 1 ;
2010-03-12 08:42:55 +01:00
posObjWorld = Vec2i ( 54 , 14 ) ;
validPosObjWorld = false ;
activeCommandType = NULL ;
activeCommandClass = ccStop ;
selectingBuilding = false ;
2010-03-25 13:15:10 +01:00
selectedBuildingFacing = CardinalDir : : NORTH ;
2010-03-12 08:42:55 +01:00
selectingPos = false ;
selectingMeetingPoint = false ;
activePos = invalidPos ;
2011-05-01 22:19:41 +02:00
lastPosDisplay = invalidPos ;
2010-09-10 14:15:21 +02:00
lastQuadCalcFrame = 0 ;
selectionCalculationFrameSkip = 10 ;
minQuadSize = 20 ;
2012-04-04 02:30:16 +02:00
selectedResourceObjectPos = Vec2i ( - 1 , - 1 ) ;
highlightedResourceObjectPos = Vec2i ( - 1 , - 1 ) ;
2012-04-05 00:57:32 +02:00
highlightedUnitId = - 1 ;
2011-06-09 02:59:03 +02:00
hudTexture = NULL ;
2011-09-01 01:10:43 +02:00
commander = NULL ;
world = NULL ;
game = NULL ;
gameCamera = NULL ;
console = NULL ;
choosenBuildingType = NULL ;
2010-03-12 08:42:55 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] END \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-12 08:42:55 +01:00
}
void Gui : : init ( Game * game ) {
this - > commander = game - > getCommander ( ) ;
2011-12-02 17:07:59 +01:00
this - > gameCamera = game - > getGameCameraPtr ( ) ;
2010-03-12 08:42:55 +01:00
this - > console = game - > getConsole ( ) ;
this - > world = game - > getWorld ( ) ;
2010-09-10 14:15:21 +02:00
this - > game = game ;
2011-05-06 10:44:50 +02:00
selection . init ( this , world - > getThisFactionIndex ( ) , world - > getThisTeamIndex ( ) ) ;
2010-03-12 08:42:55 +01:00
}
void Gui : : end ( ) {
selection . clear ( ) ;
2011-06-09 02:59:03 +02:00
if ( hudTexture ! = NULL ) {
Renderer : : getInstance ( ) . endTexture ( rsGlobal , hudTexture , false ) ;
}
hudTexture = NULL ;
2010-03-12 08:42:55 +01:00
}
// ==================== get ====================
const UnitType * Gui : : getBuilding ( ) const {
assert ( selectingBuilding ) ;
return choosenBuildingType ;
}
2012-04-04 02:30:16 +02:00
const Object * Gui : : getSelectedResourceObject ( ) const {
if ( selectedResourceObjectPos . x = = - 1 ) {
return NULL ;
}
else {
return world - > getMap ( ) - > getSurfaceCell ( selectedResourceObjectPos ) - > getObject ( ) ;
}
}
Object * Gui : : getHighlightedResourceObject ( ) const {
if ( highlightedResourceObjectPos . x = = - 1 ) {
return NULL ;
}
else {
return world - > getMap ( ) - > getSurfaceCell ( highlightedResourceObjectPos ) - > getObject ( ) ;
}
}
2010-03-12 08:42:55 +01:00
2012-04-05 00:57:32 +02:00
Unit * Gui : : getHighlightedUnit ( ) const {
if ( highlightedUnitId = = - 1 ) {
return NULL ;
}
else {
return world - > findUnitById ( highlightedUnitId ) ;
}
}
2010-03-12 08:42:55 +01:00
// ==================== is ====================
bool Gui : : isPlacingBuilding ( ) const {
return isSelectingPos ( ) & & activeCommandType ! = NULL & & activeCommandType - > getClass ( ) = = ccBuild ;
}
// ==================== set ====================
void Gui : : invalidatePosObjWorld ( ) {
validPosObjWorld = false ;
}
// ==================== reset state ====================
void Gui : : resetState ( ) {
selectingBuilding = false ;
2010-03-25 13:15:10 +01:00
selectedBuildingFacing = CardinalDir : : NORTH ;
2010-03-12 08:42:55 +01:00
selectingPos = false ;
selectingMeetingPoint = false ;
activePos = invalidPos ;
activeCommandClass = ccStop ;
activeCommandType = NULL ;
}
// ==================== events ====================
void Gui : : update ( ) {
2011-01-08 22:53:05 +01:00
2010-09-10 14:15:21 +02:00
if ( selectionQuad . isEnabled ( ) & & selectionQuad . getPosUp ( ) . dist ( selectionQuad . getPosDown ( ) ) > minQuadSize ) {
computeSelected ( false , false ) ;
}
2010-03-12 08:42:55 +01:00
mouse3d . update ( ) ;
}
void Gui : : tick ( ) {
computeDisplay ( ) ;
}
//in display coords
2011-03-28 05:54:23 +02:00
bool Gui : : mouseValid ( int x , int y ) {
2010-03-12 08:42:55 +01:00
return computePosDisplay ( x , y ) ! = invalidPos ;
}
2011-03-28 05:54:23 +02:00
void Gui : : mouseDownLeftDisplay ( int x , int y ) {
if ( ! selectingPos & & ! selectingMeetingPoint ) {
2010-03-12 08:42:55 +01:00
int posDisplay = computePosDisplay ( x , y ) ;
2011-03-28 05:54:23 +02:00
if ( posDisplay ! = invalidPos ) {
if ( selection . isCommandable ( ) ) {
if ( selectingBuilding ) {
2010-03-12 08:42:55 +01:00
mouseDownDisplayUnitBuild ( posDisplay ) ;
}
2011-03-28 05:54:23 +02:00
else {
2010-03-12 08:42:55 +01:00
mouseDownDisplayUnitSkills ( posDisplay ) ;
}
}
2011-03-28 05:54:23 +02:00
else {
2010-03-12 08:42:55 +01:00
resetState ( ) ;
}
}
computeDisplay ( ) ;
}
}
2011-03-28 05:54:23 +02:00
void Gui : : mouseMoveDisplay ( int x , int y ) {
2010-03-12 08:42:55 +01:00
computeInfoString ( computePosDisplay ( x , y ) ) ;
}
2011-05-01 22:19:41 +02:00
void Gui : : mouseMoveOutsideDisplay ( ) {
computeInfoString ( invalidPos ) ;
}
2011-03-28 05:54:23 +02:00
void Gui : : mouseDownLeftGraphics ( int x , int y , bool prepared ) {
if ( selectingPos ) {
2010-03-12 08:42:55 +01:00
//give standard orders
2011-04-28 00:35:45 +02:00
Vec2i targetPos ;
if ( Renderer : : getInstance ( ) . computePosition ( Vec2i ( x , y ) , targetPos ) & &
world - > getMap ( ) - > isInsideSurface ( world - > getMap ( ) - > toSurfCoords ( targetPos ) ) = = true ) {
giveTwoClickOrders ( x , y , prepared ) ;
}
2010-03-12 08:42:55 +01:00
resetState ( ) ;
}
//set meeting point
2011-03-28 05:54:23 +02:00
else if ( selectingMeetingPoint ) {
if ( selection . isCommandable ( ) ) {
2010-03-12 08:42:55 +01:00
Vec2i targetPos ;
2011-04-28 00:35:45 +02:00
if ( Renderer : : getInstance ( ) . computePosition ( Vec2i ( x , y ) , targetPos ) & &
world - > getMap ( ) - > isInsideSurface ( world - > getMap ( ) - > toSurfCoords ( targetPos ) ) = = true ) {
2010-03-12 08:42:55 +01:00
commander - > trySetMeetingPoint ( selection . getFrontUnit ( ) , targetPos ) ;
}
}
resetState ( ) ;
}
2011-03-28 05:54:23 +02:00
else {
2010-03-12 08:42:55 +01:00
selectionQuad . setPosDown ( Vec2i ( x , y ) ) ;
2010-09-10 14:15:21 +02:00
computeSelected ( false , false ) ;
2010-03-12 08:42:55 +01:00
}
computeDisplay ( ) ;
}
2011-03-28 05:54:23 +02:00
void Gui : : mouseDownRightGraphics ( int x , int y , bool prepared ) {
if ( selectingPos | | selectingMeetingPoint ) {
2010-03-12 08:42:55 +01:00
resetState ( ) ;
}
2011-03-28 05:54:23 +02:00
else if ( selection . isCommandable ( ) ) {
if ( prepared ) {
2011-04-28 00:35:45 +02:00
Vec2i targetPos ;
if ( Renderer : : getInstance ( ) . computePosition ( Vec2i ( x , y ) , targetPos ) & &
world - > getMap ( ) - > isInsideSurface ( world - > getMap ( ) - > toSurfCoords ( targetPos ) ) = = true ) {
givePreparedDefaultOrders ( x , y ) ;
}
2010-12-18 18:18:36 +01:00
}
2011-03-28 05:54:23 +02:00
else {
2011-04-28 00:35:45 +02:00
Vec2i targetPos ;
if ( Renderer : : getInstance ( ) . computePosition ( Vec2i ( x , y ) , targetPos ) & &
world - > getMap ( ) - > isInsideSurface ( world - > getMap ( ) - > toSurfCoords ( targetPos ) ) = = true ) {
giveDefaultOrders ( x , y ) ;
}
2010-12-18 18:18:36 +01:00
}
2010-03-12 08:42:55 +01:00
}
computeDisplay ( ) ;
}
2011-03-28 05:54:23 +02:00
void Gui : : mouseUpLeftGraphics ( int x , int y ) {
if ( ! selectingPos & & ! selectingMeetingPoint ) {
2010-03-12 08:42:55 +01:00
if ( selectionQuad . isEnabled ( ) ) {
selectionQuad . setPosUp ( Vec2i ( x , y ) ) ;
2011-04-28 00:35:45 +02:00
if ( selectionQuad . getPosUp ( ) . dist ( selectionQuad . getPosDown ( ) ) > minQuadSize ) {
2010-09-10 14:15:21 +02:00
computeSelected ( false , true ) ;
}
2011-01-09 21:52:00 +01:00
if ( selection . isCommandable ( ) & & random . randRange ( 0 , 1 ) = = 0 ) {
2010-03-12 08:42:55 +01:00
SoundRenderer : : getInstance ( ) . playFx (
selection . getFrontUnit ( ) - > getType ( ) - > getSelectionSound ( ) ,
selection . getFrontUnit ( ) - > getCurrVector ( ) ,
gameCamera - > getPos ( ) ) ;
}
selectionQuad . disable ( ) ;
}
}
}
2011-03-28 05:54:23 +02:00
void Gui : : mouseMoveGraphics ( int x , int y ) {
2010-03-12 08:42:55 +01:00
//compute selection
if ( selectionQuad . isEnabled ( ) ) {
selectionQuad . setPosUp ( Vec2i ( x , y ) ) ;
2010-09-10 14:15:21 +02:00
computeSelected ( false , false ) ;
2010-03-12 08:42:55 +01:00
}
//compute position for building
if ( isPlacingBuilding ( ) ) {
validPosObjWorld = Renderer : : getInstance ( ) . computePosition ( Vec2i ( x , y ) , posObjWorld ) ;
}
display . setInfoText ( " " ) ;
}
void Gui : : mouseDoubleClickLeftGraphics ( int x , int y ) {
if ( ! selectingPos & & ! selectingMeetingPoint ) {
selectionQuad . setPosDown ( Vec2i ( x , y ) ) ;
2010-09-10 14:15:21 +02:00
computeSelected ( true , true ) ;
2010-03-12 08:42:55 +01:00
computeDisplay ( ) ;
}
}
2011-03-28 05:54:23 +02:00
void Gui : : groupKey ( int groupIndex ) {
2010-03-12 08:42:55 +01:00
if ( isKeyDown ( vkControl ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] groupIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , groupIndex ) ;
2010-04-06 16:25:00 +02:00
2010-03-12 08:42:55 +01:00
selection . assignGroup ( groupIndex ) ;
}
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] groupIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , groupIndex ) ;
2010-04-06 16:25:00 +02:00
2011-06-10 00:41:49 +02:00
Config & config = Config : : getInstance ( ) ;
int recallGroupCenterCameraTimeout = config . getInt ( " RecallGroupCenterCameraTimeoutMilliseconds " , " 1500 " ) ;
2011-06-10 18:59:19 +02:00
if ( lastGroupRecall = = groupIndex & &
lastGroupRecallTime . getMillis ( ) > 0 & &
lastGroupRecallTime . getMillis ( ) < = recallGroupCenterCameraTimeout ) {
2011-06-10 00:37:37 +02:00
centerCameraOnSelection ( ) ;
}
else {
selection . recallGroup ( groupIndex ) ;
}
lastGroupRecallTime . start ( ) ;
2011-06-10 18:59:19 +02:00
lastGroupRecall = groupIndex ;
2010-03-12 08:42:55 +01:00
}
}
2011-06-26 03:55:08 +02:00
void Gui : : hotKey ( SDL_KeyboardEvent key ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] key = [%c][%d] \n " , __FILE__ , __FUNCTION__ , key , key ) ;
2010-03-12 08:42:55 +01:00
2010-06-12 20:27:39 +02:00
Config & configKeys = Config : : getInstance ( std : : pair < ConfigType , ConfigType > ( cfgMainKeys , cfgUserKeys ) ) ;
2011-06-26 03:55:08 +02:00
//if(key == configKeys.getCharKey("HotKeyCenterCameraOnSelection")) {
if ( isKeyPressed ( configKeys . getSDLKey ( " HotKeyCenterCameraOnSelection " ) , key ) = = true ) {
2010-03-12 08:42:55 +01:00
centerCameraOnSelection ( ) ;
}
2011-06-26 03:55:08 +02:00
//else if(key == configKeys.getCharKey("HotKeySelectIdleHarvesterUnit")) {
else if ( isKeyPressed ( configKeys . getSDLKey ( " HotKeySelectIdleHarvesterUnit " ) , key ) = = true ) {
2010-03-12 08:42:55 +01:00
selectInterestingUnit ( iutIdleHarvester ) ;
}
2011-06-26 03:55:08 +02:00
//else if(key == configKeys.getCharKey("HotKeySelectBuiltBuilding")) {
else if ( isKeyPressed ( configKeys . getSDLKey ( " HotKeySelectBuiltBuilding " ) , key ) = = true ) {
2010-03-12 08:42:55 +01:00
selectInterestingUnit ( iutBuiltBuilding ) ;
}
2011-06-26 03:55:08 +02:00
//else if(key == configKeys.getCharKey("HotKeyDumpWorldToLog")) {
else if ( isKeyPressed ( configKeys . getSDLKey ( " HotKeyDumpWorldToLog " ) , key ) = = true ) {
2010-05-18 05:53:57 +02:00
std : : string worldLog = world - > DumpWorldToLog ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] worldLog dumped to [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , worldLog . c_str ( ) ) ;
2010-05-18 05:53:57 +02:00
}
2011-06-26 03:55:08 +02:00
//else if(key == configKeys.getCharKey("HotKeyRotateUnitDuringPlacement")){
else if ( isKeyPressed ( configKeys . getSDLKey ( " HotKeyRotateUnitDuringPlacement " ) , key ) = = true ) {
2010-03-18 22:26:40 +01:00
// Here the user triggers a unit rotation while placing a unit
2010-06-17 02:08:27 +02:00
if ( isPlacingBuilding ( ) ) {
2010-03-27 04:09:11 +01:00
if ( getBuilding ( ) - > getRotationAllowed ( ) ) {
+ + selectedBuildingFacing ;
}
2010-03-12 08:42:55 +01:00
}
}
2011-06-26 03:55:08 +02:00
//else if(key == configKeys.getCharKey("HotKeySelectDamagedUnit")) {
else if ( isKeyPressed ( configKeys . getSDLKey ( " HotKeySelectDamagedUnit " ) , key ) = = true ) {
2010-03-12 08:42:55 +01:00
selectInterestingUnit ( iutDamaged ) ;
}
2011-06-26 03:55:08 +02:00
//else if(key == configKeys.getCharKey("HotKeySelectStoreUnit")) {
else if ( isKeyPressed ( configKeys . getSDLKey ( " HotKeySelectStoreUnit " ) , key ) = = true ) {
2010-03-12 08:42:55 +01:00
selectInterestingUnit ( iutStore ) ;
}
2011-06-26 03:55:08 +02:00
//else if(key == configKeys.getCharKey("HotKeySelectedUnitsAttack")) {
else if ( isKeyPressed ( configKeys . getSDLKey ( " HotKeySelectedUnitsAttack " ) , key ) = = true ) {
2010-03-12 08:42:55 +01:00
clickCommonCommand ( ccAttack ) ;
}
2011-06-26 03:55:08 +02:00
//else if(key == configKeys.getCharKey("HotKeySelectedUnitsStop")) {
else if ( isKeyPressed ( configKeys . getSDLKey ( " HotKeySelectedUnitsStop " ) , key ) = = true ) {
2010-03-12 08:42:55 +01:00
clickCommonCommand ( ccStop ) ;
}
}
2010-04-08 22:19:25 +02:00
void Gui : : switchToNextDisplayColor ( ) {
display . switchColor ( ) ;
}
2010-03-12 08:42:55 +01:00
void Gui : : onSelectionChanged ( ) {
resetState ( ) ;
computeDisplay ( ) ;
}
// ================= PRIVATE =================
void Gui : : giveOneClickOrders ( ) {
2011-07-05 06:37:35 +02:00
//printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2012-11-10 20:39:55 +01:00
std : : pair < CommandResult , string > result ( crFailUndefined , " " ) ;
2010-04-18 21:28:52 +02:00
bool queueKeyDown = isKeyDown ( queueCommandKey ) ;
2010-03-12 08:42:55 +01:00
if ( selection . isUniform ( ) ) {
2010-04-18 21:28:52 +02:00
result = commander - > tryGiveCommand ( & selection , activeCommandType , Vec2i ( 0 ) , ( Unit * ) NULL , queueKeyDown ) ;
2010-03-12 08:42:55 +01:00
}
else {
2010-04-18 21:28:52 +02:00
result = commander - > tryGiveCommand ( & selection , activeCommandClass , Vec2i ( 0 ) , ( Unit * ) NULL , queueKeyDown ) ;
2010-03-12 08:42:55 +01:00
}
addOrdersResultToConsole ( activeCommandClass , result ) ;
activeCommandType = NULL ;
activeCommandClass = ccStop ;
}
2011-01-08 22:53:05 +01:00
void Gui : : giveDefaultOrders ( int x , int y ) {
2010-03-12 08:42:55 +01:00
//compute target
const Unit * targetUnit = NULL ;
Vec2i targetPos ;
2011-01-08 22:53:05 +01:00
if ( computeTarget ( Vec2i ( x , y ) , targetPos , targetUnit ) = = false ) {
2010-03-12 08:42:55 +01:00
console - > addStdMessage ( " InvalidPosition " ) ;
return ;
}
2010-12-20 04:12:00 +01:00
giveDefaultOrders ( targetPos . x , targetPos . y , targetUnit , true ) ;
2010-12-18 18:18:36 +01:00
}
void Gui : : givePreparedDefaultOrders ( int x , int y ) {
2011-07-05 06:37:35 +02:00
//printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-12-20 04:12:00 +01:00
giveDefaultOrders ( x , y , NULL , false ) ;
2010-12-18 18:18:36 +01:00
}
2010-03-12 08:42:55 +01:00
2011-01-08 22:53:05 +01:00
void Gui : : giveDefaultOrders ( int x , int y , const Unit * targetUnit , bool paintMouse3d ) {
2011-07-05 06:37:35 +02:00
//printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-04-18 21:28:52 +02:00
bool queueKeyDown = isKeyDown ( queueCommandKey ) ;
2010-12-18 18:18:36 +01:00
Vec2i targetPos = Vec2i ( x , y ) ;
2010-03-12 08:42:55 +01:00
//give order
2012-11-10 20:39:55 +01:00
std : : pair < CommandResult , string > result = commander - > tryGiveCommand ( & selection , targetPos , targetUnit , queueKeyDown ) ;
2010-03-12 08:42:55 +01:00
//graphical result
addOrdersResultToConsole ( activeCommandClass , result ) ;
2012-11-10 20:39:55 +01:00
if ( result . first = = crSuccess | | result . first = = crSomeFailed ) {
2010-12-20 04:12:00 +01:00
if ( paintMouse3d )
mouse3d . enable ( ) ;
2010-03-12 08:42:55 +01:00
if ( random . randRange ( 0 , 1 ) = = 0 ) {
SoundRenderer : : getInstance ( ) . playFx (
selection . getFrontUnit ( ) - > getType ( ) - > getCommandSound ( ) ,
selection . getFrontUnit ( ) - > getCurrVector ( ) ,
gameCamera - > getPos ( ) ) ;
}
}
//reset
resetState ( ) ;
}
2011-01-08 22:53:05 +01:00
void Gui : : giveTwoClickOrders ( int x , int y , bool prepared ) {
2011-07-05 06:37:35 +02:00
//printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2012-11-10 20:39:55 +01:00
std : : pair < CommandResult , string > result ( crFailUndefined , " " ) ;
2010-03-12 08:42:55 +01:00
//compute target
const Unit * targetUnit = NULL ;
Vec2i targetPos ;
2010-12-18 18:18:36 +01:00
if ( prepared ) {
targetPos = Vec2i ( x , y ) ;
}
2011-01-08 22:53:05 +01:00
else {
if ( computeTarget ( Vec2i ( x , y ) , targetPos , targetUnit ) = = false ) {
2010-12-18 18:18:36 +01:00
console - > addStdMessage ( " InvalidPosition " ) ;
return ;
}
2010-03-12 08:42:55 +01:00
}
2010-04-18 21:28:52 +02:00
bool queueKeyDown = isKeyDown ( queueCommandKey ) ;
2010-03-12 08:42:55 +01:00
//give orders to the units of this faction
2011-01-08 22:53:05 +01:00
if ( selectingBuilding = = false ) {
if ( selection . isUniform ( ) ) {
2010-08-28 03:46:26 +02:00
result = commander - > tryGiveCommand ( & selection , activeCommandType ,
targetPos , targetUnit , queueKeyDown ) ;
2010-03-12 08:42:55 +01:00
}
2011-01-08 22:53:05 +01:00
else {
2010-08-28 03:46:26 +02:00
result = commander - > tryGiveCommand ( & selection , activeCommandClass ,
targetPos , targetUnit , queueKeyDown ) ;
2010-04-18 09:35:48 +02:00
}
2010-03-12 08:42:55 +01:00
}
2011-01-08 22:53:05 +01:00
else {
2010-03-12 08:42:55 +01:00
//selecting building
2010-08-28 03:46:26 +02:00
result = commander - > tryGiveCommand ( & selection ,
activeCommandType , posObjWorld , choosenBuildingType ,
selectedBuildingFacing , queueKeyDown ) ;
2010-03-12 08:42:55 +01:00
}
//graphical result
addOrdersResultToConsole ( activeCommandClass , result ) ;
2012-11-10 20:39:55 +01:00
if ( result . first = = crSuccess | | result . first = = crSomeFailed ) {
2011-01-08 22:53:05 +01:00
if ( prepared = = false ) {
2010-12-20 04:12:00 +01:00
mouse3d . enable ( ) ;
}
2010-03-12 08:42:55 +01:00
2011-01-08 22:53:05 +01:00
if ( random . randRange ( 0 , 1 ) = = 0 ) {
2010-03-12 08:42:55 +01:00
SoundRenderer : : getInstance ( ) . playFx (
selection . getFrontUnit ( ) - > getType ( ) - > getCommandSound ( ) ,
selection . getFrontUnit ( ) - > getCurrVector ( ) ,
gameCamera - > getPos ( ) ) ;
}
}
}
2011-01-08 22:53:05 +01:00
void Gui : : centerCameraOnSelection ( ) {
if ( selection . isEmpty ( ) = = false ) {
2010-03-12 08:42:55 +01:00
Vec3f refPos = selection . getRefPos ( ) ;
gameCamera - > centerXZ ( refPos . x , refPos . z ) ;
}
}
2011-01-08 22:53:05 +01:00
void Gui : : selectInterestingUnit ( InterestingUnitType iut ) {
2010-03-12 08:42:55 +01:00
const Faction * thisFaction = world - > getThisFaction ( ) ;
const Unit * previousUnit = NULL ;
bool previousFound = true ;
//start at the next harvester
if ( selection . getCount ( ) = = 1 ) {
const Unit * refUnit = selection . getFrontUnit ( ) ;
if ( refUnit - > isInteresting ( iut ) ) {
previousUnit = refUnit ;
previousFound = false ;
}
}
//clear selection
selection . clear ( ) ;
//search
for ( int i = 0 ; i < thisFaction - > getUnitCount ( ) ; + + i ) {
Unit * unit = thisFaction - > getUnit ( i ) ;
if ( previousFound ) {
if ( unit - > isInteresting ( iut ) ) {
selection . select ( unit ) ;
break ;
}
}
else {
if ( unit = = previousUnit ) {
previousFound = true ;
}
}
}
//search again if we have a previous
if ( selection . isEmpty ( ) & & previousUnit ! = NULL & & previousFound = = true ) {
for ( int i = 0 ; i < thisFaction - > getUnitCount ( ) ; + + i ) {
Unit * unit = thisFaction - > getUnit ( i ) ;
if ( unit - > isInteresting ( iut ) ) {
selection . select ( unit ) ;
break ;
}
}
}
}
void Gui : : clickCommonCommand ( CommandClass commandClass ) {
for ( int i = 0 ; i < Display : : downCellCount ; + + i ) {
const CommandType * ct = display . getCommandType ( i ) ;
if ( ( ct ! = NULL & & ct - > getClass ( ) = = commandClass ) | | display . getCommandClass ( i ) = = commandClass ) {
mouseDownDisplayUnitSkills ( i ) ;
break ;
}
}
2012-04-03 01:37:03 +02:00
computeDisplay ( ) ;
2010-03-12 08:42:55 +01:00
}
2011-01-10 07:43:47 +01:00
void Gui : : mouseDownDisplayUnitSkills ( int posDisplay ) {
if ( ! selection . isEmpty ( ) ) {
if ( posDisplay ! = cancelPos ) {
if ( posDisplay ! = meetingPointPos ) {
2010-03-12 08:42:55 +01:00
const Unit * unit = selection . getFrontUnit ( ) ;
//uniform selection
2011-01-10 07:43:47 +01:00
if ( selection . isUniform ( ) ) {
const CommandType * ct = display . getCommandType ( posDisplay ) ;
if ( ct ! = NULL & & unit - > getFaction ( ) - > reqsOk ( ct ) ) {
2010-03-12 08:42:55 +01:00
activeCommandType = display . getCommandType ( posDisplay ) ;
activeCommandClass = activeCommandType - > getClass ( ) ;
}
2011-01-10 07:43:47 +01:00
else {
2010-03-12 08:42:55 +01:00
posDisplay = invalidPos ;
activeCommandType = NULL ;
activeCommandClass = ccStop ;
return ;
}
}
//non uniform selection
2011-01-10 07:43:47 +01:00
else {
2010-03-12 08:42:55 +01:00
activeCommandType = NULL ;
activeCommandClass = display . getCommandClass ( posDisplay ) ;
}
//give orders depending on command type
if ( ! selection . isEmpty ( ) ) {
const CommandType * ct = selection . getUnit ( 0 ) - > getType ( ) - > getFirstCtOfClass ( activeCommandClass ) ;
if ( activeCommandType ! = NULL & & activeCommandType - > getClass ( ) = = ccBuild ) {
assert ( selection . isUniform ( ) ) ;
selectingBuilding = true ;
}
else if ( ct - > getClicks ( ) = = cOne ) {
invalidatePosObjWorld ( ) ;
giveOneClickOrders ( ) ;
}
else {
selectingPos = true ;
activePos = posDisplay ;
}
}
}
else {
activePos = posDisplay ;
selectingMeetingPoint = true ;
}
}
else {
commander - > tryCancelCommand ( & selection ) ;
}
}
}
void Gui : : mouseDownDisplayUnitBuild ( int posDisplay ) {
int factionIndex = world - > getThisFactionIndex ( ) ;
if ( posDisplay = = cancelPos ) {
resetState ( ) ;
}
else {
if ( activeCommandType ! = NULL & & activeCommandType - > getClass ( ) = = ccBuild ) {
const BuildCommandType * bct = static_cast < const BuildCommandType * > ( activeCommandType ) ;
const UnitType * ut = bct - > getBuilding ( posDisplay ) ;
if ( world - > getFaction ( factionIndex ) - > reqsOk ( ut ) ) {
choosenBuildingType = ut ;
assert ( choosenBuildingType ! = NULL ) ;
2010-03-25 13:15:10 +01:00
selectingPos = true ;
selectedBuildingFacing = CardinalDir : : NORTH ;
2010-03-12 08:42:55 +01:00
activePos = posDisplay ;
}
}
}
}
2011-05-01 22:19:41 +02:00
2011-05-06 10:44:50 +02:00
string Gui : : computeDefaultInfoString ( ) {
2011-09-01 01:10:43 +02:00
//Lang &lang= Lang::getInstance();
2011-05-01 22:19:41 +02:00
string result = " " ;
2011-05-06 10:44:50 +02:00
//printf("\n\n\n\n********* selection.isCommandable() [%d] selection.isUniform() [%d]\n\n",selection.isCommandable(),selection.isUniform());
2011-05-01 22:19:41 +02:00
2011-05-06 10:44:50 +02:00
if ( selection . isUniform ( ) ) {
if ( selection . isObserver ( ) | | selection . isCommandable ( ) ) {
// default is the description extension
result = selection . getFrontUnit ( ) - > getDescExtension ( ) ;
}
2011-05-01 22:19:41 +02:00
}
return result ;
}
2010-03-12 08:42:55 +01:00
void Gui : : computeInfoString ( int posDisplay ) {
Lang & lang = Lang : : getInstance ( ) ;
2011-05-01 22:19:41 +02:00
lastPosDisplay = posDisplay ;
display . setInfoText ( computeDefaultInfoString ( ) ) ;
2011-05-02 01:48:08 +02:00
if ( posDisplay ! = invalidPos & & selection . isCommandable ( ) ) {
2010-03-12 08:42:55 +01:00
if ( ! selectingBuilding ) {
if ( posDisplay = = cancelPos ) {
display . setInfoText ( lang . get ( " Cancel " ) ) ;
}
else if ( posDisplay = = meetingPointPos ) {
display . setInfoText ( lang . get ( " MeetingPoint " ) ) ;
}
else {
//uniform selection
if ( selection . isUniform ( ) ) {
const Unit * unit = selection . getFrontUnit ( ) ;
const CommandType * ct = display . getCommandType ( posDisplay ) ;
2011-05-01 22:19:41 +02:00
2010-03-12 08:42:55 +01:00
if ( ct ! = NULL ) {
if ( unit - > getFaction ( ) - > reqsOk ( ct ) ) {
display . setInfoText ( ct - > getDesc ( unit - > getTotalUpgrade ( ) ) ) ;
}
else {
if ( ct - > getClass ( ) = = ccUpgrade ) {
2011-01-24 00:57:46 +01:00
string text = " " ;
2010-03-12 08:42:55 +01:00
const UpgradeCommandType * uct = static_cast < const UpgradeCommandType * > ( ct ) ;
if ( unit - > getFaction ( ) - > getUpgradeManager ( ) - > isUpgrading ( uct - > getProducedUpgrade ( ) ) ) {
2011-01-24 00:57:46 +01:00
text = lang . get ( " Upgrading " ) + " \n \n " ;
2010-03-12 08:42:55 +01:00
}
else if ( unit - > getFaction ( ) - > getUpgradeManager ( ) - > isUpgraded ( uct - > getProducedUpgrade ( ) ) ) {
2011-01-24 00:57:46 +01:00
text = lang . get ( " AlreadyUpgraded " ) + " \n \n " ;
2010-03-12 08:42:55 +01:00
}
2011-01-24 00:57:46 +01:00
display . setInfoText ( text + ct - > getReqDesc ( ) ) ;
2010-03-12 08:42:55 +01:00
}
else {
display . setInfoText ( ct - > getReqDesc ( ) ) ;
}
}
}
}
//non uniform selection
else {
const UnitType * ut = selection . getFrontUnit ( ) - > getType ( ) ;
CommandClass cc = display . getCommandClass ( posDisplay ) ;
if ( cc ! = ccNull ) {
display . setInfoText ( lang . get ( " CommonCommand " ) + " : " + ut - > getFirstCtOfClass ( cc ) - > toString ( ) ) ;
}
}
}
}
else {
if ( posDisplay = = cancelPos ) {
display . setInfoText ( lang . get ( " Return " ) ) ;
}
else {
if ( activeCommandType ! = NULL & & activeCommandType - > getClass ( ) = = ccBuild ) {
const BuildCommandType * bct = static_cast < const BuildCommandType * > ( activeCommandType ) ;
display . setInfoText ( bct - > getBuilding ( posDisplay ) - > getReqDesc ( ) ) ;
}
}
}
}
}
2011-02-13 22:04:30 +01:00
void Gui : : computeDisplay ( ) {
2010-10-28 20:31:12 +02:00
//printf("Start ===> computeDisplay()\n");
2011-02-13 22:04:30 +01:00
Lang & lang = Lang : : getInstance ( ) ;
2010-03-12 08:42:55 +01:00
//init
display . clear ( ) ;
2011-02-13 22:04:30 +01:00
// ================ PART 1 ================
2012-04-04 02:30:16 +02:00
const Object * selectedResourceObject = getSelectedResourceObject ( ) ;
2012-11-15 00:24:25 +01:00
if ( selection . isEmpty ( ) & & selectedResourceObject ! = NULL & & selectedResourceObject - > getResource ( ) ! = NULL ) {
2011-02-13 22:04:30 +01:00
Resource * r = selectedResourceObject - > getResource ( ) ;
2012-05-02 22:46:47 +02:00
display . setTitle ( r - > getType ( ) - > getName ( true ) ) ;
2011-02-13 22:04:30 +01:00
display . setText ( lang . get ( " Amount " ) + " : " + intToStr ( r - > getAmount ( ) ) + " / " + intToStr ( r - > getType ( ) - > getDefResPerPatch ( ) ) ) ;
//display.setProgressBar(r->);
display . setUpImage ( 0 , r - > getType ( ) - > getImage ( ) ) ;
2010-03-12 08:42:55 +01:00
}
2011-10-21 18:04:21 +02:00
else {
2011-02-13 22:04:30 +01:00
//title, text and progress bar
if ( selection . getCount ( ) = = 1 ) {
display . setTitle ( selection . getFrontUnit ( ) - > getFullName ( ) ) ;
display . setText ( selection . getFrontUnit ( ) - > getDesc ( ) ) ;
display . setProgressBar ( selection . getFrontUnit ( ) - > getProductionPercent ( ) ) ;
}
2010-03-12 08:42:55 +01:00
2011-02-13 22:04:30 +01:00
//portraits
for ( int i = 0 ; i < selection . getCount ( ) ; + + i ) {
display . setUpImage ( i , selection . getUnit ( i ) - > getType ( ) - > getImage ( ) ) ;
}
2010-03-12 08:42:55 +01:00
2011-02-13 22:04:30 +01:00
// ================ PART 2 ================
2010-03-12 08:42:55 +01:00
2011-02-13 22:04:30 +01:00
if ( selectingPos | | selectingMeetingPoint ) {
//printf("selectingPos || selectingMeetingPoint\n");
display . setDownSelectedPos ( activePos ) ;
}
2010-10-28 20:31:12 +02:00
2011-10-21 18:04:21 +02:00
//printf("computeDisplay selection.isCommandable() = %d\n",selection.isCommandable());
if ( selection . isCommandable ( ) ) {
2011-02-13 22:04:30 +01:00
//printf("selection.isComandable()\n");
2010-03-12 08:42:55 +01:00
2011-02-13 22:04:30 +01:00
if ( selectingBuilding = = false ) {
2010-10-28 20:31:12 +02:00
2011-02-13 22:04:30 +01:00
//cancel button
const Unit * u = selection . getFrontUnit ( ) ;
const UnitType * ut = u - > getType ( ) ;
2011-10-21 18:04:21 +02:00
if ( selection . isCancelable ( ) ) {
2011-02-13 22:04:30 +01:00
//printf("selection.isCancelable() commandcount = %d\n",selection.getUnit(0)->getCommandSize());
if ( selection . getUnit ( 0 ) - > getCommandSize ( ) > 0 ) {
//printf("Current Command [%s]\n",selection.getUnit(0)->getCurrCommand()->toString().c_str());
}
2010-03-12 08:42:55 +01:00
2011-02-13 22:04:30 +01:00
display . setDownImage ( cancelPos , ut - > getCancelImage ( ) ) ;
display . setDownLighted ( cancelPos , true ) ;
}
2010-10-28 20:31:12 +02:00
2011-02-13 22:04:30 +01:00
//meeting point
if ( selection . isMeetable ( ) ) {
//printf("selection.isMeetable()\n");
2010-03-12 08:42:55 +01:00
2011-02-13 22:04:30 +01:00
display . setDownImage ( meetingPointPos , ut - > getMeetingPointImage ( ) ) ;
display . setDownLighted ( meetingPointPos , true ) ;
}
2010-10-28 20:31:12 +02:00
2011-10-21 18:04:21 +02:00
//printf("computeDisplay selection.isUniform() = %d\n",selection.isUniform());
if ( selection . isUniform ( ) ) {
2011-02-13 22:04:30 +01:00
//printf("selection.isUniform()\n");
//uniform selection
if ( u - > isBuilt ( ) ) {
//printf("u->isBuilt()\n");
int morphPos = 8 ;
for ( int i = 0 ; i < ut - > getCommandTypeCount ( ) ; + + i ) {
int displayPos = i ;
const CommandType * ct = ut - > getCommandType ( i ) ;
2011-10-21 18:04:21 +02:00
if ( ct - > getClass ( ) = = ccMorph ) {
2011-02-13 22:04:30 +01:00
displayPos = morphPos + + ;
}
2011-10-21 18:04:21 +02:00
//printf("computeDisplay i = %d displayPos = %d morphPos = %d ct->getClass() = %d [%s]\n",i,displayPos,morphPos,ct->getClass(),ct->getName().c_str());
2011-02-13 22:04:30 +01:00
display . setDownImage ( displayPos , ct - > getImage ( ) ) ;
display . setCommandType ( displayPos , ct ) ;
2011-10-21 18:04:21 +02:00
display . setCommandClass ( displayPos , ct - > getClass ( ) ) ;
2011-02-13 22:04:30 +01:00
display . setDownLighted ( displayPos , u - > getFaction ( ) - > reqsOk ( ct ) ) ;
2010-03-12 08:42:55 +01:00
}
}
}
2011-02-13 22:04:30 +01:00
else {
//printf("selection.isUniform() == FALSE\n");
//non uniform selection
int lastCommand = 0 ;
for ( int i = 0 ; i < ccCount ; + + i ) {
CommandClass cc = static_cast < CommandClass > ( i ) ;
2011-10-21 18:04:21 +02:00
//printf("computeDisplay i = %d cc = %d isshared = %d lastCommand = %d\n",i,cc,isSharedCommandClass(cc),lastCommand);
2011-02-13 22:04:30 +01:00
if ( isSharedCommandClass ( cc ) & & cc ! = ccBuild ) {
display . setDownLighted ( lastCommand , true ) ;
display . setDownImage ( lastCommand , ut - > getFirstCtOfClass ( cc ) - > getImage ( ) ) ;
display . setCommandClass ( lastCommand , cc ) ;
lastCommand + + ;
}
2010-03-12 08:42:55 +01:00
}
}
}
2011-02-13 22:04:30 +01:00
else {
2010-03-12 08:42:55 +01:00
2011-02-13 22:04:30 +01:00
//selecting building
const Unit * unit = selection . getFrontUnit ( ) ;
if ( activeCommandType ! = NULL & & activeCommandType - > getClass ( ) = = ccBuild ) {
const BuildCommandType * bct = static_cast < const BuildCommandType * > ( activeCommandType ) ;
for ( int i = 0 ; i < bct - > getBuildingCount ( ) ; + + i ) {
display . setDownImage ( i , bct - > getBuilding ( i ) - > getImage ( ) ) ;
display . setDownLighted ( i , unit - > getFaction ( ) - > reqsOk ( bct - > getBuilding ( i ) ) ) ;
}
display . setDownImage ( cancelPos , selection . getFrontUnit ( ) - > getType ( ) - > getCancelImage ( ) ) ;
display . setDownLighted ( cancelPos , true ) ;
2010-03-12 08:42:55 +01:00
}
}
}
2011-02-13 22:04:30 +01:00
}
2011-05-01 22:19:41 +02:00
// refresh other things
2011-05-02 02:34:04 +02:00
if ( ! isSelecting ( ) & & ! isSelectingPos ( ) ) {
2011-05-02 03:22:33 +02:00
if ( ! isSelectingPos ( ) & & lastPosDisplay = = invalidPos ) {
2011-05-02 02:34:04 +02:00
computeInfoString ( lastPosDisplay ) ;
}
2011-05-02 01:48:08 +02:00
}
2011-05-01 22:19:41 +02:00
2010-03-12 08:42:55 +01:00
}
int Gui : : computePosDisplay ( int x , int y ) {
int posDisplay = display . computeDownIndex ( x , y ) ;
2011-10-21 18:04:21 +02:00
//printf("computePosDisplay x = %d y = %d posDisplay = %d Display::downCellCount = %d cc = %d ct = %p\n",x,y,posDisplay,Display::downCellCount,display.getCommandClass(posDisplay),display.getCommandType(posDisplay));
if ( posDisplay < 0 | | posDisplay > = Display : : downCellCount ) {
2010-03-12 08:42:55 +01:00
posDisplay = invalidPos ;
2011-10-21 18:04:21 +02:00
//printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-12 08:42:55 +01:00
}
2011-10-21 18:04:21 +02:00
else if ( selection . isCommandable ( ) ) {
if ( posDisplay ! = cancelPos ) {
if ( posDisplay ! = meetingPointPos ) {
if ( selectingBuilding = = false ) {
2010-03-12 08:42:55 +01:00
//standard selection
2011-10-21 18:04:21 +02:00
if ( display . getCommandClass ( posDisplay ) = = ccNull & & display . getCommandType ( posDisplay ) = = NULL ) {
2010-03-12 08:42:55 +01:00
posDisplay = invalidPos ;
2011-10-21 18:04:21 +02:00
//printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-12 08:42:55 +01:00
}
}
2011-10-21 18:04:21 +02:00
else {
2010-03-12 08:42:55 +01:00
//building selection
2011-10-21 18:04:21 +02:00
if ( activeCommandType ! = NULL & & activeCommandType - > getClass ( ) = = ccBuild ) {
2010-03-12 08:42:55 +01:00
const BuildCommandType * bct = static_cast < const BuildCommandType * > ( activeCommandType ) ;
2011-10-21 18:04:21 +02:00
if ( posDisplay > = bct - > getBuildingCount ( ) ) {
2010-03-12 08:42:55 +01:00
posDisplay = invalidPos ;
2011-10-21 18:04:21 +02:00
//printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-12 08:42:55 +01:00
}
}
}
}
else {
//check meeting point
2011-10-21 18:04:21 +02:00
if ( ! selection . isMeetable ( ) ) {
2010-03-12 08:42:55 +01:00
posDisplay = invalidPos ;
2011-10-21 18:04:21 +02:00
//printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-12 08:42:55 +01:00
}
}
}
2011-10-21 18:04:21 +02:00
else {
2010-03-12 08:42:55 +01:00
//check cancel button
2011-10-21 18:04:21 +02:00
if ( selection . isCancelable ( ) = = false ) {
2010-03-12 08:42:55 +01:00
posDisplay = invalidPos ;
2011-10-21 18:04:21 +02:00
//printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-12 08:42:55 +01:00
}
}
}
2011-10-21 18:04:21 +02:00
else {
2010-03-12 08:42:55 +01:00
posDisplay = invalidPos ;
2011-10-21 18:04:21 +02:00
//printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-12 08:42:55 +01:00
}
2011-10-21 18:04:21 +02:00
//printf("computePosDisplay returning = %d\n",posDisplay);
2010-03-12 08:42:55 +01:00
return posDisplay ;
}
2012-11-10 20:39:55 +01:00
void Gui : : addOrdersResultToConsole ( CommandClass cc , std : : pair < CommandResult , string > result ) {
2010-03-12 08:42:55 +01:00
2012-11-10 20:39:55 +01:00
switch ( result . first ) {
2010-03-12 08:42:55 +01:00
case crSuccess :
break ;
case crFailReqs :
switch ( cc ) {
case ccBuild :
2012-11-10 20:39:55 +01:00
console - > addStdMessage ( " BuildingNoReqs " , result . second ) ;
2010-03-12 08:42:55 +01:00
break ;
case ccProduce :
2012-11-10 20:39:55 +01:00
console - > addStdMessage ( " UnitNoReqs " , result . second ) ;
2010-03-12 08:42:55 +01:00
break ;
2011-04-05 23:46:02 +02:00
case ccMorph :
2012-11-10 20:39:55 +01:00
console - > addStdMessage ( " MorphNoReqs " , result . second ) ;
2011-04-05 23:46:02 +02:00
break ;
2010-03-12 08:42:55 +01:00
case ccUpgrade :
2012-11-10 20:39:55 +01:00
console - > addStdMessage ( " UpgradeNoReqs " , result . second ) ;
2010-03-12 08:42:55 +01:00
break ;
default :
break ;
}
break ;
case crFailRes :
switch ( cc ) {
case ccBuild :
2012-11-10 20:39:55 +01:00
console - > addStdMessage ( " BuildingNoRes " , result . second ) ;
2010-03-12 08:42:55 +01:00
break ;
case ccProduce :
2012-11-10 20:39:55 +01:00
console - > addStdMessage ( " UnitNoRes " , result . second ) ;
2010-03-12 08:42:55 +01:00
break ;
2011-04-05 23:46:02 +02:00
case ccMorph :
2012-11-10 20:39:55 +01:00
console - > addStdMessage ( " MorphNoRes " , result . second ) ;
2011-04-05 23:46:02 +02:00
break ;
2010-03-12 08:42:55 +01:00
case ccUpgrade :
2012-11-10 20:39:55 +01:00
console - > addStdMessage ( " UpgradeNoRes " , result . second ) ;
2010-03-12 08:42:55 +01:00
break ;
default :
break ;
}
break ;
case crFailUndefined :
2012-11-10 20:39:55 +01:00
console - > addStdMessage ( " InvalidOrder " , result . second ) ;
2010-03-12 08:42:55 +01:00
break ;
case crSomeFailed :
2012-11-10 20:39:55 +01:00
console - > addStdMessage ( " SomeOrdersFailed " , result . second ) ;
2010-03-12 08:42:55 +01:00
break ;
}
}
bool Gui : : isSharedCommandClass ( CommandClass commandClass ) {
for ( int i = 0 ; i < selection . getCount ( ) ; + + i ) {
const Unit * unit = selection . getUnit ( i ) ;
const CommandType * ct = unit - > getType ( ) - > getFirstCtOfClass ( commandClass ) ;
if ( ct = = NULL | | ! unit - > getFaction ( ) - > reqsOk ( ct ) )
return false ;
}
return true ;
}
2010-09-10 14:15:21 +02:00
void Gui : : computeSelected ( bool doubleClick , bool force ) {
2010-03-12 08:42:55 +01:00
Selection : : UnitContainer units ;
2010-09-10 14:15:21 +02:00
if ( force | | ( lastQuadCalcFrame + selectionCalculationFrameSkip < game - > getTotalRenderFps ( ) ) ) {
lastQuadCalcFrame = game - > getTotalRenderFps ( ) ;
2012-04-04 02:30:16 +02:00
const Object * selectedResourceObject = NULL ;
selectedResourceObjectPos = Vec2i ( - 1 , - 1 ) ;
2010-09-10 14:15:21 +02:00
if ( selectionQuad . isEnabled ( ) & & selectionQuad . getPosUp ( ) . dist ( selectionQuad . getPosDown ( ) ) < minQuadSize ) {
2011-02-13 22:04:30 +01:00
Renderer : : getInstance ( ) . computeSelected ( units , selectedResourceObject , true , selectionQuad . getPosDown ( ) , selectionQuad . getPosDown ( ) ) ;
2012-04-04 02:30:16 +02:00
if ( selectedResourceObject ! = NULL ) {
selectedResourceObjectPos = Map : : toSurfCoords ( selectedResourceObject - > getMapPos ( ) ) ;
}
2010-09-10 14:15:21 +02:00
}
else {
2011-02-15 01:24:31 +01:00
Renderer : : getInstance ( ) . computeSelected ( units , selectedResourceObject , false , selectionQuad . getPosDown ( ) , selectionQuad . getPosUp ( ) ) ;
2010-09-10 14:15:21 +02:00
}
selectingBuilding = false ;
activeCommandType = NULL ;
2011-01-08 22:53:05 +01:00
2010-09-10 14:15:21 +02:00
//select all units of the same type if double click
2011-09-01 01:10:43 +02:00
if ( doubleClick & & units . empty ( ) = = false ) {
2011-03-08 21:19:19 +01:00
const Unit * refUnit = getRelevantObjectFromSelection ( & units ) ;
2010-09-10 14:15:21 +02:00
int factionIndex = refUnit - > getFactionIndex ( ) ;
for ( int i = 0 ; i < world - > getFaction ( factionIndex ) - > getUnitCount ( ) ; + + i ) {
Unit * unit = world - > getFaction ( factionIndex ) - > getUnit ( i ) ;
if ( unit - > getPos ( ) . dist ( refUnit - > getPos ( ) ) < doubleClickSelectionRadius & &
unit - > getType ( ) = = refUnit - > getType ( ) )
{
units . push_back ( unit ) ;
}
2010-03-12 08:42:55 +01:00
}
}
2011-01-08 22:53:05 +01:00
2010-09-10 14:15:21 +02:00
bool shiftDown = isKeyDown ( vkShift ) ;
bool controlDown = isKeyDown ( vkControl ) ;
2011-01-08 22:53:05 +01:00
2010-09-10 14:15:21 +02:00
if ( ! shiftDown & & ! controlDown ) {
2011-04-11 02:11:52 +02:00
//if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] about to call selection.clear()\n",__FILE__,__FUNCTION__,__LINE__);
2010-09-10 14:15:21 +02:00
selection . clear ( ) ;
}
2011-01-08 22:53:05 +01:00
2010-09-10 14:15:21 +02:00
if ( ! controlDown ) {
2011-04-11 02:11:52 +02:00
//if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] about to call selection.select(units)\n",__FILE__,__FUNCTION__,__LINE__);
2010-09-10 14:15:21 +02:00
selection . select ( units ) ;
2011-02-13 22:04:30 +01:00
if ( ! selection . isEmpty ( ) ) {
selectedResourceObject = NULL ;
}
2010-09-10 14:15:21 +02:00
}
else {
2011-04-11 02:11:52 +02:00
//if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] selection.unSelect(units)\n",__FILE__,__FUNCTION__,__LINE__);
2010-09-10 14:15:21 +02:00
selection . unSelect ( units ) ;
}
2010-03-12 08:42:55 +01:00
}
}
2011-03-29 23:44:36 +02:00
bool Gui : : computeTarget ( const Vec2i & screenPos , Vec2i & targetPos , const Unit * & targetUnit ) {
2010-03-12 08:42:55 +01:00
Selection : : UnitContainer uc ;
Renderer & renderer = Renderer : : getInstance ( ) ;
2011-03-29 23:44:36 +02:00
const Object * obj = NULL ;
2011-02-13 15:35:17 +01:00
renderer . computeSelected ( uc , obj , true , screenPos , screenPos ) ;
2010-03-12 08:42:55 +01:00
validPosObjWorld = false ;
2011-03-29 23:44:36 +02:00
if ( uc . empty ( ) = = false ) {
targetUnit = getRelevantObjectFromSelection ( & uc ) ;
2011-03-08 21:19:19 +01:00
targetPos = targetUnit - > getPos ( ) ;
2012-04-05 00:57:32 +02:00
highlightedUnitId = targetUnit - > getId ( ) ;
getHighlightedUnit ( ) - > resetHighlight ( ) ;
2010-03-12 08:42:55 +01:00
return true ;
}
2011-03-29 23:44:36 +02:00
else if ( obj ! = NULL ) {
2011-02-13 15:35:17 +01:00
targetUnit = NULL ;
2012-04-04 02:30:16 +02:00
highlightedResourceObjectPos = Map : : toSurfCoords ( obj - > getMapPos ( ) ) ;
getHighlightedResourceObject ( ) - > resetHighlight ( ) ;
2011-03-29 23:44:36 +02:00
// get real click pos
renderer . computePosition ( screenPos , targetPos ) ;
2011-03-30 01:19:19 +02:00
//validPosObjWorld= true;
2011-03-29 23:44:36 +02:00
//posObjWorld = targetPos;
int tx = targetPos . x ;
int ty = targetPos . y ;
int ox = obj - > getMapPos ( ) . x ;
int oy = obj - > getMapPos ( ) . y ;
Resource * clickedRecource = world - > getMap ( ) - > getSurfaceCell ( Map : : toSurfCoords ( obj - > getMapPos ( ) ) ) - > getResource ( ) ;
// lets see if the click had the same Resource
if ( clickedRecource = = world - > getMap ( ) - > getSurfaceCell ( Map : : toSurfCoords ( targetPos ) ) - > getResource ( ) ) {
// same ressource is meant, so use the user selected position
return true ;
}
else { // calculate a valid resource position which is as near as possible to the selected position
Vec2i testIt = Vec2i ( obj - > getMapPos ( ) ) ;
///////////////
// test both //
///////////////
if ( ty < oy ) {
testIt . y - - ;
}
else if ( ty > oy ) {
testIt . y + + ;
}
if ( tx < ox ) {
testIt . x - - ;
}
else if ( tx > ox ) {
testIt . x + + ;
}
if ( clickedRecource = = world - > getMap ( ) - > getSurfaceCell ( Map : : toSurfCoords ( testIt ) ) - > getResource ( ) ) {
// same ressource is meant, so use this position
targetPos = testIt ;
//posObjWorld= targetPos;
return true ;
}
else {
testIt = Vec2i ( obj - > getMapPos ( ) ) ;
}
/////////////////
// test y-only //
/////////////////
if ( ty < oy ) {
testIt . y - - ;
}
else if ( ty > oy ) {
testIt . y + + ;
}
if ( clickedRecource = = world - > getMap ( ) - > getSurfaceCell ( Map : : toSurfCoords ( testIt ) ) - > getResource ( ) ) {
// same ressource is meant, so use this position
targetPos = testIt ;
//posObjWorld= targetPos;
return true ;
}
else {
testIt = Vec2i ( obj - > getMapPos ( ) ) ;
}
/////////////////
// test x-only //
/////////////////
if ( tx < ox ) {
testIt . x - - ;
}
else if ( tx > ox ) {
testIt . x + + ;
}
if ( clickedRecource = = world - > getMap ( ) - > getSurfaceCell ( Map : : toSurfCoords ( testIt ) ) - > getResource ( ) ) {
// same ressource is meant, so use this position
targetPos = testIt ;
//posObjWorld= targetPos;
return true ;
}
}
// give up and use the object position;
2011-02-13 15:35:17 +01:00
targetPos = obj - > getMapPos ( ) ;
2011-03-29 23:44:36 +02:00
posObjWorld = targetPos ;
2011-02-13 15:35:17 +01:00
return true ;
2011-03-29 23:44:36 +02:00
2011-02-13 15:35:17 +01:00
}
2011-03-29 23:44:36 +02:00
else {
2010-03-12 08:42:55 +01:00
targetUnit = NULL ;
2011-03-29 23:44:36 +02:00
if ( renderer . computePosition ( screenPos , targetPos ) ) {
2010-03-12 08:42:55 +01:00
validPosObjWorld = true ;
posObjWorld = targetPos ;
2012-04-04 02:30:16 +02:00
if ( world - > getMap ( ) - > getSurfaceCell ( Map : : toSurfCoords ( targetPos ) ) - > getResource ( ) ! = NULL )
{
highlightedResourceObjectPos = Map : : toSurfCoords ( targetPos ) ;
getHighlightedResourceObject ( ) - > resetHighlight ( ) ;
}
2010-03-12 08:42:55 +01:00
return true ;
}
2011-03-29 23:44:36 +02:00
else {
2010-03-12 08:42:55 +01:00
return false ;
}
}
}
2011-03-08 21:19:19 +01:00
Unit * Gui : : getRelevantObjectFromSelection ( Selection : : UnitContainer * uc ) {
Unit * resultUnit = NULL ;
for ( int i = 0 ; i < uc - > size ( ) ; + + i ) {
resultUnit = uc - > at ( i ) ;
if ( resultUnit - > getType ( ) - > hasSkillClass ( scMove ) ) { // moving units are more relevant than non moving ones
break ;
}
}
return resultUnit ;
}
2012-03-10 04:27:25 +01:00
void Gui : : saveGame ( XmlNode * rootNode ) const {
std : : map < string , string > mapTagReplacements ;
XmlNode * guiNode = rootNode - > addChild ( " Gui " ) ;
//External objects
// RandomGen random;
guiNode - > addAttribute ( " random " , intToStr ( random . getLastNumber ( ) ) , mapTagReplacements ) ;
// const Commander *commander;
// const World *world;
// const Game *game;
// GameCamera *gameCamera;
// Console *console;
//
// //Positions
// Vec2i posObjWorld; //world coords
guiNode - > addAttribute ( " posObjWorld " , posObjWorld . getString ( ) , mapTagReplacements ) ;
// bool validPosObjWorld;
guiNode - > addAttribute ( " validPosObjWorld " , intToStr ( validPosObjWorld ) , mapTagReplacements ) ;
// //display
// const UnitType *choosenBuildingType;
if ( choosenBuildingType ! = NULL ) {
2012-03-15 08:09:36 +01:00
const Faction * thisFaction = world - > getThisFaction ( ) ;
2012-03-10 04:27:25 +01:00
guiNode - > addAttribute ( " choosenBuildingType " , choosenBuildingType - > getName ( ) , mapTagReplacements ) ;
2012-03-15 08:09:36 +01:00
guiNode - > addAttribute ( " choosenBuildingTypeFactionIndex " , intToStr ( thisFaction - > getIndex ( ) ) , mapTagReplacements ) ;
2012-03-10 04:27:25 +01:00
}
// const CommandType *activeCommandType;
if ( activeCommandType ! = NULL ) {
guiNode - > addAttribute ( " activeCommandType " , activeCommandType - > getName ( ) , mapTagReplacements ) ;
}
// CommandClass activeCommandClass;
guiNode - > addAttribute ( " activeCommandClass " , intToStr ( activeCommandClass ) , mapTagReplacements ) ;
// int activePos;
guiNode - > addAttribute ( " activePos " , intToStr ( activePos ) , mapTagReplacements ) ;
// int lastPosDisplay;
guiNode - > addAttribute ( " lastPosDisplay " , intToStr ( lastPosDisplay ) , mapTagReplacements ) ;
// //composite
// Display display;
display . saveGame ( guiNode ) ;
// Mouse3d mouse3d;
// Selection selection;
selection . saveGame ( guiNode ) ;
// SelectionQuad selectionQuad;
// int lastQuadCalcFrame;
guiNode - > addAttribute ( " lastQuadCalcFrame " , intToStr ( lastQuadCalcFrame ) , mapTagReplacements ) ;
// int selectionCalculationFrameSkip;
guiNode - > addAttribute ( " selectionCalculationFrameSkip " , intToStr ( selectionCalculationFrameSkip ) , mapTagReplacements ) ;
// int minQuadSize;
guiNode - > addAttribute ( " minQuadSize " , intToStr ( minQuadSize ) , mapTagReplacements ) ;
// Chrono lastGroupRecallTime;
//guiNode->addAttribute("lastGroupRecallTime",intToStr(lastGroupRecallTime.getMillis()), mapTagReplacements);
// int lastGroupRecall;
guiNode - > addAttribute ( " lastGroupRecall " , intToStr ( lastGroupRecall ) , mapTagReplacements ) ;
// //states
// bool selectingBuilding;
guiNode - > addAttribute ( " selectingBuilding " , intToStr ( selectingBuilding ) , mapTagReplacements ) ;
// bool selectingPos;
guiNode - > addAttribute ( " selectingPos " , intToStr ( selectingPos ) , mapTagReplacements ) ;
// bool selectingMeetingPoint;
guiNode - > addAttribute ( " selectingMeetingPoint " , intToStr ( selectingMeetingPoint ) , mapTagReplacements ) ;
// CardinalDir selectedBuildingFacing;
guiNode - > addAttribute ( " selectedBuildingFacing " , intToStr ( selectedBuildingFacing ) , mapTagReplacements ) ;
// const Object *selectedResourceObject;
//
// Texture2D* hudTexture;
2012-03-15 08:09:36 +01:00
}
void Gui : : loadGame ( const XmlNode * rootNode , World * world ) {
const XmlNode * guiNode = rootNode - > getChild ( " Gui " ) ;
2012-03-10 04:27:25 +01:00
2012-03-15 08:09:36 +01:00
//External objects
// RandomGen random;
random . setLastNumber ( guiNode - > getAttribute ( " random " ) - > getIntValue ( ) ) ;
// const Commander *commander;
// const World *world;
// const Game *game;
// GameCamera *gameCamera;
// Console *console;
//
// //Positions
// Vec2i posObjWorld; //world coords
//guiNode->addAttribute("posObjWorld",posObjWorld.getString(), mapTagReplacements);
posObjWorld = Vec2i : : strToVec2 ( guiNode - > getAttribute ( " posObjWorld " ) - > getValue ( ) ) ;
// bool validPosObjWorld;
2012-04-16 22:15:57 +02:00
validPosObjWorld = guiNode - > getAttribute ( " validPosObjWorld " ) - > getIntValue ( ) ! = 0 ;
2012-03-15 08:09:36 +01:00
// //display
// const UnitType *choosenBuildingType;
// if(choosenBuildingType != NULL) {
// guiNode->addAttribute("choosenBuildingType",choosenBuildingType->getName(), mapTagReplacements);
// }
if ( guiNode - > hasAttribute ( " choosenBuildingType " ) = = true ) {
string unitType = guiNode - > getAttribute ( " choosenBuildingType " ) - > getValue ( ) ;
int factionIndex = guiNode - > getAttribute ( " choosenBuildingTypeFactionIndex " ) - > getIntValue ( ) ;
choosenBuildingType = world - > getFaction ( factionIndex ) - > getType ( ) - > getUnitType ( unitType ) ;
}
// const CommandType *activeCommandType;
//if(activeCommandType != NULL) {
// guiNode->addAttribute("activeCommandType",activeCommandType->getName(), mapTagReplacements);
//}
// CommandClass activeCommandClass;
//guiNode->addAttribute("activeCommandClass",intToStr(activeCommandClass), mapTagReplacements);
// int activePos;
activePos = guiNode - > getAttribute ( " activePos " ) - > getIntValue ( ) ;
// int lastPosDisplay;
lastPosDisplay = guiNode - > getAttribute ( " lastPosDisplay " ) - > getIntValue ( ) ;
// //composite
// Display display;
display . loadGame ( guiNode ) ;
// Mouse3d mouse3d;
// Selection selection;
selection . loadGame ( guiNode , world ) ;
// SelectionQuad selectionQuad;
// int lastQuadCalcFrame;
2012-09-14 00:40:14 +02:00
// don't load this! lastQuadCalcFrame = guiNode->getAttribute("lastQuadCalcFrame")->getIntValue();
lastQuadCalcFrame = game - > getTotalRenderFps ( ) ;
2012-03-15 08:09:36 +01:00
// int selectionCalculationFrameSkip;
selectionCalculationFrameSkip = guiNode - > getAttribute ( " selectionCalculationFrameSkip " ) - > getIntValue ( ) ;
// int minQuadSize;
minQuadSize = guiNode - > getAttribute ( " minQuadSize " ) - > getIntValue ( ) ;
// Chrono lastGroupRecallTime;
//guiNode->addAttribute("lastGroupRecallTime",intToStr(lastGroupRecallTime.getMillis()), mapTagReplacements);
// int lastGroupRecall;
lastGroupRecall = guiNode - > getAttribute ( " lastGroupRecall " ) - > getIntValue ( ) ;
// //states
// bool selectingBuilding;
//selectingBuilding = guiNode->getAttribute("selectingBuilding")->getIntValue();
// bool selectingPos;
//guiNode->addAttribute("selectingPos",intToStr(selectingPos), mapTagReplacements);
// bool selectingMeetingPoint;
//guiNode->addAttribute("selectingMeetingPoint",intToStr(selectingMeetingPoint), mapTagReplacements);
// CardinalDir selectedBuildingFacing;
//guiNode->addAttribute("selectedBuildingFacing",intToStr(selectedBuildingFacing), mapTagReplacements);
// const Object *selectedResourceObject;
//
// Texture2D* hudTexture;
2012-03-10 04:27:25 +01:00
}
2012-03-15 08:09:36 +01:00
2010-03-12 08:42:55 +01:00
} } //end namespace