Compare commits
350 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 45095191f4 | |||
| e44a3cd98b | |||
| fff3601041 | |||
| 1639de5d0f | |||
| 59462df9a6 | |||
| 72733394ac | |||
| dc9cee5a3e | |||
| 71ab702a75 | |||
| 1bb7410210 | |||
| ec5bf74009 | |||
| 7ea6873c1f | |||
| 8c3d66a7b1 | |||
| f5e95b8c9f | |||
| 2f11a80a28 | |||
| 8b6b56164a | |||
| bbc83acc0f | |||
|
|
0d6a9f92ad | ||
|
|
4cf80517eb | ||
| 35a86a6cda | |||
| 019855d81f | |||
| 2a47de9fd8 | |||
| 0d3de6e31d | |||
| d6d4934a7e | |||
| 6369450d2f | |||
| a90b1d09b1 | |||
| 92090f0a0a | |||
| 5cfd155241 | |||
| 0b2b2f9613 | |||
| a5b59ee601 | |||
| f240588edd | |||
| 6abfb9c3d9 | |||
| e5ac4d3d9e | |||
| 7353fde799 | |||
| 4c088b9080 | |||
| b727fb15b5 | |||
| 57b4871a85 | |||
| 3ae2acbd78 | |||
| df429b617e | |||
| 0d3a7b9393 | |||
| 6de93c10af | |||
| a14cbb45d3 | |||
| 827d3d8c45 | |||
| 7bbb217018 | |||
| 62e05788d2 | |||
| 772095431f | |||
| bb7fab0857 | |||
| 1546076c80 | |||
| 8f3eb6d9a3 | |||
| ccc4a15121 | |||
| 9050e65d93 | |||
| 8f7183f998 | |||
| 3d07e774a0 | |||
| 99ba411781 | |||
| 23eaf42cad | |||
| 5bca4e3383 | |||
|
|
e2bb964423 | ||
|
|
0e2119a87d | ||
|
|
7dfbf16bc1 | ||
|
|
bac91574a2 | ||
| bf2f495812 | |||
| beddecc742 | |||
| 86f31bbe52 | |||
| d8f3db6972 | |||
|
|
494baf1e32 | ||
| e14fa83681 | |||
|
|
278b982aff | ||
|
|
89e76ce31d | ||
|
|
831abeaccf | ||
|
|
f74f85759b | ||
| 7b70838567 | |||
| fcc0717115 | |||
| 47d8ac30c4 | |||
| 3646a1a074 | |||
| b0ab40d410 | |||
| 440995b1c8 | |||
| 8da4c4c1de | |||
| 2cc2a57a46 | |||
| 6527dc1aaa | |||
| bd49805c25 | |||
| 40dd293bf8 | |||
| 568c95fa73 | |||
| d30ee3788e | |||
| 79f58a79f9 | |||
| d8aeefd202 | |||
| c0fb94ddbb | |||
| e8a3cd731d | |||
| 03ffb6bc0a | |||
| 4240737e00 | |||
| 5172dcdf01 | |||
| fc18a303dd | |||
| 04d5500374 | |||
| 72e24e8a9b | |||
| 48bba84c08 | |||
| e9a0e7bbf7 | |||
| 264867a0da | |||
| 3f046afaa1 | |||
| 2acce5001d | |||
| 78cd15681a | |||
| 3401a8ac61 | |||
| 19ac257204 | |||
| c14a469069 | |||
| 03cea2d71a | |||
| 593bfd0d8c | |||
| bf60a354d4 | |||
| 6c73220a52 | |||
| c3a9ddfdbd | |||
| e05f2ea400 | |||
| a60aae02c6 | |||
| 207c5b8d69 | |||
| a9111f2fa3 | |||
| b572bf218d | |||
| 49c0bd3930 | |||
| cdb4bfda11 | |||
| 3fbead60d9 | |||
| 2b122f5c77 | |||
| 472d1748ee | |||
| 47178dd3b3 | |||
| 3fcba8825b | |||
| e194a06427 | |||
| 32f7b4492b | |||
| 913745a1bc | |||
| 889dc6ca68 | |||
| 2805137648 | |||
| f7167cf972 | |||
| 9e1ecb3583 | |||
| b9b9b51d12 | |||
| a708fb3f83 | |||
| 4335f3ed14 | |||
| 8580d92c9c | |||
| f1f6e30a98 | |||
| 6c1e9f26f0 | |||
| d98f353936 | |||
| 14c8665910 | |||
| f7452db3ec | |||
| 588923e1b7 | |||
| 4877f64ca1 | |||
| b8b31253d4 | |||
| af4672245e | |||
| 609613dbe9 | |||
| b16593425b | |||
| 997ea48b54 | |||
| 537dcf0db1 | |||
| b74a6c006e | |||
| b3453d028c | |||
| 6842147522 | |||
| de34b9dc59 | |||
| dff19d865f | |||
| 544aa78d70 | |||
| 91d0b5f7be | |||
| 27e03a2546 | |||
| 7dbcd01c33 | |||
| cc20914391 | |||
| c2aa449a65 | |||
| 57563688ae | |||
| 317dacd0de | |||
| a7ba146a64 | |||
| efa30e30cc | |||
| 197b418fc5 | |||
| d3d53599f7 | |||
| bf517facd1 | |||
| 9cff2735ba | |||
| ea77b934b6 | |||
| 74bfd571a5 | |||
| 06132fc1dd | |||
| ade5172f26 | |||
| bdc657771d | |||
| 8c24bcbbb0 | |||
| 804a117ed0 | |||
| 37afd37f9e | |||
| 4399fb6424 | |||
| 02a54e13ea | |||
| bb2c7d6440 | |||
| d1b8ed1922 | |||
| 9992a2c091 | |||
| 155466dc71 | |||
| cea6508d68 | |||
| 726707eb8a | |||
| 9a9d28f62f | |||
| ed924a3d9d | |||
| 2d239c65ea | |||
| fd8e77555a | |||
| b60aab9602 | |||
| 180176e2cf | |||
| 952b592b97 | |||
| d2ce7e070b | |||
| 300827b7bd | |||
| 9d97eac146 | |||
| 61dcb9da01 | |||
| b597fbdf9b | |||
| 8fbaa76fe9 | |||
| abb3da416f | |||
| 801c9e71d8 | |||
| 8ba489dc58 | |||
| 6039d6bf29 | |||
| 5ef2e0610c | |||
| dee687f4b1 | |||
| 2d281fae90 | |||
| 5d363f4fa7 | |||
| 7e506f79c9 | |||
| aeb88e4bfc | |||
| b44c46b953 | |||
| ddd365c5b8 | |||
| 68ac49fad3 | |||
| 5da0be08af | |||
| 2a55b044ef | |||
| 8063aa432c | |||
| 363fd4e639 | |||
| ff7f46393f | |||
| 08831e1dcb | |||
| b251758790 | |||
| 696891bded | |||
| 3a2bb0c737 | |||
| f5b9f093d6 | |||
| 16b0354c42 | |||
| 5b401ef455 | |||
| 87467afd17 | |||
| f737516009 | |||
| 3f147443bc | |||
| 8bd1e6d3f2 | |||
| 00a3c6731c | |||
| f47a750ef9 | |||
| 0d23aab726 | |||
| 500734d0db | |||
| 1f38b4dcb6 | |||
| 9fc89c6f5f | |||
| 56bce25519 | |||
| e1446c009d | |||
| 4384dbbaee | |||
| 8f5973343d | |||
| ebe87ed69a | |||
| ead2fd6f2c | |||
| cf6cdcfc6e | |||
| 4578c816d3 | |||
| 5435ee485f | |||
| 265777be4e | |||
| ce9cc7f3a2 | |||
| 5cceaef837 | |||
| aff459ef68 | |||
| ef151893ab | |||
| d797875317 | |||
| ec8c8c7074 | |||
| 927e45db77 | |||
| 173ad37c9b | |||
| 91abd762cb | |||
| a5b71985a6 | |||
| 1926d3e26c | |||
| e9e2096999 | |||
| 0446dd0bea | |||
| 2521a90712 | |||
| 06574d2d45 | |||
| aaff2244b1 | |||
| 6dbf2f5e93 | |||
| c251567804 | |||
| 26245032f7 | |||
| 64cd56d633 | |||
| 46029e3d60 | |||
| b9928ea8a0 | |||
| ab8f227f71 | |||
| 8484065479 | |||
| 504186a852 | |||
| bcb7e97184 | |||
| af88b2a054 | |||
| 05456fa25e | |||
| 48038429d0 | |||
| 91090e6dea | |||
| b3b9e12fb8 | |||
| 19de228834 | |||
| 1984c167d0 | |||
| a1d4d59c57 | |||
| 5377e3131f | |||
| 9c74f6c265 | |||
| 71b56023bb | |||
| 3ea801a2c4 | |||
| ae9f76d938 | |||
| c2488ed5ce | |||
| aa92282b8a | |||
| 689133e71f | |||
| 296d56d233 | |||
| 7dae5d5e32 | |||
| 29c8282efb | |||
| 498aa1f9aa | |||
| 3e795ede05 | |||
| 0476164cf6 | |||
| 564db18933 | |||
| 3bbd63ba62 | |||
| 7126e28530 | |||
| 11cfb390bb | |||
| afee0ace94 | |||
| 8afef44252 | |||
| 31f1e10cfc | |||
| 98d06ee027 | |||
| 87d8cfbb5b | |||
| 050549aeef | |||
| 5a05cb7525 | |||
| da259bc99d | |||
| 6714878ae9 | |||
| 2fad69a1ec | |||
| 391625674a | |||
| 4272397678 | |||
| 8f0f404c36 | |||
| 11cd2805c8 | |||
| 28a4e37c51 | |||
| 20647f4738 | |||
| 0fd604e356 | |||
| 03c8f364b2 | |||
| 41725dc01f | |||
| b5bc0f8e91 | |||
| 110055ffc4 | |||
| 4040e0d9d0 | |||
| 0abd8183b5 | |||
| 9b5acbd23e | |||
| 040d8842a2 | |||
| 72dcc95400 | |||
| 86845f4230 | |||
| dfc75fa56c | |||
| 358779c793 | |||
| c382eb46dc | |||
| 4bccb655b6 | |||
| 515f037c0a | |||
| d1f8bc9492 | |||
| 29cc0b8935 | |||
| 7eb9cd37e9 | |||
| eebd942555 | |||
| 8db42e19f9 | |||
| 21aed09d6d | |||
| 76dbf26793 | |||
| 9b67727a01 | |||
| 31e51ce3b3 | |||
| bbce42f0aa | |||
| d1507219ff | |||
| 5e8c50082c | |||
| 88dbb867a7 | |||
| dce6ea201e | |||
| fae39b79e7 | |||
| 2447526a12 | |||
| d4f41c5aa7 | |||
| aec5a88360 | |||
| fdfb3a6388 | |||
| 291c30a7ac | |||
| c88ad2e73b | |||
| cd3aae9c1e | |||
| 3a46c4ae44 | |||
| 979a6d8674 | |||
| 138d61de85 | |||
| 644044901d | |||
| de2b3ee100 | |||
| 02cdbc3bc2 | |||
| c7734a2afa | |||
| 3a7aea4131 | |||
| 9c3e66e9cc |
25
.github/workflows/love.yml
vendored
Normal file
25
.github/workflows/love.yml
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
name: Build & Run tests Love2d
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
runs-on: "ubuntu-latest"
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
submodules: recursive
|
||||
- name: Install love2d
|
||||
run: |
|
||||
sudo apt install fuse
|
||||
wget https://github.com/love2d/love/releases/download/11.4/love-11.4-x86_64.AppImage -O love.AppImage
|
||||
sudo chmod +x love.AppImage
|
||||
- name: Run Tests
|
||||
run: |
|
||||
./love.AppImage tests
|
||||
41
.github/workflows/nix_ci.yml
vendored
Normal file
41
.github/workflows/nix_ci.yml
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
name: Build & Run tests Ubuntu
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
build-type: [Release] # Debug
|
||||
lua: ["lua 5.1", "lua 5.2", "lua 5.3", "lua 5.4", "luajit 2.1.0-beta3"]
|
||||
os: ["ubuntu-latest"]
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: '3.10'
|
||||
|
||||
- name: Setup env
|
||||
run: |
|
||||
pip install hererocks
|
||||
hererocks lua-pkg --${{ matrix.lua }} -rlatest
|
||||
|
||||
- name: Install lanes and multi
|
||||
run: |
|
||||
source ${{github.workspace}}/lua-pkg/bin/activate
|
||||
luarocks install lanes
|
||||
luarocks install rockspecs/multi-16.0-0.rockspec
|
||||
|
||||
- name: Run Tests
|
||||
run: |
|
||||
source ${{github.workspace}}/lua-pkg/bin/activate
|
||||
lua tests/runtests.lua
|
||||
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,3 +1,3 @@
|
||||
|
||||
test.lua
|
||||
test2.lua
|
||||
*.code-workspace
|
||||
lua5.4/*
|
||||
test.lua
|
||||
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Ryan Ward
|
||||
Copyright (c) 2022 Ryan Ward
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
26
NetworkManager.md
Normal file
26
NetworkManager.md
Normal file
@ -0,0 +1,26 @@
|
||||
# NTHREAD Namespace
|
||||
- [ ] NTHREAD.set(name, val)
|
||||
- [ ] NTHREAD.get(name, val)
|
||||
- [ ] NTHREAD.waitFor(name)
|
||||
- [ ] NTHREAD.getCores()*
|
||||
- [ ] NTHREAD.getConsole()
|
||||
- [ ] NTHREAD.getThreads()
|
||||
- [ ] NTHREAD.kill()
|
||||
- [ ] NTHREAD.getName()
|
||||
- [ ] NTHREAD.getID()
|
||||
- [ ] NTHREAD.pushStatus(...)
|
||||
- [ ] NTHREAD.sleep(n)
|
||||
- [ ] NTHREAD.hold(n)
|
||||
- [ ] NTHREAD.setENV(env)
|
||||
- [ ] NTHREAD.getENV()
|
||||
|
||||
# Extensions
|
||||
- [ ] multi:newNetworkThreadedQueue(name)
|
||||
- [ ] multi:newNetworkThreadedTable(name)
|
||||
- [ ] multi:newNetworkThreadedJobQueue(n)
|
||||
- [ ] multi:newNetworkThreadedConnection(name)
|
||||
|
||||
# Core
|
||||
- [ ] NTHREAD:newFunction(func, holdme)
|
||||
- [ ] NTHREAD:newNetworkThread(name, func, ...)
|
||||
- [ ] mulit:newNetworkThread(name, func, ...)
|
||||
19
PManager.txt
19
PManager.txt
@ -1,19 +0,0 @@
|
||||
C: 2731526 ~I*7
|
||||
H: 2341308 ~I*6
|
||||
A: 1951090 ~I*5
|
||||
N: 1560872 ~I*4
|
||||
B: 1170655 ~I*3
|
||||
L: 780438 ~I*2
|
||||
I: 390219 ~I
|
||||
~n=I*PRank
|
||||
|
||||
P2
|
||||
---------------
|
||||
C: 6700821
|
||||
H: 1675205
|
||||
A: 418801
|
||||
N: 104700
|
||||
B: 26175
|
||||
L: 6543
|
||||
I: 1635
|
||||
~n=n*4
|
||||
1106
README.html
1106
README.html
File diff suppressed because one or more lines are too long
934
README.md
934
README.md
@ -1,882 +1,94 @@
|
||||
# multi Version: 1.11.0 (Show me the love, love2d 11.1 support is here see changelog for details. Plus a new threaded object for testing!)
|
||||
# Multi Version: 16.0.1 - Bug fix
|
||||
|
||||
**NOTE: I have been studying a lot about threading for the past few months and have some awesome additions in store! They will take a while to come out though. The goal of the library is still to provide a simple and efficient way to multi task in lua**
|
||||
Found an issue? Please [submit it](https://github.com/rayaman/multi/issues) and someone will look into it!
|
||||
|
||||
In Changes you'll find documentation for(In Order):
|
||||
- Sterilizing Objects
|
||||
- System Threaded Job Queues
|
||||
- New mainloop functions
|
||||
- System Threaded Tables
|
||||
- System Threaded Benchmark
|
||||
- System Threaded Queues
|
||||
- Threading related features
|
||||
- And backwards compat stuff
|
||||
My multitasking library for lua. It is a pure lua binding, with exceptions of the integrations.
|
||||
|
||||
My multitasking library for lua. It is a pure lua binding if you ingore the integrations and the love2d compat. If you find any bugs or have any issues please let me know :). **If you don't see a table of contents try using the ReadMe.html file. It is eaiser to navigate the readme**</br>
|
||||
</br>
|
||||
|
||||
[TOC]
|
||||
Progress is being made in [v16.1.0](https://github.com/rayaman/multi/tree/v16.1.0)
|
||||
---
|
||||
|
||||
</br>
|
||||
|
||||
INSTALLING
|
||||
----------
|
||||
Note: The latest version of lualanes is required if you want to make use of system threads on lua 5.1+. I will update the dependencies for luarocks since this library should work fine on lua 5.1+
|
||||
Link to optional dependencies:
|
||||
- [lanes](https://github.com/LuaLanes/lanes) `luarocks install lanes`
|
||||
|
||||
To install copy the multi folder into your enviroment and you are good to go</br>
|
||||
If you want to use the system threads then you'll need to install lanes!
|
||||
**or** use luarocks
|
||||
- [chronos](https://github.com/ldrumm/chronos) `luarocks install chronos`
|
||||
|
||||
- [love2d](https://love2d.org/)
|
||||
|
||||
When using love2d add multi:uManager() or any processor to love.update()
|
||||
|
||||
```lua
|
||||
function love.update(dt)
|
||||
multi:uManager()
|
||||
end
|
||||
```
|
||||
|
||||
To install copy the multi folder into your environment and you are good to go</br>
|
||||
If you want to use the system threads, then you'll need to install lanes or love2d game engine!
|
||||
|
||||
```
|
||||
luarocks install bin -- Inorder to use the new save state stuff
|
||||
luarocks install multi
|
||||
```
|
||||
Note: In the near future you may be able to run multitasking code on multiple machines, network paralisim. This however will have to wait until I hammer out some bugs within the core of system threading itself.
|
||||
|
||||
See the rambling section to get an idea of how this will work.
|
||||
|
||||
Discord
|
||||
-------
|
||||
For real-time assistance with my libraries! A place where you can ask questions and get help with any of my libraries. Also you can request features and stuff there as well.</br>
|
||||
https://discord.gg/U8UspuA</br>
|
||||
|
||||
**Upcoming Plans:** Adding network support for threading. Kinda like your own lua cloud. This will require the bin, net, and multi library. Once that happens I will include those libraries as a set. This also means that you can expect both a stand alone and joined versions of the libraries.
|
||||
Have a question or need realtime assistance? Feel free to join the discord!</br>
|
||||
https://discord.gg/U8UspuA
|
||||
|
||||
Planned features/TODO
|
||||
---------------------
|
||||
- [x] ~~Add system threads for love2d that works like the lanesManager (loveManager, slight differences).~~
|
||||
- [x] ~~Improve performance of the library~~
|
||||
- [x] ~~Improve coroutine based threading scheduling~~
|
||||
- [ ] Improve love2d Idle thread cpu usage/Fix the performance when using system threads in love2d... Tricky Look at the rambling section for insight.
|
||||
- [x] ~~Add more control to coroutine based threading~~
|
||||
- [ ] Add more control to system based threading
|
||||
- [ ] Make practical examples that show how you can solve real problems
|
||||
- [x] ~~Add more features to support module creators~~
|
||||
- [x] ~~Make a framework for eaiser thread task distributing~~
|
||||
- [x] ~~Fix Error handling on threaded multi objects~~ Non threaded multiobjs will crash your program if they error though! Use multi:newThread() of multi:newSystemThread() if your code can error! Unless you use multi:protect() this however lowers performance!
|
||||
- [x] ~~Add multi:OnError(function(obj,err))~~
|
||||
- [ ] sThread.wrap(obj) **May or may not be completed** Theory: Allows interaction in one thread to affect it in another. The addition to threaded tables may make this possible!
|
||||
- [ ] SystemThreaded Actors -- After some tests i figured out a way to make this work... It will work slightly different though. This is due to the actor needing to be splittable...
|
||||
- [ ] LoadBalancing for system threads (Once SystemThreaded Actors are done)
|
||||
- [x] ~~Add more integrations~~
|
||||
- [ ] Fix SystemThreadedTables
|
||||
- [ ] Finish the wiki stuff. (11% done)
|
||||
- [ ] Test for unknown bugs
|
||||
- [x] ~~Create test suite (In progress, mostly done)~~
|
||||
- [ ] Network Parallelism rework
|
||||
|
||||
Usage: [Check out the documentation for more info](https://github.com/rayaman/multi/blob/master/Documentation.md)
|
||||
-----
|
||||
|
||||
You can run tests in 2 ways:
|
||||
```
|
||||
lua tests/runtests.lua (Runs all tests, attempts to use lanes)
|
||||
love tests (Runs all tests in love2d env)
|
||||
```
|
||||
|
||||
```lua
|
||||
local multi, thread = require("multi"):init()
|
||||
GLOBAL, THREAD = require("multi.integration.threading"):init()
|
||||
|
||||
multi:newSystemThread("System Thread",function()
|
||||
while true do
|
||||
THREAD.sleep(.1)
|
||||
io.write(" World")
|
||||
THREAD.kill()
|
||||
end
|
||||
end)
|
||||
|
||||
multi:newThread("Coroutine Based Thread",function()
|
||||
while true do
|
||||
io.write("Hello")
|
||||
thread.sleep(.1)
|
||||
thread.kill()
|
||||
end
|
||||
end)
|
||||
|
||||
multi:newTLoop(function(loop)
|
||||
print("!")
|
||||
loop:Destroy()
|
||||
os.exit()
|
||||
end,.3)
|
||||
|
||||
multi:mainloop()
|
||||
|
||||
--[[
|
||||
while true do
|
||||
multi:uManager()
|
||||
end
|
||||
]]
|
||||
```
|
||||
|
||||
Known Bugs/Issues
|
||||
-----------------
|
||||
~~In regards to integrations, thread cancellation works slightly different for love2d and lanes. Within love2d I was unable to (To lazy to...) not use the multi library within the thread. A fix for this is to call `multi:Stop()` when you are done with your threaded code! This may change however if I find a way to work around this. In love2d in order to mimic the GLOBAL table I needed the library to constantly sync tha data... You can use the sThread.waitFor(varname), or sThread.hold(func) methods to sync the globals, to get the value instead of using GLOBAL and this could work. If you want to go this route I suggest setting multi.isRunning=true to prevent the auto runner from doing its thing! This will make the multi manager no longer function, but thats the point :P~~ THREAD.kill() should do the trick from within the thread. A listener could be made to detect when thread kill has been requested and sent to the running thread.
|
||||
|
||||
Another bug concerns the SystemThreadedJobQueue, Only 1 can be used for now. Going to change in a future update
|
||||
|
||||
~~And systemThreadedTables only supports 1 table between the main and worker thread! They do not work when shared between 2 or more threads. If you need that much flexiblity ust the GLOBAL table that all threads have.~~ **FIXED**
|
||||
|
||||
~~For module creators using this library. I suggest using SystemThreadedQueues for data transfer instead of SystemThreadedTables for rapid data transfer, If you plan on having Constants that will always be the same then a table is a good idea! They support up to **n** threads and can be messed with and abused as much as you want :D~~ FIXED Use what you want!
|
||||
|
||||
~~Love2D SystemThreadedTAbles do not send love2d userdata, use queues instead for that!~~ **FIXED**
|
||||
|
||||
Usage:</br>
|
||||
-----
|
||||
```lua
|
||||
-- Basic usage Alarms: Have been moved to the core of the library require("multi") would work as well
|
||||
require("multi") -- gets the entire library
|
||||
alarm=multi:newAlarm(3) -- in seconds can go to .001 uses the built in os.clock()
|
||||
alarm:OnRing(function(a)
|
||||
print("3 Seconds have passed!")
|
||||
a:Reset(n) -- if n were nil it will reset back to 3, or it would reset to n seconds
|
||||
end)
|
||||
multi:mainloop() -- the main loop of the program, multi:umanager() exists as well to allow integration in other loops Ex: love2d love.update function. More on this binding in the wiki!
|
||||
```
|
||||
The library is modular so you only need to require what you need to. Because of this, the global enviroment is altered</br>
|
||||
|
||||
There are many useful objects that you can use</br>
|
||||
Check out the wiki for detailed usage, but here are the objects:</br>
|
||||
- Process#</br>
|
||||
- QueueQueuer#</br>
|
||||
- Alarm</br>
|
||||
- Loop</br>
|
||||
- Event</br>
|
||||
- Step</br>
|
||||
- Range</br>
|
||||
- TStep</br>
|
||||
- TLoop</br>
|
||||
- Condition</br>
|
||||
- Connection</br>
|
||||
- Timer</br>
|
||||
- Updater</br>
|
||||
- Thread*</br>
|
||||
- Trigger</br>
|
||||
- Task</br>
|
||||
- Job</br>
|
||||
- Function</br>
|
||||
- Watcher</br>
|
||||
Note: *Both a process and queue act like the multi namespace, but allows for some cool things. Because they use the other objects an example on them will be done last*</br>
|
||||
*Uses the built in coroutine features of lua, these have an interesting interaction with the other means of multi-tasking</br>
|
||||
Triggers are kind of useless after the creation of the Connection</br>
|
||||
Watchers have no real purpose as well I made it just because.</br>
|
||||
|
||||
# Examples of each object being used</br>
|
||||
We already showed alarms in action so lets move on to a Loop object
|
||||
|
||||
Throughout these examples I am going to do some strange things in order to show other features of the library!
|
||||
|
||||
LOOPS
|
||||
-----
|
||||
```lua
|
||||
-- Loops: Have been moved to the core of the library require("multi") would work as well
|
||||
require("multi") -- gets the entire library
|
||||
count=0
|
||||
loop=multi:newLoop(function(self,dt) -- dt is delta time and self is a reference to itself
|
||||
count=count+1
|
||||
if count > 10 then
|
||||
self:Break() -- All methods on the multi objects are upper camel case, where as methods on the multi or process/queuer namespace are lower camel case
|
||||
-- self:Break() will stop the loop and trigger the OnBreak(func) method
|
||||
-- Stopping is the act of Pausing and deactivating the object! All objects can have the multiobj:Break() command on it!
|
||||
else
|
||||
print("Loop #"..count.."!")
|
||||
end
|
||||
end)
|
||||
loop:OnBreak(function(self)
|
||||
print("You broke me :(")
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
# Output
|
||||
Loop #1!</br>
|
||||
Loop #2!</br>
|
||||
Loop #3!</br>
|
||||
Loop #4!</br>
|
||||
Loop #5!</br>
|
||||
Loop #6!</br>
|
||||
Loop #7!</br>
|
||||
Loop #8!</br>
|
||||
Loop #9!</br>
|
||||
Loop #10!</br>
|
||||
You broke me :(</br>
|
||||
|
||||
|
||||
With loops out of the way lets go down the line
|
||||
|
||||
This library aims to be Async like. In reality everything is still on one thread *unless you are using the lanes integration module WIP* (More on that later)
|
||||
|
||||
EVENTS
|
||||
------
|
||||
```lua
|
||||
-- Events, these were the first objects introduced into the library. I seldomly use them in their pure form though, but later on you'll see their advance uses!
|
||||
-- Events on there own don't really do much... We are going to need 2 objects at least to get something going
|
||||
require("multi") -- gets the entire library
|
||||
count=0
|
||||
-- lets use the loop again to add to count!
|
||||
loop=multi:newLoop(function(self,dt)
|
||||
count=count+1
|
||||
end)
|
||||
event=multi:newEvent(function() return count==100 end) -- set the event
|
||||
event:OnEvent(function(self) -- connect to the event object
|
||||
loop:Pause() -- pauses the loop from running!
|
||||
print("Stopped that loop!")
|
||||
end) -- events like alarms need to be reset the Reset() command works here as well
|
||||
multi:mainloop()
|
||||
```
|
||||
# Output
|
||||
Stopped that loop!
|
||||
|
||||
STEPS
|
||||
-----
|
||||
```lua
|
||||
require("multi")
|
||||
-- Steps, are like for loops but non blocking... You can run a loop to infintity and everything will still run I will combine Steps with Ranges in this example.
|
||||
step1=multi:newStep(1,10,1,0) -- Some explaining is due. Argument 1 is the Start # Argument 2 is the ResetAt # (inclusive) Argument 3 is the count # (in our case we are counting by +1, this can be -1 but you need to adjust your start and resetAt numbers)
|
||||
-- The 4th Argument is for skipping. This is useful for timing and for basic priority management. A priority management system is included!
|
||||
step2=multi:newStep(10,1,-1,1) -- a second step, notice the slight changes!
|
||||
step1:OnStart(function(self)
|
||||
print("Step Started!")
|
||||
end)
|
||||
step1:OnStep(function(self,pos)
|
||||
if pos<=10 then -- what what is this? the step only goes to 10!!!
|
||||
print("Stepping... "..pos)
|
||||
else
|
||||
print("How did I get here?")
|
||||
end
|
||||
end)
|
||||
step1:OnEnd(function(self)
|
||||
print("Done!")
|
||||
-- We finished here, but I feel like we could have reused this step in some way... Yeah I soule Reset() it, but what if i wanted to change it...
|
||||
if self.endAt==10 then -- lets only loop once
|
||||
self:Update(1,11,1,0) -- oh now we can reach that else condition!
|
||||
end
|
||||
-- Note Update() will restart the step!
|
||||
end)
|
||||
|
||||
-- step2 is bored lets give it some love :P
|
||||
step2.range=step2:newRange() -- Set up a range object to have a nested step in a sense! Each nest requires a new range
|
||||
-- it is in your interest not to share ranges between objects! You can however do it if it suits your needs though
|
||||
step2:OnStep(function(self,pos)
|
||||
-- for 1=1,math.huge do
|
||||
-- print("Haha I am holding the code up because I can!!!")
|
||||
--end
|
||||
-- We dont want to hold things up, but we want to nest.
|
||||
-- Note a range is not nessary if the nested for loop has a small range, if however the range is rather large you may want to allow other objects to do some work
|
||||
for i in self.range(1,100) do
|
||||
print(pos,i) -- Now our nested for loop is using a range object which allows for other objects to get some cpu time while this one is running
|
||||
end
|
||||
end)
|
||||
-- TSteps are just like alarms and steps mixed together, the only difference in construction is the 4th Argument. On a TStep that argument controls time. The defualt is 1
|
||||
-- The Reset(n) works just like you would figure!
|
||||
step3=multi:newTStep(1,10,.5,2) -- lets go from 1 to 10 counting by .5 every 2 seconds
|
||||
step3:OnStep(function(self,pos)
|
||||
print("Ok "..pos.."!")
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
# Output
|
||||
|
||||
Note: the output on this one is huge!!! So I had to ... some parts! You need to run this for your self to see what is going on!</br>
|
||||
Step Started!</br>
|
||||
Stepping... 1</br>
|
||||
10 1</br>
|
||||
Stepping... 2</br>
|
||||
10 2</br>
|
||||
Stepping... 3</br>
|
||||
10 3</br>
|
||||
...</br>
|
||||
Ok 9.5!</br>
|
||||
Ok 10!</br>
|
||||
|
||||
TLOOPS
|
||||
------
|
||||
```lua
|
||||
require("multi")
|
||||
-- TLoops are loops that run ever n second. We will also look at condition objects as well
|
||||
-- Here we are going to modify the old loop to be a little different
|
||||
count=0
|
||||
loop=multi:newTLoop(function(self) -- We are only going to coult with this loop, but doing so using a condition!
|
||||
while self:condition(self.cond) do
|
||||
count=count+1
|
||||
end
|
||||
print("Count is "..count.."!")
|
||||
self:Destroy() -- Lets destroy this object, casting it to the dark abyss MUHAHAHA!!!
|
||||
-- the reference to this object will be a phantom object that does nothing!
|
||||
end,1) -- Notice the ',1' after the function! This is where you put your time value!
|
||||
loop.cond=multi:newCondition(function() return count<=100 end) -- conditions need a bit of work before i am happy with them
|
||||
multi:mainloop()
|
||||
```
|
||||
# Output
|
||||
Count is 101!
|
||||
|
||||
Connections
|
||||
-----------
|
||||
These are my favorite objects and you'll see why. They are very useful objects for ASync connections!
|
||||
|
||||
```lua
|
||||
require("multi")
|
||||
-- Lets create the events
|
||||
yawn={} -- ill just leave that there
|
||||
OnCustomSafeEvent=multi:newConnection(true) -- lets pcall the calls incase something goes wrong defualt
|
||||
OnCustomEvent=multi:newConnection(false) -- lets not pcall the calls and let errors happen... We are good at coding though so lets get a speed advantage by not pcalling. Pcalling is useful for plugins and stuff that may have been coded badly and you can ingore those connections if need be.
|
||||
OnCustomEvent:Bind(yawn) -- create the connection lookup data in yawn
|
||||
|
||||
-- Lets connect to them, a recent update adds a nice syntax to connect to these
|
||||
cd1=OnCustomSafeEvent:Connect(function(arg1,arg2,...)
|
||||
print("CSE1",arg1,arg2,...)
|
||||
end,"bob") -- lets give this connection a name
|
||||
cd2=OnCustomSafeEvent:Connect(function(arg1,arg2,...)
|
||||
print("CSE2",arg1,arg2,...)
|
||||
end,"joe") -- lets give this connection a name
|
||||
cd3=OnCustomSafeEvent:Connect(function(arg1,arg2,...)
|
||||
print("CSE3",arg1,arg2,...)
|
||||
end) -- lets not give this connection a name
|
||||
|
||||
-- no need for connect, but I kept that function because of backwards compatibility.
|
||||
OnCustomEvent(function(arg1,arg2,...)
|
||||
print(arg1,arg2,...)
|
||||
end)
|
||||
|
||||
-- Now within some loop/other object you trigger the connection like
|
||||
OnCustomEvent:Fire(1,2,"Hello!!!") -- fire all conections
|
||||
|
||||
-- You may have noticed that some events have names! See the following example!
|
||||
OnCustomSafeEvent:getConnection("bob"):Fire(1,100,"Bye!") -- fire only bob!
|
||||
OnCustomSafeEvent:getConnection("joe"):Fire(1,100,"Hello!") -- fire only joe!!
|
||||
OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all!!!
|
||||
|
||||
-- Connections have more to them than that though!
|
||||
-- As seen above cd1-cd3 these are hooks to the connection object. This allows you to remove a connection
|
||||
-- For Example:
|
||||
cd1:Remove() -- remove this connection from the master connection object
|
||||
print("------")
|
||||
OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all again!!!
|
||||
-- To remove all connections use:
|
||||
OnCustomSafeEvent:Remove()
|
||||
print("------")
|
||||
OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all again!!!
|
||||
```
|
||||
# Output
|
||||
1 2 Hello!!!</br>
|
||||
CSE1 1 100 Bye!</br>
|
||||
CSE2 1 100 Hello!</br>
|
||||
CSE1 1 100 Hi Ya Folks!!!</br>
|
||||
CSE2 1 100 Hi Ya Folks!!!</br>
|
||||
CSE3 1 100 Hi Ya Folks!!!</br>
|
||||
CSE2 1 100 Hi Ya Folks!!!</br>
|
||||
CSE3 1 100 Hi Ya Folks!!!</br>
|
||||
</br>
|
||||
|
||||
You may think timers should be bundled with alarms, but they are a bit different and have cool features</br>
|
||||
TIMERS
|
||||
------
|
||||
```lua
|
||||
-- You see the thing is that all time based objects use timers eg. Alarms, TSteps, and Loops. Timers are more low level!
|
||||
require("multi")
|
||||
local clock = os.clock
|
||||
function sleep(n) -- seconds
|
||||
local t0 = clock()
|
||||
while clock() - t0 <= n do end
|
||||
end -- we will use this later!
|
||||
|
||||
timer=multi:newTimer()
|
||||
timer:Start()
|
||||
-- lets do a mock alarm
|
||||
set=3 -- 3 seconds
|
||||
a=0
|
||||
while timer:Get()<=set do
|
||||
-- waiting...
|
||||
a=a+1
|
||||
end
|
||||
print(set.." second(s) have passed!")
|
||||
-- Timers can do one more thing that is interesting and that is pausing them!
|
||||
timer:Pause()
|
||||
print(timer:Get()) -- should be really close to 'set'
|
||||
sleep(3)
|
||||
print(timer:Get()) -- should be really close to 'set'
|
||||
timer:Resume()
|
||||
sleep(1)
|
||||
print(timer:Get()) -- should be really close to the value of set + 1
|
||||
timer:Pause()
|
||||
print(timer:Get()) -- should be really close to 'set'
|
||||
sleep(3)
|
||||
print(timer:Get()) -- should be really close to 'set'
|
||||
timer:Resume()
|
||||
sleep(1)
|
||||
print(timer:Get()) -- should be really close to the value of set + 2
|
||||
```
|
||||
# Output
|
||||
Note: This will make more sense when you run it for your self</br>
|
||||
3 second(s) have passed!</br>
|
||||
3.001</br>
|
||||
3.001</br>
|
||||
4.002</br>
|
||||
4.002</br>
|
||||
4.002</br>
|
||||
5.003</br>
|
||||
|
||||
UPDATER
|
||||
-------
|
||||
```lua
|
||||
-- Updaters: Have been moved to the core of the library require("multi") would work as well
|
||||
require("multi")
|
||||
updater=multi:newUpdater(5) -- really simple, think of a look with the skip feature of a step
|
||||
updater:OnUpdate(function(self)
|
||||
--print("updating...")
|
||||
end)
|
||||
-- Here every 5 steps the updater will do stuff!
|
||||
-- But I feel it is now time to touch into priority management, so lets get into basic priority stuff and get into a more advance version of it
|
||||
--[[
|
||||
multi.Priority_Core -- Highest form of priority
|
||||
multi.Priority_High
|
||||
multi.Priority_Above_Normal
|
||||
multi.Priority_Normal -- The defualt form of priority
|
||||
multi.Priority_Below_Normal
|
||||
multi.Priority_Low
|
||||
multi.Priority_Idle -- Lowest form of priority
|
||||
|
||||
Note: These only take effect when you enable priority, otherwise everything is at a core like level!
|
||||
We aren't going to use regular objects to test priority, but rather benchmarks!
|
||||
to set priority on an object though you would do
|
||||
multiobj:setPriority(one of the above)
|
||||
]]
|
||||
-- lets bench for 3 seconds using the 3 forms of priority! First no Priority
|
||||
multi:benchMark(3,nil,"Regular Bench: "):OnBench(function() -- the onbench() allows us to do each bench after each other!
|
||||
print("P1\n---------------")
|
||||
multi:enablePriority()
|
||||
multi:benchMark(3,multi.Priority_Core,"Core:")
|
||||
multi:benchMark(3,multi.Priority_High,"High:")
|
||||
multi:benchMark(3,multi.Priority_Above_Normal,"Above_Normal:")
|
||||
multi:benchMark(3,multi.Priority_Normal,"Normal:")
|
||||
multi:benchMark(3,multi.Priority_Below_Normal,"Below_Normal:")
|
||||
multi:benchMark(3,multi.Priority_Low,"Low:")
|
||||
multi:benchMark(3,multi.Priority_Idle,"Idle:"):OnBench(function()
|
||||
print("P2\n---------------")
|
||||
-- Finally the 3rd form
|
||||
multi:enablePriority2()
|
||||
multi:benchMark(3,multi.Priority_Core,"Core:")
|
||||
multi:benchMark(3,multi.Priority_High,"High:")
|
||||
multi:benchMark(3,multi.Priority_Above_Normal,"Above_Normal:")
|
||||
multi:benchMark(3,multi.Priority_Normal,"Normal:")
|
||||
multi:benchMark(3,multi.Priority_Below_Normal,"Below_Normal:")
|
||||
multi:benchMark(3,multi.Priority_Low,"Low:")
|
||||
multi:benchMark(3,multi.Priority_Idle,"Idle:")
|
||||
end)
|
||||
end)
|
||||
multi:mainloop() -- Notice how the past few examples did not need this, well only actors need to be in a loop! More on this in the wiki.
|
||||
```
|
||||
# Output
|
||||
Note: These numbers will vary drastically depending on your compiler and cpu power</br>
|
||||
Regular Bench: 2094137 Steps in 3 second(s)!</br>
|
||||
P1</br>
|
||||
Below_Normal: 236022 Steps in 3 second(s)!</br>
|
||||
Normal: 314697 Steps in 3 second(s)!</br>
|
||||
Above_Normal: 393372 Steps in 3 second(s)!</br>
|
||||
High: 472047 Steps in 3 second(s)!</br>
|
||||
Core: 550722 Steps in 3 second(s)!</br>
|
||||
Low: 157348 Steps in 3 second(s)!</br>
|
||||
Idle: 78674 Steps in 3 second(s)!</br>
|
||||
P2</br>
|
||||
Core: 994664 Steps in 3 second(s)!</br>
|
||||
High: 248666 Steps in 3 second(s)!</br>
|
||||
Above_Normal: 62166 Steps in 3 second(s)!</br>
|
||||
Normal: 15541 Steps in 3 second(s)!</br>
|
||||
Below_Normal: 3885 Steps in 3 second(s)!</br>
|
||||
Idle: 242 Steps in 3 second(s)!</br>
|
||||
Low: 971 Steps in 3 second(s)!</br>
|
||||
|
||||
Notice: Even though I started each bench at the same time the order that they finished differed the order is likely to vary on your machine as well!</br>
|
||||
|
||||
Processes
|
||||
---------
|
||||
A process allows you to group the Actor objects within a controlable interface
|
||||
```lua
|
||||
require("multi")
|
||||
proc=multi:newProcess() -- takes an optional file as an argument, but for this example we aren't going to use that
|
||||
-- a process works just like the multi object!
|
||||
b=0
|
||||
loop=proc:newTLoop(function(self)
|
||||
a=a+1
|
||||
proc:Pause() -- pauses the cpu cycler for this processor! Individual objects are not paused, however because they aren't getting cpu time they act as if they were paused
|
||||
end,.1)
|
||||
updater=proc:newUpdater(multi.Priority_Idle) -- priority can be used in skip arguments as well to manage priority without enabling it!
|
||||
updater:OnUpdate(function(self)
|
||||
b=b+1
|
||||
end)
|
||||
a=0 -- a counter
|
||||
loop2=proc:newLoop(function(self,dt)
|
||||
print("Lets Go!")
|
||||
self:hold(3) -- this will keep this object from doing anything! Note: You can only have one hold active at a time! Multiple are possible, but results may not be as they seem see * for how hold works
|
||||
-- Within a process using hold will keep it alive until the hold is satisified!
|
||||
print("Done being held for 1 second")
|
||||
self:hold(function() return a>10 end)
|
||||
print("A is now: "..a.." b is also: "..b)
|
||||
self:Destroy()
|
||||
self.Parent:Pause() -- lets say you don't have the reference to the process!
|
||||
os.exit()
|
||||
end)
|
||||
-- Notice this is now being created on the multi namespace
|
||||
event=multi:newEvent(function() return os.clock()>=1 end)
|
||||
event:OnEvent(function(self)
|
||||
proc:Resume()
|
||||
self:Destroy()
|
||||
end)
|
||||
proc:Start()
|
||||
multi:mainloop()
|
||||
```
|
||||
# Output
|
||||
Lets Go!</br>
|
||||
Done being held for 1 second</br>
|
||||
A is now: 29 b is also: 479</br>
|
||||
|
||||
**Hold: This method works as follows**
|
||||
```lua
|
||||
function multi:hold(task)
|
||||
self:Pause() -- pause the current object
|
||||
self.held=true -- set held
|
||||
if type(task)=='number' then -- a sleep cmd
|
||||
local timer=multi:newTimer()
|
||||
timer:Start()
|
||||
while timer:Get()<task do -- This while loop is what makes using multiple holds tricky... If the outer while is good before the nested one then the outter one will have to wait! There is a way around this though!
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
self:Resume()
|
||||
self.held=false
|
||||
elseif type(task)=='function' then
|
||||
local env=self.Parent:newEvent(task)
|
||||
env:OnEvent(function(envt) envt:Pause() envt.Active=false end)
|
||||
while env.Active do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
env:Destroy()
|
||||
self:Resume()
|
||||
self.held=false
|
||||
else
|
||||
print('Error Data Type!!!')
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
Queuer (WIP)
|
||||
------------
|
||||
A queuer works just like a process however objects are processed in order that they were created...
|
||||
```lua
|
||||
require("multi")
|
||||
queue = multi:newQueuer()
|
||||
queue:newAlarm(3):OnRing(function()
|
||||
print("Ring ring!!!")
|
||||
end)
|
||||
queue:newStep(1,10):OnStep(function(self,pos)
|
||||
print(pos)
|
||||
end)
|
||||
queue:newLoop(function(self,dt)
|
||||
if dt==3 then
|
||||
self:Break()
|
||||
print("Done")
|
||||
end
|
||||
end)
|
||||
queue:Start()
|
||||
multi:mainloop()
|
||||
```
|
||||
# Expected Output
|
||||
Note: the queuer still does not work as expected!</br>
|
||||
Ring ring!!!</br>
|
||||
1</br>
|
||||
2</br>
|
||||
3</br>
|
||||
4</br>
|
||||
5</br>
|
||||
6</br>
|
||||
7</br>
|
||||
8</br>
|
||||
9</br>
|
||||
10</br>
|
||||
Done</br>
|
||||
# Actual Output
|
||||
Done</br>
|
||||
1</br>
|
||||
2</br>
|
||||
3</br>
|
||||
4</br>
|
||||
5</br>
|
||||
6</br>
|
||||
7</br>
|
||||
8</br>
|
||||
9</br>
|
||||
10</br>
|
||||
Ring ring!!!</br>
|
||||
|
||||
Threads
|
||||
-------
|
||||
These fix the hold problem that you get with regular objects, and they work exactly the same! They even have some extra features that make them really useful.</br>
|
||||
```lua
|
||||
require("multi")
|
||||
test=multi:newThreadedProcess("main") -- you can thread processors and all Actors see note for a list of actors you can thread!
|
||||
test2=multi:newThreadedProcess("main2")
|
||||
count=0
|
||||
test:newLoop(function(self,dt)
|
||||
count=count+1
|
||||
thread.sleep(.01)
|
||||
end)
|
||||
test2:newLoop(function(self,dt)
|
||||
print("Hello!")
|
||||
thread.sleep(1) -- sleep for some time
|
||||
end)
|
||||
-- threads take a name object then the rest as normal
|
||||
step=multi:newThreadedTStep("step",1,10)
|
||||
step:OnStep(function(self,p)
|
||||
print("step",p)
|
||||
thread.skip(21) -- skip n cycles
|
||||
end)
|
||||
step:OnEnd(function()
|
||||
print("Killing thread!")
|
||||
thread.kill() -- kill the thread
|
||||
end)
|
||||
loop=multi:newThreadedLoop("loop",function(self,dt)
|
||||
print(dt)
|
||||
thread.sleep(1.1)
|
||||
end)
|
||||
loop2=multi:newThreadedLoop("loop",function(self,dt)
|
||||
print(dt)
|
||||
thread.hold(function() return count>=100 end)
|
||||
print("Count is "..count)
|
||||
os.exit()
|
||||
end)
|
||||
alarm=multi:newThreadedAlarm("alarm",1)
|
||||
alarm:OnRing(function(self)
|
||||
print("Ring")
|
||||
self:Reset()
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
# Output
|
||||
Ring</br>
|
||||
0.992</br>
|
||||
0.992</br>
|
||||
Hello!</br>
|
||||
step 1</br>
|
||||
step 2</br>
|
||||
Hello!</br>
|
||||
Ring</br>
|
||||
2.092</br>
|
||||
step 3</br>
|
||||
Hello!</br>
|
||||
Ring</br>
|
||||
Count is 100</br>
|
||||
Threadable Actors
|
||||
-----------------
|
||||
- Alarms
|
||||
- Events
|
||||
- Loop/TLoop
|
||||
- Process
|
||||
- Step/TStep
|
||||
|
||||
Functions
|
||||
---------
|
||||
If you ever wanted to pause a function then great now you can
|
||||
The uses of the Function object allows one to have a method that can run free in a sense
|
||||
```lua
|
||||
require("multi")
|
||||
func=multi:newFunction(function(self,arg1,arg2,...)
|
||||
self:Pause()
|
||||
return arg1
|
||||
end)
|
||||
print(func("Hello"))
|
||||
print(func("Hello2")) -- returns PAUSED allows for the calling of functions that should only be called once. returns PAUSED instantly if paused
|
||||
func:Resume()
|
||||
print(func("Hello3"))
|
||||
```
|
||||
# Output
|
||||
Hello</br>
|
||||
PAUSED</br>
|
||||
Hello3</br>
|
||||
|
||||
ThreadedUpdater
|
||||
---------------
|
||||
|
||||
```lua
|
||||
-- Works the same as a regular updater!
|
||||
require("multi")
|
||||
multi:newThreadedUpdater("Test",10000):OnUpdate(function(self)
|
||||
print(self.pos)
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
# Output
|
||||
1</br>
|
||||
2</br>
|
||||
...</br>
|
||||
.inf</br>
|
||||
|
||||
Triggers
|
||||
--------
|
||||
Triggers were what I used before connections became a thing, also Function objects are a lot like triggers and can be paused as well, while triggers cannot...</br>
|
||||
They are simple to use, but in most cases you are better off using a connection</br>
|
||||
```lua
|
||||
require("multi")
|
||||
-- They work like connections but can only have one event binded to them
|
||||
trig=multi:newTrigger(function(self,a,b,c,...)
|
||||
print(a,b,c,...)
|
||||
end)
|
||||
trig:Fire(1,2,3)
|
||||
trig:Fire(1,2,3,"Hello",true)
|
||||
```
|
||||
|
||||
# Output
|
||||
1 2 3</br>
|
||||
1 2 3 Hello true</br>
|
||||
|
||||
Tasks
|
||||
-----
|
||||
Tasks allow you to run a block of code before the multi mainloops does it thing. Tasks still have a use, but depending on how you program they aren't needed.</br>
|
||||
```lua
|
||||
require("multi")
|
||||
multi:newTask(function()
|
||||
print("Hi!")
|
||||
end)
|
||||
multi:newLoop(function(self,dt)
|
||||
print("Which came first the task or the loop?")
|
||||
self:Break()
|
||||
end)
|
||||
multi:newTask(function()
|
||||
print("Hello there!")
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
# Output
|
||||
Hi!</br>
|
||||
Hello there!</br>
|
||||
Which came first the task or the loop?</br>
|
||||
|
||||
As seen in the example above the tasks were done before anything else in the mainloop! This is useful when making libraries around the multitasking features and you need things to happen in a certain order!</br>
|
||||
|
||||
Jobs
|
||||
----
|
||||
Jobs were a strange feature that was created for throttling connections! When I was building a irc bot around this library I couldn't have messages posting too fast due to restrictions. Jobs allowed functions to be added to a queue that were executed after a certain amount of time has passed
|
||||
```lua
|
||||
require("multi") -- jobs use alarms I am pondering if alarms should be added to the core or if jobs should use timers instead...
|
||||
-- jobs are built into the core of the library so no need to require them
|
||||
print(multi:hasJobs())
|
||||
multi:setJobSpeed(1) -- set job speed to 1 second
|
||||
multi:newJob(function()
|
||||
print("A job!")
|
||||
end,"test")
|
||||
|
||||
multi:newJob(function()
|
||||
print("Another job!")
|
||||
multi:removeJob("test") -- removes all jobs with name "test"
|
||||
end,"test")
|
||||
|
||||
multi:newJob(function()
|
||||
print("Almost done!")
|
||||
end,"test")
|
||||
|
||||
multi:newJob(function()
|
||||
print("Final job!")
|
||||
end,"test")
|
||||
print(multi:hasJobs())
|
||||
print("There are "..multi:getJobs().." jobs in the queue!")
|
||||
multi:mainloop()
|
||||
```
|
||||
# Output
|
||||
false 0</br>
|
||||
true 4</br>
|
||||
There are 4 jobs in the queue!</br>
|
||||
A job!</br></br>
|
||||
Another job!</br>
|
||||
|
||||
Watchers
|
||||
--------
|
||||
Watchers allow you to monitor a variable and trigger an event when the variable has changed!
|
||||
```lua
|
||||
require("multi")
|
||||
a=0
|
||||
watcher=multi:newWatcher(_G,"a") -- watch a in the global enviroment
|
||||
watcher:OnValueChanged(function(self,old,new)
|
||||
print(old,new)
|
||||
end)
|
||||
tloop=multi:newTLoop(function(self)
|
||||
a=a+1
|
||||
end,1)
|
||||
multi:mainloop()
|
||||
```
|
||||
# Output
|
||||
0 1</br>
|
||||
1 2</br>
|
||||
2 3</br>
|
||||
...</br>
|
||||
.inf-1 inf</br>
|
||||
|
||||
Timeout management
|
||||
------------------
|
||||
```lua
|
||||
-- Note: I used a tloop so I could control the output of the program a bit.
|
||||
require("multi")
|
||||
a=0
|
||||
inc=1 -- change to 0 to see it not met at all, 1 if you want to see the first condition not met but the second and 2 if you want to see it meet the condition on the first go.
|
||||
loop=multi:newTLoop(function(self)
|
||||
print("Looping...")
|
||||
a=a+inc
|
||||
if a==14 then
|
||||
self:ResolveTimer("1","2","3") -- ... any number of arguments can be passed to the resolve handler
|
||||
-- this will also automatically pause the object that it is binded to
|
||||
end
|
||||
end,.1)
|
||||
loop:SetTime(1)
|
||||
loop:OnTimerResolved(function(self,a,b,c) -- the handler will return the self and the passed arguments
|
||||
print("We did it!",a,b,c)
|
||||
end)
|
||||
loop:OnTimedOut(function(self)
|
||||
if not TheSecondTry then
|
||||
print("Loop timed out!",self.Type,"Trying again...")
|
||||
self:ResetTime(2)
|
||||
self:Resume()
|
||||
TheSecondTry=true
|
||||
else
|
||||
print("We just couldn't do it!") -- print if we don't get anything working
|
||||
end
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
# Output (Change the value inc as indicated in the comment to see the outcomes!)
|
||||
Looping...</br>
|
||||
Looping...</br>
|
||||
Looping...</br>
|
||||
Looping...</br>
|
||||
Looping...</br>
|
||||
Looping...</br>
|
||||
Looping...</br>
|
||||
Looping...</br>
|
||||
Looping...</br>
|
||||
Loop timed out! tloop Trying again...</br>
|
||||
Looping...</br>
|
||||
Looping...</br>
|
||||
Looping...</br>
|
||||
Looping...</br>
|
||||
Looping...</br>
|
||||
We did it! 1 2 3</br>
|
||||
|
||||
Rambling
|
||||
--------
|
||||
5/23/18:
|
||||
When it comes to running code across different systems we run into a problem. It takes time to send objects from one maching to another. In the beginning only local networks will be supported. I may add support to send commands to another network to do computing. Like having your own lus cloud. userdata will never be allowed to run on other machines. It is not possible unless the library you are using allows userdata to be turned into a string and back into an object. With this feature you want to send a command that will take time or needs tons of them done millions+, reason being networks are not that "fast" and only simple objects can be sent. If you mirror your enviroment then you can do some cool things.
|
||||
|
||||
The planned structure will be something like this:
|
||||
multi-Single Threaded Multitasking
|
||||
multi-Threads
|
||||
multi-System Threads
|
||||
multi-Network threads
|
||||
|
||||
where netThreads can contain systemThreads which can intern contain both Threads and single threaded multitasking
|
||||
|
||||
Nothing has been built yet, but the system will work something like this:
|
||||
#host:
|
||||
```lua
|
||||
sGLOBAL, nGlobal,sThread=require("multi.integration.networkManager").init() -- This will determine if one is using lanes,love2d, or luvit
|
||||
multi:Host("MainSystem") -- tell the network that this is the main system. Uses broadcast so that nodes know how to find the host!
|
||||
nThread = multi:newNetworkThread("NetThread_1",function(...)
|
||||
-- basic usage
|
||||
nGLOBAL["RemoteVaraible"] = true -- will sync data to all nodes and the host
|
||||
sGLOBAL["LocalMachineVaraible"] = true -- will sync data to all system threads on the local machine
|
||||
return "Hello Network!" -- send "Hello Network" back to the host node
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
#node
|
||||
```lua
|
||||
GLOBAL,sThread=require("multi.integration.networkManager").init() -- This will determine if one is using lanes,love2d, or luvit
|
||||
node = multi:newNode("NodeName","MainSystem") -- Search the network for the host, connect to it and be ready for requests!
|
||||
-- On the main thread, a simple multi:newNetworkThread thread and also non system threads, you can access global data without an issue. When dealing with system threads is when you have a problem.
|
||||
node:setLog{
|
||||
maxLines = 10000,
|
||||
cleanOnInterval = true,
|
||||
cleanInterval = "day", -- every day Supports(day, week, month, year)
|
||||
noLog = false -- default is false, make true if you do not need a log
|
||||
}
|
||||
node:settings{
|
||||
maxJobs = 100, -- Job queues will respect this as well as the host when it is figuting out which node is under the least load. Default: 0 or infinite
|
||||
sendLoadInterval = 60 -- every 60 seconds update the host of the nodes load
|
||||
sendLoad = true -- default is true, tells the server how stressed the system is
|
||||
}
|
||||
multi:mainloop()
|
||||
-- Note: the node will contain a log of all the commands that it gets. A file called "NodeName.log" will contain the info. You can set the limit by lines or file size. Also you can set it to clear the log every interval of time if an error does not exist. All errors are both logged and sent to the host as well. You can have more than one host and more than one node(duh :P).
|
||||
```
|
||||
The goal of the node is to set up a simple and easy way to run commands on a remote machine.
|
||||
|
||||
There are 2 main ways you can use this feature. 1. One node per machine with system threads being able to use the full processing power of the machine. 2. Multiple nodes on one machine where each node is acting like its own thread. And of course a mix of the two is indeed possible.
|
||||
|
||||
|
||||
Love2d Sleeping reduces the cpu time making my load detection think the system is under more load, thus preventing it from sleeping... I will look into other means. As of right now it will not eat all of your cpu if threads are active. For now I suggest killing threads that aren't needed anymore. On lanes threads at idle use 0% cpu and it is amazing. A state machine may solve what I need though. One state being idle state that sleeps and only goes into the active state if a job request or data is sent to it... after some time of not being under load it wil switch back into the idle state... We'll see what happens.
|
||||
|
||||
Love2d doesn't like to send functions through channels. By defualt it does not support this. I achieve this by dumping the function and loadstring it on the thread. This however is slow. For the System Threaded Job Queue I had to change my original idea of sending functions as jobs. The current way you do it now is register a job functions once and then call that job across the thread through a queue. Each worker thread pops from the queue and returns the job. The Job ID is automatically updated and allows you to keep track of the order that the data comes in. A table with # indexes can be used to originze the data...
|
||||
|
||||
In regards to benchmarking. If you see my bench marks and are wondering they are 10x better its because I am using luajit for my tests. I highly recommend using luajit for my library, but lua 5.1 will work just as well, but not as fast.
|
||||
|
||||
So while working on the jobQueue:doToAll() method I figured out why love2d's threaded tables were acting up when more than 1 thread was sharing the table. It turns out 1 thread was eating all of the pops from the queue and starved all of the other queues... Ill need to use the same trick I did with GLOBAL to fix the problem... However at the rate I am going threading in love will become way slower. I might use the regualr GLOBAL to manage data internally for threadedtables...
|
||||
|
||||
It has been awhile since I had to bring out the Multi Functions... Syncing within threads are a pain! I had no idea what a task it would be to get something as simple as syncing data was going to be... I will probably add a SystemThreadedSyncer in the future because it will make life eaiser for you guys as well. SystemThreadedTables are still not going to work on love2d, but will work fine on lanes... I have a solution and it is being worked on... Depending on when I pust the next update to this library the second half of this ramble won't apply anymore
|
||||
|
||||
I have been using this (EventManager --> MultiManager --> now multi) for my own purposes and started making this when I first started learning lua. You are able to see how the code changed and evolved throughout the years. I tried to include all the versions that still existed on my HDD.
|
||||
|
||||
I added my old versions to this library... It started out as the EventManager and was kinda crappy but it was the start to this library. It kept getting better and better until it became what it is today. There are some features that nolonger exist in the latest version, but they were remove because they were useless... I added these files to the github so for those interested can see into my mind in a sense and see how I developed the library before I used github.
|
||||
|
||||
The first version of the EventManager was function based not object based and benched at about 2000 steps per second... Yeah that was bad... I used loadstring and it was a mess... Take a look and see how it grew throughout the years I think it may intrest some of you guys!
|
||||
Check the [Issues tab](https://github.com/rayaman/multi/issues) for issues
|
||||
|
||||
898
changes.html
898
changes.html
File diff suppressed because one or more lines are too long
722
changes.md
722
changes.md
@ -1,722 +0,0 @@
|
||||
Changes
|
||||
-------
|
||||
Update: 1.11.0
|
||||
--------------
|
||||
Added:
|
||||
- SystemThreadedConsole(name) -- Allsow each thread to print without the sync issues that make prints merge and hard to read.
|
||||
|
||||
```lua
|
||||
-- MainThread:
|
||||
console = multi:newSystemThreadedConsole("console"):init()
|
||||
-- Thread:
|
||||
console = THREAD.waitFor("console"):init()
|
||||
|
||||
-- using the console
|
||||
console:print(...)
|
||||
console:write(...) -- kinda useless for formatting code though. other threads can eaisly mess this up.
|
||||
```
|
||||
|
||||
Fixed/Updated:
|
||||
- Love2d 11.1 support is now here! Will now require these lines in your main.lua file
|
||||
|
||||
```lua
|
||||
function love.update(dt)
|
||||
multi:uManager(dt) -- runs the main loop of the multitasking library
|
||||
end
|
||||
function love.draw()
|
||||
multi.dManager() -- If using my guimanager, if not omit this
|
||||
end
|
||||
```
|
||||
|
||||
|
||||
Update: 1.10.0
|
||||
--------------
|
||||
**Note:** The library is now considered to be stable!
|
||||
**Upcoming:** Network parallelism is on the way. It is in the works and should be released soon
|
||||
|
||||
Added:
|
||||
------
|
||||
- isMainThread true/nil
|
||||
- multi:newSystemThreadedConnection(name,protect) -- Works like normal connections, but are able to trigger events across threads
|
||||
|
||||
Example of threaded connections
|
||||
```lua
|
||||
package.path="?/init.lua;?.lua;"..package.path
|
||||
local GLOBAL,THREAD=require("multi.integration.lanesManager").init()
|
||||
multi:newSystemThread("Test_Thread_1",function()
|
||||
connOut = THREAD.waitFor("ConnectionNAMEHERE"):init()
|
||||
connOut(function(arg)
|
||||
print(THREAD.getName(),arg)
|
||||
end)
|
||||
multi:mainloop()
|
||||
end)
|
||||
multi:newSystemThread("Test_Thread_2",function()
|
||||
connOut = THREAD.waitFor("ConnectionNAMEHERE"):init()
|
||||
connOut(function(arg)
|
||||
print(THREAD.getName(),arg)
|
||||
end)
|
||||
multi:mainloop()
|
||||
end)
|
||||
connOut = multi:newSystemThreadedConnection("ConnectionNAMEHERE"):init()
|
||||
a=0
|
||||
connOut(function(arg)
|
||||
print("Main",arg)
|
||||
end)
|
||||
multi:newTLoop(function()
|
||||
a=a+1
|
||||
connOut:Fire("Test From Main Thread: "..a.."\n")
|
||||
end,1)
|
||||
```
|
||||
|
||||
Fixed:
|
||||
------
|
||||
**loveManager** and **shared threading objects**
|
||||
- sThread.waitFor()
|
||||
- sThread.hold()
|
||||
- some typos
|
||||
- SystemThreadedTables (They now work on both lanes and love2d as expected)
|
||||
|
||||
Example of threaded tables
|
||||
```lua
|
||||
package.path="?/init.lua;?.lua;"..package.path
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
multi:newSystemThread("Test_Thread_1",function()
|
||||
require("multi")
|
||||
test = sThread.waitFor("testthing"):init()
|
||||
multi:newTLoop(function()
|
||||
print("------")
|
||||
for i,v in pairs(test.tab) do
|
||||
print("T1",i,v)
|
||||
end
|
||||
end,1)
|
||||
multi:mainloop()
|
||||
end)
|
||||
multi:newSystemThread("Test_Thread_1",function()
|
||||
require("multi")
|
||||
test = sThread.waitFor("testthing"):init()
|
||||
multi:newTLoop(function()
|
||||
print("------")
|
||||
for i,v in pairs(test.tab) do
|
||||
print("T2",i,v)
|
||||
end
|
||||
end,1)
|
||||
multi:mainloop()
|
||||
end)
|
||||
test = multi:newSystemThreadedTable("testthing"):init()
|
||||
multi:newTLoop(function()
|
||||
local a,b = multi.randomString(8),multi.randomString(4)
|
||||
print(">",a,b)
|
||||
test[a]=b
|
||||
end,1)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
Update: 1.9.2
|
||||
-------------
|
||||
Added:
|
||||
- (THREAD).kill() kills a thread. Note: THREAD is based on what you name it
|
||||
- newTimeStamper() Part of the persistant systems... Useful for when you are running this library for a massive amount of time... like years stright!
|
||||
Allows one to hook to timed events such as whenever the clock strikes midnight or when the day turns to monday. The event is only done once though. so as soon as monday is set it would trigger then not trigger again until next monday
|
||||
works for seconds, minutes, days, months, year.
|
||||
```lua
|
||||
stamper = multi:newTimeStamper()
|
||||
stamper:OnTime(int hour,int minute,int second,func) or stamper:OnTime(string time,func) time as 00:00:00
|
||||
stamper:OnHour(int hour,func)
|
||||
stamper:OnMinute(int minute,func)
|
||||
stamper:OnSecond(int second,func)
|
||||
stamper:OnDay(int day,func) or stamper:OnDay(string day,func) Mon, Tues, Wed, etc...
|
||||
stamper:OnMonth(int month,func)
|
||||
stamper:OnYear(int year,func)
|
||||
```
|
||||
Updated:
|
||||
- LoadBalancing, well bettwr load balancing than existed before. This one allowd for multiple processes to have their own load reading. Calling this on the multi object will return the total load for the entire multi enviroment... loads of other processes are indeed affected by what other processes are doing. However if you combine prorioty to the mix of things then you will get differing results... these results however will most likely be higher than normal... different pirorities will have different default thresholds of performence.
|
||||
|
||||
Fixed:
|
||||
- Thread.getName() should now work on lanes and love2d, haven't tested ut nuch with the luvit side of things...
|
||||
- A bug with the lovemanager table.remove arguments were backwards haha
|
||||
- The queue object in the love2d threading has been fixed! It now supports sending all objects (even functions as long as no upvalues are present!)
|
||||
|
||||
Changed:
|
||||
- SystemThreadedJobQueues now have built in load management so they are not constantly at 100% cpu usage.
|
||||
- SystemThreadedJobQueues pushJob now retunts an id of that job which will match the same one that OnJobCompleted returns
|
||||
|
||||
|
||||
Update: 1.9.1
|
||||
-------------
|
||||
Added:
|
||||
- Integration "multi.integration.luvitManager"
|
||||
- Limited... Only the basic multi:newSystemThread(...) will work
|
||||
- Not even data passing will work other than arguments... If using the bin library you can pass tables and function... Even full objects as long as inner recursion is not preasent.
|
||||
|
||||
Updated:
|
||||
- multi:newSystemThread(name,func,...)
|
||||
- It will not pass the ... to the func(). Do not know why this wasn't done in the first place :P
|
||||
- Also multi:getPlatform(will now return "luvit" if using luvit... Though Idk if module creators would use the multi library when inside the luvit enviroment
|
||||
|
||||
Update: 1.9.0
|
||||
-------------
|
||||
Added:
|
||||
- multiobj:ToString() -- returns a string repersenting the object
|
||||
- multi:newFromString(str) -- creates an object from a string
|
||||
|
||||
Works on threads and regular objects. Requires the latest bin library to work!
|
||||
```lua
|
||||
talarm=multi:newThreadedAlarm("AlarmTest",5)
|
||||
talarm:OnRing(function()
|
||||
print("Ring!")
|
||||
end)
|
||||
bin.new(talarm:ToString()):tofile("test.dat")
|
||||
-- multi:newFromString(bin.load("test.dat"))
|
||||
```
|
||||
-- A more seamless way to use this will be made in the form of state saving.
|
||||
This is still a WIP
|
||||
processes, timers, timemasters, watchers, and queuers have not been worked on yet
|
||||
Update: 1.8.7
|
||||
-------------
|
||||
Added:
|
||||
- multi.timer(func,...)
|
||||
|
||||
```lua
|
||||
function test(a,b,c)
|
||||
print("Running...")
|
||||
a=0
|
||||
for i=1,1000000000 do
|
||||
a=a+1
|
||||
end
|
||||
return a,b+c
|
||||
end
|
||||
print(multi.timer(test,1,2,3))
|
||||
print(multi.timer(test,1,2,3))
|
||||
-- multi.timer returns the time taken then the arguments from the function... Uses unpack so careful of nil values!
|
||||
```
|
||||
Update: 1.8.6
|
||||
-------------
|
||||
Added:
|
||||
- jobQueue:doToAll(function)
|
||||
- jobQueue:start() is now required Call this after all calls to registerJob()'s. Calling it afterwards will not guarantee your next push job with that job will work. Not calling this will make pushing jobs impossible!
|
||||
- Fixed a bug with love2d Threaded Queue
|
||||
- Fixed some bugs
|
||||
- Old versions of this library! It stems back from 2012 see rambling for more info...
|
||||
|
||||
This will run said function in every thread.
|
||||
```lua
|
||||
-- Going to use love2d code this time, almost the same as last time... See ramblings
|
||||
require("core.Library")
|
||||
GLOBAL,sThread=require("multi.integration.loveManager").init() -- load the love2d version of the lanesManager and requires the entire multi library
|
||||
require("core.GuiManager")
|
||||
gui.ff.Color=Color.Black
|
||||
jQueue=multi:newSystemThreadedJobQueue()
|
||||
jQueue:registerJob("TEST_JOB",function(a,s)
|
||||
math.randomseed(s)
|
||||
TEST_JOB2()
|
||||
return math.random(0,255)
|
||||
end)
|
||||
jQueue:registerJob("TEST_JOB2",function()
|
||||
print("Test Works!")
|
||||
end)
|
||||
-- 1.8.6 EXAMPLE Change
|
||||
jQueue:start() -- This is now needed!
|
||||
--
|
||||
jQueue:doToAll(function()
|
||||
print("Doing this 2? times!")
|
||||
end)
|
||||
tableOfOrder={}
|
||||
jQueue.OnJobCompleted(function(JOBID,n)
|
||||
tableOfOrder[JOBID]=n
|
||||
if #tableOfOrder==10 then
|
||||
t.text="We got all of the pieces!"
|
||||
end
|
||||
end)
|
||||
for i=1,10 do -- Job Name of registered function, ... varargs
|
||||
jQueue:pushJob("TEST_JOB","This is a test!",math.random(1,1000000))
|
||||
end
|
||||
t=gui:newTextLabel("no done yet!",0,0,300,100)
|
||||
t:centerX()
|
||||
t:centerY()
|
||||
```
|
||||
Update: 1.8.5
|
||||
-------------
|
||||
Added:
|
||||
- SystemThreadedExecute(cmd)
|
||||
|
||||
Allows the execution of system calls without hold up. It is possible to do the same using io.popen()! You decide which works best for you!
|
||||
```lua
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
cmd=multi:newSystemThreadedExecute("SystemThreadedExecuteTest.lua") -- This file is important!
|
||||
cmd.OnCMDFinished(function(code) -- callback function to grab the exit code... Called when the command goes through
|
||||
print("Got Code: "..code)
|
||||
end)
|
||||
multi:newTLoop(function()
|
||||
print("...") -- lets show that we aren't being held up
|
||||
end,1)
|
||||
multi:mainloop()
|
||||
```
|
||||
Update: 1.8.4
|
||||
-------------
|
||||
Added:
|
||||
- multi:newSystemThreadedJobQueue()
|
||||
- Improved stability of the library
|
||||
- Fixed a bug that made the benchmark and getload commands non-thread(coroutine) safe
|
||||
- Tweaked the loveManager to help improve idle cpu usage
|
||||
- Minor tweaks to the coroutine scheduling
|
||||
|
||||
# Using multi:newSystemThreadedJobQueue()
|
||||
First you need to create the object
|
||||
This works the same way as love2d as it does with lanes... It is getting increasing harder to make both work the same way with speed in mind... Anyway...
|
||||
```lua
|
||||
-- Creating the object using lanes manager to show case this. Examples has the file for love2d
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
jQueue=multi:newSystemThreadedJobQueue(n) -- this internally creates System threads. By defualt it will use the # of processors on your system You can set this number though.
|
||||
-- Only create 1 jobqueue! For now making more than 1 is buggy. You only really need one though. Just register new functions if you want 1 queue to do more. The one reason though is keeping track of jobIDs. I have an idea that I will roll out in the next update.
|
||||
jQueue:registerJob("TEST_JOB",function(a,s)
|
||||
math.randomseed(s)
|
||||
-- We will push a random #
|
||||
TEST_JOB2() -- You can call other registered functions as well!
|
||||
return math.random(0,255) -- send the result to the main thread
|
||||
end)
|
||||
jQueue:registerJob("TEST_JOB2",function()
|
||||
print("Test Works!") -- this is called from the job since it is registered on the same queue
|
||||
end)
|
||||
tableOfOrder={} -- This is how we will keep order of our completed jobs. There is no guarantee that the order will be correct
|
||||
jQueue.OnJobCompleted(function(JOBID,n) -- whenever a job is completed you hook to the event that is called. This passes the JOBID folled by the returns of the job
|
||||
-- JOBID is the completed job, starts at 1 and counts up by 1.
|
||||
-- Threads finish at different times so jobids may be passed out of order! Be sure to have a way to order them
|
||||
tableOfOrder[JOBID]=n -- we order ours by putting them into a table
|
||||
if #tableOfOrder==10 then
|
||||
print("We got all of the pieces!")
|
||||
end
|
||||
end)
|
||||
-- Lets push the jobs now
|
||||
for i=1,10 do -- Job Name of registered function, ... varargs
|
||||
jQueue:pushJob("TEST_JOB","This is a test!",math.random(1,1000000))
|
||||
end
|
||||
print("I pushed all of the jobs :)")
|
||||
multi:mainloop() -- Start the main loop :D
|
||||
```
|
||||
|
||||
Thats it from this version!
|
||||
|
||||
Update: 1.8.3
|
||||
-------------
|
||||
Added:</br>
|
||||
**New Mainloop functions** Below you can see the slight differences... Function overhead is not too bad in lua, but has a real difference. multi:mainloop() and multi:unprotectedMainloop() use the same algorithm yet the dedicated unprotected one is slightly faster due to having less function overhead.
|
||||
- multi:mainloop()\* -- Bench: 16830003 Steps in 3 second(s)!
|
||||
- multi:protectedMainloop() -- Bench: 16699308 Steps in 3 second(s)!
|
||||
- multi:unprotectedMainloop() -- Bench: 16976627 Steps in 3 second(s)!
|
||||
- multi:prioritizedMainloop1() -- Bench: 15007133 Steps in 3 second(s)!
|
||||
- multi:prioritizedMainloop2() -- Bench: 15526248 Steps in 3 second(s)!
|
||||
|
||||
\* The OG mainloop function remains the same and old methods to achieve what we have with the new ones still exist
|
||||
|
||||
These new methods help by removing function overhead that is caused through the original mainloop function. The one downside is that you no longer have the flexiblity to change the processing during runtime.
|
||||
|
||||
However there is a work around! You can use processes to run multiobjs as well and use the other methods on them.
|
||||
|
||||
I may make a full comparison between each method and which is faster, but for now trust that the dedicated ones with less function overhead are infact faster. Not by much but still faster. :D
|
||||
|
||||
Update: 1.8.2
|
||||
-------------
|
||||
Added:</br>
|
||||
- multi:newsystemThreadedTable(name) NOTE: Metatables are not supported in transfers. However there is a work around obj:init() that you see does this. Take a look in the multi/integration/shared/shared.lua files to see how I did it!
|
||||
- Modified the GLOBAL metatable to sync before doing its tests
|
||||
- multi._VERSION was multi.Version, felt it would be more consistant this way... I left the old way of getting the version just incase someone has used that way. It will eventually be gone. Also multi:getVersion() will do the job just as well and keep your code nice and update related bug free!
|
||||
- Also everything that is included in the: multi/integration/shared/shared.lua (Which is loaded automatically) works in both lanes and love2d enviroments!
|
||||
|
||||
The threaded table is setup just like the threaded queue.</br>
|
||||
It provids GLOBAL like features without having to write to GLOBAL!</br>
|
||||
This is useful for module creators who want to keep their data private, but also use GLOBAL like coding.</br>
|
||||
It has a few features that makes it a bit better than plain ol GLOBAL (For now...)
|
||||
(ThreadedTable - TT for short)
|
||||
- TT:waitFor(name)
|
||||
- TT:sync()
|
||||
- TT["var"]=value
|
||||
- print(TT["var"])
|
||||
|
||||
we also have the "sync" method, this one was made for love2d because we do a syncing trick to get data in a table format. The lanes side has a sync method as well so no worries. Using indexing calls sync once and may grab your variable. This allows you to have the lanes indexing 'like' syntax when doing regular indexing in love2d side of the module. As of right now both sides work flawlessly! And this effect is now the GLOBAL as well</br>
|
||||
|
||||
On GLOBALS sync is a internal method for keeping the GLOBAL table in order. You can still use sThread.waitFor(name) to wait for variables that may of may not yet exist!
|
||||
|
||||
Time for some examples:
|
||||
# Using multi:newSystemThreadedTable(name)
|
||||
```lua
|
||||
-- lanes Desktop lua! NOTE: this is in lanesintergratetest6.lua in the examples folder
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
test=multi:newSystemThreadedTable("YO"):init()
|
||||
test["test1"]="lol"
|
||||
multi:newSystemThread("test",function()
|
||||
tab=sThread.waitFor("YO"):init()
|
||||
print(tab:has("test1"))
|
||||
sThread.sleep(3)
|
||||
tab["test2"]="Whats so funny?"
|
||||
end)
|
||||
multi:newThread("test2",function()
|
||||
print(test:waitFor("test2"))
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
```lua
|
||||
-- love2d gaming lua! NOTE: this is in main4.lua in the love2d examples
|
||||
require("core.Library")
|
||||
GLOBAL,sThread=require("multi.integration.loveManager").init() -- load the love2d version of the lanesManager and requires the entire multi library
|
||||
require("core.GuiManager")
|
||||
gui.ff.Color=Color.Black
|
||||
test=multi:newSystemThreadedTable("YO"):init()
|
||||
test["test1"]="lol"
|
||||
multi:newSystemThread("test",function()
|
||||
tab=sThread.waitFor("YO"):init()
|
||||
print(tab["test1"])
|
||||
sThread.sleep(3)
|
||||
tab["test2"]="Whats so funny?"
|
||||
end)
|
||||
multi:newThread("test2",function()
|
||||
print(test:waitFor("test2"))
|
||||
t.text="DONE!"
|
||||
end)
|
||||
t=gui:newTextLabel("no done yet!",0,0,300,100)
|
||||
t:centerX()
|
||||
t:centerY()
|
||||
```
|
||||
|
||||
Update: 1.8.1
|
||||
-------------
|
||||
No real change!</br>
|
||||
Changed the structure of the library. Combined the coroutine based threads into the core!</br>
|
||||
Only compat and integrations are not part of the core and never will be by nature.</br>
|
||||
This should make the library more convient to use.</br>
|
||||
I left multi/all.lua file so if anyone had libraries/projects that used that it will still work!</br>
|
||||
Updated from 1.7.6 to 1.8.0</br> (How much thread could a thread thread if a thread could thread thread?)
|
||||
Added:</br>
|
||||
- multi:newSystemThreadedQueue()
|
||||
- multi:systemThreadedBenchmark()
|
||||
- More example files
|
||||
- multi:canSystemThread() -- true if an integration was added false otherwise (For module creation)
|
||||
- Fixed a few bugs in the loveManager
|
||||
|
||||
# Using multi:systemThreadedBenchmark()
|
||||
```lua
|
||||
package.path="?/init.lua;"..package.path
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
multi:systemThreadedBenchmark(3):OnBench(function(self,count)
|
||||
print("First Bench: "..count)
|
||||
multi:systemThreadedBenchmark(3,"All Threads: ")
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
# Using multi:newSystemThreadedQueue()
|
||||
Quick Note: queues shared across multiple objects will be pulling from the same "queue" keep this in mind when coding! ~~Also the queue respects direction a push on the thread side cannot be popped on the thread side... Same goes for the mainthread!</br>~~ Turns out i was wrong about this...
|
||||
```lua
|
||||
-- in love2d, this file will be in the same example folder as before, but is named main2.lua
|
||||
require("core.Library")
|
||||
GLOBAL,sThread=require("multi.integration.loveManager").init() -- load the love2d version of the lanesManager and requires the entire multi library
|
||||
--IMPORTANT
|
||||
-- Do not make the above local, this is the one difference that the lanesManager does not have
|
||||
-- If these are local the functions will have the upvalues put into them that do not exist on the threaded side
|
||||
-- You will need to ensure that the function does not refer to any upvalues in its code. It will print an error if it does though
|
||||
-- Also each thread has a .1 second delay! This is used to generate a random values for each thread!
|
||||
require("core.GuiManager")
|
||||
gui.ff.Color=Color.Black
|
||||
function multi:newSystemThreadedQueue(name) -- in love2d this will spawn a channel on both ends
|
||||
local c={}
|
||||
c.name=name
|
||||
if love then
|
||||
if love.thread then
|
||||
function c:init()
|
||||
self.chan=love.thread.getChannel(self.name)
|
||||
function self:push(v)
|
||||
self.chan:push(v)
|
||||
end
|
||||
function self:pop()
|
||||
return self.chan:pop()
|
||||
end
|
||||
GLOBAL[self.name]=self
|
||||
return self
|
||||
end
|
||||
return c
|
||||
else
|
||||
error("Make sure you required the love.thread module!")
|
||||
end
|
||||
else
|
||||
c.linda=lanes.linda()
|
||||
function c:push(v)
|
||||
self.linda:send("Q",v)
|
||||
end
|
||||
function c:pop()
|
||||
return ({self.linda:receive(0,"Q")})[2]
|
||||
end
|
||||
function c:init()
|
||||
return self
|
||||
end
|
||||
GLOBAL[name]=c
|
||||
end
|
||||
return c
|
||||
end
|
||||
queue=multi:newSystemThreadedQueue("QUEUE"):init()
|
||||
queue:push("This is a test")
|
||||
queue:push("This is a test2")
|
||||
queue:push("This is a test3")
|
||||
queue:push("This is a test4")
|
||||
multi:newSystemThread("test2",function()
|
||||
queue=sThread.waitFor("QUEUE"):init()
|
||||
data=queue:pop()
|
||||
while data do
|
||||
print(data)
|
||||
data=queue:pop()
|
||||
end
|
||||
queue:push("DONE!")
|
||||
end)
|
||||
multi:newThread("test!",function()
|
||||
thread.hold(function() return queue:pop() end)
|
||||
t.text="Done!"
|
||||
end)
|
||||
t=gui:newTextLabel("no done yet!",0,0,300,100)
|
||||
t:centerX()
|
||||
t:centerY()
|
||||
```
|
||||
# In Lanes
|
||||
```lua
|
||||
-- The code is compatible with each other, I just wanted to show different things you can do in both examples
|
||||
-- This file can be found in the examples folder as lanesintegrationtest4.lua
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
queue=multi:newSystemThreadedQueue("QUEUE"):init()
|
||||
queue:push("This is a test")
|
||||
queue:push("This is a test2")
|
||||
queue:push("This is a test3")
|
||||
queue:push("This is a test4")
|
||||
multi:newSystemThread("test2",function()
|
||||
queue=sThread.waitFor("QUEUE"):init()
|
||||
data=queue:pop()
|
||||
while data do
|
||||
print(data)
|
||||
data=queue:pop()
|
||||
end
|
||||
queue:push("This is a test5")
|
||||
queue:push("This is a test6")
|
||||
queue:push("This is a test7")
|
||||
queue:push("This is a test8")
|
||||
end)
|
||||
multi:newThread("test!",function() -- this is a lua thread
|
||||
thread.sleep(.1)
|
||||
data=queue:pop()
|
||||
while data do
|
||||
print(data)
|
||||
data=queue:pop()
|
||||
end
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
Update: 1.7.6
|
||||
-------------
|
||||
Fixed:
|
||||
Typos like always
|
||||
Added:</br>
|
||||
multi:getPlatform() -- returns "love2d" if using the love2d platform or returns "lanes" if using lanes for threading</br>
|
||||
examples files</br>
|
||||
In Events added method setTask(func)</br>
|
||||
The old way still works and is more convient to be honest, but I felt a method to do this was ok.</br>
|
||||
|
||||
Updated:
|
||||
some example files to reflect changes to the core. Changes allow for less typing</br>
|
||||
loveManager to require the compat if used so you don't need 2 require line to retrieve the library</br>
|
||||
|
||||
Update: 1.7.5
|
||||
-------------
|
||||
Fixed some typos in the readme... (I am sure there are more there are always more)</br>
|
||||
Added more features for module support</br>
|
||||
TODO:</br>
|
||||
Work on performance of the library... I see 3 places where I can make this thing run quicker</br>
|
||||
|
||||
I'll show case some old versions of the multitasking library eventually so you can see its changes in days past!</br>
|
||||
|
||||
Update: 1.7.4
|
||||
-------------
|
||||
Added: the example folder which will be populated with more examples in the near future!</br>
|
||||
The loveManager integration that mimics the lanesManager integration almost exactly to keep coding in both enviroments as close to possible. This is done mostly for library creation support!</br>
|
||||
An example of the loveManager in action using almost the same code as the lanesintergreationtest2.lua</br>
|
||||
NOTE: This code has only been tested to work on love2d version 1.10.2 thoough it should work version 0.9.0
|
||||
```lua
|
||||
require("core.Library") -- Didn't add this to a repo yet! Will do eventually... Allows for injections and other cool things
|
||||
require("multi.compat.love2d") -- allows for multitasking and binds my libraies to the love2d engine that i am using
|
||||
GLOBAL,sThread=require("multi.integration.loveManager").init() -- load the love2d version of the lanesManager
|
||||
--IMPORTANT
|
||||
-- Do not make the above local, this is the one difference that the lanesManager does not have
|
||||
-- If these are local the functions will have the upvalues put into them that do not exist on the threaded side
|
||||
-- You will need to ensure that the function does not refer to any upvalues in its code. It will print an error if it does though
|
||||
-- Also each thread has a .1 second delay! This is used to generate a random values for each thread!
|
||||
require("core.GuiManager") -- allows the use of graphics in the program.
|
||||
gui.ff.Color=Color.Black
|
||||
function comma_value(amount)
|
||||
local formatted = amount
|
||||
while true do
|
||||
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
|
||||
if (k==0) then
|
||||
break
|
||||
end
|
||||
end
|
||||
return formatted
|
||||
end
|
||||
multi:newSystemThread("test1",function() -- Another difference is that the multi library is already loaded in the threaded enviroment as well as a call to multi:mainloop()
|
||||
multi:benchMark(sThread.waitFor("Bench"),nil,"Thread 1"):OnBench(function(self,c) GLOBAL["T1"]=c multi:Stop() end)
|
||||
end)
|
||||
multi:newSystemThread("test2",function() -- spawns a thread in another lua process
|
||||
multi:benchMark(sThread.waitFor("Bench"),nil,"Thread 2"):OnBench(function(self,c) GLOBAL["T2"]=c multi:Stop() end)
|
||||
end)
|
||||
multi:newSystemThread("test3",function() -- spawns a thread in another lua process
|
||||
multi:benchMark(sThread.waitFor("Bench"),nil,"Thread 3"):OnBench(function(self,c) GLOBAL["T3"]=c multi:Stop() end)
|
||||
end)
|
||||
multi:newSystemThread("test4",function() -- spawns a thread in another lua process
|
||||
multi:benchMark(sThread.waitFor("Bench"),nil,"Thread 4"):OnBench(function(self,c) GLOBAL["T4"]=c multi:Stop() end)
|
||||
end)
|
||||
multi:newSystemThread("test5",function() -- spawns a thread in another lua process
|
||||
multi:benchMark(sThread.waitFor("Bench"),nil,"Thread 5"):OnBench(function(self,c) GLOBAL["T5"]=c multi:Stop() end)
|
||||
end)
|
||||
multi:newSystemThread("test6",function() -- spawns a thread in another lua process
|
||||
multi:benchMark(sThread.waitFor("Bench"),nil,"Thread 6"):OnBench(function(self,c) GLOBAL["T6"]=c multi:Stop() end)
|
||||
end)
|
||||
multi:newSystemThread("Combiner",function() -- spawns a thread in another lua process
|
||||
function comma_value(amount)
|
||||
local formatted = amount
|
||||
while true do
|
||||
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
|
||||
if (k==0) then
|
||||
break
|
||||
end
|
||||
end
|
||||
return formatted
|
||||
end
|
||||
local b=comma_value(tostring(sThread.waitFor("T1")+sThread.waitFor("T2")+sThread.waitFor("T3")+sThread.waitFor("T4")+sThread.waitFor("T5")+sThread.waitFor("T6")))
|
||||
GLOBAL["DONE"]=b
|
||||
end)
|
||||
multi:newThread("test0",function()
|
||||
-- sThread.waitFor("DONE") -- lets hold the main thread completely so we don't eat up cpu
|
||||
-- os.exit()
|
||||
-- when the main thread is holding there is a chance that error handling on the system threads may not work!
|
||||
-- instead we can do this
|
||||
while true do
|
||||
thread.skip(1) -- allow error handling to take place... Otherwise lets keep the main thread running on the low
|
||||
-- Before we held just because we could... But this is a game and we need to have logic continue
|
||||
--sThreadM.sleep(.001) -- Sleeping for .001 is a greeat way to keep cpu usage down. Make sure if you aren't doing work to rest. Abuse the hell out of GLOBAL if you need to :P
|
||||
if GLOBAL["DONE"] then
|
||||
t.text="Bench: "..GLOBAL["DONE"]
|
||||
end
|
||||
end
|
||||
end)
|
||||
GLOBAL["Bench"]=3
|
||||
t=gui:newTextLabel("no done yet!",0,0,300,100)
|
||||
t:centerX()
|
||||
t:centerY()
|
||||
```
|
||||
Update: 1.7.3
|
||||
-------------
|
||||
Changed how requiring the library works!
|
||||
`require("multi.all")` Will still work as expected; however, with the exception of threading, compat, and integrations everything else has been moved into the core of the library.
|
||||
```lua
|
||||
-- This means that these are no longer required and will cause an error if done so
|
||||
require("multi.loop")
|
||||
require("multi.alarm")
|
||||
require("multi.updater")
|
||||
require("multi.tloop")
|
||||
require("multi.watcher")
|
||||
require("multi.tstep")
|
||||
require("multi.step")
|
||||
require("multi.task")
|
||||
-- ^ they are all part of the core now
|
||||
```
|
||||
|
||||
Update: 1.7.2
|
||||
-------------
|
||||
Moved updaters, loops, and alarms into the init.lua file. I consider them core features and they are referenced in the init.lua file so they need to exist there. Threaded versions are still separate though. Added another example file
|
||||
|
||||
Update: 1.7.1 Bug Fixes Only
|
||||
-------------
|
||||
|
||||
Update: 1.7.0
|
||||
-------------
|
||||
Modified: multi.integration.lanesManager.lua
|
||||
It is now in a stable and simple state Works with the latest lanes version! Tested with version 3.11 I cannot promise that everything will work with eariler versions. Future versions are good though.</br>
|
||||
Example Usage:</br>
|
||||
sThread is a handle to a global interface for system threads to interact with themself</br>
|
||||
thread is the interface for multithreads as seen in the threading section</br>
|
||||
|
||||
GLOBAL a table that can be used throughout each and every thread
|
||||
|
||||
sThreads have a few methods</br>
|
||||
sThread.set(name,val) -- you can use the GLOBAL table instead modifies the same table anyway</br>
|
||||
sThread.get(name) -- you can use the GLOBAL table instead modifies the same table anyway</br>
|
||||
sThread.waitFor(name) -- waits until a value exists, if it does it returns it</br>
|
||||
sThread.getCores() -- returns the number of cores on your cpu</br>
|
||||
sThread.sleep(n) -- sleeps for a bit stopping the entire thread from running</br>
|
||||
sThread.hold(n) -- sleeps until a condition is met</br>
|
||||
```lua
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
require("multi.all")
|
||||
multi:newAlarm(2):OnRing(function(self)
|
||||
GLOBAL["NumOfCores"]=sThread.getCores()
|
||||
end)
|
||||
multi:newAlarm(7):OnRing(function(self)
|
||||
GLOBAL["AnotherTest"]=true
|
||||
end)
|
||||
multi:newAlarm(13):OnRing(function(self)
|
||||
GLOBAL["FinalTest"]=true
|
||||
end)
|
||||
multi:newSystemThread("test",function() -- spawns a thread in another lua process
|
||||
require("multi.all") -- now you can do all of your coding with the multi library! You could even spawn more threads from here with the integration. You would need to require the interaction again though
|
||||
print("Waiting for variable: NumOfCores")
|
||||
print("Got it: ",sThread.waitFor("NumOfCores"))
|
||||
sThread.hold(function()
|
||||
return GLOBAL["AnotherTest"] -- note this would hold the entire systemthread. Spawn a coroutine thread using multi:newThread() or multi:newThreaded...
|
||||
end)
|
||||
print("Holding works!")
|
||||
multi:newThread("tests",function()
|
||||
thread.hold(function()
|
||||
return GLOBAL["FinalTest"] -- note this will not hold the entire systemthread. As seen with the TLoop constantly going!
|
||||
end)
|
||||
print("Final test works!")
|
||||
os.exit()
|
||||
end)
|
||||
local a=0
|
||||
multi:newTLoop(function()
|
||||
a=a+1
|
||||
print(a)
|
||||
end,.5)
|
||||
multi:mainloop()
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
Update: 1.6.0
|
||||
-------------
|
||||
Changed: steps and loops
|
||||
```lua
|
||||
-- Was
|
||||
step:OnStep(function(pos,self) -- same goes for tsteps as well
|
||||
print(pos)
|
||||
end)
|
||||
multi:newLoop(function(dt,self)
|
||||
print(dt)
|
||||
end)
|
||||
-- Is now
|
||||
step:OnStep(function(self,pos) -- same goes for tsteps as well
|
||||
print(pos)
|
||||
end)
|
||||
multi:newLoop(function(self,dt)
|
||||
print(dt)
|
||||
end)
|
||||
```
|
||||
Reasoning I wanted to keep objects consistant, but a lot of my older libraries use the old way of doing things. Therefore I added a backwards module
|
||||
```lua
|
||||
require("multi.all")
|
||||
require("multi.compat.backwards[1,5,0]") -- allows for the use of features that were scrapped/changed in 1.6.0+
|
||||
```
|
||||
Update: 1.5.0
|
||||
-------------
|
||||
Added:
|
||||
- An easy way to manage timeouts
|
||||
- Small bug fixes
|
||||
|
||||
Update: 1.4.1 - First Public release of the library
|
||||
-------------
|
||||
|
||||
**IMPORTANT:**
|
||||
Every update I make aims to make things simpler more efficent and just better, but a lot of old code, which can be really big, uses a lot of older features. I know the pain of having to rewrite everything. My promise to my library users is that I will always have backwards support for older features! New ways may exist that are quicker and eaiser, but the old features/methods will be supported.</br>
|
||||
@ -1,5 +0,0 @@
|
||||
MAIN: Love2d support is updated to 11.1
|
||||
|
||||
May be bugs in supporting libraries, but the multitasking library is fully updated.
|
||||
|
||||
The GuiManager may have a bug or 2, but I haven't found any ground breaking bugs that haven't been fixed
|
||||
971
docs/Documentation.md
Normal file
971
docs/Documentation.md
Normal file
@ -0,0 +1,971 @@
|
||||
Current Multi Version: 15.1.0
|
||||
|
||||
# Multi static variables
|
||||
`multi.Version` — The current version of the library
|
||||
|
||||
`multi.TIMEOUT` — The value returned when a timed method times out
|
||||
|
||||
`multi.Priority_Core` — Highest level of pirority that can be given to a process
|
||||
</br>`multi.Priority_Very_High`
|
||||
</br>`multi.Priority_High`
|
||||
</br>`multi.Priority_Above_Normal`
|
||||
</br>`multi.Priority_Normal` — The default level of pirority that is given to a process
|
||||
</br>`multi.Priority_Below_Normal`
|
||||
</br>`multi.Priority_Low`
|
||||
</br>`multi.Priority_Very_Low`
|
||||
</br>`multi.Priority_Idle` — Lowest level of pirority that can be given to a process
|
||||
|
||||
# Multi Runners
|
||||
`multi:lightloop()` — A light version of the mainloop doesn't run Coroutine based threads
|
||||
</br>`multi:loveloop([BOOLEAN: light true])` — Run's all the love related features as well
|
||||
</br>`multi:mainloop([TABLE settings])` — This runs the mainloop by having its own internal while loop running
|
||||
</br>`multi:threadloop([TABLE settings])` — This runs the mainloop by having its own internal while loop running, but prioritizes threads over multi-objects
|
||||
</br>`multi:uManager([TABLE settings])` — This runs the mainloop, but does not have its own while loop and thus needs to be within a loop of some kind.
|
||||
|
||||
# Global Methods
|
||||
|
||||
`multi:init()` — Uesd to initiate the library, should only be called once
|
||||
`multi.getCurrentProcess()` — Returns currently running Process
|
||||
`multi.`
|
||||
|
||||
# Processor Methods
|
||||
|
||||
These methods can be called either on the multi namespace or a process returned by `proc = multi:newProcessor()`
|
||||
|
||||
`proc.Stop()` — Stops the main process/child process. **Note:** If the main process is stopped all child processes are stopped as well
|
||||
`proc:getTasksDetails([STRING: displaytype])` — Gets a table or string of all the running tasks
|
||||
|
||||
Processor Attributes
|
||||
---
|
||||
|
||||
| Attribute | Type | Returns | Description |
|
||||
---|---|---|---
|
||||
Start|Method()|self| Starts the process
|
||||
Stop|Method()|self| Stops the process
|
||||
OnError|Connection|connection| Allows connection to the process error handler
|
||||
Type|Member:`string`|"process"| Contains the type of object
|
||||
Active|Member:`boolean`|variable| If false the process is not active
|
||||
Name|Member:`string`|variable| The name set at process creation
|
||||
process|Thread|thread| A handle to a multi thread object
|
||||
|
||||
[Refer to the objects for more methods](#non-actors)
|
||||
|
||||
Example:
|
||||
```lua
|
||||
package.path = "./?/init.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
|
||||
-- Create a processor object, it works a lot like the multi object
|
||||
sandbox = multi:newProcessor()
|
||||
|
||||
-- On our processor object create a TLoop that prints "testing..." every second
|
||||
sandbox:newTLoop(function()
|
||||
print("testing...")
|
||||
end,1)
|
||||
|
||||
-- Create a thread on the processor object
|
||||
sandbox:newThread("Test Thread",function()
|
||||
-- Create a counter named 'a'
|
||||
local a = 0
|
||||
-- Start of the while loop that ends when a = 10
|
||||
while true do
|
||||
-- pause execution of the thread for 1 second
|
||||
thread.sleep(1)
|
||||
-- increment a by 1
|
||||
a = a + 1
|
||||
-- display the name of the current process
|
||||
print("Thread Test: ".. multi.getCurrentProcess().Name)
|
||||
if a == 10 then
|
||||
-- Stopping the processor stops all objects created inside that process including threads. In the backend threads use a regular multiobject to handle the scheduler and all of the holding functions. These all stop when a processor is stopped. This can be really useful to sandbox processes that might need to turned on and off with ease and not having to think about it.
|
||||
sandbox.Stop()
|
||||
end
|
||||
end
|
||||
-- Catch any errors that may come up
|
||||
end).OnError(function(...)
|
||||
print(...)
|
||||
end)
|
||||
|
||||
sandbox.Start() -- Start the process
|
||||
|
||||
multi:mainloop() -- The main loop that allows all processes to continue
|
||||
```
|
||||
|
||||
# Multi Settings
|
||||
|
||||
**Note:** Most settings have been fined tuned to be at the peak of performance already, however preLoop, protect (Which drastically lowers preformance), and stopOnError should be used freely to fit your needs.
|
||||
|
||||
| Setting | Type: default | Purpose |
|
||||
| --------------- | --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| preLoop | function: nil | This is a function that is called after all the important components of the library are loaded. This is called once only. The first and only argument passed is a reference to itself. |
|
||||
| protect | boolean: false | This runs code within a protected call. To catch when errors happen see built in connections |
|
||||
| stopOnError | boolean: false | This setting is used with protect. If an object crashes due to some error should it be paused? |
|
||||
| priority | number: 0 | This sets the priority scheme. Look at the P-Charts below for examples. |
|
||||
| auto_priority | boolean: false | **Note: This overrides any value set for priority!** If auto priority is enabled then priority scheme 3 is used and processes are considered for "recheck" after a certain amount of time. If a process isn't taking too long to complete anymore then it will be reset to core, if it starts to take a lot of time all of a sudden it will be set to idle. |
|
||||
| auto_stretch | number: 1 | For use with auto_priority. Modifies the internal reperesentation of idle time by multiplying multi.Priority_Idle by the value given |
|
||||
| auto_delay | number: 3 | For use with auto_priority. This changes the time in seconds that process are "rechecked" |
|
||||
| auto_lowerbound | number: multi.Priority_Idle | For use with auto_priority. The lowerbound is what is considered to be idle time. A higher value combined with auto_stretch allows one to fine tune how pirority is managed. |
|
||||
|
||||
# P-Chart: Priority 1
|
||||
|
||||
P1 follows a forumla that resembles this: ~n=I*PRank</br>Where **n** is the amount of steps given to an object with PRank and where I is the idle time see chart below. The aim of this priority scheme was to make core objects run fastest while letting idle processes get decent time as well.
|
||||
|
||||
| Priority: n | PRank | Formula |
|
||||
| --------------------- | ----- | ------------ |
|
||||
| Core: 3322269 | 7 | n = ~**I***7 |
|
||||
| High: 2847660 | 6 | n = ~**I***6 |
|
||||
| Above_Normal: 2373050 | 5 | n = ~**I***5 |
|
||||
| Normal: 1898440 | 4 | n = ~**I***4 |
|
||||
| Below_Normal: 1423830 | 3 | n = ~**I***3 |
|
||||
| Low: 949220 | 2 | n = ~**I***2 |
|
||||
| **I**dle: 474610 | 1 | n = ~**I***1 |
|
||||
|
||||
**General Rule:** ~n=**I***PRank
|
||||
|
||||
# P-Chart: Priority 2
|
||||
|
||||
P2 follows a formula that resembles this: ~n=n*4 where n starts as the initial idle time, see chart below. The goal of this one was to make core process’ higher while keeping idle process’ low.
|
||||
|
||||
| Priority: n |
|
||||
|-|
|
||||
| Core: 6700821|
|
||||
| High: 1675205|
|
||||
| Above_Normal: 418801|
|
||||
| Normal: 104700|
|
||||
| Below_Normal: 26175|
|
||||
| Low: 6543|
|
||||
| **I**dle: 1635|
|
||||
|
||||
**General Rule:** `~n=n*4` Where the inital n = **I**
|
||||
|
||||
# P-Chart: Priority 3
|
||||
P3 Ignores using a basic formula and instead bases its processing time on the amount of cpu time is there. If cpu-time is low and a process is set at a lower priority it will get its time reduced. There is no formula, at idle almost all process work at the same speed!
|
||||
|
||||
There are 2 settings for this: Core and Idle. If a process takes too long then it is set to idle. Otherwise it will stay core.
|
||||
|
||||
Example of settings:
|
||||
```lua
|
||||
settings = {
|
||||
preLoop = function(m)
|
||||
print("All settings have been loaded!")
|
||||
end,
|
||||
protect = false,
|
||||
stopOnError = false,
|
||||
priority = 0,
|
||||
auto_priority = false,
|
||||
auto_stretch = 1,
|
||||
auto_delay = 3,
|
||||
auto_lowerbound = multi.Priority_Idle
|
||||
}
|
||||
|
||||
-- Below are how the runners work
|
||||
|
||||
multi:lightloop() -- lighter version of mainloop. Everything except priority management for non service objects will function like normal!
|
||||
|
||||
-- or
|
||||
|
||||
multi:mainloop(settings) -- normal runner
|
||||
|
||||
-- or
|
||||
|
||||
multi:threadloop(settings) -- Prioritizes threads over multi-objs
|
||||
|
||||
-- or
|
||||
|
||||
while true do
|
||||
multi:uManager(settings) -- allows you to run the multi main loop within another loop
|
||||
end
|
||||
```
|
||||
|
||||
# Non-Actors
|
||||
`timer = multi:newTimer()`
|
||||
- `conn = multi:newConnection([BOOLEAN protect true])`
|
||||
- `func = multi:newFunction(FUNCTION func)`
|
||||
|
||||
# Actors
|
||||
- `event = multi:newEvent(FUNCTION task)`
|
||||
- `updater = multi:newUpdater([NUMBER skip 1])`
|
||||
- `alarm = multi:newAlarm([NUMBER 0])`
|
||||
- `loop = multi:newLoop(FUNCTION func)`
|
||||
- `tloop = multi:newTLoop(FUNCTION func ,NUMBER: [set 1])`
|
||||
- `step = multi:newStep(NUMBER start,*NUMBER reset, [NUMBER count 1], [NUMBER skip 0])`
|
||||
- `tstep = multi:newStep(NUMBER start, NUMBER reset, [NUMBER count 1], [NUMBER set 1])`
|
||||
|
||||
**Note:** A lot of methods will return itself as a return. This allows for chaining of methods to work.
|
||||
|
||||
# Non-Actor: Timers
|
||||
`timer = multi:newTimer()` — Creates a timer object that can keep track of time
|
||||
|
||||
- **self** = timer:Start() — Starts the timer
|
||||
- time_elapsed = timer:Get() — Returns the time elapsed since timer:Start() was called
|
||||
- boolean = timer:isPaused() — Returns if the timer is paused or not
|
||||
- **self** = timer:Pause() — Pauses the timer, it skips time that would be counted during the time that it is paused
|
||||
- **self** = timer:Resume() — Resumes a paused timer. **See note below**
|
||||
- **self** = timer:tofile(**STRING** path) — Saves the object to a file at location path
|
||||
|
||||
**Note:** If a timer was paused after 1 second then resumed a second later and Get() was called a second later, timer would have 2 seconds counted though 3 really have passed.
|
||||
|
||||
# Non-Actor: Connections
|
||||
`conn = multi:newConnection([BOOLEAN: protect true],FUNCTION: callback, BOOLEAN: kill false)` —
|
||||
Creates a connection object and defaults to a protective state. All calls will run within pcall() callback if it exists will be triggered each time the connection is fired. kill when set to true makes the connection object work like a queue. Where all the events that are fired is removed from the queue.
|
||||
- `self = conn:HoldUT([NUMBER n 0])` — Will hold futhur execution of the thread until the connection was triggered. If n is supplied the connection must be triggered n times before it will allow ececution to continue.
|
||||
- `conntable_old = conn:Bind(TABLE conntable)` — sets the table to hold the connections. A quick way to destroy all connections is by binding it to a new table.
|
||||
- `conntable = conn:Remove()` — Removes all connections. Returns the conntable
|
||||
- `link = conn:connect(FUNCTION func, [STRING name nil], [NUMBER num #conns+1])` — Connects to the object using function func which will recieve the arguments passed by Fire(...). You can name a connection, which allows you to use conn:getConnection(name). Names must be unique! num is simple the position in the order in which connections are triggered. The return Link is the link to the connected event that was made. You can remove this event or even trigger it specifically if need be.
|
||||
- `link:Fire(...)` — Fires the created event
|
||||
- `bool = link:Destroy()` — returns true if success.
|
||||
- `subConn = conn:getConnection(STRING name, BOOLEAN ingore)` — returns the sub connection which matches name.
|
||||
returns or nil
|
||||
- subConn:Fire() — "returns" if non-nil is a table containing return values from the triggered connections.
|
||||
- `self = conn:tofile(STRING path)` — Saves the object to a file at location path
|
||||
|
||||
The connect feature has some syntax sugar to it as seen below
|
||||
- `link = conn(FUNCTION func, [STRING name nil], [NUMBER #conns+1])`
|
||||
- `combinedconn = conn1 + conn2` — A combined connection is triggered when all connections are triggered. See example [here](#coroutine-based-threading-cbt)
|
||||
|
||||
|
||||
|
||||
Example:
|
||||
```lua
|
||||
multi,thread = require("multi"):init()
|
||||
-- Let’s create the events
|
||||
yawn={}
|
||||
OnCustomSafeEvent=multi:newConnection(true) -- lets pcall the calls in case something goes wrong default
|
||||
OnCustomEvent=multi:newConnection(false) -- let’s not pcall the calls and let errors happen.
|
||||
OnCustomEvent:Bind(yawn) -- create the connection lookup data in yawn
|
||||
|
||||
-- Let’s connect to them, a recent update adds a nice syntax to connect to these
|
||||
cd1=OnCustomSafeEvent:Connect(function(arg1,arg2,...)
|
||||
print("CSE1",arg1,arg2,...)
|
||||
end,"bob") -- let’s give this connection a name
|
||||
cd2=OnCustomSafeEvent:Connect(function(arg1,arg2,...)
|
||||
print("CSE2",arg1,arg2,...)
|
||||
end,"joe") -- let’s give this connection a name
|
||||
cd3=OnCustomSafeEvent:Connect(function(arg1,arg2,...)
|
||||
print("CSE3",arg1,arg2,...)
|
||||
end) -- let’s not give this connection a name
|
||||
|
||||
-- Using syntax sugar
|
||||
OnCustomEvent(function(arg1,arg2,...)
|
||||
print(arg1,arg2,...)
|
||||
end)
|
||||
|
||||
-- Now within some loop/other object you trigger the connection like
|
||||
OnCustomEvent:Fire(1,2,"Hello!!!") -- fire all connections
|
||||
|
||||
-- You may have noticed that some events have names! See the following example!
|
||||
OnCustomSafeEvent:getConnection("bob"):Fire(1,100,"Bye!") -- fire only bob!
|
||||
OnCustomSafeEvent:getConnection("joe"):Fire(1,100,"Hello!") -- fire only joe!!
|
||||
OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all!!!
|
||||
|
||||
-- Connections have more to them than that though!
|
||||
-- As seen above cd1-cd3 these are hooks to the connection object. This allows you to remove a connection
|
||||
-- For Example:
|
||||
cd1:Remove() -- remove this connection from the master connection object
|
||||
print("------")
|
||||
OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all again!!!
|
||||
-- To remove all connections use:
|
||||
OnCustomSafeEvent:Remove()
|
||||
print("------")
|
||||
OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all again!!!
|
||||
```
|
||||
|
||||
# Semi-Actors: timeouts
|
||||
Timeouts are a collection of methods that allow you to handle timeouts. These only work on multi-objs, and much of the functionality can easly be done now using threads!
|
||||
|
||||
```lua
|
||||
package.path="?.lua;?/init.lua;?.lua;?/?/init.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
|
||||
loop = multi:newLoop(function()
|
||||
-- do stuff
|
||||
end)
|
||||
|
||||
loop:SetTime(3)
|
||||
multi:newAlarm(2):OnRing(function()
|
||||
-- some condition that leads to resolving the timer
|
||||
loop:ResolveTimer(true,"We good")
|
||||
multi:newAlarm(2):OnRing(function()
|
||||
loop:SetTime(2)
|
||||
end)
|
||||
end)
|
||||
|
||||
loop:OnTimedOut(function()
|
||||
print("Timeout")
|
||||
end)
|
||||
|
||||
loop:OnTimerResolved(function(self,...)
|
||||
print(...)
|
||||
end)
|
||||
|
||||
multi:mainloop()
|
||||
```
|
||||
As mentioned above this is made much easier using threads
|
||||
```lua
|
||||
package.path="?.lua;?/init.lua;?.lua;?/?/init.lua;"..package.path
|
||||
multi, thread = require("multi"):init()
|
||||
func = thread:newFunction(function(a)
|
||||
return thread.holdFor(3,function()
|
||||
return a==5 and "This is returned" -- Condition being tested!
|
||||
end)
|
||||
end,true)
|
||||
print(func(5))
|
||||
print(func(0))
|
||||
-- You actually do not need the light/mainloop or any runner for threaded functions to work
|
||||
-- multi:lightloop()
|
||||
```
|
||||
|
||||
# Semi-Actors: scheduleJob
|
||||
`multi:scheduleJob(TABLE: time, FUNCTION: callback)`
|
||||
- `TABLE: time`
|
||||
- `NUMBER: time.min` — Minute(0-59) Repeats every hour
|
||||
- `NUMBER: time.hour` — Hour(0-23) Repeats every day
|
||||
- `NUMBER: time.day` — Day of month(1-31) repeats every month
|
||||
- `NUMBER: time.wday` — Weekday(0-6) repeats every week
|
||||
- `NUMBER: time.month` — Month(1-12) repeats every year
|
||||
- `FUNCTION: callback`
|
||||
- Called when the time table is matched
|
||||
|
||||
Example:
|
||||
```lua
|
||||
package.path="?.lua;?/init.lua;?.lua;?/?/init.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
multi:scheduleJob({min = 30},function() -- Every hour at minute 30 this event will be triggered! You can mix and match as well!
|
||||
print("Hi")
|
||||
end)
|
||||
multi:scheduleJob({min = 30,hour = 0},function() -- Every day at 12:30AM this event will be triggered
|
||||
print("Hi")
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
# Universal Actor methods
|
||||
All of these functions are found on actors
|
||||
- `self = multiObj:Pause()` — Pauses the actor from running
|
||||
- `self = multiObj:Resume()` — Resumes the actor that was paused
|
||||
- `nil = multiObj:Destroy()` — Removes the object from the mainloop
|
||||
- `bool = multiObj:isPaused()` — Returns true if the object is paused, false otherwise
|
||||
- `string = multiObj:getType()` — Returns the type of the object
|
||||
- `self = multiObj:SetTime(n)` — Sets a timer, and creates a special "timemaster" actor, which will timeout unless ResolveTimer is called
|
||||
- `self = multiObj:ResolveTimer(...)` — Stops the timer that was put onto the multiObj from timing out
|
||||
- `self = multiObj:OnTimedOut(func)` — If ResolveTimer was not called in time this event will be triggered. The function connected to it get a refrence of the original object that the timer was created on as the first argument.
|
||||
- `self = multiObj:OnTimerResolved(func)` — This event is triggered when the timer gets resolved. Same argument as above is passed, but the variable arguments that are accepted in resolvetimer are also passed as well.
|
||||
- `self = multiObj:Reset(n)` — In the cases where it isn't obvious what it does, it acts as Resume()
|
||||
- `self = multiObj:SetName(STRING name)`
|
||||
|
||||
# Actor: Events
|
||||
`event = multi:newEvent(FUNCTION task)` — The object that started it all. These are simply actors that wait for a condition to take place, then auto triggers an event. The event when triggered once isn't triggered again unless you Reset() it.
|
||||
|
||||
- `self = event:SetTask(FUNCTION func)` — This function is not needed if you supplied task at construction time
|
||||
- `self = event:OnEvent(FUNCTION func)` — Connects to the OnEvent event passes argument self to the connectee
|
||||
|
||||
Example:
|
||||
```lua
|
||||
multi,thread = require("multi"):init()
|
||||
count=0
|
||||
-- A loop object is used to demostrate how one could use an event object.
|
||||
loop=multi:newLoop(function(self,dt)
|
||||
count=count+1
|
||||
end)
|
||||
event=multi:newEvent(function() return count==100 end) -- set the event
|
||||
event:OnEvent(function(self) -- connect to the event object
|
||||
loop:Destroy() -- destroys the loop from running!
|
||||
print("Stopped that loop!",count)
|
||||
end) -- events like alarms need to be reset the Reset() command works here as well
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
# Actor: Updaters
|
||||
`updater = multi:newUpdater([NUMBER skip 1])` — set the amount of steps that are skipped.
|
||||
|
||||
Updaters are a mix between both loops and steps. They were a way to add basic priority management to loops (until a better way was added). Now they aren't as useful, but if you do not want the performance hit of turning on priority then they are useful to auro skip some loops. Note: The performance hit due to priority management is not as bas as it used to be.
|
||||
|
||||
- `self = updater:SetSkip(NUMBER n)` — sets the amount of steps that are skipped
|
||||
- `self = OnUpdate(FUNCTION func)` — connects to the main trigger of the updater which is called every nth step
|
||||
|
||||
Example:
|
||||
```lua
|
||||
multi,thread = require("multi"):init()
|
||||
updater=multi:newUpdater(5000) -- simple, think of a loop with the skip feature of a step
|
||||
updater:OnUpdate(function(self)
|
||||
print("updating...")
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
# Actor: Alarms
|
||||
`alarm = multi:newAlarm([NUMBER 0])` — creates an alarm which waits n seconds
|
||||
Alarms ring after a certain amount of time, but you need to reset the alarm every time it rings! Use a TLoop if you do not want to have to reset.
|
||||
|
||||
- `self = alarm:Reset([NUMBER sec current_time_set])` — Allows one to reset an alarm, optional argument to change the time until the next ring.
|
||||
- `self = alarm:OnRing(FUNCTION func` — Allows one to connect to the alarm event which is triggerd after a certain amount of time has passed.
|
||||
|
||||
Example:
|
||||
```lua
|
||||
multi,thread = require("multi"):init()
|
||||
alarm=multi:newAlarm(3) -- in seconds can go to .001 uses the built in os.clock()
|
||||
alarm:OnRing(function(a)
|
||||
print("3 Seconds have passed!")
|
||||
a:Reset(n) -- if n were nil it will reset back to 3, or it would reset to n seconds
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
# Actor: Loops
|
||||
`loop = multi:newLoop(FUNCTION func)` — func the main connection that you can connect to. Is optional, but you can also use OnLoop(func) to connect as well.
|
||||
Loops are events that happen over and over until paused. They act like a while loop.
|
||||
|
||||
- `self = OnLoop(FUNCTION func)` — func the main connection that you can connect to. Alllows multiple connections to one loop if need be.
|
||||
|
||||
Example:
|
||||
```lua
|
||||
package.path="?/init.lua;?.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
local a = 0
|
||||
loop = multi:newLoop(function()
|
||||
a = a + 1
|
||||
if a == 1000 then
|
||||
print("a = 1000")
|
||||
loop:Pause()
|
||||
end
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
# Actor: TLoops
|
||||
`tloop = multi:newTLoop(FUNCTION func ,NUMBER: [set 1])` — TLoops are pretty much the same as loops. The only difference is that they take set which is how long it waits, in seconds, before triggering function func.
|
||||
|
||||
- `self = OnLoop(FUNCTION func)` — func the main connection that you can connect to. Alllows multiple connections to one TLoop if need be.
|
||||
|
||||
Example:
|
||||
```lua
|
||||
package.path="?/init.lua;?.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
local a = 0
|
||||
loop = multi:newTLoop(function()
|
||||
a = a + 1
|
||||
if a == 10 then
|
||||
print("a = 10")
|
||||
loop:Pause()
|
||||
end
|
||||
end,1)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
# Actor: Steps
|
||||
`step = multi:newStep(NUMBER start,*NUMBER reset, [NUMBER count 1], [NUMBER skip 0])` — Steps were originally introduced to bs used as for loops that can run parallel with other code. When using steps think of it like this: `for i=start,reset,count do` When the skip argument is given, each time the step object is given cpu cycles it will be skipped by n cycles. So if skip is 1 every other cpu cycle will be alloted to the step object.
|
||||
|
||||
- `self = step:OnStart(FUNCTION func(self))` — This connects a function to an event that is triggered everytime a step starts.
|
||||
- `self = step:OnStep(FUNCTION func(self,i))` — This connects a function to an event that is triggered every step or cycle that is alloted to the step object
|
||||
- `self = step:OnEnd(FUNCTION func(self))` — This connects a function to an event that is triggered when a step reaches its goal
|
||||
- `self = step:Update(NUMBER start,*NUMBER reset, [NUMBER count 1], [NUMBER skip 0])` — Update can be used to change the goals of the step.
|
||||
- `self = step:Reset()` — Resets the step
|
||||
|
||||
Example:
|
||||
```lua
|
||||
package.path="?/init.lua;?.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
multi:newStep(1,10,1,0):OnStep(function(step,pos)
|
||||
print(step,pos)
|
||||
end):OnEnd(fucntion(step)
|
||||
step:Destroy()
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
# Actor: TSteps
|
||||
`tstep = multi:newStep(NUMBER start, NUMBER reset, [NUMBER count 1], [NUMBER set 1])` — TSteps work just like steps, the only difference is that instead of skip, we have set which is how long in seconds it should wait before triggering the OnStep() event.
|
||||
|
||||
- `self = tstep:OnStart(FUNCTION func(self))` — This connects a function to an event that is triggered everytime a step starts.
|
||||
- `self = tstep:OnStep(FUNCTION func(self,i))` — This connects a function to an event that is triggered every step or cycle that is alloted to the step object
|
||||
- `self = tstep:OnEnd(FUNCTION func(self))` — This connects a function to an event that is triggered when a step reaches its goal
|
||||
- `self = tstep:Update(NUMBER start,*NUMBER reset, [NUMBER count 1], [NUMBER set 1])` — Update can be used to change the goals of the step. You should call step:Reset() after using Update to restart the step.
|
||||
- `self = tstep:Reset([NUMBER n set])` — Allows you to reset a tstep that has ended, but also can change the time between each trigger.
|
||||
|
||||
Example:
|
||||
```lua
|
||||
package.path="?/init.lua;?.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
multi:newTStep(1,10,1,1):OnStep(function(step,pos)
|
||||
print(step,pos)
|
||||
end):OnEnd(fucntion(step)
|
||||
step:Destroy()
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
# Coroutine based Threading (CBT)
|
||||
Helpful methods are wrapped around the builtin coroutine module which make it feel like real threading.
|
||||
|
||||
**threads.\* used within threaded enviroments**
|
||||
- `thread.sleep(NUMBER n)` — Holds execution of the thread until a certain amount of time has passed
|
||||
- `VARIABLE val = THREAD.hold(FUNCTION|CONNCETION|NUMBER func, TABLE options)` — Holds the current thread until a condition is met
|
||||
|
||||
| Option | Description |
|
||||
---|---
|
||||
| interval | Time between each poll |
|
||||
| cycles | Number of cycles before timing out |
|
||||
| sleep | Number of seconds before timing out |
|
||||
| skip | Number of cycles before testing again, does not cause a timeout! |
|
||||
|
||||
**Note:** cycles and sleep options cannot both be used at the same time. Interval and skip cannot be used at the same time either. Cycles take priority over sleep if both are present! HoldFor and HoldWithin can be emulated using the new features. Old functions will remain for backward compatibility.
|
||||
|
||||
Using cycles, sleep or interval will cause a timeout; returning nil, multi.TIMEOUT
|
||||
|
||||
`func` can be a number and `thread.hold` will act like `thread.sleep`. When `func` is a number the option table will be ignored!
|
||||
|
||||
`func` can be a connection and will hold until the condition is triggered. When using a connection the option table is ignored!
|
||||
|
||||
- `thread.skip(NUMBER n)` — How many cycles should be skipped until I execute again
|
||||
- `thread.kill()` — Kills the thread
|
||||
- `thread.yeild()` — Is the same as using thread.skip(0) or thread.sleep(0), hands off control until the next cycle
|
||||
- `BOOLEAM bool = thread.isThread()` — Returns true if the current running code is inside of a coroutine based thread
|
||||
- `NUMBER conres = thread.getCores()` — Returns the number of cores that the current system has. (used for system threads)
|
||||
- `thread.set(STRING name, VARIABLE val)` — A global interface where threads can talk with eachother. sets a variable with name and its value
|
||||
- `thread.get(STRING name)` — Gets the data stored in name
|
||||
- `VARIABLE val = thread.waitFor(STRING name)` — Holds executon of a thread until variable name exists
|
||||
- `thread.request(THREAD th,STRING cmd, VARIABLE args)` — Sends a request to the selected thread telling it to do a certain command
|
||||
- `th = thread.getRunningThread()` — Returns the currently running thread
|
||||
- `VARIABLE returns or nil, "TIMEOUT" = thread.holdFor(NUMBER: sec, FUNCTION: condition)` — Holds until a condidtion is met, or if there is a timeout nil,"TIMEOUT"
|
||||
- `VARIABLE returns or nil, "TIMEOUT" = thread.holdWithin(NUMBER: skip, FUNCTION: func)` — Holds until a condition is met or n cycles have happened.
|
||||
- `func = thread:newFunction(FUNCTION: func, [BOOLEAN: holdme false])` — func: The function you want to be threaded. holdme: If true the function waits until it has returns and then returns them. Otherwise the function returns a table
|
||||
- `func:Pause()` — Pauses a function, function will return `nil`, `"Function is paused"`
|
||||
- `func:Resume()` — Resumes a paused function
|
||||
- `func:holdMe(BOOLEAN: set)` — Sets the holdme argument to `set`
|
||||
- `handler = func(VARIABLE args)` — Calls the function, will return
|
||||
- `handler.isTFunc` — if true then its a threaded function
|
||||
- `handler.wait()` — waits for the function to finish and returns like normal
|
||||
- `handler.connect(Function: func(returns))` — Connects to the event that is triggered when the returns are avaiable and returns them
|
||||
- `VARIABLE returns = handler.wait()` — Waits until returns are avaiable and then
|
||||
- `handler.OnStatus(connector(VARIABLE args))` — A connection to the running function's status see example below
|
||||
- `handler.OnReturn(connector(VARIABLE args))` — A connection that is triggered when the running function is finished see example below
|
||||
- `handler.OnError(connector(nil,error))`
|
||||
|
||||
Example:
|
||||
|
||||
```lua
|
||||
package.path = "./?/init.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
|
||||
func = thread:newFunction(function(count)
|
||||
local a = 0
|
||||
while true do
|
||||
a = a + 1
|
||||
thread.sleep(.1)
|
||||
thread.pushStatus(a,count)
|
||||
if a == count then break end
|
||||
end
|
||||
return "Done"
|
||||
end)
|
||||
|
||||
multi:newThread("Function Status Test",function()
|
||||
local ret = func(10)
|
||||
local ret2 = func(15)
|
||||
local ret3 = func(20)
|
||||
ret.OnStatus(function(part,whole)
|
||||
--[[ Print out the current status. In this case every second it will update with:
|
||||
10%
|
||||
20%
|
||||
30%
|
||||
...
|
||||
100%
|
||||
|
||||
Function Done!
|
||||
]]
|
||||
print(math.ceil((part/whole)*1000)/10 .."%")
|
||||
end)
|
||||
ret2.OnStatus(function(part,whole)
|
||||
print("Ret2: ",math.ceil((part/whole)*1000)/10 .."%")
|
||||
end)
|
||||
ret3.OnStatus(function(part,whole)
|
||||
print("Ret3: ",math.ceil((part/whole)*1000)/10 .."%")
|
||||
end)
|
||||
-- Connections can now be added together, if you had multiple holds and one finished before others and wasn't consumed it would lock forever! This is now fixed
|
||||
thread.hold(ret2.OnReturn + ret.OnReturn + ret3.OnReturn)
|
||||
print("Function Done!")
|
||||
os.exit()
|
||||
end)
|
||||
```
|
||||
|
||||
<b>\*</b>A note about multi.NIL, this should only be used within the hold and hold like methods. thread.hold(), thread.holdFor(), and thread.holdWithin() methods. This is not needed within threaded functions! The reason hold prevents nil and false is because it is testing for a condition so the first argument needs to be non nil nor false! multi.NIL should not be used anywhere else. Sometimes you may need to pass a 'nil' value or return. While you could always return true or something you could use multi.NIL to force a nil value through a hold like method.
|
||||
|
||||
# CBT: newService(FUNCTION: func)
|
||||
`serv = newService(FUNCTION: func(self,TABLE: data))` — func is called each time the service is updated think of it like a loop multi-obj. self is the service object and data is a private table that only the service can see.
|
||||
- `serv.OnError(FUNCTION: func)` — connection that fired if there is an error
|
||||
- `serv.OnStopped(FUNCTION: func(serv))` — connection that is fired when a service is stopped
|
||||
- `serv.OnStarted(FUNCTION: func(serv))` — connection that is fired when a service is started
|
||||
- `serv.Start()` — Starts the service
|
||||
- `serv.Stop()` — Stops the service and destroys the data table
|
||||
- `serv.Pause()` — Pauses the service
|
||||
- `serv.Resume()` — Resumes the service
|
||||
- `serv.GetUpTime()` — Returns the amount of time the service has been running
|
||||
- `serv.SetPriority(PRIORITY: pri)` — Sets the priority of the service
|
||||
- `multi.Priority_Core`
|
||||
- `multi.Priority_Very_High`
|
||||
- `multi.Priority_High`
|
||||
- `multi.Priority_Above_Normal`
|
||||
- `multi.Priority_Normal` **Default**
|
||||
- `multi.Priority_Below_Normal`
|
||||
- `multi.Priority_Low`
|
||||
- `multi.Priority_Very_Low`
|
||||
- `multi.Priority_Idle`
|
||||
- `serv.SetScheme(NUMBER: n)` — Sets the scheme of the priority management
|
||||
- `1` **Default** — uses a time based style of yielding. thread.sleep()
|
||||
- `2` — uses a cycle based style of yielding. thread.skip()
|
||||
- `CONVERTS(serv) = serv.Destroy()` — Stops the service then Destroys the service triggering all events! The service becomes a destroyed object
|
||||
|
||||
Example:
|
||||
```lua
|
||||
-- Jobs are not natively part of the multi library. I planned on adding them, but decided against it. Below is the code that would have been used.
|
||||
-- Implementing a job manager using services
|
||||
package.path="?/init.lua;?.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
multi.Jobs = multi:newService(function(self,jobs)
|
||||
local job = table.remove(jobs,1)
|
||||
if job and job.removed==nil then
|
||||
job.func()
|
||||
end
|
||||
end)
|
||||
multi.Jobs.OnStarted(function(self,jobs)
|
||||
function self:newJob(func,name)
|
||||
table.insert(jobs,{
|
||||
func = func,
|
||||
name = name,
|
||||
removeJob = function(self) self.removed = true end
|
||||
})
|
||||
end
|
||||
function self:getJobs(name)
|
||||
local tab = {}
|
||||
if not name then return jobs end
|
||||
for i=1,#jobs do
|
||||
if name == jobs[i].name then
|
||||
table.insert(tab,jobs[i])
|
||||
end
|
||||
end
|
||||
return tab
|
||||
end
|
||||
function self:removeJobs(name)
|
||||
for i=1,#jobs do
|
||||
if name ~= nil and name == jobs[i].name then
|
||||
jobs[i]:removeJob()
|
||||
elseif name == nil then
|
||||
jobs[i]:removeJob()
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
multi.Jobs.SetPriority(multi.Priority_Normal)
|
||||
multi.Jobs.Start()
|
||||
|
||||
-- Testing job stuff
|
||||
function pushJobs()
|
||||
multi.Jobs:newJob(function()
|
||||
print("job called")
|
||||
end) -- No name job
|
||||
multi.Jobs:newJob(function()
|
||||
print("job called2")
|
||||
end,"test")
|
||||
multi.Jobs:newJob(function()
|
||||
print("job called3")
|
||||
end,"test2")
|
||||
end
|
||||
pushJobs()
|
||||
pushJobs()
|
||||
local jobs = multi.Jobs:getJobs() -- gets all jobs
|
||||
local jobsn = multi.Jobs:getJobs("test") -- gets all jobs names 'test'
|
||||
jobsn[1]:removeJob() -- Select a job and remove it
|
||||
multi.Jobs:removeJobs("test2") -- Remove all jobs names 'test2'
|
||||
multi.Jobs.SetScheme(1) -- Jobs are internally a service, so setting scheme and priority
|
||||
multi.Jobs.SetPriority(multi.Priority_Core)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
# CBT: newThread()
|
||||
`th = multi:newThread([STRING name,] FUNCTION func)` — Creates a new thread with name and function.
|
||||
|
||||
when within a thread, if you have any holding code you will want to use thread.* to give time to other threads while your code is running.
|
||||
Constants
|
||||
---
|
||||
- `th.Name` — Name of thread
|
||||
- `th.Type` — Type="thread"
|
||||
- `th.TID` — Thread ID
|
||||
- `conn = th.OnError(FUNCTION: callback)` — Connect to an event which is triggered when an error is encountered within a thread
|
||||
- `conn = th.OnDeath(FUNCTION: callback)` — Connect to an event which is triggered when the thread had either been killed or stopped running. (Not triggered when there is an error!)
|
||||
- `boolean = th:isPaused()`\* — Returns true if a thread has been paused
|
||||
- `self = th:Pause()`\* — Pauses a thread
|
||||
- `self = th:Resume()`\* — Resumes a paused thread
|
||||
- `self = th:Kill()`\* — Kills a thread
|
||||
- `self = th:Destroy()`\* — Destroys a thread
|
||||
|
||||
<b>*</b>Using these methods on a thread directly you are making a request to a thread! The thread may not accept your request, but it most likely will. You can contorl the thread flow within the thread's function itself
|
||||
|
||||
Examples:
|
||||
```lua
|
||||
package.path="?/init.lua;?.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
multi:newThread("Example of basic usage",function()
|
||||
while true do
|
||||
thread.sleep(1)
|
||||
print("We just made an alarm!")
|
||||
end
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
# CBT: newISOThread()
|
||||
`th = multi:newThread([STRING name,] FUNCTION func, TABLE: env)` — Creates a new thread with name and function func. Sets the enviroment of the func to env. Both the thread.* and multi.* are automatically placed in the enviroment.
|
||||
|
||||
When within a thread, if you have any holding code you will want to use thread.* to give time to other threads while your code is running. This type of thread does not have access to outside local or globals. Only what is in the env can be seen. (This thread was made so pesudo threading could work)
|
||||
Constants
|
||||
---
|
||||
- `th.Name` — Name of thread
|
||||
- `th.Type` — Type="thread"
|
||||
- `th.TID` — Thread ID
|
||||
- `conn = th.OnError(FUNCTION: callback)` — Connect to an event which is triggered when an error is encountered within a thread
|
||||
- `conn = th.OnDeath(FUNCTION: callback)` — Connect to an event which is triggered when the thread had either been killed or stopped running. (Not triggered when there is an error!)
|
||||
- `boolean = th:isPaused()`\* — Returns true if a thread has been paused
|
||||
- `self = th:Pause()`\* — Pauses a thread
|
||||
- `self = th:Resume()`\* — Resumes a paused thread
|
||||
- `self = th:Kill()`\* — Kills a thread
|
||||
- `self = th:Destroy()`\* — Destroys a thread
|
||||
|
||||
<b>*</b>Using these methods on a thread directly you are making a request to a thread! The thread may not accept your request, but it most likely will. You can contorl the thread flow within the thread's function itself
|
||||
```lua
|
||||
package.path="?.lua;?/init.lua;?.lua;?/?/init.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
GLOBAL,THREAD = require("multi.integration.threading"):init() -- Auto detects your enviroment and uses what's available
|
||||
|
||||
jq = multi:newSystemThreadedJobQueue(5) -- Job queue with 4 worker threads
|
||||
func = jq:newFunction("test",function(a,b)
|
||||
THREAD.sleep(2)
|
||||
return a+b
|
||||
end)
|
||||
|
||||
for i = 1,10 do
|
||||
func(i,i*3).connect(function(data)
|
||||
print(data)
|
||||
end)
|
||||
end
|
||||
|
||||
local a = true
|
||||
b = false
|
||||
|
||||
multi:newThread("Standard Thread 1",function()
|
||||
while true do
|
||||
thread.sleep(1)
|
||||
print("Testing 1 ...",a,b,test)
|
||||
end
|
||||
end).OnError(function(self,msg)
|
||||
print(msg)
|
||||
end)
|
||||
|
||||
-- All upvalues are stripped! no access to the global, multi and thread are exposed however
|
||||
multi:newISOThread("ISO Thread 2",function()
|
||||
while true do
|
||||
thread.sleep(1)
|
||||
print("Testing 2 ...",a,b,test) -- a and b are nil, but test is true
|
||||
end
|
||||
end,{test=true,print=print})
|
||||
|
||||
.OnError(function(self,msg)
|
||||
print(msg)
|
||||
end)
|
||||
|
||||
multi:mainloop()
|
||||
```
|
||||
# System Threads (ST) - Multi-Integration Getting Started
|
||||
The system threads need to be required seperatly.
|
||||
```lua
|
||||
-- I recommend keeping these as globals. When using lanes you can use local and things will work, but if you use love2d and locals, upvalues are not transfered over threads and this can be an issue
|
||||
GLOBAL, THREAD = require("multi.integration.threading"):init() -- We will talk about the global and thread interface that is returned
|
||||
GLOBAL, THREAD = require("multi.integration.loveManager"):init()
|
||||
GLOBAL, THREAD = require("luvitManager") --*
|
||||
```
|
||||
Using this integration modifies some methods that the multi library has.
|
||||
- `multi:canSystemThread()` — Returns true if system threading is possible.
|
||||
- `multi:getPlatform()` — Returns (for now) either "lanes", "love2d" and "luvit"
|
||||
- `multi.isMainThread = true` — This is only modified on the main thread. So code that moves from one thread to another knows where it's at.
|
||||
|
||||
<b>*</b>GLOBAL and THREAD do not do anything when using the luvit integration
|
||||
|
||||
# ST - THREAD namespace
|
||||
- `THREAD.set(STRING name, VALUE val)` — Sets a value in GLOBAL
|
||||
- `THREAD.get(STRING name)` — Gets a value in GLOBAL
|
||||
- `THREAD.waitFor(STRING name)` — Waits for a value in GLOBAL to exist
|
||||
- `THREAD.getCores()` — Returns the number of actual system threads/cores
|
||||
- `THREAD.kill()` — Kills the thread
|
||||
- `THREAD.getName()` — Returns the name of the working thread
|
||||
- `THREAD.sleep(NUMBER n)` — Sleeps for an amount of time stopping the current thread
|
||||
- `THREAD.hold(FUNCTION func, TABLE options)` — Holds the current thread until a condition is met
|
||||
- `THREAD.getID()` — returns a unique ID for the current thread. This varaiable is visible to the main thread as well as by accessing it through the returned thread object. OBJ.Id
|
||||
|
||||
# ST - GLOBAL namespace
|
||||
Treat global like a table.
|
||||
```lua
|
||||
GLOBAL["name"] = "Ryan"
|
||||
print(GLOBAL["name"])
|
||||
```
|
||||
Removes the need to use THREAD.set() and THREAD.get()
|
||||
|
||||
ST - System Threads
|
||||
-------------------
|
||||
- `systemThread = multi:newSystemThread(STRING thread_name, FUNCTION spawned_function,ARGUMENTS ...)` — Spawns a thread with a certain name.
|
||||
- `systemThread:kill()` — kills a thread; can only be called in the main thread!
|
||||
- `systemThread.OnError(FUNCTION(systemthread,errMsg,errorMsgWithThreadName))`
|
||||
|
||||
System Threads are the feature that allows a user to interact with systen threads. It differs from regular coroutine based thread in how it can interact with variables. When using system threads the GLOBAL table is the "only way"* to send data. Spawning a System thread is really simple once all the required libraries are in place. See example below:
|
||||
|
||||
```lua
|
||||
multi,thread = require("multi"):init() -- keep this global when using lanes or implicitly define multi within the spawned thread
|
||||
local GLOBAL, THREAD = require("multi.integration.threading").init()
|
||||
multi:newSystemThread("Example thread",function()
|
||||
local multi = require("multi") -- we are in a thread so lets not refer to that upvalue!
|
||||
print("We have spawned a thread!")
|
||||
-- we could do work but theres no need to we can save that for other examples
|
||||
print("Lets have a non ending loop!")
|
||||
while true do
|
||||
-- If this was not in a thread execution would halt for the entire process
|
||||
end
|
||||
end,"A message that we are passing") -- There are restrictions on what can be passed!
|
||||
|
||||
tloop = multi:newTLoop(function()
|
||||
print("I'm still kicking!")
|
||||
end,1)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
<b>*</b>This isn't entirely true, as of right now the compatiablity with the lanes library and love2d engine have their own methods to share data, but if you would like to have your code work in both enviroments then using the GLOBAL table and the data structures provided by the multi library will ensure this happens. If you do not plan on having support for both platforms then feel free to use linda's in lanes and channels in love2d.
|
||||
|
||||
**Note:** luvit currently has very basic support, it only allows the spawning of system threads, but no way to send data back and forth as of yet. I do not know if this is doable or not, but I will keep looking into it.
|
||||
|
||||
# ST - System Threaded Objects
|
||||
Great we are able to spawn threads, but unless your working with a process that works on passed data and then uses a socket or writes to the disk I can't do to much with out being able to pass data between threads. This section we will look at how we can share objects between threads. In order to keep the compatibility between both love2d and lanes I had to format the system threaded objects in a strange way, but they are consistant and should work on both enviroments.
|
||||
|
||||
When creating objects with a name they are automatically exposed to the GLOBAL table. Which means you can retrieve them from a spawned thread. For example we have a queue object, which will be discussed in more detail next.
|
||||
|
||||
```lua
|
||||
-- Exposing a queue
|
||||
multi,thread = require("multi"):init()
|
||||
local GLOBAL, THREAD = require("multi.integration.threading").init() -- The standard setup above
|
||||
queue = multi:newSystemThreadedQueue("myQueue"):init() -- We create and initiate the queue for the main thread
|
||||
queue:push("This is a test!") -- We push some data onto the queue that other threads can consume and do stuff with
|
||||
multi:newSystemThread("Example thread",function() -- Create a system thread
|
||||
queue = THREAD.waitFor("myQueue"):init() -- Get the queue. It is good pratice to use the waitFor command when getting objects. If it doesn't exist yet we wait for it, preventing future errors. It is possible for the data to not ve present when a thread is looking for it! Especally when using the love2d module, my fault needs some rewriting data passing on the GLOBAL is quite slow, but the queue internally uses channels so after it is exposed you should have good speeds!
|
||||
local data = queue:pop() -- Get the data
|
||||
print(data) -- print the data
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
# ST - SystemThreadedQueue
|
||||
- `queue(nonInit) = multi:newSystemThreadedQueue(STRING name)` — You must enter a name!
|
||||
- `queue = queue:init()` — initiates the queue, without doing this it will not work
|
||||
- `void = queue:push(DATA data)` — Pushes data into a queue that all threads that have been shared have access to
|
||||
- `data = queue:pop()` — pops data from the queue removing it from all threads
|
||||
- `data = queue:peek()` — looks at data that is on the queue, but dont remove it from the queue
|
||||
|
||||
Let's get into some examples:
|
||||
```lua
|
||||
multi,thread = require("multi"):init()
|
||||
thread_names = {"Thread_A","Thread_B","Thread_C","Thread_D"}
|
||||
local GLOBAL, THREAD = require("multi.integration.threading"):init()
|
||||
queue = multi:newSystemThreadedQueue("myQueue"):init()
|
||||
for _,n in pairs(thread_names) do
|
||||
multi:newSystemThread(n,function()
|
||||
queue = THREAD.waitFor("myQueue"):init()
|
||||
local name = THREAD.getName()
|
||||
local data = queue:pop()
|
||||
while data do
|
||||
print(name.." "..data)
|
||||
data = queue:pop()
|
||||
end
|
||||
end)
|
||||
end
|
||||
for i=1,100 do
|
||||
queue:push(math.random(1,1000))
|
||||
end
|
||||
multi:newEvent(function() -- Felt like using the event object, I hardly use them for anything non internal
|
||||
return not queue:peek()
|
||||
end):OnEvent(function()
|
||||
print("No more data within the queue!")
|
||||
os.exit()
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
You have probable noticed that the output from this is a total mess! Well I though so too, and created the system threaded console!
|
||||
|
||||
# ST - Using the Console
|
||||
`console = THREAD.getConsole()`
|
||||
|
||||
This does guarantee an order to console output, it does ensure that all things are on nice neat lines
|
||||
```lua
|
||||
multi,thread = require("multi"):init()
|
||||
local GLOBAL, THREAD = require("multi.integration.threading"):init()
|
||||
|
||||
console.print("Hello World!")
|
||||
```
|
||||
# ST - SystemThreadedJobQueue
|
||||
`jq = multi:newSystemThreadedJobQueue([NUMBER: threads])` — Creates a system threaded job queue with an optional number of threads
|
||||
- `boolean jq:isEmpty()` — Returns true if the jobqueue is empty false otherwise
|
||||
- `jq.cores = (supplied number) or (the number of cores on your system*2)`
|
||||
- `jq.OnJobCompleted(FUNCTION: func(jID,...))` — Connection that is triggered when a job has been completed. The jobID and returns of the job are supplies as arguments
|
||||
- `self = jq:doToAll(FUNCTION: func)` — Send data to every thread in the job queue. Useful if you want to require a module and have it available on all threads
|
||||
- `self = jq:registerFunction(STRING: name, FUNCTION: func)` — Registers a function on the job queue. Name is the name of function func
|
||||
- `jID = jq:pushJob(STRING: name,[...])` — Pushes a job onto the jobqueue
|
||||
- `handler = jq:newFunction([STRING: name], FUNCTION: func)` — returns a threaded Function that wraps around jq.registerFunction, jq.pushJob() and jq.OnJobCompleted() to provide an easy way to create and work with the jobqueue
|
||||
- `handler.connect(Function: func(returns))` — Connects to the event that is triggered when the returns are avaiable
|
||||
- `VARIAABLE returns = handler.wait()` — Waits until returns are avaiable and then returns them
|
||||
|
||||
**Note:** Created functions using this method act as normal functions on the queue side of things. So you can call the functions from other queue functions as if they were normal functions.
|
||||
|
||||
Example:
|
||||
```lua
|
||||
package.path="?.lua;?/init.lua;?.lua;?/?/init.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
GLOBAL, THREAD = require("multi.integration.threading"):init()
|
||||
local jq = multi:newSystemThreadedJobQueue(4) -- job queue using 4 cores
|
||||
jq:doToAll(function()
|
||||
Important = 15
|
||||
end)
|
||||
jq:registerFunction("test",function(a,b)
|
||||
--print(a,b,a+b)
|
||||
return true
|
||||
end)
|
||||
jq.OnJobCompleted(function(jid,arg)
|
||||
print(jid,arg)
|
||||
end)
|
||||
local jid = jq:pushJob("test",10,5)
|
||||
print("Job pushed! ID = ".. jid)
|
||||
local func = jq:newFunction("test2",function(a,b)
|
||||
print(a,b,a*b)
|
||||
return
|
||||
end)
|
||||
print("Waited",func(10,5).wait())
|
||||
func(5,5).connect(function(ret)
|
||||
print("Connected",ret)
|
||||
os.exit()
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
# ST - SystemThreadedTable
|
||||
`stt = multi:newSystemThreadedTable(STRING: name)`
|
||||
- `stt:init()` — Used to init object over threads
|
||||
- `stt[var] = val`
|
||||
- `val = stt[var]`
|
||||
|
||||
Example:
|
||||
```lua
|
||||
package.path="?.lua;?/init.lua;?.lua;?/?/init.lua;"..package.path
|
||||
multi,thread = require("multi"):init()
|
||||
GLOBAL, THREAD = require("multi.integration.threading"):init()
|
||||
local stt = multi:newSystemThreadedTable("stt")
|
||||
stt["hello"] = "world"
|
||||
multi:newSystemThread("test thread",function()
|
||||
local stt = GLOBAL["stt"]:init()
|
||||
print(stt["hello"])
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
# Network Threads - Multi-Integration WIP Being Reworked
|
||||
More of a fun project of mine then anything core to to the library it will be released and documented when it is ready. I do not have a timeframe for this
|
||||
|
||||
3535
docs/changes.md
Normal file
3535
docs/changes.md
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,6 +0,0 @@
|
||||
package.path="?/init.lua;"..package.path
|
||||
require("multi")
|
||||
multi:newAlarm(5):OnRing(function()
|
||||
os.exit(10)
|
||||
end)
|
||||
multi:mainloop()
|
||||
@ -1,11 +0,0 @@
|
||||
-- Tick Tock Example
|
||||
require("multi")
|
||||
alarm=multi:newAlarm(1)
|
||||
alarm.state=-1 -- set the state to -1
|
||||
alarm.sounds={[-1]="Tick",[1]="Tock"} -- this makes changing between states easy and fast
|
||||
alarm:OnRing(function(self)
|
||||
print(self.sounds[self.state])
|
||||
self.state=self.state*-1 -- change the state in one line
|
||||
self:Reset() -- Reset the alarm so it runs again
|
||||
end)
|
||||
multi:mainloop()
|
||||
@ -1,7 +0,0 @@
|
||||
package.path="?/init.lua;"..package.path
|
||||
require("multi")
|
||||
require("multi.compat.backwards[1,5,0]")
|
||||
multi:newLoop(function(dt,self)
|
||||
print(dt)
|
||||
end)
|
||||
multi:mainloop() -- start the main runner
|
||||
@ -1,34 +0,0 @@
|
||||
package.path="?/init.lua;?.lua;"..package.path
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
multi:newAlarm(2):OnRing(function(self)
|
||||
GLOBAL["NumOfCores"]=sThread.getCores()
|
||||
end)
|
||||
multi:newAlarm(7):OnRing(function(self)
|
||||
GLOBAL["AnotherTest"]=true
|
||||
end)
|
||||
multi:newAlarm(13):OnRing(function(self)
|
||||
GLOBAL["FinalTest"]=true
|
||||
end)
|
||||
multi:newSystemThread("test",function() -- spawns a thread in another lua process
|
||||
require("multi.all") -- now you can do all of your coding with the multi library! You could even spawn more threads from here with the intergration. You would need to require the interaction again though
|
||||
print("Waiting for variable: NumOfCores")
|
||||
print("Got it: ",sThread.waitFor("NumOfCores"))
|
||||
sThread.hold(function()
|
||||
return GLOBAL["AnotherTest"] -- note this would hold the entire systemthread. Spawn a coroutine thread using multi:newThread() or multi:newThreaded...
|
||||
end)
|
||||
print("Holding works!")
|
||||
multi:newThread("tests",function()
|
||||
thread.hold(function()
|
||||
return GLOBAL["FinalTest"] -- note this will not hold the entire systemthread. As seen with the TLoop constantly going!
|
||||
end)
|
||||
print("Final test works!")
|
||||
os.exit()
|
||||
end)
|
||||
local a=0
|
||||
multi:newTLoop(function()
|
||||
a=a+1
|
||||
print(a)
|
||||
end,.5)
|
||||
multi:mainloop()
|
||||
end)
|
||||
multi:mainloop()
|
||||
@ -1,59 +0,0 @@
|
||||
package.path="?/init.lua;?.lua;"..package.path
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init() -- loads the lanesManager and includes the entire multi library
|
||||
local function comma_value(amount)
|
||||
local formatted = amount
|
||||
while true do
|
||||
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
|
||||
if (k==0) then
|
||||
break
|
||||
end
|
||||
end
|
||||
return formatted
|
||||
end
|
||||
multi:newSystemThread("test1",function() -- spawns a thread in another lua process
|
||||
require("multi.all") -- now you can do all of your coding with the multi library! You could even spawn more threads from here with the intergration. You would need to require the interaction again though
|
||||
multi:benchMark(sThread.waitFor("Bench"),nil,"Thread 1"):OnBench(function(self,c) GLOBAL["T1"]=c multi:Stop() end)
|
||||
multi:mainloop()
|
||||
end)
|
||||
multi:newSystemThread("test2",function() -- spawns a thread in another lua process
|
||||
require("multi.all") -- now you can do all of your coding with the multi library! You could even spawn more threads from here with the intergration. You would need to require the interaction again though
|
||||
multi:benchMark(sThread.waitFor("Bench"),nil,"Thread 2"):OnBench(function(self,c) GLOBAL["T2"]=c multi:Stop() end)
|
||||
multi:mainloop()
|
||||
end)
|
||||
multi:newSystemThread("test3",function() -- spawns a thread in another lua process
|
||||
require("multi.all") -- now you can do all of your coding with the multi library! You could even spawn more threads from here with the intergration. You would need to require the interaction again though
|
||||
multi:benchMark(sThread.waitFor("Bench"),nil,"Thread 3"):OnBench(function(self,c) GLOBAL["T3"]=c multi:Stop() end)
|
||||
multi:mainloop()
|
||||
end)
|
||||
multi:newSystemThread("test4",function() -- spawns a thread in another lua process
|
||||
require("multi.all") -- now you can do all of your coding with the multi library! You could even spawn more threads from here with the intergration. You would need to require the interaction again though
|
||||
multi:benchMark(sThread.waitFor("Bench"),nil,"Thread 4"):OnBench(function(self,c) GLOBAL["T4"]=c multi:Stop() end)
|
||||
multi:mainloop()
|
||||
end)
|
||||
multi:newSystemThread("test5",function() -- spawns a thread in another lua process
|
||||
require("multi.all") -- now you can do all of your coding with the multi library! You could even spawn more threads from here with the intergration. You would need to require the interaction again though
|
||||
multi:benchMark(sThread.waitFor("Bench"),nil,"Thread 5"):OnBench(function(self,c) GLOBAL["T5"]=c multi:Stop() end)
|
||||
multi:mainloop()
|
||||
end)
|
||||
multi:newSystemThread("test6",function() -- spawns a thread in another lua process
|
||||
require("multi.all") -- now you can do all of your coding with the multi library! You could even spawn more threads from here with the intergration. You would need to require the interaction again though
|
||||
multi:benchMark(sThread.waitFor("Bench"),nil,"Thread 6"):OnBench(function(self,c) GLOBAL["T6"]=c multi:Stop() end)
|
||||
multi:mainloop()
|
||||
print("Bench: ",comma_value(tostring(sThread.waitFor("T1")+sThread.waitFor("T2")+sThread.waitFor("T3")+sThread.waitFor("T4")+sThread.waitFor("T5")+sThread.waitFor("T6"))))
|
||||
GLOBAL["DONE"]=true
|
||||
end)
|
||||
multi:newThread("test0",function()
|
||||
-- sThread.waitFor("DONE") -- lets hold the main thread completely so we don't eat up cpu
|
||||
-- os.exit()
|
||||
-- when the main thread is holding there is a chance that error handling on the system threads may not work!
|
||||
-- instead we can do this
|
||||
while true do
|
||||
thread.skip(1) -- allow error handling to take place... Otherwise lets keep the main thread running on the low
|
||||
sThread.sleep(.001) -- Sleeping for .001 is a greeat way to keep cpu usage down. Make sure if you aren't doing work to rest. Abuse the hell out of GLOBAL if you need to :P
|
||||
if GLOBAL["DONE"] then
|
||||
os.exit()
|
||||
end
|
||||
end
|
||||
end)
|
||||
GLOBAL["Bench"]=60
|
||||
multi:mainloop()
|
||||
@ -1,48 +0,0 @@
|
||||
package.path="?/init.lua;?.lua;"..package.path -- Spawing threads using 1 method and the sThread.getCores() function!
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init() -- loads the lanesManager and includes the entire multi library
|
||||
local function comma_value(amount)
|
||||
local formatted = amount
|
||||
while true do
|
||||
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
|
||||
if (k==0) then
|
||||
break
|
||||
end
|
||||
end
|
||||
return formatted
|
||||
end
|
||||
GLOBAL["BENCHCOUNT"],GLOBAL["CNUM"],GLOBAL["DONE"]=0,0,0
|
||||
cores=sThread.getCores()
|
||||
function benchmark() -- our single function that will be used across a bunch of threads
|
||||
require("multi.all") -- get the library
|
||||
local n=GLOBAL["CNUM"]; GLOBAL["CNUM"]=n+1 -- do some math so we can identify which thread is which
|
||||
multi:benchMark(sThread.waitFor("BENCH"),nil,"Thread "..n+1):OnBench(function(self,c) GLOBAL["BENCHCOUNT"]=GLOBAL["BENCHCOUNT"]+c; GLOBAL["DONE"]=GLOBAL["DONE"]+1; multi:Stop() end)
|
||||
-- ^ do the bench mark and add to the BENCHCOUNT GLOBAL value, then increment the DONE Value
|
||||
multi:mainloop()
|
||||
end
|
||||
for i=1,cores do -- loop based on the number of cores you have
|
||||
multi:newSystemThread("test"..i,benchmark) -- create a system thread based on the benchmark
|
||||
end
|
||||
multi:newThread("test0",function()
|
||||
while true do
|
||||
thread.skip(1)
|
||||
sThread.sleep(.001)
|
||||
if GLOBAL["DONE"]==cores then
|
||||
print(comma_value(tostring(GLOBAL["BENCHCOUNT"])))
|
||||
os.exit()
|
||||
end
|
||||
end
|
||||
end)
|
||||
GLOBAL["BENCH"]=1
|
||||
print("Platform is: ",multi:getPlatform()) -- returns love2d or lanes depending on which platform you are using... If I add more intergrations then this method will be updated! corona sdk may see this library in the future...
|
||||
multi:mainloop()
|
||||
--[[ Output on my machine! I am using luajit and have 6 cores on my computer. Your numbers will vary, but it should look something like this
|
||||
Intergrated Lanes!
|
||||
Platform is: lanes
|
||||
Thread 1 62442125 Steps in 10 second(s)!
|
||||
Thread 2 61379095 Steps in 10 second(s)!
|
||||
Thread 3 62772502 Steps in 10 second(s)!
|
||||
Thread 4 62740684 Steps in 10 second(s)!
|
||||
Thread 5 60926715 Steps in 10 second(s)!
|
||||
Thread 6 61793175 Steps in 10 second(s)!
|
||||
372,054,296
|
||||
]]
|
||||
@ -1,27 +0,0 @@
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
queue=multi:newSystemThreadedQueue("QUEUE"):init()
|
||||
queue:push("This is a test")
|
||||
queue:push("This is a test2")
|
||||
queue:push("This is a test3")
|
||||
queue:push("This is a test4")
|
||||
multi:newSystemThread("test2",function()
|
||||
queue=sThread.waitFor("QUEUE"):init()
|
||||
data=queue:pop()
|
||||
while data do
|
||||
print(data)
|
||||
data=queue:pop()
|
||||
end
|
||||
queue:push("This is a test5")
|
||||
queue:push("This is a test6")
|
||||
queue:push("This is a test7")
|
||||
queue:push("This is a test8")
|
||||
end)
|
||||
multi:newThread("test!",function() -- this is a lua thread
|
||||
thread.sleep(.1)
|
||||
data=queue:pop()
|
||||
while data do
|
||||
print(data)
|
||||
data=queue:pop()
|
||||
end
|
||||
end)
|
||||
multi:mainloop()
|
||||
@ -1,28 +0,0 @@
|
||||
package.path="?/init.lua;"..package.path -- slightly different usage of the code
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
queue=multi:newSystemThreadedQueue("QUEUE")
|
||||
queue:push(1)
|
||||
queue:push(2)
|
||||
queue:push(3)
|
||||
queue:push(4)
|
||||
queue:push(5)
|
||||
queue:push(6)
|
||||
multi:newSystemThread("STHREAD_1",function()
|
||||
queue=sThread.waitFor("QUEUE"):init()
|
||||
GLOBAL["QUEUE"]=nil
|
||||
data=queue:pop()
|
||||
while data do
|
||||
print(data)
|
||||
data=queue:pop()
|
||||
end
|
||||
end)
|
||||
multi:newThread("THREAD_1",function()
|
||||
while true do
|
||||
if GLOBAL["QUEUE"]==nil then
|
||||
print("Deleted a Global!")
|
||||
break
|
||||
end
|
||||
thread.skip(1)
|
||||
end
|
||||
end)
|
||||
multi:mainloop()
|
||||
@ -1,14 +0,0 @@
|
||||
-- lanes Desktop lua! NOTE: this is in lanesintergratetest6.lua in the examples folder
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
test=multi:newSystemThreadedTable("YO"):init()
|
||||
test["test1"]="lol"
|
||||
multi:newSystemThread("test",function()
|
||||
tab=sThread.waitFor("YO"):init()
|
||||
print(tab["test1"])
|
||||
sThread.sleep(3)
|
||||
tab["test2"]="Whats so funny?"
|
||||
end)
|
||||
multi:newThread("test2",function()
|
||||
print(test:waitFor("test2"))
|
||||
end)
|
||||
multi:mainloop()
|
||||
@ -1,29 +0,0 @@
|
||||
-- Creating the object using lanes manager to show case this. Examples has the file for love2d
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
jQueue=multi:newSystemThreadedJobQueue(n) -- this internally creates System threads. By defualt it will use the # of processors on your system You can set this number though.
|
||||
-- Only create 1 jobqueue! For now making more than 1 is buggy. You only really need one though. Just register new functions if you want 1 queue to do more. The one reason though is keeping track of jobIDs. I have an idea that I will roll out in the next update.
|
||||
jQueue:registerJob("TEST_JOB",function(a,s)
|
||||
math.randomseed(s)
|
||||
-- We will push a random #
|
||||
TEST_JOB2() -- You can call other registered functions as well!
|
||||
return math.random(0,255) -- send the result to the main thread
|
||||
end)
|
||||
jQueue:registerJob("TEST_JOB2",function()
|
||||
print("Test Works!") -- this is called from the job since it is registered on the same queue
|
||||
end)
|
||||
jQueue:start()
|
||||
tableOfOrder={} -- This is how we will keep order of our completed jobs. There is no guarantee that the order will be correct
|
||||
jQueue.OnJobCompleted(function(JOBID,n) -- whenever a job is completed you hook to the event that is called. This passes the JOBID folled by the returns of the job
|
||||
-- JOBID is the completed job, starts at 1 and counts up by 1.
|
||||
-- Threads finish at different times so jobids may be passed out of order! Be sure to have a way to order them
|
||||
tableOfOrder[JOBID]=n -- we order ours by putting them into a table
|
||||
if #tableOfOrder==10 then
|
||||
print("We got all of the pieces!")
|
||||
end
|
||||
end)
|
||||
-- LEts push the jobs now
|
||||
for i=1,10 do -- Job Name of registered function, ... varargs
|
||||
jQueue:pushJob("TEST_JOB","This is a test!",math.random(1,1000000))
|
||||
end
|
||||
print("I pushed all of the jobs :)")
|
||||
multi:mainloop() -- Start the main loop :D
|
||||
@ -1,11 +0,0 @@
|
||||
package.path="../?.lua;../?/init.lua;"..package.path
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
cmd=multi:newSystemThreadedExecute("SystemThreadedExecuteTest.lua") -- This file is important!
|
||||
cmd.OnCMDFinished(function(code) -- callback function to grab the exit code... Called when the command goes through
|
||||
print("Got Code: "..code)
|
||||
end)
|
||||
multi:newTLoop(function()
|
||||
print("...")
|
||||
end,1)
|
||||
multi:mainloop()
|
||||
|
||||
@ -1,26 +0,0 @@
|
||||
--~ package.path="?/init.lua;"..package.path
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
jQueue=multi:newSystemThreadedJobQueue(n)
|
||||
jQueue:registerJob("TEST_JOB",function(a,s)
|
||||
math.randomseed(s)
|
||||
TEST_JOB2()
|
||||
return math.random(0,255)
|
||||
end)
|
||||
jQueue:registerJob("TEST_JOB2",function()
|
||||
print("Test Works!")
|
||||
end)
|
||||
jQueue:start()
|
||||
jQueue:doToAll(function()
|
||||
print("Doing this 16? times!")
|
||||
end)
|
||||
for i=1,10 do -- Job Name of registered function, ... varargs
|
||||
jQueue:pushJob("TEST_JOB","This is a test!",math.random(1,1000000))
|
||||
end
|
||||
tableOfOrder={}
|
||||
jQueue.OnJobCompleted(function(JOBID,n)
|
||||
tableOfOrder[JOBID]=n
|
||||
if #tableOfOrder==10 then
|
||||
print("We got all of the pieces!")
|
||||
end
|
||||
end)
|
||||
multi:mainloop()
|
||||
@ -1,9 +0,0 @@
|
||||
-- like the while loop (kinda)
|
||||
require("multi")
|
||||
loop=multi:newLoop(function(self,dt)
|
||||
if dt>10 then
|
||||
print("Enough time has passed!")
|
||||
self:Break() -- lets break this thing
|
||||
end
|
||||
end)
|
||||
multi:mainloop()
|
||||
@ -1,15 +0,0 @@
|
||||
function gui:Clickable()
|
||||
local x,y,w,h=love.graphics.getScissor()
|
||||
local mx=love.mouse.getX()
|
||||
local my=love.mouse.getY()
|
||||
if _GuiPro.HasStencel then
|
||||
local obj=_GuiPro.StencelHolder
|
||||
if self:isDescendant(obj) then
|
||||
return math.sqrt((mx-obj.x)^2+(my-obj.y)^2)<=(obj.offset.size.x or 0)
|
||||
end
|
||||
end
|
||||
if not(x) then
|
||||
return true
|
||||
end
|
||||
return not(mx>x+w or mx<x or my>y+h or my<y)
|
||||
end
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,5 +0,0 @@
|
||||
function DrawThings(items)
|
||||
for i=1,#items do
|
||||
items[i]:draw()
|
||||
end
|
||||
end
|
||||
@ -1,85 +0,0 @@
|
||||
function gui:OnClicked(func)
|
||||
table.insert(self.funcs,func)
|
||||
end
|
||||
function gui:OnReleased(func)
|
||||
table.insert(self.funcs2,func)
|
||||
end
|
||||
function gui:OnEnter(func)
|
||||
table.insert(self.funcs3,func)
|
||||
end
|
||||
function gui:OnExit(func)
|
||||
table.insert(self.funcs4,func)
|
||||
end
|
||||
function gui:OnUpdate(func)
|
||||
table.insert(self.funcs5,func)
|
||||
end
|
||||
function gui:OnDragStart(func)
|
||||
table.insert(self.func8,func)
|
||||
end
|
||||
function gui:OnDragging(func)
|
||||
table.insert(self.func6,func)
|
||||
end
|
||||
function gui:OnDragEnd(func)
|
||||
table.insert(self.func7,func)
|
||||
end
|
||||
function gui:WhileHovering(func)
|
||||
table.insert(self.func9,func)
|
||||
end
|
||||
function gui:OnMouseMoved(func)
|
||||
table.insert(self.func10,func)
|
||||
end
|
||||
function gui:getChildren()
|
||||
return self.Children
|
||||
end
|
||||
function gui:LClicked()
|
||||
return self.lclicked
|
||||
end
|
||||
function gui:RClicked()
|
||||
return self.rclicked
|
||||
end
|
||||
function gui:MClicked()
|
||||
return self.mclicked
|
||||
end
|
||||
function gui:Clicked()
|
||||
return (self.lclicked or self.rclicked)
|
||||
end
|
||||
function gui:Hovering()
|
||||
return self.hovering
|
||||
end
|
||||
function gui:FreeConnections()
|
||||
self.funcs={function(b,self) if b=="l" then self.LRE=true end end,function(b,self) if b=="r" then self.RRE=true end end,function(b,self) if b=="m" then self.MRE=true end end}
|
||||
self.funcs2={function(b,self) if b=="l" then self.LRE=false end end,function(b,self) if b=="r" then self.RRE=false end end,function(b,self) if b=="m" then self.MRE=false end end}
|
||||
self.funcs3={function(self) self.HE=true end}
|
||||
self.funcs4={function(self) self.HE=false end}
|
||||
self.funcs5={function(self) self.x=(self.Parent.width*self.scale.pos.x)+self.offset.pos.x+self.Parent.x self.y=(self.Parent.height*self.scale.pos.y)+self.offset.pos.y+self.Parent.y self.width=(self.Parent.width*self.scale.size.x)+self.offset.size.x self.height=(self.Parent.height*self.scale.size.y)+self.offset.size.y end}
|
||||
end
|
||||
function gui:LClick()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i]("l",self)
|
||||
end
|
||||
end
|
||||
function gui:RClick()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i]("r",self)
|
||||
end
|
||||
end
|
||||
function gui:MClick()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i]("m",self)
|
||||
end
|
||||
end
|
||||
function gui:LRelease()
|
||||
for i=1,#self.funcs2 do
|
||||
self.funcs2[i]("l",self)
|
||||
end
|
||||
end
|
||||
function gui:RRelease()
|
||||
for i=1,#self.funcs2 do
|
||||
self.funcs2[i]("r",self)
|
||||
end
|
||||
end
|
||||
function gui:MRelease()
|
||||
for i=1,#self.funcs2 do
|
||||
self.funcs2[i]("m",self)
|
||||
end
|
||||
end
|
||||
@ -1,46 +0,0 @@
|
||||
function UpdateThings(items)
|
||||
for i=#items,1,-1 do
|
||||
if items[i]:LClicked() then
|
||||
for g=1,#items[i].funcs do
|
||||
items[i].funcs[g]("l",items[i],love.mouse.getX()-items[i].x,love.mouse.getY()-items[i].y)
|
||||
end
|
||||
elseif items[i]:RClicked() then
|
||||
for g=1,#items[i].funcs do
|
||||
items[i].funcs[g]("r",items[i],love.mouse.getX()-items[i].x,love.mouse.getY()-items[i].y)
|
||||
end
|
||||
elseif items[i]:MClicked() then
|
||||
for g=1,#items[i].funcs do
|
||||
items[i].funcs[g]("m",items[i],love.mouse.getX()-items[i].x,love.mouse.getY()-items[i].y)
|
||||
end
|
||||
end
|
||||
if not(items[i]:LClicked()) and items[i].LRE then
|
||||
for g=1,#items[i].funcs2 do
|
||||
items[i].funcs2[g]("l",items[i],love.mouse.getX()-items[i].x,love.mouse.getY()-items[i].y)
|
||||
end
|
||||
elseif not(items[i]:RClicked()) and items[i].RRE then
|
||||
for g=1,#items[i].funcs2 do
|
||||
items[i].funcs2[g]("r",items[i],love.mouse.getX()-items[i].x,love.mouse.getY()-items[i].y)
|
||||
end
|
||||
elseif not(items[i]:MClicked()) and items[i].MRE then
|
||||
for g=1,#items[i].funcs2 do
|
||||
items[i].funcs2[g]("m",items[i],love.mouse.getX()-items[i].x,love.mouse.getY()-items[i].y)
|
||||
end
|
||||
end
|
||||
if items[i]:Hovering() and items[i].HE==false then
|
||||
for g=1,#items[i].funcs3 do
|
||||
items[i].funcs3[g](items[i],love.mouse.getX()-items[i].x,love.mouse.getY()-items[i].y)
|
||||
end
|
||||
elseif not(items[i]:Hovering()) and items[i].HE==true then
|
||||
for g=1,#items[i].funcs4 do
|
||||
items[i].funcs4[g](items[i],love.mouse.getX()-items[i].x,love.mouse.getY()-items[i].y)
|
||||
end
|
||||
elseif items[i]:Hovering() then
|
||||
for g=1,#items[i].func9 do
|
||||
items[i].func9[g](items[i],love.mouse.getX()-items[i].x,love.mouse.getY()-items[i].y)
|
||||
end
|
||||
end
|
||||
for g=1,#items[i].funcs5 do
|
||||
items[i].funcs5[g](items[i])
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,25 +0,0 @@
|
||||
function GetAllChildren(Object)
|
||||
local Stuff = {}
|
||||
function Seek(Items)
|
||||
for i=1,#Items do
|
||||
if Items[i].Visible==true then
|
||||
table.insert(Stuff,Items[i])
|
||||
local NItems = Items[i]:getChildren()
|
||||
if NItems ~= nil then
|
||||
Seek(NItems)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local Objs = Object:getChildren()
|
||||
for i=1,#Objs do
|
||||
if Objs[i].Visible==true then
|
||||
table.insert(Stuff,Objs[i])
|
||||
local Items = Objs[i]:getChildren()
|
||||
if Items ~= nil then
|
||||
Seek(Items)
|
||||
end
|
||||
end
|
||||
end
|
||||
return Stuff
|
||||
end
|
||||
@ -1,21 +0,0 @@
|
||||
function GetAllChildren2(Object)
|
||||
local Stuff = {}
|
||||
function Seek(Items)
|
||||
for i=1,#Items do
|
||||
table.insert(Stuff,Items[i])
|
||||
local NItems = Items[i]:getChildren()
|
||||
if NItems ~= nil then
|
||||
Seek(NItems)
|
||||
end
|
||||
end
|
||||
end
|
||||
local Objs = Object:getChildren()
|
||||
for i=1,#Objs do
|
||||
table.insert(Stuff,Objs[i])
|
||||
local Items = Objs[i]:getChildren()
|
||||
if Items ~= nil then
|
||||
Seek(Items)
|
||||
end
|
||||
end
|
||||
return Stuff
|
||||
end
|
||||
@ -1,14 +0,0 @@
|
||||
function gui:eventable()
|
||||
if self.important then
|
||||
return true
|
||||
end
|
||||
if _GuiPro.Hierarchy then
|
||||
if _GuiPro.TopHovered~=nil then
|
||||
return self:isDescendant(_GuiPro.TopHovered) or _GuiPro.TopHovered==self
|
||||
else
|
||||
return true
|
||||
end
|
||||
else
|
||||
return true
|
||||
end
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:full()
|
||||
self:SetDualDim(nil,nil,nil,nil,nil,nil,1,1)
|
||||
end
|
||||
@ -1,244 +0,0 @@
|
||||
function gui.enableAutoWindowScaling(b)
|
||||
_GuiPro.DPI_ENABLED=b or true
|
||||
_defaultfont=love.graphics.newFont(12*love.window.getPixelScale())
|
||||
end
|
||||
function filter(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
if type(name)~="string" then
|
||||
sh=sw
|
||||
sw=sy
|
||||
sy=sx
|
||||
sx=h
|
||||
h=w
|
||||
w=y
|
||||
y=x
|
||||
x=name
|
||||
end
|
||||
return x,y,w,h,sx,sy,sw,sh
|
||||
end
|
||||
function gui:newBase(tp,name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
_GuiPro.count=_GuiPro.count+1
|
||||
local c = {}
|
||||
setmetatable(c, gui)
|
||||
if self==gui then
|
||||
c.Parent=_GuiPro
|
||||
else
|
||||
c.Parent=self
|
||||
end
|
||||
c.segments=nil
|
||||
c.ry=nil
|
||||
c.rx=nil
|
||||
c.DPI=1
|
||||
if _GuiPro.DPI_ENABLED then
|
||||
c.DPI=love.window.getPixelScale()
|
||||
x, y, w, h=c.DPI*x,c.DPI*y,c.DPI*w,c.DPI*h
|
||||
end
|
||||
c.centerFontY=true
|
||||
c.FormFactor="rectangle"
|
||||
c.Type=tp
|
||||
c.Active=true
|
||||
c.form="rectangle"
|
||||
c.Draggable=false
|
||||
c.Name=name or "Gui"..tp
|
||||
c:SetName(name)
|
||||
c.BorderSize=1
|
||||
c.BorderColor={0,0,0}
|
||||
c.VIS=true
|
||||
c.Visible=true
|
||||
c.oV=true
|
||||
c.Children={}
|
||||
c.hovering=false
|
||||
c.rclicked=false
|
||||
c.lclicked=false
|
||||
c.mclicked=false
|
||||
c.clicked=false
|
||||
c.Visibility=1
|
||||
c.ClipDescendants=false
|
||||
c.TextWrap=true
|
||||
c.scale={}
|
||||
c.scale.size={}
|
||||
c.scale.size.x=sw or 0
|
||||
c.scale.size.y=sh or 0
|
||||
c.offset={}
|
||||
c.offset.size={}
|
||||
c.offset.size.x=w or 0
|
||||
c.offset.size.y=h or 0
|
||||
c.scale.pos={}
|
||||
c.scale.pos.x=sx or 0
|
||||
c.scale.pos.y=sy or 0
|
||||
c.offset.pos={}
|
||||
c.offset.pos.x=x or 0
|
||||
c.offset.pos.y=y or 0
|
||||
c.width = 0
|
||||
c.height = 0
|
||||
c.LRE=false
|
||||
c.RRE=false
|
||||
c.MRE=false
|
||||
c.Color = {255, 255, 255}
|
||||
function c:setRoundness(rx,ry,segments)
|
||||
self.segments=segments
|
||||
self.ry=ry
|
||||
self.rx=rx
|
||||
end
|
||||
function c.stfunc()
|
||||
love.graphics.rectangle("fill", c.x, c.y, c.width, c.height,c.rx,c.ry,c.segments)
|
||||
end
|
||||
function c:hasRoundness()
|
||||
return (self.ry or self.rx)
|
||||
end
|
||||
c.funcs={function(b,self)
|
||||
if b=="l" then
|
||||
self.LRE=true
|
||||
end
|
||||
end,
|
||||
function(b,self)
|
||||
if b=="r" then
|
||||
self.RRE=true
|
||||
end
|
||||
end,
|
||||
function(b,self)
|
||||
if b=="m" then
|
||||
self.MRE=true
|
||||
end
|
||||
end}
|
||||
c.funcs2={function(b,self)
|
||||
if b=="l" then
|
||||
self.LRE=false
|
||||
end
|
||||
end,
|
||||
function(b,self)
|
||||
if b=="r" then
|
||||
self.RRE=false
|
||||
end
|
||||
end,
|
||||
function(b,self)
|
||||
if b=="m" then
|
||||
self.MRE=false
|
||||
end
|
||||
end}
|
||||
c.HE=false
|
||||
c.funcs3={function(self)
|
||||
self.HE=true
|
||||
end}
|
||||
c.funcs4={function(self)
|
||||
self.HE=false
|
||||
end}
|
||||
c.funcs5={}
|
||||
c.tid={}
|
||||
c.touchcount=0
|
||||
c.x=(c.Parent.width*c.scale.pos.x)+c.offset.pos.x+c.Parent.x
|
||||
c.y=(c.Parent.height*c.scale.pos.y)+c.offset.pos.y+c.Parent.y
|
||||
c.width=(c.Parent.width*c.scale.size.x)+c.offset.size.x
|
||||
c.height=(c.Parent.height*c.scale.size.y)+c.offset.size.y
|
||||
c.func6={}
|
||||
c.func7={function() _GuiPro.DragItem={} end}
|
||||
c.func8={function(self) _GuiPro.DragItem=self end}
|
||||
c.func9={}
|
||||
c.func10={}
|
||||
function c:ImageRule()
|
||||
if self.Image then
|
||||
local sx=self.width/self.ImageWidth
|
||||
local sy=self.height/self.ImageHeigth
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.ImageVisibility*255)
|
||||
if self.width~=self.ImageWidth and self.height~=self.ImageHeigth then
|
||||
love.graphics.draw(self.Image,self.x,self.y,math.rad(self.rotation),sx,sy)
|
||||
else
|
||||
love.graphics.draw(self.Image,self.Quad,self.x,self.y,math.rad(self.rotation),sx,sy)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:VideoRule()
|
||||
if self.Video then
|
||||
local sx=self.width/self.VideoWidth
|
||||
local sy=self.height/self.VideoHeigth
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.VideoVisibility*255)
|
||||
if self.width~=self.VideoWidth and self.height~=self.VideoHeigth then
|
||||
love.graphics.draw(self.Video,self.x,self.y,math.rad(self.rotation),sx,sy)
|
||||
else
|
||||
love.graphics.draw(self.Video,self.Quad,self.x,self.y,math.rad(self.rotation),sx,sy)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:repeatImage(b,b2)
|
||||
if b then
|
||||
self.Image:setWrap(b,b2 or "repeat")
|
||||
function self:ImageRule()
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.ImageVisibility*255)
|
||||
love.graphics.draw(self.Image,self.Quad,self.x,self.y,math.rad(self.rotation))
|
||||
end
|
||||
else
|
||||
sx=self.width/self.ImageWidth
|
||||
sy=self.height/self.ImageHeigth
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.ImageVisibility*255)
|
||||
love.graphics.draw(self.Image,self.Quad,self.x,self.y,math.rad(self.rotation),sx,sy)
|
||||
end
|
||||
end
|
||||
function c:Mutate(t)
|
||||
for i,v in pairs(t) do
|
||||
_GuiPro.self=self
|
||||
if type(i)=="number" then
|
||||
loadstring("_GuiPro.self:"..v)()
|
||||
elseif i:match"__self__" then
|
||||
local ind=i:match"__self__(.+)"
|
||||
if not self[ind] then self[ind]={} end
|
||||
loadstring("_GuiPro.self."..ind.."=_GuiPro.self:"..v)()
|
||||
elseif i:match"__child__" then
|
||||
local ind,child = i:match"__child__(%S-)_(.+)"
|
||||
self[ind][child]=v
|
||||
else
|
||||
self[i]=v
|
||||
end
|
||||
end
|
||||
return self
|
||||
end
|
||||
c:WhileHovering(function(self)
|
||||
self.omx=self.nmx
|
||||
self.omy=self.nmy
|
||||
self.nmx=love.mouse.getX()
|
||||
self.nmy=love.mouse.getY()
|
||||
if self.omx~=self.nmx or self.omy~=self.nmy then
|
||||
for i=1,#self.func10 do
|
||||
if self and self.nmx and self.nmy and self.omx and self.omy then
|
||||
self.func10[i](self,self.nmx,self.nmy,self.omx,self.omy)
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.WasBeingDragged==true and love.mouse.isDown(self.dragbut or "m")==false and self.Type~="TextImageButtonFrameDrag" then
|
||||
for i=1,#self.func7 do
|
||||
self.func7[i](self,(love.mouse.getX())-self.width/2,(love.mouse.getY())-self.height/2)
|
||||
end
|
||||
end
|
||||
if _GuiPro.hasDrag==false and love.mouse.isDown(self.dragbut or "m") then
|
||||
for i=1,#self.func8 do
|
||||
self.func8[i](self,(love.mouse.getX())-self.width/2,(love.mouse.getY())-self.height/2)
|
||||
end
|
||||
end
|
||||
if self.IsBeingDragged==true then
|
||||
_GuiPro.hasDrag=true
|
||||
self.WasBeingDragged=true
|
||||
elseif self.WasBeingDragged==true and self.IsBeingDragged==false then
|
||||
self.WasBeingDragged=false
|
||||
_GuiPro.hasDrag=false
|
||||
end
|
||||
if self.Draggable==true and love.mouse.isDown(self.dragbut or "m") and _GuiPro.hasDrag==false then
|
||||
for i=1,#self.func6 do
|
||||
self.func6[i](self,(love.mouse.getX())-self.width/2,(love.mouse.getY())-self.height/2)
|
||||
end
|
||||
_GuiPro.hasDrag=true
|
||||
if self.FormFactor:lower()=="circle" or self.FormFactor:lower()=="c" or self.FormFactor:lower()=="cir" then
|
||||
self.IsBeingDragged=true
|
||||
x=(love.mouse.getX()-self.x)
|
||||
y=(love.mouse.getY()-self.y)
|
||||
self:Move(x,y)
|
||||
elseif self.FormFactor:lower()=="rectangle" or self.FormFactor:lower()=="r" or self.FormFactor:lower()=="rect" then
|
||||
self.IsBeingDragged=true
|
||||
x=(love.mouse.getX()-self.x)-self.width/2
|
||||
y=(love.mouse.getY()-self.y)-self.height/2
|
||||
self:Move(x,y)
|
||||
end
|
||||
else
|
||||
self.IsBeingDragged=false
|
||||
end
|
||||
end)
|
||||
table.insert(c.Parent.Children,c)
|
||||
return c
|
||||
end
|
||||
@ -1,7 +0,0 @@
|
||||
_GuiPro.mousedownfunc=love.mouse.isDown
|
||||
function love.mouse.isDown(b)
|
||||
if not(b) then
|
||||
return false
|
||||
end
|
||||
return _GuiPro.mousedownfunc(({["l"]=1,["r"]=2,["m"]=3})[b] or b)
|
||||
end
|
||||
@ -1,40 +0,0 @@
|
||||
function gui:toString() -- oh boy this is gonna be painful lol
|
||||
multi:newThread("saving data: ",function()
|
||||
local dat=bin.stream("test.dat",false)
|
||||
function GetAllChildren2(Object)
|
||||
local Stuff = {}
|
||||
function Seek(Items)
|
||||
for i=1,#Items do
|
||||
--table.insert(Stuff,Items[i])
|
||||
for a,v in pairs(Items[i]) do
|
||||
-- dat:tackE(a.."|"..tostring(v))
|
||||
print(a.."|"..tostring(v))
|
||||
-- dat.workingfile:flush()
|
||||
end
|
||||
thread.skip()
|
||||
local NItems = Items[i]:getChildren()
|
||||
if NItems ~= nil then
|
||||
Seek(NItems)
|
||||
end
|
||||
end
|
||||
end
|
||||
local Objs = Object:getChildren()
|
||||
for i=1,#Objs do
|
||||
-- table.insert(Stuff,Objs[i])
|
||||
for a,v in pairs(Objs[i]) do
|
||||
-- dat:tackE(a.."|"..tostring(v))
|
||||
print(Objs[i].Type..":"..a.."|"..tostring(v))
|
||||
-- dat.workingfile:flush()
|
||||
end
|
||||
thread.skip()
|
||||
local Items = Objs[i]:getChildren()
|
||||
if Items ~= nil then
|
||||
Seek(Items)
|
||||
end
|
||||
end
|
||||
-- dat:tofile("test.dat")
|
||||
return Stuff
|
||||
end
|
||||
GetAllChildren2(self)
|
||||
end)
|
||||
end
|
||||
@ -1,204 +0,0 @@
|
||||
--[[WORKING ON
|
||||
doubleTap - UnFinished!
|
||||
touchRendering - Broken
|
||||
]]
|
||||
function gui:TClickable(mx,my)
|
||||
local x,y,w,h=love.graphics.getScissor()
|
||||
if _GuiPro.HasStencel then
|
||||
local obj=_GuiPro.StencelHolder
|
||||
if self:isDescendant(obj) then
|
||||
return math.sqrt((mx-obj.x)^2+(my-obj.y)^2)<=(obj.offset.size.x or 0)
|
||||
end
|
||||
end
|
||||
if not(x) then
|
||||
return true
|
||||
end
|
||||
return not(mx>x+w or mx<x or my>y+h or my<y)
|
||||
end
|
||||
function gui:touchable(t)
|
||||
local touches = love.touch.getTouches()
|
||||
local x,y=0,0
|
||||
for i, id in ipairs(touches) do
|
||||
if self.id==id then
|
||||
x, y = love.touch.getPosition(id)
|
||||
return (x > self.x and x < self.x+self.width and y > self.y and y < self.y+self.height and self:TClickable(x,y) and self:eventable())
|
||||
end
|
||||
end
|
||||
self.id=-1
|
||||
end
|
||||
multi:newTask(function() -- A bit of post-loading haha
|
||||
gui.touchpressed=multi:newConnection()
|
||||
gui.touchreleased=multi:newConnection()
|
||||
gui.touchmoved=multi:newConnection()
|
||||
love.touchpressed=Library.convert(love.touchpressed or function() end)
|
||||
love.touchreleased=Library.convert(love.touchreleased or function() end)
|
||||
love.touchmoved=Library.convert(love.touchmoved or function() end)
|
||||
love.touchpressed:inject(function(id, x, y, dx, dy, pressure) gui.touchpressed:Fire(id, x, y, dx, dy, pressure) return {id, x, y, dx, dy, pressure} end,1)
|
||||
love.touchreleased:inject(function(id, x, y, dx, dy, pressure) gui.touchreleased:Fire(id, x, y, dx, dy, pressure) return {id, x, y, dx, dy, pressure} end,1)
|
||||
love.touchmoved:inject(function(id, x, y, dx, dy, pressure) gui.touchmoved:Fire(id, x, y, dx, dy, pressure) return {id, x, y, dx, dy, pressure} end,1)
|
||||
_GuiPro.TouchReady=true
|
||||
_GuiPro.TouchRegister={}
|
||||
gui.touchpressed:connect(function(id, x, y, dx, dy, pressure)
|
||||
for i,v in pairs(_GuiPro.TouchRegister) do
|
||||
if #v.tid==0 then
|
||||
if (x > v.x and x < v.x+v.width and y > v.y and y < v.y+v.height and v:TClickable(x,y) and v:eventable()) then
|
||||
v:addTID(id)
|
||||
v.touchcount=1
|
||||
for i=1,#v.ToFuncP do
|
||||
v.ToFuncP[i](v,id, x-v.x, y-v.y, dx, dy or 0, pressure or 1)
|
||||
end
|
||||
end
|
||||
elseif not(v:hasTID(id)) then
|
||||
if (x > v.x and x < v.x+v.width and y > v.y and y < v.y+v.height and v:TClickable(x,y) and v:eventable()) then
|
||||
v:addTID(id)
|
||||
v.touchcount=v.touchcount+1
|
||||
for i=1,#v.ToFuncP do
|
||||
v.ToFuncP[i](v,id, x-v.x, y-v.y, dx, dy or 0, pressure or 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
gui.touchreleased:connect(function(id, x, y, dx, dy, pressure)
|
||||
for i,v in pairs(_GuiPro.TouchRegister) do
|
||||
if v:hasTID(id) then
|
||||
v:removeTID(id)
|
||||
for i=1,#v.ToFuncR do
|
||||
v.ToFuncR[i](v,id, x-v.x, y-v.y, dx, dy or 0, pressure or 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
gui.touchmoved:connect(function(id, x, y, dx, dy, pressure)
|
||||
for i,v in pairs(_GuiPro.TouchRegister) do
|
||||
if v:hasTID(id) and (x > v.x and x < v.x+v.width and y > v.y and y < v.y+v.height and v:TClickable(x,y) and v:eventable()) then
|
||||
for i=1,#v.ToFuncM do
|
||||
v.ToFuncM[i](v,id, x-v.x, y-v.y, dx, dy or 0, pressure or 1)
|
||||
end
|
||||
elseif v:hasTID(id) and not((x > v.x and x < v.x+v.width and y > v.y and y < v.y+v.height and v:TClickable(x,y) and v:eventable())) then
|
||||
v:removeTID(id)
|
||||
for i=1,#v.ToFuncR do
|
||||
v.ToFuncR[i](v,id, x-v.x, y-v.y, dx, dy or 0, pressure or 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
end)
|
||||
-- now that that is done lets set up some more post loading checks
|
||||
_GuiPro.int=multi:newProcess()
|
||||
_GuiPro.int:Start()
|
||||
_GuiPro.int:setJobSpeed(.001)
|
||||
_GuiPro.EXACT=0
|
||||
_GuiPro.LAX=.01
|
||||
_GuiPro.LAZY=.05
|
||||
-- now lets define the reg function
|
||||
function gui.Compare(a,b,v,tp)
|
||||
if tp==">" then
|
||||
if (a+v>b or a-v>b) then
|
||||
return true
|
||||
end
|
||||
elseif tp=="<" then
|
||||
if (a+v<b or a-v<b) then
|
||||
return true
|
||||
end
|
||||
elseif tp=="<=" then
|
||||
if (a+v<=b or a-v<=b) then
|
||||
return true
|
||||
end
|
||||
elseif tp==">=" then
|
||||
if (a+v>=b or a-v>=b) then
|
||||
return true
|
||||
end
|
||||
elseif tp=="==" then -- this one is gonna be tricky
|
||||
if (a>=b-v and a<=b+v) or (b>=a-v and b<=a+v) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
function gui:regesterTouch()
|
||||
local obj=self
|
||||
obj.ToFuncP={}
|
||||
obj.ToFuncM={}
|
||||
obj.ToFuncR={}
|
||||
obj.To2Func={}
|
||||
obj.ToDTFunc={}
|
||||
obj.touchRendering =_GuiPro.EXACT -- exact(0), lax(), #
|
||||
function obj:removeTID(id)
|
||||
for i=1,#self.tid do
|
||||
if self.tid[i]==id then
|
||||
table.remove(self.tid,i)
|
||||
self.touchcount=self.touchcount-1
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
function obj:hasTID(id)
|
||||
for i=1,#self.tid do
|
||||
if self.tid[i]==id then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
obj.txl1=0
|
||||
obj.tyl1=0
|
||||
obj.txl2=0
|
||||
obj.tyl2=0
|
||||
obj.LS=0
|
||||
obj:OnUpdate(function(self)
|
||||
if self.touchcount==2 then
|
||||
local x1,y1=love.touch.getPosition( self.tid[1] )
|
||||
local x2,y2=love.touch.getPosition( self.tid[2] )
|
||||
local CS=math.sqrt((x2-x1)^2+(y2-y1)^2)
|
||||
if gui.Compare(CS,self.LS,self.touchRendering,">") then
|
||||
for i=1,#self.To2Func do
|
||||
self.To2Func[i](self,CS,x1-self.x,y1-self.y,x2-self.x,y2-self.y)
|
||||
end
|
||||
elseif gui.Compare(CS,self.LS,self.touchRendering,"<") then
|
||||
for i=1,#self.To2Func do
|
||||
self.To2Func[i](self,-CS,x1-self.x,y1-self.y,x2-self.x,y2-self.y)
|
||||
end
|
||||
elseif gui.Compare(CS,self.LS,self.touchRendering,"==") then
|
||||
for i=1,#self.To2Func do
|
||||
self.To2Func[i](self,0,x1-self.x,y1-self.y,x2-self.x,y2-self.y)
|
||||
end
|
||||
end
|
||||
-- if self.txl1~=x1 or self.txl2~=x2 or self.tyl1~=y1 or self.tyl2~=y2 then
|
||||
-- for i=1,#self.To2Func do
|
||||
-- self.To2Func[i](self,0,x1-self.x,y1-self.y,x2-self.x,y2-self.y)
|
||||
-- end
|
||||
-- end
|
||||
self.LS=CS
|
||||
self.txl1=x1
|
||||
self.txl2=x2
|
||||
self.tyl1=y1
|
||||
self.tyl2=y2
|
||||
end
|
||||
end)
|
||||
function obj:OnDoubleTap(func)
|
||||
table.insert(self.ToDTFunc,func)
|
||||
end
|
||||
function obj:On2TouchMoved(func)
|
||||
table.insert(self.To2Func,func)
|
||||
end
|
||||
function obj:addTID(id)
|
||||
table.insert(self.tid,id)
|
||||
end
|
||||
function obj:OnTouchPressed(func)
|
||||
table.insert(self.ToFuncP,func) -- event for touches
|
||||
end
|
||||
function obj:OnTouchReleased(func) -- event for touches
|
||||
table.insert(self.ToFuncR,func)
|
||||
end
|
||||
function obj:OnTouchMoved(func) -- event for touches
|
||||
table.insert(self.ToFuncM,func)
|
||||
end
|
||||
if _GuiPro.TouchReady then -- my sneaky test
|
||||
print("Registred: "..tostring(obj))
|
||||
table.insert(_GuiPro.TouchRegister,obj)
|
||||
else
|
||||
print("Attempting to register: "..tostring(obj))
|
||||
_GuiPro.int:newJob(function() table.insert(_GuiPro.TouchRegister,obj) end) -- a sneaky way to ensure that your object gets registered eventually, even if you call the method before the touch patch was activated.
|
||||
end
|
||||
end
|
||||
@ -1,4 +0,0 @@
|
||||
function gui:AddDrawRuleB(rule)
|
||||
if not(self.DrawRulesB) then self.DrawRulesB={} end
|
||||
table.insert(self.DrawRulesB,rule)
|
||||
end
|
||||
@ -1,4 +0,0 @@
|
||||
function gui:AddDrawRuleE(rule)
|
||||
if not(self.DrawRulesE) then self.DrawRulesE={} end
|
||||
table.insert(self.DrawRulesE,rule)
|
||||
end
|
||||
@ -1,12 +0,0 @@
|
||||
function gui:draw()
|
||||
if _GuiPro.rotate~=0 then
|
||||
love.graphics.rotate(math.rad(_GuiPro.rotate))
|
||||
end
|
||||
if self.FormFactor:lower()=="rectangle" then
|
||||
self:drawR()
|
||||
elseif self.FormFactor:lower()=="circle" then
|
||||
self:drawC()
|
||||
else
|
||||
error("Unsupported FormFactor: "..self.FormFactor.."!")
|
||||
end
|
||||
end
|
||||
@ -1,89 +0,0 @@
|
||||
function gui:drawC()
|
||||
if love.mouse.isDown("l")==false and love.mouse.isDown("m")==false and love.mouse.isDown("r")==false then
|
||||
_GuiPro.DragItem={}
|
||||
_GuiPro.hasDrag=false
|
||||
end
|
||||
if self.Visible==true and self.VIS==true then
|
||||
local b=true
|
||||
for i,v in pairs(_GuiPro.Clips) do
|
||||
if self:isDescendant(v)==true then
|
||||
b=false
|
||||
end
|
||||
end
|
||||
if b then
|
||||
love.graphics.setStencilTest( )
|
||||
_GuiPro.HasStencel=false
|
||||
_GuiPro.StencelHolder=nil
|
||||
end
|
||||
local x,y,r,s=(self.offset.pos.x or 0)+self.Parent.x,(self.offset.pos.y or 0)+self.Parent.y,self.offset.size.x or 0,self.offset.size.y or 360
|
||||
if self.CC then
|
||||
x,y=x+r,y+r
|
||||
end
|
||||
self.x,self.y=x,y
|
||||
_GuiPro.circleStencilFunction = function()
|
||||
love.graphics.circle("fill",x,y,r,s)
|
||||
end
|
||||
if math.sqrt((love.mouse.getX()-x)^2+(love.mouse.getY()-y)^2)<=r and self:eventable() and self:Clickable() and self.Active==true then
|
||||
self.hovering=true
|
||||
if love.mouse.isDown("l") and _GuiPro.hasDrag==false then
|
||||
if string.find(self.Type, "Button") then
|
||||
love.graphics.setColor(self.Color[1]-10, self.Color[2]-10, self.Color[3]-10,self.Visibility)
|
||||
else
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.Visibility)
|
||||
end
|
||||
self.lclicked=true
|
||||
elseif love.mouse.isDown("r") and _GuiPro.hasDrag==false then
|
||||
if string.find(self.Type, "Button") then
|
||||
love.graphics.setColor(self.Color[1]-10, self.Color[2]-10, self.Color[3]-10,self.Visibility)
|
||||
else
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.Visibility)
|
||||
end
|
||||
self.rclicked=true
|
||||
elseif love.mouse.isDown("m") and _GuiPro.hasDrag==false then
|
||||
if string.find(self.Type, "Button") then
|
||||
love.graphics.setColor(self.Color[1]-10, self.Color[2]-10, self.Color[3]-10,self.Visibility)
|
||||
else
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.Visibility)
|
||||
end
|
||||
self.mclicked=true
|
||||
else
|
||||
if string.find(self.Type, "Button") and _GuiPro.hasDrag==false then
|
||||
love.graphics.setColor(self.Color[1]-5, self.Color[2]-5, self.Color[3]-5,self.Visibility)
|
||||
else
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.Visibility)
|
||||
end
|
||||
self.rclicked=false
|
||||
self.lclicked=false
|
||||
self.mclicked=false
|
||||
end
|
||||
else
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.Visibility)
|
||||
self.hovering=false
|
||||
self.rclicked=false
|
||||
self.lclicked=false
|
||||
self.mclicked=false
|
||||
end
|
||||
if self.ClipDescendants==true then
|
||||
_GuiPro.Clips[tostring(self)]=self
|
||||
_GuiPro.HasStencel=true
|
||||
_GuiPro.StencelHolder=self
|
||||
love.graphics.stencil(_GuiPro.circleStencilFunction)
|
||||
love.graphics.setStencilTest("notequal",0)
|
||||
end
|
||||
love.graphics.circle("fill",x,y,r,s)
|
||||
love.graphics.setColor(self.BorderColor[1], self.BorderColor[2], self.BorderColor[3],self.Visibility)
|
||||
for b=0,self.BorderSize-1 do
|
||||
love.graphics.circle("line",x,y,r+b,s)
|
||||
end
|
||||
if string.find(self.Type, "Text") then
|
||||
if self.text~=nil then
|
||||
if self.AutoScaleText then
|
||||
self.FontSize=math.floor(self.height/1.45833)
|
||||
end
|
||||
love.graphics.setColor(self.TextColor[1],self.TextColor[2],self.TextColor[3],self.TextVisibility)
|
||||
love.graphics.setFont(self.Font)
|
||||
love.graphics.printf(self.text, x-(r/2)+(self.XTween), y-(r/2)+self.Tween, r, self.TextFormat)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,131 +0,0 @@
|
||||
function gui:drawR()
|
||||
if love.mouse.isDown("l")==false and love.mouse.isDown("m")==false and love.mouse.isDown("r")==false then
|
||||
_GuiPro.DragItem={}
|
||||
_GuiPro.hasDrag=false
|
||||
end
|
||||
if self.Visible==true and self.VIS==true then
|
||||
local b=true
|
||||
for i,v in pairs(_GuiPro.Clips) do
|
||||
if self:isDescendant(v)==true then
|
||||
b=false
|
||||
end
|
||||
end
|
||||
if b==true then
|
||||
love.graphics.setStencilTest()
|
||||
love.graphics.setScissor()
|
||||
end
|
||||
self.x=(self.Parent.width*self.scale.pos.x)+self.offset.pos.x+self.Parent.x
|
||||
self.y=(self.Parent.height*self.scale.pos.y)+self.offset.pos.y+self.Parent.y
|
||||
self.width=(self.Parent.width*self.scale.size.x)+self.offset.size.x
|
||||
self.height=(self.Parent.height*self.scale.size.y)+self.offset.size.y
|
||||
if self.DrawRulesB then
|
||||
for dr=1,#self.DrawRulesB do
|
||||
self.DrawRulesB[dr](self)
|
||||
end
|
||||
end
|
||||
if (love.mouse.getX() > self.x and love.mouse.getX() < self.x+self.width and love.mouse.getY() > self.y and love.mouse.getY() < self.y+self.height and self:Clickable() and self:eventable()) or self:touchable("r") and self.Active==true then
|
||||
self.hovering=true
|
||||
if love.mouse.isDown("l") or self:touchable("r") and _GuiPro.hasDrag==false then
|
||||
if string.find(self.Type, "Button") then
|
||||
love.graphics.setColor(self.Color[1]-10, self.Color[2]-10, self.Color[3]-10,self.Visibility)
|
||||
else
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.Visibility)
|
||||
end
|
||||
self.lclicked=true
|
||||
elseif love.mouse.isDown("r") or self:touchable("r") and _GuiPro.hasDrag==false then
|
||||
if string.find(self.Type, "Button") then
|
||||
love.graphics.setColor(self.Color[1]-10, self.Color[2]-10, self.Color[3]-10,self.Visibility)
|
||||
else
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.Visibility)
|
||||
end
|
||||
self.rclicked=true
|
||||
elseif love.mouse.isDown("m") or self:touchable("r") and _GuiPro.hasDrag==false then
|
||||
if string.find(self.Type, "Button") then
|
||||
love.graphics.setColor(self.Color[1]-10, self.Color[2]-10, self.Color[3]-10,self.Visibility)
|
||||
else
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.Visibility)
|
||||
end
|
||||
self.mclicked=true
|
||||
else
|
||||
if string.find(self.Type, "Button") or self:touchable("r") and _GuiPro.hasDrag==false then
|
||||
love.graphics.setColor(self.Color[1]-5, self.Color[2]-5, self.Color[3]-5,self.Visibility)
|
||||
else
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.Visibility)
|
||||
end
|
||||
self.rclicked=false
|
||||
self.lclicked=false
|
||||
self.mclicked=false
|
||||
end
|
||||
else
|
||||
love.graphics.setColor(self.Color[1],self.Color[2],self.Color[3],self.Visibility)
|
||||
self.hovering=false
|
||||
self.rclicked=false
|
||||
self.lclicked=false
|
||||
self.mclicked=false
|
||||
end
|
||||
if self.ClipDescendants==true then
|
||||
_GuiPro.Clips[tostring(self)]=self
|
||||
love.graphics.setScissor(self.x, self.y, self.width, self.height)
|
||||
end
|
||||
if self:hasRoundness() then
|
||||
love.graphics.stencil(self.stfunc, "replace", 1)
|
||||
love.graphics.setStencilTest("greater", 0)
|
||||
end
|
||||
|
||||
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height,(self.rx or 1)*self.DPI,(self.ry or 1)*self.DPI,(self.segments or 1)*self.DPI)
|
||||
if string.find(self.Type, "Image") then
|
||||
self:ImageRule()
|
||||
end
|
||||
if self.Type=="Video" then
|
||||
self:VideoRule()
|
||||
end
|
||||
if self:hasRoundness() then
|
||||
love.graphics.setStencilTest()
|
||||
end
|
||||
love.graphics.setColor(self.BorderColor[1], self.BorderColor[2], self.BorderColor[3],self.Visibility)
|
||||
for b=0,self.BorderSize-1 do
|
||||
love.graphics.rectangle("line", self.x-(b/2), self.y-(b/2), self.width+b, self.height+b,(self.rx or 1)*self.DPI,(self.ry or 1)*self.DPI,(self.segments or 1)*self.DPI)
|
||||
end
|
||||
if string.find(self.Type, "Text") then
|
||||
if self.text~=nil then
|
||||
if self.AutoScaleText then
|
||||
self.FontSize=math.floor(self.height/1.45833)
|
||||
end
|
||||
love.graphics.setColor(self.TextColor[1],self.TextColor[2],self.TextColor[3],self.TextVisibility)
|
||||
if self.Font==_defaultfont then
|
||||
love.graphics.setFont(self.Font)
|
||||
love.graphics.printf(
|
||||
self.text,
|
||||
(self.x+2+(self.marginL or 0) or self.XTween)*self.DPI,
|
||||
(self.y+math.floor((self.FontHeight-self.FontSize)/2)+self.Tween)*self.DPI,
|
||||
(self.width+(0 or (self.marginR or 0)))*self.DPI,
|
||||
self.TextFormat,
|
||||
self.TextRotation)
|
||||
else
|
||||
if type(self.Font)=="string" then
|
||||
self.Font=love.graphics.newFont(self.Font,self.FontSize)
|
||||
self.FontHeight=self.Font:getHeight()
|
||||
else
|
||||
love.graphics.setFont(self.Font)
|
||||
end
|
||||
if type(self.FontSize)=="string" then
|
||||
self.FontSize=tonumber(self.FontSize)
|
||||
love.graphics.setNewFont(self.FontSize)
|
||||
end
|
||||
love.graphics.printf(
|
||||
self.text,
|
||||
(self.x+2+(self.marginL or 0) or self.XTween)*self.DPI,
|
||||
(self.y+math.floor((self.FontHeight-self.FontSize)/2)+self.Tween)*self.DPI,
|
||||
(self.width+(0 or (self.marginR or 0)))*self.DPI,
|
||||
self.TextFormat,
|
||||
self.TextRotation)
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.DrawRulesE then
|
||||
for dr=1,#self.DrawRulesE do
|
||||
self.DrawRulesE[dr](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,37 +0,0 @@
|
||||
function gui:newDropFrame(name,x, y, w, h, sx ,sy ,sw ,sh)
|
||||
x,y,w,h,sx,sy,sw,sh=filter(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("DropFrame",name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
c.WasBeingDragged=false
|
||||
c.IsBeingDragged=false
|
||||
c.Draggable=false
|
||||
c.funcD={}
|
||||
function c:GetDroppedItems()
|
||||
local t=self:getChildren()
|
||||
local tab={}
|
||||
for i=1,#t do
|
||||
if t[i].Type=="TextImageButtonFrameDrag" then
|
||||
table.insert(tab,t[i])
|
||||
end
|
||||
end
|
||||
return tab
|
||||
end
|
||||
function c:OnDropped(func)
|
||||
table.insert(self.funcD,func)
|
||||
end
|
||||
c:OnUpdate(function(self)
|
||||
if _GuiPro.DragItem then
|
||||
if _GuiPro.DragItem.Type=="TextImageButtonFrameDrag" and love.mouse.isDown(_GuiPro.DragItem.dragbut or "m")==false and self:IsHovering() then
|
||||
local t=_GuiPro.DragItem
|
||||
_GuiPro.DragItem={}
|
||||
for i=1,#t.funcD do
|
||||
t.funcD[i](self,t)
|
||||
end
|
||||
for i=1,#self.funcD do
|
||||
self.funcD[i](self,t)
|
||||
end
|
||||
_GuiPro.hasDrag=false
|
||||
end
|
||||
end
|
||||
end)
|
||||
return c
|
||||
end
|
||||
@ -1,8 +0,0 @@
|
||||
function gui:newFrame(name,x, y, w, h, sx ,sy ,sw ,sh)
|
||||
x,y,w,h,sx,sy,sw,sh=filter(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("Frame",name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
c.WasBeingDragged=false
|
||||
c.IsBeingDragged=false
|
||||
c.Draggable=false
|
||||
return c
|
||||
end
|
||||
@ -1,4 +0,0 @@
|
||||
function gui:newFullFrame(name)
|
||||
name=name or ""
|
||||
return self:newFrame(name,0,0,0,0,0,0,1,1)
|
||||
end
|
||||
@ -1,59 +0,0 @@
|
||||
function gui:newTabFrame(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=gui:newFrame(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
c.tabheight=20
|
||||
c.Holder=c:newFrame("Holder",0,c.tabheight,0,0,0,0,1,1)
|
||||
c.TabHolder=c:newFrame("TabHolder",0,0,0,c.tabheight,0,0,1)
|
||||
function c:setTabHeight(n)
|
||||
self.tabheight=n
|
||||
self.Holder:SetDualDim(0,-self.tabheight,0,0,0,0,1,1)
|
||||
end
|
||||
function c:addTab(name,colorT,colorH)
|
||||
if colorT and not(colorH) then
|
||||
colorH=colorT
|
||||
end
|
||||
local tab=self.TabHolder:newTextButton(name,name,0,0,0,0,0,0,0,1)
|
||||
tab.Tween=-3
|
||||
if colorT then
|
||||
tab.Color=colorT
|
||||
end
|
||||
local holder=self.Holder:newFrame(name,0,0,0,0,0,0,1,1)
|
||||
if colorH then
|
||||
holder.Color=colorH
|
||||
end
|
||||
tab.frame=holder
|
||||
tab:OnReleased(function(b,self)
|
||||
if b=="l" then
|
||||
local tt=self.Parent:getChildren()
|
||||
local th=self.Parent.Parent.Holder:getChildren()
|
||||
for i=1,#th do
|
||||
th[i].Visible=false
|
||||
end
|
||||
for i=1,#tt do
|
||||
tt[i].frame.Visible=false
|
||||
tt[i].BorderSize=1
|
||||
end
|
||||
self.BorderSize=0
|
||||
self.frame.Visible=true
|
||||
end
|
||||
end)
|
||||
local tt=self.TabHolder:getChildren()
|
||||
for i=1,#tt do
|
||||
tt[i].frame.Visible=false
|
||||
tt[i].BorderSize=1
|
||||
end
|
||||
tab.frame.Visible=true
|
||||
tab.BorderSize=0
|
||||
return tab,holder
|
||||
end
|
||||
c:OnUpdate(function(self)
|
||||
local th=self.TabHolder:getChildren()
|
||||
local l=self.width/#th
|
||||
for i=1,#th do
|
||||
th[i]:SetDualDim(l*(i-1),0,l)
|
||||
end
|
||||
if #th==0 then
|
||||
self:Destroy()
|
||||
end
|
||||
end)
|
||||
return c
|
||||
end
|
||||
@ -1,13 +0,0 @@
|
||||
function gui:SetImage(i)
|
||||
if type(i)=="string" or tostring(i):find("ImageData") then
|
||||
self.Image=love.graphics.newImage(i)
|
||||
else
|
||||
self.Image=i
|
||||
end
|
||||
if self.Image then
|
||||
self.ImageHeigth=self.Image:getHeight()
|
||||
self.ImageWidth=self.Image:getWidth()
|
||||
self.Quad=love.graphics.newQuad(0,0,self.width,self.height,self.ImageWidth,self.ImageHeigth)
|
||||
end
|
||||
return self.ImageWidth,self.ImageHeigth
|
||||
end
|
||||
@ -1,5 +0,0 @@
|
||||
function gui:UpdateImage()
|
||||
self.ImageHeigth=self.Image:getHeight()
|
||||
self.ImageWidth=self.Image:getWidth()
|
||||
self.Quad=love.graphics.newQuad(0,0,self.width,self.height,self.ImageWidth,self.ImageHeigth)
|
||||
end
|
||||
@ -1,20 +0,0 @@
|
||||
function gui:getTile(i,x,y,w,h)-- returns imagedata
|
||||
if type(i)=="string" then
|
||||
i=love.graphics.newImage(i)
|
||||
elseif type(i)=="userdata" then
|
||||
-- do nothing
|
||||
elseif string.find(self.Type,"Image",1,true) then
|
||||
local i,x,y,w,h=self.Image,i,x,y,w
|
||||
else
|
||||
error("getTile invalid args!!! Usage: ImageElement:getTile(x,y,w,h) or gui:getTile(imagedata,x,y,w,h)")
|
||||
end
|
||||
local iw,ih=i:getDimensions()
|
||||
local id,_id=i:getData(),love.image.newImageData(w,h)
|
||||
for _x=x,w+x-1 do
|
||||
for _y=y,h+y-1 do
|
||||
--
|
||||
_id:setPixel(_x-x,_y-y,id:getPixel(_x,_y))
|
||||
end
|
||||
end
|
||||
return love.graphics.newImage(_id)
|
||||
end
|
||||
@ -1,57 +0,0 @@
|
||||
function gui:newAnim(file,delay, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local x,y,w,h,sx,sy,sw,sh=filter(file, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("ImageAnimation",file, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
c.Visibility=0
|
||||
c.ImageVisibility=1
|
||||
c.delay=delay or .05
|
||||
c.files={}
|
||||
c.AnimStart={}
|
||||
c.AnimEnd={}
|
||||
local _files=alphanumsort(love.filesystem.getDirectoryItems(file))
|
||||
for i=1,#_files do
|
||||
if string.sub(_files[i],-1,-1)~="b" then
|
||||
table.insert(c.files,love.graphics.newImage(file.."/".._files[i]))
|
||||
end
|
||||
end
|
||||
c.step=multi:newTStep(1,#c.files,1,c.delay)
|
||||
c.step.parent=c
|
||||
c.rotation=0
|
||||
c.step:OnStart(function(step)
|
||||
for i=1,#step.parent.AnimStart do
|
||||
step.parent.AnimStart[i](step.parent)
|
||||
end
|
||||
end)
|
||||
c.step:OnStep(function(pos,step)
|
||||
step.parent:SetImage(step.parent.files[pos])
|
||||
end)
|
||||
c.step:OnEnd(function(step)
|
||||
for i=1,#step.parent.AnimEnd do
|
||||
step.parent.AnimEnd[i](step.parent)
|
||||
end
|
||||
end)
|
||||
function c:OnAnimStart(func)
|
||||
table.insert(self.AnimStart,func)
|
||||
end
|
||||
function c:OnAnimEnd(func)
|
||||
table.insert(self.AnimEnd,func)
|
||||
end
|
||||
function c:Pause()
|
||||
self.step:Pause()
|
||||
end
|
||||
function c:Resume()
|
||||
self.step:Resume()
|
||||
end
|
||||
function c:Reset()
|
||||
self.step.pos=1
|
||||
end
|
||||
function c:getFrames()
|
||||
return #self.files
|
||||
end
|
||||
function c:getFrame()
|
||||
return self.step.pos
|
||||
end
|
||||
function c:setFrame(n)
|
||||
return self:SetImage(self.files[n])
|
||||
end
|
||||
return c
|
||||
end
|
||||
@ -1,52 +0,0 @@
|
||||
function gui:newAnimFromData(data,delay, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
x,y,w,h,sx,sy,sw,sh=filter(x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("ImageAnimation","FromFile", x, y, w, h, sx ,sy ,sw ,sh)
|
||||
c.Visibility=0
|
||||
c.ImageVisibility=1
|
||||
c.delay=delay or .05
|
||||
c.files=data
|
||||
c.AnimStart={}
|
||||
c.AnimEnd={}
|
||||
c:SetImage(c.files[1])
|
||||
c.step=multi:newTStep(1,#c.files,1,c.delay)
|
||||
c.step.parent=c
|
||||
c.rotation=0
|
||||
c.step:OnStart(function(step)
|
||||
for i=1,#step.parent.AnimStart do
|
||||
step.parent.AnimStart[i](step.parent)
|
||||
end
|
||||
end)
|
||||
c.step:OnStep(function(pos,step)
|
||||
step.parent:SetImage(step.parent.files[pos])
|
||||
end)
|
||||
c.step:OnEnd(function(step)
|
||||
for i=1,#step.parent.AnimEnd do
|
||||
step.parent.AnimEnd[i](step.parent)
|
||||
end
|
||||
end)
|
||||
function c:OnAnimStart(func)
|
||||
table.insert(self.AnimStart,func)
|
||||
end
|
||||
function c:OnAnimEnd(func)
|
||||
table.insert(self.AnimEnd,func)
|
||||
end
|
||||
function c:Pause()
|
||||
self.step:Pause()
|
||||
end
|
||||
function c:Resume()
|
||||
self.step:Resume()
|
||||
end
|
||||
function c:Reset()
|
||||
self.step.pos=1
|
||||
end
|
||||
function c:getFrames()
|
||||
return #self.files
|
||||
end
|
||||
function c:getFrame()
|
||||
return self.step.pos
|
||||
end
|
||||
function c:setFrame(n)
|
||||
return self:SetImage(self.files[n])
|
||||
end
|
||||
return c
|
||||
end
|
||||
@ -1,59 +0,0 @@
|
||||
function gui:newAnimFromTiles(file,xd,yd,delay, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
x,y,w,h,sx,sy,sw,sh=filter(file, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("ImageAnimation",file, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local im=love.graphics.newImage(file)
|
||||
local _x,_y=im:getDimensions()
|
||||
c.Visibility=0
|
||||
c.ImageVisibility=1
|
||||
c.delay=delay or .05
|
||||
c.files={}
|
||||
c.AnimStart={}
|
||||
c.AnimEnd={}
|
||||
for i=0,_y/yd-1 do
|
||||
for j=0,_x/xd-1 do
|
||||
table.insert(c.files,gui:getTile(im,j*xd,i*yd,xd,yd))
|
||||
end
|
||||
end
|
||||
c:SetImage(c.files[1])
|
||||
c.step=multi:newTStep(1,#c.files,1,c.delay)
|
||||
c.step.parent=c
|
||||
c.rotation=0
|
||||
c.step:OnStart(function(step)
|
||||
for i=1,#step.parent.AnimStart do
|
||||
step.parent.AnimStart[i](step.parent)
|
||||
end
|
||||
end)
|
||||
c.step:OnStep(function(pos,step)
|
||||
step.parent:SetImage(step.parent.files[pos])
|
||||
end)
|
||||
c.step:OnEnd(function(step)
|
||||
for i=1,#step.parent.AnimEnd do
|
||||
step.parent.AnimEnd[i](step.parent)
|
||||
end
|
||||
end)
|
||||
function c:OnAnimStart(func)
|
||||
table.insert(self.AnimStart,func)
|
||||
end
|
||||
function c:OnAnimEnd(func)
|
||||
table.insert(self.AnimEnd,func)
|
||||
end
|
||||
function c:Pause()
|
||||
self.step:Pause()
|
||||
end
|
||||
function c:Resume()
|
||||
self.step:Resume()
|
||||
end
|
||||
function c:Reset()
|
||||
self.step.pos=1
|
||||
end
|
||||
function c:getFrames()
|
||||
return #self.files
|
||||
end
|
||||
function c:getFrame()
|
||||
return self.step.pos
|
||||
end
|
||||
function c:setFrame(n)
|
||||
return self:SetImage(self.files[n])
|
||||
end
|
||||
return c
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:newFullImageLabel(i,name)
|
||||
return self:newImageLabel(i,name,0,0,0,0,0,0,1,1)
|
||||
end
|
||||
@ -1,25 +0,0 @@
|
||||
function gui:newImageButton(i,name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
x,y,w,h,sx,sy,sw,sh=filter(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("ImageButton",name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
if type(i)=="string" or type(i):find("ImageData") then
|
||||
c.Image=love.graphics.newImage(i)
|
||||
else
|
||||
c.Image=i
|
||||
end
|
||||
c.Visibility=0
|
||||
c.ImageVisibility=1
|
||||
c.rotation=0
|
||||
if c.Image~=nil then
|
||||
c.ImageHeigth=c.Image:getHeight()
|
||||
c.ImageHeight=c.Image:getHeight()
|
||||
c.ImageWidth=c.Image:getWidth()
|
||||
c.Quad=love.graphics.newQuad(0,0,w,h,c.ImageWidth,c.ImageHeigth)
|
||||
end
|
||||
c:OnEnter(function()
|
||||
love.mouse.setCursor(_GuiPro.CursorH)
|
||||
end)
|
||||
c:OnExit(function()
|
||||
love.mouse.setCursor(_GuiPro.CursorN)
|
||||
end)
|
||||
return c
|
||||
end
|
||||
@ -1,18 +0,0 @@
|
||||
function gui:newImageLabel(i,name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
x,y,w,h,sx,sy,sw,sh=filter(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("ImageLabel",name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
if type(i)=="string" or type(i):find("ImageData") then
|
||||
c.Image=love.graphics.newImage(i)
|
||||
else
|
||||
c.Image=i
|
||||
end
|
||||
c.Visibility=0
|
||||
c.ImageVisibility=1
|
||||
c.rotation=0
|
||||
if c.Image then
|
||||
c.ImageHeigth=c.Image:getHeight()
|
||||
c.ImageWidth=c.Image:getWidth()
|
||||
c.Quad=love.graphics.newQuad(0,0,w,h,c.ImageWidth,c.ImageHeigth)
|
||||
end
|
||||
return c
|
||||
end
|
||||
@ -1,62 +0,0 @@
|
||||
function gui:newVideo(name,i,x,y,w,h,sx,sy,sw,sh)
|
||||
x,y,w,h,sx,sy,sw,sh=filter(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("Video",name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
if type(i)=="string" then
|
||||
c.Video=love.graphics.newVideo(i)
|
||||
else
|
||||
c.Video=i
|
||||
end
|
||||
c.Visibility=0
|
||||
c.VideoVisibility=1
|
||||
c.rotation=0
|
||||
if c.Video~=nil then
|
||||
c.VideoHeigth=c.Video:getHeight()
|
||||
c.VideoWidth=c.Video:getWidth()
|
||||
c.Quad=love.graphics.newQuad(0,0,w,h,c.VideoWidth,c.VideoHeigth)
|
||||
end
|
||||
c.funcV={}
|
||||
function c:Play()
|
||||
self.handStart=true
|
||||
self.Video:play()
|
||||
end
|
||||
function c:Pause()
|
||||
self.Video:pause()
|
||||
end
|
||||
c.Resume=c.Play
|
||||
function c:Stop()
|
||||
self.handStart=false
|
||||
self:Pause()
|
||||
self:Rewind()
|
||||
for i=1,# self.funcV do
|
||||
self.funcV[i](self)
|
||||
end
|
||||
end
|
||||
function c:OnVideoStopped(func)
|
||||
table.insert(self.funcV,func)
|
||||
end
|
||||
function c:Rewind()
|
||||
self.Video:rewind()
|
||||
end
|
||||
function c:Restart()
|
||||
self:Rewind()
|
||||
self:Play()
|
||||
end
|
||||
function c:Seek(o)
|
||||
self.Video:seek(o)
|
||||
end
|
||||
function c:Tell()
|
||||
self.Video:tell()
|
||||
end
|
||||
function c:SetFilter(min, mag, anisotropy)
|
||||
self.Video:setFilter(min, mag, anisotropy)
|
||||
end
|
||||
function c:IsPlaying()
|
||||
return self.Video:isPlaying()
|
||||
end
|
||||
c:OnUpdate(function(self)
|
||||
if self.Video:isPlaying()==false and self.handStart == true then
|
||||
self:Stop()
|
||||
end
|
||||
end)
|
||||
return c
|
||||
end
|
||||
@ -1,51 +0,0 @@
|
||||
function gui:newDragItem(t,i,name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
x,y,w,h,sx,sy,sw,sh=filter(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("TextImageButtonFrameDrag",name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
c.WasBeingDragged=false
|
||||
c.IsBeingDragged=false
|
||||
c.Draggable=true
|
||||
c.funcD={}
|
||||
if type(i)=="string" then
|
||||
c.Image=love.graphics.newImage(i)
|
||||
c.ImageVisibility=1
|
||||
c.ImageHeigth=c.Image:getHeight()
|
||||
c.ImageWidth=c.Image:getWidth()
|
||||
c.Quad=love.graphics.newQuad(0,0,w,h,c.ImageWidth,c.ImageHeigth)
|
||||
elseif type(i)=="image" then
|
||||
c.Image=i
|
||||
c.ImageVisibility=1
|
||||
c.ImageHeigth=c.Image:getHeight()
|
||||
c.ImageWidth=c.Image:getWidth()
|
||||
c.Quad=love.graphics.newQuad(0,0,w,h,c.ImageWidth,c.ImageHeigth)
|
||||
end
|
||||
c:OnDragStart(function(self,x,y)
|
||||
if _GuiPro.hasDrag==false then
|
||||
self:setParent(_GuiPro)
|
||||
self:SetDualDim(x,y)
|
||||
self:TopStack()
|
||||
end
|
||||
end)
|
||||
c.rotation=0
|
||||
c.Tween=0
|
||||
c.XTween=0
|
||||
c.text = t
|
||||
c.AutoScaleText=false
|
||||
c.FontHeight=_defaultfont:getHeight()
|
||||
c.Font=_defaultfont
|
||||
c.FontSize=15
|
||||
c.TextFormat="center"
|
||||
c.TextVisibility=1
|
||||
c.TextColor = {0, 0, 0}
|
||||
function c:OnDropped(func)
|
||||
table.insert(self.funcD,func)
|
||||
end
|
||||
c:OnUpdate(function(self)
|
||||
if love.mouse.isDown("m" or self.dragbut)==false and self==_GuiPro.DragItem and self.hovering==false then
|
||||
_GuiPro.DragItem={}
|
||||
for i=1,#self.func7 do
|
||||
self.func7[i](self,(love.mouse.getX())-self.width/2,(love.mouse.getY())-self.height/2)
|
||||
end
|
||||
end
|
||||
end)
|
||||
return c
|
||||
end
|
||||
@ -1,28 +0,0 @@
|
||||
function gui:newItem(t,i,name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
x,y,w,h,sx,sy,sw,sh=filter(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("TextImageButtonFrame",name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
if type(i)=="string" then
|
||||
c.Image=love.graphics.newImage(i)
|
||||
else
|
||||
c.Image=i
|
||||
end
|
||||
c.rotation=0
|
||||
c.ImageVisibility=1
|
||||
c.Draggable=false
|
||||
if c.Image~=nil then
|
||||
c.ImageHeigth=c.Image:getHeight()
|
||||
c.ImageWidth=c.Image:getWidth()
|
||||
c.Quad=love.graphics.newQuad(0,0,w,h,c.ImageWidth,c.ImageHeigth)
|
||||
end
|
||||
c.Tween=0
|
||||
c.XTween=0
|
||||
c.text = t
|
||||
c.AutoScaleText=false
|
||||
c.FontHeight=_defaultfont:getHeight()
|
||||
c.Font=_defaultfont
|
||||
c.FontSize=15
|
||||
c.TextFormat="center"
|
||||
c.TextVisibility=1 -- 0=invisible,1=solid (self.TextVisibility*254+1)
|
||||
c.TextColor = {0, 0, 0}
|
||||
return c
|
||||
end
|
||||
@ -1,45 +0,0 @@
|
||||
function gui:AdvTextBox(txt,x,y,w,h,sx,sy,sw,sh)
|
||||
name="AdvTextBox"
|
||||
x,y,w,h,sx,sy,sw,sh=filter(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("AdvTextBoxFrame",name, x, y, w, 30, sx ,sy ,sw ,sh)
|
||||
c.Draggable=true
|
||||
c.dragbut="r"
|
||||
c.BorderSize=0
|
||||
c:ApplyGradient{Color.Blue,Color.sexy_purple}
|
||||
c:newTextLabel(txt,"Holder",0,0,0,h-30,0,1,1,0).Color=Color.sexy_purple
|
||||
c.funcO={}
|
||||
c.funcX={}
|
||||
c:OnDragStart(function(self)
|
||||
self:TopStack()
|
||||
end)
|
||||
--local temp = c:newTextButton("X","Close",-25,5,20,20,1)
|
||||
--temp.Tween=-5
|
||||
--temp.XTween=-2
|
||||
--temp:OnReleased(function(b,self) for i=1,#self.Parent.funcX do self.Parent.funcX[i](self.Parent) end end)
|
||||
--temp.Color=Color.Red
|
||||
c.tLink=c:newTextBox("puttext","TextBox",5,h-95,-40,30,0,1,1,1)
|
||||
c.tLink.Color=Color.light_gray
|
||||
c.tLink.ClearOnFocus=true
|
||||
c.tLink:OnFocus(function(self) self.ClearOnFocus=false end)
|
||||
local temp=c:newTextButton("OK","Ok",-35,h-65,30,30,1,1)
|
||||
temp:OnReleased(function(b,self) for i=1,#self.Parent.funcO do self.Parent.funcO[i](self.Parent,self.Parent.tLink.text) end end)
|
||||
temp.Color=Color.Green
|
||||
temp.XTween=-2
|
||||
local temp=c:newTextButton("X","Cancel",-35,h-95,30,30,1,1)
|
||||
temp:OnReleased(function(b,self) for i=1,#self.Parent.funcX do self.Parent.funcX[i](self.Parent,self.Parent.tLink.text) end end)
|
||||
temp.Color=Color.Red
|
||||
temp.XTween=-2
|
||||
function c:Close()
|
||||
self.Visible=false
|
||||
end
|
||||
function c:Open()
|
||||
self.Visible=true
|
||||
end
|
||||
function c:OnOk(func)
|
||||
table.insert(self.funcO,func)
|
||||
end
|
||||
function c:OnX(func)
|
||||
table.insert(self.funcX,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
@ -1,36 +0,0 @@
|
||||
function _GuiPro.gradient(colors)
|
||||
local direction = colors.direction or "horizontal"
|
||||
colors.direction=nil
|
||||
trans = colors.trans or 1
|
||||
trans=math.floor(trans)
|
||||
if direction == "horizontal" then
|
||||
direction = true
|
||||
elseif direction == "vertical" then
|
||||
direction = false
|
||||
else
|
||||
error("Invalid direction '" .. tostring(direction) "' for gradient. Horizontal or vertical expected.")
|
||||
end
|
||||
local result = love.image.newImageData(direction and 1 or #colors, direction and #colors or 1,"rgba32f")
|
||||
for __i, color in ipairs(colors) do
|
||||
local x, y
|
||||
if direction then
|
||||
x, y = 0, __i - 1
|
||||
else
|
||||
x, y = __i - 1, 0
|
||||
end
|
||||
result:setPixel(x, y, color[1], color[2], color[3], trans)
|
||||
end
|
||||
result = love.graphics.newImage(result)
|
||||
result:setFilter('linear', 'linear')
|
||||
return result
|
||||
end
|
||||
function _GuiPro.drawinrect(img, x, y, w, h, r, ox, oy, kx, ky)
|
||||
love.graphics.draw(img, x, y, r, w / img:getWidth(), h / img:getHeight(), ox, oy, kx, ky)
|
||||
end
|
||||
function gui:ApplyGradient(rules)
|
||||
self.Image=nil
|
||||
self.Type=self.Type.."w/GradImage"
|
||||
self.rotation=0
|
||||
self.ImageVisibility=rules.visibility or 1
|
||||
self:SetImage(_GuiPro.gradient(rules))
|
||||
end
|
||||
@ -1,10 +0,0 @@
|
||||
function gui:BottomStack()
|
||||
childs=self.Parent:getChildren()
|
||||
for i=1,#childs do
|
||||
if childs[i]==self then
|
||||
table.remove(self.Parent.Children,i)
|
||||
table.insert(self.Parent.Children,1,self)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,4 +0,0 @@
|
||||
function gui:center()
|
||||
self:centerX()
|
||||
self:centerY()
|
||||
end
|
||||
@ -1,10 +0,0 @@
|
||||
function gui:Destroy()
|
||||
check=self.Parent:getChildren()
|
||||
local cc=0
|
||||
for cc=1,#check do
|
||||
if check[cc]==self then
|
||||
table.remove(self.Parent.Children,cc)
|
||||
end
|
||||
end
|
||||
self.Destroyed = true
|
||||
end
|
||||
@ -1,25 +0,0 @@
|
||||
function gui:GetAllChildren()
|
||||
local Stuff = {}
|
||||
function Seek(Items)
|
||||
for i=1,#Items do
|
||||
if Items[i].Visible==true then
|
||||
table.insert(Stuff,Items[i])
|
||||
local NItems = Items[i]:getChildren()
|
||||
if NItems ~= nil then
|
||||
Seek(NItems)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local Objs = self:getChildren()
|
||||
for i=1,#Objs do
|
||||
if Objs[i].Visible==true then
|
||||
table.insert(Stuff,Objs[i])
|
||||
local Items = Objs[i]:getChildren()
|
||||
if Items ~= nil then
|
||||
Seek(Items)
|
||||
end
|
||||
end
|
||||
end
|
||||
return Stuff
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:GetChild(name)
|
||||
return self.Children[name] or self
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function InGrid(i,x,y,s)
|
||||
return math.floor((i-1)/x)*s,(i-1)*s-(math.floor((i-1)/y)*(s*x))
|
||||
end
|
||||
@ -1,5 +0,0 @@
|
||||
function InGridX(i,w,h,xs,ys)
|
||||
local xc,yc=math.floor(w/xs),math.floor(h/ys)
|
||||
local xi,yi=(i-1)%xc,math.floor((i-1)/xc)
|
||||
return xi*xs,yi*ys
|
||||
end
|
||||
@ -1,5 +0,0 @@
|
||||
function InGridY(i,w,h,xs,ys)
|
||||
local xc,yc=math.floor(w/xs),math.floor(h/ys)
|
||||
local xi,yi=math.floor((i-1)/yc),(i-1)%yc
|
||||
return xi*xs,yi*ys
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:IsHovering()
|
||||
return (love.mouse.getX() > self.x and love.mouse.getX() < self.x+self.width and love.mouse.getY() > self.y and love.mouse.getY() < self.y+self.height)
|
||||
end
|
||||
@ -1,4 +0,0 @@
|
||||
function gui:Move(x,y)
|
||||
self.offset.pos.x=self.offset.pos.x+x
|
||||
self.offset.pos.y=self.offset.pos.y+y
|
||||
end
|
||||
@ -1,46 +0,0 @@
|
||||
function gui:SetDualDim(x, y, w, h, sx ,sy ,sw ,sh)
|
||||
if _GuiPro.DPI_ENABLED then
|
||||
if x then
|
||||
x=self.DPI*x
|
||||
end
|
||||
if y then
|
||||
y=self.DPI*y
|
||||
end
|
||||
if w then
|
||||
w=self.DPI*w
|
||||
end
|
||||
if h then
|
||||
h=self.DPI*h
|
||||
end
|
||||
end
|
||||
if sx then
|
||||
self.scale.pos.x=sx
|
||||
end
|
||||
if sy then
|
||||
self.scale.pos.y=sy
|
||||
end
|
||||
if x then
|
||||
self.offset.pos.x=x
|
||||
end
|
||||
if y then
|
||||
self.offset.pos.y=y
|
||||
end
|
||||
if sw then
|
||||
self.scale.size.x=sw
|
||||
end
|
||||
if sh then
|
||||
self.scale.size.y=sh
|
||||
end
|
||||
if w then
|
||||
self.offset.size.x=w
|
||||
end
|
||||
if h then
|
||||
self.offset.size.y=h
|
||||
end
|
||||
if self.Image then
|
||||
self:SetImage(self.Image)
|
||||
end
|
||||
end
|
||||
function gui:setDualDim(...)
|
||||
self:SetDualDim(...)
|
||||
end
|
||||
@ -1,5 +0,0 @@
|
||||
_GuiPro.CursorN=love.mouse.getSystemCursor("arrow")
|
||||
_GuiPro.CursorH=love.mouse.getSystemCursor("hand")
|
||||
function gui:SetHand(img,x,y)
|
||||
_GuiPro.CursorN=love.mouse.newCursor(img,x,y)
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:SetHover(img,x,y)
|
||||
_GuiPro.CursorH=love.mouse.newCursor(img,x,y)
|
||||
end
|
||||
@ -1,4 +0,0 @@
|
||||
function gui:SetName(name)
|
||||
self.Parent.Children[name]=self
|
||||
self.Name=name
|
||||
end
|
||||
@ -1,10 +0,0 @@
|
||||
function gui:TopStack()
|
||||
childs=self.Parent:getChildren()
|
||||
for i=1,#childs do
|
||||
if childs[i]==self then
|
||||
table.remove(self.Parent.Children,i)
|
||||
table.insert(self.Parent.Children,self)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:addDominance()
|
||||
_GuiPro.TopHovered=self
|
||||
end
|
||||
@ -1,6 +0,0 @@
|
||||
function gui:addHotKey(key)
|
||||
local temp=self:newFrame(0,0,0,0)
|
||||
temp.Visible=false
|
||||
temp:setHotKey(key)
|
||||
return temp
|
||||
end
|
||||
@ -1,7 +0,0 @@
|
||||
function alphanumsort(o)
|
||||
local function padnum(d) local dec, n = string.match(d, "(%.?)0*(.+)")
|
||||
return #dec > 0 and ("%.12f"):format(d) or ("%s%03d%s"):format(dec, #n, n)
|
||||
end
|
||||
table.sort(o, function(a,b) return tostring(a):gsub("%.?%d+",padnum)..("%3d"):format(#b)< tostring(b):gsub("%.?%d+",padnum)..("%3d"):format(#a) end)
|
||||
return o
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:anchorRight(n)
|
||||
self:SetDualDim(-(self.width+n),nil,nil,nil,1)
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:centerX()
|
||||
self:SetDualDim(-(self.width/2),nil,nil,nil,.5)
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:centerY()
|
||||
self:SetDualDim(nil,-(self.height/2),nil,nil,nil,.5)
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:disrespectHierarchy()
|
||||
_GuiPro.Hierarchy=false
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:getChildren()
|
||||
return self.Children
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:getColor(cindex)
|
||||
return Color[cindex]
|
||||
end
|
||||
@ -1,12 +0,0 @@
|
||||
function gui:getFullSize()
|
||||
local maxx,maxy=-math.huge,-math.huge
|
||||
local temp = self:GetAllChildren()
|
||||
for i=1,#temp do
|
||||
if temp[i].width+temp[i].offset.pos.x>maxx then
|
||||
maxx=temp[i].width+temp[i].offset.pos.x
|
||||
elseif temp[i].height+temp[i].offset.pos.y>maxy then
|
||||
maxy=temp[i].height+temp[i].offset.pos.y
|
||||
end
|
||||
end
|
||||
return maxx,maxy
|
||||
end
|
||||
@ -1,5 +0,0 @@
|
||||
function gui:getHighest()
|
||||
if self.Children[#self.Children]~=nil then
|
||||
return self.Children[#self.Children]
|
||||
end
|
||||
end
|
||||
@ -1,5 +0,0 @@
|
||||
function gui:getLowest()
|
||||
if self.Children[1]~=nil then
|
||||
return self.Children[1]
|
||||
end
|
||||
end
|
||||
@ -1,9 +0,0 @@
|
||||
function gui:isDescendant(obj)
|
||||
local things=obj:GetAllChildren()
|
||||
for i=1,#things do
|
||||
if things[i]==self then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:isHighest()
|
||||
return (self==self.Parent:getHighest())
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
function gui:isLowest()
|
||||
return (self==self.Parent:getLowest())
|
||||
end
|
||||
@ -1,6 +0,0 @@
|
||||
function gui.massMutate(t,...)
|
||||
local mut={...}
|
||||
for i=1,#mut do
|
||||
mut[i]:Mutate(t)
|
||||
end
|
||||
end
|
||||
@ -1,41 +0,0 @@
|
||||
if love.filesystem.getInfo("CheckBoxes.png") then
|
||||
_GuiPro.UC=gui:getTile("CheckBoxes.png",0,0,16,16)
|
||||
_GuiPro.C=gui:getTile("CheckBoxes.png",16,0,16,16)
|
||||
_GuiPro.UCH=gui:getTile("CheckBoxes.png",0,16,16,16)
|
||||
_GuiPro.CH=gui:getTile("CheckBoxes.png",16,16,16,16)
|
||||
end
|
||||
function gui:newCheckBox(name,x,y)
|
||||
if not(_GuiPro.UC) then error("CheckBoxes.png not found! Cannot currently use checkbox without the data") end
|
||||
if type(name)~="String" then
|
||||
x,y,name=name,x,"CheckBox"
|
||||
end
|
||||
local c=self:newImageLabel(_GuiPro.UC,name, x, y, 16,16)
|
||||
c.Visibility=0
|
||||
c.check=false
|
||||
c:OnEnter(function(self)
|
||||
if self.check then
|
||||
self:SetImage(_GuiPro.CH)
|
||||
else
|
||||
self:SetImage(_GuiPro.UCH)
|
||||
end
|
||||
end)
|
||||
function c:isChecked()
|
||||
return self.check
|
||||
end
|
||||
c:OnExit(function(self)
|
||||
if self.check then
|
||||
self:SetImage(_GuiPro.C)
|
||||
else
|
||||
self:SetImage(_GuiPro.UC)
|
||||
end
|
||||
end)
|
||||
c:OnReleased(function(b,self)
|
||||
self.check=not(self.check)
|
||||
if self.check then
|
||||
self:SetImage(_GuiPro.CH)
|
||||
else
|
||||
self:SetImage(_GuiPro.UCH)
|
||||
end
|
||||
end)
|
||||
return c
|
||||
end
|
||||
@ -1,36 +0,0 @@
|
||||
function gui:newMessageBox(txt,x,y,w,h,sx,sy,sw,sh)
|
||||
name="MessageBox"
|
||||
x,y,w,h,sx,sy,sw,sh=filter(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("MessageBoxFrame",name, x, y, w, 30, sx ,sy ,sw ,sh)
|
||||
c.Draggable=true
|
||||
c.dragbut="r"
|
||||
c:ApplyGradient{Color.Blue,Color.sexy_purple}
|
||||
c.BorderSize=0
|
||||
c:newTextLabel(txt,"Holder",0,0,0,h-30,0,1,1,0).Color=Color.sexy_purple
|
||||
c.funcO={}
|
||||
c.funcX={}
|
||||
c:OnDragStart(function(self)
|
||||
self:TopStack()
|
||||
end)
|
||||
local temp = c:newTextButton("X","Close",-25,5,20,20,1)
|
||||
temp.Tween=-5
|
||||
temp.XTween=-2
|
||||
temp:OnReleased(function(b,self) for i=1,#self.Parent.funcX do self.Parent.funcX[i](self.Parent) end end)
|
||||
temp.Color=Color.Red
|
||||
local temp=c:newTextButton("OK","Ok",0,h-65,0,30,.25,1,.5)
|
||||
temp:OnReleased(function(b,self) for i=1,#self.Parent.funcO do self.Parent.funcO[i](self.Parent) end end)
|
||||
temp.Color=Color.Green
|
||||
function c:Close()
|
||||
self.Visible=false
|
||||
end
|
||||
function c:Open()
|
||||
self.Visible=true
|
||||
end
|
||||
function c:OnOk(func)
|
||||
table.insert(self.funcO,func)
|
||||
end
|
||||
function c:OnX(func)
|
||||
table.insert(self.funcX,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
@ -1,49 +0,0 @@
|
||||
function gui:newPart(x, y,w ,h , sx ,sy ,sw ,sh)
|
||||
local c = {}
|
||||
setmetatable(c, gui)
|
||||
if self==gui then
|
||||
c.Parent=_GuiPro
|
||||
else
|
||||
c.Parent=self
|
||||
end
|
||||
c.funcs={}
|
||||
c.funcs2={}
|
||||
c.funcs3={}
|
||||
c.funcs4={}
|
||||
c.funcs5={}
|
||||
c.func6={}
|
||||
c.func7={}
|
||||
c.func8={}
|
||||
c.func9={}
|
||||
c.func10={}
|
||||
c.form="rectangle"
|
||||
c.Color = {255, 255, 255}
|
||||
c.scale={}
|
||||
c.scale.size={}
|
||||
c.scale.size.x=sw or 0
|
||||
c.scale.size.y=sh or 0
|
||||
c.offset={}
|
||||
c.offset.size={}
|
||||
c.offset.size.x=w or 0
|
||||
c.offset.size.y=h or 0
|
||||
c.scale.pos={}
|
||||
c.scale.pos.x=sx or 0
|
||||
c.scale.pos.y=sy or 0
|
||||
c.offset.pos={}
|
||||
c.offset.pos.x=x or 0
|
||||
c.offset.pos.y=y or 0
|
||||
c.VIS=true
|
||||
c.Visible=true
|
||||
c.Visibility=1
|
||||
c.BorderColor={0,0,0}
|
||||
c.BorderSize=0
|
||||
c.Type="Part"
|
||||
c.Name="GuiPart"
|
||||
_GuiPro.count=_GuiPro.count+1
|
||||
c.x=(c.Parent.width*c.scale.pos.x)+c.offset.pos.x+c.Parent.x
|
||||
c.y=(c.Parent.height*c.scale.pos.y)+c.offset.pos.y+c.Parent.y
|
||||
c.width=(c.Parent.width*c.scale.size.x)+c.offset.size.x
|
||||
c.height=(c.Parent.height*c.scale.size.y)+c.offset.size.y
|
||||
table.insert(c.Parent.Children,c)
|
||||
return c
|
||||
end
|
||||
@ -1,45 +0,0 @@
|
||||
function gui:newProgressBar(txt,x,y,w,h,sx,sy,sw,sh)
|
||||
name="newProgressBar"
|
||||
x,y,w,h,sx,sy,sw,sh=filter(name, x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local c=self:newBase("newProgressBarFrame",name, x, y, w, 30, sx ,sy ,sw ,sh)
|
||||
c.Draggable=true
|
||||
c.dragbut="r"
|
||||
c.BorderSize=0
|
||||
c:ApplyGradient{Color.Blue,Color.sexy_purple}
|
||||
c:newTextLabel(txt,"Holder",0,0,0,h-30,0,1,1,0).Color=Color.sexy_purple
|
||||
c.funcO={}
|
||||
c.funcX={}
|
||||
c:OnDragStart(function(self)
|
||||
self:TopStack()
|
||||
end)
|
||||
local temp = c:newTextButton("X","Close",-25,5,20,20,1)
|
||||
temp.Tween=-5
|
||||
temp.XTween=-2
|
||||
temp:OnReleased(function(b,self) for i=1,#self.Parent.funcX do self.Parent.funcX[i](self.Parent) end end)
|
||||
temp.Color=Color.Red
|
||||
c.BarBG=c:newTextButton("",5,h-65,-10,30,0,1,1)
|
||||
c.BarBG:ApplyGradient{Color.Red,Color.light_red}
|
||||
c.Bar=c.BarBG:newTextLabel("",0,0,0,0,0,0,0,1)
|
||||
c.Bar:ApplyGradient{Color.Green,Color.light_green}
|
||||
c.BarDisp=c.BarBG:newTextLabel("0%","0%",0,0,0,0,0,0,1,1)
|
||||
c.BarDisp.Visibility=0
|
||||
c.BarDisp.Link=c.Bar
|
||||
c.BarDisp:OnUpdate(function(self)
|
||||
self.text=self.Link.scale.size.x*100 .."%"
|
||||
end)
|
||||
c.Func1={}
|
||||
function c:On100(func)
|
||||
table.insert(self.Func1,func)
|
||||
end
|
||||
c:OnUpdate(function(self)
|
||||
if self.Bar.scale.size.x*100>=100 then
|
||||
for P=1,#self.Func1 do
|
||||
self.Func1[P](self)
|
||||
end
|
||||
end
|
||||
end)
|
||||
function c:SetPercentage(n)
|
||||
self.Bar:SetDualDim(0,0,0,0,0,0,n/100,1)
|
||||
end
|
||||
return c
|
||||
end
|
||||
@ -1,42 +0,0 @@
|
||||
function gui:newScrollBar(color1,color2)
|
||||
local scrollbar=self:newFrame(-20,0,20,0,1,0,0,1)
|
||||
scrollbar.funcS={}
|
||||
scrollbar.Color=color1 or Color.saddle_brown
|
||||
scrollbar:OnClicked(function(b,self,x,y)
|
||||
love.mouse.setX(self.x+10)
|
||||
if y>=10 and y<=self.height-10 then
|
||||
self.mover:SetDualDim(0,y-10)
|
||||
end
|
||||
if y<10 then
|
||||
love.mouse.setY(10+self.y)
|
||||
end
|
||||
if y>self.height-10 then
|
||||
love.mouse.setY((self.height-10)+self.y)
|
||||
end
|
||||
for i=1,#self.funcS do
|
||||
self.funcS[i](self,self:getPosition())
|
||||
end
|
||||
end)
|
||||
scrollbar:OnEnter(function(self)
|
||||
self:addDominance()
|
||||
end)
|
||||
scrollbar:OnExit(function(self)
|
||||
self:removeDominance()
|
||||
end)
|
||||
scrollbar.mover=scrollbar:newTextButton("","",0,0,20,20)
|
||||
scrollbar.mover.Color=color2 or Color.light_brown
|
||||
function scrollbar:getPosition()
|
||||
return ((self.mover.offset.pos.y)/(self.height-20))*100
|
||||
end
|
||||
function scrollbar:setPosition(n)
|
||||
print((self.height-20),n)
|
||||
self.mover.offset.pos.y=((self.height-20)/(100/n))
|
||||
for i=1,#self.funcS do
|
||||
self.funcS[i](self,self:getPosition())
|
||||
end
|
||||
end
|
||||
function scrollbar:OnScroll(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
return scrollbar
|
||||
end
|
||||
@ -1,98 +0,0 @@
|
||||
function gui:newScrollMenu(title,tabN,onloop,x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local Main = self:newFrame(x, y, w, h, sx ,sy ,sw ,sh)
|
||||
local Title=Main:newTextButton(title,"Title",0,0,0,20,0,0,1)
|
||||
Title.Tween=-4
|
||||
Title.FontSize=12
|
||||
Title:OnReleased(function(b,self)
|
||||
self.Parent.Tick=not(self.Parent.Tick)
|
||||
end)
|
||||
local scroll=Main:newTextButton("","Scroll",-20,20,20,-20,1,0,0,1)
|
||||
scroll:OnClicked(function(b,self,x,y)
|
||||
self.Parent.Mover:SetDualDim(0,y-10,20,20)
|
||||
if self.Parent.Mover.offset.pos.y<0 then
|
||||
self.Parent.Mover:SetDualDim(0,0,20,20)
|
||||
end
|
||||
if self.Parent.Mover.offset.pos.y>self.Parent.height-40 then
|
||||
self.Parent.Mover:SetDualDim(0,self.Parent.height-40,20,20)
|
||||
end
|
||||
local temp = #self.Parent.TList
|
||||
self.Parent.pos=(math.floor((temp*self.Parent.Mover.offset.pos.y)/self.height))+1
|
||||
end)
|
||||
Main:OnUpdate(function(self)
|
||||
if self.Tick==false then
|
||||
self.Visibility=0
|
||||
end
|
||||
end)
|
||||
scroll:OnUpdate(function(self)
|
||||
self.Visible=self.Parent.Tick
|
||||
end)
|
||||
local Mover=scroll:newTextLabel("",0,0,20,20)
|
||||
Main.Mover=Mover
|
||||
Main.TList=tabN
|
||||
Main.pos=1
|
||||
Main.Tick=true
|
||||
function Main:Update(title,tabN,onloop)
|
||||
ch=self:getChildren()
|
||||
for i=#ch,1,-1 do
|
||||
ch[i]:Destroy()
|
||||
end
|
||||
Title=Main:newTextButton(title,"Title",0,0,0,20,0,0,1)
|
||||
Title.Tween=-4
|
||||
Title.FontSize=12
|
||||
Title:OnReleased(function(b,self)
|
||||
self.Parent.Tick=not(self.Parent.Tick)
|
||||
end)
|
||||
scroll=Main:newTextButton("","Scroll",-20,20,20,-20,1,0,0,1)
|
||||
scroll:OnClicked(function(b,self,x,y)
|
||||
self.Parent.Mover:SetDualDim(0,y-10,20,20)
|
||||
if self.Parent.Mover.offset.pos.y<0 then
|
||||
self.Parent.Mover:SetDualDim(0,0,20,20)
|
||||
end
|
||||
if self.Parent.Mover.offset.pos.y>self.Parent.height-40 then
|
||||
self.Parent.Mover:SetDualDim(0,self.Parent.height-40,20,20)
|
||||
end
|
||||
local temp = #self.Parent.TList
|
||||
self.Parent.pos=(math.floor((temp*self.Parent.Mover.offset.pos.y)/self.height))+1
|
||||
end)
|
||||
local Mover=scroll:newTextLabel("",0,0,20,20)
|
||||
Main.Mover=Mover
|
||||
Main.TList=tabN
|
||||
Main.pos=1
|
||||
Main.Tick=true
|
||||
scroll:OnUpdate(function(self)
|
||||
self.Visible=self.Parent.Tick
|
||||
end)
|
||||
for i=1,math.floor(Main.height/20)-1 do
|
||||
local temp=Main:newTextButton("","Item"..i,0,i*20,-20,20,0,0,1)
|
||||
temp.FontSize=10
|
||||
temp.Tween=-4
|
||||
temp.pos=i
|
||||
temp:OnUpdate(function(self)
|
||||
self.text=self.Parent.TList[(self.Parent.pos+self.pos)-1]
|
||||
self.Visible=self.Parent.Tick
|
||||
end)
|
||||
if onloop then
|
||||
onloop(temp,i)
|
||||
end
|
||||
end
|
||||
end
|
||||
io.write(tostring(Main.height).."\n")
|
||||
for i=1,math.floor(Main.height/20)-1 do
|
||||
local temp=Main:newTextButton("Item"..i,0,i*20,-20,20,0,0,1)
|
||||
temp.FontSize=10
|
||||
temp.Tween=-4
|
||||
temp.pos=i
|
||||
temp:OnUpdate(function(self)
|
||||
if self.Parent.TList[(self.Parent.pos+self.pos)-1]~=nil then
|
||||
self.text=self.Parent.TList[(self.Parent.pos+self.pos)-1]
|
||||
else
|
||||
self.text=""
|
||||
end
|
||||
self.Visible=self.Parent.Tick
|
||||
end)
|
||||
if onloop then
|
||||
onloop(temp,i)
|
||||
end
|
||||
end
|
||||
return Main
|
||||
end
|
||||
@ -1,6 +0,0 @@
|
||||
function gui:destroyAllChildren()
|
||||
local c=self.Children
|
||||
for i=1,#c do
|
||||
c[i]:Destroy()
|
||||
end
|
||||
end
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user