-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.ms
More file actions
211 lines (190 loc) · 5.93 KB
/
Timer.ms
File metadata and controls
211 lines (190 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*! © 2022 imaoki | MIT License | https://github.com/imaoki */
/*-
.NETの`Timer`オブジェクトの`Tick`イベントのイベントハンドラ。
@param sender <DotNetObject:System.Windows.Forms.Timer> イベント発生元の`Timer`オブジェクト。
@param event <DotNetObject:System.EventArgs> イベントデータ。
@returns <OkClass>
*/
global mxsDotNetTimerTickHandler = fn mxsDotNetTimerTickHandler sender event = (
if classOf sender == DotNetObject \
and (DotNet.GetType "System.Windows.Forms.Timer").IsInstanceOfType sender do (
if classOf sender.Tag == DotNetMxsValue do (
local timerObject = sender.Tag.Value
if isStruct timerObject \
and isProperty timerObject #StructName \
and classOf timerObject.StructName == MAXScriptFunction \
and timerObject.StructName() == #TimerStruct do (
timerObject.UpdateTicks()
)
)
)
ok
)
/*-
.NETの[Timer](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.timer?view=netframework-4.8)のラッパー。
@remarks 作成パラメータ
: ```maxscript
TimerStruct [interval]
```
通知
: | 通知名 | 通知データ | タイミング |
| ----------- | ------------------------ | ------------------------------------ |
| `#Interval` | 新たな発生間隔(ミリ秒) | `Tick`イベントの発生間隔を設定した後 |
| `#Ticks` | 自己インスタンス | `Tick`イベントが発生した直後 |
*/
struct TimerStruct (
/*- @prop <Point> 作成パラメータ1。`Tick`イベントの発生間隔(ミリ秒)。既定値は`100`。 */
public _CP1_ = 100,
/*- @prop <Struct:DotNetUtilityStruct> */
private dotNetUtility,
/*- @prop <Integer> `Tick`イベントが発生回数。既定値は`0`。 */
private ticks = 0,
/*- @prop <DotNetObject:System.Windows.Forms.Timer> */
private timer,
/*
public fn GetInterval = (),
public fn GetTicks = (),
public fn GetTimer = (),
public fn IsEnabled = (),
public fn Reset = (),
public fn Restart = (),
public fn SetInterval input = (),
public fn Start = (),
public fn Stop = (),
public fn UpdateTicks = (),
*/
/*-
`Tick`イベントの発生間隔(ミリ秒)を取得する。
@returns <Integer>
*/
public fn GetInterval = (
this.timer.Interval
),
/*-
`Tick`イベントが発生回数を取得する。
@returns <Integer>
*/
public fn GetTicks = (
this.ticks
),
/*-
`Timer`オブジェクトを取得する。
@returns <DotNetObject:System.Windows.Forms.Timer>
*/
public fn GetTimer = (
this.timer
),
/*-
タイマーが実行されているかどうかを取得する。
@returns <BooleanClass>
*/
public fn IsEnabled = (
this.timer.Enabled
),
/*-
タイマーを停止して`ticks`を`0`にリセットする。
@returns <OkClass>
@remarks `ticks`は変更するが通知は発行しない。
*/
public fn Reset = (
this.Stop()
this.ticks = 0
ok
),
/*-
タイマーを停止して`ticks`を`0`にリセットし、再度タイマーを起動する。
@returns <OkClass>
*/
public fn Restart = (
this.Reset()
this.Start()
ok
),
/*-
`Tick`イベントの発生間隔(ミリ秒)を設定する。
@param input <Integer> `1`以上の値。
@returns <Integer> 新たに設定された値。
*/
public fn SetInterval input = (
if input < 1 do input = 1
this.timer.Interval = input
this.StateChanged.Notify #Interval this.timer.Interval
this.GetInterval()
),
/*-
タイマーを起動する。
@returns <OkClass>
*/
public fn Start = (
this.timer.Start()
ok
),
/*-
タイマーを停止する。
@returns <OkClass>
*/
public fn Stop = (
this.timer.Stop()
ok
),
/*-
`Tick`イベントが発生した時に呼ばれ、`ticks`を更新する。
@returns <Integer> 新たに設定された`ticks`の値。
*/
public fn UpdateTicks = (
this.ticks += 1
this.StateChanged.Notify #Ticks this
this.GetTicks()
),
/*- @returns <Name> */
public fn StructName = #TimerStruct,
/*-
@param indent: <String>
@param out: <FileStream|StringStream|WindowStream> 出力先。既定値は`listener`。
@returns <OkClass>
*/
public fn Dump indent:"" out:listener = (
format "%TimerStruct\n" indent to:out
format "% ticks:%\n" indent this.ticks to:out
ok
),
/*-
@param obj <Any>
@returns <BooleanClass>
@remarks 大文字と小文字を区別する。
*/
public fn Equals obj = (
local isEqualStructName = isStruct obj \
and isProperty obj #StructName \
and classOf obj.StructName == MAXScriptFunction \
and obj.StructName() == this.StructName()
local isEqualProperties = isProperty obj #GetInterval \
and classOf obj.GetInterval == MAXScriptFunction \
and obj.GetInterval() == this.GetInterval() \
and isProperty obj #GetTicks \
and classOf obj.GetTicks == MAXScriptFunction \
and obj.GetTicks() == this.GetTicks() \
and isProperty obj #GetTimer \
and classOf obj.GetTimer == MAXScriptFunction
if isEqualProperties do (
local ov = obj.GetTimer()
local tv = this.GetTimer()
isEqualProperties = this.dotNetUtility.IsInstanceOf "System.Windows.Forms.Timer" ov \
and ov.Equals tv
)
isEqualStructName and isEqualProperties
),
/*- @prop <Struct:ObservableStruct> */
public StateChanged,
on Create do (
this.StateChanged = (::standardDefinitionPool[@"Observable.ms"])()
this.dotNetUtility = (::standardDefinitionPool[@"DotNetUtility.ms"])()
this.timer = DotNetObject "System.Windows.Forms.Timer"
if classOf this._CP1_ == Integer do (
this.SetInterval this._CP1_
)
this.timer.Tag = DotNetMxsValue this
DotNet.RemoveEventHandlers this.timer "Tick"
DotNet.AddEventHandler this.timer "Tick" ::mxsDotNetTimerTickHandler
)
)