Difference between revisions of "Lua API:Interface"

From The Powder Toy
Jump to: navigation, search
(ProgressBar, Slider and Checkbox)
(Example)
Line 181: Line 181:
 
  nil interface.closeWindow(Window newWindow)
 
  nil interface.closeWindow(Window newWindow)
 
Pop a Window off the top of the modal stack. If the given Window is not the top item in the stack, this will have no effect,
 
Pop a Window off the top of the modal stack. If the given Window is not the top item in the stack, this will have no effect,
 +
 +
== Example ==
 +
This code has examples of some of the features of the interface API, it shows a window with various components with some testing behaviour.
 +
<syntaxhighlight lang="lua">
 +
-- Test Window
 +
local testWindow = Window:new(-1, -1, 300, 200)
 +
 +
local currentY = 10
 +
 +
--Example label
 +
local testLabel = Label:new(10, currentY, (select(1, testWindow:size())/2)-20, 16, "This is a test label")
 +
 +
--Example button
 +
local buttonPresses = 1
 +
currentY = currentY + 20
 +
local testButton = Button:new(10, currentY, (select(1, testWindow:size())/2)-20, 16, "This is a test button")
 +
testButton:enabled(false)
 +
testButton:action(
 +
function(sender)
 +
sender:text("Pressed " .. buttonPresses .. " times")
 +
buttonPresses = buttonPresses + 1
 +
end
 +
)
 +
 +
--Example Textbox
 +
currentY = currentY + 20
 +
local textboxInfo = Label:new(10+((select(1, testWindow:size())/2)-20), currentY, (select(1, testWindow:size())/2)-20, 16, "0 characters")
 +
local testTextbox = Textbox:new(10, currentY, (select(1, testWindow:size())/2)-20, 16, "", "[place text here]")
 +
testTextbox:onTextChanged(
 +
function(sender)
 +
textboxInfo:text(sender:text():len().." characters");
 +
end
 +
)
 +
 +
--Example Checkbox
 +
currentY = currentY + 20
 +
local testCheckbox = Checkbox:new(10, currentY, (select(1, testWindow:size())/2)-20, 16, "Unchecked");
 +
testCheckbox:action(
 +
function(sender, checked)
 +
if(checked) then
 +
sender:text("Checked")
 +
else
 +
sender:text("Unchecked")
 +
end
 +
testButton:enabled(checked);
 +
end
 +
)
 +
 +
--Example progress bar
 +
currentY = currentY + 20
 +
local testProgressBar = ProgressBar:new(10, currentY, (select(1, testWindow:size())/2)-20, 16, 0, "Slider: 0");
 +
 +
--Example slider
 +
currentY = currentY + 20
 +
local testSlider = Slider:new(10, currentY, (select(1, testWindow:size())/2)-20, 16, 10);
 +
testSlider:onValueChanged(
 +
function(sender, value)
 +
testProgressBar:progress(value * 10)
 +
testProgressBar:status("Slider: " .. value)
 +
end
 +
)
 +
 +
-- Close button
 +
local closeButton = Button:new(10, select(2, testWindow:size())-26, 100, 16, "Close")
 +
 +
closeButton:action(function() interface.closeWindow(testWindow) end)
 +
 +
testWindow:onTryExit(function() interface.closeWindow(testWindow) end) -- Allow the default exit events
 +
testWindow:onMouseMove(
 +
function(x, y, dx, dy)
 +
testLabel:text("Mouse: "..x..", "..y)
 +
end
 +
)
 +
 +
testWindow:addComponent(testLabel)
 +
testWindow:addComponent(testButton)
 +
testWindow:addComponent(testTextbox)
 +
testWindow:addComponent(testCheckbox)
 +
testWindow:addComponent(testProgressBar)
 +
testWindow:addComponent(testSlider)
 +
testWindow:addComponent(textboxInfo)
 +
testWindow:addComponent(closeButton)
 +
 +
interface.showWindow(testWindow)
 +
</syntaxhighlight>

Revision as of 11:37, 17 September 2012

The Interface API includes objects for UI components such as buttons, labels and checkboxes and methods for access to the very primitive window manager and input events.

Classes

Component

Abstract class, no constructor

Component:visible

boolean Component:visible()

Returns the visibility of the component

nil Component:visible(boolean visible)

Sets the visibility of the component

Component:size

number, number Component:size()

Returns the width and height of the component

nil Component:size(number width, number height)

Sets the size of the component to be width by height

Component:position

number, number Component:position()

Returns the x and y coord of the component

nil Component:position(number x, number y)

Sets the position of the component to be x, y

Button

Button Button:new(number x, number y, number width, number height, [string text = "", [string tooltip = ""]])

Extends Component, fires "action" when clicked

Button:action

nil Button:action(function(sender) actionListener)

Sets the listener for button actions Example:

local newButton = Button:new(10, 10, 100, 17, "Press to change text")
newButton:action(function(sender) sender:text("Text changed") end)
interface.addComponent(newButton)

Button:text

string Button:text()

Returns the button text

nil Button:text(string text)

Sets the text of the button

Button:enabled

boolean Button:enabled()

Returns the enabled state of the button

nil Button:enabled(boolean enabled)

Sets the enabled state of the button

ProgressBar

ProgressBar ProgressBar:new(number x, number y, number width, number height, number progress, string status)

Extends Component, used to indicate progress for long running tasks

ProgressBar:progress

number ProgressBar:progress()

Returns the progress value

nil ProgressBar:progress(number progress)

Sets the progress value

Progress ranges from 0 to 100, but a special case of -1 will change the behaviour of the progress bar to intermediate (constantly scrolling to indicate progress)

ProgressBar:status

string ProgressBar:status()

Returns the progress bar status

nil ProgressBar:status(string status)

Sets the progress bar status

Status is simple a text representation of the current action being performed, for example "Working" or just a percentage

Slider

Slider Slider:new(number x, number y, number width, number height, [string steps = ""])

Extends Component, fires "onValueChanged" when the value is changed (i.e used by the user)

Slider:action

nil Slider:onValueChanged(function(sender, value) actionListener)

Sets the listener for slider actions

Slider:value

number Slider:value()

Returns the value of the slider

nil Slider:value(number value)

Sets the value of the slider

Slider:steps

number Slider:steps()

Returns the number of steps the slider has

nil Slider:steps(number steps)

Sets the number of steps for the slider

Checkbox

Checkbox Checkbox:new(number x, number y, number width, number height, [string text = ""])

Extends Component, fires "onValueChanged" when the checkbox is checked or unchecked

Checkbox:action

nil Checkbox:action(function(sender, checked) actionListener)

Sets the listener for checkbox actions

Checkbox:text

string Checkbox:text()

Returns the checkbox text

nil Checkbox:text(string text)

Sets the text of the checkbox

Checkbox:checked

boolean Checkbox:checked()

Returns the checked state of the checkbox

nil Checkbox:checked(boolean checked)

Sets the checked state of the checkbox

Label

Label Label:new(number x, number y, number width, number height, [string text = ""])

Extends Component, is a simple selectable, readonly text field

Label:text

string Label:text()

Returns the label text

nil Label:text(string text)

Sets the text of the label

Textbox

Textbox Textbox:new(number x, number y, number width, number height [, string text = "" [, string placeholder = "" ]])

Extends Component, is a text input field, the placeholder text is shown if the component is no focused and contains no text

Textbox:onTextChanged

nil Textbox:onTextChanged(function(sender) textChangedListener)

Sets the listener for text changed actions

Textbox:text

string Textbox:text()

Returns the text in the field

nil Textbox:text(string text)

Sets the text of the field

Textbox:readonly

boolean Textbox:readonly()

Returns the readonly status of the field.

nil Textbox:readonly(boolean readonly)

Sets the readonly status of the field.

Window

Window Window:new(number x, number y, number width, number height)

A modal form to display components, using -1 for either x or y values will centre the Window on that axis.

Window:addComponent

nil Window:addComponent(Component newComponent)

Add a component to the window (The component must not have already been added to another Window object)

Window:removeComponent

nil Window:removeComponent(Component newComponent)

Remove a component from the window

Methods

interface.addComponent

nil interface.addComponent(Component newComponent)

Add a component to master game window.

interface.removeComponent

nil interface.removeComponent(Component newComponent)

Remove a component from the master game window.

interface.showWindow

nil interface.showWindow(Window newWindow)

Push a Window into the top of the modal stack

interface.closeWindow

nil interface.closeWindow(Window newWindow)

Pop a Window off the top of the modal stack. If the given Window is not the top item in the stack, this will have no effect,

Example

This code has examples of some of the features of the interface API, it shows a window with various components with some testing behaviour.

-- Test Window
local testWindow = Window:new(-1, -1, 300, 200)

local currentY = 10

--Example label
local testLabel = Label:new(10, currentY, (select(1, testWindow:size())/2)-20, 16, "This is a test label")

--Example button
local buttonPresses = 1
currentY = currentY + 20
local testButton = Button:new(10, currentY, (select(1, testWindow:size())/2)-20, 16, "This is a test button")
testButton:enabled(false)
testButton:action(
	function(sender)
		sender:text("Pressed " .. buttonPresses .. " times")
		buttonPresses = buttonPresses + 1
	end
)

--Example Textbox
currentY = currentY + 20
local textboxInfo = Label:new(10+((select(1, testWindow:size())/2)-20), currentY, (select(1, testWindow:size())/2)-20, 16, "0 characters")
local testTextbox = Textbox:new(10, currentY, (select(1, testWindow:size())/2)-20, 16, "", "[place text here]")
testTextbox:onTextChanged(
	function(sender)
		textboxInfo:text(sender:text():len().." characters");
	end
)

--Example Checkbox
currentY = currentY + 20
local testCheckbox = Checkbox:new(10, currentY, (select(1, testWindow:size())/2)-20, 16, "Unchecked");
testCheckbox:action(
	function(sender, checked)
		if(checked) then
			sender:text("Checked")
		else
			sender:text("Unchecked")
		end
		testButton:enabled(checked);
	end
)

--Example progress bar
currentY = currentY + 20
local testProgressBar = ProgressBar:new(10, currentY, (select(1, testWindow:size())/2)-20, 16, 0, "Slider: 0");

--Example slider
currentY = currentY + 20
local testSlider = Slider:new(10, currentY, (select(1, testWindow:size())/2)-20, 16, 10);
testSlider:onValueChanged(
	function(sender, value)
		testProgressBar:progress(value * 10)
		testProgressBar:status("Slider: " .. value)
	end
)

-- Close button
local closeButton = Button:new(10, select(2, testWindow:size())-26, 100, 16, "Close")

closeButton:action(function() interface.closeWindow(testWindow) end)

testWindow:onTryExit(function() interface.closeWindow(testWindow) end) -- Allow the default exit events
testWindow:onMouseMove(
	function(x, y, dx, dy)
		testLabel:text("Mouse: "..x..", "..y)
	end
)

testWindow:addComponent(testLabel)
testWindow:addComponent(testButton)
testWindow:addComponent(testTextbox)
testWindow:addComponent(testCheckbox)
testWindow:addComponent(testProgressBar)
testWindow:addComponent(testSlider)
testWindow:addComponent(textboxInfo)
testWindow:addComponent(closeButton)

interface.showWindow(testWindow)