Classes are just class objects stored in super global variables.
With %% we can look up a variable by its name.
Nested classes are stored inside the parent class in a key with their name.
Generally AHK uses this naming Scheme for subclasses: "ParentClassName.ChildClassName"
A class object that you got in any way can be used just like the variable itself:
e.g.
rect1 := new Rectangle() is the same as:
Code: Select all
var := Rectangle
rect1 := new var()
With this knowledge we can create and use a general purpose classLookup function:
Code: Select all
classLookup(name) {
Local _splitName, _branch, _branchName, _each ;defining the local variables will make the function assume global
;it's not necessary but will prevent other super-globals from interfering with those values
_splitName := StrSplit(name, ".") ;split up the string at the . to get the seperate parts of the name
_branchName := _splitName.removeAt(1) ;get the first part - the name of the top-level parent class
_branch := %_branchName% ;get the top-level parent class object
for _each,_branchName in _splitName { ;the remaining parts of the name are nested classes
_branch := ObjRawGet(_branch, _branchName) ;look up the nested class inside the current parent
}
return _branch ;finally return what we looked up
}
This function is able to look up any AHK class-object inside your script.
Some tests:
Code: Select all
classLookup("A").msg()
classLookup("A.B").msg()
classLookup("A.C.D.E").msg()
class A {
class B {
msg() {
Msgbox Hello this is class A.B
}
}
class C {
class D {
class E {
msg() {
Msgbox Hello World
}
}
}
}
msg() {
Msgbox This is the containing class
}
}
classLookup(name) {
Local _splitName, _branch, _branchName, _each ;defining the local variables will make the function assume global
;it's not necessary but will prevent other super-globals from interfering with those values
_splitName := StrSplit(name, ".") ;split up the string at the . to get the seperate parts of the name
_branchName := _splitName.removeAt(1) ;get the first part - the name of the top-level parent class
_branch := %_branchName% ;get the top-level parent class object
for _each,_branchName in _splitName { ;the remaining parts of the name are nested classes
_branch := ObjRawGet(_branch, _branchName) ;look up the nested class inside the current parent
}
return _branch ;finally return what we looked up
}