· 6 years ago · Sep 20, 2019, 11:32 PM
1-- Minex dialogs api
2
3termWidth, termHeight = term.getSize()
4local termHalfWidth = math.floor(termWidth / 2)
5local termHalfHeight = math.floor(termHeight / 2)
6
7-- Character table.
8-- If you have a better way of doing this, lemme know.
9local keyboard = {
10 [30] = 'a',
11 [48] = 'b',
12 [46] = 'c',
13 [32] = 'd',
14 [18] = 'e',
15 [33] = 'f',
16 [34] = 'g',
17 [35] = 'h',
18 [23] = 'i',
19 [36] = 'j',
20 [37] = 'k',
21 [38] = 'l',
22 [50] = 'm',
23 [49] = 'n',
24 [24] = 'o',
25 [25] = 'p',
26 [16] = 'q',
27 [19] = 'r',
28 [31] = 's',
29 [20] = 't',
30 [22] = 'u',
31 [47] = 'v',
32 [17] = 'w',
33 [45] = 'x',
34 [21] = 'y',
35 [44] = 'z',
36 [2] = '1',
37 [3] = '2',
38 [4] = '3',
39 [5] = '4',
40 [6] = '5',
41 [7] = '6',
42 [8] = '7',
43 [9] = '8',
44 [10] = '9',
45 [11] = '0',
46 [57] = ' '
47}
48
49-- Self explanatory, this function prints text centered on the screen.
50-- Or at least, it tries to.
51function centeredText(y, text)
52 local textLength = string.len(text)
53 local cursorXPos = termHalfWidth - math.floor(textLength / 2)
54 term.setCursorPos(cursorXPos, y)
55 term.write(text)
56end
57
58-- A standard dialog screen.
59function notice(text, colorSchema)
60 -- Set background color.
61 paintutils.drawFilledBox(1, 2, termWidth, termHeight, colorSchema.bgColor)
62 term.setBackgroundColor(colorSchema.bgColor)
63 term.setTextColor(colorSchema.fgColor)
64 centeredText(termHalfHeight, text)
65
66 --draw buttons.
67 local buttonEnd = (termHalfWidth + 3)
68 local buttonStart = (termHalfWidth - 2)
69
70 local yPos = (termHalfHeight + 2)
71
72 paintutils.drawLine(buttonStart, yPos, buttonEnd, yPos, colorSchema.btColor)
73 term.setCursorPos(buttonStart+1, yPos)
74 term.write("Okay")
75
76 --wait for input.
77 while true do
78 -- sets multiple variables using os.pullEvent, if the mouse if clicked.
79 local event, button, x, y = os.pullEvent("mouse_click")
80 -- Basic logic to check which button has been pressed.
81 if y == yPos then
82 if x >= buttonStart and x <= buttonEnd then
83 break
84 end
85 end
86 end
87end
88
89-- A y/n dialog screen.
90function truth(text, colorSchema)
91 -- Set background color.
92 paintutils.drawFilledBox(1, 2, termWidth, termHeight, colorSchema.bgColor)
93 term.setBackgroundColor(colorSchema.bgColor)
94 term.setTextColor(colorSchema.fgColor)
95 centeredText(termHalfHeight, text)
96
97 --draw buttons.
98 local yesEnd = (termHalfWidth - 1)
99 local yesStart = (yesEnd - 4)
100 local noStart = (termHalfWidth + 1)
101 local noEnd = (noStart + 3)
102 local yPos = (termHalfHeight + 2)
103
104 paintutils.drawLine(yesStart, yPos, yesEnd, yPos, colorSchema.btColor)
105 paintutils.drawLine(noStart, yPos, noEnd, yPos, colorSchema.btColor)
106 term.setCursorPos(yesStart+1, yPos)
107 term.write("yes")
108
109 term.setCursorPos(noStart+1, yPos)
110 term.write("no")
111
112 --wait for input.
113 while true do
114 -- sets multiple variables using os.pullEvent, if the mouse is clicked.
115 local event, button, x, y = os.pullEvent("mouse_click")
116
117 -- Basic logic to check which button has been pressed.
118 if y == yPos then
119 if x >= yesStart and x <= yesEnd then
120 return true
121 elseif x >= noStart and x <= noEnd then
122 return false
123 end
124 end
125 end
126end
127
128-- A textfield dialog screen.
129function text(text, colorSchema)
130 -- Set background color.
131 paintutils.drawFilledBox(1, 2, termWidth, termHeight, colorSchema.bgColor)
132 term.setBackgroundColor(colorSchema.bgColor)
133 term.setTextColor(colorSchema.fgColor)
134 centeredText(termHalfHeight, text)
135
136 --draw buttons.
137 local buttonEnd = (termHalfWidth + 3)
138 local buttonStart = (termHalfWidth - 2)
139
140 local yPos = (termHalfHeight + 5)
141
142 paintutils.drawLine(1, yPos - 2, termWidth, yPos - 2, colorSchema.btColor)
143 paintutils.drawLine(buttonStart, yPos, buttonEnd, yPos, colorSchema.btColor)
144 term.setCursorPos(buttonStart+1, yPos)
145 term.write("Okay")
146
147 --wait for input.
148 local content = ""
149 while true do
150 -- sets multiple variables using os.pullEvent, if key is pressed.
151 local event, key, mouseX, mouseY = os.pullEvent()
152 -- return stored value when enter key pressed
153 if event == "key" then
154 if key == keys.enter then
155 return content
156 -- Otherwise, concatenate to the value.
157 elseif keyboard[key] ~= nil and string.len(content) < termWidth - 2 then
158 content = content .. keyboard[key]
159 -- display output.
160 term.setCursorPos(2, yPos - 2)
161 term.write(content)
162 end
163 end
164
165 -- check if button clicked
166 -- Basic logic to check which button has been pressed.
167 if event == "mouse_click" then
168 if mouseY == yPos then
169 if mouseX >= buttonStart and mouseX <= buttonEnd then
170 return content
171 end
172 end
173 end
174 end
175end