-
Notifications
You must be signed in to change notification settings - Fork 0
/
TexturePropertyEditor.py
190 lines (153 loc) · 5.55 KB
/
TexturePropertyEditor.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
__author__ = 'Charles-Jianye Chen'
import os
import time
import pygame
import wapisp
pygame.init()
import rgine as rgine
_ok = rgine.surface_buffer.read_buffer("ok", 32, 32)
_cross = rgine.surface_buffer.read_buffer("cross", 32, 32)
def getTerrainByPos(terrain, mouseX, mouseY, shiftX, shiftY):
a, b = terrain.getTerrainByRelativeRect((mouseX-shiftX, mouseY-shiftY, 0, 0))
return a[0], b[0]
def test(property_byte, digit):
return rgine.byte2bool(bytes([property_byte]))[digit]
screen = pygame.display.set_mode((16*2*32, 9*2*32))
terrainFilename = os.path.join("maps","1399326333.terrain")
textureFilename = "myTexture.texture"
textureSize = 32
terrain = rgine.Terrain("w", 0, 0, textureSize, textureSize)
terrain.readTerrain(terrainFilename)
terrain.readTextureFromSurface(pygame.image.load(os.path.join("maps","1399326333.jpeg")).convert_alpha())
##terrain.readTextureFromFile(textureFilename)
textureInfo = \
{
"width": terrain.width,
"height": terrain.height,
}
evt = rgine.Event()
wm = rgine.windows.WindowsManager()
wmbuttons = {}
wmacro = rgine.windows.WindowsMacros()
buttons = ["Open", "Save", "[0]", "[1]", "[2]", "[3]", "[4]", "[5]", "[6]", "[7]"]
for i in range(len(buttons)):
t = wm.CreateWindow(wmacro.WC_BUTTON, ((158//2, 59//2), rgine.windows._button,buttons[i]))
wm.MoveWindow(t, 158//2*i, 0)
wmbuttons[t] = buttons[i]
wm.SetTopmost(-1, False)
##screen = pygame.display.set_mode((16*50, 9*50))
hPygameWindow = pygame.display.get_wm_info()["window"]
world = rgine.TerrainWorld(textureInfo["width"]*textureSize, textureInfo["height"]*textureSize)
world.setTextureFormat(textureSize, textureSize)
wOffset = (0, 32)
world.setProjectionSize(16*2*32-wOffset[0], 9*2*32-wOffset[1])
wRect = pygame.Rect(*(wOffset+world.getSize()))
changed = True
CurrentDigit = 0
while True:
evt.update()
if evt.type == pygame.QUIT:
break
screen.fill((0, 0, 0))
world.new()
rgx, rgy = world.getTerrainRange()
terrain.render_s(world.getSurface(), world.getShift(), range(*rgx), range(*rgy))
screen.blit(world.render_s(), wOffset)
for x in range(*rgx):
for y in range(*rgy):
px, py, w, h = terrain.getRelativeClientRect(x, y)
px += wOffset[0]
py += wOffset[1]
px -= world.getShift()[0]
py -= world.getShift()[1]
t = terrain.getProperty_s(x, y)
if t is None: continue
if test(t, CurrentDigit):
screen.blit(_ok, (px, py))
else:
screen.blit(_cross, (px, py))
if evt.isMouseHit(evt.MOUSE_LEFT):
if wRect.collidepoint(evt.getMousePos()):
wsx, wsy = world.getShift()
x, y = getTerrainByPos(*[terrain]+[evt.getMousePos()[0]+wsx, evt.getMousePos()[1]+wsy]+list(wOffset))
t = terrain.getProperty_s(x, y)
if t is not None:
li = rgine.int2bool(t)
li[CurrentDigit] = not li[CurrentDigit]
terrain.setProperty(x, y, rgine.bool2int(li))
if evt.isMouseHit(evt.MOUSE_SCROLL_UP) and evt.isKeyDown(pygame.K_LSHIFT):
if wRect.collidepoint(evt.getMousePos()):
world.shiftH(-textureSize)
elif evt.isMouseHit(evt.MOUSE_SCROLL_UP):
if wRect.collidepoint(evt.getMousePos()):
world.shiftV(-textureSize)
if evt.isMouseHit(evt.MOUSE_SCROLL_DOWN) and evt.isKeyDown(pygame.K_LSHIFT):
if wRect.collidepoint(evt.getMousePos()):
world.shiftH(textureSize)
elif evt.isMouseHit(evt.MOUSE_SCROLL_DOWN):
if wRect.collidepoint(evt.getMousePos()):
world.shiftV(textureSize)
for hWnd, msg, surface, pos in wm.DispatchMessage(evt):
screen.blit(surface, pos)
if hWnd in wmbuttons and msg == wmacro.HIT:
wm.SetTopmost(-1, False)
if wmbuttons[hWnd] == "Open":
strfilter = ".texture file\x00*.texture\x00.jpeg image file\x00*.jpeg\x00\x00"
wapisp.MessageBox(hPygameWindow, "Terrain file first, then .texture/.jpeg file, and optional textureProperty file. ", wmbuttons[hWnd])
result, fname = wapisp.GetOpenFileName(hPygameWindow)
if not result:
continue
else:
terrainfname = fname
result, fname = wapisp.GetOpenFileName(hPygameWindow, strfilter)
if not result:
continue
else:
texturefname = fname
terrain.readTerrain(terrainfname)
if fname.split(".")[-1].lower() == "jpeg":
surf = pygame.image.load(fname)
surf.convert_alpha()
terrain.readTextureFromSurface(surf)
elif fname.split(".")[-1].lower() == "texture":
terrain.readTextureFromFile(texturefname)
else:
raise ValueError(fname)
result, fname = wapisp.GetOpenFileName(hPygameWindow)
if not result:
pass
else:
terrain.readTextureProperty(fname)
terrain.render_check()
world.resize(terrain.width*terrain.textureW, terrain.height*terrain.textureH)
sx = terrain.width*terrain.textureW
sy = terrain.height*terrain.textureH
world.resize(sx, sy)
if sx > world.getRect()[2]:
sx = world.getRect()[2]
if sy > world.getRect()[3]:
sy = world.getRect()[3]
world.setProjectionSize(sx, sy)
terrain.render(world.getSurface())
wRect = pygame.Rect(*(wOffset+world.getSize()))
elif wmbuttons[hWnd] == "Save":
fname = terrain.writeTextureProperty(str(int(time.time()))+".textureProperty")
wapisp.MessageBox(hPygameWindow, "Done. Filename is %s"%fname, wmbuttons[hWnd])
elif wmbuttons[hWnd] == "[0]":
CurrentDigit = 0
elif wmbuttons[hWnd] == "[1]":
CurrentDigit = 1
elif wmbuttons[hWnd] == "[2]":
CurrentDigit = 2
elif wmbuttons[hWnd] == "[3]":
CurrentDigit = 3
elif wmbuttons[hWnd] == "[4]":
CurrentDigit = 4
elif wmbuttons[hWnd] == "[5]":
CurrentDigit = 5
elif wmbuttons[hWnd] == "[6]":
CurrentDigit = 6
elif wmbuttons[hWnd] == "[7]":
CurrentDigit = 7
pygame.display.flip()
pygame.quit()