1 /*
2  * Copyright (C) 2007-2020 by the Widelands Development Team
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 
20 #include "wui/game_tips.h"
21 
22 #include <memory>
23 
24 #include <SDL_timer.h>
25 
26 #include "base/i18n.h"
27 #include "graphic/font_handler.h"
28 #include "graphic/graphic.h"
29 #include "graphic/rendertarget.h"
30 #include "graphic/text_layout.h"
31 #include "scripting/lua_interface.h"
32 #include "scripting/lua_table.h"
33 
34 #define BG_IMAGE "images/loadscreens/tips_bg.png"
35 
36 constexpr int kTextPadding = 48;
37 
GameTips(UI::ProgressWindow & progressWindow,const std::vector<std::string> & names)38 GameTips::GameTips(UI::ProgressWindow& progressWindow, const std::vector<std::string>& names)
39    : lastUpdated_(0),
40      updateAfter_(0),
41      progressWindow_(progressWindow),
42      registered_(false),
43      lastTip_(0) {
44 	// Loading the "texts" locale for translating the tips
45 	i18n::Textdomain textdomain("texts");
46 
47 	for (uint8_t i = 0; i < names.size(); ++i)
48 		load_tips(names[i]);
49 
50 	if (!tips_.empty()) {
51 		// add visualization only if any tips are loaded
52 		progressWindow_.add_visualization(this);
53 		registered_ = true;
54 		lastTip_ = tips_.size();
55 	}
56 }
57 
~GameTips()58 GameTips::~GameTips() {
59 	stop();
60 }
61 
62 /// Loads tips out of \var name
load_tips(const std::string & name)63 void GameTips::load_tips(const std::string& name) {
64 	try {
65 		LuaInterface lua;
66 		std::unique_ptr<LuaTable> table(lua.run_script("txts/tips/" + name + ".lua"));
67 		std::unique_ptr<LuaTable> tip_table;
68 		for (const int key : table->keys<int>()) {
69 			tip_table = table->get_table(key);
70 			Tip tip;
71 			tip.text = tip_table->get_string("text");
72 			tip.interval = tip_table->get_int("seconds");
73 			tips_.push_back(tip);
74 		}
75 	} catch (LuaError& err) {
76 		log("Error loading tips script for %s:\n%s\n", name.c_str(), err.what());
77 		// No further handling necessary - tips do not impact game
78 	}
79 }
80 
update(bool repaint)81 void GameTips::update(bool repaint) {
82 	uint32_t ticks = SDL_GetTicks();
83 	if (ticks >= (lastUpdated_ + updateAfter_)) {
84 		const uint32_t next = rand() % tips_.size();
85 		if (next == lastTip_)
86 			lastTip_ = (next + 1) % tips_.size();
87 		else
88 			lastTip_ = next;
89 		show_tip(next);
90 		lastUpdated_ = SDL_GetTicks();
91 		updateAfter_ = tips_[next].interval * 1000;
92 	} else if (repaint) {
93 		show_tip(lastTip_);
94 	}
95 }
96 
stop()97 void GameTips::stop() {
98 	if (registered_) {
99 		progressWindow_.remove_visualization(this);
100 		registered_ = false;
101 	}
102 }
103 
show_tip(int32_t index)104 void GameTips::show_tip(int32_t index) {
105 	RenderTarget& rt = *g_gr->get_render_target();
106 
107 	const Image* pic_background = g_gr->images().get(BG_IMAGE);
108 	const int w = pic_background->width();
109 	const int h = pic_background->height();
110 	Vector2i pt((g_gr->get_xres() - w) / 2, (g_gr->get_yres() - h) / 2);
111 	rt.blit(pt, pic_background);
112 
113 	std::shared_ptr<const UI::RenderedText> rendered_text =
114 	   UI::g_fh->render(as_game_tip(tips_[index].text), w - 2 * kTextPadding);
115 	pt = Vector2i((g_gr->get_xres() - rendered_text->width()) / 2,
116 	              (g_gr->get_yres() - rendered_text->height()) / 2);
117 	rendered_text->draw(rt, pt);
118 }
119