Small problem with my GUI framework

Post Reply
chalraes
Posts: 10
Joined: Mon Jun 14, 2010 10:48 pm

Small problem with my GUI framework

Post by chalraes » Thu Jun 17, 2010 1:29 am

Hello again.

I've been trying to develop a semi-fluid widget-based framework for my GUI, and I have run into more than a few snags. It seems even if the widgets successfully loads, it will not display on screen. I can't seem to find where I screwed up. Any help would be appreciated. Oh, just as a note, it uses Geyser.

main gui script
Code: [show] | [select all] lua
gui = gui or {}

gui.const = gui.const or {}
gui.const.maxwidth, gui.const.maxheight = getMainWindowSize()

gui.locations = gui.locations or {
	leftSidebar = {
		width = 0,
		height = gui.const.maxheight,
		x = 0,
		y = "0px",
		enabled = false,
		widgets = {}
	};
	rightSidebar = {
		width = 0,
		height = gui.const.maxheight,
		x = 0,
		y = "0px",
		enabled = false,
		widgets = {}
	};
	overhead = {
		width = 0,
		height = 0,
		x = 0,
		y = "0px",
		enabled = false,
		widgets = {}
	}
}

function gui:calculatePos()
	if gui.locations.rightSidebar.enabled == true then
		gui.locations.rightSidebar.width = widgets[gui:getWidest(gui.locations.rightSidebar.widgets)].width
		gui.locations.rightSidebar.x = 0 - gui.locations.rightSidebar.width - 9 
	end
	if gui.locations.leftSidebar.enabled == true then
		gui.locations.leftSidebar.x = 0
		gui.locations.leftSidebar.width = widgets[gui:getWidest(gui.locations.leftSidebar.widgets)].width
	end
	if gui.locations.overhead.enabled == true then
		gui.locations.overhead.x = 0
		gui.locations.overhead.width = gui.const.maxwidth - 9 --neg 9 due to scrollbar
		gui.locations.overhead.height = gui:getTotalOverheadHeight()
	end
	if gui.locations.leftSidebar.enabled == true and gui.locations.overhead.enabled == true then
		gui.locations.overhead.x = gui.locations.leftSidebar.width
		gui.locations.overhead.width = gui.locations.overhead.width - gui.locations.leftSidebar.width
		if gui.locations.rightSidebar.enabled == true then
			gui.locations.overhead.width = gui.locations.overhead.width - gui.locations.rightSidebar.width + 9
		end
	end
	if gui.locations.rightSidebar.enabled == true and gui.locations.overhead.enabled == true and gui.locations.leftSidebar.enabled == false then
		gui.locations.overhead.width = gui.locations.overhead.width - gui.locations.rightSidebar.width
	end
	gui.locations.rightSidebar.width = tostring(gui.locations.rightSidebar.width) .. "px"
	gui.locations.rightSidebar.x = tostring(gui.locations.rightSidebar.x) .. "px"
	gui.locations.leftSidebar.width = tostring(gui.locations.leftSidebar.width) .. "px"
	gui.locations.leftSidebar.x = tostring(gui.locations.leftSidebar.x) .. "px"
	gui.locations.overhead.width = tostring(gui.locations.overhead.width) .. "px"
	gui.locations.overhead.x = tostring(gui.locations.overhead.x) .. "px"
end

function gui:getWidest(widtable)
	local widest = nil
	local widewidth = nil
	for _,v in pairs(widtable) do
		if widgets[v].width ~= nil then
			if widest == nil or widgets[v].width > widewidth then
				widest = v
				widewidth = widgets[v].width
			end
		end
	end
	return widest
end

function gui:getTotalOverheadHeight()
	local wid = 0
	for _,v in pairs(gui.locations.overhead.widgets) do
		wid = wid + widgets[v].height
	end
	return wid
end

function gui:resetLocation()
	gui.locations.leftSidebar = {
		width = 0,
		height = "100%",
		x = 0,
		y = "0px",
		enabled = false,
		widgets = {}
	}
	gui.locations.rightSidebar = {
		width = 0,
		height = "100%",
		x = 0,
		y = "0px",
		enabled = false,
		widgets = {}
	}
	gui.locations.overhead = {
		width = 0,
		height = 0,
		x = 0,
		y = "0px",
		enabled = false,
		widgets = {}
	}
end

function gui:update()
	if gui.locations.leftSidebar.enabled == true then
		for _, v in pairs(gui.locations.leftSidebar.widgets) do
			widgets[v]:update() 
		end
		tempx = 0
		tempy = 0
	end
	if gui.locations.rightSidebar.enabled == true then
		for _, v in pairs(gui.locations.rightSidebar.widgets) do
			widgets[v]:update() 
		end
		tempx = 0
		tempy = 0
	end
	if gui.locations.overhead.enabled == true then
		for _, v in pairs(gui.locations.overhead.widgets) do
			widgets[v]:update() 
		end
		tempx = 0
		tempy = 0
	end
end

function gui:deploy()
	local tempx = 0 --for overhead
	local tempy = 0 --for sidebar
	if gui.locations.leftSidebar.enabled == true then
		gui.leftSidebar = Geyser.Container:new(
			{x = gui.locations.leftSidebar.x, y = gui.locations.leftSidebar.y,
			width = gui.locations.leftSidebar.width, height = gui.locations.leftSidebar.height,
			name = "leftSidebar"})
		for _, v in pairs(gui.locations.leftSidebar.widgets) do
			tempx, tempy = widgets[v]:initiate("leftSidebar", tempx, tempy) 
		end
		echo("t")
		gui.leftSidebar:show()
		echo("t")
		tempx = 0
		tempy = 0
	end
	if gui.locations.rightSidebar.enabled == true then
		gui.rightSidebar = Geyser.Container:new(
			{x = gui.locations.rightSidebar.x, y = gui.locations.rightSidebar.y,
			width = gui.locations.rightSidebar.width, height = gui.locations.rightSidebar.height,
			name = "rightSidebar"})
		for _, v in pairs(gui.locations.rightSidebar.widgets) do
			tempx, tempy = widgets[v]:initiate("rightSidebar", tempx, tempy)
			echo(tempx .. "/" .. tempy .. "/")
		end
		gui.rightSidebar:show()
		tempx = 0
		tempy = 0
	end
	if gui.locations.overhead.enabled == true then
		gui.overhead = Geyser.Container:new(
			{x = gui.locations.overhead.x, y = gui.locations.overhead.y,
			width = gui.locations.overhead.width, height = gui.locations.overhead.height,
			name = "overhead"})
		for _, v in pairs(gui.locations.overhead.widgets) do
			tempx, tempy = widgets[v]:initiate("overhead", tempx, tempy) 
		end
		gui.overhead:show()
		tempx = 0
		tempy = 0
	end
end

function gui:destroy()
	if gui.locations.leftSidebar.enabled == true then
		for _, v in pairs(gui.locations.leftSidebar.widgets) do
			widgets[v]:destroy() 
		end
		tempx = 0
		tempy = 0
	end
	if gui.locations.rightSidebar.enabled == true then
		for _, v in pairs(gui.locations.rightSidebar.widgets) do
			widgets[v]:destroy() 
		end
		tempx = 0
		tempy = 0
	end
	if gui.locations.overhead.enabled == true then
		for _, v in pairs(gui.locations.overhead.widgets) do
			widgets[v]:destroy() 
		end
		tempx = 0
		tempy = 0
	end
	Geyser.hideAll()
end

function gui:loadProfile(profile)
	gui:destroy()
	gui:resetLocation()
	gui:calculatePos()
	if profiles[profile].leftSidebar.enabled == true then
		gui.locations.leftSidebar.enabled = true
		gui.locations.leftSidebar.widgets = profiles[profile].leftSidebar.widgets
	end
	if profiles[profile].rightSidebar.enabled == true then
		gui.locations.rightSidebar.enabled = true
		gui.locations.rightSidebar.widgets = profiles[profile].rightSidebar.widgets
	end
	if profiles[profile].overhead.enabled == true then
		gui.locations.overhead.enabled = true
		gui.locations.overhead.widgets = profiles[profile].overhead.widgets
	end
	gui:deploy()
end

widget init
Code: [show] | [select all] lua
widgets = widgets or {}
profile init
Code: [show] | [select all] lua
profiles = profiles or {}
test profile
Code: [show] | [select all] lua
--Set profile information
profiles.sideprofile =  { 	--initialize profile, set name to testprofile
	leftSidebar = { --set information for left sidebar location
		enabled = true, --show left sidebar
		widgets = { --set left sidebar widgets
			"sidegauges"
		}
	}; --end left sidebar information
	rightSidebar = { -- set right sidebar information
		enabled = false --disable right sidebar
	}; --end right sidebar information
	overhead = { --You know the drill
		enabled = false --yada yada
	}
} --end profile
widget - sidegauges
Code: [show] | [select all] lua
widgets.sidegauges = widgets.sidegauges or { --required in every widget
	intendedLocation = "overhead", --Really, ignore this. It has no purpose other than
		--telling people where to place this
	width = 200 -- You need at least one widget with a specified width in a sidebar plugin.
}

function widgets.sidegauges:initiate(location, tempx, tempy) --required in every widget
	widgets.sidegauges.sidegauges = Geyser.Container:new(
      { x = tempx .. "px", y = tempy .. "px",
		width = "100%", height = "62px",
		name = "sidegauges"}) --create console

	widgets.sidegauges.hgauge = Geyser.Gauge:new(
			{ x = "0px", y = "0px",
			width = "100%", height = "12px",
			orientation = "horizontal", color = "red",
			name = "hGauge"}, widgets.sidegauges.sidegauges)
	widgets.sidegauges.mgauge = Geyser.Gauge:new(
			{ x = "0px", y = "0px",
			width = "100%", height = "12px",
			orientation = "horizontal", color = "blue",
			name = "mGauge"}, widgets.sidegauges.sidegauges)
	widgets.sidegauges.egauge = Geyser.Gauge:new(
			{ x = "0px", y = "0px",
			width = "100%", height = "12px",
			orientation = "horizontal", color = "yellow",
			name = "eGauge"}, widgets.sidegauges.sidegauges)
	widgets.sidegauges.pgauge = Geyser.Gauge:new(
			{ x = "0px", y = "0px",
			width = "100%", height = "12px",
			orientation = "horizontal", color = "violet",
			name = "pGauge"}, widgets.sidegauges.sidegauges)
	widgets.sidegauges.xpgauge = Geyser.Gauge:new(
			{ x = "0px", y = "0px",
			width = "100%", height = "12px",
			orientation = "horizontal", color = "NavyBlue",
			name = "xpGauge"}, widgets.sidegauges.sidegauges)


	if location == "leftSidebar" then
		gui.leftSidebar:add(widgets.sidegauges.sidegauges)
	elseif location == "rightSidebar" then
		gui.rightSidebar:add(widgets.sidegauges.sidegauges)
	elseif location == "overhead" then
		gui.overhead:add(widgets.sidegauges.sidegauges)
	end

	local r = tempx
	local s = tempy + 62
	return r, s --height is used for sidebars. Both for overhead
end

function widgets.sidegauges:destroy()
end

function widgets.sidegauges:update()
	if tonumber(stats.health) > tonumber(stats.maxhealth) then
		widgets.sidegauges.hgauge:setValue(tonumber(stats.maxhealth), tonumber(stats.maxhealth))
	else
		widgets.sidegauges.hgauge:setValue(tonumber(stats.health), tonumber(stats.maxhealth))
	end
	widgets.sidegauges.hgauge:setText(tostring(stats.health) .. "/" .. tostring(stats.maxhealth))
	if tonumber(stats.mana) > tonumber(stats.maxmana) then
		widgets.sidegauges.mgauge:setValue(tonumber(stats.maxmana), tonumber(stats.maxmana))
	else
		widgets.sidegauges.mgauge:setValue(tonumber(stats.mana), tonumber(stats.maxmana))
	end
	widgets.sidegauges.mgauge:setText(tostring(stats.mana) .. "/" .. tostring(stats.maxmana))
	if tonumber(stats.ego) > tonumber(tonumber(stats.maxego)) then
		widgets.sidegauges.egauge:setValue(tonumber(stats.maxego), tonumber(stats.maxego))
	else
		widgets.sidegauges.egauge:setValue(tonumber(stats.ego), tonumber(stats.maxego))
	end
	widgets.sidegauges.egauge:setText(tostring(stats.ego) .. "/" .. tostring(stats.maxego))
	widgets.sidegauges.pgauge:setValue(tonumber(stats.power), 10, tostring(stats.power) .. "/10")
	widgets.sidegauges.xpgauge:setValue(tonumber(stats.newlevel), 100,tostring(stats.newlevel) .. "/100")
end
EDIT: If I am going to give you giant walls of code, I should at least give you the right ones :P

naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

Re: Small problem with my GUI framework

Post by naftali » Thu Jun 17, 2010 3:51 pm

I did something like this for a GUI I coded recently, and you're making it far too difficult for yourself. One of the great things about Geyser is you don't necessarily NEED huge tables full of GUI constants. Here's a part of mine, for example:
Code: [show] | [select all] lua
local tabs = { "All", "Tells", "City", "House", "Family", "Order", "Combat", "Guard", "Party" }

function topStartup()
	guiTopMain = guiTopMain or Geyser.Container:new({ name = "guiChatMain" })
	guiTopMain:move( 0, 0 )
	guiTopMain:resize( "100%", 210 )

	local posX,posY,width,height
	width = 300

	guiMapConsole = guiMapConsole or Geyser.MiniConsole:new({ name = "guiMapConsole" }, guiTopMain)
	guiMapConsole:move( -width, 0 )
	guiMapConsole:resize( width, "100%" )
	guiMapConsole:setFontSize(9)
	guiMapConsole:setColor( 0,0,0, 0)
	setBorderRight( width )

	width = guiTopMain.get_width() - guiMapConsole.get_width() - 2

	guiChatMain = guiChatMain or Geyser.Container:new({ name = "guiChatMain" }, guiTopMain )
	guiChatMain:move( 0, 0 )
	guiChatMain:resize( width, "100%")

	chatlabels = chatlabels or {}

	width, height = 50, 25

	guiChatLabels = guiChatLabels or Geyser.Container:new({ name = "guiChatLabels" }, guiChatMain )
	guiChatLabels:move( 0, 0 )
	guiChatLabels:resize( width * 2, "100%" )

	for i,v in ipairs(tabs) do
		local tabname

		tabname = "button" .. v:title()
		posX = 0
		if i / 2 == math.floor(i / 2) then posX = posX + width end
		posY = ( math.ceil( i / 2 ) - 1 ) * height

		chatlabels[tabname] = chatlabels[tabname] or Geyser.Label:new({ name = tabname }, guiChatLabels)
		chatlabels[tabname]:move( posX, posY )
		chatlabels[tabname]:resize( width, height )
		chatlabels[tabname]:setColor( 25,25,25, 0 )
		chatlabels[tabname]:echo("<center><font color='grey' size='2'>" .. v .. "</font></center>")
		chatlabels[tabname]:setClickCallback("chatSwapTab", "chat" .. v )
	end

	chatconsoles = chatconsoles or {}

	posX, posY = guiChatLabels:get_width(), 0
	width = guiChatMain:get_width() - guiChatLabels:get_width()
	height = "100%"
	local fontsize = 10
	local wrap = math.floor( width / calcFontSize(fontsize) )

	for i,v in ipairs(tabs) do
		local tabname = "chat" .. v:title()

		chatconsoles[tabname] = chatconsoles[tabname] or Geyser.MiniConsole:new({ name = tabname, }, guiChatMain )
		chatconsoles[tabname]:move( posX, posY )
		chatconsoles[tabname]:resize( width, height )
		chatconsoles[tabname]:setColor( 25,25,25, 0)
		chatconsoles[tabname]:setFontSize(fontsize)
		chatconsoles[tabname]:setWrap(wrap)
		chatconsoles[tabname]:hide()
	end

	chatconsoles.chatAll:show()
	chatlabels.buttonAll:setColor( 50,50,50, 255 )
	globals.chatCurrent = "chatAll"
	setBorderTop(guiTopMain.get_height())
end

function echoChat(channel)
	local str = "chat" .. channel or "chatAll"
	selectString(line,1)
	setBgColor(25,25,25)
	copy()
	appendBuffer("chatAll")
	if str ~= "chatAll" then appendBuffer(str) end
	setBgColor(0,0,0)
	if cgag[str] then deleteFull() end
	str = "button" .. string.title(channel)
	chatlabels[str]:flash(0.5)
	--growlNotify(channel,line:gsub(",","\,"))
end
What I did is use local variables to make defining the Geyser windows clearer, but I left them all within the script. At some point I'll probably find the ones that I want to be easy to change and make a script full of just global variables so that they can be changed easy. Not all the variables need to be that way though - some are gonna be defined relative to other variables, others will be defined relative to their containers. Until I figure out which ones are which I'm just putting everything where I know where it is. I'll worry about making it easy to change things after I get things the way I want them.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests