1
+ -- New in this version
2
+
3
+ -- Call cursor.loadCursor with the path to the cursor and store
4
+ -- the return value, which is a pointer to the cursor resource
5
+
6
+ -- Call cursor.setCursor and provide the pointer returned
7
+ -- by cursor.loadCursor to set the cursor for the window
8
+
1
9
local widget = require (' widget' )
2
10
local cursor = require (' plugin.hwcursor' )
3
11
4
- local hidden = false
5
12
cursor .initPlugin ()
6
13
7
- -- New in this version
8
-
9
- -- Call cursor.loadCursor the first time with the path to the cursor
10
- -- and store the return value, which is a pointer to the cursor resource (preload step)
11
-
12
- -- Call cursor.loadCursor the second time with the previously returned pointer
13
- -- to actually set the cursor (usage step)
14
+ local visible = false
14
15
15
16
-- Cursors must be preloaded before being used
16
17
local cursors = {
@@ -24,9 +25,10 @@ display.newText(
24
25
x = display .contentCenterX ,
25
26
y = 50 ,
26
27
width = 300 ,
28
+ fontSize = 20 ,
27
29
align = ' center' ,
28
30
font = native .systemFont ,
29
- text = ' Press numbers 1-0 and keys q-r on your keyboard for windows-provided cursors '
31
+ text = ' Hardware Cursor Plugin for Windows OS '
30
32
}
31
33
)
32
34
@@ -51,9 +53,8 @@ widget.newButton(
51
53
shape = ' roundedRect' ,
52
54
x = display .contentCenterX ,
53
55
y = display .contentCenterY - 50 ,
54
- label = ' Custom cursor 1' ,
55
- -- Set cursor using stored pointer
56
- onRelease = function () cursor .loadCursor (cursors .cursor1 ) end ,
56
+ label = ' Custom static cursor' ,
57
+ onRelease = function () cursor .setCursor (cursors .cursor1 ) end ,
57
58
labelColor = { default = { 1 , 1 , 1 }, over = { 0 , 0 , 0 , 0.5 } },
58
59
fillColor = { default = { 1 , 0.2 , 0.5 , 0.7 }, over = { 1 , 0.2 , 0.5 , 1 } }
59
60
}
@@ -66,9 +67,8 @@ widget.newButton(
66
67
shape = ' roundedRect' ,
67
68
x = display .contentCenterX ,
68
69
y = display .contentCenterY ,
69
- label = ' Custom cursor 2' ,
70
- -- Set cursor using stored pointer
71
- onRelease = function () cursor .loadCursor (cursors .cursor2 ) end ,
70
+ label = ' Custom animated cursor' ,
71
+ onRelease = function () cursor .setCursor (cursors .cursor2 ) end ,
72
72
labelColor = { default = { 1 , 1 , 1 }, over = { 0 , 0 , 0 , 0.5 } },
73
73
fillColor = { default = { 1 , 0.2 , 0.5 , 0.7 }, over = { 1 , 0.2 , 0.5 , 1 } }
74
74
}
@@ -81,9 +81,8 @@ widget.newButton(
81
81
shape = ' roundedRect' ,
82
82
x = display .contentCenterX ,
83
83
y = display .contentCenterY + 50 ,
84
- label = ' Custom cursor 3' ,
85
- -- Set cursor using stored pointer
86
- onRelease = function () cursor .loadCursor (cursors .cursor3 ) end ,
84
+ label = ' Custom huge cursor' ,
85
+ onRelease = function () cursor .setCursor (cursors .cursor3 ) end ,
87
86
labelColor = { default = { 1 , 1 , 1 }, over = { 0 , 0 , 0 , 0.5 } },
88
87
fillColor = { default = { 1 , 0.2 , 0.5 , 0.7 }, over = { 1 , 0.2 , 0.5 , 1 } }
89
88
}
@@ -98,12 +97,8 @@ widget.newButton(
98
97
y = display .contentCenterY + 100 ,
99
98
label = ' Show/Hide cursor' ,
100
99
onRelease = function ()
101
- if hidden then
102
- cursor .showCursor ()
103
- else
104
- cursor .hideCursor ()
105
- end
106
- hidden = not hidden
100
+ cursor .setVisibility (visible )
101
+ visible = not visible
107
102
end ,
108
103
labelColor = { default = { 1 , 1 , 1 }, over = { 0 , 0 , 0 , 0.5 } },
109
104
fillColor = { default = { 1 , 0.2 , 0.5 , 0.7 }, over = { 1 , 0.2 , 0.5 , 1 } }
@@ -119,11 +114,7 @@ widget.newButton(
119
114
y = display .contentCenterY + 150 ,
120
115
label = ' Exit application' ,
121
116
onRelease = function ()
122
- -- Destroy all cursors before exiting
123
- for k , v in pairs (cursors ) do
124
- cursor .destroyCursor (v )
125
- cursors [k ] = nil
126
- end
117
+ -- Free the plugin before calling "native.requestExit()"
127
118
cursor .freePlugin ()
128
119
native .requestExit ()
129
120
end ,
@@ -132,90 +123,13 @@ widget.newButton(
132
123
}
133
124
)
134
125
135
- --[[
136
- widget.newButton(
137
- {
138
- width = 200,
139
- height = 30,
140
- shape = 'roundedRect',
141
- x = display.contentCenterX,
142
- y = display.contentCenterY + 200,
143
- label = 'Destroy cursor 1',
144
- onRelease = function()
145
- cursor.destroyCursor(cursors.cursor1)
146
- cursors.cursor1 = nil
147
- end,
148
- labelColor = { default = { 1, 1, 1 }, over = { 0, 0, 0, 0.5 } },
149
- fillColor = { default = { 1, 0.2, 0.5, 0.7 }, over = { 1, 0.2, 0.5, 1 } }
150
- }
151
- )
152
-
153
- widget.newButton(
154
- {
155
- width = 200,
156
- height = 30,
157
- shape = 'roundedRect',
158
- x = display.contentCenterX,
159
- y = display.contentCenterY + 250,
160
- label = 'Destroy cursor 2',
161
- onRelease = function()
162
- cursor.destroyCursor(cursors.cursor2)
163
- cursors.cursor2 = nil
164
- end,
165
- labelColor = { default = { 1, 1, 1 }, over = { 0, 0, 0, 0.5 } },
166
- fillColor = { default = { 1, 0.2, 0.5, 0.7 }, over = { 1, 0.2, 0.5, 1 } }
167
- }
168
- )
169
-
170
- widget.newButton(
171
- {
172
- width = 200,
173
- height = 30,
174
- shape = 'roundedRect',
175
- x = display.contentCenterX,
176
- y = display.contentCenterY + 300,
177
- label = 'Destroy cursor 3',
178
- onRelease = function()
179
- cursor.destroyCursor(cursors.cursor3)
180
- cursors.cursor3 = nil
181
- end,
182
- labelColor = { default = { 1, 1, 1 }, over = { 0, 0, 0, 0.5 } },
183
- fillColor = { default = { 1, 0.2, 0.5, 0.7 }, over = { 1, 0.2, 0.5, 1 } }
184
- }
185
- )
186
- ]] --
187
-
188
- Runtime :addEventListener (' key' ,
126
+ Runtime :addEventListener (' system' ,
189
127
function (event )
190
- if event .phase == ' up' then
191
- if event .keyName == ' 1' then
192
- cursor .loadWinCursor (cursor .ARROW )
193
- elseif event .keyName == ' 2' then
194
- cursor .loadWinCursor (cursor .POINTER )
195
- elseif event .keyName == ' 3' then
196
- cursor .loadWinCursor (cursor .CROSSHAIR )
197
- elseif event .keyName == ' 4' then
198
- cursor .loadWinCursor (cursor .IBEAM )
199
- elseif event .keyName == ' 5' then
200
- cursor .loadWinCursor (cursor .NOTALLOWED )
201
- elseif event .keyName == ' 6' then
202
- cursor .loadWinCursor (cursor .RESIZE )
203
- elseif event .keyName == ' 7' then
204
- cursor .loadWinCursor (cursor .WAIT )
205
- elseif event .keyName == ' 8' then
206
- cursor .loadWinCursor (cursor .HELP )
207
- elseif event .keyName == ' 9' then
208
- cursor .loadWinCursor (cursor .BUSY )
209
- elseif event .keyName == ' 0' then
210
- cursor .loadWinCursor (cursor .RESIZENESW )
211
- elseif event .keyName == ' q' then
212
- cursor .loadWinCursor (cursor .RESIZENS )
213
- elseif event .keyName == ' w' then
214
- cursor .loadWinCursor (cursor .RESIZENWSE )
215
- elseif event .keyName == ' e' then
216
- cursor .loadWinCursor (cursor .RESIZEWE )
217
- elseif event .keyName == ' r' then
218
- cursor .loadWinCursor (cursor .UPARROW )
128
+ if event .type == ' applicationExit' then
129
+ -- Free all cursor pointers before application exit (best practice)
130
+ for k , v in pairs (cursors ) do
131
+ cursor .freeCursor (v )
132
+ cursors [k ] = nil
219
133
end
220
134
end
221
135
end
0 commit comments