SHOW:
|
|
- or go back to the newest paste.
1 | - | local function fileGen() |
1 | + | os.loadAPI('mcPaintFolder/screenAPI.lua') |
2 | - | if not fs.exists('mcPaintFolder/info.txt') then |
2 | + | local mon = '' |
3 | - | term.clear() |
3 | + | local event = '' |
4 | - | term.write('Enter monitor side: ') |
4 | + | mon, isMonitor = screenAPI.wrapMon() |
5 | - | side = io.read() |
5 | + | |
6 | - | if peripheral.getType(side) ~= 'monitor' then |
6 | + | if not isMonitor then |
7 | - | term.write('No monitor on this side!') |
7 | + | mon = term |
8 | - | fileGen() |
8 | + | eventClick = 'mouse_click' |
9 | - | else |
9 | + | eventDrag = 'mouse_drag' |
10 | - | term.write('Data saved!') |
10 | + | else |
11 | - | info = fs.open('mcPaintFolder/info.txt', 'w') |
11 | + | mon = screenAPI.wrapMon() |
12 | - | info.write(side) |
12 | + | mon.setTextScale(0.5) |
13 | - | info.close() |
13 | + | eventClick = 'monitor_touch' |
14 | - | os.sleep(2) |
14 | + | |
15 | ||
16 | - | end |
16 | + | |
17 | mon.clear() | |
18 | local x, y = 1, 1 | |
19 | - | fileGen() |
19 | + | local length, height = mon.getSize() |
20 | - | info = fs.open('mcPaintFolder/info.txt', 'r') |
20 | + | |
21 | - | side = info.readAll() |
21 | + | |
22 | local imageTable = {} | |
23 | - | local mon = peripheral.wrap(side) |
23 | + | |
24 | - | mon.setTextScale(0.5) |
24 | + | |
25 | local lineMode = false | |
26 | ||
27 | local function createImageFile(color) -- creates a table of the form imageTable[x][y] = black as default | |
28 | - | local length, width = mon.getSize() |
28 | + | |
29 | imageTable[x] = {} | |
30 | for y = 1, height do | |
31 | imageTable[x][y] = color | |
32 | end | |
33 | end | |
34 | end | |
35 | ||
36 | local function drawPalette() -- Draws up the GUI | |
37 | local color = 0 | |
38 | - | for y = 1, width do |
38 | + | |
39 | color = 2^(i-1) | |
40 | colorTable[i] = color | |
41 | mon.setBackgroundColor(color) | |
42 | mon.setCursorPos(length, i) | |
43 | mon.write(' ') | |
44 | end | |
45 | mon.setCursorPos(length, 17) | |
46 | mon.write('E') | |
47 | mon.setCursorPos(length, 18) | |
48 | mon.write('B') | |
49 | mon.setCursorPos(length, 19) | |
50 | mon.write('L') | |
51 | mon.setCursorPos(length, 20) | |
52 | mon.write('S') | |
53 | mon.setCursorPos(length, 21) | |
54 | mon.write('X') | |
55 | end | |
56 | ||
57 | function infoTrans(table1, table2) --Transfers the information from the various screenAPI functions to the table we want, in this case imageTable | |
58 | for i, v in pairs(table1) do | |
59 | for k, u in pairs(table1[i]) do | |
60 | table2[i][k] = table1[i][k] | |
61 | end | |
62 | end | |
63 | end | |
64 | - | if not boxModeToggled then |
64 | + | |
65 | - | if not eraseMode then |
65 | + | |
66 | - | imageTable[x][y] = currentColor |
66 | + | if boxModeToggled then |
67 | - | mon.setCursorPos(x,y) |
67 | + | local _, _, x2, y2 = os.pullEvent(eventClick) |
68 | - | mon.write(' ') |
68 | + | if x2 ~= length then |
69 | - | else |
69 | + | boxTable = screenAPI.drawBox(x, y, x2, y2, currentColor) |
70 | - | imageTable[x][y] = currentColor |
70 | + | infoTrans(boxTable, imageTable) |
71 | - | mon.setCursorPos(x,y) |
71 | + | |
72 | - | mon.write(' ') |
72 | + | elseif lineModeToggled then |
73 | local _, _, x2, y2 = os.pullEvent(eventClick) | |
74 | if x2 ~= length then | |
75 | - | local _, _, x2, y2 = os.pullEvent('monitor_touch') |
75 | + | lineTable = screenAPI.drawLine(x, y, x2, y2, currentColor) |
76 | - | local xinc, yinc = 1, 1 |
76 | + | infoTrans(lineTable, imageTable) |
77 | - | if x < x2 then |
77 | + | |
78 | - | xinc = 1 |
78 | + | |
79 | - | else |
79 | + | imageTable[x][y] = currentColor |
80 | - | xinc = -1 |
80 | + | mon.setCursorPos(x,y) |
81 | mon.write(' ') | |
82 | - | if y < y2 then |
82 | + | |
83 | - | yinc = 1 |
83 | + | |
84 | - | else |
84 | + | |
85 | - | yinc = -1 |
85 | + | |
86 | mon.setBackgroundColor(2^15) | |
87 | - | for i = x, x2, xinc do |
87 | + | mon.clear() |
88 | - | for k = y, y2, yinc do |
88 | + | mon.setCursorPos(1,1) |
89 | - | imageTable[i][k] = currentColor |
89 | + | write('Enter your file\'s name: \n\n') |
90 | - | mon.setCursorPos(i,k) |
90 | + | for i= 1, #fs.list('mcPaintFolder/images/') do |
91 | - | mon.write(' ') |
91 | + | write(fs.list('mcPaintFolder/images')[i]..'\n') |
92 | end | |
93 | mon.setCursorPos(1,2) | |
94 | - | end |
94 | + | |
95 | if not fs.exists('mcPaintFolder/images/'..fileName) then | |
96 | image = fs.open('mcPaintFolder/images/'..fileName, 'w') | |
97 | imageString = textutils.serialise(imageTable) | |
98 | - | if not fs.exists('/images/') then |
98 | + | |
99 | - | fs.makeDir('/images') |
99 | + | |
100 | mon.clear() | |
101 | - | term.clear() |
101 | + | shell.run('mcImage.lua', fileName) |
102 | - | term.setCursorPos(1,1) |
102 | + | drawPalette() |
103 | - | term.write('Enter your file\'s name: \n') |
103 | + | else term.write('file already exists!') |
104 | saveImage() | |
105 | end | |
106 | end | |
107 | ||
108 | local function boxMode() -- toggles box creation | |
109 | if boxModeToggled == true then | |
110 | - | else term.write('file already exists! Change file name: \n') |
110 | + | |
111 | mon.setBackgroundColor(2^15) | |
112 | mon.setCursorPos(length,18) | |
113 | mon.write('B') | |
114 | else | |
115 | boxModeToggled = true | |
116 | mon.setBackgroundColor(2^14) | |
117 | mon.setCursorPos(length,18) | |
118 | mon.write('B') | |
119 | end | |
120 | mon.setBackgroundColor(currentColor) | |
121 | end | |
122 | ||
123 | local function lineMode() -- toggles line creation | |
124 | if lineModeToggled == true then | |
125 | lineModeToggled = false | |
126 | mon.setBackgroundColor(2^15) | |
127 | mon.setCursorPos(length,19) | |
128 | mon.write('L') | |
129 | else | |
130 | lineModeToggled = true | |
131 | mon.setBackgroundColor(2^14) | |
132 | mon.setCursorPos(length,19) | |
133 | mon.write('L') | |
134 | end | |
135 | mon.setBackgroundColor(currentColor) | |
136 | end | |
137 | ||
138 | ||
139 | - | _, _, x, y = os.pullEvent('monitor_touch') |
139 | + | |
140 | - | if x == length then |
140 | + | |
141 | - | eraseMode = false |
141 | + | |
142 | - | if y <= 16 then |
142 | + | |
143 | - | selectColor() |
143 | + | |
144 | - | elseif y == 17 then |
144 | + | -- This is the MAIN part of code. |
145 | - | eraseMode = true |
145 | + | |
146 | - | mon.setBackgroundColor(2^15) |
146 | + | |
147 | - | currentColor = 2^15 |
147 | + | |
148 | - | elseif y == 18 then |
148 | + | |
149 | - | boxMode() |
149 | + | eventType, _, x, y = os.pullEvent() |
150 | - | elseif y == 19 then |
150 | + | if (eventType == eventClick) or (eventType == eventDrag) then |
151 | - | saveImage() |
151 | + | if eventType == eventClick and x == length then |
152 | - | elseif y == 20 then |
152 | + | eraseMode = false |
153 | - | mon.setBackgroundColor(2^15) |
153 | + | if y <= 16 then |
154 | - | mon.clear() |
154 | + | selectColor() |
155 | - | return |
155 | + | elseif y == 17 then |
156 | ||
157 | - | elseif x < length then |
157 | + | eraseMode = true |
158 | - | draw() |
158 | + | mon.setBackgroundColor(2^15) |
159 | currentColor = 2^15 | |
160 | ||
161 | elseif y == 18 then | |
162 | ||
163 | boxMode() | |
164 | ||
165 | elseif y == 19 then | |
166 | ||
167 | lineMode() | |
168 | ||
169 | elseif y == 20 then | |
170 | ||
171 | saveImage() | |
172 | ||
173 | elseif y == 21 then | |
174 | ||
175 | mon.setBackgroundColor(2^15) | |
176 | mon.clear() | |
177 | mon.setCursorPos(1,1) | |
178 | return | |
179 | end | |
180 | elseif x < length and (eventType == eventClick or eventType == eventDrag) then | |
181 | draw() | |
182 | end | |
183 | end | |
184 | end |