-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsa_handler.lua
169 lines (151 loc) · 4.31 KB
/
tsa_handler.lua
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
TSA = {}
TSA.__index = TSA
TSA.ids = {}
-- Reads tileset information from the rom
-- Todo: tile_layout_locations should be moved into the class
function TSA:read_tsa(idx)
local loc = get_ts_info("absolute_address", idx)
local tileset = {}
for j=0, 255 do
tileset[j + 1] = {
rom.readbyte(loc + j),
rom.readbyte(loc + j + 0x100),
rom.readbyte(loc + j + 0x200),
rom.readbyte(loc + j + 0x300)
}
end
return tileset
end
-- Quick load for all the tilesets we have
function TSA:load_tilesets()
local tilesets = {}
for i in pairs(tilesetz) do
tilesets[i] = self:read_tsa(i)
end
return tilesets
end
function TSA:get_current_pal()
return self.palette[cur_pal]
end
-- Forms a 16x16 block from an idx
function TSA:load_tsa_img(tiln)
return iup.image{
width=16,
height=16,
pixels=merge_tiles(
self.chr_handler:get_iup_img_from_pages(self.tileset[cur_tsa][tiln][1], cur_tsa),
self.chr_handler:get_iup_img_from_pages(self.tileset[cur_tsa][tiln][2], cur_tsa),
self.chr_handler:get_iup_img_from_pages(self.tileset[cur_tsa][tiln][3], cur_tsa),
self.chr_handler:get_iup_img_from_pages(self.tileset[cur_tsa][tiln][4], cur_tsa),
8
),
colors=get_attribute_palette(self:get_current_pal(), math.floor((tiln - 1) / 0x40) + 1)
}
end
-- Returns the current tile that should be displayed in the tsa
function TSA:get_tile(tiln, n, tile)
if not tile then
tile = cur_tsa
end
return self.tileset[tile][tiln][n]
end
-- Loads the tsa for the first time
-- Todo: Should use the TSA:reload() instead of the code here for readability
function TSA:initilize_gui()
local tiles, hboxes = {}, {}
local vbox
for i=0, 15 do
local hbox = {}
for j=1, 16 do
local til = self:load_tsa_img(j + i * 16)
local tile = iup.button{
image=til,
impress=til,
impressboarder="no",
rastersize="16x16"
}
tile.action = "TSA.ids[".. self.id .."]:btn_act(".. j + i * 16 ..")"
table.insert(tiles, tile)
hbox[j] = tile
end
hbox["margin"] = "0x0"
hboxes[i + 1] = iup.hbox(hbox)
end
vbox = iup.vbox(hboxes)
return tiles, hboxes, vbox
end
function TSA:update_pal()
return get_ts_info("palettes", cur_tsa)
end
-- Creates the tsa and sets it up for use
function TSA:create(chr_handler)
local tsa = {}
setmetatable(tsa, TSA)
table.insert(TSA.ids, tsa)
self.id = tablelength(TSA.ids)
self.chr_handler = chr_handler
self.tileset = self:load_tilesets()
self.palette = self:update_pal()
self.tiles, self.hboxes, self.vbox = self:initilize_gui()
self.tcnt = nil
self.tactt_gui = nil
self.attr = nil
return tsa
end
-- Reloads a tile in the TSA
function TSA:reload_tile(tiln)
self.tiles[tiln]["image"] = self:load_tsa_img(tiln)
iup.Update(self.tiles[tiln])
end
-- Reloads the entire TSA
function TSA:reload()
self.attr:set_idx(math.max(cur_tsa - 1, 0) * 4 + math.floor(current_tile / 0x40))
self.attr:update_gui()
self.palette = self:update_pal()
for i=1, 256 do
self:reload_tile(i)
end
self:update_tile_attribute_names()
end
function TSA:update_tile_attribute_names()
if cur_tsa == 1 then
tile_attribute_labels[1].title = "Solid "
iup.Update(tile_attribute_labels[1])
tile_attribute_labels[2].title = "Can Enter "
iup.Update(tile_attribute_labels[2])
tile_attribute_labels[3].title = "nil "
iup.Update(tile_attribute_labels[3])
tile_attribute_labels[4].title = "nil "
iup.Update(tile_attribute_labels[4])
else
tile_attribute_labels[1].title = "Air "
iup.Update(tile_attribute_labels[1])
tile_attribute_labels[2].title = "Water "
iup.Update(tile_attribute_labels[2])
tile_attribute_labels[3].title = "Semisolid "
iup.Update(tile_attribute_labels[3])
tile_attribute_labels[4].title = "Solid "
iup.Update(tile_attribute_labels[4])
end
end
function TSA:set_tile(tiln, n, new)
self.tileset[cur_tsa][tiln][n] = new
self:reload_tile(tiln)
end
function TSA:set_tcnt(tcnt)
self.tcnt = tcnt
end
function TSA:set_current_tile_gui(tactt_gui)
self.tactt_gui = tactt_gui
end
function TSA:set_tile_attr(attr)
self.attr = attr
end
function TSA:btn_act(idx)
self.tcnt:set_tsa_idx(idx)
current_tile = idx
self.tactt_gui.title = "tile: ".. dec_to_hex_byte(current_tile - 1)
self.attr:set_idx(math.max(cur_tsa - 1, 0) * 4 + math.floor(current_tile / 0x40))
self.attr:set_current_tile(current_tile)
self.attr:update_gui()
end