Created
October 2, 2017 00:21
-
-
Save kyzentun/1541709f5d7c7ffe46c69a3be3ecd970 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local str_digit_to_frame= {} | |
for i= 0, 9 do | |
str_digit_to_frame[tostring(i)]= i | |
end | |
local function sprite_number(digits, commands) | |
local parts= {} | |
local spacing= 32 | |
local midpoint= spacing * digits * .5 | |
local frame= Def.ActorFrame(commands) | |
local internal= false | |
local part_holder= Def.ActorFrame{ | |
InitCommand= function(self) | |
internal= self | |
end, | |
} | |
frame[#frame+1]= part_holder | |
frame.SetCommand= function(self, number) | |
-- 5.1-new or 5.0.x compat | |
if type(number) == "table" then | |
number= number[1] | |
end | |
-- reverse the string because part[1] is the ones digit. | |
local str= ("%i"):format(math.floor(number)):reverse() | |
local last= math.min(#str, #parts) | |
for sid= 1, last do | |
local part= parts[sid] | |
local was_vis= part:GetVisible() | |
local prev_state= part:GetState() | |
local new_state= str_digit_to_frame[str:sub(sid, sid)] | |
part:setstate(new_state) | |
part:visible(true) | |
if not was_vis or prev_state ~= new_state then | |
part:playcommand("Bounce") | |
end | |
end | |
for pid= last+1, #parts do | |
local part= parts[pid] | |
part:visible(false) | |
end | |
local width= last * spacing | |
internal:x((width * .5) + midpoint) | |
end | |
for id= 1, digits do | |
part_holder[#part_holder+1]= Def.Sprite{ | |
Texture= THEME:GetPathG("", "grades/number_grades"), | |
InitCommand= function(self) | |
parts[id]= self | |
self:animate(false) | |
-- lowest id digit is the ones place | |
:x((id-1) * -spacing) | |
:visible(false) | |
end, | |
BounceCommand= function(self) | |
self:finishtweening() | |
:spring(.2):y(-8) | |
:spring(.2):y(0) | |
end, | |
} | |
end | |
return frame | |
end | |
local curr_value= 0 | |
local sprnum= false | |
local function input(event) | |
if event.type == "InputEventType_Release" then return end | |
local button= event.DeviceInput.button | |
if button == "DeviceButton_f" then | |
curr_value= math.max(0, curr_value - 1) | |
sprnum:playcommand("Set", curr_value) | |
elseif button == "DeviceButton_j" then | |
curr_value= math.min(999999, curr_value + 1) | |
sprnum:playcommand("Set", curr_value) | |
end | |
end | |
local frame= Def.ActorFrame{ | |
quaid(_screen.cx, _screen.cy, _screen.w, _screen.h, {.7, .7, .7, .7}), | |
OnCommand= function(self) | |
sprnum= self:GetChild("sprnum") | |
SCREENMAN:GetTopScreen():AddInputCallback(input) | |
sprnum:playcommand("Set", curr_value) | |
end, | |
sprite_number(6, {Name= "sprnum", InitCommand= function(self) self:Center() end}), | |
} | |
return frame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment