Okay first library is my mouse control library, titled ow_control.
Code: Select all
--[[
LICENCE DETAILS: ZLIB LICENCE
Copyright (c) <2012> <Lewis Pearce>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
]]
-- HOLDS ALL GAME CONTROLS AND SOME FUNCTIONS RELATING TO THEM
-- interaction tables used by the menu system example
-- this is how i would use it myself
--[[
startuptable = { -- table key/name, for distinction, but tables without names can be fed straight into currenttable
{x=100,y=70,w=600,h=80,action={"pre","rwrinfo"},}, -- position and size, with action telling what peice of code to use with it, action here has 2 entries the section and the actual action, this just lets me distinguish sections in the mouseAction function, but it can be coded to distinguish on table name/key instead
}
]]
-- currenttable to draw menu style interactions from
-- place in main.lua
-- more tables can be added to this but the detection functions return the last detection they make, allowing overlay menus to work without tinkery provided they are last in the currenttable list.
--currenttable = {startuptable}
-- click area detection functions for menu code
function nearestmenu(x,y)
return nearestestui(x,y,currenttable)
end
function nearestestui(x,y,table)
local id = null
for i = 1,#table do
for i,v in ipairs(table[i]) do
if checkCollision(x,y,1,1,v.x,v.y,v.w,v.h) == true then
id = {i,j,v.action}
end
end
end
return id
end
--input library for mousepresses
-- bashes the information straight through to the mouseAction below, but exceptions can be added so that it doesn't in some circumstances
function love.mousepressed(x,y,button)
mouseAction(x,y,button)
end
function love.mousereleased(x,y,button)
if button == "m" then
mouseAction(x,y,5)
end
end
-- HUD DETECTION CODE
--SUPERFUNCTION
--controls all of the mouse actions within the editor
--a bit cryptic but not to hard to understand
function mouseAction(x,y,act)
-- this just gets what your currently clicking on from the tables your testing at the moment
local n = nearestmenu(x,y)
--NULL INPUT
--stops it from crashing if theres nothing at click
if n ~= null then
if n[3][1] == "pre" then -- test one used as a distinction
if n[3][2] == "rwrinfo" then -- test two used to work out the command
-- STUFF TO DO HERE
end
end
end
end
Above is the version that uses two entries in action to distinguish which part of mouseAction to go to, though it could use the key of the tables easily with minor modifications to mouseAction and nearestestui, I've just found that making sections independent from table saves alot of repeat code.
Would love any critique on this as I'm always expanding and simplifying its style, I'm aware that it is a very dry system that requires setting up for each project and page but I find this makes it more powerful just slower to enable and set up, though outside dependancies and a creator for the actual menutables could easily be written in.
This was developed over about 5 or 6 failed projects which required large and varied ability menus, huds and ui's. The original version was infact incredibly huge and about double the size of this due to no simplification or anything, I personally love this system and combined with a debug function shared below can be added and modified very very easily and rapidly.
The debug system just added to the bottom of love.graphics.draw(), you have to set the value "debug" as debug = true and create a font of size 11 called font11 to make it work, but it can be switched off by just setting debug to false.
Code: Select all
if debug then
-- currenttable highlights
love.graphics.setColor(255,255,255,100)
love.graphics.setFont(font11)
for i=1,#currenttable do
for i,v in ipairs(currenttable[i]) do
love.graphics.rectangle("fill",v.x,v.y,v.w,v.h)
end
end
-- infopanel
local temp = "FPS: "..love.timer.getFPS()..". x:"..mx..". y:"..my.."."
if (mx+font11:getWidth(temp)) < love.graphics.getWidth() then
love.graphics.setColor(0,30,100,150)
love.graphics.rectangle("fill",mx+10,my-0,font11:getWidth(temp),11)
love.graphics.setColor(200,255,255,255)
love.graphics.rectangle("line",mx+10,my+0,font11:getWidth(temp),11)
love.graphics.print(temp,mx+10,my+0)
else
love.graphics.setColor(0,30,100,150)
love.graphics.rectangle("fill",mx-(font11:getWidth(temp)+10),my-0,font11:getWidth(temp),11)
love.graphics.setColor(200,255,255,255)
love.graphics.rectangle("line",mx-(font11:getWidth(temp)+10),my+0,font11:getWidth(temp),11)
love.graphics.print(temp,mx-(font11:getWidth(temp)+10),my+0)
end
temp = nil
end
Finally here is an image of a menu created recently with this version of the system and with the debug module turned on.
- ow_controlsystem.png (235.03 KiB) Viewed 10574 times
It can also be viewed by pressing f1 in audiotree or f1/f2 in most of my programs.
If this is useful or interesting to anyone I will soon release the next block of code.
Dull but sincere filler.