File: shaderlib.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (122 lines) | stat: -rw-r--r-- 2,609 bytes parent folder | download | duplicates (3)
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
#pragma once

#include "string/string.h"
#include "character.h"
#include "ishaders.h"
#include "ibrush.h"
#include "iundo.h"
#include "ipatch.h"
#include "iselection.h"
#include "scene/Traverse.h"
#include "gamelib.h"

inline bool shader_equal(const std::string& shader, const std::string& other)
{
  return string_equal_nocase(shader.c_str(), other.c_str());
}

inline bool shader_valid(const char* shader)
{
  return string_is_ascii(shader)
    && strchr(shader, ' ') == 0
    && strchr(shader, '\n') == 0
    && strchr(shader, '\r') == 0
    && strchr(shader, '\t') == 0
    && strchr(shader, '\v') == 0
    && strchr(shader, '\\') == 0;
}

inline const char* GlobalTexturePrefix_get()
{
  return GlobalMaterialManager().getTexturePrefix();
}

inline const char* shader_get_textureName(const char* name)
{
  return name + std::strlen(GlobalTexturePrefix_get());
}

inline const std::string& texdef_name_default()
{
	static std::string _default = game::current::getValue<std::string>("/defaults/defaultTexture", "_default");
	return _default;
}

namespace scene
{

/**
 * greebo: This replaces the shader of the visited face/patch with <replace>
 * if the face is textured with <find> and increases the given <counter>.
 */
class ShaderReplacer
{
private:
	const std::string _find;
	const std::string _replace;
	int _counter;

public:
	ShaderReplacer(const std::string& find, const std::string& replace) :
		_find(find),
		_replace(replace),
		_counter(0)
	{}

	int getReplacedCount() const
	{
		return _counter;
	}

	void operator()(IFace& face)
	{
		if (face.getShader() == _find)
		{
			face.setShader(_replace);
			_counter++;
		}
	}

	void operator()(IPatch& patch)
	{
		if (patch.getShader() == _find)
		{
			patch.setShader(_replace);
			_counter++;
		}
	}

	void operator()(const IPatchNodePtr& node)
	{
		(*this)(node->getPatch());
	}
};

inline int findAndReplaceShader(const std::string& find, const std::string& replace, bool selectedOnly)
{
	std::string command("textureFindReplace");
	command += "-find " + find + " -replace " + replace;
	UndoableCommand undo(command);

	// Construct a visitor class
	ShaderReplacer replacer(find, replace);

	if (selectedOnly)
	{
		if (GlobalSelectionSystem().getSelectionMode() != selection::SelectionMode::Component)
		{
			// Find & replace all the brush and patch shaders
			GlobalSelectionSystem().foreachFace(std::ref(replacer));
			GlobalSelectionSystem().foreachPatch(std::ref(replacer));
		}
	}
	else
	{
		scene::foreachVisibleFace(std::ref(replacer));
		scene::foreachVisiblePatch(std::ref(replacer));
	}

	return replacer.getReplacedCount();
}

}