Repository navigation
Expand file tree
/
Copy pathrectField.lua
More file actions
130 lines (112 loc) · 3 KB
/
Copy pathrectField.lua
File metadata and controls
130 lines (112 loc) · 3 KB
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
local GC=GC
---@class Techmino.RectField
---@field _width number
---@field _matrix Mat<Techmino.Brik.Cell | false>
local F={}
--------------------------------------------------------------
-- Methods
function F:removeLine(h)
table.remove(self._matrix,h)
end
function F:fresh()
for y=#self._matrix,1,-1 do
for i=1,#self._matrix[y] do
if self._matrix[y][i] then return end
end
self:removeLine(y)
end
end
function F:export_table_simp()
local t={}
for y=1,#self._matrix do
t[y]={}
for x=1,self._width do
t[y][x]=self._matrix[y][x] and true or false
end
end
return t
end
function F:export_string_simp()
local buf=STRING.newBuf((self._width+1)*#self._matrix)
for y=1,#self._matrix do
for x=1,self._width do
buf:put(self._matrix[y][x] and 'x' or '_')
end
buf:put('/')
end
return buf:get()
end
function F:export_string_color()
local buf=STRING.newBuf((self._width*2+1)*#self._matrix)
for y=1,#self._matrix do
for x=1,self._width do
buf:put(STRING.toHex(self._matrix[y][x].color,2))
end
buf:put('/')
end
return buf:get()
end
function F:export_fumen()
local str=''
-- Anyone interested? I don't
error("Not implemented yet")
return str
end
function F:drawThumbnail_simp(step,size)
if not step then step=40 end
if not size then size=step end
local f=self._matrix
for y=1,#f do
for x=1,self._width do
local c=f[y][x]
if c then
GC.rectangle('fill',(x-1)*step,-y*step,size,size)
end
end
end
end
function F:drawThumbnail_color(step,size)
if not step then step=40 end
if not size then size=step end
local f=self._matrix
for y=1,#f do
for x=1,self._width do
local c=f[y][x]
if c then
GC.setColor(RGB9[c.color])
GC.rectangle('fill',(x-1)*step,-y*step,size,size)
end
end
end
end
---@return number
function F:getHeight()
return #self._matrix
end
local wallCell=setmetatable({},{__newIndex=NULL,__metatable=true})
---@return Techmino.Brik.Cell | false
function F:getCell(x,y)
if x<=0 or x>self._width or y<=0 then return wallCell end
if y>#self._matrix then return false end
local line=rawget(self._matrix,y)
return line and line[x] or false
end
function F:setCell(cell,x,y)
if not self._matrix[y] then
if y>1260 then return end -- Too high, do nothing
for i=#self._matrix+1,y do
self._matrix[i]=TABLE.new(false,self._width)
end
end
self._matrix[y][x]=cell
end
--------------------------------------------------------------
-- Builder
function F.new(width)
assert(type(width)=='number',"[Field].new(width): Need number")
local f=setmetatable({_width=width},{__index=F,__metatable=true})
f._matrix={}
return f
end
--------------------------------------------------------------
return F