System control miniwindow

Posted by Dontarion on Tue 14 Feb 2012 06:05 PM — 4 posts, 17,994 views.

USA #0
This code is a bit incomplete but gives you a framework to work and learn from for your own windows. At this point I know there is some optimization I could do but I haven't gotten around to it.

Some of the variables and code may be similar to one of Nick's fancy windows and that's because I learned from reading that code and making my own.

Do with it what you will. Most of this is fairly static in how it does the layout for now.


------
----- Everything below this point is for the graphical control window.
------


-- Config	
currentTab = "Info"
GCW = "GleamControlWindow"..GetPluginID() -- GCW = Gleam Control Window.

-- window size in pixels
WINDOW_WIDTH = 420
WINDOW_HEIGHT = 299   -- 200 is 16 lines of 9-point Lucida Console
LEFT_MARGIN = 5
RIGHT_MARGIN = 5
TOP_MARGIN = 5
BOTTOM_MARGIN = 5
WINDOW_POSITION = 6

-- colours
WINDOW_BACKGROUND_COLOUR = ColourNameToRGB ("black")
WINDOW_TEXT_COLOUR = ColourNameToRGB ("black")
WINDOW_BORDER_COLOUR = ColourNameToRGB ("#E8E8E8")

-- Tab info
SystemTabs = {"Info", "Defenses", "Curing", "Parry", "Modes", "Help"} -- Each entry here has its own function.
buttons_max_width = GetInfo(240)*(string.len(table.concat(SystemTabs, ""))+2*table.maxn(SystemTabs))
buttons_left_start = (WINDOW_WIDTH - buttons_max_width) / 2
draw_tab_top = GetInfo(241)*3.5+1+TOP_MARGIN+3
draw_tab_left = LEFT_MARGIN + 5
	
-- font
FONT_NAME = "Dina"
FONT_SIZE = "8"
FONT_ID_NORMAL = "font" .. GCW




function init()
	
	WindowCreate (GCW, 
                windowinfo.window_left, 
                windowinfo.window_top, 
                WINDOW_WIDTH,    
                WINDOW_HEIGHT,  
                windowinfo.window_mode, 
                windowinfo.window_flags, 
                WINDOW_BACKGROUND_COLOUR)
	
	-- show border so we can see what we are doing
	WindowRectOp (GCW, 1, 0, 0, 0, 0, WINDOW_BORDER_COLOUR)
	
	-- write Gleam Control stuff to top of screen.
	drawTitle()
	
	-- Write buttons to screen.
	drawButtons()

	-- Write tab info to screen.
    drawTab()
						
	-- Show the window
	WindowShow(GCW, true)
	movewindow.save_state (GCW)
end

function drawTitle()
	-- This function draws the system title etc above the control window buttons.
	title_text = "Gleam Control Window"
	title_height = GetInfo(241)*1.5
	title_width = GetInfo(240)*(string.len(title_text)+2)
	title_top = TOP_MARGIN
	title_left = (WINDOW_WIDTH - title_width)/2
	title_bottom = title_top + title_height
	title_right = title_left + title_width
	TITLE_TEXT_COLOUR = ColourNameToRGB ("gold")
	
	WindowText(GCW, FONT_ID_NORMAL, title_text, title_left, title_top, title_right, title_bottom, TITLE_TEXT_COLOUR, false)
end


function drawButtons() -- The code below adds the tabs for each control.
    WindowDeleteAllHotspots(GCW)
	count = buttons_left_start
	
	for a, tabTitle in pairs(SystemTabs) do
	
		tab_width = GetInfo(240)*(string.len(tabTitle)+2)
		tab_height = GetInfo(241)*1.5
		tab_left = count
		tab_right = tab_left + tab_width - 3
		tab_top = GetInfo(241)*2
		tab_bottom = tab_top + tab_height + 1
		
		if string.lower(currentTab) == string.lower(tabTitle) then
			TAB_TEXT_COLOUR = ColourNameToRGB "red"
		else
			TAB_TEXT_COLOUR = ColourNameToRGB "black"
		end
		
		if string.lower(currentTab) == string.lower(tabTitle) then -- Active tab is pushed in.
			WindowRectOp (GCW, miniwin.rect_draw_edge, tab_left, tab_top, tab_right, tab_bottom,
				miniwin.rect_edge_etched,
				miniwin.rect_edge_at_all+miniwin.rect_option_fill_middle)
		else -- inactive tabs are raised.
			WindowRectOp (GCW, miniwin.rect_draw_edge, tab_left, tab_top, tab_right, tab_bottom,
				miniwin.rect_edge_raised,
				miniwin.rect_edge_at_all+miniwin.rect_option_fill_middle)
		end
		
		WindowAddHotspot(GCW, tabTitle, tab_left, tab_top, tab_right, tab_bottom, "MouseOver", "CancelMouseOver", "MouseDown", "CancelMouseDown", "MouseUp", tabTitle, 1, 0)
		WindowText(GCW, FONT_ID_NORMAL, tabTitle, count+5, (GetInfo(241)*2.5)-1, count + tab_width - 5, 0, TAB_TEXT_COLOUR, false)

		count = count + tab_width + 1	
	end
	
	movewindow.add_drag_handler (GCW, 0, 0, WINDOW_WIDTH, GetInfo(241), 10)
end


See next post.
Amended on Tue 14 Feb 2012 06:11 PM by Dontarion
USA #1
function drawTab()
	-- There are no more lines.  This is a placeholder now.
	display_window_left = LEFT_MARGIN
	display_window_right = WINDOW_WIDTH - RIGHT_MARGIN
	display_window_top = GetInfo(241)*3.5+1+TOP_MARGIN
	display_window_bottom = WINDOW_HEIGHT - BOTTOM_MARGIN
	WindowRectOp (GCW, miniwin.rect_draw_edge, display_window_left, display_window_top, display_window_right, display_window_bottom,
				miniwin.rect_edge_etched,
				miniwin.rect_edge_at_all + miniwin.rect_option_fill_middle)
	if string.lower(currentTab) == "info" then
		drawTabInfo()
	elseif string.lower(currentTab) == "defenses" then
		drawTabDefenses()
	elseif string.lower(currentTab) == "curing" then
		drawTabCuring()
	elseif string.lower(currentTab) == "parry" then
		drawTabParry()
	elseif string.lower(currentTab) == "modes" then
		drawTabModes()
	elseif string.lower(currentTab) == "help" then
		drawTabHelp()
	end
end
USA #2
My function drawTabInfo() alone is over 6k characters so I can't post it.

function refresh()
	WindowRectOp(GCW, 2, 0, 0, 0, 0, WINDOW_BACKGROUND_COLOUR)
	WindowShow(GCW, true)
	init()
end

function drawTabDefenses()
	line_count = 1
	-- draw_tab_top == top line info.
	-- draw_tab_left == left start
	info_tab_left = draw_tab_left
	info_tab_top = draw_tab_top
	info_tab_bottom = info_tab_top + GetInfo(241)*2 + 2
	
	WindowText(GCW, FONT_ID_NORMAL, "[Have] [Keep] Defenses:", info_tab_left, info_tab_top, 0, 0, WINDOW_TEXT_COLOUR, true)
	info_tab_top = info_tab_top + GetInfo(241) + 2
	tempcount = 1
	for a,b in pairs(def) do
		if a == "groundwatch" then defname = "groundwat"
		elseif a == "chargeshield" then defname = "charshield"
		elseif a == "diamondskin" then defname = "dskin"
		elseif a == "stoneskin" then defname = "sskin"
		else
			defname = a
		end
		if tempcount == 1 then
			temphold1 = defname
			if b == 1 then
				temphold1t = "X"
			else
				temphold1t = " "
			end
			if def_keep[a] == 1 then
				temphold1k = "X"
			else
				temphold1k = " "
			end
			tempcount = tempcount + 1
		elseif tempcount == 2 then
			temphold2 = defname
			if b == 1 then
				temphold2t = "X"
			else
				temphold2t = " "
			end
			if def_keep[a] == 1 then
				temphold2k = "X"
			else
				temphold2k = " "
			end
			tempcount = tempcount + 1
		else
			temphold3 = defname
			if b == 1 then
				temphold3t = "X"
			else
				temphold3t = " "
			end
			if def_keep[a] == 1 then
				temphold3k = "X"
			else
				temphold3k = " "
			end
			WindowText(GCW, FONT_ID_NORMAL, "[" .. temphold1t .. "] [" .. temphold1k .. "] " .. string.proper(temphold1) .. " " .. string.rep(" ", 10-string.len(temphold1)) .. "[" .. temphold2t .. "] [" .. temphold2k .. "] " .. string.proper(temphold2) .. " " .. string.rep(" ", 10-string.len(temphold2)) .. "[" .. temphold3t .. "] [" .. temphold3k .. "] " .. string.proper(temphold3), info_tab_left, info_tab_top, 0, 0, WINDOW_TEXT_COLOUR, true)
			hotspot_left = info_tab_left + GetInfo(240)*1
			hotspot_right = hotspot_left + GetInfo(240)*1
			if def[temphold1] == 0 then
				WindowAddHotspot(GCW, "def-" .. temphold1, hotspot_left, info_tab_top, hotspot_right, info_tab_bottom, "MouseOver", "CancelMouseOver", "MouseDownDef", "CancelMouseDown", "MouseUp", "Turn on " .. string.proper(temphold1), 1, 0)
			end
			hotspot_left = hotspot_right + GetInfo(240)*3
			hotspot_right = hotspot_left + GetInfo(240)*1
			WindowAddHotspot(GCW, "defkeep-" .. temphold1, hotspot_left, info_tab_top, hotspot_right, info_tab_bottom, "MouseOver", "CancelMouseOver", "MouseDownDef", "CancelMouseDown", "MouseUp", "Keep up " .. string.proper(temphold1), 1, 0)
			hotspot_left = hotspot_left + GetInfo(240)*15
			hotspot_right = hotspot_left + GetInfo(240)*1
			if def[temphold2] == 0 then
				WindowAddHotspot(GCW, "def-" .. temphold2, hotspot_left, info_tab_top, hotspot_right, info_tab_bottom, "MouseOver", "CancelMouseOver", "MouseDownDef", "CancelMouseDown", "MouseUp", "Turn on " .. string.proper(temphold2), 1, 0)
			end
			hotspot_left = hotspot_right + GetInfo(240)*3
			hotspot_right = hotspot_left + GetInfo(240)*1
			WindowAddHotspot(GCW, "defkeep-" .. temphold2, hotspot_left, info_tab_top, hotspot_right, info_tab_bottom, "MouseOver", "CancelMouseOver", "MouseDownDef", "CancelMouseDown", "MouseUp", "Keep up " .. string.proper(temphold2), 1, 0)
			hotspot_left = hotspot_left + GetInfo(240)*15
			hotspot_right = hotspot_left + GetInfo(240)*1
			if def[temphold3] == 0 then
				WindowAddHotspot(GCW, "def-" .. temphold3, hotspot_left, info_tab_top, hotspot_right, info_tab_bottom, "MouseOver", "CancelMouseOver", "MouseDownDef", "CancelMouseDown", "MouseUp", "Turn on " .. string.proper(temphold3), 1, 0)
			end
			hotspot_left = hotspot_right + GetInfo(240)*3
			hotspot_right = hotspot_left + GetInfo(240)*1
			WindowAddHotspot(GCW, "defkeep-" .. temphold3, hotspot_left, info_tab_top, hotspot_right, info_tab_bottom, "MouseOver", "CancelMouseOver", "MouseDownDef", "CancelMouseDown", "MouseUp", "Keep up " .. string.proper(temphold3), 1, 0)
			info_tab_top = info_tab_top + GetInfo(241) + 2
			info_tab_bottom = info_tab_top + GetInfo(241) + 2
			tempcount = 1
		end
	end
	
	
	WindowText(GCW, FONT_ID_NORMAL, "", info_tab_left, info_tab_top, 0, 0, WINDOW_TEXT_COLOUR, true)
	info_tab_top = info_tab_top + GetInfo(241) + 2
	WindowText(GCW, FONT_ID_NORMAL, "", info_tab_left, info_tab_top, 0, 0, WINDOW_TEXT_COLOUR, true)
	
end
USA #3

function drawTabCuring()
end

function drawTabParry()
	-- This tab I want a body with limb hit counts on each limb, a red limb for what I'm parrying
	-- and to display what mode its using to parry.
	-- I want to be able to pick and chose which modes it uses and setup a static parry limb from
	-- here
end

function drawTabModes()
	-- List ALL system modes here with them being clickable to turn on and off.
	-- This means arena, breath, mass, moss, eventually parry, rebounding, meditating
	-- autotent, autoeye, bash, bals, autofashion, theft, ikill, todo, dor, bb, 
	-- 
end

function drawTabHelp()
end

function MouseOver(flags, hotspot_id)
end

function CancelMouseOver(flags, hotspot_id)
end

function MouseDown(flags, hotspot_id)
	currentTab = hotspot_id
	refresh()
end

function MouseDownDef(flags, hotspot_id)
	-- deconstruct hotspot_id, either defup-name or defkeep-name then set what it needs
	_, _, deftype, defname = string.find(hotspot_id, "(%a+)-(%a+)")
	if defname and deftype then
		if deftype == "defkeep" then
			if def_keep[defname] == 0 then
				def_keep[defname] = 1
			else
				def_keep[defname] = 0
			end
		else
			if def[defname] == 0 then
				def_set(defname)
			end
		end
	end
	
	refresh()
end

function MouseDownDefKeep(flags, hotspot_id)
	def_set(hotspot_id)
	def_keep[hotspot_id] = 1
	refresh()
end

function CancelMouseDown(flags, hotspot_id)
end

function MouseUp(flags, hotspot_id)
end

-- Execute on loadup.
windowinfo = movewindow.install (GCW, WINDOW_POSITION, 0)
	WindowCreate (GCW, 
                windowinfo.window_left, 
                windowinfo.window_top, 
                WINDOW_WIDTH,    
                WINDOW_HEIGHT,  
                windowinfo.window_mode, 
                windowinfo.window_flags, 
                WINDOW_BACKGROUND_COLOUR)
	
	-- Setup the fonts.
	WindowFont (GCW, FONT_ID_NORMAL, FONT_NAME, FONT_SIZE)
init()



------
------ End window control module stuff.
------