forked from derickd/moaigui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarious.lua
196 lines (154 loc) · 5.28 KB
/
various.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
--[[
Demo for a basic GUI system
This demonstrates creating various widgets programmatically.
]]
require "gui/support/class"
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"
ButtonHandler = class()
-- Respond to button 1 being clicked
function _handleButton1Click(event, data)
print("Button 1 Click")
end
-- Respond to button 2 being clicked
function ButtonHandler:handleButtonClick(event, data)
print("Button 2 Click")
end
function ButtonHandler:init()
end
-- Turn off the texture loading logging
-- MOAILogMgr.setLogLevel(MOAILogMgr.LOG_NONE)
-- Setup your basic window
local width = 320
local height = 480
MOAISim.openWindow("Various", width, height)
-- Create the GUI, passing in the dimensions of the screen
local g = gui.GUI(width, height)
-- Resource paths - 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"))
-- 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")
-- Show some simple text
local label = g:createLabel()
label:setPos(5, 3)
label:setDim(50, 5)
label:setText("Just some simple text")
-- A couple buttons
local button = g:createButton()
button:setPos(5, 10)
button:setDim(30, 10)
button:setText("button 1")
-- -- Register the button click event for this widget. In this case, we are registering to a
-- -- standard function, so the second parameter is 'nil'
button:registerEventHandler(button.EVENT_BUTTON_CLICK, nil, _handleButton1Click)
local buttonHandler = ButtonHandler()
local button = g:createButton()
button:setPos(45, 10)
button:setDim(30, 20)
button:setText("image button")
button:setNormalImage(resources.getPath("cathead.png"), 1, 1, 1, 1)
button:setHoverImage(resources.getPath("cathead.png"), 1, 0, 0, 1)
button:setPushedImage(resources.getPath("cathead.png"), 0, 1, 0, 1)
button:setDisabledImage(resources.getPath("cathead.png"), 0, 0, 1, 1)
-- Register the button click event for this widget. This time, we are registering to a
-- method of a class, so the second parameter is the class instance, and the function is
-- referenced by name (so the object can call it by table index)
button:registerEventHandler(button.EVENT_BUTTON_CLICK, buttonHandler, "handleButtonClick")
-- Static image
local image = g:createImage()
image:setPos(5, 23)
image:setDim(22, 15)
image:setText("Image")
image:setImage(resources.getPath("cathead.png"), 1, 1, 1, 1)
image:setTextAlignment(image.TEXT_ALIGN_CENTER, image.TEXT_ALIGN_BOTTOM)
-- -- Progress bar.
-- local progress = g:createProgressBar()
-- progress:setPos(45, 33)
-- progress:setDim(30, 5)
-- progress:setProgress(40)
-- A couple radio buttons
for i = 1, 3 do
local radio = g:createRadioButton()
radio:setPos((i - 1) * 25 + 5, 40)
radio:setDim(20, 4)
radio:setText("Radio")
-- Assign all of these to the same group, so only one in this group can be selected at a time
radio:setGroup(1)
end
-- Some check boxes
for i = 1, 3 do
local check = g:createCheckBox()
check:setPos((i - 1) * 25 + 5, 50)
check:setDim(20, 4)
check:setText("Check")
end
local label = g:createLabel()
label:setPos(5, 60)
label:setDim(30, 5)
label:setText("Slide Me")
-- A horizontal slider, with its current value displayed below the slider
local slider = g:createHorzSlider()
slider:setPos(35, 60)
slider:setDim(50, 5)
slider:setRange(1, 20)
local label = g:createLabel()
label:setPos(5, 70)
label:setDim(30, 5)
label:setText("Enter Text")
-- Allows entry of keyboard input
local edit = g:createEditBox()
edit:setPos(35, 70)
edit:setDim(50, 5)
-- 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)
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)