summaryrefslogtreecommitdiff
path: root/tools/editor/icons/SCsub
blob: f2f5dcca48d65999a7e61ce2bda8dcf0ff727b5f (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
Import('env')

def make_editor_icons_action(target, source, env):

	import os
	import cStringIO

	dst = target[0].srcnode().abspath
	pixmaps = source

	s = cStringIO.StringIO()

	s.write("#include \"editor_icons.h\"\n\n")
 	s.write("#include \"editor_scale.h\"\n\n")
	s.write("#include \"scene/resources/theme.h\"\n\n")

	hidpi_list=[]

	for x in pixmaps:

		x=str(x)
		var_str=os.path.basename(x)[:-4]+"_png";
		#print(var_str)

		s.write("static const unsigned char "+ var_str +"[]={\n");

		pngf=open(x,"rb");

		b=pngf.read(1);
		while(len(b)==1):
			s.write(hex(ord(b)))
			b=pngf.read(1);
			if (len(b)==1):
				s.write(",")

		s.write("\n};\n\n");

		pngf.close();
		var_str=os.path.basename(x)[:-4]+"_hidpi_png";
#print("TRY OPEN: "+os.path.dirname(x)+"/2x/"+os.path.basename(x)+"\n")
		try:

			pngf = open(os.path.dirname(x)+"/2x/"+os.path.basename(x))

			#print(var_str)

			s.write("static const unsigned char "+ var_str +"[]={\n");

			b=pngf.read(1);
			while(len(b)==1):
				s.write(hex(ord(b)))
				b=pngf.read(1);
				if (len(b)==1):
					s.write(",")

			s.write("\n};\n\n\n");
			hidpi_list.append(x)

		except:
			s.write("static const unsigned char* "+ var_str +"=NULL;\n\n\n");




	s.write("static Ref<ImageTexture> make_icon(const uint8_t* p_png,const uint8_t* p_hidpi_png) {\n")
	s.write("\tRef<ImageTexture> texture( memnew( ImageTexture ) );\n")
	s.write("\tImage img((editor_is_hidpi()&&p_hidpi_png)?p_hidpi_png:p_png);\n")
	s.write("\tif (editor_is_hidpi() && !p_hidpi_png) { img.convert(Image::FORMAT_RGBA); img.expand_x2_hq2x(); }\n")
	s.write("\ttexture->create_from_image( img,ImageTexture::FLAG_FILTER );\n")
	s.write("\treturn texture;\n")
	s.write("}\n\n")

	s.write("void editor_register_icons(Ref<Theme> p_theme) {\n\n")

	for x in pixmaps:

		x=os.path.basename(str(x))
		type=x[5:-4].title().replace("_","");
		var_str=x[:-4]+"_png";
		var_str_hidpi=x[:-4]+"_hidpi_png";
		s.write("\tp_theme->set_icon(\""+type+"\",\"EditorIcons\",make_icon("+var_str+","+var_str_hidpi+"));\n");

	s.write("\n\n}\n\n");

	f = open(dst,"wb")
	f.write(s.getvalue())
	f.close()
	s.close()

make_editor_icons_builder = Builder(action=make_editor_icons_action,
							suffix = '.cpp',
							src_suffix = '.png')
env['BUILDERS']['MakeEditorIconsBuilder']=make_editor_icons_builder
env.Alias('editor_icons',[env.MakeEditorIconsBuilder('#tools/editor/editor_icons.cpp',Glob("*.png"))])

env.tool_sources.append("#tools/editor/editor_icons.cpp")
Export('env')