Repository navigation
Expand file tree
/
Copy pathmain_original.lua
More file actions
147 lines (119 loc) · 7.25 KB
/
Copy pathmain_original.lua
File metadata and controls
147 lines (119 loc) · 7.25 KB
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
--- ************************************************************************************************************************************************************************
---
--- Name : main.lua
--- Purpose : Simple demo of the bitmap strings.
--- Created: 30 April 2014
--- Author: Paul Robson (paul@robsons.org.uk)
--- License: MIT
---
--- ************************************************************************************************************************************************************************
display.setStatusBar(display.HiddenStatusBar)
fm = require("system.fontmanager") -- get an instance of the font manager.
fm.FontManager:setEncoding("utf8") -- set expected encoding to UTF-8 (default is Unicode character set.)
local bgr = display.newRect( 0,0,320,480) -- blue background
bgr:setFillColor(0,0,0.4)
bgr.anchorX,bgr.anchorY = 0,0
local msg = "An{brown}other {1,0,1}line\rwit{}h\ra curve" .. string.char(0xC3,0xBE,0x2A) -- this is A~ 3/4 in Unicode, and a circle with a vertical line in UTF-8.
msg = msg .. string.char(0xE2,0x82,0xAC,0x21)
-- msg = msg .. string.char(0xF0,0xA4,0xAD,0xA2,0x45)
-- the curly brackets set tinting colours.
local str = fm.BitmapString:new("demofont") -- create a string OOP method.
str:moveTo(160,240):setFontSize(60)
str:setText(msg) -- set the text
--str:setDirection(180) -- write it backwards (why did I do this ?)
str:setAnchor(0.5,0) -- anchor top centre,
--str:setModifier("iscale") -- shape with a curve
str:setVerticalSpacing(1):animate(4) -- animate it - if you comment this out it will curve but not animate
-- the number is a speed scalar.
str:setTintColor(1,1,0) -- apply a tint to it.
str2 = display.newBitmapText("Bye !",0,0,"font2",45) -- or we can do it Corona style ! - YAY !!!!
-- *BUT* it does not have compatible methods. So you have to use moveTo()
str2:setAnchor(0,0):setDirection(90) -- and setAnchor() for example, rather than accessing members directly.
local str3 = fm.BitmapString:new("font2",str.DEFAULT_SIZE): -- a third string, created using the constructor, showing chaining.
moveTo(160,400):setText("Total..") -- could use fm.BitmapString.DEFAULT_SIZE
-- str3:setModifier(fm.Modifiers.WobbleModifier:new(2)) -- a more violent wobble with a new wobble modifier instance
-- str3:setModifier(SimpleCurveModifier:new(0,180,4,2)) -- simple curves and scales with a different part of the trigonometrical curve
-- str3:setModifier(SimpleCurveScaleModifier:new(0,180,4,2))
str3:setModifier("wobble") -- or just wobble.
str3:animate() -- make this one animate.
print(str2:getText(),str3:getText())
--
-- A sample function type modifier, okay , so I chose 360 because it saves me scaling the rotation :)
--
function pulser(modifier, cPos, info)
local w = math.floor(info.elapsed/360) % info.length + 1 -- every 360ms change character, creates a number 1 .. length
if info.index == w then -- are we scaling this character
local newScale = 1 + (info.elapsed % 360) / 360 -- calculate the scale zoom - make it 2- rather than 1+, it goes backwards
modifier.xScale,modifier.yScale = newScale,newScale -- scale it up
--modifier.rotation = info.elapsed % 360 -- this looks ridiculous, but it's interesting
modifier.tint.red = 0 -- so, we set the tinting for that one as well.
end
end
--
-- Identical but pulses lines, so uses lineIndex and lineCount instead
--
function linePulser(modifier, cPos, info)
local w = math.floor(info.elapsed/360) % info.lineCount + 1
if info.lineIndex == w then
local newScale = 1 + (info.elapsed % 360) / 360
modifier.xScale,modifier.yScale = newScale,newScale
-- modifier.rotation = info.elapsed % 360
modifier.tint.red = 0
end
end
--
-- Identical but pulses words, so uses wordIndex and wordCount instead
--
-- tests wordCount because this could be zero, causing divide by zero.
--
function wordPulser(modifier, cPos, info)
if info.wordCount == 0 then return end
local w = math.floor(info.elapsed/360) % info.wordCount + 1
if info.wordIndex == w then
local newScale = 1 + (info.elapsed % 360) / 360
modifier.xScale,modifier.yScale = newScale,newScale
-- modifier.rotation = info.elapsed % 360
modifier.tint.red = 0
end
end
-- str:setModifier(linePulser) -- pick something to run, comment both out, it's a curve/scale
-- str:setModifier(wordPulser)
local str4 = display.newBitmapText("pulse",160,240,"retrofont",80) -- create a new string using Corona method.
str4:setModifier(pulser):animate() -- make it use the above modifier, and animate it.
--str4:setDirection(180) -- backwards
-- str4:setText("Hello\nWorld !") -- you can change the text, strings aren't immutable.
local demoTarget = {} -- something to send an event to.
function demoTarget.tap(event) print("tap",event) end
str4:addEventListener( "tap", demoTarget ) -- print 'tap' if you tap it.
-- str4:removeEventListener("tap")
--str4:remove() -- remove kills it. str4 will be an empty object.
--str4:remove() -- multiple removes() aere fine
-- for _,n in pairs(str4) do print("str4",_,n) end
local t = 3000 -- run over 8 seconds.
-- str:getView().isVisible = false str2:getView().isVisible = false str3:getView().isVisible = false str4:getView().isVisible = false
--
-- Animate using the usual Corona methods.
--
-- Remove the comment, the screen clears at the end, this is a 'tidy everything up' routine.
--
transition.to(str:getView(),{ time = t,rotation = 0, y = 0, xScale = 1, yScale = 1, rotation = 360*2,
onComplete = function()
-- fm.FontManager:clearText()
end })
transition.to(str2:getView(), { time = t,x = 300, y = 400, alpha = 0.4,xScale = 0.4,yScale = 0.4 })
transition.to(str3:getView(), { time = t,rotation = 360 })
transition.to(str4:getView(), { time = t, xScale = 2,yScale = 2})
--[[
local composer = require( "composer" )
composer.loadScene("demoscene")
print(_G.scene,_G.scene.view,_G.sceneText,getmetatable(_G.sceneText:getView()),_G.sceneText:getView().numChildren,_G.sceneText:getView().isVisible,_G.owner.isVisible,_G.owner.numChildren)
--composer.gotoScene("demoscene")
composer.removeScene("demoscene")
print(_G.scene,_G.scene.view,_G.sceneText,getmetatable(_G.sceneText:getView()),_G.sceneText:getView().numChildren,_G.sceneText:getView().isVisible,_G.owner.isVisible,_G.owner.numChildren)
--]]
-- changes made (that have retrospective effect - there are improvements as well !)
-- a) setScale() no longer functions. Adjust font size to suit, or scale overall.
-- b) there is no FontManager object, really, though setEncoding() and setAnimationRate() still work.
-- c) curve and scale have been switched so they are the right way round. Previously they were 'visual' opposites.
-- d) FontManager:Clear() does not exist
-- e) You cannot directly subclass BitMapString, because it is now a mixin.