Implemented import

This commit is contained in:
neauoire
2019-11-02 17:50:24 -04:00
parent ff934f9f16
commit b4beaf03af
5 changed files with 56 additions and 36 deletions

View File

@@ -18,12 +18,14 @@ function Source () {
this.cache = {}
}
this.open = (callback) => {
this.open = (ext, callback) => {
console.log('Source', 'Open file..')
const input = document.createElement('input')
input.type = 'file'
input.onchange = (e) => {
this.cache = e.target.files[0]
const file = e.target.files[0]
if (file.name.indexOf(ext) < 0) { console.warn('Source', 'File is not ' + ext); return }
this.cache = file
this.load(this.cache, callback)
}
input.click()

View File

@@ -3,19 +3,17 @@
/* global Image */
function Library (ronin) {
// Modularity: Write simple parts connected by clean interfaces.
// Composition: Design programs to be connected to other programs.
// Parsimony: Write a big program only when it is clear by demonstration that nothing else will do.
// IO
this.import = async (path, shape, alpha = 1) => { // Imports a graphic file with format.
// const img = new Image()
// img.src = path
// return ronin.surface.draw(img, shape, alpha)
this.import = async (name, shape, alpha = 1) => { // Imports a graphic file with format.
const src = ronin.cache.get(name)
if (!src) { ronin.log('No data for ' + name); return }
const img = new Image()
img.src = src
return ronin.surface.draw(img, shape, alpha)
}
this.export = (name = 'export', type = 'image/png', quality = 1.0) => { // Exports a graphic file with format.
this.export = async (name = 'export', type = 'image/png', quality = 1.0) => { // Exports a graphic file with format.
const base64 = ronin.surface.el.toDataURL(type, quality)
const link = document.createElement('a')
link.setAttribute('href', base64)
@@ -23,14 +21,6 @@ function Library (ronin) {
link.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }))
}
this.open = async (path, ratio = 1) => { // Imports a graphic file and resizes the frame.
return ronin.surface.open(path, ratio)
}
this.exit = (force = false) => { // Exits Ronin.
ronin.source.quit(force)
}
// Shapes
this.pos = (x = 0, y = 0) => { // Returns a position shape.

View File

@@ -39,7 +39,7 @@ function Ronin () {
this.acels.set('File', 'New', 'CmdOrCtrl+N', () => { this.source.new(); this.surface.clear(); this.commander.clear() })
this.acels.set('File', 'Save', 'CmdOrCtrl+S', () => { this.source.save('export.lisp', this.commander._input.value, 'text/plain') })
this.acels.set('File', 'Save As', 'CmdOrCtrl+Shift+S', () => { this.source.saveAs() })
this.acels.set('File', 'Open', 'CmdOrCtrl+O', () => { this.source.open(this.whenOpen) })
this.acels.set('File', 'Open', 'CmdOrCtrl+O', () => { this.source.open('lisp', this.whenOpen) })
this.acels.set('File', 'Revert', 'CmdOrCtrl+W', () => { this.source.revert() })
this.acels.set('View', 'Toggle Guides', 'CmdOrCtrl+Shift+H', () => { this.surface.toggleGuides() })
this.acels.set('View', 'Toggle Commander', 'CmdOrCtrl+K', () => { this.commander.toggle() })
@@ -174,7 +174,29 @@ function Ronin () {
this.onDrop = (e) => {
e.preventDefault()
e.stopPropagation()
this.source.load(e.dataTransfer.files[0], this.whenOpen)
const file = e.dataTransfer.files[0]
if (file.name.indexOf('.lisp') > -1) {
this.source.load(e.dataTransfer.files[0], this.whenOpen)
}
if (file.type === 'image/jpeg' || file.type === 'image/png') {
const img = new Image()
img.onload = () => {
this.cache.set(file.name, img.src)
}
img.src = URL.createObjectURL(file)
}
}
this.cache = {
data: {},
set: (key, content) => {
this.log((this.cache.data[key] ? 'Updated ' : 'Stored ') + key)
this.cache.data[key] = content
},
get: (key) => {
return this.cache.data[key]
}
}
this.mouseShape = (position, type) => {