Skip to content

Instantly share code, notes, and snippets.

@trevordevore
Created February 23, 2015 17:45
Show Gist options
  • Save trevordevore/0983b633ef2d1c54ec1d to your computer and use it in GitHub Desktop.
Save trevordevore/0983b633ef2d1c54ec1d to your computer and use it in GitHub Desktop.
rotatingfont.mlc
-- declaring extension as widget, followed by identifier
widget com.livecode.extensions.devore.rotatingfont
--
-- adding metadata to ensure the extension displays correctly in livecode
metadata title is "Rotating Font"
metadata author is "Trevor DeVore"
metadata version is "1.0.1"
--
-- dependency declarations
use com.livecode.canvas
use com.livecode.widget
use com.livecode.engine
--
private variable mFontSize as number
private variable mRotation as number
private variable mMargin as number
private variable mRotateBy as integer
private variable mFrequency as number
property margin get mMargin set mMargin
property fontSize get mFontSize set mFontSize
property rotateBy get mRotateBy set mRotateBy
property frequency get mFrequency set mFrequency
--
----------
-- called when widget is created
public handler OnCreate()
put 48 into mFontSize
put 0 into mRotation
put 5 into mMargin
put 45 into mRotateBy
put 0.1 into mFrequency
schedule timer in mFrequency seconds
end handler
----------
----------
-- called whenever LiveCode needs to redraw the widget
public handler OnPaint()
set the paint of this canvas to solid paint with color [0.0, 0.0, 0.0]
set the font of this canvas to font "FontAwesome Regular" at size mFontSize
-- fa-spinner 
-- fa-repeat 
drawTextCenteredAtPointWithRotation("", mRotation)
end handler
----------
public handler rectCenter(in pRect as Rectangle) as Point
return point [the left of pRect + the width of pRect / 2, the top of pRect + the height of pRect / 2]
end handler
public handler drawTextCenteredAtPointWithRotation(in pText as string, in pAngle as number) as undefined
save state of this canvas
// Find the center-point of the text
variable tBounds
measure pText on this canvas
put the result into tBounds
variable tCenter
put rectCenter(tBounds) into tCenter
//add mMargin to the width of tBounds
//add mMargin to the height of tBounds
// Move the canvas origin to the middle of the size of the font
translate this canvas by [the width of tBounds/2, the height of tBounds/2]
// Rotate the canvas
rotate this canvas by pAngle
// Draw the text, offsetting by the distance to the center point of the text bounds
fill text pText at point [- the x of tCenter, - the y of tCenter] on this canvas
// OR Draw the text, rotating around the middle of the baseline
--fill text pText at point [- the x of tCenter, 0] on this canvas
restore state of this canvas
end handler
----------
----------
-- this handler is called when the timer scheduled with 'schedule timer' fires
public handler OnTimer()
put (mRotation + mRotateBy) wrap 360 into mRotation
--variable tList as list
--put the empty list into tList
--push mRotation onto tList
--log "rotation set to %@" with tList
redraw all
schedule timer in mFrequency seconds
end handler
----------
-- this handler returns the amount of time until the seconds change
handler computeNextTimer() as number
return 0.1
-- use the 'execute script' command
--execute script "return 1 - (the long seconds - round(the long seconds - 0.5))"
--return the result
end handler
end widget
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment