forked from derickd/moaigui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgetlistfromlayout.lua
126 lines (97 loc) · 3.62 KB
/
widgetlistfromlayout.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
--[[
Demo for a basic GUI system
This demonstrates loading a widget list from a GUI layout file.
]]
local gui = require "gui/gui"
local resources = require "gui/support/resources"
local filesystem = require "gui/support/filesystem"
local inputconstants = require "gui/support/inputconstants"
local layermgr = require "layermgr"
-- Turn off the texture loading logging
MOAILogMgr.setLogLevel(MOAILogMgr.LOG_NONE)
-- Setup your basic window
local width = 320
local height = 480
MOAISim.openWindow("Widget List From Layout", width, height)
-- Create the GUI, passing in the dimensions of the screen
local g = gui.GUI(width, height)
-- Search through these for specified resources
g:addToResourcePath(filesystem.pathJoin("resources", "fonts"))
g:addToResourcePath(filesystem.pathJoin("resources", "gui"))
g:addToResourcePath(filesystem.pathJoin("resources", "media"))
g:addToResourcePath(filesystem.pathJoin("resources", "themes"))
g:addToResourcePath(filesystem.pathJoin("resources", "layouts"))
-- Add the gui layer to the rendering stack, making sure its always high up on the
-- layer stack
layermgr.addLayer("gui", 99999, g:layer())
-- This sets up the theme to be used for widgets. If images aren't set
-- manually for a widget, the system looks to the theme for the images
-- to use.
g:setTheme("basetheme.lua")
-- The font used for text
g:setCurrTextStyle("default")
-- Load the widgets from the specified file
-- Parameter 1: filename of the layout
-- Parameter 2: (optional) a prefix added to the name (from the layout file) of each widget; this
-- is to help avoid name collisions
-- Returns three values, but the second is the only one we're really interested in
local roots, widgets, groups = g:loadLayout(resources.getPath("widgetlistlayout.lua"))
-- Check if the widget list is a part of the layout file
if (nil ~= widgets.widgetlist1) then
local widgetList = widgets.widgetlist1.window
-- Add some rows, and fill it with some junk data
for i = 1, 30 do
local row = widgetList:addRow()
-- The return from getCell is the widget created by setColumnWidget, so the normal
-- functionality for the widget is available.
row:getCell(1):setText("Name " .. i)
row:getCell(2):setText(tostring(i))
end
end
-- Callback functions for input
-- When the user provides input, it must be sent into the GUI system so that it can
-- be handled properly.
function onKeyboardEvent(key, down)
if (down == true) then
g:injectKeyDown(key)
else
g:injectKeyUp(key)
-- Get the current selections made by the user, and print them out
local selections = widgetList:getSelections()
if (nil == selections) then return end
print("---------------------")
for i = 1, #selections do
print(selections[i])
end
end
end
MOAIInputMgr.device.keyboard:setCallback ( onKeyboardEvent )
function onPointerEvent(x, y)
g:injectMouseMove(x, y)
end
function onMouseLeftEvent(down)
if (down) then
g:injectMouseButtonDown(inputconstants.LEFT_MOUSE_BUTTON)
else
g:injectMouseButtonUp(inputconstants.LEFT_MOUSE_BUTTON)
end
end
function onMouseMiddleEvent(down)
if (down) then
g:injectMouseButtonDown(inputconstants.MIDDLE_MOUSE_BUTTON)
else
g:injectMouseButtonUp(inputconstants.MIDDLE_MOUSE_BUTTON)
end
end
function onMouseRightEvent(down)
if (down) then
g:injectMouseButtonDown(inputconstants.RIGHT_MOUSE_BUTTON)
else
g:injectMouseButtonUp(inputconstants.RIGHT_MOUSE_BUTTON)
end
end
-- Register the callbacks for input
MOAIInputMgr.device.pointer:setCallback(onPointerEvent)
MOAIInputMgr.device.mouseLeft:setCallback(onMouseLeftEvent)
MOAIInputMgr.device.mouseMiddle:setCallback(onMouseMiddleEvent)
MOAIInputMgr.device.mouseRight:setCallback(onMouseRightEvent)