aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/content_mapblock.cpp61
-rw-r--r--src/content_mapnode.cpp14
-rw-r--r--src/content_mapnode.h2
-rw-r--r--src/environment.cpp30
-rw-r--r--src/map.cpp1
-rw-r--r--src/mapgen.h3
-rw-r--r--src/mapnode.h4
-rw-r--r--src/server.cpp28
8 files changed, 141 insertions, 2 deletions
diff --git a/src/content_mapblock.cpp b/src/content_mapblock.cpp
index 1cc37b969..9a156cb55 100644
--- a/src/content_mapblock.cpp
+++ b/src/content_mapblock.cpp
@@ -199,6 +199,18 @@ void mapblock_mesh_generate_special(MeshMakeData *data,
g_texturesource->getTextureId("apple.png"));
material_apple.setTexture(0, pa_apple.atlas);
+
+ // Sapling material
+ video::SMaterial material_sapling;
+ material_sapling.setFlag(video::EMF_LIGHTING, false);
+ material_sapling.setFlag(video::EMF_BILINEAR_FILTER, false);
+ material_sapling.setFlag(video::EMF_FOG_ENABLE, true);
+ material_sapling.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
+ AtlasPointer pa_sapling = g_texturesource->getTexture(
+ g_texturesource->getTextureId("sapling.png"));
+ material_sapling.setTexture(0, pa_sapling.atlas);
+
+
// junglegrass material
video::SMaterial material_junglegrass;
material_junglegrass.setFlag(video::EMF_LIGHTING, false);
@@ -1263,6 +1275,55 @@ void mapblock_mesh_generate_special(MeshMakeData *data,
collector.append(material_apple, vertices, 4, indices, 6);
}
}
+ else if(n.getContent() == CONTENT_SAPLING) {
+ u8 l = decode_light(undiminish_light(n.getLightBlend(data->m_daynight_ratio)));
+ video::SColor c = MapBlock_LightColor(255, l);
+
+ for(u32 j=0; j<4; j++)
+ {
+ video::S3DVertex vertices[4] =
+ {
+ video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
+ pa_sapling.x0(), pa_sapling.y1()),
+ video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
+ pa_sapling.x1(), pa_sapling.y1()),
+ video::S3DVertex(BS/2,BS/1,0, 0,0,0, c,
+ pa_sapling.x1(), pa_sapling.y0()),
+ video::S3DVertex(-BS/2,BS/1,0, 0,0,0, c,
+ pa_sapling.x0(), pa_sapling.y0()),
+ };
+
+ if(j == 0)
+ {
+ for(u16 i=0; i<4; i++)
+ vertices[i].Pos.rotateXZBy(45);
+ }
+ else if(j == 1)
+ {
+ for(u16 i=0; i<4; i++)
+ vertices[i].Pos.rotateXZBy(-45);
+ }
+ else if(j == 2)
+ {
+ for(u16 i=0; i<4; i++)
+ vertices[i].Pos.rotateXZBy(135);
+ }
+ else if(j == 3)
+ {
+ for(u16 i=0; i<4; i++)
+ vertices[i].Pos.rotateXZBy(-135);
+ }
+
+ for(u16 i=0; i<4; i++)
+ {
+ vertices[i].Pos += intToFloat(p + blockpos_nodes, BS);
+ }
+
+ u16 indices[] = {0,1,2,2,3,0};
+ // Add to mesh collector
+ collector.append(material_sapling, vertices, 4, indices, 6);
+ }
+ }
}
}
#endif
diff --git a/src/content_mapnode.cpp b/src/content_mapnode.cpp
index 09a84156a..308397735 100644
--- a/src/content_mapnode.cpp
+++ b/src/content_mapnode.cpp
@@ -242,6 +242,8 @@ void content_mapnode_init()
{
f->setAllTextures("[noalpha:leaves.png");
}
+ f->extra_dug_item = std::string("MaterialItem2 ")+itos(CONTENT_SAPLING)+" 1";
+ f->extra_dug_item_rarity = 10;
f->dug_item = std::string("MaterialItem2 ")+itos(i)+" 1";
setWoodLikeDiggingProperties(f->digging_properties, 0.15);
@@ -629,6 +631,18 @@ void content_mapnode_init()
f->setInventoryTexture("nc_rb.png");
f->dug_item = std::string("MaterialItem2 ")+itos(i)+" 1";
setStoneLikeDiggingProperties(f->digging_properties, 3.0);
+
+ i = CONTENT_SAPLING;
+ f = &content_features(i);
+ f->param_type = CPT_LIGHT;
+ f->setAllTextures("sapling.png");
+ f->setInventoryTexture("sapling.png");
+ f->dug_item = std::string("MaterialItem2 ")+itos(i)+" 1";
+ f->light_propagates = true;
+ f->air_equivalent = false;
+ f->solidness = 0; // drawn separately, makes no faces
+ f->walkable = false;
+ f->digging_properties.set("", DiggingProperties(true, 0.0, 0));
i = CONTENT_APPLE;
f = &content_features(i);
diff --git a/src/content_mapnode.h b/src/content_mapnode.h
index 35dcc20ec..c1e12ab42 100644
--- a/src/content_mapnode.h
+++ b/src/content_mapnode.h
@@ -84,7 +84,7 @@ MapNode mapnode_translate_to_internal(MapNode n_from, u8 version);
#define CONTENT_NC 0x817
#define CONTENT_NC_RB 0x818
#define CONTENT_APPLE 0x819
-
+#define CONTENT_SAPLING 0x820
#endif
diff --git a/src/environment.cpp b/src/environment.cpp
index 8103b7110..93f1627c5 100644
--- a/src/environment.cpp
+++ b/src/environment.cpp
@@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapblock.h"
#include "serverobject.h"
#include "content_sao.h"
+#include "mapgen.h"
Environment::Environment():
m_time_of_day(9000)
@@ -922,7 +923,34 @@ void ServerEnvironment::step(float dtime)
addActiveObject(obj);
}
}
- }
+ }
+ /*
+ Make trees from saplings!
+ */
+ if(n.getContent() == CONTENT_SAPLING)
+ {
+ if(myrand()%2 == 0)
+ {
+ core::map<v3s16, MapBlock*> modified_blocks;
+ v3s16 tree_p = p;
+ MapEditEvent event;
+ event.type = MEET_OTHER;
+ ManualMapVoxelManipulator vmanip(m_map);
+ v3s16 tree_blockp = getNodeBlockPos(tree_p);
+ vmanip.initialEmerge(tree_blockp - v3s16(1,1,1), tree_blockp + v3s16(1,1,1));
+ bool is_apple_tree = myrand()%4 == 0;
+ mapgen::make_tree(vmanip, tree_p, is_apple_tree);
+ for(core::map<v3s16, MapBlock*>::Iterator
+ i = modified_blocks.getIterator();
+ i.atEnd() == false; i++)
+ {
+ v3s16 p = i.getNode()->getKey();
+ event.modified_blocks.insert(p, true);
+ }
+ m_map->dispatchEvent(&event);
+ }
+ }
+
}
}
}
diff --git a/src/map.cpp b/src/map.cpp
index 7de79c75d..d870b5e71 100644
--- a/src/map.cpp
+++ b/src/map.cpp
@@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "porting.h"
#include "mapgen.h"
#include "nodemetadata.h"
+#include "content_mapnode.h"
/*
SQLite format specification:
diff --git a/src/mapgen.h b/src/mapgen.h
index 57d0ee8a0..f848389a8 100644
--- a/src/mapgen.h
+++ b/src/mapgen.h
@@ -40,6 +40,9 @@ namespace mapgen
// Add objects according to block content
void add_random_objects(MapBlock *block);
+
+ // Add a tree
+ void make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0, bool is_apple_tree);
/*
These are used by FarMesh
diff --git a/src/mapnode.h b/src/mapnode.h
index 3ad67aaf6..88e7a0d0a 100644
--- a/src/mapnode.h
+++ b/src/mapnode.h
@@ -145,6 +145,10 @@ struct ContentFeatures
// Inventory item string as which the node appears in inventory when dug.
// Mineral overrides this.
std::string dug_item;
+
+ // Extra dug item and its rarity
+ std::string extra_dug_item;
+ s32 extra_dug_item_rarity;
// Initial metadata is cloned from this
NodeMetadata *initial_metadata;
diff --git a/src/server.cpp b/src/server.cpp
index a04417074..04a23b777 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -2735,6 +2735,34 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
UpdateCrafting(player->peer_id);
SendInventory(player->peer_id);
}
+
+ item = NULL;
+
+ if(mineral != MINERAL_NONE)
+ item = getDiggedMineralItem(mineral);
+
+ // If not mineral
+ if(item == NULL)
+ {
+ std::string &extra_dug_s = content_features(material).extra_dug_item;
+ s32 extra_rarity = content_features(material).extra_dug_item_rarity;
+ if(extra_dug_s != "" && extra_rarity != 0
+ && myrand() % extra_rarity == 0)
+ {
+ std::istringstream is(extra_dug_s, std::ios::binary);
+ item = InventoryItem::deSerialize(is);
+ }
+ }
+
+ if(item != NULL)
+ {
+ // Add a item to inventory
+ player->inventory.addItem("main", item);
+
+ // Send inventory
+ UpdateCrafting(player->peer_id);
+ SendInventory(player->peer_id);
+ }
}
/*
'>603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
/*
Copyright (C) 2014 sapier
Copyright (C) 2018 srifqi, Muhammad Rifqi Priyo Susanto
		<muhammadrifqipriyosusanto@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "touchscreengui.h"
#include "irrlichttypes.h"
#include "irr_v2d.h"
#include "log.h"
#include "client/keycode.h"
#include "settings.h"
#include "gettime.h"
#include "util/numeric.h"
#include "porting.h"
#include "client/guiscalingfilter.h"

#include <iostream>
#include <algorithm>

#include <ISceneCollisionManager.h>

// Very slow button repeat frequency (in seconds)
#define SLOW_BUTTON_REPEAT	(1.0f)

using namespace irr::core;

const char **touchgui_button_imagenames = (const char *[]) {
	"jump_btn.png",
	"down.png",
	"zoom.png",
	"aux_btn.png"
};

const char **touchgui_joystick_imagenames = (const char *[]) {
	"joystick_off.png",
	"joystick_bg.png",
	"joystick_center.png"
};

static irr::EKEY_CODE id2keycode(touch_gui_button_id id)
{
	std::string key = "";
	switch (id) {
		case forward_id:
			key = "forward";
			break;
		case left_id:
			key = "left";
			break;
		case right_id:
			key = "right";
			break;
		case backward_id:
			key = "backward";
			break;
		case inventory_id:
			key = "inventory";
			break;
		case drop_id:
			key = "drop";
			break;
		case jump_id:
			key = "jump";
			break;
		case crunch_id:
			key = "sneak";
			break;
		case zoom_id:
			key = "zoom";
			break;
		case special1_id:
			key = "special1";
			break;
		case fly_id:
			key = "freemove";
			break;
		case noclip_id:
			key = "noclip";
			break;
		case fast_id:
			key = "fastmove";
			break;
		case debug_id:
			key = "toggle_debug";
			break;
		case toggle_chat_id:
			key = "toggle_chat";
			break;
		case minimap_id:
			key = "minimap";
			break;
		case chat_id:
			key = "chat";
			break;
		case camera_id:
			key = "camera_mode";
			break;
		case range_id:
			key = "rangeselect";
			break;
	}
	assert(key != "");
	return keyname_to_keycode(g_settings->get("keymap_" + key).c_str());
}

TouchScreenGUI *g_touchscreengui;

static void load_button_texture(button_info *btn, const char *path,
		rect<s32> button_rect, ISimpleTextureSource *tsrc, video::IVideoDriver *driver)
{
	unsigned int tid;
	video::ITexture *texture = guiScalingImageButton(driver,
			tsrc->getTexture(path, &tid), button_rect.getWidth(),
			button_rect.getHeight());
	if (texture) {
		btn->guibutton->setUseAlphaChannel(true);
		if (g_settings->getBool("gui_scaling_filter")) {
			rect<s32> txr_rect = rect<s32>(0, 0, button_rect.getWidth(), button_rect.getHeight());
			btn->guibutton->setImage(texture, txr_rect);
			btn->guibutton->setPressedImage(texture, txr_rect);
			btn->guibutton->setScaleImage(false);
		} else {
			btn->guibutton->setImage(texture);
			btn->guibutton->setPressedImage(texture);
			btn->guibutton->setScaleImage(true);
		}
		btn->guibutton->setDrawBorder(false);
		btn->guibutton->setText(L"");
		}
}

AutoHideButtonBar::AutoHideButtonBar(IrrlichtDevice *device,
		IEventReceiver *receiver) :
			m_driver(device->getVideoDriver()),
			m_guienv(device->getGUIEnvironment()),
			m_receiver(receiver)
{
}

void AutoHideButtonBar::init(ISimpleTextureSource *tsrc,
		const char *starter_img, int button_id, v2s32 UpperLeft,
		v2s32 LowerRight, autohide_button_bar_dir dir, float timeout)
{
	m_texturesource = tsrc;

	m_upper_left = UpperLeft;
	m_lower_right = LowerRight;

	// init settings bar

	irr::core::rect<int> current_button = rect<s32>(UpperLeft.X, UpperLeft.Y,
			LowerRight.X, LowerRight.Y);

	m_starter.guibutton         = m_guienv->addButton(current_button, 0, button_id, L"", 0);
	m_starter.guibutton->grab();
	m_starter.repeatcounter     = -1;
	m_starter.keycode           = KEY_OEM_8; // use invalid keycode as it's not relevant
	m_starter.immediate_release = true;
	m_starter.ids.clear();

	load_button_texture(&m_starter, starter_img, current_button,
			m_texturesource, m_driver);

	m_dir = dir;
	m_timeout_value = timeout;

	m_initialized = true;
}

AutoHideButtonBar::~AutoHideButtonBar()
{
	if (m_starter.guibutton) {
		m_starter.guibutton->setVisible(false);
		m_starter.guibutton->drop();
	}
}

void AutoHideButtonBar::addButton(touch_gui_button_id button_id,
		const wchar_t *caption, const char *btn_image)
{

	if (!m_initialized) {
		errorstream << "AutoHideButtonBar::addButton not yet initialized!"
				<< std::endl;
		return;
	}
	int button_size = 0;

	if ((m_dir == AHBB_Dir_Top_Bottom) || (m_dir == AHBB_Dir_Bottom_Top)) {
		button_size = m_lower_right.X - m_upper_left.X;
	} else {
		button_size = m_lower_right.Y - m_upper_left.Y;
	}

	irr::core::rect<int> current_button;

	if ((m_dir == AHBB_Dir_Right_Left) || (m_dir == AHBB_Dir_Left_Right)) {

		int x_start = 0;
		int x_end = 0;

		if (m_dir == AHBB_Dir_Left_Right) {
			x_start = m_lower_right.X + (button_size * 1.25 * m_buttons.size())
					+ (button_size * 0.25);
			x_end = x_start + button_size;
		} else {
			x_end = m_upper_left.X - (button_size * 1.25 * m_buttons.size())
					- (button_size * 0.25);
			x_start = x_end - button_size;
		}

		current_button = rect<s32>(x_start, m_upper_left.Y, x_end,
				m_lower_right.Y);
	} else {
		int y_start = 0;
		int y_end = 0;

		if (m_dir == AHBB_Dir_Top_Bottom) {
			y_start = m_lower_right.X + (button_size * 1.25 * m_buttons.size())
					+ (button_size * 0.25);
			y_end = y_start + button_size;
		} else {
			y_end = m_upper_left.X - (button_size * 1.25 * m_buttons.size())
					- (button_size * 0.25);
			y_start = y_end - button_size;
		}

		current_button = rect<s32>(m_upper_left.X, y_start, m_lower_right.Y,
				y_end);
	}

	button_info *btn       = new button_info();
	btn->guibutton         = m_guienv->addButton(current_button, 0, button_id, caption, 0);
	btn->guibutton->grab();
	btn->guibutton->setVisible(false);
	btn->guibutton->setEnabled(false);
	btn->repeatcounter     = -1;
	btn->keycode           = id2keycode(button_id);
	btn->immediate_release = true;
	btn->ids.clear();

	load_button_texture(btn, btn_image, current_button, m_texturesource,
			m_driver);

	m_buttons.push_back(btn);
}

void AutoHideButtonBar::addToggleButton(touch_gui_button_id button_id,
		const wchar_t *caption, const char *btn_image_1,
		const char *btn_image_2)
{
	addButton(button_id, caption, btn_image_1);
	button_info *btn = m_buttons.back();
	btn->togglable = 1;
	btn->textures.push_back(btn_image_1);
	btn->textures.push_back(btn_image_2);
}

bool AutoHideButtonBar::isButton(const SEvent &event)
{
	IGUIElement *rootguielement = m_guienv->getRootGUIElement();

	if (rootguielement == NULL) {
		return false;
	}

	gui::IGUIElement *element = rootguielement->getElementFromPoint(
			core::position2d<s32>(event.TouchInput.X, event.TouchInput.Y));

	if (element == NULL) {
		return false;
	}

	if (m_active) {
		// check for all buttons in vector

		std::vector<button_info *>::iterator iter = m_buttons.begin();

		while (iter != m_buttons.end()) {
			if ((*iter)->guibutton == element) {

				SEvent *translated = new SEvent();
				memset(translated, 0, sizeof(SEvent));
				translated->EventType            = irr::EET_KEY_INPUT_EVENT;
				translated->KeyInput.Key         = (*iter)->keycode;
				translated->KeyInput.Control     = false;
				translated->KeyInput.Shift       = false;
				translated->KeyInput.Char        = 0;

				// add this event
				translated->KeyInput.PressedDown = true;
				m_receiver->OnEvent(*translated);

				// remove this event
				translated->KeyInput.PressedDown = false;
				m_receiver->OnEvent(*translated);

				delete translated;

				(*iter)->ids.push_back(event.TouchInput.ID);

				m_timeout = 0;

				if ((*iter)->togglable == 1) {
					(*iter)->togglable = 2;
					load_button_texture(*iter, (*iter)->textures[1],
							(*iter)->guibutton->getRelativePosition(),
							m_texturesource, m_driver);
				} else if ((*iter)->togglable == 2) {
					(*iter)->togglable = 1;
					load_button_texture(*iter, (*iter)->textures[0],
							(*iter)->guibutton->getRelativePosition(),
							m_texturesource, m_driver);
				}

				return true;
			}
			++iter;
		}
	} else {
		// check for starter button only
		if (element == m_starter.guibutton) {
			m_starter.ids.push_back(event.TouchInput.ID);
			m_starter.guibutton->setVisible(false);
			m_starter.guibutton->setEnabled(false);
			m_active = true;
			m_timeout = 0;

			std::vector<button_info*>::iterator iter = m_buttons.begin();

			while (iter != m_buttons.end()) {
				(*iter)->guibutton->setVisible(true);
				(*iter)->guibutton->setEnabled(true);
				++iter;
			}

			return true;
		}
	}
	return false;
}

bool AutoHideButtonBar::isReleaseButton(int eventID)
{
	std::vector<int>::iterator id = std::find(m_starter.ids.begin(),
			m_starter.ids.end(), eventID);

	if (id != m_starter.ids.end()) {
		m_starter.ids.erase(id);
		return true;
	}

	std::vector<button_info *>::iterator iter = m_buttons.begin();

	while (iter != m_buttons.end()) {
		std::vector<int>::iterator id = std::find((*iter)->ids.begin(),
				(*iter)->ids.end(), eventID);

		if (id != (*iter)->ids.end()) {
			(*iter)->ids.erase(id);
			// TODO handle settings button release
			return true;
		}
		++iter;
	}

	return false;
}

void AutoHideButtonBar::step(float dtime)
{
	if (m_active) {
		m_timeout += dtime;

		if (m_timeout > m_timeout_value) {
			deactivate();
		}
	}
}

void AutoHideButtonBar::deactivate()
{
	if (m_visible) {
		m_starter.guibutton->setVisible(true);
		m_starter.guibutton->setEnabled(true);
	}
	m_active = false;

	std::vector<button_info *>::iterator iter = m_buttons.begin();

	while (iter != m_buttons.end()) {
			(*iter)->guibutton->setVisible(false);
			(*iter)->guibutton->setEnabled(false);
		++iter;
	}
}

void AutoHideButtonBar::hide()
{
	m_visible = false;
	m_starter.guibutton->setVisible(false);
	m_starter.guibutton->setEnabled(false);

	std::vector<button_info *>::iterator iter = m_buttons.begin();

	while (iter != m_buttons.end()) {
		(*iter)->guibutton->setVisible(false);
		(*iter)->guibutton->setEnabled(false);
		++iter;
	}
}

void AutoHideButtonBar::show()
{
	m_visible = true;

	if (m_active) {
		std::vector<button_info *>::iterator iter = m_buttons.begin();

		while (iter != m_buttons.end()) {
			(*iter)->guibutton->setVisible(true);
			(*iter)->guibutton->setEnabled(true);
			++iter;
		}
	} else {
		m_starter.guibutton->setVisible(true);
		m_starter.guibutton->setEnabled(true);
	}
}

TouchScreenGUI::TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver):
	m_device(device),
	m_guienv(device->getGUIEnvironment()),
	m_receiver(receiver),
	m_settingsbar(device, receiver),
	m_rarecontrolsbar(device, receiver)
{
	for (unsigned int i=0; i < after_last_element_id; i++) {
		m_buttons[i].guibutton     =  0;
		m_buttons[i].repeatcounter = -1;
		m_buttons[i].repeatdelay   = BUTTON_REPEAT_DELAY;
	}

	m_touchscreen_threshold = g_settings->getU16("touchscreen_threshold");
	m_fixed_joystick = g_settings->getBool("fixed_virtual_joystick");
	m_joystick_triggers_special1 = g_settings->getBool("virtual_joystick_triggers_aux");
	m_screensize = m_device->getVideoDriver()->getScreenSize();
}

void TouchScreenGUI::initButton(touch_gui_button_id id, rect<s32> button_rect,
		std::wstring caption, bool immediate_release, float repeat_delay)
{

	button_info *btn       = &m_buttons[id];
	btn->guibutton         = m_guienv->addButton(button_rect, 0, id, caption.c_str());
	btn->guibutton->grab();
	btn->repeatcounter     = -1;
	btn->repeatdelay       = repeat_delay;
	btn->keycode           = id2keycode(id);
	btn->immediate_release = immediate_release;
	btn->ids.clear();

	load_button_texture(btn, touchgui_button_imagenames[id], button_rect,
			m_texturesource, m_device->getVideoDriver());
}

button_info *TouchScreenGUI::initJoystickButton(touch_gui_button_id id, rect<s32> button_rect,
		int texture_id, bool visible)
{
	button_info *btn = new button_info();
	btn->guibutton   = m_guienv->addButton(button_rect, 0, id, L"O");
	btn->guibutton->setVisible(visible);
	btn->guibutton->grab();
	btn->ids.clear();

	load_button_texture(btn, touchgui_joystick_imagenames[texture_id], button_rect,
			m_texturesource, m_device->getVideoDriver());

	return btn;
}

static int getMaxControlPadSize(float density) {
	return 200 * density * g_settings->getFloat("hud_scaling");
}

int TouchScreenGUI::getGuiButtonSize()
{
	u32 control_pad_size = MYMIN((2 * m_screensize.Y) / 3,
			getMaxControlPadSize(porting::getDisplayDensity()));

	return control_pad_size / 3;
}

void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
{
	assert(tsrc);

	u32 button_size = getGuiButtonSize();
	m_visible       = true;
	m_texturesource = tsrc;

	/* Init joystick display "button"
	 * Joystick is placed on bottom left of screen.
	 */
	if (m_fixed_joystick) {
		m_joystick_btn_off = initJoystickButton(joystick_off_id,
				rect<s32>(button_size,
						m_screensize.Y - button_size * 4,
						button_size * 4,
						m_screensize.Y - button_size), 0);
	} else {
		m_joystick_btn_off = initJoystickButton(joystick_off_id,
				rect<s32>(button_size,
						m_screensize.Y - button_size * 3,
						button_size * 3,
						m_screensize.Y - button_size), 0);
	}

	m_joystick_btn_bg = initJoystickButton(joystick_bg_id,
			rect<s32>(button_size,
					m_screensize.Y - button_size * 4,
					button_size * 4,
					m_screensize.Y - button_size),
			1, false);

	m_joystick_btn_center = initJoystickButton(joystick_center_id,
			rect<s32>(0, 0, button_size, button_size), 2, false);

	// init jump button
	initButton(jump_id,
			rect<s32>(m_screensize.X - (1.75 * button_size),
					m_screensize.Y - button_size,
					m_screensize.X - (0.25 * button_size),
					m_screensize.Y),
			L"x", false);

	// init crunch button
	initButton(crunch_id,
			rect<s32>(m_screensize.X - (3.25 * button_size),
					m_screensize.Y - button_size,
					m_screensize.X - (1.75 * button_size),
					m_screensize.Y),
			L"H", false);

	// init zoom button
	initButton(zoom_id,
			rect<s32>(m_screensize.X - (1.25 * button_size),
					m_screensize.Y - (4 * button_size),
					m_screensize.X - (0.25 * button_size),
					m_screensize.Y - (3 * button_size)),
			L"z", false);

	// init special1/aux button
	if (!m_joystick_triggers_special1)
		initButton(special1_id,
				rect<s32>(m_screensize.X - (1.25 * button_size),
						m_screensize.Y - (2.5 * button_size),
						m_screensize.X - (0.25 * button_size),
						m_screensize.Y - (1.5 * button_size)),
				L"spc1", false);

	m_settingsbar.init(m_texturesource, "gear_icon.png", settings_starter_id,
		v2s32(m_screensize.X - (1.25 * button_size),
			m_screensize.Y - ((SETTINGS_BAR_Y_OFFSET + 1.0) * button_size)
				+ (0.5 * button_size)),