Page 1 of 1

lua console v.04a

Posted: 07 Jan 2011, 03:51
by Alchemist
something i whipped up, it's functional but some bugs that i'm still working on..

it's very simple now, so all it's capable of is single line calls, eg. Spring.GetSelectedUnits(), etc...

Code: Select all

press Ctrl to activate, then simply start typing a command.
Enter to enter...
Ctrl again to deactivate
download here: http://www.springfiles.com/show_file.php?id=2902


Things to work on:
- working on implementing a different mode to be able to type more than a single line of code
- implement a better gui
- not all errors are printed


criticism, suggestions, and bug reports welcome

Re: lua console v.04a

Posted: 07 Jan 2011, 05:04
by knorke
interessting. (but too lazy to test right now)
you could make into a debug tool thing, like printing unitIDs above units could be usefull for DestroyUnit (u) etc

Re: lua console v.04a

Posted: 07 Jan 2011, 13:54
by zwzsg
knorke wrote:printing unitIDs above units

Code: Select all

function widget:GetInfo()
    return {
            name    = "Show Unit ID",
            desc    = "Show all ID of all units",
            author    = "zwzsg",
            date    = "August 2010",
            license    = "Free",
            layer    = 0,
            enabled    = false,
        }
end


function widget:DrawScreenEffects()
    for _,id in ipairs(Spring.GetAllUnits()) do
        local x,y=Spring.WorldToScreenCoords(Spring.GetUnitPosition(id))
        gl.Text("ID:"..id,x,y,16,"od")
    end
end

Re: lua console v.04a

Posted: 24 Mar 2014, 21:56
by Beherith
This gadget does not work for me, anyone know of a quick fix?
Would be super useful...

Code: Select all

[f=0000026] ---Lua Console deACTIVATED---
[f=0000026] Error: LuaRules::RunCallIn: error = 2, KeyPress, [Internal Lua error: Call failure] [string "LuaRules/Gadgets/dbg_console.lua"]:119: attempt to call field 'handleKeys' (a nil value)
[f=0000054] ---Lua Console ACTIVATED--- 
Error in: GG.handleKeys(k, m, l)
Thanks!

Re: lua console v.04a

Posted: 30 Mar 2014, 12:48
by knorke
Attachment is version of zwzsg's "Show Unit ID" widget that also shows IDs of features and projectiles.
Can easily be edited to fit needs, display other information than id.
Small widget that has saved me lots of time, imo very helpful.

Re: lua console v.04a

Posted: 25 Jul 2014, 18:20
by code_man

Code: Select all

-- console.lua

--

local version = .05

function gadget:GetInfo()
	return {
		name = "Lua Console",
		desc = "version " .. version,
		author = "Alchemist",
		date = "January 2011",
		license = "MIT X11",
		layer = 0,
		enabled = true
	}
end

local msgID = gadget:GetInfo().name .. ": "

--UNSYNCED-------------------------------------------------------------------
if (not gadgetHandler:IsSyncedCode()) then
    local active = false
    local command = ""
    local hist = ""
    local specialKeys = { [0] = ')', [1] = '!', [2] = '@', [3] = '#', [4] = '$', [5] = '%', [6] = '^', [7] = '&', [8] = '*', [9] = '(',
						[32] = ' ', [39] = '\'', [39.1] = '\"', [44] = ',', [44.1] = '<', [45] = '-', [45.1] = '_',
						[46] = '.', [46.1] = '>', [59] = ';', [59.1] = ':', [61] = '=', [61.1] = '+',
						[91] = '[', [93] = ']', [91.1] = '{', [93.1] = '}', [96] = '`', [96.1] = '~'}
    local actions = { "gameinfo", "controlunit", "sharedialog", "togglelos", "mouse2", "mousestate", "chat", "debug" }

    function gadget:GamePreload()
	Spring.SendMessage("Lua Console loaded... to Activate/Deactivate press CTRL")
    end

    function gadget:KeyPress(k, m, r, l)
        handleKeys(k, m, l)
    end
	
    function handleKeys(key, m, l)
        if (key == 306) then
            if (active == false) then
                active = true
                Spring.SendMessage("---Lua Console ACTIVATED---")
		for _, a in pairs(actions) do
		    Spring.SendCommands({"unbindAction " .. a})
		end
	    else
		active = false
		Spring.SendMessage("---Lua Console deACTIVATED---")
		--Spring.SendCommands({"keyreload"})  CRASH!!!
	    end
	    return false
	end
	
	if (active == true) then
	    if(key <= 122 and key >= 97) then -- A-Z and a-z
		if(m.shift) then
		    command = command .. string.sub(string.upper(l), -1)
		    elseif(m.alt or m.ctrl) then
			command = command .. string.sub(l, -1)
		    else
			command = command .. l
		end
		elseif (key <= 57 and key >= 48) then -- 0-9
		     key = key - 48
		     if (m.shift) then
			 command = command .. specialKeys[key]
			else
			    command = command .. key
			end
		    elseif (key == 8) then -- Backspace
				if(string.len(command) >= 1) then
					command = string.sub(command, 1, -2)
				end
			elseif (key == 13) then -- Enter
				eval_and_display (command)
				hist = command .. '\n' .. hist
				command = ""
			elseif (specialKeys[key] == nil) then
			else
				if (m.shift) then
					command = command .. specialKeys[key + .1]
				else
					command = command .. specialKeys[key]
				end
			end
		end
	end
	
	GG.handleKeys = handleKeys

	function eval_and_display (cmd)
		local chunk = loadstring('return ' .. cmd)
		if(chunk) then
			local results = { pcall(chunk) }
			if(not results[1]) then
				Spring.SendLuaRulesMsg(msgID .. cmd)
			else
				table.remove(results, 1)
				Spring.Echo("--> " .. tostring (results[1]))
			end
		end
	end

	function DrawScreen()
		local vsx, vsy = gl.GetViewSizes()
		local posx, posy = vsx * 0.25, vsy * 0.75
                gl.Rect (posx - 5, posy + 13, posx + 502, posy + 10)
		gl.Text(command, posx, posy, 12, "on")
		gl.Text(hist, posx, (.9 * posy), 12, "on")
	end

	function gadget:KeyPress(k, m, r, l)
		GG.handleKeys(k, m, l)
	end
--SYNCED---------------------------------------------------------------------
else

	function gadget:RecvLuaMsg(msg, player)
		local cmd = string.match(msg, msgID .. "(.*)")
		if cmd then
			local chunk = loadstring('return ' .. cmd)
			if(chunk) then
				doline(chunk)
			end
		end
	end

	function doline(cmd)
		local results = { pcall(cmd) }
		if (not results[1]) then
			Spring.Echo(results[2])
		else
			table.remove(results, 1)
			Spring.Echo(unpack(results))
		end
	end
	
end
Heres a improved version, it gives better output and doesnt just suppres erros, tough its an uncought side effect i think.

Also i have these 2 problems, i dont know how to restrict gl.Text to some size and how to have it draw a gl.Rect inside gadget:KeyPress so it only draws when it gets activated.
Im fairly new to this whole graphics stuff.

Re: lua console v.04a

Posted: 25 Jul 2014, 18:42
by Jools
Don't use gl.Text. Use font:Print instead, and see http://springrts.com/wiki/Lua_Fonts

Re: lua console v.04a

Posted: 26 Jul 2014, 01:12
by Silentwings
Checkout http://springrts.com/wiki/Lua_OpenGL_Api#Text for the syntax, which allows you to choose font size. To show/hide on a mouse event, use one of the Mouse**** callins to catch the mouse event and save the result to a local variable which can then be read when the Draw*** call in runs.

There is nothing wrong with gl.Text if it can do what you want. If you want a really nice UI then using chili is best but I would not let that bother you for now.

Re: lua console v.04a

Posted: 29 Jul 2014, 20:47
by code_man
Jools wrote:Don't use gl.Text. Use font:Print instead, and see http://springrts.com/wiki/Lua_Fonts
I dont know how to use this, if i try calling it it complains its not defined.
Does the ':' carry a special meaning?
Im still a bit new to lua.

Re: lua console v.04a

Posted: 29 Jul 2014, 22:44
by FLOZi
Fairly crude example of font usage here: https://sourceforge.net/p/mwspring/code ... ounter.lua

basics are;

Code: Select all

local btFont = gl.LoadFont("LuaUI/Fonts/bt_oldstyle.ttf", 16, 2, 30)
To initalise the font, then

Code: Select all

btFont:Begin()
		btFont:Print(cBillsText, xMax * 0.25, yMax - 32, 16, "od")
		btFont:Print(tonnageText, xMax * 0.45, yMax - 32, 16, "od")
		btFont:Print(dropTime, xMax * 0.75, yMax - 32, 16, "odr")
btFont:End()
For printing; it is more efficient to put multiple print calls between Begin() and End(). The first param is a string (can be passed directly) of the text to print, then the x and y screen coordinates, font size and options.

API reference here:

http://springrts.com/wiki/Lua_Fonts

Re: lua console v.04a

Posted: 31 Jul 2014, 14:51
by code_man
I dont have that font, is there a spring default one?
I cant see any .ttf files in /luaUI/fonts.

Re: lua console v.04a

Posted: 31 Jul 2014, 16:24
by FLOZi
The point is you can add any ttf font you find / choose to use.

Re: lua console v.04a

Posted: 04 Aug 2014, 10:52
by code_man
Right this dont really help me. I dont see the point of font stuff, its nearly identical to gl.text and font:WrapText doenst seem to be of much use either.
It didnt came to me before but i can just line break the text myself i think, but its a stupid thing to do considering such functions should be able to this themselfs.

Also whats the deal with the DrawScreen()?
Where those that get called and why can i put gl. and font: functions only there?

Wiki is almost empty in that regard.