summaryrefslogtreecommitdiff
path: root/src/network/networkpacket.h
blob: 72f8cabe276e0c3e19af340d27c16eb104a00a89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Minetest
Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>

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.
*/

#ifndef NETWORKPACKET_HEADER
#define NETWORKPACKET_HEADER

#include "util/pointer.h"
#include "util/numeric.h"
#include "networkprotocol.h"

class NetworkPacket
{

public:
		NetworkPacket(u16 command, u32 datasize, u16 peer_id);
		NetworkPacket(u16 command, u32 datasize);
		NetworkPacket(): m_datasize(0), m_read_offset(0), m_command(0),
				m_peer_id(0) {}
		~NetworkPacket();

		void putRawPacket(u8 *data, u32 datasize, u16 peer_id);

		// Getters
		u32 getSize() { return m_datasize; }
		u16 getPeerId() { return m_peer_id; }
		u16 getCommand() { return m_command; }

		// Returns a c-string without copying.
		// A better name for this would be getRawString()
		char* getString(u32 from_offset);
		// major difference to putCString(): doesn't write len into the buffer
		void putRawString(const char* src, u32 len);

		NetworkPacket& operator>>(std::string& dst);
		NetworkPacket& operator<<(std::string src);

		void putLongString(std::string src);

		NetworkPacket& operator>>(std::wstring& dst);
		NetworkPacket& operator<<(std::wstring src);

		std::string readLongString();

		char getChar(u32 offset);
		NetworkPacket& operator>>(char& dst);
		NetworkPacket& operator<<(char src);

		NetworkPacket& operator>>(bool& dst);
		NetworkPacket& operator<<(bool src);

		u8 getU8(u32 offset);

		NetworkPacket& operator>>(u8& dst);
		NetworkPacket& operator<<(u8 src);

		u8* getU8Ptr(u32 offset);

		u16 getU16(u32 from_offset);
		NetworkPacket& operator>>(u16& dst);
		NetworkPacket& operator<<(u16 src);

		NetworkPacket& operator>>(u32& dst);
		NetworkPacket& operator<<(u32 src);

		NetworkPacket& operator>>(u64& dst);
		NetworkPacket& operator<<(u64 src);

		NetworkPacket& operator>>(float& dst);
		NetworkPacket& operator<<(float src);

		NetworkPacket& operator>>(v2f& dst);
		NetworkPacket& operator<<(v2f src);

		NetworkPacket& operator>>(v3f& dst);
		NetworkPacket& operator<<(v3f src);

		NetworkPacket& operator>>(s16& dst);
		NetworkPacket& operator<<(s16 src);

		NetworkPacket& operator>>(s32& dst);
		NetworkPacket& operator<<(s32 src);

		NetworkPacket& operator>>(v2s32& dst);
		NetworkPacket& operator<<(v2s32 src);

		NetworkPacket& operator>>(v3s16& dst);
		NetworkPacket& operator<<(v3s16 src);

		NetworkPacket& operator>>(v3s32& dst);
		NetworkPacket& operator<<(v3s32 src);

		NetworkPacket& operator>>(video::SColor& dst);
		NetworkPacket& operator<<(video::SColor src);

		// Temp, we remove SharedBuffer when migration finished
		Buffer<u8> oldForgePacket();
private:
		void checkReadOffset(u32 from_offset, u32 field_size);

		inline void checkDataSize(u32 field_size)
		{
			if (m_read_offset + field_size > m_datasize) {
				m_datasize = m_read_offset + field_size;
				m_data.resize(m_datasize);
			}
		}

		std::vector<u8> m_data;
		u32 m_datasize;
		u32 m_read_offset;
		u16 m_command;
		u16 m_peer_id;
};

#endif
'>543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 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
/*
Copyright (C) 2014 sapier

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 "keycode.h"
#include "settings.h"
#include "gettime.h"
#include "util/numeric.h"
#include "porting.h"
#include "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;

extern Settings *g_settings;

const char** touchgui_button_imagenames = (const char*[]) {
	"up_arrow.png",
	"down_arrow.png",
	"left_arrow.png",
	"right_arrow.png",
	"inventory_btn.png",
	"drop_btn.png",
	"jump_btn.png",
	"down.png",
	"fly_btn.png",
	"noclip_btn.png",
	"fast_btn.png",
	"debug_btn.png",
	"chat_btn.png",
	"camera_btn.png",
	"rangeview_btn.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 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 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;

TouchScreenGUI::TouchScreenGUI(IrrlichtDevice *device, IEventReceiver* receiver):
	m_device(device),
	m_guienv(device->getGUIEnvironment()),
	m_camera_yaw(0.0),
	m_camera_pitch(0.0),
	m_visible(false),
	m_move_id(-1),
	m_receiver(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_screensize = m_device->getVideoDriver()->getScreenSize();
}

void TouchScreenGUI::loadButtonTexture(button_info* btn, const char* path, rect<s32> button_rect)
{
	unsigned int tid;
	video::ITexture *texture = guiScalingImageButton(m_device->getVideoDriver(),
		m_texturesource->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"");
		}
}

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();

	loadButtonTexture(btn,touchgui_button_imagenames[id], button_rect);
}

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

void TouchScreenGUI::init(ISimpleTextureSource* tsrc, float density)
{
	assert(tsrc != 0);

	u32 control_pad_size =
			MYMIN((2 * m_screensize.Y) / 3,getMaxControlPadSize(density));

	u32 button_size      = control_pad_size / 3;
	m_visible            = true;
	m_texturesource      = tsrc;
	m_control_pad_rect   = rect<s32>(0, m_screensize.Y - 3 * button_size,
			3 * button_size, m_screensize.Y);
	/*
	draw control pad
	0 1 2
	3 4 5
	for now only 0, 1, 2, and 4 are used
	*/
	int number = 0;
	for (int y = 0; y < 2; ++y)
		for (int x = 0; x < 3; ++x, ++number) {
			rect<s32> button_rect(
					x * button_size, m_screensize.Y - button_size * (2 - y),
					(x + 1) * button_size, m_screensize.Y - button_size * (1 - y)
			);
			touch_gui_button_id id = after_last_element_id;
			std::wstring caption;
			switch (number) {
			case 0:
				id = left_id;
				caption = L"<";
				break;
			case 1:
				id = forward_id;
				caption = L"^";
				break;
			case 2:
				id = right_id;
				caption = L">";
				break;
			case 4:
				id = backward_id;
				caption = L"v";
				break;
			}
			if (id != after_last_element_id) {
				initButton(id, button_rect, caption, false);
				}
		}

	/* init inventory button */
	initButton(inventory_id,
			rect<s32>(0, m_screensize.Y - (button_size/2),
					(button_size/2), m_screensize.Y), L"inv", true);

	/* init drop button */
	initButton(drop_id,
			rect<s32>(2.5*button_size, m_screensize.Y - (button_size/2),
					3*button_size, m_screensize.Y), L"drop", true);

	/* init jump button */
	initButton(jump_id,
			rect<s32>(m_screensize.X-(1.75*button_size),
					m_screensize.Y - (0.5*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 - (0.5*button_size),
					m_screensize.X-(1.75*button_size),
					m_screensize.Y),
			L"H",false);

	/* init fly button */
	initButton(fly_id,
			rect<s32>(m_screensize.X - (0.75*button_size),
					m_screensize.Y - (2.25*button_size),
					m_screensize.X, m_screensize.Y - (button_size*1.5)),
			L"fly", false, SLOW_BUTTON_REPEAT);

	/* init noclip button */
	initButton(noclip_id,