-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.lua
More file actions
196 lines (163 loc) · 4.06 KB
/
Window.lua
File metadata and controls
196 lines (163 loc) · 4.06 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
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
191
192
193
194
195
196
Window = {
body = nil,
parent = nil,
header = nil,
gBody = nil,
gHeader = nil,
title = "",
minWidth = 0,
minHeight = 0,
priority = 0,
prefWidth = 0,
prefHeight = 0,
visible = true,
layoutId = "",
layoutIndex = 0
}
function Window:new(parent, x, y, width, height, title)
local header = nil
local gHeader = nil
if title ~= nil then
if height < 3 then
return nil
end
header = window.create(parent, x, y, width, 2);
y = y + 2
height = height - 2
gHeader = Graphics.Graphics:new(header)
end
local body = window.create(parent, x, y, width, height)
local gBody = Graphics.Graphics:new(body)
local self = {
body = body,
header = header,
gBody = gBody,
gHeader = gHeader,
title = title or "",
parent = parent
}
setmetatable(self, {
__index = Window
})
self:redraw()
return self
end
function Window:setTitle(title)
self.title = title
if self:hasTitle() == false and title ~= nil then
if self:getHeight() < 3 then
return
end
local x, y = self:getPosition()
self.header = window.create(self.parent, x, y, self:getWidth(), 2)
self.gHeader = Graphics.Graphics:new(self.header)
self.body.reposition(x, y + 2, self:getWidth(), self:getHeight() - 2)
end
if self:hasTitle() and title == nil then
local x, y = self:getPosition()
self.body.reposition(x, y, self:getWidth(), self:getHeight() + 2)
self.header = nil
self.gHeader = nil
end
self:redraw()
end
function Window:hasTitle()
return self.header ~= nil
end
function Window:inside(x, y, header)
local window = header and self.header or self.body
local posX, posY = window.getPosition()
return x >= posX and x <= posX + self:getWidth() and y >= posY and y <= posY + self:getHeight()
end
function Window:setLayoutId(id)
self.layoutId = id
end
function Window:getLayoutId()
return self.layoutId
end
function Window:getLayoutIndex()
return self.layoutIndex;
end
function Window:setLayoutIndex(index)
self.layoutIndex = index;
end
function Window:setVisible(visible)
self.visible = visible;
self.body.setVisible(visible);
self.header.setVisible(visible);
end
function Window:isVisible()
return self.visible;
end
function Window:setMinSize(width, height)
self.minWidth = width
self.minHeight = height
end
function Window:setLayoutPriority(priority)
self.priority = priority
end
function Window:setPrefSize(width, height)
self.prefHeight = height
self.prefWidth = width
end
function Window:getWidth()
local w, _ = self.body.getSize()
return w
end
function Window:getHeight()
local _, h = self.body.getSize()
if self:hasTitle() then
h = h + 2
end
return h
end
function Window:getPrefWidth()
return self.prefWidth
end
function Window:getPrefHeight()
return self.prefHeight
end
function Window:getMinWidth()
return self.minWidth
end
function Window:getMinHeight()
return self.minHeight
end
function Window:getLayoutPriority()
return self.priority
end
function Window:redraw()
if self:hasTitle() then
local g = self.gHeader;
g:fillBox(1, 1, g:getWidth(), 1, "0");
g:write(1, 1, self.title, "f", "0")
g:write(1, 2, string.rep("-", g:getWidth()))
self.header.redraw()
end
self.body.redraw()
end
function Window:reposition(x, y, width, height)
if self:hasTitle() then
if height < 3 then
return false
end
self.header.reposition(x, y, width, 2)
y = y + 2
height = height - 2
end
self.body.reposition(x, y, width, height)
self.gBody:updateSize()
self.gHeader:updateSize()
self:redraw()
return true
end
function Window:getPosition()
if self:hasTitle() then
return self.header.getPosition()
else
return self.body.getPosition()
end
end
function Window:getGraphics()
return self.gBody
end