/**
 * Removes the given number of units to produce from the project if possible.
 * @param change How much we want to subtract.
 */
void ManufactureInfoState::lessUnit(int change)
{
	if (change <= 0) return;
	int units = _production->getAmountTotal();
	change = std::min(units-(_production->getAmountProduced()+1), change);
	_production->setAmountTotal(units-change);
	setAssignedEngineer();
}
/**
 * Build one less unit(if possible)
 */
void ManufactureInfoState::onLessUnit()
{
    int less = _production->getAmountRemaining ();
    if(less > (_production->getAmountProduced () + 1))
    {
        _production->setAmountRemaining (--less);
        setAssignedEngineer();
    }
}
/**
 * Build one less unit(if possible)
 */
void ProductionState::onLessUnit()
{
	int less = _production->getNumberOfItemTodo ();
	if(less > (_production->getNumberOfItemDone () + 1))
	{
		_production->setNumberOfItemTodo (--less);
		setAssignedEngineer();	
	}
}
/**
 * Remove one engineer(if possible)
 */
void ProductionState::onLessEngineer()
{
	int assigned = _production->getAssignedEngineers();
	if(assigned > 0)
	{
		_production->setAssignedEngineers(--assigned);
		setAssignedEngineer();
	}
}
/**
 * Removes the given number of units to produce from the project if possible.
 * @param change How much we want to subtract.
 */
void ManufactureInfoState::lessUnit(int change)
{
    if (0 >= change) return;
    if (Options::getBool("allowAutoSellProduction") && _production->getAmountTotal() == std::numeric_limits<int>::max())
        _production->setAmountTotal(std::max(_production->getAmountProduced()+1,999));
    int units = _production->getAmountTotal();
    change = std::min(units-(_production->getAmountProduced()+1), change);
    _production->setAmountTotal(units-change);
    setAssignedEngineer();
}
/**
 * Remove one engineer(if possible)
 */
void ManufactureInfoState::onLessEngineer()
{
    int assigned = _production->getAssignedEngineers();
    if(assigned > 0)
    {
        _production->setAssignedEngineers(--assigned);
        _base->setEngineers(_base->getEngineers()+1);
        setAssignedEngineer();
    }
}
/**
 * Assign one more engineer(if possible)
 */
void ProductionState::onMoreEngineer()
{
	int assigned = _production->getAssignedEngineers();
	int availableEngineer = _base->getFreeEngineers();
	int availableWorkSpace = _base->getAvailableWorkshops() - _base->getUsedWorkshops();
	if (availableEngineer > 0 && availableWorkSpace > 0)
	{
		_production->setAssignedEngineers(++assigned);
		setAssignedEngineer();
	}
}
/**
 * Removes the given number of engineers from the project if possible.
 * @param change How much we want to subtract.
 */
void ManufactureInfoState::lessEngineer(int change)
{
	if (change <= 0) return;
	int assigned = _production->getAssignedEngineers();
	if (assigned > 0)
	{
		change = std::min(assigned, change);
		_production->setAssignedEngineers(assigned-change);
		_base->setEngineers(_base->getEngineers()+change);
		setAssignedEngineer();
	}
}
/**
 * Assign one more engineer(if possible)
 */
void ManufactureInfoState::onMoreEngineer()
{
    int assigned = _production->getAssignedEngineers();
    int availableEngineer = _base->getAvailableEngineers();
    int availableWorkSpace = _base->getFreeWorkshops();
    if (availableEngineer > 0 && availableWorkSpace > 0)
    {
        _production->setAssignedEngineers(++assigned);
        _base->setEngineers(_base->getEngineers()-1);
        setAssignedEngineer();
    }
}
/**
 * Adds given number of engineers to the project if possible.
 * @param change How much we want to add.
 */
void ManufactureInfoState::moreEngineer(int change)
{
	if (change <= 0) return;
	int availableEngineer = _base->getAvailableEngineers();
	int availableWorkSpace = _base->getFreeWorkshops();
	if (availableEngineer > 0 && availableWorkSpace > 0)
	{
		change = std::min(std::min(availableEngineer, availableWorkSpace), change);
		_production->setAssignedEngineers(_production->getAssignedEngineers()+change);
		_base->setEngineers(_base->getEngineers()-change);
		setAssignedEngineer();
	}
}
/**
 * Build one more unit
 */
void ManufactureInfoState::onMoreUnit()
{
    int more = _production->getAmountRemaining ();
    if (_production->getRules()->getCategory() == "STR_CRAFT" && _base->getAvailableHangars() - _base->getUsedHangars() == 0)
    {
        _timerMoreUnit->stop();
        _game->pushState(new ErrorMessageState(_game, "STR_NO_FREE_HANGARS_FOR_CRAFT_PRODUCTION", Palette::blockOffset(15)+1, "BACK17.SCR", 6));
    }
    else
    {
        _production->setAmountRemaining (++more);
        setAssignedEngineer();
    }
}
/**
 * Decreases the units to produce.
 * @param action A pointer to an Action.
 */
void ManufactureInfoState::lessUnitClick(Action *action)
{
	if (action->getDetails()->button.button == SDL_BUTTON_RIGHT
	||  action->getDetails()->button.button == SDL_BUTTON_LEFT)
	{
		_production->setInfiniteAmount(false);
		if (action->getDetails()->button.button == SDL_BUTTON_RIGHT
		|| _production->getAmountTotal() <= _production->getAmountProduced())
		{ // So the produced item number is increased over the planned, OR it was simply a right-click
			_production->setAmountTotal(_production->getAmountProduced()+1);
			setAssignedEngineer();
		}
		if (action->getDetails()->button.button == SDL_BUTTON_LEFT) lessUnit(1);
	}
}
/**
 * Adds given number of units to produce to the project if possible.
 * @param change How much we want to add.
 */
void ManufactureInfoState::moreUnit(int change)
{
    if (0 >= change) return;
    if (_production->getRules()->getCategory() == "STR_CRAFT" && _base->getAvailableHangars() - _base->getUsedHangars() == 0)
    {
        _timerMoreUnit->stop();
        _game->pushState(new ErrorMessageState(_game, "STR_NO_FREE_HANGARS_FOR_CRAFT_PRODUCTION", Palette::blockOffset(15)+1, "BACK17.SCR", 6));
    }
    else
    {
        int units = _production->getAmountTotal();
        change = std::min(std::numeric_limits<int>::max()-units, change);
        if (_production->getRules()->getCategory() == "STR_CRAFT")
            change = std::min(_base->getAvailableHangars() - _base->getUsedHangars(), change);
        _production->setAmountTotal(units+change);
        setAssignedEngineer();
    }
}
/**
 * Adds given number of units to produce to the project if possible.
 * @param change How much we want to add.
 */
void ManufactureInfoState::moreUnit(int change)
{
	if (change <= 0) return;
	if (_production->getRules()->getCategory() == "STR_CRAFT" && _base->getAvailableHangars() - _base->getUsedHangars() <= 0)
	{
		_timerMoreUnit->stop();
		_game->pushState(new ErrorMessageState(tr("STR_NO_FREE_HANGARS_FOR_CRAFT_PRODUCTION"), _palette, _game->getRuleset()->getInterface("basescape")->getElement("errorMessage")->color, "BACK17.SCR", _game->getRuleset()->getInterface("basescape")->getElement("errorPalette")->color));
	}
	else
	{
		int units = _production->getAmountTotal();
		change = std::min(std::numeric_limits<int>::max()-units, change);
		if (_production->getRules()->getCategory() == "STR_CRAFT")
			change = std::min(_base->getAvailableHangars() - _base->getUsedHangars(), change);
		_production->setAmountTotal(units+change);
		setAssignedEngineer();
	}
}
/**
 * Increases the "units to produce", in the case of a right-click, to infinite, and 1 on left-click.
 * @param action A pointer to an Action.
 */
void ManufactureInfoState::moreUnitClick(Action *action)
{
	if (_production->getInfiniteAmount()) return; // We can't increase over infinite :)
	if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
	{
		if (_production->getRules()->getCategory() == "STR_CRAFT")
		{
			moreUnit(std::numeric_limits<int>::max());
		}
		else
		{
			_production->setInfiniteAmount(true);
			setAssignedEngineer();
		}
	}
	else if (action->getDetails()->button.button == SDL_BUTTON_LEFT)
	{
		moreUnit(1);
	}
}
/**
 * Builds screen User Interface.
 */
void ManufactureInfoState::buildUi()
{
	_screen = false;

	_window = new Window(this, 320, 150, 0, 25, POPUP_BOTH);
	_txtTitle = new Text(320, 17, 0, 35);
	_btnOk = new TextButton(136, 16, 168, 150);
	_btnStop = new TextButton(136, 16, 16, 150);
	_btnSell = new ToggleTextButton(60, 16, 244, 56);
	_txtAvailableEngineer = new Text(200, 9, 16, 55);
	_txtAvailableSpace = new Text(200, 9, 16, 65);
	_txtAllocatedEngineer = new Text(112, 32, 16, 75);
	_txtUnitToProduce = new Text(104, 32, 168, 75);
	_txtEngineerUp = new Text(90, 9, 40, 113);
	_txtEngineerDown = new Text(90, 9, 40, 133);
	_txtUnitUp = new Text(90, 9, 192, 113);
	_txtUnitDown = new Text(90, 9, 192, 133);
	_btnEngineerUp = new ArrowButton(ARROW_BIG_UP, 13, 14, 132, 109);
	_btnEngineerDown = new ArrowButton(ARROW_BIG_DOWN, 13, 14, 132, 131);
	_btnUnitUp = new ArrowButton(ARROW_BIG_UP, 13, 14, 284, 109);
	_btnUnitDown = new ArrowButton(ARROW_BIG_DOWN, 13, 14, 284, 131);
	_txtAllocated = new Text(40, 16, 128, 83);
	_txtTodo = new Text(40, 16, 272, 83);

	_surfaceEngineers = new InteractiveSurface(160, 150, 0, 25);
	_surfaceEngineers->onMouseClick((ActionHandler)&ManufactureInfoState::handleWheelEngineer, 0);

	_surfaceUnits = new InteractiveSurface(160, 150, 160, 25);
	_surfaceUnits->onMouseClick((ActionHandler)&ManufactureInfoState::handleWheelUnit, 0);

	// Set palette
	setInterface("manufactureInfo");

	add(_surfaceEngineers);
	add(_surfaceUnits);
	add(_window, "window", "manufactureInfo");
	add(_txtTitle, "text", "manufactureInfo");
	add(_txtAvailableEngineer, "text", "manufactureInfo");
	add(_txtAvailableSpace, "text", "manufactureInfo");
	add(_txtAllocatedEngineer, "text", "manufactureInfo");
	add(_txtAllocated, "text", "manufactureInfo");
	add(_txtUnitToProduce, "text", "manufactureInfo");
	add(_txtTodo, "text", "manufactureInfo");
	add(_txtEngineerUp, "text", "manufactureInfo");
	add(_txtEngineerDown, "text", "manufactureInfo");
	add(_btnEngineerUp, "button1", "manufactureInfo");
	add(_btnEngineerDown, "button1", "manufactureInfo");
	add(_txtUnitUp, "text", "manufactureInfo");
	add(_txtUnitDown, "text", "manufactureInfo");
	add(_btnUnitUp, "button1", "manufactureInfo");
	add(_btnUnitDown, "button1", "manufactureInfo");
	add(_btnOk, "button2", "manufactureInfo");
	add(_btnStop, "button2", "manufactureInfo");
	add(_btnSell, "button1", "manufactureInfo");

	centerAllSurfaces();

	_window->setBackground(_game->getResourcePack()->getSurface("BACK17.SCR"));

	_txtTitle->setText(tr(_item ? _item->getName() : _production->getRules()->getName()));
	_txtTitle->setBig();
	_txtTitle->setAlign(ALIGN_CENTER);

	_txtAllocatedEngineer->setText(tr("STR_ENGINEERS__ALLOCATED"));
	_txtAllocatedEngineer->setBig();
	_txtAllocatedEngineer->setWordWrap(true);
	_txtAllocatedEngineer->setVerticalAlign(ALIGN_MIDDLE);

	_txtAllocated->setBig();

	_txtTodo->setBig();

	_txtUnitToProduce->setText(tr("STR_UNITS_TO_PRODUCE"));
	_txtUnitToProduce->setBig();
	_txtUnitToProduce->setWordWrap(true);
	_txtUnitToProduce->setVerticalAlign(ALIGN_MIDDLE);

	_txtEngineerUp->setText(tr("STR_INCREASE_UC"));

	_txtEngineerDown->setText(tr("STR_DECREASE_UC"));

	_btnEngineerUp->onMousePress((ActionHandler)&ManufactureInfoState::moreEngineerPress);
	_btnEngineerUp->onMouseRelease((ActionHandler)&ManufactureInfoState::moreEngineerRelease);
	_btnEngineerUp->onMouseClick((ActionHandler)&ManufactureInfoState::moreEngineerClick, 0);

	_btnEngineerDown->onMousePress((ActionHandler)&ManufactureInfoState::lessEngineerPress);
	_btnEngineerDown->onMouseRelease((ActionHandler)&ManufactureInfoState::lessEngineerRelease);
	_btnEngineerDown->onMouseClick((ActionHandler)&ManufactureInfoState::lessEngineerClick, 0);

	_btnUnitUp->onMousePress((ActionHandler)&ManufactureInfoState::moreUnitPress);
	_btnUnitUp->onMouseRelease((ActionHandler)&ManufactureInfoState::moreUnitRelease);
	_btnUnitUp->onMouseClick((ActionHandler)&ManufactureInfoState::moreUnitClick, 0);

	_btnUnitDown->onMousePress((ActionHandler)&ManufactureInfoState::lessUnitPress);
	_btnUnitDown->onMouseRelease((ActionHandler)&ManufactureInfoState::lessUnitRelease);
	_btnUnitDown->onMouseClick((ActionHandler)&ManufactureInfoState::lessUnitClick, 0);

	_txtUnitUp->setText(tr("STR_INCREASE_UC"));

	_txtUnitDown->setText(tr("STR_DECREASE_UC"));

	_btnSell->setText(tr("STR_SELL_PRODUCTION"));

	_btnOk->setText(tr("STR_OK"));
	_btnOk->onMouseClick((ActionHandler)&ManufactureInfoState::btnOkClick);
	_btnOk->onKeyboardPress((ActionHandler)&ManufactureInfoState::btnOkClick, Options::keyOk);
	_btnOk->onKeyboardPress((ActionHandler)&ManufactureInfoState::btnOkClick, Options::keyCancel);

	_btnStop->setText(tr("STR_STOP_PRODUCTION"));
	_btnStop->onMouseClick((ActionHandler)&ManufactureInfoState::btnStopClick);
	if (!_production)
	{
		_production = new Production (_item, 1);
		_base->addProduction(_production);
	}
	setAssignedEngineer();
	_btnSell->setPressed(_production->getSellItems());

	_timerMoreEngineer = new Timer(250);
	_timerLessEngineer = new Timer(250);
	_timerMoreUnit = new Timer(250);
	_timerLessUnit = new Timer(250);
	_timerMoreEngineer->onTimer((StateHandler)&ManufactureInfoState::onMoreEngineer);
	_timerLessEngineer->onTimer((StateHandler)&ManufactureInfoState::onLessEngineer);
	_timerMoreUnit->onTimer((StateHandler)&ManufactureInfoState::onMoreUnit);
	_timerLessUnit->onTimer((StateHandler)&ManufactureInfoState::onLessUnit);
}
/**
 * Builds screen User Interface.
 */
void ManufactureInfoState::buildUi()
{
    _changeValueByMouseWheel = Options::getInt("changeValueByMouseWheel");

    _screen = false;
    int width = 320;
    int height = 170;
    int max_width = 320;
    int max_height = 200;
    int start_x = (max_width - width) / 2;
    int start_y = (max_height - height) / 2;
    int button_x_border = 10;
    int button_y_border = 10;
    int button_height = 16;

    int button_width = (width - 5 * button_x_border) / 2;
    _window = new Window(this, width, height, start_x, start_y);
    _txtTitle = new Text (width - 4 * button_x_border, button_height * 2, start_x + button_x_border, start_y + button_y_border);
    _btnOk = new TextButton (button_width, button_height, width - button_width - button_x_border, start_y + height - button_height - button_y_border);
    _btnStop = new TextButton (button_width, button_height, start_x + button_x_border, start_y + height - button_height - button_y_border);
    _txtAvailableEngineer = new Text(width - 4 * button_x_border, button_height, start_x + button_x_border, start_y + 2 * button_height);
    _txtAvailableSpace = new Text(width - 4 * button_x_border, button_height, start_x + button_x_border, start_y + 2.7f * button_height);
    _txtAllocatedEngineer = new Text(button_width - 1 * button_x_border, 2*button_height, start_x + button_x_border, start_y + 3.5f * button_height);
    _txtUnitToProduce = new Text(button_width - 4 * button_x_border, 2*button_height, width - button_width - button_x_border, start_y + 3.5f * button_height);
    _txtEngineerUp = new Text(button_width, 2*button_height, start_x + 3*button_x_border, start_y + 6 * button_height);
    _txtEngineerDown = new Text(button_width, 2*button_height, start_x + 3*button_x_border, start_y + 7.5f * button_height);
    _txtUnitUp = new Text(button_width, 2*button_height, width - button_width - button_x_border + 3*button_x_border, start_y + 6 * button_height);
    _txtUnitDown = new Text(button_width, 2*button_height, width - button_width - button_x_border + 3*button_x_border, start_y + 7.5f * button_height);
    _btnEngineerUp = new ArrowButton (ARROW_BIG_UP, 1.4f*button_x_border, button_height-2, width - button_width - 4*button_x_border, start_y + 6 * button_height);
    _btnEngineerDown = new ArrowButton (ARROW_BIG_DOWN, 1.4f*button_x_border, button_height-2, width - button_width - 4*button_x_border, start_y + 7.5f * button_height);
    _btnUnitUp = new ArrowButton (ARROW_BIG_UP, 1.4f*button_x_border, button_height-2, width - 4*button_x_border, start_y + 6 * button_height);
    _btnUnitDown = new ArrowButton (ARROW_BIG_DOWN, 1.4f*button_x_border, button_height-2, width - 4*button_x_border, start_y + 7.5f * button_height);
    _txtAllocated = new Text(button_width, 2*button_height, width - button_width - 5*button_x_border, start_y + 4 * button_height);
    _txtTodo = new Text(button_width, 2*button_height, width - 5*button_x_border, start_y + 4 * button_height);
    _game->setPalette(_game->getResourcePack()->getPalette("BACKPALS.DAT")->getColors(Palette::blockOffset(6)), Palette::backPos, 16);

    _surface1 = new InteractiveSurface((_btnEngineerUp->getX()+_btnEngineerUp->getWidth()+_txtUnitToProduce->getX()) / 2, height, start_x, start_y);
    _surface1->onMouseClick((ActionHandler)&ManufactureInfoState::handleWheelEngineer, 0);

    _surface2 = new InteractiveSurface(_surface1->getWidth(), height, start_x + _surface1->getWidth(), start_y);
    _surface2->onMouseClick((ActionHandler)&ManufactureInfoState::handleWheelUnit, 0);

    add(_surface1);
    add(_surface2);
    add(_window);
    add(_txtTitle);
    add(_txtAvailableEngineer);
    add(_txtAvailableSpace);
    add(_txtAllocatedEngineer);
    add(_txtAllocated);
    add(_txtUnitToProduce);
    add(_txtTodo);
    add(_txtEngineerUp);
    add(_txtEngineerDown);
    add(_btnEngineerUp);
    add(_btnEngineerDown);
    add(_txtUnitUp);
    add(_txtUnitDown);
    add(_btnUnitUp);
    add(_btnUnitDown);
    add(_btnOk);
    add(_btnStop);

    centerAllSurfaces();

    _window->setColor(Palette::blockOffset(15)+1);
    _window->setBackground(_game->getResourcePack()->getSurface("BACK17.SCR"));
    _txtTitle->setColor(Palette::blockOffset(15)+1);
    _txtTitle->setText(tr(_item ? _item->getName() : _production->getRules()->getName()));
    _txtTitle->setBig();
    _txtTitle->setAlign(ALIGN_CENTER);

    _txtAvailableEngineer->setColor(Palette::blockOffset(15)+1);
    _txtAvailableEngineer->setSecondaryColor(Palette::blockOffset(13));
    _txtAvailableSpace->setColor(Palette::blockOffset(15)+1);
    _txtAvailableSpace->setSecondaryColor(Palette::blockOffset(13));

    _txtAllocatedEngineer->setColor(Palette::blockOffset(15)+1);
    _txtAllocatedEngineer->setText(tr("STR_ENGINEERS__ALLOCATED"));
    _txtAllocatedEngineer->setBig();
    _txtAllocatedEngineer->setWordWrap(true);

    _txtAllocated->setColor(Palette::blockOffset(15)+1);
    _txtAllocated->setSecondaryColor(Palette::blockOffset(13));
    _txtAllocated->setBig();
    _txtTodo->setColor(Palette::blockOffset(15)+1);
    _txtTodo->setSecondaryColor(Palette::blockOffset(13));
    _txtTodo->setBig();

    _txtUnitToProduce->setColor(Palette::blockOffset(15)+1);
    _txtUnitToProduce->setText(tr("STR_UNITS_TO_PRODUCE"));
    _txtUnitToProduce->setBig();
    _txtUnitToProduce->setWordWrap(true);

    _txtEngineerUp->setColor(Palette::blockOffset(15)+1);
    _txtEngineerUp->setText(tr("STR_INCREASE_UC"));
    _txtEngineerDown->setColor(Palette::blockOffset(15)+1);
    _txtEngineerDown->setText(tr("STR_DECREASE_UC"));
    _btnEngineerUp->setColor(Palette::blockOffset(15)+1);
    _btnEngineerUp->onMousePress((ActionHandler)&ManufactureInfoState::moreEngineerPress);
    _btnEngineerUp->onMouseRelease((ActionHandler)&ManufactureInfoState::moreEngineerRelease);
    _btnEngineerUp->onMouseClick((ActionHandler)&ManufactureInfoState::moreEngineerClick, 0);

    _btnEngineerDown->setColor(Palette::blockOffset(15)+1);
    _btnEngineerDown->onMousePress((ActionHandler)&ManufactureInfoState::lessEngineerPress);
    _btnEngineerDown->onMouseRelease((ActionHandler)&ManufactureInfoState::lessEngineerRelease);
    _btnEngineerDown->onMouseClick((ActionHandler)&ManufactureInfoState::lessEngineerClick, 0);

    _btnUnitUp->setColor(Palette::blockOffset(15)+1);
    _btnUnitUp->onMousePress((ActionHandler)&ManufactureInfoState::moreUnitPress);
    _btnUnitUp->onMouseRelease((ActionHandler)&ManufactureInfoState::moreUnitRelease);
    _btnUnitUp->onMouseClick((ActionHandler)&ManufactureInfoState::moreUnitClick, 0);

    _btnUnitDown->setColor(Palette::blockOffset(15)+1);
    _btnUnitDown->onMousePress((ActionHandler)&ManufactureInfoState::lessUnitPress);
    _btnUnitDown->onMouseRelease((ActionHandler)&ManufactureInfoState::lessUnitRelease);
    _btnUnitDown->onMouseClick((ActionHandler)&ManufactureInfoState::lessUnitClick, 0);

    _txtUnitUp->setColor(Palette::blockOffset(15)+1);
    _txtUnitUp->setText(tr("STR_INCREASE_UC"));
    _txtUnitDown->setColor(Palette::blockOffset(15)+1);
    _txtUnitDown->setText(tr("STR_DECREASE_UC"));

    _btnOk->setColor(Palette::blockOffset(15)+6);
    _btnOk->setText(tr("STR_OK"));
    _btnOk->onMouseClick((ActionHandler)&ManufactureInfoState::btnOkClick);
    _btnOk->onKeyboardPress((ActionHandler)&ManufactureInfoState::btnOkClick, (SDLKey)Options::getInt("keyOk"));
    _btnOk->onKeyboardPress((ActionHandler)&ManufactureInfoState::btnOkClick, (SDLKey)Options::getInt("keyCancel"));

    _btnStop->setColor(Palette::blockOffset(15)+6);
    _btnStop->setText(tr("STR_STOP_PRODUCTION"));
    _btnStop->onMouseClick((ActionHandler)&ManufactureInfoState::btnStopClick);
    if(!_production)
    {
        _production = new Production (_item, 0);
        _base->addProduction(_production);
    }
    setAssignedEngineer();

    _timerMoreEngineer = new Timer(250);
    _timerLessEngineer = new Timer(250);
    _timerMoreUnit = new Timer(250);
    _timerLessUnit = new Timer(250);
    _timerMoreEngineer->onTimer((StateHandler)&ManufactureInfoState::onMoreEngineer);
    _timerLessEngineer->onTimer((StateHandler)&ManufactureInfoState::onLessEngineer);
    _timerMoreUnit->onTimer((StateHandler)&ManufactureInfoState::onMoreUnit);
    _timerLessUnit->onTimer((StateHandler)&ManufactureInfoState::onLessUnit);
}
Exemple #18
0
/**
 * Build one more unit
 */
void ProductionState::onMoreUnit()
{
	int more = _production->getNumberOfItemTodo ();
	_production->setNumberOfItemTodo (++more);
	setAssignedEngineer();	
}
/**
 * build screen User Interface
*/
void ManufactureInfoState::buildUi()
{
    _screen = false;
    int width = 320;
    int height = 170;
    int max_width = 320;
    int max_height = 200;
    int start_x = (max_width - width) / 2;
    int start_y = (max_height - height) / 2;
    int button_x_border = 10;
    int button_y_border = 10;
    int button_height = 16;

    int button_width = (width - 5 * button_x_border) / 2;
    _window = new Window(this, width, height, start_x, start_y);
    _txtTitle = new Text (width - 4 * button_x_border, button_height * 2, start_x + button_x_border, start_y + button_y_border);
    _btnOk = new TextButton (button_width, button_height, width - button_width - button_x_border, start_y + height - button_height - button_y_border);
    _btnStop = new TextButton (button_width, button_height, start_x + button_x_border, start_y + height - button_height - button_y_border);
    _txtAvailableEngineer = new Text(width - 4 * button_x_border, button_height, start_x + button_x_border, start_y + 2 * button_height);
    _txtAvailableSpace = new Text(width - 4 * button_x_border, button_height, start_x + button_x_border, start_y + 2.7f * button_height);
    _txtAllocatedEngineer = new Text(button_width, 2*button_height, start_x + button_x_border, start_y + 3.5f * button_height);
    _txtUnitToProduce = new Text(button_width, 2*button_height, width - button_width - button_x_border, start_y + 3.5f * button_height);
    _txtEngineerUp = new Text(button_width, 2*button_height, start_x + 3*button_x_border, start_y + 6 * button_height);
    _txtEngineerDown = new Text(button_width, 2*button_height, start_x + 3*button_x_border, start_y + 7.5f * button_height);
    _txtUnitUp = new Text(button_width, 2*button_height, width - button_width - button_x_border + 3*button_x_border, start_y + 6 * button_height);
    _txtUnitDown = new Text(button_width, 2*button_height, width - button_width - button_x_border + 3*button_x_border, start_y + 7.5f * button_height);
    _btnEngineerUp = new ArrowButton (ARROW_BIG_UP, 1.4f*button_x_border, button_height, width - button_width - 4*button_x_border, start_y + 6 * button_height);
    _btnEngineerDown = new ArrowButton (ARROW_BIG_DOWN, 1.4f*button_x_border, button_height, width - button_width - 4*button_x_border, start_y + 7.5f * button_height);
    _btnUnitUp = new ArrowButton (ARROW_BIG_UP, 1.4f*button_x_border, button_height, width - 4*button_x_border, start_y + 6 * button_height);
    _btnUnitDown = new ArrowButton (ARROW_BIG_DOWN, 1.4f*button_x_border, button_height, width - 4*button_x_border, start_y + 7.5f * button_height);
    _txtAllocated = new Text(button_width, 2*button_height, width - button_width - 5*button_x_border, start_y + 4 * button_height);
    _txtTodo = new Text(button_width, 2*button_height, width - 5*button_x_border, start_y + 4 * button_height);
    _game->setPalette(_game->getResourcePack()->getPalette("BACKPALS.DAT")->getColors(Palette::blockOffset(6)), Palette::backPos, 16);

    add(_window);
    add(_txtTitle);
    add(_txtAvailableEngineer);
    add(_txtAvailableSpace);
    add(_txtAllocatedEngineer);
    add(_txtAllocated);
    add(_txtUnitToProduce);
    add(_txtTodo);
    add(_txtEngineerUp);
    add(_txtEngineerDown);
    add(_btnEngineerUp);
    add(_btnEngineerDown);
    add(_txtUnitUp);
    add(_txtUnitDown);
    add(_btnUnitUp);
    add(_btnUnitDown);
    add(_btnOk);
    add(_btnStop);
    _window->setColor(Palette::blockOffset(15)+1);
    _window->setBackground(_game->getResourcePack()->getSurface("BACK17.SCR"));
    _txtTitle->setColor(Palette::blockOffset(15)+1);
    _txtTitle->setText(_game->getLanguage()->getString(_item ? _item->getName() : _production->getRules()->getName()));
    _txtTitle->setBig();
    _txtTitle->setAlign(ALIGN_CENTER);

    _txtAvailableEngineer->setColor(Palette::blockOffset(15)+1);
    _txtAvailableEngineer->setSecondaryColor(Palette::blockOffset(13));
    _txtAvailableSpace->setColor(Palette::blockOffset(15)+1);
    _txtAvailableSpace->setSecondaryColor(Palette::blockOffset(13));

    _txtAllocatedEngineer->setColor(Palette::blockOffset(15)+1);
    _txtAllocatedEngineer->setText(_game->getLanguage()->getString("STR_ENGINEERS__ALLOCATED"));
    _txtAllocatedEngineer->setBig();

    _txtAllocated->setColor(Palette::blockOffset(15)+1);
    _txtAllocated->setSecondaryColor(Palette::blockOffset(13));
    _txtAllocated->setBig();
    _txtTodo->setColor(Palette::blockOffset(15)+1);
    _txtTodo->setSecondaryColor(Palette::blockOffset(13));
    _txtTodo->setBig();

    _txtUnitToProduce->setColor(Palette::blockOffset(15)+1);
    _txtUnitToProduce->setText(_game->getLanguage()->getString("STR_TOTAL_TO_PRODUCE"));
    _txtUnitToProduce->setBig();

    _txtEngineerUp->setColor(Palette::blockOffset(15)+1);
    _txtEngineerUp->setText(_game->getLanguage()->getString("STR_INCREASE_UC"));
    _txtEngineerDown->setColor(Palette::blockOffset(15)+1);
    _txtEngineerDown->setText(_game->getLanguage()->getString("STR_DECREASE_UC"));
    _btnEngineerUp->setColor(Palette::blockOffset(15)+1);
    _btnEngineerUp->onMousePress((ActionHandler)&ManufactureInfoState::moreEngineerPress);
    _btnEngineerUp->onMouseRelease((ActionHandler)&ManufactureInfoState::moreEngineerRelease);

    _btnEngineerDown->setColor(Palette::blockOffset(15)+1);
    _btnEngineerDown->onMousePress((ActionHandler)&ManufactureInfoState::lessEngineerPress);
    _btnEngineerDown->onMouseRelease((ActionHandler)&ManufactureInfoState::lessEngineerRelease);

    _btnUnitUp->setColor(Palette::blockOffset(15)+1);
    _btnUnitUp->onMousePress((ActionHandler)&ManufactureInfoState::moreUnitPress);
    _btnUnitUp->onMouseRelease((ActionHandler)&ManufactureInfoState::moreUnitRelease);

    _btnUnitDown->setColor(Palette::blockOffset(15)+1);
    _btnUnitDown->onMousePress((ActionHandler)&ManufactureInfoState::lessUnitPress);
    _btnUnitDown->onMouseRelease((ActionHandler)&ManufactureInfoState::lessUnitRelease);

    _txtUnitUp->setColor(Palette::blockOffset(15)+1);
    _txtUnitUp->setText(_game->getLanguage()->getString("STR_INCREASE_UC"));
    _txtUnitDown->setColor(Palette::blockOffset(15)+1);
    _txtUnitDown->setText(_game->getLanguage()->getString("STR_DECREASE_UC"));

    _btnOk->setColor(Palette::blockOffset(13)+10);
    _btnOk->setText(_game->getLanguage()->getString("STR_OK"));
    _btnOk->onMouseClick((ActionHandler)&ManufactureInfoState::btnOkClick);

    _btnStop->setColor(Palette::blockOffset(13)+10);
    _btnStop->setText(_game->getLanguage()->getString("STR_STOP_PRODUCTION"));
    _btnStop->onMouseClick((ActionHandler)&ManufactureInfoState::btnStopClick);
    if(!_production)
    {
        _production = new Production (_item, 0);
        _base->addProduction(_production);
    }
    setAssignedEngineer();

    _timerMoreEngineer = new Timer(30);
    _timerLessEngineer = new Timer(30);
    _timerMoreUnit = new Timer(30);
    _timerLessUnit = new Timer(30);
    _timerMoreEngineer->onTimer((StateHandler)&ManufactureInfoState::onMoreEngineer);
    _timerLessEngineer->onTimer((StateHandler)&ManufactureInfoState::onLessEngineer);
    _timerMoreUnit->onTimer((StateHandler)&ManufactureInfoState::onMoreUnit);
    _timerLessUnit->onTimer((StateHandler)&ManufactureInfoState::onLessUnit);
}