How to create a new Morphic project by code
Last updated at 3:40 am UTC on 6 December 2021
Example 1
- Create a new MorphicProject
- add an example morph to that new project
- hide the docking bar in the new project
- set the background color of the new project to white
- open the project view morph in the current project thus giving access to the newly created MorphicProject
| newProject exampleMorph |
newProject := MorphicProject new.
newProject name: 'MyNewProject'.
exampleMorph := Morph new extent: 100@200; color: Color red; position: 20@20.
newProject world addMorph: exampleMorph.
newProject showWorldMainDockingBar: false.
newProject world color: Color white.
ProjectViewMorph openOn: newProject.
Example 2
The same example as in example 1 but with a more complex content morph taken from A4 and A3 sheets in Morphic.
| a4 newProject |
a4Extent := 210@ 297 "millimeter".
a4ExtentPrintableArea := 190 @ 277 "millimeter".
scaleFactorForDisplay := 2.
a4ExtentForDisplay := a4Extent * scaleFactorForDisplay.
a4 := Morph new extent: a4ExtentForDisplay.
a4 color: Color lightBlue.
a4PrintableAreaExtentForDisplay := a4ExtentPrintableArea *scaleFactorForDisplay.
a4PrintableArea := Morph new extent: a4PrintableAreaExtentForDisplay.
a4PrintableArea color: Color white.
a4 addMorph: a4PrintableArea.
boxHeight := (a4PrintableArea height) /4.
boxWidth := (a4PrintableArea width) /3.
innerBoxWidth := boxHeight / (2 sqrt).
0 to: 3 do: [:row |
0 to: 2 do: [:col |
box:= Morph new extent: innerBoxWidth@boxHeight; color: Color random; borderWidth: 0.
centerPoint := ((col+0.5) * boxWidth)@((row+0.5) * boxHeight).
box center: centerPoint.
a4PrintableArea addMorph: box ]
].
a4PrintableArea center: a4 center.
a4 position: 20@50.
newProject := MorphicProject new.
newProject name: 'Layout 12 boxes - A4 sheet'.
newProject world addMorph: a4.
newProject showWorldMainDockingBar: false.
newProject world color: Color blue.
ProjectViewMorph openOn: newProject.
Example 3
The example is similar to example 1 and is based on StringMorph and SimpleButtonMorph example -- another counter
- Create a new MorphicProject
- add three morphs s,bm and bp to that new project (StringMorph and two button morphs)
- hide the docking bar in the new project
- set the background color of the new project to yellow
- open the project view morph in the current project thus giving access to the newly created MorphicProject
| newProject |
newProject := MorphicProject new.
newProject name: 'MyCounter'.
s := StringMorph new.
newProject world addMorph: s.
s contents: '0'.
s position: 100@100.
bp := SimpleButtonMorph new.
newProject world addMorph: bp.
bp label: 'Plus-1'.
bp position: 150@100.
bm := SimpleButtonMorph new.
newProject world addMorph: bm.
bm label: 'Minus-1'.
bm position: 250@100.
"—— Add actions to buttons ———— "
bp target: [ s contents: (s contents asInteger + 1) asString ].
bp actionSelector: #value.
bm target: [ s contents: (s contents asInteger - 1) asString ].
bm actionSelector: #value.
newProject showWorldMainDockingBar: false.
newProject world color: Color yellow twiceLighter.
ProjectViewMorph openOn: newProject.
Example 4
IMAGE function cannot find the upload.
| newProject slideExtent mySlide box |
"Create a slide (a Morph with property #isSlide)"
slideExtent := 800@600.
mySlide := Morph new extent: slideExtent; color: Color white.
mySlide setProperty: #isSlide toValue: true.
"Add some simple content to the slide"
box := Morph new extent: (slideExtent * 0.2).
mySlide addMorph: box.
box := Morph new extent: (slideExtent * 0.2).
box position: (mySlide position + (slideExtent * 0.2)).
box color: Color red.
mySlide addMorph: box.
box := Morph new extent: (slideExtent * 0.2).
box position: (mySlide position + 2* (slideExtent * 0.2)).
box color: Color yellow.
mySlide addMorph: box.
box := Morph new extent: (slideExtent * 0.2).
box position: (mySlide position + 3* (slideExtent * 0.2)).
box color: Color green.
mySlide addMorph: box.
box := Morph new extent: (slideExtent * 0.2).
box position: (mySlide position + 4* (slideExtent * 0.2)).
box color: Color orange.
mySlide addMorph: box.
newProject := MorphicProject new.
newProject name: 'Project with a Slide'.
newProject showWorldMainDockingBar: false.
newProject world addMorph: mySlide.
ProjectViewMorph openOn: newProject.