summaryrefslogtreecommitdiffstats
path: root/src/UI/WindowOwner.h
blob: 4dc3cf080b80eaab2a5eb14ed32fed5bda0781aa (plain) (blame)
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
#pragma once

#include "../BlockEntities/BlockEntity.h"
#include "../Entities/Entity.h"
#include "Window.h"

/*
Being a descendant of cWindowOwner means that the class can own one window. That window can be
queried, opened by other players, closed by players and finally destroyed.
Also, a cWindowOwner can be queried for the block coords where the window is displayed. That will be used
for entities / players in motion to close their windows when they get too far away from the window "source".
*/





/**
Base class for the window owning
*/
class cWindowOwner
{
public:
	cWindowOwner() :
		m_Window(nullptr)
	{
	}

	virtual ~cWindowOwner()
	{
	}

	void CloseWindow(void)
	{
		m_Window = nullptr;
	}

	void OpenWindow(cWindow * a_Window)
	{
		m_Window = a_Window;
		m_Window->SetOwner(this);
	}

	cWindow * GetWindow(void) const
	{
		return m_Window;
	}

	/// Returns the block position at which the element owning the window is
	virtual Vector3i GetBlockPos(void) = 0;

private:
	cWindow * m_Window;
};





/**
Window owner that is associated with a block entity (chest, furnace, ...)
*/
class cBlockEntityWindowOwner :
	public cWindowOwner
{
public:
	cBlockEntityWindowOwner(cBlockEntity * a_BlockEntity) :
		m_BlockEntity(a_BlockEntity)
	{
	}

	virtual Vector3i GetBlockPos(void) override
	{
		return Vector3i(m_BlockEntity->GetPosX(), m_BlockEntity->GetPosY(), m_BlockEntity->GetPosZ());
	}

private:
	cBlockEntity * m_BlockEntity;
};





/**
Window owner that is associated with an entity (chest minecart)
*/
class cEntityWindowOwner :
	public cWindowOwner
{
public:
	cEntityWindowOwner(cEntity * a_Entity) :
		m_Entity(a_Entity)
	{
	}

	virtual Vector3i GetBlockPos(void) override
	{
		return m_Entity->GetPosition().Floor();
	}

private:
	cEntity * m_Entity;
};