|
| 1 | +const EasyCoder_PP = { |
| 2 | + |
| 3 | + name: `EasyCoder_PP`, |
| 4 | + |
| 5 | + pp: { |
| 6 | + defaults: {} |
| 7 | + }, |
| 8 | + |
| 9 | + PP: { |
| 10 | + |
| 11 | + compile: (compiler) => { |
| 12 | + let symbolRecord; |
| 13 | + let token; |
| 14 | + let item; |
| 15 | + let url; |
| 16 | + let value; |
| 17 | + let pp; |
| 18 | + let id; |
| 19 | + const lino = compiler.getLino(); |
| 20 | + const action = compiler.nextToken(); |
| 21 | + if (compiler.isSymbol()) { |
| 22 | + symbolRecord = compiler.getSymbolRecord(); |
| 23 | + if (symbolRecord.keyword === `pppanel`) { |
| 24 | + token = compiler.nextToken(); |
| 25 | + switch (token) { |
| 26 | + case `text`: |
| 27 | + value = compiler.getNextValue(); |
| 28 | + compiler.addCommand({ |
| 29 | + domain: `pp`, |
| 30 | + keyword: `pp`, |
| 31 | + lino, |
| 32 | + action: `setText`, |
| 33 | + name: symbolRecord.name, |
| 34 | + value |
| 35 | + }); |
| 36 | + return true; |
| 37 | + case `dissolve`: |
| 38 | + if (compiler.nextTokenIs(`to`)) { |
| 39 | + value = compiler.getNextValue(); |
| 40 | + compiler.addCommand({ |
| 41 | + domain: `pp`, |
| 42 | + keyword: `pp`, |
| 43 | + lino, |
| 44 | + action: `dissolve`, |
| 45 | + name: symbolRecord.name, |
| 46 | + value |
| 47 | + }); |
| 48 | + return true; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + return false; |
| 53 | + } |
| 54 | + switch (action) { |
| 55 | + case `attach`: |
| 56 | + if (compiler.nextTokenIs(`to`)) { |
| 57 | + compiler.next(); |
| 58 | + } |
| 59 | + if (compiler.isSymbol()) { |
| 60 | + symbolRecord = compiler.getSymbolRecord(); |
| 61 | + if (symbolRecord.keyword === `div`) { |
| 62 | + compiler.next(); |
| 63 | + compiler.addCommand({ |
| 64 | + domain: `pp`, |
| 65 | + keyword: `pp`, |
| 66 | + lino, |
| 67 | + action: `attachDiv`, |
| 68 | + container: symbolRecord.name |
| 69 | + }); |
| 70 | + return true; |
| 71 | + } |
| 72 | + } else { |
| 73 | + id = compiler.getValue(); |
| 74 | + compiler.addCommand({ |
| 75 | + domain: `pp`, |
| 76 | + keyword: `pp`, |
| 77 | + lino, |
| 78 | + action: `attachId`, |
| 79 | + id |
| 80 | + }); |
| 81 | + return true; |
| 82 | + } |
| 83 | + break; |
| 84 | + case `background`: |
| 85 | + url = compiler.getNextValue(); |
| 86 | + compiler.addCommand({ |
| 87 | + domain: `pp`, |
| 88 | + keyword: `pp`, |
| 89 | + lino, |
| 90 | + action, |
| 91 | + url |
| 92 | + }); |
| 93 | + return true; |
| 94 | + case `init`: |
| 95 | + const overrides = []; |
| 96 | + compiler.next(); |
| 97 | + item = `0`; |
| 98 | + while (item) { |
| 99 | + item = compiler.getToken(); |
| 100 | + switch (item) { |
| 101 | + case `fontFace`: |
| 102 | + case `fontSize`: |
| 103 | + case `fontWeight`: |
| 104 | + case `fontStyle`: |
| 105 | + case `fontColor`: |
| 106 | + case `textAlign`: |
| 107 | + case `panelLeft`: |
| 108 | + case `panelTop`: |
| 109 | + case `panelWidth`: |
| 110 | + case `panelHeight`: |
| 111 | + case `panelBorder`: |
| 112 | + case `panelPadding`: |
| 113 | + case `panelBackground`: |
| 114 | + value = compiler.getNextValue(); |
| 115 | + overrides.push({item,value}); |
| 116 | + break; |
| 117 | + default: |
| 118 | + item = null; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + compiler.addCommand({ |
| 123 | + domain: `pp`, |
| 124 | + keyword: `pp`, |
| 125 | + lino, |
| 126 | + action, |
| 127 | + overrides |
| 128 | + }); |
| 129 | + return true; |
| 130 | + case `panel`: |
| 131 | + symbolRecord = compiler.compileVariable(`pp`, `pppanel`); |
| 132 | + pp = {}; |
| 133 | + while (true) { |
| 134 | + item = compiler.getToken(); |
| 135 | + if ([`panelLeft`, `panelTop`, `panelWidth`, `panelHeight`, |
| 136 | + `panelBorder`, `panelPadding`, `panelBackground`, |
| 137 | + `fontFace`, `fontSize`, `fontWeight`, `fontStyle`, `fontColor`, |
| 138 | + `textAlign`] |
| 139 | + .includes(item)) { |
| 140 | + value = compiler.getNextValue(); |
| 141 | + pp[item] = value; |
| 142 | + } |
| 143 | + else { |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | + compiler.addCommand({ |
| 148 | + domain: `pp`, |
| 149 | + keyword: `pp`, |
| 150 | + lino, |
| 151 | + action, |
| 152 | + name: symbolRecord.name, |
| 153 | + pp |
| 154 | + }); |
| 155 | + return true; |
| 156 | + default: |
| 157 | + break; |
| 158 | + } |
| 159 | + return false; |
| 160 | + }, |
| 161 | + |
| 162 | + run: (program) => { |
| 163 | + const command = program[program.pc]; |
| 164 | + const action = command.action; |
| 165 | + let symbolRecord; |
| 166 | + let element; |
| 167 | + let inner; |
| 168 | + let container; |
| 169 | + let pp; |
| 170 | + let values; |
| 171 | + let value; |
| 172 | + let w; |
| 173 | + let h; |
| 174 | + const nConst = (content) => { |
| 175 | + return { |
| 176 | + type: `constant`, |
| 177 | + numeric: true, |
| 178 | + content |
| 179 | + }; |
| 180 | + }; |
| 181 | + const sConst = (content) => { |
| 182 | + return { |
| 183 | + type: `constant`, |
| 184 | + numeric: false, |
| 185 | + content |
| 186 | + }; |
| 187 | + }; |
| 188 | + const setupContainer = (program, container) => { |
| 189 | + EasyCoder_PP.pp.container = container; |
| 190 | + container.style[`position`] = `relative`; |
| 191 | + container.style[`overflow`] = `hidden`; |
| 192 | + const defaults = EasyCoder_PP.pp.defaults; |
| 193 | + const height = parseFloat(container.offsetWidth) |
| 194 | + * program.getValue(defaults.aspectH) / program.getValue(defaults.aspectW); |
| 195 | + container.style[`height`] = `${Math.round(height)}px`; |
| 196 | + }; |
| 197 | + switch (action) { |
| 198 | + case `attachDiv`: |
| 199 | + symbolRecord = program.getSymbolRecord(command.container); |
| 200 | + container = symbolRecord.element[symbolRecord.index]; |
| 201 | + setupContainer(program, container); |
| 202 | + break; |
| 203 | + case `attachId`: |
| 204 | + container = document.getElementById(program.getValue(command.id)); |
| 205 | + setupContainer(program, container); |
| 206 | + break; |
| 207 | + case `background`: |
| 208 | + container = EasyCoder_PP.pp.container; |
| 209 | + container.style[`background-image`] = `url("${program.getValue(command.url)}")`; |
| 210 | + container.style[`background-size`] = `cover`; |
| 211 | + break; |
| 212 | + case `init`: |
| 213 | + EasyCoder_PP.pp.container = null; |
| 214 | + EasyCoder_PP.pp.defaults = { |
| 215 | + aspectW: nConst(16), |
| 216 | + aspectH: nConst(9), |
| 217 | + fontFace: sConst(`sans-serif`), |
| 218 | + fontSize: nConst(50), |
| 219 | + fontWeight: sConst(`normal`), |
| 220 | + fontStyle: sConst(`normal`), |
| 221 | + fontColor: sConst(`black`), |
| 222 | + textAlign: sConst(`left`), |
| 223 | + panelLeft: nConst(100), |
| 224 | + panelTop: nConst(100), |
| 225 | + panelWidth: nConst(800), |
| 226 | + panelHeight: nConst(800), |
| 227 | + panelBorder: sConst(`1px solid black`), |
| 228 | + panelPadding: sConst(`1em`), |
| 229 | + panelBackground: sConst(`none`) |
| 230 | + }; |
| 231 | + for (const item of command.overrides) { |
| 232 | + EasyCoder_PP.pp.defaults[item.item] = item.value; |
| 233 | + } |
| 234 | + break; |
| 235 | + case `panel`: |
| 236 | + symbolRecord = program.getSymbolRecord(command.name); |
| 237 | + pp = command.pp; |
| 238 | + values = JSON.parse(JSON.stringify(EasyCoder_PP.pp.defaults)) |
| 239 | + if (pp.panelLeft) { |
| 240 | + values.panelLeft = pp.panelLeft; |
| 241 | + } |
| 242 | + if (pp.panelTop) { |
| 243 | + values.panelTop = pp.panelTop; |
| 244 | + } |
| 245 | + if (pp.panelWidth) { |
| 246 | + values.panelWidth = pp.panelWidth; |
| 247 | + } |
| 248 | + if (pp.panelHeight) { |
| 249 | + values.panelHeight = pp.panelHeight; |
| 250 | + } |
| 251 | + if (pp.panelBorder) { |
| 252 | + values.panelBorder = pp.panelBorder; |
| 253 | + } |
| 254 | + if (pp.panelPadding) { |
| 255 | + values.panelPadding = pp.panelPadding; |
| 256 | + } |
| 257 | + if (pp.panelBackground) { |
| 258 | + values.panelBackground = pp.panelBackground; |
| 259 | + } |
| 260 | + if (pp.fontFace) { |
| 261 | + values.fontFace = pp.fontFace; |
| 262 | + } |
| 263 | + if (pp.fontSize) { |
| 264 | + values.fontSize = pp.fontSize; |
| 265 | + } |
| 266 | + if (pp.fontWeight) { |
| 267 | + values.fontWeight = pp.fontWeight; |
| 268 | + } |
| 269 | + if (pp.fontStyle) { |
| 270 | + values.fontStyle = pp.fontStyle; |
| 271 | + } |
| 272 | + if (pp.fontColor) { |
| 273 | + values.fontColor = pp.fontColor; |
| 274 | + } |
| 275 | + if (pp.textAlign) { |
| 276 | + values.textAlign = pp.textAlign; |
| 277 | + } |
| 278 | + container = EasyCoder_PP.pp.container; |
| 279 | + w = Math.round(container.getBoundingClientRect().width); |
| 280 | + h = Math.round(container.getBoundingClientRect().height); |
| 281 | + element = document.createElement(`div`); |
| 282 | + symbolRecord.element[symbolRecord.index] = element; |
| 283 | + element.id = `ec-${symbolRecord.name}-${symbolRecord.index}-${EasyCoder.elementId++}`; |
| 284 | + element.style[`position`] = `absolute`; |
| 285 | + element.style[`left`] = program.getValue(values.panelLeft) * w / 1000; |
| 286 | + element.style[`top`] = program.getValue(values.panelTop) * h / 1000; |
| 287 | + element.style[`width`] = `${program.getValue(values.panelWidth) * w / 1000}px`; |
| 288 | + element.style[`height`] = `${program.getValue(values.panelHeight) * h / 1000}px`; |
| 289 | + element.style[`background`] = program.getValue(values.panelBackground); |
| 290 | + element.style[`border`] = `${program.getValue(values.panelBorder)}`; |
| 291 | + container.appendChild(element); |
| 292 | + inner = document.createElement(`div`); |
| 293 | + inner.style[`margin`] = `${program.getValue(values.panelPadding)}`; |
| 294 | + inner.style[`font-face`] = program.getValue(values.fontFace); |
| 295 | + inner.style[`font-size`] = `${program.getValue(values.fontSize) * h / 1000}px`; |
| 296 | + inner.style[`font-weight`] = program.getValue(values.fontWeight); |
| 297 | + inner.style[`font-style`] = program.getValue(values.fontStyle); |
| 298 | + inner.style[`color`] = program.getValue(values.fontColor); |
| 299 | + inner.style[`text-align`] = program.getValue(values.textAlign); |
| 300 | + element.appendChild(inner); |
| 301 | + element.inner = inner; |
| 302 | + break; |
| 303 | + case`setText`: |
| 304 | + symbolRecord = program.getSymbolRecord(command.name); |
| 305 | + element = symbolRecord.element[symbolRecord.index]; |
| 306 | + value = program.getValue(command.value); |
| 307 | + element.inner.innerHTML = value.split(`\n`).join(`<br>`); |
| 308 | + break; |
| 309 | + case `dissolve`: |
| 310 | + symbolRecord = program.getSymbolRecord(command.name); |
| 311 | + element = symbolRecord.element[symbolRecord.index]; |
| 312 | + value = program.getValue(command.value); |
| 313 | + element.inner.innerHTML = value.split(`\n`).join(`<br>`); |
| 314 | + break; |
| 315 | + } |
| 316 | + return command.pc + 1; |
| 317 | + } |
| 318 | + }, |
| 319 | + |
| 320 | + PPPanel: { |
| 321 | + |
| 322 | + run: (program) => { |
| 323 | + return program[program.pc].pc + 1; |
| 324 | + } |
| 325 | + }, |
| 326 | + |
| 327 | + getHandler: (name) => { |
| 328 | + switch (name) { |
| 329 | + case `pp`: |
| 330 | + return EasyCoder_PP.PP; |
| 331 | + case `pppanel`: |
| 332 | + return EasyCoder_PP.PPPanel; |
| 333 | + default: |
| 334 | + return null; |
| 335 | + } |
| 336 | + }, |
| 337 | + |
| 338 | + run: program => { |
| 339 | + const command = program[program.pc]; |
| 340 | + const handler = EasyCoder_PP.getHandler(command.keyword); |
| 341 | + if (!handler) { |
| 342 | + program.runtimeError(command.lino, `Unknown keyword '${command.keyword}' in 'pp' package`); |
| 343 | + } |
| 344 | + return handler.run(program); |
| 345 | + }, |
| 346 | + |
| 347 | + value: { |
| 348 | + |
| 349 | + compile: () => { |
| 350 | + return null; |
| 351 | + }, |
| 352 | + |
| 353 | + get: () => { |
| 354 | + return null; |
| 355 | + } |
| 356 | + }, |
| 357 | + |
| 358 | + condition: { |
| 359 | + |
| 360 | + compile: () => {}, |
| 361 | + |
| 362 | + test: () => {} |
| 363 | + } |
| 364 | +}; |
| 365 | + |
| 366 | +// eslint-disable-next-line no-unused-vars |
| 367 | +EasyCoder.domain.pp = EasyCoder_PP; |
0 commit comments