#10 2011-03-16 16:02:38

YourLocalBlockLib
Scratcher
Registered: 2011-03-10
Posts: 100+

Re: ITopic: Welcome to your local block library!

Variables
/img/tinypic/21j38ra.jpg

/img/dropbox/BlockLibDiv2.png

/img/weebly/280364342.gif Shared by Hardmath123
Blockspec:

('make new variable' #- #addGlobalVariable)

Code:
no code needed
What it does:
Creates a new variable!

/img/dropbox/BlockLibDiv2.png

/img/weebly/387583531.gif Shared by Pecola1
Blockspec:

('move %v to x: %n y: %n' #- #positionVar:atX:y: '' 0 0)

Code:

Code:

positionVar: t1 atX: t2 y: t3 
    | t4 t5 t6 t7 t8 t9 |
    (self varNames includes: t1)
        ifFalse: [^ self].
    t4 _ self ownerThatIsA: ScratchFrameMorph.
    t4
        ifNil: 
            [(t5 _ self ownerThatIsA: OffscreenWorldMorph) ifNil: [^ self].
            t4 _ t5 frame].
    t6 _ VariableBlockMorph new commandSpec: t1;
             receiver: self blockReceiver.
    (t5 _ t4 watcherForBlock: t6) ifNil: [^ self].
    t7 _ t2.
    t7 isNaN ifTrue: [t7 _ 0].
    t7 > 240 ifTrue: [t7 _ 240].
    t7 < (-240 - t5 extent x) ifTrue: [t7 _ -240 - t5 extent x].
    t8 _ t3.
    t8 isNaN ifTrue: [t8 _ 0].
    t8 < -180 ifTrue: [t8 _ -180].
    t8 > (180 + t5 extent y) ifTrue: [t8 _ 180 + t5 extent y].
    t9 _ t7 @ t8 negated.
    t5 position: ScratchOrigin + t9

Code 2:

moveVar: t1 toX: t2 y: t3
    | t4 |
    self
        positionVar: t1
        atX: t2
        y: t3.
    t4 _ self ownerThatIsA: ScratchStageMorph.
    t4 ~= self
        ifTrue: [t4
                positionVar: t1
                atX: t2
                y: t3]

What it does:
Changes the position of a variable watcher on the Stage.

/img/dropbox/BlockLibDiv2.png

/img/weebly/1489182.gif Shared by Hardmath123
Blockspec:

('NewList' #- #addGlobalList)

Code:
No code needed.
What it does:
Opens the new list dialog.

/img/dropbox/BlockLibDiv2.png

/img/weebly/3672059.gif Shared by Hardmath123
Blockspec:

('Add local list named %s' #- #createListNamed:)

Code:
No code needed.
What it does:
Creates a local list (for this sprite only) named the specified name.

/img/dropbox/BlockLibDiv2.png

/img/weebly/2633125.gif Shared by Hardmath123
Blockspec:

('Add global list named %s' #- #GLN: #MyList)

Code:

GLN: t2
    | t1 |
    (t1 _ self ownerThatIsA: ScratchFrameMorph) ifNil: [^ self beep].
    t2 size = 0 ifTrue: [^ self].
    t1 workPane createListNamed: t2.
    t1 viewerPane categoryChanged: 'variables'

What it does:
Creates a global list (for all sprites) named the specified name.

/img/dropbox/BlockLibDiv2.png

/img/weebly/4589197.gif Shared by Hardmath123
Blockspec:

('set the list blocks color to %c' #- #setListBlockColorSelf:)

Code: (place under sensing ops)

setListBlockColorSelf: t1
    ListBlockColor _ t1

What it does:
Changes the color for the list blocks.

/img/dropbox/BlockLibDiv2.png

/img/polyeztahpuppies/importfonts.gif Shared by Greenatic, improved by LS97 and Baderous
Blockspec:

('import fonts to list %L' #- #FontstoList: 'list')

Code:

FontstoList: t1
    | t2 |
    t2 _ UnicodePlugin getFontList.
    t2 do: [:t3 | self append: t3 toList: t1]

Scratch-Objects > Scriptable Scratch Morph > blocks > defaultArgsFor:
Add right before the last line ( ^ t2 ):

#FontstoList: = t4
        ifTrue: [t2 size >= 1 ifTrue: [t2 at: 1 put: self defaultListName]].

What it does:
Adds all the fonts on your computer to a list.

/img/dropbox/BlockLibDiv2.png

http://www.freewebs.com/polyeztahpuppies/averageoflist.gif Shared by Greenatic
Blockspec:

('%Y of %L' #r #Math:List: 'mean' 'list')

Code:

Code:

Math: t1 List: t2
| max min line sum t3 t4 a |
t1 = 'maximum' ifTrue:[
(self lineCountOfList: t2) asNumberNoError = 0 ifTrue: [^ 0].
    (self lineCountOfList: t2)
        = 1 ifTrue: [^ (self getLine: 1 ofList: t2) asNumberNoError].
    max _ (self getLine: 1 ofList: t2) asNumberNoError.
    line _ 2.
    (self lineCountOfList: t2) asNumberNoError - 1
        timesRepeat: 
            [(self getLine: line ofList: t2) asNumberNoError > max ifTrue: [max _ (self getLine: line ofList: t2) asNumberNoError].
            line _ line + 1].
    ^ max].
t1 = 'minimum' ifTrue:[
(self lineCountOfList: t2) asNumberNoError = 0 ifTrue: [^ 0].
    (self lineCountOfList: t2)
        = 1 ifTrue: [^ (self getLine: 1 ofList: t2) asNumberNoError].
    min _ (self getLine: 1 ofList: t2) asNumberNoError.
    line _ 2.
    (self lineCountOfList: t2) asNumberNoError - 1
        timesRepeat: 
            [(self getLine: line ofList: t2) asNumberNoError < min ifTrue: [min _ (self getLine: line ofList: t2) asNumberNoError].
            line _ line + 1].
    ^ min].
t1 = 'mean' ifTrue:[
 (self lineCountOfList: t2)
        = 0 ifTrue: [^ 0].
    (self lineCountOfList: t2)
        = 1 ifTrue: [^ self getLine: 1 ofList: t2].
   (self lineCountOfList: t2)
        = 0 ifTrue: [^ 0].
    (self lineCountOfList: t2)
        = 1 ifTrue: [^ (self getLine: 1 ofList: t2) asNumberNoError].
    t3 _ 2.
    sum _ (self getLine: 1 ofList: t2) asNumberNoError.
    (self lineCountOfList: t2)
        - 1
        timesRepeat: 
            [sum _ sum + (self getLine: t3 ofList: t2) asNumberNoError.
            t3 _ t3 + 1].
    ^ sum
        / (self lineCountOfList: t2)].
t1 = 'range' ifTrue:[
    (self lineCountOfList: t2)
        = 0 ifTrue: [^ 0].
        (self lineCountOfList: t2)
        = 1 ifTrue: [^ 0].
    (self lineCountOfList: t2) asNumberNoError = 0 ifTrue: [^ 0].
    (self lineCountOfList: t2)
        = 1 ifTrue: [^ (self getLine: 1 ofList: t2) asNumberNoError].
    max _ (self getLine: 1 ofList: t2) asNumberNoError.
    line _ 2.
    (self lineCountOfList: t2) asNumberNoError - 1
        timesRepeat: 
            [(self getLine: line ofList: t2) asNumberNoError > max ifTrue: [max _ (self getLine: line ofList: t2) asNumberNoError].
            line _ line + 1].
    (self lineCountOfList: t2) asNumberNoError = 0 ifTrue: [^ 0].
    (self lineCountOfList: t2)
        = 1 ifTrue: [^ (self getLine: 1 ofList: t2) asNumberNoError].
    min _ (self getLine: 1 ofList: t2) asNumberNoError.
    line _ 2.
    (self lineCountOfList: t2) asNumberNoError - 1
        timesRepeat: 
            [(self getLine: line ofList: t2) asNumberNoError < min ifTrue: [min _ (self getLine: line ofList: t2) asNumberNoError].
            line _ line + 1].
    ^ max - min].
t1 = 'median' ifTrue:[
(self lineCountOfList: t2)
        = 0 ifTrue: [^ 0].
    t3 _ #() asOrderedCollection.
    t4 _ 1.
    (self lineCountOfList: t2)
        timesRepeat: 
            [t3 add: (self getLine: t4 ofList: t2) asNumberNoError.
            t4 _ t4 + 1].
    t3 _ t3 asArray sort.
    t3 _ t3 asOrderedCollection.
    [t3 size > 2]
        whileTrue: 
            [t3 removeAt: 1.
            t3 removeAt: t3 size].
    t3 size = 1 ifTrue: [^ t3 at: 1].
    ^ (t3 at: 1)
        + (t3 at: 2) / 2].
t1 = 'sum' ifTrue:[
(self lineCountOfList: t2)
        = 0 ifTrue: [^ 0].
    (self lineCountOfList: t2)
        = 1 ifTrue: [^ (self getLine: 1 ofList: t2) asNumberNoError].
    t3 _ 2.
    a _ (self getLine: 1 ofList: t2) asNumberNoError.
    (self lineCountOfList: t2)
        - 1
        timesRepeat: 
            [a _ a + (self getLine: t3 ofList: t2) asNumberNoError.
            t3 _ t3 + 1].
    ^ a].
t1 = 'difference' ifTrue:[
(self lineCountOfList: t2)
        = 0 ifTrue: [^ 0].
    (self lineCountOfList: t2)
        = 1 ifTrue: [^ (self getLine: 1 ofList: t2) asNumberNoError].
    t3 _ 2.
    a _ (self getLine: 1 ofList: t2) asNumberNoError.
    (self lineCountOfList: t2)
        - 1
        timesRepeat: 
            [a _ a - (self getLine: t3 ofList: t2) asNumberNoError.
            t3 _ t3 + 1].
    ^ a].
t1 = 'product' ifTrue:[
(self lineCountOfList: t2)
        = 0 ifTrue: [^ 0].
    (self lineCountOfList: t2)
        = 1 ifTrue: [^ (self getLine: 1 ofList: t2) asNumberNoError].
    t3 _ 2.
    a _ (self getLine: 1 ofList: t2) asNumberNoError.
    (self lineCountOfList: t2)
        - 1
        timesRepeat: 
            [a _ a * (self getLine: t3 ofList: t2) asNumberNoError.
            t3 _ t3 + 1].
    ^ a].
t1 = 'quotient' ifTrue:[
(self lineCountOfList: t2)
        = 0 ifTrue: [^ 0].
    (self lineCountOfList: t2)
        = 1 ifTrue: [^ (self getLine: 1 ofList: t2) asNumberNoError].
    t3 _ 2.
    a _ (self getLine: 1 ofList: t2) asNumberNoError.
    (self lineCountOfList: t2)
        - 1
        timesRepeat: 
            [a _ a / (self getLine: t3 ofList: t2) asNumberNoError.
            t3 _ t3 + 1].
    ^ a].

ScriptableScratchMorph > list ops

listMath
    ^ #('mean' 'median' #- 'maximum' 'minimum' 'range' #- 'sum' 'difference' 'product' 'quotient' )

Scratch-Blocks> CommandBlockMorph> private> uncoloredArgMorphFor:  add:

$Y = t2 ifTrue: [^ ChoiceOrExpressionArgMorph new getOptionsSelector: #listMath;
choice: 'mean'].

Scratch-Objects > Scriptable Scratch Morph > blocks > defaultArgsFor:
Add right before the last line ( ^ t2 ):

#Math:List: = t4
        ifTrue: [t2 size >= 2 ifTrue: [t2 at: 1 put: (t2 at: 1) localized.
t2 at: 2 put: self defaultListName]].

What it does:
Performs operations on a list and reports the result.

/img/dropbox/BlockLibDiv2.png

/img/polyeztahpuppies/additemstolist.gif Shared by Greenatic
Blockspec:

('add items %n to %n of %L to %L' #- #First:Last:From:To: '1' '3' 'list' 'list')

Code:

Code:

First: t1 Last: t2 From: t3 To: t4 
    | t5 t6 |
    t1 isInf ifTrue: [^ self].
    t1 isNaN ifTrue: [^ self].
    t1 isNil ifTrue: [^ self].
    t2 isInf ifTrue: [^ self].
    t2 isNaN ifTrue: [^ self].
    t2 isNil ifTrue: [^ self].
    t1 = t1 rounded ifFalse: [^ self].
    t2 = t2 rounded ifFalse: [^ self].
    t1 < 1 ifTrue: [^ self].
    t2 < 1 ifTrue: [^ self].
    t1 > (self lineCountOfList: t3) ifTrue: [^ self].
    t2 > (self lineCountOfList: t3) ifTrue: [^ self].
    t5 _ t1.
    (t2 - t1) abs + 1
        timesRepeat: 
            [self append: (self getLine: t5 ofList: t3)
                toList: t4.
            t6 _ false.
            t2 > t1
                ifTrue: 
                    [t5 _ t5 + 1.
                    t6 _ true].
            t6 = false ifTrue: [t5 _ t5 - 1]]

Scratch-Objects > ScriptableScratchMorph > blocks > defaultArgsFor:
Add right before the last line ( ^ t2 ):

#First:Last:From:To: = t4
        ifTrue: [t2 size >= 4
                ifTrue:
                    [t2 at: 1 put: (t2 at: 1) localized.
                    t2 at: 2 put: (t2 at: 2) localized.
                    t2 at: 3 put: self defaultListName.
                    t2 at: 4 put: self defaultListName]].

What it does:
Adds some items from one list to another.

/img/dropbox/BlockLibDiv2.png

/img/polyeztahpuppies/numbersort.gif Shared by Greenatic
Blockspec:

('sort items of %L by numerical value' #- #NumberSort: 'list')

Code:

Code:

NumberSort: t1 
    | t2 t3 t4 t5 t6 |
    t2 _ #() asOrderedCollection.
    t3 _ #() asOrderedCollection.
    t4 _ #() asOrderedCollection.
    t6 _ #() asOrderedCollection.
    t5 _ 1.
    (self lineCountOfList: t1)
        timesRepeat: 
            [t2 add: (self getLine: t5 ofList: t1).
            t5 _ t5 + 1].
    t2 do: 
        [:t7 | 
        t3 add: t7 asNumberNoError.
        t7 asNumberNoError = 0 ifTrue: [t4 add: t7]].
    t3 _ t3 asArray sort.
    t3 _ t3 asOrderedCollection.
    t5 _ 1.
    t3 do: 
        [:t8 | 
        t8 = 0 ifFalse: [t6 add: t8].
        t8 = 0
            ifTrue: 
                [t6 add: (t4 at: t5).
                t5 _ t5 + 1]].
    self deleteLine: 'all' ofList: t1.
    t6 do: [:t9 | self append: t9 toList: t1]

Scratch-Objects > ScriptableScratchMorph > blocks > defaultArgsFor:
Add right before the last line ( ^ t2 ):

#NumberSort: = t4
        ifTrue: [t2 size >= 1 ifTrue: [t2 at: 1 put: self defaultListName]].

What it does:
Interprets the items of a list as numbers and sorts them.  (A string counts as 0).

/img/dropbox/BlockLibDiv2.png

/img/polyeztahpuppies/stringsort.gif Shared by Greenatic
Blockspec:

('sort items of %L by string value' #- #StringSort: 'list')

Code:

Code:

SringSort: t1 
    | t2 t3 |
    (self lineCountOfList: t1)
        = 0 ifTrue: [^ self].
    (self lineCountOfList: t1)
        = 1 ifTrue: [^ self].
    t2 _ #() asOrderedCollection.
    t3 _ 1.
    (self lineCountOfList: t1)
        timesRepeat: 
            [t2 add: (self getLine: t3 ofList: t1).
            t3 _ t3 + 1].
    t2 _ t2 asArray sort.
    self deleteLine: 'all' ofList: t1.
    t2 do: [:t4 | self append: t4 toList: t1]

Scratch-Objects > ScriptableScratchMorph > blocks > defaultArgsFor:
Add right before the last line ( ^ t2 ):

#StringSort: = t4
        ifTrue: [t2 size >= 1 ifTrue: [t2 at: 1 put: self defaultListName]].

What it does:
Interprets the items of a list as strings and sorts them.

/img/dropbox/BlockLibDiv2.png

/img/polyeztahpuppies/firstindex.gif Shared by Greenatic
Blockspec:

('first index of %s in %L' #r #IndexOf:List: '' 'list')

Code:

Code:

IndexOf: t1 List: t2 
    | t3 |
    t3 _ 1.
    (self lineCountOfList: t2)
        timesRepeat: 
            [(self getLine: t3 ofList: t2)
                = t1 ifTrue: [^ t3].
            t3 _ t3 + 1].
    ^ 0

Scratch-Objects > ScriptableScratchMorph > blocks > defaultArgsFor:
Add right before the last line ( ^ t2 ):

#IndexOf:List: = t4
        ifTrue: [t2 size >= 2
                ifTrue:
                    [t2 at: 1 put: (t2 at: 1) localized.
                    t2 at: 2 put: self defaultListName]].

What it does:
Reports the first index of the argument in the list, and reports 0 if the item is not in the list.

/img/dropbox/BlockLibDiv2.png

/img/polyeztahpuppies/timesappearsinlist.gif Shared by Greenatic
Blockspec:

('times %s appears in %L' #r #Times:List: '' 'list')

Code:

Code:

Times: t1 List: t2 
    | t3 t4 |
    t3 _ 1.
    t4 _ 0.
    (self lineCountOfList: t2)
        timesRepeat: 
            [(self getLine: t3 ofList: t2)
                = t1 ifTrue: [t4 _ t4 + 1].
            t3 _ t3 + 1].
    ^ t4

Scratch-Objects > ScriptableScratchMorph > blocks > defaultArgsFor:
Add right before the last line ( ^ t2 ):

#Times:List: = t4
        ifTrue: [t2 size >= 2
                ifTrue:
                    [t2 at: 1 put: (t2 at: 1) localized.
                    t2 at: 2 put: self defaultListName]].

What it does:
Reports the number of times the argument is in the list.

/img/dropbox/BlockLibDiv2.png

/img/polyeztahpuppies/modesof.gif Shared by Greenatic
Blockspec:

('mode(s) of %L, spacer %s' #r #ModeOf:Spacer: 'list' ',')

Code:

Code:

ModeOf: t1 Spacer: t2 
    | t3 t4 t5 t6 t7 |
    (self lineCountOfList: t1)
        = 0 ifTrue: [^ nil].
    (self lineCountOfList: t1)
        = 1 ifTrue: [^ nil].
    t3 _ #() asOrderedCollection.
    t4 _ #() asOrderedCollection.
    t5 _ 0.
    (self lineCountOfList: t1)
        timesRepeat: 
            [t6 _ 1.
            t7 _ false.
            t5 _ t5 + 1.
            (t3 includes: (self getLine: t5 ofList: t1) asNumberNoError)
                ifFalse: 
                    [t3 add: (self getLine: t5 ofList: t1) asNumberNoError.
                    t4 add: 1.
                    t7 _ true].
            t7 = false
                ifTrue: 
                    [[(t3 at: t6)
                        = (self getLine: t5 ofList: t1) asNumberNoError]
                        whileFalse: [t6 _ t6 + 1].
                    t4 _ t4 asArray.
                    t4 at: t6 put: (t4 at: t6)
                            + 1.
                    t4 _ t4 asOrderedCollection]].
    t5 _ 1.
    t6 _ 1.
    t4 size
        timesRepeat: 
            [(t4 at: t5)
                > t6 ifTrue: [t6 _ t4 at: t5].
            t5 _ t5 + 1].
    t6 = 1 ifTrue: [^ nil].
    t5 _ 1.
    t4 size
        timesRepeat: 
            [t7 _ false.
            (t4 at: t5)
                < t6
                ifTrue: 
                    [t3 removeAt: t5.
                    t4 removeAt: t5.
                    t7 _ true].
            t7 = false ifTrue: [t5 _ t5 + 1]].
    t3 size = 1 ifTrue: [^ t3 at: 1].
    t5 _ 2.
    t3 _ t3 asArray sort.
    t3 _ t3 asOrderedCollection.
    t6 _ (t3 at: 1) asString.
    t3 size - 1
        timesRepeat: 
            [t6 _ t6 , t2.
            t6 _ t6 , (t3 at: t5) asString.
            t5 _ t5 + 1].
    ^ t6

Scratch-Objects > ScriptableScratchMorph > blocks > defaultArgsFor:
Add right before the last line ( ^ t2 ):

#ModeOf:Spacer: = t4
        ifTrue: [t2 size >= 2
                ifTrue:
                    [t2 at: 1 put: self defaultListName.
                    t2 at: 2 put: (t2 at: 2) localized]].

What it does:
Reports the mode of the list.  If there is more than one, it reports them as a single string with the modes separated by the spacer value.

/img/dropbox/BlockLibDiv2.png

^ Back to Scratch Blocks
^ Back to Contents
 

Last edited by YourLocalBlockLib (2011-12-17 14:53:13)


/img/dropbox/BlockLibraryTitle.png

Offline