Run upgrade to love11

This commit is contained in:
qfx 2020-02-13 19:29:00 +02:00
parent 9690d82140
commit 354abe1954
2 changed files with 100 additions and 109 deletions

181
README.md
View File

@ -1,33 +1,33 @@
# Helium # Helium
## user facing functions ## user facing functions
```lua ```lua
Element(function,reloader,w,h,parameters) --Creates a new element Element(function,reloader,w,h,parameters) --Creates a new element
:draw(x,y) --Renders the element at a location :draw(x,y) --Renders the element at a location
:undraw() --Removes the element from the render buffer :undraw() --Removes the element from the render buffer
--The intended loader for element files (supports optional live hotswapping) --The intended loader for element files (supports optional live hotswapping)
HeliumLoader(filepath) -> ElementFactory HeliumLoader(filepath) -> ElementFactory
ElementFactory(w,h,parameters) -> Element ElementFactory(w,h,parameters) -> Element
Input Input
.subscribe(x, y, w, h, subType, callback, startOn) .subscribe(x, y, w, h, subType, callback, startOn)
subType -- Subscription type subType -- Subscription type
callback -- Subscription callback on event callback -- Subscription callback on event
startOn -- a bool to disable a subscription by default startOn -- a bool to disable a subscription by default
-> Subscription -> Subscription
:on() --Turns an inactive subscription on :on() --Turns an inactive subscription on
:off() --Turns an active subscription off :off() --Turns an active subscription off
subType: subType:
Advanced: Advanced:
"clicked" --Gets called whenever the subscribed area is pressed, with an optional return callback "clicked" --Gets called whenever the subscribed area is pressed, with an optional return callback
"dragged" --Gets called whenever the subscribed area is dragged, with an optional 'finish' callback "dragged" --Gets called whenever the subscribed area is dragged, with an optional 'finish' callback
Basic events: Basic events:
"mousepressed" --Gets called whenever the subscribed area gets pressed "mousepressed" --Gets called whenever the subscribed area gets pressed
"mousereleased" --Gets called whenever mouse is released in the subscription area "mousereleased" --Gets called whenever mouse is released in the subscription area
"mousepressed_outside" --This type gets called when mouse is pressed outside the subscription area "mousepressed_outside" --This type gets called when mouse is pressed outside the subscription area
"mousereleased_outside" --This type gets called when mouse is released outside the sub area "mousereleased_outside" --This type gets called when mouse is released outside the sub area
"keypressed" --Basic keyboard input "keypressed" --Basic keyboard input
``` ```
## Basic overview: ## Basic overview:
@ -41,12 +41,12 @@ Create a new file for your awesome element, say 'helloWorld.lua'
The basic structure for an element file is: The basic structure for an element file is:
```lua ```lua
return function(param,state,view) return function(param,state,view)
--Setup zone --Setup zone
return function() return function()
--Rendering zone --Rendering zone
end
end end
end
``` ```
That's it, it's now a correct helium element That's it, it's now a correct helium element
@ -55,71 +55,16 @@ So lets make a simple button!
In helloWorld.lua: In helloWorld.lua:
```lua ```lua
local input = require "helium.core.input" local input = require "helium.core.input"
return function(param,state,view)
--Press state
state.pressed = false
--The callback for the input subscription
local callback = function() state.pressed = true end
--The actual input subscription
input.subscribe(0,0,view.w,view.h,'clicked',callback)
return function()
if state.pressed then
love.graphics.setColor(0.3,0.3,0.9)
else
love.graphics.setColor(0.3,0.3,0.5)
end
love.graphics.rectangle('fill', 0, 0, view.w, view.h)
love.graphics.setColor(1,1,1)
love.graphics.printf("Pressed? "..tostring(state.pressed),0,view.h/2-5,view.w,'center')
end
end
```
And in main.lua:
```lua
local buttonFactory = HeliumLoader('helloWorld.lua')
local button = buttonFactory({}, 200, 100)
button:draw(10,10)
```
![alt text](https://i.imgur.com/polli7q.jpg "Before")
![alt text](https://i.imgur.com/VGql2He.jpg "After")
Now theres a lot to explain, but its fairly simple, so lets take it by chunks
```lua
local input = require "helium.core.input"
```
Here we import the input module of Helium, so that we can later subscribe to an event
```lua
return function(param,state,view)
--Press state
state.pressed = false state.pressed = false
``` --The callback for the input subscription
Here we create a state field called pressed, think of state as a helium elements self
It works like a regular table, with the caveat that you shouldnt overwrite it directly like state = {}
```lua
local callback = function() state.pressed = true end local callback = function() state.pressed = true end
``` --The actual input subscription
Then we overwrite that state.pressed inside a callback which will be called every time our button is pressed
```lua
input.subscribe(0,0,view.w,view.h,'clicked',callback) input.subscribe(0,0,view.w,view.h,'clicked',callback)
```
This is creating an input subscription for the event of your choice
```lua
return function() return function()
if state.pressed then if state.pressed then
love.graphics.setColor(0.3,0.3,0.9) love.graphics.setColor(0.3,0.3,0.9)
@ -130,6 +75,56 @@ This is creating an input subscription for the event of your choice
love.graphics.setColor(1,1,1) love.graphics.setColor(1,1,1)
love.graphics.printf("Pressed? "..tostring(state.pressed),0,view.h/2-5,view.w,'center') love.graphics.printf("Pressed? "..tostring(state.pressed),0,view.h/2-5,view.w,'center')
end end
end
```
And in main.lua:
```lua
local buttonFactory = HeliumLoader('helloWorld.lua')
local button = buttonFactory({}, 200, 100)
button:draw(10,10)
```
![alt text](https://i.imgur.com/polli7q.jpg "Before")
![alt text](https://i.imgur.com/VGql2He.jpg "After")
Now theres a lot to explain, but its fairly simple, so lets take it by chunks
```lua
local input = require "helium.core.input"
```
Here we import the input module of Helium, so that we can later subscribe to an event
---
```lua
state.pressed = false
```
Here we create a state field called pressed, think of state as a helium elements self
It works like a regular table, with the caveat that you shouldnt overwrite it directly like state = {}
---
```lua
local callback = function() state.pressed = true end
```
Then we overwrite that state.pressed inside a callback which will be called every time our button is pressed
---
```lua
input.subscribe(0,0,view.w,view.h,'clicked',callback)
```
This is creating an input subscription for the event of your choice
---
```lua
return function()
if state.pressed then
love.graphics.setColor(0.3,0.3,0.9)
else
love.graphics.setColor(0.3,0.3,0.5)
end
love.graphics.rectangle('fill', 0, 0, view.w, view.h)
love.graphics.setColor(1,1,1)
love.graphics.printf("Pressed? "..tostring(state.pressed),0,view.h/2-5,view.w,'center')
end
``` ```
Is the rendering code, it works more or less like a mini window of a love.draw() Is the rendering code, it works more or less like a mini window of a love.draw()
@ -145,7 +140,7 @@ if autorun is off then you NEED to place helium.update(dt), helium.render() some
and if you need input, hook it up to the eventHandlers in your own love.run: and if you need input, hook it up to the eventHandlers in your own love.run:
```lua ```lua
if not(helium.input.eventHandlers[name]) or not(helium.input.eventHandlers[name](a, b, c, d, e, f)) then if not(helium.input.eventHandlers[name]) or not(helium.input.eventHandlers[name](a, b, c, d, e, f)) then
love.handlers[name](a, b, c, d, e, f) love.handlers[name](a, b, c, d, e, f)
end end
``` ```

View File

@ -13,6 +13,9 @@ helium.loader = require(path..".loader")
helium.elementBuffer = {} helium.elementBuffer = {}
function helium.render() function helium.render()
--We don't want any side effects affecting internal rendering
love.graphics.reset()
for i, e in ipairs(helium.elementBuffer) do for i, e in ipairs(helium.elementBuffer) do
e:externalRender() e:externalRender()
end end
@ -45,8 +48,8 @@ end
--[[ --[[
A user doesn't have to use this particular love.run A user doesn't have to use this particular love.run
*.element.bufferUpdate() helium.render()
*.draw() helium.update(dt)
Need to be called either through love.update and love.draw respectively Need to be called either through love.update and love.draw respectively
or put in to your custom love.run or put in to your custom love.run
@ -67,12 +70,7 @@ end
]] ]]
if helium.conf.AUTO_RUN then if helium.conf.AUTO_RUN then
function love.run() function love.run()
if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
if love.math then
love.math.setRandomSeed(os.time())
end
if love.load then love.load(arg) end
-- We don't want the first frame's dt to include time taken by love.load. -- We don't want the first frame's dt to include time taken by love.load.
if love.timer then love.timer.step() end if love.timer then love.timer.step() end
@ -80,14 +78,14 @@ if helium.conf.AUTO_RUN then
local dt = 0 local dt = 0
-- Main loop time. -- Main loop time.
while true do return function()
-- Process events. -- Process events.
if love.event then if love.event then
love.event.pump() love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do for name, a,b,c,d,e,f in love.event.poll() do
if name == "quit" then if name == "quit" then
if not love.quit or not love.quit() then if not love.quit or not love.quit() then
return a return a or 0
end end
end end
@ -99,25 +97,23 @@ if helium.conf.AUTO_RUN then
-- Update dt, as we'll be passing it to update -- Update dt, as we'll be passing it to update
if love.timer then if love.timer then dt = love.timer.step() end
love.timer.step()
dt = love.timer.getDelta()
end
-- Call update and draw -- Call update and draw
if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
helium.update(dt) helium.update(dt)
if love.graphics and love.graphics.isActive() then if love.graphics and love.graphics.isActive() then
love.graphics.clear(love.graphics.getBackgroundColor())
love.graphics.origin() love.graphics.origin()
love.graphics.clear(love.graphics.getBackgroundColor())
if love.draw then love.draw() end if love.draw then love.draw() end
helium.render() helium.render()
love.graphics.present() love.graphics.present()
end end
if love.timer then love.timer.sleep(0.00001) end if love.timer then love.timer.sleep(0.001) end
end end
end end
end end