changed eventHandler

- the event handler is now only listening to necessary events, which makes the overall event loop much smaller and makes other stuff also possible
- base frame is now auto. resizing
- setMoveable -> setMovable

Note: There may be some (ofc expected) bugs..
This commit is contained in:
Robert Jelic
2022-08-12 01:52:06 +02:00
parent 5f76a059cc
commit 6dcd4c2427
76 changed files with 1905 additions and 14981 deletions

View File

@@ -1,7 +1,7 @@
--Basalt configurated installer
local filePath = "basalt.lua" --here you can change the file path default: basalt.lua
if not(fs.exists(filePath))then
shell.run("pastebin run ESs1mg7P packed true "..filePath) -- this is an alternative to the wget command
shell.run("pastebin run ESs1mg7P packed true "..filePath:gsub(".lua", "")) -- this is an alternative to the wget command
end
local basalt = require(filePath:gsub(".lua", "")) -- here you can change the variablename in any variablename you want default: basalt

162
examples/basaltPreview2.lua Normal file
View File

@@ -0,0 +1,162 @@
local basalt = require("Basalt")
basalt.setVariable("buttonColor", basalt.schedule(function(self)
self:setBackground(colors.black)
self:setForeground(colors.lightGray)
os.sleep(0.1)
self:setBackground(colors.gray)
self:setForeground(colors.black)
end))
local main
basalt.setVariable("ex1", function()
main:addAnimation():setObject(main):setAutoDestroy():offset(0,0,1):play()
end)
basalt.setVariable("ex1Top", function()
local example1 = main:getDeepObject("example1")
example1:addAnimation():setObject(example1):setAutoDestroy():offset(0,0,1):play()
end)
basalt.setVariable("ex2", function()
main:addAnimation():setObject(main):setAutoDestroy():offset(main:getWidth(),0,1):play()
end)
basalt.setVariable("p1", function()
local example2 = main:getDeepObject("example2")
example2:addAnimation():setObject(example2):setAutoDestroy():offset(0,0,1):play()
end)
basalt.setVariable("p2", function()
local example2 = main:getDeepObject("example2")
example2:addAnimation():setObject(example2):setAutoDestroy():offset(0,example2:getHeight(),1):play()
end)
basalt.setVariable("p3", function()
local example2 = main:getDeepObject("example2")
example2:addAnimation():setObject(example2):setAutoDestroy():offset(0,example2:getHeight()*2,1):play()
end)
basalt.setVariable("ex3", function()
main:addAnimation():setObject(main):setAutoDestroy():offset(main:getWidth()*2,0,1):play()
end)
basalt.setVariable("e1", function()
local example3 = main:getDeepObject("example3")
example3:addAnimation():setObject(example3):setAutoDestroy():offset(0,0,1):play()
end)
basalt.setVariable("e2", function()
local example3 = main:getDeepObject("example3")
example3:addAnimation():setObject(example3):setAutoDestroy():offset(0,example3:getHeight(),1):play()
end)
basalt.setVariable("e3", function()
local example3 = main:getDeepObject("example3")
example3:addAnimation():setObject(example3):setAutoDestroy():offset(0,example3:getHeight()*2,1):play()
end)
basalt.setVariable("ex4", function()
main:addAnimation():setObject(main):setAutoDestroy():offset(main:getWidth()*3,0,1):play()
end)
basalt.setVariable("progressChange", function(self)
main:getDeepObject("progressLabel"):setText(self:getValue().."%")
end)
basalt.setVariable("pauseP2", function()
main:getDeepObject("program2"):pause()
end)
basalt.setVariable("pauseP3", function()
main:getDeepObject("program3"):pause()
end)
basalt.setVariable("startAnimation", function()
main:getDeepObject("animation1"):play()
end)
basalt.setVariable("disableStartButton", function()
main:getDeepObject("animationButton"):disable()
end)
basalt.setVariable("enableStartButton", function()
main:getDeepObject("animationButton"):enable()
end)
basalt.setVariable("onTextfieldFocus", function()
main:getDeepObject("coolTextfield"):setForeground(colors.lightGray)
main:getDeepObject("textfieldAnimLoseFocus"):cancel()
main:getDeepObject("textfieldAnimFocus"):play()
end)
basalt.setVariable("onTextfieldLoseFocus", function()
main:getDeepObject("coolTextfield"):setForeground(colors.gray)
main:getDeepObject("textfieldAnimFocus"):cancel()
main:getDeepObject("textfieldAnimLoseFocus"):play()
end)
basalt.setVariable("makeButtonVisible", function()
main:getDeepObject("showAnimBtn1"):show()
main:getDeepObject("showAnimBtn2"):show()
main:getDeepObject("showAnimBtn3"):show()
end)
basalt.setVariable("dragPosition", function(ob, ev, bt, x, y, dragStartX, dragStartY, mouseX, mouseY)
ob:setPosition(x, y)
end)
local function inject(prog, key)
local events = prog:getQueuedEvents()
table.insert(events, 1, {event="key", args = {key}})
prog:injectEvents(events)
prog:updateQueuedEvents({})
end
basalt.setVariable("p3Up", function()
local program = main:getDeepObject("program3")
inject(program, keys.w)
end)
basalt.setVariable("p3Down", function()
local program = main:getDeepObject("program3")
inject(program, keys.s)
end)
basalt.setVariable("p3Left", function()
local program = main:getDeepObject("program3")
inject(program, keys.a)
end)
basalt.setVariable("p3Right", function()
local program = main:getDeepObject("program3")
inject(program, keys.d)
end)
basalt.setVariable("noDrag", function(self)
return false
end)
basalt.setVariable("openSidebar", function(self)
main:addAnimation():setObject(main:getDeepObject("sidebar")):setAutoDestroy():move(-12,1,1):play()
end)
basalt.setVariable("closeSidebar", function(self)
main:addAnimation():setObject(main:getDeepObject("sidebar")):setAutoDestroy():move(2,1,1):play()
end)
basalt.setVariable("progressTheProgressbar", function()
os.sleep(1)
local progressbar = main:getDeepObject("progressBar")
local progress = 0
while true do
progressbar:setProgress(progress)
progress = progress+0.25
os.sleep(1)
end
end)
main = basalt.createFrame():addLayout("basaltPreview2.xml")
basalt.autoUpdate()

209
examples/basaltPreview2.xml Normal file
View File

@@ -0,0 +1,209 @@
<frame onScroll="ex1OnScroll" id="example1" width="parent.w" height="parent.w" bg="lightGray" maxScroll="32" scrollable="true">
<label text="Objects" font="2" x="16" y="2" />
<button onClick="buttonColor" onClick="ex2" anchor="topRight" height="1" width="8" x="-7" y="2" text="Next" />
<button onDrag="dragPosition" x="2" y="6" width="parent.w/2-2" height="5" />
<button onDrag="dragPosition" x="parent.w/2+1" y="6" width="parent.w/2-2" height="5" />
<frame y="13" x="2" width="parent.w/2-2" height="16" bg="black" scrollable="true" importantScroll="true">
<button onClick="buttonColor" width="parent.w-2" x="2" y="2" text="Example Button 1" />
<button onClick="buttonColor" width="parent.w-2" x="2" y="6" text="Example Button 2" />
<button onClick="buttonColor" width="parent.w-2" x="2" y="10" text="Example Button 3" />
<button onClick="buttonColor" width="parent.w-2" x="2" y="14" text="Example Button 4" />
<button onClick="buttonColor" width="parent.w-2" x="2" y="18" text="Example Button 5" />
<button onClick="buttonColor" width="parent.w-2" x="2" y="22" text="Example Button 6" />
</frame>
<frame y="13" x="parent.w/2+1" width="parent.w/2-2" height="16" bg="black" scrollable="true" importantScroll="true">
<label x="2" y="2" text="Radios and Checkboxes:" fg="lightGray" />
<radio x="2" y="4" bg="gray" fg="lightGray" boxBG="black" boxFG="lightGray" selectionBG="black" inactiveBoxBG="black">
<item><text>Radio 1</text><x>2</x><y>1</y><bg>black</bg></item>
<item><text>Radio 2</text><x>2</x><y>3</y><bg>black</bg></item>
<item><text>Radio 3</text><x>2</x><y>5</y><bg>black</bg></item>
<item><text>Radio 4</text><x>2</x><y>7</y><bg>black</bg></item>
<item><text>Radio 5</text><x>2</x><y>9</y><bg>black</bg></item>
</radio>
<checkbox x="3" y="15" /><label x="5" y="15" text="Checkbox 1" fg="lightGray" />
<checkbox x="3" y="17" /><label x="5" y="17" text="Checkbox 2" fg="lightGray" />
<checkbox x="3" y="19" /><label x="5" y="19" text="Checkbox 3" fg="lightGray" />
<checkbox x="3" y="21" /><label x="5" y="21" text="Checkbox 4" fg="lightGray" />
</frame>
<progressbar onChange="progressChange" width="parent.w-2" height="3" x="2" y="30" id="progressBar" />
<label x="parent.w/2-7" y="31" zIndex="6" bg="false" text="Progressbar Example" fg="lightGray" />
<label id="progressLabel" x="3" y="31" zIndex="6" bg="false" text="0%" fg="lightGray" />
<thread thread="progressTheProgressbar" start="true" />
<frame zIndex="16" y="34" x="4" width="32" height="12" moveable="true" bar="true" barText="Moveable Frame" barBG="black" barFG="lightGray" shadow="true">
<label x="2" y="3" text="Input:" fg="lightGray" />
<input default="Default Text" defaultFG="gray" width="parent.w-2" x="2" y="5" bg="black" fg="lightGray" />
<input default="Only numbers" defaultFG="gray" width="parent.w-2" x="2" y="7" type="number" bg="black" fg="lightGray" />
<input default="Password" defaultFG="gray" width="parent.w-2" x="2" y="9" type="password" bg="black" fg="lightGray" />
</frame>
<frame zIndex="16" y="36" x="6" width="32" height="12" moveable="true" bar="true" barText="Moveable Frame 2" barBG="black" barFG="lightGray" border="true" borderTop="false">
<label x="2" y="3" text="Dropdowns, Lists and Menubars" fg="lightGray" />
<dropdown x="2" y="5" bg="black" fg="lightGray">
<item><text>Entry 1</text></item>
<item><text>Entry 2</text></item>
<item><text>Entry 3</text></item>
<item><text>Entry 4</text></item>
<item><text>Entry 5</text></item>
<item><text>Entry 6</text></item>
<item><text>Entry 7</text></item>
<item><text>Entry 8</text></item>
</dropdown>
<list x="parent.w/2" y="5" width="parent.w/2-1" bg="black" fg="gray" selectionFG="lightGray">
<item><text>Entry 1</text></item>
<item><text>Entry 2</text></item>
<item><text>Entry 3</text></item>
<item><text>Entry 4</text></item>
<item><text>Entry 5</text></item>
<item><text>Entry 6</text></item>
<item><text>Entry 7</text></item>
<item><text>Entry 8</text></item>
</list>
<menubar x="2" y="1" anchor="bottomLeft" width="parent.w-2" bg="black" fg="gray" selectionFG="lightGray" scrollable="true">
<item><text>Entry 1</text></item>
<item><text>Entry 2</text></item>
<item><text>Entry 3</text></item>
<item><text>Entry 4</text></item>
<item><text>Entry 5</text></item>
<item><text>Entry 6</text></item>
<item><text>Entry 7</text></item>
<item><text>Entry 8</text></item>
</menubar>
</frame>
<button onClick="buttonColor" onClick="ex1Top" x="parent.w-12" y="48" text="Top" />
</frame>
<frame id="example3" x="parent.w*2+1" width="parent.w" height="parent.h" bg="lightGray" >
<label text="Editor" font="2" x="16" y="2" />
<textfield x="2" y="6" width="parent.w-2" height="parent.h-7"/>
<frame ignoreOffset="true" id="sidebar" anchor="topRight" x="3" width="14" height="parent.h" bg="black" >
<button onClick="buttonColor" onClick="closeSidebar" anchor="bottomLeft" width="5" height="1" x="1" y="1" fg="black" bg="gray" text="Close" />
<button onClick="buttonColor" onClick="e1" onClick="closeSidebar" width="parent.w-2" x="2" y="2" text="Example 1" />
<button onClick="buttonColor" onClick="e2" onClick="closeSidebar" width="parent.w-2" x="2" y="6" text="Example 2" />
<button onClick="buttonColor" onClick="e3" onClick="closeSidebar" width="parent.w-2" x="2" y="10" text="Example 3" />
</frame>
<textfield x="2" y="parent.h+2" width="parent.w-2" height="parent.h-3">
<keywords>
<purple>
<keyword>if</keyword>
<keyword>then</keyword>
<keyword>else</keyword>
<keyword>elseif</keyword>
<keyword>repeat</keyword>
<keyword>do</keyword>
<keyword>while</keyword>
<keyword>end</keyword>
<keyword>function</keyword>
<keyword>for</keyword>
</purple>
<blue>
<keyword>local</keyword>
<keyword>true</keyword>
<keyword>false</keyword>
<keyword>nil</keyword>
</blue>
<yellow>
<keyword>print</keyword>
<keyword>pairs</keyword>
<keyword>ipairs</keyword>
</yellow>
</keywords>
<rules>
<rule>
<pattern>%d</pattern>
<fg>lightBlue</fg>
</rule>
<rule>
<pattern>%"%a+%"</pattern>
<fg>red</fg>
</rule>
<rule>
<pattern>[-]+[%w*%s*%p*]*</pattern>
<fg>green</fg>
</rule>
</rules>
</textfield>
<textfield id="coolTextfield" onGetFocus="onTextfieldFocus" onLoseFocus="onTextfieldLoseFocus" x="2" y="parent.h*2+2" width="20" height="3" bg="black" fg="gray">
<lines>
<line>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna </line>
<line>aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata </line>
<line>sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor </line>
<line>invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet</line>
<line>clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</line>
</lines>
</textfield>
<button id="anotherRandomButton" onClick="buttonColor" x="coolTextfield.x" y="coolTextfield.y + coolTextfield.h + 1" text="Button" />
<button onClick="buttonColor" x="coolTextfield.x + coolTextfield.w + 2" y="coolTextfield.y" text="Button" />
<animation id="textfieldAnimFocus" object="coolTextfield"><size width="40" height="12" duration="1"/></animation>
<animation id="textfieldAnimLoseFocus" object="coolTextfield"><size width="20" height="3" duration="1"/></animation>
<button ignoreOffset="true" onClick="buttonColor" onClick="openSidebar" anchor="bottomRight" width="5" height="1" x="-3" y="1" text="Open" />
<button onClick="buttonColor" onClick="ex2" x="2" y="2" width="8" height="1" text="Back" />
<button onClick="buttonColor" onClick="ex4" anchor="topRight" x="-7" y="2" width="8" height="1" text="Next" />
</frame>
<frame zIndex="17" id="example4" x="parent.w*3+1" width="parent.w" height="parent.h" bg="lightGray" >
<label text="Animations" font="2" x="16" y="2" />
<button onClick="buttonColor" onClick="ex3" x="2" y="2" width="8" height="1" text="Back" />
<frame id="showAnimFrame1" x="15" y="30" width="2" height="2" bg="black" >
<label id="animFrameLabel" text="Hello" x="-5" y="-1" fg="lightGray" />
<button anchor="bottomLeft" id="animFrameBtn1" text="Cool" x="-5" y="15" fg="lightGray" bg="gray" />
<button anchor="bottomRight" id="animFrameBtn2" text="Button" x="50" y="15" fg="lightGray" bg="gray" />
</frame>
<animation id="animation19" onDone="enableStartButton" object="animationButton"><move x="22" y="10" duration="0.5"/> <size width="12" height="3" duration="1"/></animation>
<animation id="animation18" onDone="#animation19" object="showAnimFrame1"><move x="50" y="20" duration="0.5"/><size width="2" height="2" duration="0.5"/></animation>
<animation id="animation17" onDone="#animation18" object="showAnimFrame1"><move x="5" y="6" duration="1"/><size width="30" height="8" duration="1"/></animation>
<animation id="animation16" onDone="#animation17" object="showAnimFrame1"><move x="2" y="6" duration="1"/><size width="48" height="13" duration="1"/></animation>
<animation id="animation15" onDone="#animation16" object="showAnimFrame1"><size width="36" height="10" duration="1"/></animation>
<animation id="animation14" onDone="#animation15" object="animFrameBtn2"><move x="-12" y="-2" duration="0.7"/></animation>
<animation id="animation13" onDone="#animation14" object="animFrameBtn1"><move x="3" y="-2" duration="0.7"/></animation>
<animation id="animation12" onDone="#animation13" object="animFrameLabel"><move x="3" y="3" duration="1"/><text duration="2"><text>Hello, i</text><text>Hello, i am</text><text>Hello, i am just</text><text>Hello, i am just a</text><text>Hello, i am just a label</text></text></animation>
<animation id="animation11" onDone="#animation12" object="showAnimFrame1"><move x="5" y="6" duration="1"/><size width="42" height="12" duration="1"/></animation>
<button id="showAnimBtn1" x="25" y="-6" width="16" text="Button 1" visible="false" />
<button id="showAnimBtn2" x="0" y="-6" width="16" text="Button 2" visible="false" />
<button id="showAnimBtn3" x="50" y="-6" width="16" text="Button 3" visible="false" />
<animation id="animation10" onDone="#animation11" object="showAnimBtn1"><move x="60" y="6" duration="1"/><size width="2" height="2" duration="1"/></animation>
<animation id="animation9" onDone="#animation10" object="showAnimBtn1"><move x="5" y="6" duration="0.5"/><size width="42" height="9" duration="1"/></animation>
<animation id="btn2PosAnim2" object="showAnimBtn2"><move x="-15" y="22" duration="0.5"/></animation>
<animation id="btn3PosAnim2" object="showAnimBtn3"><move x="55" y="22" duration="0.5"/></animation>
<animation mode="linear" id="animation8" onDone="#animation9" onDone="#btn2PosAnim2" onDone="#btn3PosAnim2" object="showAnimBtn3"><textColor duration="1"><color>yellow</color><color>green</color><color>red</color><color>blue</color><color>purple</color><color>orange</color><color>brown</color><color>black</color></textColor>
<background duration="3"><color>red</color><color>blue</color><color>green</color><color>purple</color><color>orange</color><color>black</color><color>lightBlue</color><color>gray</color></background></animation>
<animation mode="linear" id="animation7" object="showAnimBtn2"><textColor duration="3"><color>yellow</color><color>green</color><color>red</color><color>blue</color><color>purple</color><color>orange</color><color>brown</color><color>black</color></textColor></animation>
<animation mode="linear" id="animation6" object="showAnimBtn1"><background duration="3"><color>red</color><color>blue</color><color>green</color><color>purple</color><color>orange</color><color>black</color><color>brown</color><color>gray</color></background></animation>
<animation id="btn3PosAnim" onDone="#animation6" object="showAnimBtn3"><move x="9" y="14" duration="0.8"/><size width="35" height="3" duration="1.2"/></animation>
<animation id="btn2PosAnim" onDone="#animation7" object="showAnimBtn2"><move x="9" y="10" duration="0.6"/><size width="35" height="3" duration="1.2"/></animation>
<animation id="btn1PosAnim" onDone="#animation8" object="showAnimBtn1"><move x="9" y="6" duration="0.4"/><size width="35" height="3" duration="1.2"/></animation>
<animation mode="linear" id="animation4" onDone="#btn1PosAnim" onDone="#btn2PosAnim" onDone="#btn3PosAnim" onDone="makeButtonVisible" object="animationButton"><move x="-15" y="4" duration="0.5"/><size width="1" height="1" duration="0.5"/></animation>
<animation mode="linear" id="animation3" onDone="#animation4" object="animationButton"><move x="12" y="14" duration="0.7"/></animation>
<animation mode="linear" id="animation2" onDone="#animation3" object="animationButton"><move x="32" y="16" duration="1"/></animation>
<animation mode="linear" id="animation1" onStart="disableStartButton" onDone="#animation2" object="animationButton"><move x="34" y="9" duration="1.2"/></animation>
<button id="animationButton" onClick="startAnimation" onClick="buttonColor" x="22" y="10" text="Start" />
</frame>
<frame id="example2" x="parent.w+1" width="parent.w" height="parent.h" bg="lightGray" >
<label text="Program" font="2" x="16" y="2" />
<program x="2" y="6" width="parent.w-12" height="parent.h-6" path="rom/programs/shell.lua" execute="true" />
<button onClick="buttonColor" onClick="p2" anchor="bottomRight" width="8" x="-7" y="-2" text="Down" />
<frame moveable="true" bar="true" barText="Program" x="2" y="parent.h+2" width="28" height="12" bg="black" >
<program id="program2" x="1" y="2" width="parent.w" height="parent.h-1" path="rom/programs/fun/worm.lua" execute="true" />
</frame>
<frame moveable="true" bar="true" barText="Program" x="6" y="parent.h+4" width="28" height="12" bg="black" >
<program id="program2" x="1" y="2" width="parent.w" height="parent.h-1" path="rom/programs/shell.lua" execute="true" />
</frame>
<button onClick="buttonColor" onClick="p1" width="8" x="parent.w-8" y="parent.h+2" text="Up" />
<button onClick="buttonColor" onClick="p3" width="8" x="parent.w-8" y="parent.h*2-3" text="Down" />
<button onClick="buttonColor" onClick="p2" width="8" x="parent.w-8" y="parent.h*2+2" text="Up" />
<program id="program3" onClick="test" x="2" y="parent.h*2+2" width="parent.w-12" height="parent.h-2" path="rom/programs/fun/worm.lua" execute="true" />
<button onClick="buttonColor" onClick="pauseP3" width="8" x="parent.w-8" y="parent.w*2+7" text="Pause" />
<button onClick="buttonColor" onClick="p3Up" width="1" height="1" x="parent.w-5" y="parent.h*2+11" text="^" />
<button onClick="buttonColor" onClick="p3Down" width="1" height="1" x="parent.w-5" y="parent.h*2+13" text="v" />
<button onClick="buttonColor" onClick="p3Left" width="1" height="1" x="parent.w-7" y="parent.h*2+12" text="<" />
<button onClick="buttonColor" onClick="p3Right" width="1" height="1" x="parent.w-3" y="parent.h*2+12" text="&#62;" />
<button onClick="buttonColor" onClick="ex1" x="2" y="2" width="8" height="1" text="Back" />
<button onClick="buttonColor" onClick="ex3" anchor="topRight" x="-7" y="2" width="8" height="1" text="Next" />
</frame>

291
examples/discordCC.lua Normal file
View File

@@ -0,0 +1,291 @@
local bot_id = "" -- put the bot id between the ""!
local servers = { -- setup the server/channels here, look at the example.
[""] = {
"",
},
--[[ Example:
["SERVER_ID"] = {
"CHANNEL_ID",
"CHANNEL_ID",
"CHANNEL_ID",
},
["SERVER_ID"] = {
"CHANNEL_ID",
"CHANNEL_ID",
"CHANNEL_ID",
},
]]
}
if(bot_id=="")then
error("Please setup the bot id and servers/channels first!")
end
--Basalt configurated installer
local filePath = "basalt.lua" --here you can change the file path default: basalt.lua
if not(fs.exists(filePath))then
shell.run("pastebin run ESs1mg7P packed true "..filePath:gsub(".lua", "")) -- this is an alternative to the wget command
end
local basalt = require(filePath:gsub(".lua", "")) -- here you can change the variablename in any variablename you want default: basalt
local main = basalt.createFrame():setBackground(colors.lightGray)
local loginFrame = main:addFrame():setBackground(colors.lightGray)
local messageFrameList = main:addFrame():setPosition("parent.w+1", 1):setBackground(colors.black):setScrollable(true):setImportantScroll(true)
local refreshRate = 2
local messageFrames = {}
local availableGuilds = {}
local channel_id = ""
for k,v in pairs(servers)do
if(v[1]~=nil)then
channel_id = v[1]
end
break
end
local function getAllGuilds(bot)
local content = http.get("https://discord.com/api/users/@me/guilds", {["Content-Type"] = "application/json", ["Authorization"] = "Bot "..bot})
if(content~=nil)then
return textutils.unserializeJSON(content.readAll())
end
end
local function getAllChannels(bot, guild)
local content = http.get("https://discord.com/api/guilds/"..guild.."/channels", {["Content-Type"] = "application/json", ["Authorization"] = "Bot "..bot})
if(content~=nil)then
local t = {}
for k,v in pairs(textutils.unserializeJSON(content.readAll()))do
table.insert(t, v.position, v)
end
return t
end
end
local splitString = function(str, sep)
if sep == nil then
sep = "%s"
end
local t={}
for v in string.gmatch(str, "([^"..sep.."]+)") do
table.insert(t, v)
end
if(#t==0)then table.insert(t,str) end
return t
end
local function createText(str, width)
local uniqueLines = splitString(str, "\n")
local lines = {}
for k,v in pairs(uniqueLines)do
local line = ""
local words = splitString(v, " ")
for a,b in pairs(words)do
if(#line+#b <= width)then
line = line=="" and b or line.." "..b
if(a==#words)then table.insert(lines, line) end
else
table.insert(lines, line)
line = b:sub(1,width)
if(a==#words)then table.insert(lines, line) end
end
end
end
return lines
end
local maxOffset = 0
local autoOffset = true
local function newMessage(position, msg, username, sendTime)
local lines = createText(msg, messageFrameList:getWidth()-5)
if(messageFrames[position]==nil)then
if(messageFrames[position-1]~=nil)then
messageFrames[position] = messageFrameList:addFrame("message"..tostring(position)):setPosition(2, "message"..(position-1)..".y + message"..(position-1)..".h")
else
messageFrames[position] = messageFrameList:addFrame("message"..tostring(position)):setPosition(2, 1)
end
messageFrames[position]:addLabel("title")
messageFrames[position]:addLabel("body")
end
maxOffset = maxOffset + #lines+3
if(autoOffset)then
messageFrameList:setOffset(0, maxOffset - messageFrameList:getHeight()+1)
end
messageFrames[position]:setSize("parent.w-1", #lines+3):setBackground(colors.black)
messageFrames[position]:getObject("title"):setSize("parent.w-2", 1):setPosition(2,1):setText(username):setForeground(colors.lightGray):setBackground(colors.gray)
messageFrames[position]:getObject("body"):setSize("parent.w-2", #lines+1):setPosition(2,3):setText(msg):setForeground(colors.lightGray)
end
local function updateDiscordMessages(channel, bot)
if(channel~=nil)and(bot~=nil)then
currentMessages = {}
local content = http.get("https://discord.com/api/channels/"..channel.."/messages?limit=25", {["Content-Type"] = "application/json", ["Authorization"] = "Bot "..bot})
if(content~=nil)then
local t = textutils.unserializeJSON(content.readAll())
local tR = {}
for i=#t, 1, -1 do
tR[#tR+1] = t[i]
end
for k,v in pairs(tR)do
newMessage(k, v.content, v.author.username, v.time)
end
end
end
end
local animations = {}
local function offsetAnimation(obj, x, y, t)
if(animations[obj:getName()]~=nil)then animations[obj:getName()]:cancel() end
animations[obj:getName()] = main:addAnimation():setAutoDestroy(true):setObject(obj):offset(x, y, t or 1):play()
end
local function positionAnimation(obj, x, y, t)
if(animations[obj:getName()]~=nil)then animations[obj:getName()]:cancel() end
animations[obj:getName()] = main:addAnimation():setAutoDestroy(true):setObject(obj):move(x, y, t or 1):play()
end
local sideBar = messageFrameList:addFrame():setPosition(-18, 1):setSize(20, "parent.h"):setZIndex(17):ignoreOffset():setScrollable(true):setImportantScroll(true)
sideBar:addButton():setText("Back"):setForeground(colors.lightGray):setBackground(colors.black):setPosition(3,2):setSize("parent.w - 4", 3):onClick(function()
offsetAnimation(main, 0, 0)
positionAnimation(sideBar, -18, 1)
end)
sideBar:addLabel():setText("Channels:"):setForeground(colors.black):setPosition(2,6)
sideBar:onClick(function(self, event)
if(event=="mouse_click")then
positionAnimation(self, 1, 1)
messageFrameList:setImportantScroll(false)
end
end)
sideBar:onLoseFocus(function()
positionAnimation(sideBar, -18, 1)
messageFrameList:setImportantScroll(true)
end)
local newTextFrame = messageFrameList:addFrame():setSize("parent.w - 4", 10):setPosition(3, 1):setZIndex(16):ignoreOffset():setBackground(colors.gray):setAnchor("bottomLeft")
local msgInfo = newTextFrame:addLabel():setText("Click here to write a message")
local messageField = newTextFrame:addTextfield():setSize("parent.w-2", "parent.h-4"):setPosition(2,3):setBackground(colors.lightGray)
newTextFrame:onClick(function(self, event)
if(event=="mouse_click")then
positionAnimation(self, 3, -8, 0.5)
messageFrameList:setImportantScroll(false)
msgInfo:setText("New Message:")
end
end)
messageFrameList:onScroll(function()
local xO, yO = messageFrameList:getOffset()
messageFrameList:getMaxScroll()
if(yO==messageFrameList:getMaxScroll())then
autoOffset = true
else
autoOffset = false
end
end)
local function messageBoxLoseFocus()
positionAnimation(newTextFrame, 3, 1, 0.5)
messageFrameList:setImportantScroll(true)
msgInfo:setText("Click here to write a message")
messageField:clear()
end
newTextFrame:addButton():setText("Cancel"):setAnchor("bottomLeft"):setBackground(colors.black):setForeground(colors.lightGray):setSize(12,1):setPosition(2,1):onClick(function()
messageBoxLoseFocus()
end)
newTextFrame:onLoseFocus(messageBoxLoseFocus)
loginFrame:addLabel():setAnchor("center"):setPosition(-2, -1):setText("Username:")
local nameInput = loginFrame:addInput():setAnchor("center"):setPosition(3,0):setBackground(colors.black):setForeground(colors.lightGray):setSize(16,1):setDefaultText("Username...", colors.gray)
local serverList = loginFrame:addList():setPosition(3, 6):setSize(16, 10)
local channelRadio = sideBar:addRadio():setForeground(colors.black):setBackground(colors.gray):setSelectedItem(colors.gray, colors.lightGray):setActiveSymbol(" ")
local channelObjects = {}
local updateChannels = basalt.shedule(function()
if(bot_id~=nil)then
for k,v in pairs(channelObjects)do
sideBar:removeObject(v)
end
channelObjects = {}
if(serverList:getValue().args~=nil)then
local y = 8
local maxScroll = 2
for k,v in pairs(servers[serverList:getValue().args[1]])do
local content = http.get("https://discord.com/api/channels/"..v, {["Content-Type"] = "application/json", ["Authorization"] = "Bot "..bot_id})
local channel = textutils.unserializeJSON(content.readAll())
if(channel~=nil)then
channelRadio:addItem("#"..channel.name,1, y, nil,nil,v)
y = y + 1
maxScroll = maxScroll + 1
end
end
end
end
end)
serverList:onChange(updateChannels)
basalt.shedule(function()
if(bot_id~=nil)then
for k,v in pairs(servers)do
local content = http.get("https://discord.com/api/guilds/"..k, {["Content-Type"] = "application/json", ["Authorization"] = "Bot "..bot_id})
local guild = textutils.unserializeJSON(content.readAll())
if(guild~=nil)then
serverList:addItem(guild.name,nil,nil,k)
end
end
end
end)()
updateChannels()
channelRadio:onChange(function(self)
local val = self:getValue()
if(val~=nil)and(val.args[1]~=nil)then
channel_id = val.args[1]
end
end)
loginFrame:addButton():setAnchor("bottomRight"):setPosition(-10, -2):setSize(11,3):setText("Login"):onClick(function()
offsetAnimation(main, main:getWidth(), 0)
end)
loginFrame:addLabel():setPosition(3, 5):setText("Servers:")
local function sendDiscordMessage(msg, channel, bot)
if(channel~=nil)and(bot~=nil)then
if(nameInput:getValue()~="")then
msg = string.gsub(msg, "\n", "\\n")
http.post("https://discord.com/api/channels/"..channel.."/messages", '{ "content": "['..nameInput:getValue()..']: '..msg..'" }', {["Content-Type"] = "application/json", ["Authorization"] = "Bot "..bot})
end
end
end
newTextFrame:addButton():setText("Send"):setAnchor("bottomRight"):setBackground(colors.black):setForeground(colors.lightGray):setSize(12,1):setPosition(-11,1)
:onClick(function()
local msg = table.concat(messageField:getLines(), "\n")
if(#msg>0)then
sendDiscordMessage(msg, channel_id, bot_id)
end
messageBoxLoseFocus()
end)
local function refreshMessages()
while true do
maxOffset = 0
updateDiscordMessages(channel_id, bot_id)
maxOffset = maxOffset - messageFrameList:getHeight()+1
messageFrameList:setMaxScroll(maxOffset)
sleep(refreshRate)
end
end
local thread = main:addThread():start(refreshMessages)
basalt.autoUpdate()

View File

@@ -0,0 +1,49 @@
--Basalt configurated installer
local filePath = "basalt.lua" --here you can change the file path default: basalt
if not(fs.exists(filePath))then
shell.run("pastebin run ESs1mg7P packed true "..filePath:gsub(".lua", "")) -- this is an alternative to the wget command
end
-- toastonrye's example: Redstone Analog Output
local basalt = require(filePath:gsub(".lua", "")) -- here you can change the variablename in any variablename you want default: basalt
local w, h = term.getSize() -- dimensions to use when drawing the sub frame
local main = basalt.createFrame()
:show()
:setBackground(colours.blue) -- using colours to easily determine what frame I'm in
local sub = main:addFrame()
:setPosition(2,2)
:setSize(w-2,h-2)
:setBackground(colours.lightBlue)
local rFrame = sub:addFrame("redstoneFrame")
:setPosition(1,1)
:setSize(25,5)
:setMoveable(true) -- the next release of Basalt will fix spelling to :setMovable
:setBackground(colours.red)
-- Redstone Analog Output
local redstoneAnalog = rFrame:addLabel() -- label that displays the value of the slider & Redstone output
:setPosition(18,3):setText("1")
redstone.setAnalogOutput("left", 1) -- initialize the redstone output to 1, to match the above label
rFrame:addLabel() -- draw a label on the frame
:setText("Redstone Analog Output")
:setPosition(1,2)
rFrame:addSlider()
:setPosition(1,3)
:onChange(function(self) -- when a player interacts with the slider, update the variable redstoneAnalog
redstoneAnalog:setText(self:getValue())
end)
:setMaxValue(15) -- max value of the slider, default 8. Redstone has 15 levels (16 including 0)
:setSize(15,1) -- draw the slider to this size, without this redstoneAnalog value can have decimals
redstoneAnalog:onChange(function(self) -- when the slider value changes, change the Redstone output to match
redstone.setAnalogOutput("left", tonumber(self:getValue()))
basalt.debug(self:getValue())
end)
basalt.autoUpdate()