1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-12-04 00:46:08 +00:00
Aquaria/Aquaria/RecipeMenuEntry.cpp

455 lines
8.9 KiB
C++
Raw Normal View History

/*
Copyright (C) 2007, 2010 - Bit-Blot
This file is part of Aquaria.
Aquaria is free software; you can redistribute it 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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "RecipeMenuEntry.h"
#include "Game.h"
#include "InGameMenu.h"
namespace RecipeMenuNamespace
{
int pageSize = 4;
std::string processFoodName(std::string name)
{
size_t p = name.find(' ');
if (p != std::string::npos)
{
name[p] = '\n';
}
return name;
}
}
using namespace RecipeMenuNamespace;
2016-05-05 17:40:28 +00:00
RecipeMenuEntry::RecipeMenuEntry(Recipe *recipe) : RenderObject(), recipe(recipe)
{
selected = 0;
2016-05-05 17:40:28 +00:00
data = dsq->continuity.getIngredientDataByName(recipe->result);
if (data)
{
glow = new Quad("particles/glow", Vector(-100, 0));
glow->scale = Vector(4,4);
glow->setBlendType(BLEND_ADD);
glow->alphaMod = 0;
addChild(glow, PM_POINTER);
2016-05-05 17:40:28 +00:00
result = new Quad("ingredients/" + data->gfx, Vector(-100,0));
result->scale = Vector(0.7, 0.7);
addChild(result, PM_POINTER);
2016-05-05 17:40:28 +00:00
BitmapText *text = new BitmapText(&dsq->smallFont);
text->scale = Vector(0.7, 0.7);
text->color = 0;
text->position = result->position + Vector(0, 18);
2016-05-05 17:40:28 +00:00
text->setText(processFoodName(data->displayName));
addChild(text, PM_POINTER);
}
Quad *equals = new Quad("gui/recipe-equals", Vector(-50, 0));
equals->scale = Vector(0.7, 0.7);
addChild(equals, PM_POINTER);
int c = 0;
2016-05-05 17:40:28 +00:00
int size=0;
for (int i = 0; i < recipe->names.size(); i++)
size += recipe->names[i].amount;
for (int j = 0; j < recipe->types.size(); j++)
size += recipe->types[j].amount;
size --;
2016-05-05 17:40:28 +00:00
for (int i = 0; i < recipe->names.size(); i++)
{
debugLog("recipe name: " + recipe->names[i].name);
IngredientData *data = dsq->continuity.getIngredientDataByName(recipe->names[i].name);
if (data)
{
for (int j = 0; j < recipe->names[i].amount; j++)
{
ing[c] = new Quad("ingredients/" + data->gfx, Vector(100*c,0));
ing[c]->scale = Vector(0.7, 0.7);
addChild(ing[c], PM_POINTER);
2016-05-05 17:40:28 +00:00
BitmapText *text = new BitmapText(&dsq->smallFont);
text->scale = Vector(0.7, 0.7);
text->color = 0;
text->position = ing[c]->position + Vector(0, 18);
text->setText(processFoodName(data->displayName));
addChild(text, PM_POINTER);
2016-05-05 17:40:28 +00:00
if (c < size)
{
Quad *plus = new Quad("gui/recipe-plus", Vector(100*c+50, 0));
plus->scale = Vector(0.7, 0.7);
addChild(plus, PM_POINTER);
}
2016-05-05 17:40:28 +00:00
c++;
if (c > 3)
{
errorLog(recipe->result + std::string("'s ingredient count exceeded"));
break;
}
}
}
}
2016-05-05 17:40:28 +00:00
for (int i = 0; i < recipe->types.size(); i++)
{
2016-05-05 17:40:28 +00:00
for (int j = 0; j < recipe->types[i].amount; j++)
{
// any type of whatever...
BitmapText *text = new BitmapText(&dsq->smallFont);
text->color = 0;
text->scale = Vector(0.8, 0.8);
2016-05-05 17:40:28 +00:00
text->position = Vector(100*c, 0);
std::string typeName = dsq->continuity.getIngredientDisplayName(recipe->types[i].typeName);
if (recipe->types[i].type != IT_ANYTHING)
typeName = dsq->continuity.stringBank.get(2031) + "\n" + typeName;
else
typeName = std::string("\n") + typeName;
text->setText(typeName);
addChild(text, PM_POINTER);
2016-05-05 17:40:28 +00:00
if (c < size)
{
Quad *plus = new Quad("gui/recipe-plus", Vector(100*c+50, 0));
plus->scale = Vector(0.7, 0.7);
addChild(plus, PM_POINTER);
}
2016-05-05 17:40:28 +00:00
c++;
2016-05-05 17:40:28 +00:00
}
}
description = 0;
alpha = 0;
alpha.interpolateTo(1, 0.2);
shareAlphaWithChildren = 1;
followCamera = 1;
}
void RecipeMenuEntry::onUpdate(float dt)
{
RenderObject::onUpdate(dt);
RecipeMenu& recipeMenu = game->getInGameMenu()->recipeMenu;
if (!recipeMenu.description)
return;
if (alpha.x == 1)
{
Vector p = result->getWorldPosition();
int w2 = 40, h2 = 32, w3 = 300;
if (core->mouse.position.x > p.x - w2
&& core->mouse.position.x < p.x + w3
&& core->mouse.position.y > p.y - h2
&& core->mouse.position.y < p.y + h2)
{
glow->alphaMod = 0.2;
std::ostringstream ds;
2016-05-05 17:40:28 +00:00
for (int i = 0; i < data->effects.size(); i++)
{
ds << dsq->continuity.getIEString(data, i);
if (i == data->effects.size()-1)
{
2016-05-05 17:40:28 +00:00
}
else
{
ds << ", ";
}
}
recipeMenu.description->setText(ds.str());
recipeMenu.description->offset = Vector(0, -10 * (recipeMenu.description->getNumLines()-1));
2016-05-05 17:40:28 +00:00
selected = 1;
}
else
{
glow->alphaMod = 0;
selected = 0;
int n=0;
for (int i = 0; i < recipeMenu.recipeMenuEntries.size(); i++)
{
if (!recipeMenu.recipeMenuEntries[i]->selected)
n++;
}
if (n == recipeMenu.recipeMenuEntries.size())
{
recipeMenu.description->setText("");
}
}
}
}
RecipeMenu::RecipeMenu()
{
on = false;
currentPage = 0;
description = 0;
}
void RecipeMenu::slide(RenderObject *r, bool in, float t)
{
int oy=-640;
if (in)
{
r->alpha = 1;
2016-05-05 17:40:28 +00:00
r->offset = Vector(0,oy);
r->offset.interpolateTo(Vector(0,0), t, 0, 0, 1);
}
else
{
r->alpha.interpolateTo(0, 1);
r->offset.interpolateTo(Vector(0,oy), t, 0, 0, 1);
}
}
int RecipeMenu::getNumKnown()
{
int num = 0;
for (int i = 0; i < dsq->continuity.recipes.size(); i++)
{
if (dsq->continuity.recipes[i].isKnown())
{
num++;
}
}
return num;
}
int RecipeMenu::getNumPages()
{
2016-05-05 17:40:28 +00:00
int numKnown = dsq->continuity.recipes.size();
int numPages = (numKnown/pageSize);
2016-05-05 17:40:28 +00:00
std::ostringstream os;
os << "numKnown: " << numKnown << " pagesSize: " << pageSize << " numPages: " << numPages;
debugLog(os.str());
2016-05-05 17:40:28 +00:00
return numPages;
}
void RecipeMenu::goPrevPage()
{
if (getNumPages() <= 1) return;
dsq->sound->playSfx("recipemenu-pageturn");
destroyPage();
2016-05-05 17:40:28 +00:00
currentPage--;
2016-05-05 17:40:28 +00:00
if (currentPage < 0)
currentPage = getNumPages();
2016-05-05 17:40:28 +00:00
createPage(currentPage);
}
void RecipeMenu::goNextPage()
{
if (getNumPages() <= 1) return;
dsq->sound->playSfx("recipemenu-pageturn");
destroyPage();
2016-05-05 17:40:28 +00:00
currentPage++;
2016-05-05 17:40:28 +00:00
int pages = getNumPages();
if (currentPage > pages)
{
currentPage = 0;
}
2016-05-05 17:40:28 +00:00
createPage(currentPage);
}
void RecipeMenu::toggle(bool on, bool watch)
{
if (dsq->isNested()) return;
static bool toggling = false;
if (toggling) return;
debugLog("RecipeMenu::toggle");
toggling = true;
2016-05-05 17:40:28 +00:00
float t = 0.6;
if (on)
{
slide(scroll, 1, t);
slide(scrollEnd, 1, t);
slide(header, 1, t);
slide(page, 1, t);
dsq->sound->playSfx("recipemenu-open");
if (watch)
dsq->run(t);
if (!dsq->game->isInGameMenu())
{
slide(scroll, 0, 0);
slide(scrollEnd, 0, 0);
slide(header, 0, 0);
slide(page, 0, 0);
toggling = false;
return;
}
createPage(currentPage);
2016-05-05 17:40:28 +00:00
nextPage->alpha.interpolateTo(1, 0.2);
prevPage->alpha.interpolateTo(1, 0.2);
}
else
{
destroyPage();
slide(scroll, 0, t);
slide(scrollEnd, 0, t);
slide(header, 0, t);
slide(page, 0, t);
if (this->on)
dsq->sound->playSfx("recipemenu-close");
2016-05-05 17:40:28 +00:00
nextPage->alpha = 0;
prevPage->alpha = 0;
if (watch)
dsq->run(t);
}
this->on = on;
AquariaGuiElement::canDirMoveGlobal = !this->on;
toggling = false;
}
void RecipeMenu::createPage(int p)
{
int num = 0;
int startNum = p*pageSize;
int checkNum = 0;
for (int i = dsq->continuity.recipes.size()-1; i >=0 ; i--)
{
if (dsq->continuity.recipes[i].isKnown())
{
if (checkNum >= startNum)
{
if (num < pageSize)
{
RecipeMenuEntry *r = new RecipeMenuEntry(&dsq->continuity.recipes[i]);
recipeMenuEntries.push_back(r);
2016-05-05 17:40:28 +00:00
r->position = Vector(500-5, 65 + num * 70);
dsq->game->addRenderObject(r, scroll->layer);
num++;
}
else
break;
}
else
{
checkNum++;
}
}
}
2016-05-05 17:40:28 +00:00
if (num == 0)
{
2016-05-05 17:40:28 +00:00
}
description = new BitmapText(&dsq->smallFont);
description->followCamera = 1;
description->scale = Vector(0.7, 0.7);
description->setAlign(ALIGN_LEFT);
2016-05-05 17:40:28 +00:00
description->position = Vector(364, 334); //most recent: (364, 334) //348, 328
description->color = Vector(0,0,0);//Vector(0.7,0,0);
description->setText("");
description->setWidth(500); // 1000??
2016-05-05 17:40:28 +00:00
dsq->game->addRenderObject(description, scroll->layer);
std::ostringstream os2;
os2 << dsq->continuity.stringBank.get(2006) << " " << currentPage+1 << "/" << getNumPages()+1;
page->setText(os2.str());
debugLog("done: " + os2.str());
}
void RecipeMenu::destroyPage()
{
for (int i = 0; i < recipeMenuEntries.size(); i++)
{
recipeMenuEntries[i]->setLife(1);
recipeMenuEntries[i]->setDecayRate(20);
recipeMenuEntries[i]->fadeAlphaWithLife = 1;
}
recipeMenuEntries.clear();
if (description)
{
description->setLife(1);
description->setDecayRate(4);
description->fadeAlphaWithLife = 1;
description = 0;
}
}