Skip to content

Commit

Permalink
Implemented wheel scrolling and changed colors.
Browse files Browse the repository at this point in the history
The colors were changed to look more like common editors, like Notepad+
  • Loading branch information
ahmed-dardery committed Feb 11, 2016
1 parent b38c83b commit 77384f7
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions viewcode.lua
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
local lexer = require("lexer")
local filename = (...)

function love.load()
love.graphics.setFont(love.graphics.newFont(11))
parsed = {}
fontheight = love.graphics.getFont():getHeight()

local colors = {keyword = {250,175,200}, string = {255,255,100}, comment = {175,175,175},
number = {50,50,255} , iden = {255,255,255}, default = {250,175,200}}
local colors = {keyword = {0,0,255}, string = {128,128,128}, comment = {0,128,0},
number = {255,128,0} , iden = {0,0,0}, default = {0,0,128}, line={255,0,0}}

local contents = love.filesystem.read(filename, 4096)
if not contents then
contents = "error loading file" .. filename
return
end


maxheight=0 --this should help us in the scrolling thingy
for t in contents:gfind("\n") do
maxheight=maxheight+fontheight
end

contents = contents:gsub("\r", "")
for t,v in lexer.lua(contents, {}, {}) do
if colors[t] ~= nil then table.insert(parsed, colors[t]) table.insert(parsed, v)
else table.insert(parsed, colors["default"]) table.insert(parsed, v)
--table.insert(parsed, t..":"..v..", ")
end
table.insert(parsed, colors[t] or colors["default"])
table.insert(parsed, v)
end

end

offy=0
function love.draw()
love.graphics.setBackgroundColor({72, 131, 168})
love.graphics.translate(0,offy)
love.graphics.setBackgroundColor({255, 255, 255})
love.graphics.print(parsed, 10, 20)
end

function love.wheelmoved(_,y)
y=y*15
height=love.graphics.getHeight()
if maxheight > height then --scrolling up
if y>0 and offy<0 then
offy=offy+y
elseif y<0 and height - offy - 40< maxheight then --scrolling down
offy=offy+y
end
if -offy > maxheight then offy=-maxheight end
if offy > 0 then offy=0 end
end
end

0 comments on commit 77384f7

Please sign in to comment.