ComputerCraft Archive

Image

computer utility kepler155c github

Description

ComputerCraft OS

Installation

Copy one of these commands into your ComputerCraft terminal:

wget:wget https://raw.githubusercontent.com/kepler155c/opus/develop-1.8/sys/modules/opus/ui/components/Image.lua image
Archive:wget https://cc.shobie.xyz/cc/get/gh-kepler155c-opus-sys-modules-opus-ui-components-image image
Quick Install: wget https://cc.shobie.xyz/cc/get/gh-kepler155c-opus-sys-modules-opus-ui-components-image Image

Usage

Run: Image

Tags

none

Source

View Original Source

Code Preview

local class = require('opus.class')
local UI    = require('opus.ui')
local Util  = require('opus.util')

local lookup = '0123456789abcdef'

-- handle files produced by Paint
UI.Image = class(UI.Window)
UI.Image.defaults = {
	UIElement = 'Image',
	event = 'button_press',
}
function UI.Image:postInit()
	if self.filename then
		self.image = Util.readLines(self.filename)
	end

	if self.image and not (self.height or self.ey) then
		self.height = #self.image
	end
	if self.image and not (self.width or self.ex) then
		for i = 1, self.height do
			self.width = math.max(self.width or 0, #self.image[i])
		end
	end
end

function UI.Image:draw()
	self:clear()
	if self.image then
		for y = 1, #self.image do
			local line = self.image[y]
			for x = 1, #line do
				local ch = lookup:find(line:sub(x, x))
				if ch then
					self:write(x, y, ' ', 2 ^ (ch - 1))
				end
			end
		end
	end
	self:drawChildren()
end

function UI.Image:setImage(image)
	self.image = image
end

function UI.Image.example()
	return UI.Image {
		backgroundColor = 'primary',
		filename = 'test.paint',
	}
end