1
0
mirror of https://github.com/lxsang/Diya-API.git synced 2024-12-27 20:08:22 +01:00
Diya-API/Diya/DiyaNode.class.st

351 lines
6.5 KiB
Smalltalk
Raw Normal View History

2022-03-01 22:58:58 +01:00
Class {
#name : #DiyaNode,
#superclass : #DiyaBaseObject,
#instVars : [
'translation',
'parent',
2022-03-03 19:19:40 +01:00
'children',
2022-03-01 22:58:58 +01:00
'scale',
'rotation',
'tf',
'shader',
2022-03-04 20:28:38 +01:00
'context',
2022-03-19 02:18:29 +01:00
'ehandlers',
2022-03-23 00:52:15 +01:00
'root',
2022-08-07 20:43:16 +02:00
'styleName',
'style',
'id',
'visibility',
'pivot'
2022-03-01 22:58:58 +01:00
],
#pools : [
'OpenGLConstants',
2022-03-19 02:18:29 +01:00
'OpenGLTypes',
'SDL2Constants'
2022-03-01 22:58:58 +01:00
],
#category : #'Diya-Graphics'
}
{ #category : #'instance creation' }
DiyaNode class >> with: shader [
^self new shader: shader; yourself
]
2022-08-07 20:43:16 +02:00
{ #category : #accessing }
DiyaNode >> ? styleAttr [
| value|
styleName ifNotNil: [
style ifNil: [
style := DiyaUIThemesManager uniqueInstance currentTheme ? (self styleName).
].
value := style at: styleAttr ifAbsent:[nil].
2022-08-07 20:43:16 +02:00
value ifNotNil: [ ^value ].
].
"try to look at parent style"
parent ifNil:[self styleNotFound: styleAttr].
^ parent ? styleAttr
2022-08-07 20:43:16 +02:00
]
2022-03-03 19:19:40 +01:00
{ #category : #accessing }
DiyaNode >> addNode: node [
^self addNode: node at: 0@0
]
{ #category : #accessing }
DiyaNode >> addNode: node at: pos [
children ifNil: [ ^self ].
node parent: self.
children add: node.
node position: pos.
2022-03-19 02:18:29 +01:00
node root: self root.
node setDirtyAll.
^ node
]
2022-03-03 19:19:40 +01:00
{ #category : #accessing }
DiyaNode >> boundingBox [
^ self subclassResponsibility
]
{ #category : #accessing }
DiyaNode >> children [
^children
]
{ #category : #rendering }
2022-03-03 19:19:40 +01:00
DiyaNode >> draw [
self subclassResponsibility
]
{ #category : #requirements }
DiyaNode >> empty [
children := OrderedCollection new.
]
2022-03-04 20:28:38 +01:00
{ #category : #accessing }
DiyaNode >> extent [
2022-03-16 01:32:01 +01:00
^ self subclassResponsibility
2022-03-04 20:28:38 +01:00
]
{ #category : #processing }
DiyaNode >> forceReload [
self process.
self setClean.
children ifNotNil: [
children do:[:c|
c forceReload
]]
]
{ #category : #accessing }
DiyaNode >> id [
^ id
]
2022-03-01 22:58:58 +01:00
{ #category : #initialization }
DiyaNode >> initialize [
super initialize.
parent := nil.
2022-03-03 19:19:40 +01:00
shader := nil.
context := DiyaRenderer.
2022-03-03 19:19:40 +01:00
children := OrderedCollection new.
2022-03-19 02:18:29 +01:00
ehandlers := Dictionary new.
2022-08-07 20:43:16 +02:00
styleName := nil.
style := nil.
2022-03-19 02:18:29 +01:00
root := nil.
visibility := true.
pivot := 0@0.
id := self className,'#',(Random new nextInt: 1e6) asString.
2022-03-19 02:18:29 +01:00
]
{ #category : #testing }
2022-03-19 02:18:29 +01:00
DiyaNode >> inner: aPoint [
^ self subclassResponsibility
]
{ #category : #testing }
DiyaNode >> isRoot [
2022-03-19 02:18:29 +01:00
^ false
]
{ #category : #convenience }
DiyaNode >> on: eventName do: aBlock [
2022-03-21 01:45:21 +01:00
eventName isArray ifFalse:[ ^ self register: aBlock to: eventName ].
eventName do:[:e| self register: aBlock to:e ].
2022-03-19 02:18:29 +01:00
2022-03-01 22:58:58 +01:00
]
{ #category : #accessing }
DiyaNode >> parent [
^ parent
]
{ #category : #accessing }
DiyaNode >> parent: anObject [
parent := anObject
]
{ #category : #accessing }
DiyaNode >> pivot [
^ pivot
]
2022-03-01 22:58:58 +01:00
{ #category : #accessing }
DiyaNode >> position [
^ translation
]
{ #category : #accessing }
DiyaNode >> position: anObject [
translation := anObject.
self updateTF.
2022-03-01 22:58:58 +01:00
]
{ #category : #processing }
DiyaNode >> process [
^self subclassResponsibility
]
2022-03-21 01:45:21 +01:00
{ #category : #convenience }
DiyaNode >> register: aBlock to: eventName [
|evtCode|
evtCode := SDL2Constants bindingOf: ('SDL_', eventName asUppercase).
evtCode ifNil: [ evtCode := eventName ].
2022-03-21 01:45:21 +01:00
ehandlers at: evtCode value put: aBlock.
]
{ #category : #removing }
DiyaNode >> remove [
self setClean.
root := nil.
parent ifNotNil: [ parent removeChild: self ]
]
{ #category : #removing }
DiyaNode >> removeChild: c [
children ifNotNil: [ children remove: c ifAbsent:[ ]]
]
{ #category : #rendering }
DiyaNode >> render [
visibility ifFalse:[^self].
root ifNil: [ ^self ].
2022-08-07 18:15:57 +02:00
shader ifNotNil: [self setUpShader].
2022-03-03 19:19:40 +01:00
self draw.
children ifNil: [ ^self ].
root renderNext: children
2022-03-01 22:58:58 +01:00
]
2022-03-19 02:18:29 +01:00
{ #category : #accessing }
DiyaNode >> root [
^ root
]
{ #category : #accessing }
DiyaNode >> root: anObject [
root = anObject ifTrue:[^self].
root := anObject.
children ifNotNil: [
children do:[:c | c root: root]
]
2022-03-19 02:18:29 +01:00
]
2022-03-01 22:58:58 +01:00
{ #category : #accessing }
DiyaNode >> rotation [
^ rotation
]
{ #category : #accessing }
DiyaNode >> rotation: anObject [
rotation := anObject.
self updateTF.
2022-03-01 22:58:58 +01:00
]
{ #category : #accessing }
DiyaNode >> rotation: anObject pivot: p [
rotation := anObject.
pivot := p.
self updateTF.
]
2022-03-01 22:58:58 +01:00
{ #category : #accessing }
DiyaNode >> scale [
^ scale
]
{ #category : #accessing }
DiyaNode >> scale: anObject [
scale := anObject.
self updateTF.
2022-03-01 22:58:58 +01:00
]
{ #category : #'changing state' }
2022-08-07 18:15:57 +02:00
DiyaNode >> setClean [
root ifNil: [ ^self ].
root cleanDirtyNode: self.
2022-08-07 18:15:57 +02:00
]
{ #category : #'changing state' }
2022-08-07 18:15:57 +02:00
DiyaNode >> setDirty [
root ifNil: [ ^self ].
self root enqueueDirtyNode: self.
2022-08-07 18:15:57 +02:00
]
{ #category : #'changing state' }
DiyaNode >> setDirtyAll [
self setDirty.
children ifNotNil: [
children do:[:c| c setDirtyAll] ]
]
{ #category : #rendering }
2022-03-06 18:33:10 +01:00
DiyaNode >> setUpShader [
2022-03-15 19:11:19 +01:00
|mem|
mem := self tf asGLBuffer.
2022-03-06 18:33:10 +01:00
shader use;
setUniform: #u_time value: DiyaSystemClock elapsedTime asFloat;
2022-03-06 18:33:10 +01:00
setUniform: #u_projection value: {GL_FALSE. context projection buffer};
setUniform: #u_resolution value: { context resolution x. context resolution y };
2022-03-15 19:11:19 +01:00
setUniform: #u_transform value: {GL_TRUE. mem}.
2022-03-06 18:33:10 +01:00
context mouse ifNotNil: [
"in shader, window origin is bottom left conor of the window
the mouse position should be transformed to this coodinate"
shader setUniform: #u_mouse value:
{ context mouse x. context mouse y }.
2022-03-06 18:33:10 +01:00
].
2022-03-15 19:11:19 +01:00
mem free.
2022-03-06 18:33:10 +01:00
]
2022-03-01 22:58:58 +01:00
{ #category : #accessing }
DiyaNode >> shader [
2022-03-03 19:19:40 +01:00
shader ifNil: [
parent ifNil: [ ^nil ].
^parent shader ].
2022-03-01 22:58:58 +01:00
^ shader
]
{ #category : #accessing }
DiyaNode >> shader: anObject [
shader := anObject
]
{ #category : #stepping }
DiyaNode >> step [
]
{ #category : #stepping }
DiyaNode >> stepDown [
self step.
children ifNotNil: [ children do:[:c | c stepDown ] ]
]
2022-03-23 00:52:15 +01:00
{ #category : #accessing }
2022-08-07 20:43:16 +02:00
DiyaNode >> styleName [
^ styleName
2022-03-23 00:52:15 +01:00
]
{ #category : #accessing }
2022-08-07 20:43:16 +02:00
DiyaNode >> styleName: anObject [
styleName := anObject.
style := nil.
2022-08-07 20:43:16 +02:00
self setDirty
]
{ #category : #'event handling' }
DiyaNode >> styleNotFound: styleAttr [
DiyaCoreAPIError signal: 'Query undefined style ', styleAttr, ' in', styleName.
2022-03-23 00:52:15 +01:00
]
2022-03-01 22:58:58 +01:00
{ #category : #accessing }
DiyaNode >> tf [
^ tf
]
2022-08-07 18:15:57 +02:00
{ #category : #'event handling' }
2022-03-19 02:18:29 +01:00
DiyaNode >> trigger: evt [
evt enable ifFalse:[^self].
ehandlers at: evt mapped type ifPresent:[:handler|
evt target: self.
handler value: evt].
2022-03-19 02:18:29 +01:00
children ifNil: [^self].
evt enable ifTrue: [
children select: [:node | evt mapped triggableOn: node ] thenDo:[:node| node trigger: evt]
].
]
{ #category : #processing }
2022-03-01 22:58:58 +01:00
DiyaNode >> updateTF [
self subclassResponsibility
]
{ #category : #accessing }
DiyaNode >> visibility [
^ visibility
]
{ #category : #accessing }
DiyaNode >> visibility: anObject [
visibility := anObject
]