~hilaire-fernandes/drgeo/trunk

« back to all changes in this revision

Viewing changes to src/DrGeoII-Core/DrGValueScriptItem.class.st

  • Committer: Hilaire Fernandes
  • Date: 2017-11-15 13:17:03 UTC
  • Revision ID: hilaire.fernandes@gmail.com-20171115131703-pz09obavthi53ebt
DrGeo code under Tonel file format

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"
 
2
Script
 
3
        - script: a script class instance 
 
4
        - parents: a collection of costume (BEWARE, and not math item)
 
5
"
 
6
Class {
 
7
        #name : #DrGValueScriptItem,
 
8
        #superclass : #DrGValueItem,
 
9
        #instVars : [
 
10
                'script',
 
11
                'parentsCostume'
 
12
        ],
 
13
        #category : #'DrGeoII-Core-Item'
 
14
}
 
15
 
 
16
{ #category : #comparing }
 
17
DrGValueScriptItem >> = aScriptItem [
 
18
        ^super = aScriptItem and: [self script class == aScriptItem script class]
 
19
]
 
20
 
 
21
{ #category : #private }
 
22
DrGValueScriptItem >> adaptiveDescriptiveName [
 
23
        | string |
 
24
        parents size > 0 
 
25
                ifFalse: [string := 'This script "{1}"' translated]
 
26
                ifTrue: 
 
27
                        [parents size = 1 
 
28
                                ifTrue: [string := 'This script "{1}" with argument: %2' translated]
 
29
                                ifFalse: [string := 'This script "{1}" with arguments: %2' translated].
 
30
                        string := string copyReplaceAll: '%2' with: self argumentsName].
 
31
        ^string format: {script class scriptName}.
 
32
]
 
33
 
 
34
{ #category : #private }
 
35
DrGValueScriptItem >> argumentsName [
 
36
        "collect parents name in a string as A, B, C"
 
37
        | stream |
 
38
        parents size = 0 ifTrue: [ ^ '' ].
 
39
        stream := ReadWriteStream on: String new.
 
40
        parents allButLast do: 
 
41
                [ :item | 
 
42
                stream
 
43
                        nextPutAll: (item safeName ifEmpty: [item printString]);
 
44
                        nextPutAll: ', ' ].
 
45
        stream nextPutAll: (parents last safeName ifEmpty: [parents last printString]).
 
46
        ^ stream contents
 
47
]
 
48
 
 
49
{ #category : #accessing }
 
50
DrGValueScriptItem >> compute [
 
51
"Do the script computation and return its result "
 
52
        ^ self script compute
 
53
]
 
54
 
 
55
{ #category : #accessing }
 
56
DrGValueScriptItem >> costumeClass [ 
 
57
         ^ DrGScriptCostume 
 
58
]
 
59
 
 
60
{ #category : #accessing }
 
61
DrGValueScriptItem >> costumes [
 
62
        ^ parentsCostume 
 
63
]
 
64
 
 
65
{ #category : #initialization }
 
66
DrGValueScriptItem >> initialize: theParentsCostume [ 
 
67
        super initialize: theParentsCostume.
 
68
        self script: (theParentsCostume at: theParentsCostume size - 1)
 
69
]
 
70
 
 
71
{ #category : #testing }
 
72
DrGValueScriptItem >> isPropertyEditable [
 
73
        ^ DrGDefault isTablet 
 
74
                ifTrue: [false]  
 
75
                ifFalse:  [true]
 
76
]
 
77
 
 
78
{ #category : #testing }
 
79
DrGValueScriptItem >> isScriptItem [
 
80
        ^true
 
81
]
 
82
 
 
83
{ #category : #'xml writing' }
 
84
DrGValueScriptItem >> nodeType [
 
85
        ^#script
 
86
]
 
87
 
 
88
{ #category : #accessing }
 
89
DrGValueScriptItem >> parents: aCollection [ 
 
90
        "last is the initial screen position of the value, and previous last is the script class"
 
91
        aCollection isEmptyOrNil ifTrue: 
 
92
                [parents := aCollection.
 
93
                parentsCostume := aCollection.
 
94
                ^ self ].
 
95
        aCollection last isPoint 
 
96
                ifTrue: [ parents := aCollection allButLast: 2 ]
 
97
                ifFalse: 
 
98
                        [ "play nicely with macro"
 
99
                        parents := aCollection ].
 
100
 
 
101
]
 
102
 
 
103
{ #category : #accessing }
 
104
DrGValueScriptItem >> parents: aCollection in: app [
 
105
        "last is the initial screen position of the value, and previous last is the script class"
 
106
        self parents: aCollection.
 
107
        parents ifNotNil: [parentsCostume := app costumesOf: parents].
 
108
        "reinject the argument in the script in cas it is already instanciated"
 
109
        script ifNotNil: [script arguments: parentsCostume]
 
110
]
 
111
 
 
112
{ #category : #printing }
 
113
DrGValueScriptItem >> printNameOn: aStream [
 
114
        aStream nextPutAll: 'Script ' translated.
 
115
 
 
116
]
 
117
 
 
118
{ #category : #printing }
 
119
DrGValueScriptItem >> printValueOn: aStream [
 
120
        value isNumber 
 
121
                ifTrue: [^ self printDecimal: value on: aStream].
 
122
        value isPoint 
 
123
                ifTrue: [^ self printCoordinates: value on:  aStream].
 
124
        aStream print: value
 
125
]
 
126
 
 
127
{ #category : #comparing }
 
128
DrGValueScriptItem >> rehash [
 
129
        ^hash := super rehash bitXor: script class hash
 
130
]
 
131
 
 
132
{ #category : #accessing }
 
133
DrGValueScriptItem >> script [
 
134
        ^script
 
135
]
 
136
 
 
137
{ #category : #accessing }
 
138
DrGValueScriptItem >> script: aScriptClass [
 
139
        script := aScriptClass new arguments: self costumes.
 
140
        self rehash 
 
141
]
 
142
 
 
143
{ #category : #updating }
 
144
DrGValueScriptItem >> update [
 
145
        self doParentsExist ifTrue: [ value := script compute]
 
146
]
 
147
 
 
148
{ #category : #'xml writing' }
 
149
DrGValueScriptItem >> writeAsXmlTo: aNode [
 
150
        |node|
 
151
        node := super writeAsXmlTo: aNode.
 
152
        node attributeAt: #class put: script class printString.
 
153
        node addElement: (
 
154
                (XMLElement named: #code) addContent: (
 
155
                        XMLStringNode string: (String streamContents: [:out |script class fileOutOn: out]))).
 
156
        ^ self writeAsXmlPositionTo: node.
 
157
]