summaryrefslogtreecommitdiff
path: root/gfx/gl/bufferedLocation.h
blob: 87b957fc170390701b57fce80106120927ae2ce4 (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
#pragma once

#include "location.h"
#include <functional>
#include <glm/mat4x4.hpp>
#include <glm/vec3.hpp>
#include <utility>

class BufferedLocation {
public:
	BufferedLocation(GlobalPosition3D = {}, Rotation3D = {});
	BufferedLocation(const Location &);
	virtual ~BufferedLocation() = default;

	BufferedLocation & operator=(const Location &);

	operator const Location &() const;

	[[nodiscard]] GlobalPosition3D position() const;
	[[nodiscard]] Rotation3D rotation() const;
	void setPosition(GlobalPosition3D, bool update = true);
	void setRotation(Rotation3D, bool update = true);
	void setLocation(GlobalPosition3D, Rotation3D);

	[[nodiscard]] glm::mat4 getRotationTransform() const;

private:
	virtual void updateBuffer() const = 0;

	Location loc;
};

class BufferedLocationUpdater : public BufferedLocation {
public:
	template<typename... LocationArgs>
	BufferedLocationUpdater(std::function<void(const BufferedLocation *)> onUpdate, LocationArgs &&... t) :
		BufferedLocation {std::forward<LocationArgs>(t)...}, onUpdate {std::move(onUpdate)}
	{
		updateBuffer();
	}

	using BufferedLocation::operator=;

private:
	void updateBuffer() const override;

	std::function<void(const BufferedLocation *)> onUpdate;
};