Skip to content

Instantly share code, notes, and snippets.

@JMV38
Created February 11, 2013 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JMV38/4752973 to your computer and use it in GitHub Desktop.
Save JMV38/4752973 to your computer and use it in GitHub Desktop.
Black screen bug
--# Window
Window = class()
-- functionnalities:
-- a window name + close button (x) + minimize/maximize button (_)/(=)
-- print
-- 1 touch: move the window
-- 2 touches: resize h, resize v
-- 1 touch long press: print txt, save to documents image dialog
function Window:init(arg)
self.top = arg.top or 100 -- measured from top
self.left = arg.left or 100
self.w = arg.w or 200
self.h = arg.h or 400
self.name = arg.name or "name"
-- important to support changing device orientation
self.HEIGHT = HEIGHT
self.WIDTH = WIDTH
self.htop = 30
self.txtData = {}
self.txtBorder = 5
self.frameColor = color(223, 211, 172, 255)
self:refresh()
end
function Window:xline(img, x0,x1,y,c)
local set = img.set
for x=x0,x1 do
set(img,x,y,c)
end
end
function Window:yline(img, y0,y1,x,c)
local set = img.set
for y=y0,y1 do
set(img,x,y,c)
end
end
function Window:xyline(img, x0,x1,y0,c)
local set = img.set
local imax = math.min(x1-x0,y1-y0)
for i=0,x1-x0 do
set(img,x0+i,y0+i,c)
end
end
function Window:refresh()
self.frameImg = self:refreshFrame()
self.rx = self.w/2
self.ry = self.h/2
self.cx = self.left + self.w/2
self.cy = HEIGHT - self.top - self.h/2 - self.htop
self.barImg = self:refreshBar()
self.bar_cy = HEIGHT - self.top - self.htop/2
self.txtImg = self:refreshTxt()
end
function Window:clear()
self.txtData = {}
self:refresh()
end
-- usefull definitions
local blue = color(0, 126, 255, 255)
local black = color(0, 0, 0, 255)
local blackLight = color(35, 35, 35, 255)
local blackAlpha = color(0, 0, 0, 128)
local gray = color(156, 156, 156, 255)
local gray = color(127, 127, 127, 255)
local white = color(255, 255, 255, 255)
local whiteAlpha = color(255, 255, 255, 128)
local whiteGloss = color(56, 56, 56, 255)
local blackGloss = color(176, 176, 176, 255)
local text2 = function (str,x,y,colorSwitch)
-- colorSwitch = 1 for white text, 0 for black
pushStyle()
resetStyle()
textMode(CORNER)
textAlign(LEFT)
if colorSwitch==1 then
blendMode(ADDITIVE)
fill(whiteGloss)
text(str,x+1,y-1)
blendMode(MULTIPLY)
fill(blackGloss)
text(str,x-0.5,y+0.5)
blendMode(NORMAL)
fill(white)
else
blendMode(ADDITIVE)
fill(whiteGloss)
text(str,x+1,y-1)
blendMode(MULTIPLY)
fill(blackGloss)
text(str,x-0.5,y+0.5)
blendMode(NORMAL)
fill(black)
end
text(str,x,y)
popStyle()
end
local imgCopy = function(img)
local copy = image(img.width,img.height)
setContext(copy)
pushStyle()
spriteMode(CENTER)
sprite(img, img.width/2, img.height/2 )
popStyle()
setContext()
return copy
end
function Window:refreshBar()
local img = image(self.w,self.htop)
local x0,x1,y0,y1 = 1, img.width, 1, img.height
pushStyle()
-- temporary image
local temp = image(x1,2)
-- gradient
setContext(temp)
background(0, 0, 0, 0)
stroke(255, 255, 255, 83)
stroke(53, 53, 53, 255)
strokeWidth(2)
line(x0-1,2,x1+1,2)
setContext()
local reflect = image(self.w,self.htop)
setContext(reflect)
-- black bar
spriteMode(CENTER)
sprite(temp, x1/2, y1/2, x1, y1)
setContext()
-- reflections
self:yline(reflect, y0,y1,x0,whiteGloss)
self:xline(reflect, x0,x1,y1,whiteGloss)
-- shadows
local shadows = image(self.w,self.htop)
setContext(shadows)
background(white)
setContext()
self:yline(shadows, y0,y1,x1,blackGloss)
self:xline(shadows, x0,x1,y0,blackGloss)
setContext(img)
background( blackLight )
-- background(gray)
blendMode(ADDITIVE)
sprite(reflect, x1/2, y1/2, x1, y1)
blendMode(MULTIPLY)
sprite(shadows, x1/2, y1/2, x1, y1)
blendMode(NORMAL)
local txtColor = 1
text2(self.name,x0+8,y1/4,txtColor)
--[[
--a cross to close the window, but removed now
textAlign(RIGHT)
text2("☒",x1-23,y1/4-2,txtColor)
--]]
setContext()
-- rounded corners
c=color(0, 0, 0, 0)
-- top right
img:set(x1,y1,c)
img:set(x1-1,y1,c)
img:set(x1,y1-1,c)
-- top left
img:set(x0,y1,c)
img:set(x0+1,y1,c)
img:set(x0,y1-1,c)
-- bot right
img:set(x1,y0,c)
img:set(x1-1,y0,c)
img:set(x1,y0-1,c)
-- bot left
img:set(x0,y0,c)
img:set(x0+1,y0,c)
img:set(x0,y0-1,c)
popStyle()
collectgarbage()
return img
end
function Window:refreshFrame()
local img = image(self.w,self.h)
local x0,x1,y0,y1 = 1, img.width, 1, img.height
pushStyle()
setContext(img)
background(self.frameColor)
setContext()
-- draw metalic border
local c = color(183, 183, 183, 255)
local c = gray
for i=0,3 do
self:yline(img, y0,y1,x0+i,c)
self:yline(img, y0,y1,x1-3+i,c)
self:xline(img, x0,x1,y0+i,c)
self:xline(img, x0,x1,y1-3+i,c)
end
-- reflections
local reflect = image(self.w,self.h)
self:yline(reflect, y0,y1,x0,whiteGloss)
self:yline(reflect, y0+3,y1-3,x1-3,whiteGloss)
self:xline(reflect, x0+4,x1-3,y0+3,whiteGloss)
self:xline(reflect, x0,x1,y1,whiteGloss)
-- shadows
local shadows = image(self.w,self.h)
setContext(shadows)
background(white)
setContext()
self:yline(shadows, y0,y1-3,x0+3,blackGloss)
self:yline(shadows, y0,y1,x1,blackGloss)
self:xline(shadows, x0,x1,y0,blackGloss)
self:xline(shadows, x0+3,x1,y1-3,blackGloss)
setContext(img)
blendMode(ADDITIVE)
sprite(reflect, x1/2, y1/2, x1, y1)
blendMode(MULTIPLY)
sprite(shadows, x1/2, y1/2, x1, y1)
blendMode(NORMAL)
setContext()
-- rounded corners
c=color(0, 0, 0, 0)
img:set(x0,y0,c)
img:set(x1,y0,c)
c=color(127, 127, 127, 255)
img:set(x0+1,y0,c)
img:set(x0,y0+1,c)
img:set(x1,y1,c)
c=color(0, 0, 0, 255)
img:set(x1-1,y0,c)
img:set(x1,y0+1,c)
popStyle()
collectgarbage()
return img
end
function Window:refreshTxt()
local str = table.concat(self.txtData,"\n")
-- local img = image(self.w-2*self.txtBorder,self.h-2*self.txtBorder)
local img = self.frameImg
local d = self.txtBorder
local x0,x1,y0,y1 = 1+d, img.width-d, 1+d, img.height-d
pushStyle()
resetStyle()
textWrapWidth(x1)
local w,h = textSize(str)
local t = self.txtData
if h>y1 and #t>1 then -- if output is too big and more than one lines
local w1,h1,cond
cond=true
while cond and #t>1 do
local first = t[1]
-- check if it is big enough to remove first txt line
w1,h1 = textSize(first)
cond = h1<(h-y1)
if cond then table.remove(t,1) end
end
end
setContext(img)
fill(self.frameColor)
rectMode(CENTER)
rect((x1+x0)/2,(y1+y0)/2,(x1-x0),(y1-y0))
fill(black)
textMode(CORNER)
local y = y1-h
if y<y0 then y =y0 end
text(str,x0,y)
setContext()
popStyle()
collectgarbage()
return img
end
function Window:print(txt)
table.insert(self.txtData,txt)
self.txtImg = self:refreshTxt()
end
function Window:loadImage(tbl)
end
function Window:txtCopy()
end
function Window:imgCopy()
end
function Window:close()
end
function Window:minimize()
end
function Window:maximize()
end
function Window:move(dx,dy)
end
function Window:resize(dw,dh)
end
function Window:draw()
pushStyle()
spriteMode(CENTER)
sprite(self.frameImg,self.cx,self.cy)
sprite(self.barImg,self.cx,self.bar_cy)
sprite(self.txtImg,self.cx,self.cy)
popStyle()
end
function Window:touched(touch)
end
--# Windows
Windows = class()
function Windows:init()
self.window = {}
--<windows>
local name = 'details'
local win = Window({})
self.window[name] = win
win.top = 200
win.left = 800
win.w = 200
win.h = 200
win.name = "details"
win.HEIGHT = 1024
win.WIDTH = 768
win:refresh()
local name = 'console'
local win = Window({})
self.window[name] = win
win.top = 50
win.left = 0
win.w = 800
win.h = 680
win.name = "console"
win.HEIGHT = 1024
win.WIDTH = 768
win:refresh()
--</windows>
end
function Windows:save()
local tab = readProjectTab("Windows")
local _,i0 = string.find(tab,"--<windows>")
local i1,_ = string.find(tab,"--</windows>")
local before = string.sub(tab,1,i0)
local after = string.sub(tab,i1,-1)
local str = "\n\t"
for name,win in pairs(self.window) do
str = str.."local name = '"..name.."'" .. "\n\t"
str = str.. "local win = Window({})" .. "\n\t"
str = str.. "self.window[name] = win" .. "\n\t"
str = str.. "win.top = " ..win.top .. "\n\t"
str = str.. "win.left = " ..win.left .. "\n\t"
str = str.. "win.w = " ..win.w .. "\n\t"
str = str.. "win.h = " ..win.h .. "\n\t"
str = str.. "win.name = '" ..name .. "'\n\t"
str = str.. "win.HEIGHT = "..WIDTH .. "\n\t"
str = str.. "win.WIDTH = ".. HEIGHT .. "\n\t"
str = str.. "win:refresh()" .. "\n\t"
end
-- self.window["console"]:print(str)
str = before .. str .. after
saveProjectTab("Windows",str)
end
function Windows:draw()
for i,win in pairs(self.window) do win:draw() end
end
function Windows:touched(touch)
for i,win in pairs(self.window) do win:touched(touch) end
--self:save()
end
--# Main
-- myLib console
displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
collectgarbage()
manager = Windows()
win1 = manager.window["console"]
win2 = manager.window["details"]
fps=FPS()
win1:print("This is an example of output to the console")
win1:print("And antoher line.")
win1:print("123")
win1:print("123")
win1:print("12345678901234567890123456789012345678901234567890")
-- myTimer = Timer("loop", helloFunction ,"infiniteLoop")
myTimer = Timer("loop", helloFunction )
myTimer:start(1)
end
function helloFunction(id)
win1:print("Hello World! from "..id.." @ "..tostring(ElapsedTime).." s")
local n = #win1.txtData
win2:print("number: "..tostring(n))
end
Timer = class()
function Timer:init(id,callBackFun,mode)
self.id = id
self.action = callBackFun
self.stopTime = 0
self.enable = false
self.oneShot = (mode~="infiniteLoop")
end
function Timer:start(delay)
self.stopTime = ElapsedTime + delay
self.delay = delay
self.enable = true
end
function Timer:update()
if (self.enable == true) then
if ElapsedTime >= self.stopTime then
if self.oneShot then self.enable = false
else self:start(self.delay)
end
if type(self.action) == "function" then
self.action(self.id)
end
end
end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(127, 127, 127, 255)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
manager:draw()
myTimer:update()
fps:draw()
end
function touched(touch)
manager:touched(touch)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment