diff --git a/src/apis/gui.lua b/src/apis/gui.lua deleted file mode 100644 index cbc604b..0000000 --- a/src/apis/gui.lua +++ /dev/null @@ -1,239 +0,0 @@ ---[[GUI API BY MHIEKURU - functions available - - OBJECTS - gui.background() - gui.progressBar() - gui.toggleButton() - gui.pulseButton() - - RENDER - gui.render() -]] ---CORE FUNCTION-- - -local function convertArea( vertex ) - local xMax, yMax = term.getSize() - --if vertex consist of two numbers, convert given as length and height, then set starting vertex to the current cursor position - if type(vertex[1]) == 'number' and type(vertex[2]) == 'number' and vertex[3] == nil and vertex[4] == nil then - local x, y = term.getCursorPos() - if vertex[1] < 0 then - vertex[3] = xMax+vertex[1]+1 - else - vertex[3] = x-1+vertex[1] - end - if vertex[2] < 0 then - vertex[4] = yMax+vertex[2]+1 - else - vertex[4] = y-1+vertex[2] - end - vertex[1] = x - vertex[2] = y - --if vertex consist of four numbers, convert each number to its [maxAxis plus the negative vertex plus one] if the vertex is a negative value - elseif type(vertex[1]) == 'number' and type(vertex[2]) == 'number' and type(vertex[3]) == 'number' and type(vertex[4]) == 'number' then - if vertex[1] < 0 then - vertex[1] = xMax+vertex[1]+1 - end - if vertex[2] < 0 then - vertex[2] = yMax+vertex[2]+1 - end - if vertex[3] < 0 then - vertex[3] = xMax+vertex[3]+1 - end - if vertex[4] < 0 then - vertex[4] = yMax+vertex[4]+1 - end - end - --switch vertex[1] and vertex[3] if vertex[1] is greater - local placeHolderX, placeHolderY - if vertex[3] < vertex[1] then - placeHolderX = vertex[1] - vertex[1] = vertex[3] - vertex[3] = placeHolderX - end - --switch vertex[1] and vertex[3] if vertex[1] is greater - if vertex[4] < vertex[2] then - placeHolderY = vertex[2] - vertex[2] = vertex[4] - vertex[4] = placeHolderY - end - return vertex -end - -local function fill( vertex, color ) - term.setBackgroundColor( color ) - for i = vertex[2], vertex[4] do - term.setCursorPos( vertex[1], i ) - write( string.rep( ' ', vertex[3] - vertex[1] + 1 ) ) - end -end - -function setCursorPos( vertex, string, position ) - if position == 'c' then - term.setCursorPos( math.ceil( ( ( vertex[3] - vertex[1] + 1 - #string ) / 2 ) + vertex[1] ), - math.ceil( ( math.floor(vertex[4]) + math.floor(vertex[2]) ) / 2 ) ) - elseif position == 'l' then - term.setCursorPos( vertex[1], math.ceil( ( math.floor(vertex[4]) + math.floor(vertex[2]) ) / 2 ) ) - end -end - ---GUI LIBRARY-- - -background = function( color ) - return - { - type = 'background', - color = color - } -end - -progressBar = function( vertex, color, string ) - return - { - type = 'bar_percent', - vertex = vertex, - color = color, - string = string, - } -end - -toggleButton = function( vertex, color, string, guifunction ) - return - { - type = 'button_toggle', - vertex = vertex, - color = color, - string = string, - guifunction = guifunction, - state = 1 - } -end - -pulseButton = function( vertex, color, string, guifunction ) - return - { - type = 'button_pulse', - vertex = vertex, - color = color, - string = string, - guifunction = guifunction, - } -end - -render = function(gui) - local counter = 0 - local counterEvent - local refreshrate = 0 - os.startTimer(0) - while true do - if counter == 3 then - if gui[counterEvent].guifunction then - local startTime = os.clock() - gui[counterEvent].guifunction() - if os.clock() > startTime+refreshrate then - os.startTimer(refreshrate) - end - end - counter = 0 - elseif counter > 0 then - counter = counter+1 - end - --wait event - local event, mbttn, crsrX, crsrY = os.pullEvent() - --check and run functions - if event == 'mouse_click' or event == 'monitor_touch' or event == 'mouse_drag' then - local startTime = os.clock() - for i = 1, #gui do - if gui[i].type == 'button_toggle' or gui[i].type == 'button_pulse' then - if crsrX >= gui[i].vertex[1] and crsrX <= gui[i].vertex[3] and crsrY >= gui[i].vertex[2] and crsrY <= gui[i].vertex[4] then - if gui[i].type == 'button_toggle' then - if gui[i].guifunction then - gui[i].guifunction[gui[i].state]() - end - if mbttn == 1 then -- [+] - if gui[i].state == #gui[i].color then --only when func activated - gui[i].state = 1 - else - gui[i].state = gui[i].state + 1 - end - else -- [-] - if gui[i].state == 1 then --only when func activated - gui[i].state = #gui[i].color - else - gui[i].state = gui[i].state - 1 - end - end - elseif gui[i].type == 'button_pulse' then - counter = 1 - counterEvent = i - end - end - end - end - if os.clock() > startTime+refreshrate then - os.startTimer(refreshrate) - end - elseif event == 'timer' then - --render - for i, v in ipairs(gui) do - if gui[i].type == 'button_toggle' then - gui[i].vertex = convertArea( gui[i].vertex ) - --renders button's background - fill( gui[i].vertex, gui[i].color[gui[i].state] ) - --renders button's text - setCursorPos( gui[i].vertex, gui[i].string[gui[i].state], 'c' ) - term.setTextColor( colors.white ) - write( gui[i].string[gui[i].state] ) - term.setCursorPos( gui[i].vertex[1], gui[i].vertex[4] + 2 ) - elseif gui[i].type == 'button_pulse' then - gui[i].vertex = convertArea( gui[i].vertex ) - --renders button's background - if counter == 0 then - fill( gui[i].vertex, gui[i].color[1] ) - else - fill( gui[i].vertex, gui[i].color[2] ) - end - --renders button's text - setCursorPos( gui[i].vertex, gui[i].string, 'c' ) - term.setTextColor( colors.white ) - write( gui[i].string ) - term.setCursorPos( gui[i].vertex[1], gui[i].vertex[4] + 2 ) - elseif gui[i].type == 'bar_percent' then - if gui[i].string then - local percent = gui[i].string() - gui[i].vertex = convertArea( gui[i].vertex ) - if percent > 100 then percent = 100 end --limit percent to 100% - --renders progress bar's background - fill( {math.floor(((gui[i].vertex[3]-gui[i].vertex[1]+1)*percent/100)+gui[i].vertex[1]), gui[i].vertex[2], gui[i].vertex[3], gui[i].vertex[4]}, gui[i].color[1] ) - --renders progress bar's foreground - fill( {gui[i].vertex[1], gui[i].vertex[2], math.floor(((gui[i].vertex[3]-gui[i].vertex[1]+1)*percent/100)+gui[i].vertex[1]-1), gui[i].vertex[4]}, gui[i].color[2] ) - --renders data to be displayed - local perTab = {} - for j = 1, #tostring( percent .. '%' ) do - perTab[ j ] = tostring( percent .. '%' ):sub( j, j ) - end - setCursorPos( gui[i].vertex, perTab, 'c' ) - for j = 1, #perTab do - local perX, perY = term.getCursorPos() - if perX <= math.floor(((gui[i].vertex[3]-gui[i].vertex[1]+1)*percent/100)+gui[i].vertex[1]-1) and perX >= gui[i].vertex[1] then - term.setBackgroundColor(gui[i].color[2]) - term.setTextColor(gui[i].color[1]) - else - term.setBackgroundColor(gui[i].color[1]) - term.setTextColor(gui[i].color[2]) - end - write(perTab[j]) - end - term.setCursorPos( gui[i].vertex[1], gui[i].vertex[4] + 2 ) - end - elseif gui[i].type == 'background' then - local x, y = term.getCursorPos() - term.setBackgroundColor( gui[ i ].color ) - term.clear() - term.setCursorPos( x, y ) - end - end - os.startTimer(refreshrate) - end - end -end diff --git a/src/apis/rmenu.lua b/src/apis/rmenu.lua deleted file mode 100644 index 6c1f5c1..0000000 --- a/src/apis/rmenu.lua +++ /dev/null @@ -1,81 +0,0 @@ ---rMenu made by trainerred2000 - -function createMenu(sx,sy,type) - if(not sx or not sy or not type)then - error("Argumets invalid - start x, starty, type - valid types are (mouse and key)") - end - - menu = { - mtype = type, - x=sx, - y=sy, - selop = 1, - - options = { - }, - - addOption = function(self,name,textcolor,backcolor,selectedtextcolor,selectedbackcolor,mfunction) - - self.options[#self.options+1] = {name=name,textcolor=textcolor,backcolor=backcolor,selectedtextcolor=selectedtextcolor,selectedbackcolor=selectedbackcolor,func=mfunction} - - end, - - update = function(self) - if(#self.options <= 0)then - error("Menu empty! run :addOption(option) ") - end - - if(self.mtype == "mouse")then - for i = 1, #self.options do - term.setCursorPos(sx,sy+i) - term.setTextColor(self.options[i].textcolor) - term.setBackgroundColor(self.options[i].backcolor) - print(" "..self.options[i].name.." ") - end - - elseif(self.mtype == "key")then - for i = 1, #self.options do - if(self.selop == i)then - term.setCursorPos(sx,sy+i) - term.setTextColor(self.options[i].selectedtextcolor) - term.setBackgroundColor(self.options[i].selectedbackcolor) - print("> "..self.options[i].name.." <") - else - term.setCursorPos(sx,sy+i) - term.setTextColor(self.options[i].textcolor) - term.setBackgroundColor(self.options[i].backcolor) - print(" "..self.options[i].name.." ") - end - end - end - - a = {os.pullEvent()} - - if(self.mtype == "mouse" and a[1] == "mouse_click" and a[2] == 1)then - for i = 1, #self.options do - if(a[3] >= self.x and a[3] <= self.x+(#self.options[i].name)+2 and a[4] == math.floor(self.y+i))then - term.setCursorPos(sx,sy+i) - term.setTextColor(self.options[i].selectedtextcolor) - term.setBackgroundColor(self.options[i].selectedbackcolor) - print(" "..self.options[i].name.." ") - sleep(0.3) - self.options[i].func() - --else - --error(a[3].."|"..self.x.."| "..self.x+(#self.options[i].name+2).."|"..a[4].."|"..(self.y+i) ) - end - end - - - elseif(self.mtype == "key" and a[1] == "key")then - if(a[2] == keys.up and self.selop > 1)then self.selop = self.selop - 1 end - if(a[2] == keys.down and self.selop < (#self.options))then self.selop = self.selop + 1 end - if(a[2] == keys.enter)then self.options[self.selop].func() end - end - - end, - - } - - return menu - -end diff --git a/src/apis/ui.lua b/src/apis/ui.lua new file mode 100644 index 0000000..ef8d6cc --- /dev/null +++ b/src/apis/ui.lua @@ -0,0 +1,998 @@ +-- UI API From LMNetOS + + +function header(butt) + +if not sPhone then + sPhone = { + user = "Unknown", + } +end + + paintutils.drawLine(1,1,26,1, colors.gray) + term.setTextColor(colors.white) + term.setCursorPos(1,1) + write(" "..sPhone.user) + term.setCursorPos(26,1) + write("X") + term.setBackgroundColor(colors.white) + term.setTextColor(colors.black) + term.setCursorPos(1,3) +end + + +xLen,yLen = term.getSize() + +function isSpace() + local xNow, yNow = term.getCursorPos() + return yNow < yLen +end + +function version() + return 1,"LMNetUI for sPhone" +end + +function has(pWhat) + -- I want a boolean, not a function... + return ui[pWhat] ~= nil +end + +function clearArea(pYStart, pYEnd) + for i=pYStart,pYEnd do + term.setCursorPos(1,i) + term.clearLine() + end +end + +function colorPicker(title, moreText, customColors) + local screenTitle = title or "Color Picker" + local colorList = {} + local selected = 1 + if customColors then + for i = 1, #customColors do + table.insert(colorList, customColors[i]) + end + else + for i = 0, 15 do + local num = 2^i + if term.isColor() or num == 1 or num == 32768 then + table.insert(colorList, num) + end + end + end + local function redraw() + term.setTextColor(1) + term.setBackgroundColor(32768) + term.clear() + term.setCursorPos(1, 1) + local w, h = term.getSize() + local cx, cy = math.ceil(w/2), math.ceil(h/2) + term.setCursorPos(cx-math.floor(screenTitle:len()/2), 1) + print(screenTitle) + if moreText then + for i, v in pairs(moreText) do + print(v) + end + end + for i = 1, #colorList do + term.setTextColor(colorList[i]) + if selected == i then + write("#") + else + write("_") + end + end + term.setCursorPos(1, h-1) + term.setTextColor(colors.white) + term.setBackgroundColor(colors.black) + write("Use arrow keys to move,") + term.setCursorPos(1, h) + write("press enter to pick color.") + end + local oldPullEvent = os.pullEvent + os.pullEvent = os.pullEventRaw + while true do + redraw() + local event = {os.pullEvent()} + if event[1] == "terminate" then + os.pullEvent = oldPullEvent + return + elseif event[1] == "key" then + if event[2] == keys.left then + if selected == 1 then + selected = #colorList + else + selected = selected - 1 + end + elseif event[2] == keys.right then + if selected == #colorList then + selected = 1 + else + selected = selected + 1 + end + elseif event[2] == keys.enter then + os.pullEvent = oldPullEvent + return colorList[selected] + end + end + end +end + +function cprint(text) + if type(text) ~= 'table' then + text = {text} + end + + local w, h = term.getSize() + + for i=1,#text do + local x, y = term.getCursorPos() + term.setCursorPos(math.floor(w/2)-math.floor(text[i]:len()/2), y) + print(text[i]) + end +end + +function menu(items, title, closeButton) + local function clear() + term.clear() + term.setCursorPos(1, 1) + end + + local termWidth, termHeight = term.getSize() + local drawSize = termHeight - 6 + + local function maxPages() + local itemCount = #items + local pageCount = 0 + while itemCount > 0 do + itemCount = itemCount - drawSize + pageCount = pageCount + 1 + end + return pageCount + end + + local function iif(cond, trueval, falseval) + if cond then + return trueval + else + return falseval + end + end + + local function pagedItems() + local ret = {} + for i = 1, maxPages() do + local tmp = {} + local nElements = 0 + for j = drawSize*(i-1)+1, iif(drawSize*(i+1) > #items, #items, drawSize*(i+1)) do + if nElements < drawSize then + table.insert(tmp, items[j]) + nElements = nElements + 1 + end + end + table.insert(ret, tmp) + end + return ret + end + + local selected = 1 + if start then + selected = start + end + local page = 1 + + local function redraw() + term.setBackgroundColor(colors.white) + term.setTextColor(colors.black) + clear() + header(closeButton) + term.setCursorPos(1,3) + if not title then + title = "sPhone" + end + cprint(" "..title) + if moreTitle then + head = moreTitle + else + head = {"\n",} + if not allowNil or allowNil == true then + --head[3] = 'Terminate to cancel.' + end + end + for i=1,#head do + print(head[i]) + end + if maxPages() > 1 then + pages = "<- (page "..page.." of "..maxPages()..") ->" + print(pages) + end + for i = 1, #pagedItems()[page] do + if selected == drawSize*(page-1)+i then + term.setBackgroundColor(colors.white) + term.setTextColor(colors.black) + else + term.setBackgroundColor(colors.white) + term.setTextColor(colors.black) + end + term.clearLine() + cprint(iif(selected == drawSize*(page-1)+i,"","").." "..pagedItems()[page][i]) + term.setBackgroundColor(colors.white) + term.setTextColor(colors.black) + end + end + + local function changePage(pW) + if pW == 1 and page < maxPages() then + page = page + 1 + if selected + drawSize > #items then + selected = #items + else + selected = selected + drawSize + end + elseif pW == -1 and page > 1 then + page = page - 1 + if selected - drawSize < 1 then + selected = 1 + else + selected = selected - drawSize + end + end + end + + while true do + redraw() + local eventData = {os.pullEventRaw()} + if eventData[1] == 'mouse_click' then + if eventData[4] == 1 and eventData[3] == 26 then + return false, 0 + end + if eventData[4] > 3 then + clear() + selected = (eventData[4]-6+((page-1)*drawSize))+1 + if selected then + return items[selected], selected + end + end + end + sleep(0) + end +end + +function yesno(text, title, start) + + local function clear() + term.clear() + term.setBackgroundColor(colors.white) + term.setCursorPos(1, 1) + end + term.setBackgroundColor(colors.white) + clear() + + local function drawButton(buttonText, x, y, x2, y2, enabled) + if enabled then + term.setBackgroundColor(colors.red) + else + if term.isColor() then + term.setBackgroundColor(colors.lightGray) + end + end + term.setCursorPos(x, y) + for _y = y, y2 do + for _x = x, x2 do + term.setCursorPos(_x, _y) + write(" ") + end + end + term.setCursorPos(x+math.floor((x2-x)/2)-math.floor(buttonText:len()/2), y+math.floor((y2-y+1)/2)) + term.setTextColor(colors.white) + write(buttonText) + term.setTextColor(colors.white) + term.setBackgroundColor(colors.white) + end + + local selected = true + + local function redraw() + clear() + header() + term.setCursorPos(1,5) + cprint(title) + term.setCursorPos(1,7) + cprint(text) + local w, h = term.getSize() + drawButton(lang_yes, 2, h-1, math.floor(w/2)-1, h-1, selected) + drawButton(lang_no, math.floor(w/2)+1, h-1, w-1, h-1, not selected) + end + + if start ~= nil and type(start) == "boolean" then + selected = start + end + local w, h = term.getSize() + while true do + redraw() + local eventData = {os.pullEventRaw()} + if eventData[1] == "terminate" then + clear() + return + elseif eventData[1] == "key" then + if eventData[2] == keys.up or eventData[2] == keys.down or eventData[2] == keys.left or eventData[2] == keys.right then + selected = not selected + elseif eventData[2] == keys.enter then + clear() + return selected + end + elseif eventData[1] == 'mouse_click' then + if eventData[4] == h-1 then + if eventData[3] >= math.floor(w/2)+1 and eventData[3] <= w-1 then + clear() + return false + elseif eventData[3] >= 2 and eventData[3] <= math.floor(w/2)-1 then + clear() + term.setTextColor(colors.red) + return true + end + end + end + term.setTextColor(colors.red) + sleep(0) + end +end + +function button(pLabel,pX,pY,pCol) + local xLen,yLen = term.getSize() + local rtn = {} + local rtn_m = {} + rtn_m.__index = rtn_m + rtn.label = pLabel + if pX == 'm' then + rtn.xStart = (xLen/2)-(pLabel:len()/2) + else + rtn.xStart = pX + end + + rtn.xEnd = rtn.xStart+pLabel:len()+2 + rtn.y = pY + rtn.color = pCol + rtn.textColor = colors.white + rtn.onClick = nil + rtn.autoExec = true + rtn.type = "button" + + setmetatable(rtn,rtn_m) + + function rtn_m:draw() + paintutils.drawLine(self.xStart,self.y,self.xEnd,self.y,self.color) + term.setCursorPos(self.xStart+1,self.y) + term.setTextColor(self.textColor) + write(self.label) + end + + function rtn_m:setNewLabel(pLabel) + local oLabel = self.label + self.label = pLabel + self.xEnd = self.xStart+self.label:len()+2 + end + + function rtn_m:isClicked(pX,pY) + if pY == self.y and pX >= self.xStart and pX <= self.xEnd then + if self.autoExec and self.onClick then + self.onClick() + end + return true + else + return false + end + end + + return rtn +end + +function clickedElements(pX,pY, ... ) + local x = { ... } + for i,v in pairs(x) do + if v:isClicked(pX,pY) then + if v.type == 'button' then + v:exec() + end + return v.label + end + end + return false +end + +clickedButtons = clickedElements + +function progressBar(pX,pY,pLen,pCol,pTxt) + local rtn = {} + local rtn_m = {} + rtn_m.__index = rtn_m + + rtn.x = pX + rtn.y = pY + rtn.len = pLen + rtn.color = pCol + rtn.textColor = colors.white + rtn.showText = pTxt + rtn.percent = 0 + rtn.type = "progressBar" + + setmetatable(rtn,rtn_m) + + function rtn_m:draw() + term.setCursorPos(self.x,self.y) + term.clearLine() + local leng = math.floor((self.percent/100)*self.len) + paintutils.drawLine(self.x,self.y,leng,self.y,self.color) + term.setTextColor(self.textColor) + term.setCursorPos((self.len/2)-1,self.y) + term.setBackgroundColor(colors.black) + write(tostring(self.percent)..' %') + end + + function rtn_m:progress( pProgress ) + local progress + if pProgress <= 1 then + progress = pProgress*100 + else + progress = pProgress + end + + self.percent = progress + self:draw() + end + + return rtn +end + +function splitStr(str, maxWidth) + local words = {} + for word in str:gmatch("[^ \t]+") do + table.insert(words, word) + end + local lines = {} + local cLn = 1 + for i, word in ipairs(words) do + if not lines[cLn] then + lines[cLn] = word + elseif (lines[cLn].." "..word):len() > maxWidth then + cLn = cLn + 1 + lines[cLn] = word + else + lines[cLn] = lines[cLn].." "..word + end + end + return lines +end + +function textField(pId,pX,pY,pLen,pBg,pTxt) + local rtn = {} + local rtn_m = {} + rtn_m.__index = rtn_m + rtn.x = pX + rtn.y = pY + + if pLen == 'f' then + local b + rtn.len,b = term.getSize() + else + rtn.len = pLen + end + + rtn.textColor = pTxt + rtn.backgroundColor = pBg + rtn.onClickRead = true + rtn.value = '' + rtn.id = pId + rtn.text = pId + rtn.type = "textField" + + setmetatable(rtn,rtn_m) + + function rtn_m:draw() + paintutils.drawLine(self.x,self.y,self.len,self.y,self.backgroundColor) + local sign,gray + if self.value ~= '' then + sign = self.value + gray = false + else + sign = self.text + gray = true + end + + term.setCursorPos(self.x,self.y) + term.clearLine() + if gray then + term.setTextColor(colors.gray) + else + term.setTextColor(self.textColor) + end + + local i = 1 + while i <= sign:len() and i <= self.len do + write(sign:sub(i,i)) + i = i+1 + end + end + + function rtn_m:isClicked(pX,pY) + if pX >= self.x and pX <= self.len and pY == self.y then + if self.onClickRead then + term.setCursorPos(self.x,self.y) + term.setTextColor(self.textColor) + term.setBackgroundColor(self.backgroundColor) + term.clearLine() + self.value = read() + end + return true + else + return false + end + end + + function rtn_m:warn() + + end + + return rtn +end + +function contextMenu(pItems,pX,pY,pID) + local rtn = {} + local rtn_m = {} + rtn_m["__index"] = rtn_m + + setmetatable(rtn,rtn_m) + rtn.items = pItems + rtn.x = pX + rtn.y = pY + rtn.color = colors.white + rtn.textColor = colors.black + rtn.clicked = false + rtn.len = #rtn.items + rtn.wide = 0 + rtn.onClickActions = {} + rtn.type = "contextMenu" + rtn.id = pID + + for i,v in pairs(rtn.items) do + if v:len() > rtn.wide then + rtn.wide = v:len() + end + end + + rtn.wide = rtn.wide+1 + + function rtn_m:draw() + term.setCursorPos(self.x,self.y) + for i=0,self.len-1 do + paintutils.drawLine(self.x, self.y+i, self.x+self.wide, self.y+i, self.color) + end + term.setTextColor(self.textColor) + for i=1,self.len do + term.setCursorPos(self.x+1,self.y+(i-1)) + print(self['items'][i]) + end + end + + function rtn_m:isClicked(pX,pY, ... ) + if pX >= self.x and pX <= self.x+self.wide and pY >= self.y and pY <= self.y+self.len then + local obj = (pY - self.y)+1 + self.object = self.items[obj] + self.clicked = true + if self.onClickActions[self.object] then + self.onClickActions(self.id, ... ) + end + return true,self.object + else + self.clicked = false + self.object = nil + return false + end + end + + function rtn_m:addOnClick(pItem,pFkt) + self.onClickActions[pItem] = pFkt + end + + return rtn +end + +function textToTable(allowNil, ... ) + local inp = { ... } + local rtn = {} + for i,v in ipairs(inp) do + if v.type == "textField" then + if not allowNil and v.value == '' then + v:warn() + return nil + end + rtn[v.id] = v.value + end + end + return rtn +end + +function createSwitch(tElements,yPos,colorSelect,colorNormal,pID) + --"Label" + rtn = {} + rtn_m = {} + rtn_m["__index"] = rtn_m + setmetatable(rtn,rtn_m) + + rtn.type = "switch" + rtn.id = pID + rtn.buttons = {} + rtn.color = colorNormal + rtn.colorSelect = colorSelect + rtn.onClick = nil + rtn.y = yPos + + for i=1,#tElements do + local j = 1 + if rtn.buttons[i-1] then + j = rtn.buttons[i-1].xEnd + end + table.insert(rtn.buttons,ui.button(tElements[i],j,yPos,rtn.color)) + end + + rtn.select = rtn.buttons[1] + + function rtn_m:draw() + for i,v in pairs(self.buttons) do + if self.select == v then + v.color = self.colorSelect + else + v.color = self.color + end + v:draw() + end + end + + function rtn_m:value() + return self.select.label + end + + function rtn_m:isClicked(pX,pY) + if self.y == pY then + for i,v in pairs(self.buttons) do + if v:isClicked(pX,pY) then + self.select = v + if self.onClick then + self.onClick(self.select.value,self.select) + end + return true,self.select + end + end + end + return false,self.select + end + + return rtn +end + +function toogle(pID,pX,pY) + local rtn = {} + local rtn_m = {} + rtn_m.__index = rtn_m + setmetatable(rtn,rtn_m) + + rtn.x = pX + rtn.y = pY + rtn.id = pID + rtn.type = "toogle" + rtn.value = false + + function rtn_m:draw() + local dp = paintutils.drawPixel + if self.value then + dp(self.x,self.y,colors.green) + dp(self.x+1,self.y,colors.green) + term.setCursorPos(self.x+1,self.y) + write("1") + dp(self.x+2,self.y,colors.gray) + else + dp(self.x,self.y,colors.gray) + dp(self.x+1,self.y,colors.red) + dp(self.x+2,self.y,colors.red) + term.setCursorPos(self.x+1,self.y) + write("0") + end + end + + function rtn_m:isClicked(pX,pY) + if pX >= self.x and pX <= self.x+2 and pY == self.y then + self.value = not self.value + self:draw() + return true + else + return false + end + end + + function rtn_m:makeTo(pTrue, pFalse) + if self.value then + return pTrue + else + return pFalse + end + end + + return rtn +end + +function inputNumber(pID,pX,pY,pLen,pColor) + local rtn,rtn_m = {},{} + rtn_m.__index = rtn_m + setmetatable(rtn,rtn_m) + + rtn.x = pX + rtn.y = pY + rtn.id = pID + rtn.len = pLen + rtn.value = "" + rtn.reverse = {} + for i=1,rtn.len do + rtn.value = rtn.value .. "0" + rtn.reverse[i] = rtn.len-(i-1) + end + rtn.type = "inputNumber" + rtn.color = pColor + rtn.textColor = colors.white + + function rtn_m:draw() + term.setTextColor(self.textColor) + term.setBackgroundColor(self.color) + for i=1,self.len do + term.setCursorPos(self.x+(i-1),self.y) + write("+") + term.setCursorPos(self.x+(i-1),self.y+1) + write(self.value:sub(i,i)) + term.setCursorPos(self.x+(i-1),self.y+2) + write("-") + end + end + + function rtn_m:isClicked(pX,pY) + if pX >= self.x and pX <= self.x+self.len and pY >= self.y and pY <= self.y+3 then + local field = (pX-self.x)+1 + local front,back,handle,add + + if field > 1 then + front = self.value:sub(1,field-1) + else + front = "" + end + + if field < self.len then + back = self.value:sub(field+1,self.value:len()) + else + back = "" + end + + handle = self.value:sub(field,field) + + if pY == self.y or pY == self.y+2 then + local zehner = (self.reverse[field]-1)*10 + + if zehner == 0 then + zehner = 1 + end + + if pY == self.y then + add = zehner + else + add = -zehner + end + + handle = tostring( tonumber(self.value) + add ) + --self.value = front..handle..back + else + term.setCursorPos(self.x,self.y+1) + self.value = read() + end + self:draw() + return true + else + return false + end + end + + function rtn_m:asNumber() + return tonumber(self.value) + end + + return rtn +end + +function listView(pid, pX, pY, pLen, pWide, tListItems) + local rtn = {} + local rtn_m = {} + rtn_m.__index = rtn_m + setmetatable(rtn,rtn_m) + + rtn.x = pX + rtn.y = pY + rtn.len = pLen == "f" and yLen or pLen + rtn.wide = pWide == "f" and xLen or pWide + rtn.maxX = rtn.x + rtn.wide + rtn.maxY = rtn.y + rtn.len + + rtn.elements = #tListItems + rtn.list = tListItems + rtn.page = 1 + rtn.type = "listView" + rtn.clickable = {} + rtn.id = pid + + function rtn_m:draw() + ui.clearArea(self.y, self.maxY) + self.clickable = {} + term.setCursorPos(self.x,self.y) + local temp1, temp2 = 0,0 + local c = self.page + while ui.isSpace() and c <= self.elements do + temp1 = self:item(self.x, self.y+temp2, self.list[c], c) + for i=1,temp1 do + self.clickable[temp2+i] = c + end + temp2 = temp2+temp1 + c = c+1 + end + end + + function rtn_m:newList(pList) + self.list = pList + self.elements = #pList + self:draw() + end + + function rtn_m:item(pX, pY, pItem, pNumber) + local col + if (pNumber / 2) == math.floor(pNumber/2) then + col = colors.gray + else + col = colors.lightGray + end + term.setCursorPos(pX,pY) + local len = 1 + if type(pItem) == "table" then + for i=1,#pItem do + paintutils.drawLine(pX,pY+(i-1),xLen,pY+(i-1),col) + end + term.setCursorPos(pX, pY) + for i=1,#pItem do + print(pItem[i]) + end + len = #pItem + else + paintutils.drawLine(pX,pY,xLen,pY,col) + print(pItem) + end + + return len + end + + function rtn_m.isClicked(pX, pY) + if pX >= self.x and pX <= self.maxX and pY >= self.y and pY <= self.maxY then + local itemThere = self.clickable[ (pY - self.y) + 1 ] + local itemList = self.list[ itemThere + (self.page - 1) ] + return true, itemThere + (self.page - 1), itemList + else + return false + end + end + + function rtn_m:handleScroll(pF, pX, pY) + if pX and pY then + local buffer = self.autoExec + self.autoExec = false + local ist = self:isClicked(pX, pY) + self.autoExec = buffer + if ist then + self.page = self.page+pF + self:draw() + end + else + self.page = self.page+pF + self:draw() + end + end + + return rtn +end + +function inGreen(pTxt) + term.setTextColor(8192) + print(pTxt) + term.setTextColor(colors.white) +end + +function inBrown(pTxt) + term.setTextColor(4096) + print(pTxt) + term.setTextColor(colors.white) +end + +function inBlack(pTxt) + term.setTextColor(32768) + print(pTxt) + term.setTextColor(colors.white) +end + +function inPink(pTxt) + term.setTextColor(64) + print(pTxt) + term.setTextColor(colors.white) +end + +function inYellow(pTxt) + term.setTextColor(16) + print(pTxt) + term.setTextColor(colors.white) +end + +function inOrange(pTxt) + term.setTextColor(2) + print(pTxt) + term.setTextColor(colors.white) +end + +function inMagenta(pTxt) + term.setTextColor(4) + print(pTxt) + term.setTextColor(colors.white) +end + +function inPurple(pTxt) + term.setTextColor(1024) + print(pTxt) + term.setTextColor(colors.white) +end + +function inCyan(pTxt) + term.setTextColor(512) + print(pTxt) + term.setTextColor(colors.white) +end + +function inRed(pTxt) + term.setTextColor(16384) + print(pTxt) + term.setTextColor(colors.white) +end + +function inWhite(pTxt) + term.setTextColor(1) + print(pTxt) + term.setTextColor(colors.white) +end + +function inLightBlue(pTxt) + term.setTextColor(8) + print(pTxt) + term.setTextColor(colors.white) +end + +function inLightGray(pTxt) + term.setTextColor(256) + print(pTxt) + term.setTextColor(colors.white) +end + +function inGray(pTxt) + term.setTextColor(128) + print(pTxt) + term.setTextColor(colors.white) +end + +function inLime(pTxt) + term.setTextColor(32) + print(pTxt) + term.setTextColor(colors.white) +end + +function inBlue(pTxt) + term.setTextColor(2048) + print(pTxt) + term.setTextColor(colors.white) +end diff --git a/src/apps/cst/api.lua b/src/apps/cst/api.lua new file mode 100644 index 0000000..51423ca --- /dev/null +++ b/src/apps/cst/api.lua @@ -0,0 +1,49 @@ +function login(name,pass) + local stream = http.get("http://crystalcoins.site88.net/user.php?action=login&user="..name.."&pass="..pass) + local erg = stream.readAll() + stream.close() + if erg == "true" then + return true + else + return false + end +end + +function create(name,pass) + local stream = http.get("http://crystalcoins.site88.net/user.php?action=create&user="..name.."&pass="..pass) + local erg = stream.readAll() + stream.close() + if erg == "Success!" then + return true + else + return false + end +end + +function newpass(name,oldpass,newpass) + local stream = http.get("http://crystalcoins.site88.net/user.php?action=newpass&user="..name.."&oldpass="..oldpass.."&pass="..newpass) + local erg = stream.readAll() + stream.close() + return erg +end + +function delete(name,pass) + local stream = http.get("http://crystalcoins.site88.net/user.php?action=delete&user="..name.."&pass="..pass) + local erg = stream.readAll() + stream.close() + return erg +end + +function getBalance(name) + local stream = http.get("http://crystalcoins.site88.net/user.php?action=getBalance&user="..name) + local erg = stream.readAll() + stream.close() + return erg +end + +function transaction(user,pass,to,amt) + local stream = http.get("http://crystalcoins.site88.net/user.php?action=transaction&user="..user.."&pass="..pass.."&to="..to.."&amt="..amt) + local erg = stream.readAll() + stream.close() + return erg +end \ No newline at end of file diff --git a/src/apps/cstwallet.lua b/src/apps/cstwallet.lua new file mode 100644 index 0000000..1b1312a --- /dev/null +++ b/src/apps/cstwallet.lua @@ -0,0 +1,258 @@ +os.loadAPI("/.sPhone/apps/cst/api") +--[[ + + CrystalCoin Wallet (KST) + +--]] +local function drawCryst(x,y) + term.setCursorPos(x,y) + term.setBackgroundColor(colors.lightGray) + term.setTextColor(colors.lightBlue) + write("/") + term.setCursorPos(x + 1,y) + term.setBackgroundColor(colors.lightBlue) + term.setTextColor(colors.blue) + write("\\") + term.setCursorPos(x,y + 1) + write("\\") + term.setCursorPos(x + 1,y + 1) + term.setBackgroundColor(colors.blue) + term.setTextColor(colors.lightBlue) + write("/") +end + +local function center(str,yLvl) + x, y = term.getSize() + x = x / 2 + x = x - ( #str / 2 ) + term.setCursorPos(x,yLvl) + write(str) +end + +hash = sha256.sha256 + +term.setBackgroundColor(colors.white) +term.clear() +drawCryst(2,2) +term.setCursorPos(2,5) +term.setBackgroundColor(colors.white) +term.setTextColor(colors.blue) +write("Username: ") +term.setTextColor(colors.lightBlue) +username = read() +term.setCursorPos(2,6) +term.setTextColor(colors.blue) +write("Password: ") +term.setTextColor(colors.lightBlue) +password = hash(read("*")) +term.setBackgroundColor(colors.white) +term.clear() +x, y = term.getSize() +drawCryst(x / 2,y / 2) +sleep(0.8) +term.setCursorPos(2,2) +term.setBackgroundColor(colors.lightGray) +x, y = term.getSize() +x = x - 2 +for i=1,x do + write(" ") +end +if api.login(username,password) then + term.setTextColor(colors.lime) + center("Logged In Successfully.",2) +else + createUser = api.create(username,password) + if createUser == true then + term.setTextColor(colors.lime) + center("Created User",2) + else + term.setTextColor(colors.red) + center("Already Taken / Wrong Password",2) + sleep(2) + term.setBackgroundColor(colors.black) + term.clear() + term.setCursorPos(1,1) + password = nil + return + end +end +sleep(2) +while true do + term.setBackgroundColor(colors.white) + term.clear() + x, y = term.getSize() + x = x - 2 + drawCryst(x,2) + term.setCursorPos(2,2) + term.setBackgroundColor(colors.white) + term.setTextColor(colors.blue) + write("Balance: ") + term.setTextColor(colors.lightBlue) + bal = api.getBalance(username) + write(bal.."CST") + term.setCursorPos(2,4) + term.setBackgroundColor(colors.lightGray) + term.setTextColor(colors.gray) + write(" Exit ") + term.setCursorPos(2,6) + write("Transfer") + term.setCursorPos(2,8) + write(" Lookup ") + term.setCursorPos(2,10) + write("Password") + term.setCursorPos(2,12) + term.setBackgroundColor(colors.red) + write(" Delete ") + e, c, x, y = os.pullEvent("mouse_click") + if x > 1 and x < 10 and y == 4 then + term.setBackgroundColor(colors.black) + term.clear() + term.setCursorPos(1,1) + password = nil + return + end + if x > 1 and x < 10 and y == 6 then + term.setBackgroundColor(colors.white) + term.clear() + x, y = term.getSize() + x = x - 2 + drawCryst(x,2) + term.setCursorPos(2,2) + term.setBackgroundColor(colors.white) + term.setTextColor(colors.blue) + write("Send To: ") + term.setTextColor(colors.lightBlue) + toUser = read() + term.setCursorPos(2,3) + term.setTextColor(colors.blue) + write("Ammount: ") + term.setTextColor(colors.lightBlue) + ammount = tonumber(read()) + if ammount == nil then + term.setCursorPos(2,5) + term.setTextColor(colors.orange) + write("Ammount not a number") + else + trans = api.transaction(username,password,toUser,ammount) + if trans == "User doesn't exists!" then + term.setTextColor(colors.red) + term.setCursorPos(2,5) + write("Username doesn't exist!") + elseif trans == "Not enough CST" then + term.setTextColor(colors.red) + term.setCursorPos(2,5) + write("Insufficent Funds") + elseif trans == "Negative amount" then + term.setTextColor(colors.red) + term.setCursorPos(2,5) + write("Negative Ammount!!!!") + elseif username == toUser then + term.setCursorPos(2,5) + term.setTextColor(colors.red) + write("Cannot give yourself money") + else + term.setTextColor(colors.lime) + term.setCursorPos(2,5) + write("Transfer Successful!") + end + end + sleep(1.5) + end + if x > 1 and x < 10 and y == 8 then + term.setBackgroundColor(colors.white) + term.clear() + x,y = term.getSize() + x = x - 2 + drawCryst(x,2) + term.setCursorPos(2,2) + term.setBackgroundColor(colors.white) + term.setTextColor(colors.blue) + write("Account: ") + term.setTextColor(colors.lightBlue) + acc = read() + bal = api.getBalance(acc) + if bal == "User doesn't exists!" then + term.setCursorPos(2,4) + term.setTextColor(colors.red) + write("No such user") + sleep(2) + else + term.setCursorPos(2,4) + term.setTextColor(colors.blue) + write(acc.." Has ") + term.setTextColor(colors.lightBlue) + write(bal.."CST") + os.pullEvent("key") + end + end + if x > 1 and x < 10 and y == 10 then + term.setBackgroundColor(colors.white) + term.clear() + x, y = term.getSize() + x = x - 2 + drawCryst(x,2) + term.setCursorPos(2,2) + term.setTextColor(colors.blue) + term.setBackgroundColor(colors.white) + write("Change Password") + term.setCursorPos(2,4) + write("Old Password: ") + oldPW = hash(read("*")) + if api.login(username,oldPW) then + term.setCursorPos(2,5) + term.setTextColor(colors.blue) + write("New Password: ") + newPW = hash(read("*")) + api.newpass(username,password,newPW) + term.setCursorPos(2,7) + term.setTextColor(colors.lime) + write("Changed Password.") + sleep(2) + else + term.setCursorPos(2,6) + term.setTextColor(colors.red) + write("Incorrect Password") + sleep(2) + end + end + if x > 1 and x < 10 and y == 12 then + term.setBackgroundColor(colors.white) + term.clear() + x, y = term.getSize() + x = x - 2 + drawCryst(x,2) + term.setCursorPos(2,2) + term.setBackgroundColor(colors.white) + term.setTextColor(colors.red) + write("Account Deletion") + term.setCursorPos(2,4) + term.setTextColor(colors.blue) + write("Press [1] To Delete") + term.setCursorPos(2,5) + write("Press [2] To Go Back") + e, k = os.pullEvent("key") + sleep(0.5) + if k == 2 then + term.setCursorPos(2,6) + write("Username: ") + term.setTextColor(colors.lightBlue) + delUser = read() + term.setTextColor(colors.blue) + term.setCursorPos(2,7) + write("Password: ") + term.setTextColor(colors.lightBlue) + delPass = hash(read("*")) + if api.login(username,delPass) then + api.delete(username,delPass) + term.setBackgroundColor(colors.black) + term.clear() + term.setCursorPos(1,1) + return + else + term.setTextColor(colors.red) + term.setCursorPos(2,9) + write("Incorrect Password") + end + end + end +end \ No newline at end of file diff --git a/src/apps/gps.lua b/src/apps/gps.lua new file mode 100644 index 0000000..f33a38a --- /dev/null +++ b/src/apps/gps.lua @@ -0,0 +1,17 @@ +term.setBackgroundColor(colors.white) +term.clear() +term.setCursorPos(1,1) +term.setTextColor(colors.black) +print("Loading...") +while true do + term.clear() + x, y, z = gps.locate(0) + term.setCursorPos(1,3) + if not x then + x, y, z = "?", "?", "?" + end + print(" X: "..x) + print(" Y: "..y) + print(" Z: "..z) + sleep(1) +end diff --git a/src/apps/sms.lua b/src/apps/sms.lua new file mode 100644 index 0000000..622ee44 --- /dev/null +++ b/src/apps/sms.lua @@ -0,0 +1,13 @@ +local function clear() + term.setBackgroundColor(colors.white) + term.clear() + term.setCursorPos(1,1) + term.setTextColor(colors.black) +end + +sPhone.winOk("The service is","currently offline", colors.lime, colors.green, colors.white, colors.lime) + + +--while true do + --clear() +--end diff --git a/src/apps/system/settings.lua b/src/apps/system/settings.lua new file mode 100644 index 0000000..1b2512b --- /dev/null +++ b/src/apps/system/settings.lua @@ -0,0 +1,191 @@ +local menu = { + "Update", + "Change username", + "Change password", +} + +local function clear() + term.setBackgroundColor(colors.white) + term.setTextColor(colors.black) + term.clear() + term.setCursorPos(1,1) +end + +local function header() + clear() + local w, h = term.getSize() + paintutils.drawLine(1,1,w,1, colors.gray) + term.setTextColor(colors.white) + term.setCursorPos(1,1) + write(" "..sPhone.user) + term.setCursorPos(1,2) + term.setBackgroundColor(colors.white) + term.setTextColor(colors.black) +end + +local function changeUsername() + header() + term.setCursorPos(2,3) + write("New Username: ") + local newUsername = read() + local f = fs.open("/.sPhone/config/username","w") + f.write(newUsername) + f.close() + sPhone.user = newUsername + print(" All Set!") +end + +local function changePassword() + while true do + term.clear() + term.setCursorPos(1,1) + paintutils.drawImage(paintutils.loadImage("/.sPhone/interfaces/login"),1,1) + term.setTextColor(colors.white) + term.setBackgroundColor(colors.gray) + term.setCursorPos(1,1) + write(" "..sPhone.user) + if sPhone.wrongPassword then + term.setTextColor(colors.red) + term.setBackgroundColor(colors.white) + sertextext.center(13," Wrong Password") + end + term.setTextColor(colors.black) + term.setBackgroundColor(colors.white) + sertextext.center(7," Current Password") + term.setTextColor(colors.black) + term.setCursorBlink(true) + term.setCursorPos(9,10) + local _, k1 = os.pullEvent("char") + write("*") + term.setCursorPos(12,10) + local _, k2 = os.pullEvent("char") + write("*") + term.setCursorPos(15,10) + local _, k3 = os.pullEvent("char") + write("*") + term.setCursorPos(18,10) + local _, k4 = os.pullEvent("char") + write("*") + term.setCursorBlink(false) + local password = k1..k2..k3..k4 + local fpw = fs.open("/.sPhone/.password","r") + if sha256.sha256(password) ~= fpw.readLine() then + sPhone.wrongPassword = true + else + sPhone.wrongPassword = false + fpw.close() + break + end + end + + + while true do + term.clear() + term.setCursorPos(1,1) + paintutils.drawImage(paintutils.loadImage("/.sPhone/interfaces/login"),1,1) + term.setTextColor(colors.white) + term.setBackgroundColor(colors.gray) + term.setCursorPos(1,1) + write(" "..sPhone.user) + if sPhone.wrongPassword then + term.setTextColor(colors.red) + term.setBackgroundColor(colors.white) + sertextext.center(13," Wrong Password") + end + term.setTextColor(colors.black) + term.setBackgroundColor(colors.white) + sertextext.center(7," New Password") + term.setTextColor(colors.black) + term.setCursorBlink(true) + term.setCursorPos(9,10) + local _, k1 = os.pullEvent("char") + write("*") + term.setCursorPos(12,10) + local _, k2 = os.pullEvent("char") + write("*") + term.setCursorPos(15,10) + local _, k3 = os.pullEvent("char") + write("*") + term.setCursorPos(18,10) + local _, k4 = os.pullEvent("char") + write("*") + term.setCursorBlink(false) + local pwChange = k1..k2..k3..k4 + + term.clear() + term.setCursorPos(1,1) + paintutils.drawImage(paintutils.loadImage("/.sPhone/interfaces/login"),1,1) + term.setTextColor(colors.white) + term.setBackgroundColor(colors.gray) + term.setCursorPos(1,1) + write(" "..sPhone.user) + term.setTextColor(colors.black) + term.setBackgroundColor(colors.white) + sertextext.center(7," Repeat Password") + term.setTextColor(colors.black) + term.setCursorBlink(true) + term.setCursorPos(9,10) + local _, r1 = os.pullEvent("char") + write("*") + term.setCursorPos(12,10) + local _, r2 = os.pullEvent("char") + write("*") + term.setCursorPos(15,10) + local _, r3 = os.pullEvent("char") + write("*") + term.setCursorPos(18,10) + local _, r4 = os.pullEvent("char") + write("*") + term.setCursorBlink(false) + pwChangeRep = r1..r2..r3..r4 + if sha256.sha256(pwChange) ~= sha256.sha256(pwChangeRep) then + sPhone.wrongPassword = true + + else + sPhone.wrongPassword = false + break + end + end + if not sPhone.wrongPassword then + local f = fs.open("/.sPhone/.password","w") + f.write(sha256.sha256(pwChangeRep)) + f.close() + end + header() + term.setCursorPos(2,3) + print("All Set!") + sleep(2) + return +end + +local w, h = term.getSize() + +local function redraw() + clear() + local w, h = term.getSize() + paintutils.drawLine(1,1,w,1, colors.gray) + term.setTextColor(colors.white) + term.setCursorPos(2,1) + write(sPhone.user) + term.setCursorPos(w,1) + write("X") + term.setCursorPos(1,3) + term.setBackgroundColor(colors.white) + term.setTextColor(colors.black) + end + redraw() + sPhone.winOk("Work In","Progress") + + while true do + redraw() + local name, id = ui.menu(menu, "Settings",true) + if id == 0 then + return + elseif id == 1 then + setfenv(loadstring(http.get("https://raw.githubusercontent.com/Sertex-Team/sPhone/master/src/installer.lua").readAll()),getfenv())() + elseif id == 2 then + changeUsername() + elseif id == 3 then + changePassword() + end + end \ No newline at end of file diff --git a/src/installer.lua b/src/installer.lua index ddec574..79da07a 100644 --- a/src/installer.lua +++ b/src/installer.lua @@ -5,12 +5,22 @@ end local files = { ["src/sPhone.lua"] = "/.sPhone/sPhone", + ["src/apis/sha256.lua"] = "/.sPhone/apis/sha256", ["src/apis/sertextext.lua"] = "/.sPhone/apis/sertextext", ["src/apis/graphics.lua"] = "/.sPhone/apis/graphics", - ["src/apis/rmenu.lua"] = "/.sPhone/apis/rmenu", - ["src/apis/gui.lua"] = "/.sPhone/apis/gui", - + ["src/apis/ui.lua"] = "/.sPhone/apis/ui", + + ["src/apps/system/settings.lua"] = "/.sPhone/apps/system/settings", + ["src/apps/cstwallet.lua"] = "/.sPhone/apps/cstwallet", + ["src/apps/cst/api.lua"] = "/.sPhone/apps/cst/api", + + ["src/apps/sms.lua"] = "/.sPhone/apps/sms", + ["src/apps/gps.lua"] = "/.sPhone/apps/gps", + + ["src/interfaces/login"] = "/.sPhone/interfaces/login", + + ["src/startup"] = "/startup", } diff --git a/src/interfaces/login b/src/interfaces/login new file mode 100644 index 0000000..e2013d1 --- /dev/null +++ b/src/interfaces/login @@ -0,0 +1,20 @@ +77777777777777777777777777 +00000000000000000000000000 +00000000000000000000000000 +00000000000000000000000000 +00000000000000000000000000 +00000000000000000000000000 +00000000000000000000000000 +00000000000000000000000000 +00000088888888888888000000 +00000088088088088088000000 +00000088888888888888000000 +00000000000000000000000000 +00000000000000000000000000 +00000000000000000000000000 +00000000000000000000000000 +00000000000000000000000000 +00000000000000000000000000 +00000000000000000000000000 +00000000000000000000000000 +00000000000000000000000000 diff --git a/src/sPhone.lua b/src/sPhone.lua index e3657a6..160052e 100644 --- a/src/sPhone.lua +++ b/src/sPhone.lua @@ -1,85 +1,428 @@ +os.forceShutdown = os.shutdown + local function crash(err) - os.pullEvent = os.pullEventRaw - if not err then - err = "Unknown" - end - term.setBackgroundColor(colors.black) - term.clear() - term.setCursorPos(1,2) - term.setTextColor(colors.white) - print(" ###") - print(" # ") - print(" ###") - print(" #") - print(" ###") - print("") - print("sPhone crash: ") - print(err) - while true do - sleep(3600) - end + if not sPhone then + sPhone = { + devMode = false, + } + end + term.setCursorBlink(false) + term.setBackgroundColor(colors.blue) + term.clear() + term.setCursorPos(1,1) + term.setTextColor(colors.white) + if not err then + err = "Unknown" + end + + print("sPhone Crash:\n") + term.setBackgroundColor(colors.black) + printError(err) + term.setBackgroundColor(colors.blue) + print("\nContact sPhone devs: GitHub: Sertex-Team/sPhone") + print("Press any key") + repeat + sleep(0) + until os.pullEvent("key") + if not sPhone.devMode then + os.forceShutdown() + end + term.setBackgroundColor(colors.black) + term.clear() + term.setCursorPos(1,1) + sleep(0.1) + shell.run("/rom/programs/shell") end +if not pocket or not term.isColor() then + crash("Computer not supported: use an Advanced Pocket Computer or an Advanced Wireless Pocket Computer") +end + + local function kernel() - _G.sPhone = { - version = "1.0", - eApp = false, - } - - if not fs.exists("/.sPhone/apis") then - fs.makeDir("/.sPhone/apis") - end - - for k, v in ipairs(fs.list("/.sPhone/apis")) do - os.loadAPI("/.sPhone/apis/"..v) - end - - function os.version() - return "sPhone "..sPhone.version - end - - local function clear() - term.setBackgroundColor(colors.white) - term.clear() - term.setCursorPos(1,1) - term.setTextColor(colors.black) - end - - os.forceShutdown = os.shutdown - os.forceReboot = os.reboot - - function os.shutdown() - clear() - print("Goodbye") - sleep(1) - os.forceShutdown() - end - - function os.reboot() - clear() - print("See you!") - sleep(1) - os.forceReboot() - end - - function home() - while true do - clear() - local reboot = gui.pulseButton({2, 3}, {colors.lime, colors.green}, "Reboot", os.reboot()) - gui.render({reboot}) - end - end - - function login() - - end - - home() - + _G.sPhone = { + version = "Alpha 1", + user = "Unknown", + devMode = true, + } + + if fs.exists("/.sPhone/config/username") then + local u = fs.open("/.sPhone/config/username","r") + sPhone.user = u.readLine() + u.close() + end + + if not fs.exists("/.sPhone/apis") then + fs.makeDir("/.sPhone/apis") + end + + for k, v in pairs(fs.list("/.sPhone/apis")) do + os.loadAPI("/.sPhone/apis/"..v) + end + + function os.version() + return "sPhone "..sPhone.version + end + + local function clear() + term.setBackgroundColor(colors.white) + term.clear() + term.setCursorPos(1,1) + term.setTextColor(colors.black) + end + + sPhone.forceShutdown = os.shutdown + sPhone.forceReboot = os.reboot + + function os.shutdown() + os.pullEvent = os.pullEventRaw + if sPhone.doneShutdown then + clear() + w, h = term.getSize() + term.setCursorPos( (w/2)- 7, h/2) + write("Press CTRL + S") + while true do + sleep(3600) + end + end + sPhone.doneShutdown = true + clear() + w, h = term.getSize() + term.setCursorPos( (w / 2) - 1, h / 2) + for i = 1,3 do + sleep(0.3) + write(".") + end + sleep(0.2) + sPhone.forceShutdown() + end + + function os.reboot() + os.pullEvent = os.pullEventRaw + if sPhone.doneShutdown then + clear() + w, h = term.getSize() + term.setCursorPos( (w/2)- 7, h/2) + write("Press CTRL + R") + while true do + sleep(3600) + end + end + sPhone.doneShutdown = true + clear() + w, h = term.getSize() + term.setCursorPos( (w / 2) - 1, h / 2) + for i = 1,3 do + sleep(0.3) + write(".") + end + sleep(0.2) + sPhone.forceReboot() + end + + function sPhone.winOk(fmessage, smessage, bg, side, text, button) + if not fmessage then + fmessage = "" + end + if not bg then + bg = colors.gray + end + if not text then + text = colors.white + end + if not button then + button = colors.lightGray + end + if not side then + side = colors.lightGray + end + local w, h = term.getSize + term.setBackgroundColor(side) + paintutils.drawBox(11 - math.ceil(#fmessage / 2), 5, 16 + math.ceil(#fmessage / 2), 10, side) + term.setBackgroundColor(bg) + paintutils.drawFilledBox(12 - math.ceil(#fmessage / 2), 6, 15 + math.ceil(#fmessage / 2), 9, bg) + term.setCursorPos(14 - math.ceil(#fmessage / 2), 7) + term.setTextColor(text) + write(fmessage) + if smessage then + term.setCursorPos(14 - math.ceil(#smessage / 2), 8) + write(smessage) + end + term.setCursorPos(13,10) + term.setBackgroundColor(button) + write("Ok") + while true do + local e, k, x,y = os.pullEvent() + if e == "mouse_click" then + if y == 10 then + if x == 13 or x == 14 then + return + end + end + elseif e == "key" then + if k == 28 then + return + end + end + end + end + + function lChat() + clear() + local w, h = term.getSize() + paintutils.drawLine(1,1,w,1,colors.gray) + term.setTextColor(colors.white) + sertextext.center(1," Chat") + term.setBackgroundColor(colors.white) + term.setTextColor(colors.black) + term.setCursorPos(2, 5) + if not peripheral.isPresent("back") or not peripheral.getType("back") == "modem" then + print("Modem not found") + print(" Press any key") + os.pullEvent("key") + return + end + write("Host: ") + local h = read() + term.setCursorPos(2,6) + shell.run("/rom/programs/rednet/chat", "join", h, sPhone.user) + sleep(1) + end + + local function home() + local function drawHome() + local function box(x,y,text,bg,colorText,page) + graphics.box(x,y,x+1+#text,y+2,bg) + term.setCursorPos(x+1,y+1) + term.setTextColor(colorText) + write(text) + end + clear() + local w, h = term.getSize() + paintutils.drawLine(1,1,w,1, colors.gray) + term.setTextColor(colors.white) + sertextext.right(1,"vvv") + term.setCursorPos(1,1) + write(" "..sPhone.user) + box(2,3,"Shell",colors.black,colors.yellow) + box(19,3,"Lock",colors.lightGray,colors.black) + box(11,3,"sP",colors.red,colors.white) + box(2,7,"Games",colors.pink,colors.blue) + box(10,7,"Chat", colors.black,colors.white) + box(19,7,"SMS",colors.green,colors.white) + box(3, 11, "CST", colors.lightBlue, colors.blue) + box(10, 11, "GPS", colors.red, colors.black) + end + local function footerMenu() + sPhone.isFooterMenuOpen = true + function redraw() + local w, h = term.getSize() + graphics.box(1,2,w,4,colors.gray) + term.setTextColor(colors.white) + term.setBackgroundColor(colors.gray) + sertextext.right(1,"^^^") + sertextext.right(3, "Reboot") + term.setCursorPos(11,3) + write("Settings") + term.setCursorPos(2,3) + write("Shutdown") + end + while true do + redraw() + local _,_,x,y = os.pullEvent("mouse_click") + if y == 3 then + if x > 1 and x < 10 then + os.shutdown() + elseif x > 19 and x < 26 then + os.reboot() + elseif x > 10 and x < 19 then + shell.run("/.sPhone/apps/system/settings") + drawHome() + end + elseif y == 1 then + if x < 26 and x > 22 then + sPhone.isFooterMenuOpen = false + return + end + end + end + end + + while true do + drawHome() + term.setCursorBlink(false) + local _,m,x,y = os.pullEvent("mouse_click") + + if y == 1 then + if x < 26 and x > 22 then + footerMenu() + end + else + if (y > 2 and x > 1) and (y < 6 and x < 9) then + term.setBackgroundColor(colors.black) + term.clear() + term.setCursorPos(1,1) + term.setTextColor(colors.white) + print("Type \"exit\" to close the shell") + shell.run("/rom/programs/shell") + elseif (y > 2 and x > 10) and (y < 6 and x < 16) then + sPhone.winOk("Work In", "Progress") + elseif (y > 2 and x > 18) and (y < 6 and x < 25) then + login() + elseif (y > 6 and x > 1) and (y < 10 and x < 9) then + sPhone.winOk("Work In", "Progress") + elseif (y > 6 and x > 9) and (y < 10 and x < 16) then + lChat() + elseif (y > 6 and x > 18) and (y < 10 and x < 24) then + shell.run("/.sPhone/apps/sms") + elseif (y > 10 and x > 2) and (y < 14 and x < 8) then + shell.run("/.sPhone/apps/cstwallet") + elseif (y > 10 and x > 9) and (y < 14 and x < 15) then + shell.run("/.sPhone/apps/gps") + end + end + end + end + + function login() + if fs.exists("/.sPhone/.password") then + while true do + term.clear() + term.setCursorPos(1,1) + paintutils.drawImage(paintutils.loadImage("/.sPhone/interfaces/login"),1,1) + term.setTextColor(colors.white) + term.setBackgroundColor(colors.gray) + term.setCursorPos(1,1) + write(" "..sPhone.user) + if sPhone.wrongPassword then + term.setTextColor(colors.red) + term.setBackgroundColor(colors.white) + sertextext.center(13," Wrong Password") + end + term.setTextColor(colors.black) + term.setBackgroundColor(colors.white) + sertextext.center(7," Insert Password") + term.setTextColor(colors.black) + term.setCursorBlink(true) + term.setCursorPos(9,10) + local _, k1 = os.pullEvent("char") + write("*") + term.setCursorPos(12,10) + local _, k2 = os.pullEvent("char") + write("*") + term.setCursorPos(15,10) + local _, k3 = os.pullEvent("char") + write("*") + term.setCursorPos(18,10) + local _, k4 = os.pullEvent("char") + write("*") + term.setCursorBlink(false) + local passwordLogin = k1..k2..k3..k4 + local fpw = fs.open("/.sPhone/.password","r") + if sha256.sha256(passwordLogin) == fpw.readLine() then + sPhone.wrongPassword = false + home() + else + sPhone.wrongPassword = true + end + end + else + while true do + term.clear() + term.setCursorPos(1,1) + paintutils.drawImage(paintutils.loadImage("/.sPhone/interfaces/login"),1,1) + if sPhone.wrongPassword then + term.setTextColor(colors.red) + sertextext.center(13," Wrong Password") + end + term.setTextColor(colors.black) + term.setBackgroundColor(colors.white) + sertextext.center(3," Setup") + sertextext.center(7," Insert Password") + term.setTextColor(colors.black) + term.setCursorBlink(true) + term.setCursorPos(9,10) + local _, k1 = os.pullEvent("char") + write("*") + term.setCursorPos(12,10) + local _, k2 = os.pullEvent("char") + write("*") + term.setCursorPos(15,10) + local _, k3 = os.pullEvent("char") + write("*") + term.setCursorPos(18,10) + local _, k4 = os.pullEvent("char") + write("*") + term.setCursorBlink(false) + local password1 = k1..k2..k3..k4 + term.clear() + term.setCursorPos(1,1) + paintutils.drawImage(paintutils.loadImage("/.sPhone/interfaces/login"),1,1) + term.setTextColor(colors.black) + term.setBackgroundColor(colors.white) + sertextext.center(3," Setup") + sertextext.center(7," Repeat") + term.setTextColor(colors.black) + term.setCursorBlink(true) + term.setCursorPos(9,10) + local _, v1 = os.pullEvent("char") + write("*") + term.setCursorPos(12,10) + local _, v2 = os.pullEvent("char") + write("*") + term.setCursorPos(15,10) + local _, v3 = os.pullEvent("char") + write("*") + term.setCursorPos(18,10) + local _, v4 = os.pullEvent("char") + write("*") + term.setCursorBlink(false) + local password2 = v1..v2..v3..v4 + if password1 == password2 then + local f = fs.open("/.sPhone/.password", "w") + f.write(sha256.sha256(password1)) + f.close() + term.setTextColor(colors.lime) + sertextext.center(13," Password set!") + sleep(2) + break + else + sPhone.wrongPassword = true + end + end + + term.clear() + term.setCursorPos(1,1) + local w, h = term.getSize() + paintutils.drawLine(1,1,w,1,colors.gray) + term.setTextColor(colors.black) + term.setBackgroundColor(colors.white) + sertextext.center(3," Setup") + sertextext.center(7," Your Username") + term.setCursorPos(3,10) + local name = read() + local f = fs.open("/.sPhone/config/username","w") + f.write(name) + f.close() + sertextext.center(13," All Set!") + sertextext.center(14,"Have fun with sPhone") + sleep(2) + home() + end + end + + login() end -local ok, err = pcall(kernel) +if sPhone then + printError("sPhone already started") + return +end + +local ok, error = pcall(kernel) if not ok then - sPhone.crash(err) + crash(error) end + +os.forceShutdown() \ No newline at end of file