| 1 | #include "Library/Stage/StageSwitchUtil.h" |
| 2 | |
| 3 | #include "Library/Placement/PlacementFunction.h" |
| 4 | #include "Library/Stage/IUseStageSwitch.h" |
| 5 | #include "Library/Stage/StageSwitchKeeper.h" |
| 6 | #include "Library/Thread/FunctorV0M.h" |
| 7 | #include "Project/Stage/StageSwitchAccesser.h" |
| 8 | #include "Project/Stage/StageSwitchFunctorListener.h" |
| 9 | |
| 10 | namespace al { |
| 11 | void initStageSwitch(IUseStageSwitch* user, StageSwitchDirector* stageSwitchDirector, |
| 12 | const PlacementInfo& placementInfo) { |
| 13 | if (user->getStageSwitchKeeper()) |
| 14 | return; |
| 15 | |
| 16 | user->initStageSwitchKeeper(); |
| 17 | |
| 18 | s32 linkCount = calcLinkCountClassName(placementInfo, className: "StageSwitch" ); |
| 19 | if (linkCount == 0) |
| 20 | return; |
| 21 | |
| 22 | StageSwitchKeeper* keeper = user->getStageSwitchKeeper(); |
| 23 | keeper->setUseName(user); |
| 24 | keeper->init(director: stageSwitchDirector, placementInfo); |
| 25 | } |
| 26 | |
| 27 | static StageSwitchAccesser* getStageSwitchAccesser(const IUseStageSwitch* user, |
| 28 | const char* linkName) { |
| 29 | StageSwitchKeeper* keeper = user->getStageSwitchKeeper(); |
| 30 | if (!keeper) |
| 31 | return nullptr; |
| 32 | StageSwitchAccesser* accesser = keeper->tryGetStageSwitchAccesser(linkName); |
| 33 | if (!accesser) |
| 34 | return nullptr; |
| 35 | |
| 36 | accesser->isEnableRead(); |
| 37 | return accesser; |
| 38 | } |
| 39 | |
| 40 | bool isValidStageSwitch(const IUseStageSwitch* user, const char* linkName) { |
| 41 | StageSwitchAccesser* accesser = getStageSwitchAccesser(user, linkName); |
| 42 | return accesser && accesser->isValid(); |
| 43 | } |
| 44 | |
| 45 | bool isOnStageSwitch(const IUseStageSwitch* user, const char* linkName) { |
| 46 | StageSwitchAccesser* accesser = getStageSwitchAccesser(user, linkName); |
| 47 | return accesser && accesser->isOnSwitch(); |
| 48 | } |
| 49 | |
| 50 | void onStageSwitch(IUseStageSwitch* user, const char* linkName) { |
| 51 | StageSwitchAccesser* accesser = getStageSwitchAccesser(user, linkName); |
| 52 | if (accesser) |
| 53 | accesser->onSwitch(); |
| 54 | } |
| 55 | |
| 56 | void offStageSwitch(IUseStageSwitch* user, const char* linkName) { |
| 57 | StageSwitchAccesser* accesser = getStageSwitchAccesser(user, linkName); |
| 58 | if (accesser) |
| 59 | accesser->offSwitch(); |
| 60 | } |
| 61 | |
| 62 | bool tryOnStageSwitch(IUseStageSwitch* user, const char* linkName) { |
| 63 | StageSwitchAccesser* accesser = getStageSwitchAccesser(user, linkName); |
| 64 | if (!accesser || !accesser->isValid() || accesser->isOnSwitch()) |
| 65 | return false; |
| 66 | accesser->onSwitch(); |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | bool tryOffStageSwitch(IUseStageSwitch* user, const char* linkName) { |
| 71 | StageSwitchAccesser* accesser = getStageSwitchAccesser(user, linkName); |
| 72 | if (!accesser || !accesser->isValid() || !accesser->isOnSwitch()) |
| 73 | return false; |
| 74 | accesser->offSwitch(); |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | bool isSameStageSwitch(const IUseStageSwitch* user, const IUseStageSwitch* otherUser, |
| 79 | const char* linkName) { |
| 80 | StageSwitchAccesser* accesser = getStageSwitchAccesser(user, linkName); |
| 81 | if (!accesser) |
| 82 | return false; |
| 83 | |
| 84 | StageSwitchAccesser* otherAccesser = getStageSwitchAccesser(user: otherUser, linkName); |
| 85 | if (!otherAccesser) |
| 86 | return false; |
| 87 | |
| 88 | return accesser->isEqualSwitch(other: otherAccesser); |
| 89 | } |
| 90 | |
| 91 | bool isValidSwitchAppear(const IUseStageSwitch* user) { |
| 92 | return isValidStageSwitch(user, linkName: "SwitchAppear" ); |
| 93 | } |
| 94 | |
| 95 | bool isOnSwitchAppear(const IUseStageSwitch* user) { |
| 96 | return isOnStageSwitch(user, linkName: "SwitchAppear" ); |
| 97 | } |
| 98 | |
| 99 | bool isValidSwitchKill(const IUseStageSwitch* user) { |
| 100 | return isValidStageSwitch(user, linkName: "SwitchKill" ); |
| 101 | } |
| 102 | |
| 103 | bool isValidSwitchDeadOn(const IUseStageSwitch* user) { |
| 104 | return isValidStageSwitch(user, linkName: "SwitchDeadOn" ); |
| 105 | } |
| 106 | |
| 107 | void onSwitchDeadOn(IUseStageSwitch* user) { |
| 108 | return onStageSwitch(user, linkName: "SwitchDeadOn" ); |
| 109 | } |
| 110 | |
| 111 | void offSwitchDeadOn(IUseStageSwitch* user) { |
| 112 | return offStageSwitch(user, linkName: "SwitchDeadOn" ); |
| 113 | } |
| 114 | |
| 115 | bool tryOnSwitchDeadOn(IUseStageSwitch* user) { |
| 116 | return tryOnStageSwitch(user, linkName: "SwitchDeadOn" ); |
| 117 | } |
| 118 | |
| 119 | bool tryOffSwitchDeadOn(IUseStageSwitch* user) { |
| 120 | return tryOffStageSwitch(user, linkName: "SwitchDeadOn" ); |
| 121 | } |
| 122 | |
| 123 | bool isValidSwitchStart(const IUseStageSwitch* user) { |
| 124 | return isValidStageSwitch(user, linkName: "SwitchStart" ); |
| 125 | } |
| 126 | |
| 127 | bool isOnSwitchStart(const IUseStageSwitch* user) { |
| 128 | return isOnStageSwitch(user, linkName: "SwitchStart" ); |
| 129 | } |
| 130 | |
| 131 | bool listenStageSwitchOn(IUseStageSwitch* user, const char* eventName, const FunctorBase& action) { |
| 132 | StageSwitchAccesser* accesser = getStageSwitchAccesser(user, linkName: eventName); |
| 133 | if (!accesser || !accesser->isValid()) |
| 134 | return false; |
| 135 | |
| 136 | auto* listener = new StageSwitchFunctorListener(); |
| 137 | listener->setOnFunctor(action); |
| 138 | accesser->addListener(listener); |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | bool listenStageSwitchOff(IUseStageSwitch* user, const char* eventName, const FunctorBase& action) { |
| 143 | StageSwitchAccesser* accesser = getStageSwitchAccesser(user, linkName: eventName); |
| 144 | if (!accesser || !accesser->isValid()) |
| 145 | return false; |
| 146 | |
| 147 | auto* listener = new StageSwitchFunctorListener(); |
| 148 | listener->setOffFunctor(action); |
| 149 | accesser->addListener(listener); |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | bool listenStageSwitchOnOff(IUseStageSwitch* user, const char* eventName, |
| 154 | const FunctorBase& actionOn, const FunctorBase& actionOff) { |
| 155 | StageSwitchAccesser* accesser = getStageSwitchAccesser(user, linkName: eventName); |
| 156 | if (!accesser || !accesser->isValid()) |
| 157 | return false; |
| 158 | |
| 159 | auto* listener = new StageSwitchFunctorListener(); |
| 160 | listener->setOnFunctor(actionOn); |
| 161 | listener->setOffFunctor(actionOff); |
| 162 | accesser->addListener(listener); |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | bool listenStageSwitchOnAppear(IUseStageSwitch* user, const FunctorBase& action) { |
| 167 | return listenStageSwitchOn(user, eventName: "SwitchAppear" , action); |
| 168 | } |
| 169 | |
| 170 | bool listenStageSwitchOnOffAppear(IUseStageSwitch* user, const FunctorBase& actionOn, |
| 171 | const FunctorBase& actionOff) { |
| 172 | return listenStageSwitchOnOff(user, eventName: "SwitchStart" , actionOn, actionOff); |
| 173 | } |
| 174 | |
| 175 | bool listenStageSwitchOnKill(IUseStageSwitch* user, const FunctorBase& action) { |
| 176 | return listenStageSwitchOn(user, eventName: "SwitchKill" , action); |
| 177 | } |
| 178 | |
| 179 | bool listenStageSwitchOnOffKill(IUseStageSwitch* user, const FunctorBase& actionOn, |
| 180 | const FunctorBase& actionOff) { |
| 181 | return listenStageSwitchOnOff(user, eventName: "SwitchKill" , actionOn, actionOff); |
| 182 | } |
| 183 | |
| 184 | bool trySyncStageSwitchOnOffAppear(IUseStageSwitch* user, const FunctorBase& actionOn, |
| 185 | const FunctorBase& actionOff) { |
| 186 | if (listenStageSwitchOnOffAppear(user, actionOn, actionOff)) { |
| 187 | actionOff(); |
| 188 | return true; |
| 189 | } else { |
| 190 | actionOn(); |
| 191 | return false; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | bool trySyncStageSwitchOnOffKill(IUseStageSwitch* user, const FunctorBase& actionOn, |
| 196 | const FunctorBase& actionOff) { |
| 197 | bool listened = listenStageSwitchOnOffKill(user, actionOn, actionOff); |
| 198 | actionOff(); |
| 199 | return listened; |
| 200 | } |
| 201 | |
| 202 | bool trySyncStageSwitchOnOffAppearAndKill(IUseStageSwitch* user, const FunctorBase& actionOn, |
| 203 | const FunctorBase& actionOff) { |
| 204 | if (trySyncStageSwitchOnOffAppear(user, actionOn, actionOff)) |
| 205 | return true; |
| 206 | |
| 207 | return trySyncStageSwitchOnOffKill(user, actionOn: actionOff, actionOff: actionOn); |
| 208 | } |
| 209 | |
| 210 | bool listenStageSwitchOnStart(IUseStageSwitch* user, const FunctorBase& action) { |
| 211 | return listenStageSwitchOn(user, eventName: "SwitchStart" , action); |
| 212 | } |
| 213 | |
| 214 | bool listenStageSwitchOnOffStart(IUseStageSwitch* user, const FunctorBase& actionOn, |
| 215 | const FunctorBase& actionOff) { |
| 216 | return listenStageSwitchOnOff(user, eventName: "SwitchStart" , actionOn, actionOff); |
| 217 | } |
| 218 | |
| 219 | bool listenStageSwitchOnStop(IUseStageSwitch* user, const FunctorBase& action) { |
| 220 | return listenStageSwitchOn(user, eventName: "SwitchStop" , action); |
| 221 | } |
| 222 | |
| 223 | } // namespace al |
| 224 | |