/
MPP_ScrollBar.js
309 lines (282 loc) · 9.83 KB
/
MPP_ScrollBar.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//=============================================================================
// MPP_ScrollBar.js
//=============================================================================
// Copyright (c) 2019 Mokusei Penguin
// Released under the MIT license
// http://opensource.org/licenses/mit-license.php
//=============================================================================
/*:
* @plugindesc 【ver.2.2】將滾動條添加到視窗。
* @author 木星ペンギン( 翻譯 : ReIris )
*
* @help ※與滑鼠觸碰操作改善 [MPP_SimpleTouch3.js] 一起使用時,
* 請在下方安裝此插件。
*
* ● 拖曳滾動
* 您可以通過拖曳來控制滾動條。
* 操作的範圍判斷是從視窗右邊緣開始的寬度。
* 滾動條的寬度與範圍判斷的寬度無關。
*
* ================================
* 制作 : 木星ペンギン
* URL : http://woodpenguin.blog.fc2.com/
*
* @param Veiw Time
* @type number
* @desc 表示時間
* (0で常に表示)
* @default 60
*
* @param Scroll Bar Color1
* @text 滾動條顏色 1
* @desc 滾動條的顏色。
* @default 160,160,160
*
* @param Scroll Bar Color2
* @text 滾動條顏色 2
* @desc 滾動條的顏色。
* @default 192,192,192
*
* @param Scroll Bar Width
* @text 滾動條寬度
* @type number
* @min 1
* @desc 滾動條的寬度。
* @default 6
*
* @param Right Padding
* @text 右方留白
* @type number
* @desc 滾動條的右方留白。
* @default 5
*
* @param Hitbox Width
* @text 判定範圍寬度
* @type number
* @min 1
* @desc 滾動條操作範圍判定的寬度。
* @default 24
*
*
*
*/
(function () {
var MPPlugin = {};
(function () {
MPPlugin.contains = {};
MPPlugin.contains['SimpleTouch3'] = $plugins.some(function(plugin) {
return (plugin.name === 'MPP_SimpleTouch3' && plugin.status);
});
var parameters = PluginManager.parameters('MPP_ScrollBar');
MPPlugin.VeiwTime = Number(parameters['Veiw Time'] || 0);
MPPlugin.ScrollBarColor1 = 'rgb(%1)'.format(parameters['Scroll Bar Color1'] || '128,128,128');
MPPlugin.ScrollBarColor2 = 'rgb(%1)'.format(parameters['Scroll Bar Color2'] || '192,192,192');
MPPlugin.ScrollBarWidth = Number(parameters['Scroll Bar Width'] || 6);
MPPlugin.RightPadding = Number(parameters['Right Padding'] || 5);
MPPlugin.HitboxWidth = Number(parameters['Hitbox Width'] || 18);
})();
var Alias = {};
//-----------------------------------------------------------------------------
// Window_Selectable
//
if (Window_Selectable.prototype.hasOwnProperty('updateTransform')) {
Alias.WiSe_updateTransform = Window_Selectable.prototype.updateTransform;
}
Window_Selectable.prototype.updateTransform = function () {
this._updateScrollBar();
if (Alias.WiSe_updateTransform) {
Alias.WiSe_updateTransform.call(this);
} else {
Window_Base.prototype.updateTransform.call(this);
}
};
//
if (Window_Selectable.prototype.hasOwnProperty('_refreshAllParts')) {
Alias.WiSe__refreshAllParts = Window_Selectable.prototype._refreshAllParts;
}
Window_Selectable.prototype._refreshAllParts = function () {
this._refreshScrollBar();
if (Alias.WiSe__refreshAllParts) {
Alias.WiSe__refreshAllParts.call(this);
} else {
Window_Base.prototype._refreshAllParts.call(this);
}
};
Window_Selectable.prototype._refreshScrollBar = function () {
var maxRow = this.maxRows();
var maxPageRows = this.maxPageRows();
if (maxRow > maxPageRows) {
if (!this._scrollBarSprite) {
this._scrollBarSprite = new Sprite();
this._scrollBarSprite.opacity = 0;
this.addChild(this._scrollBarSprite);
}
var height = this.height - this.standardPadding() * 2;
var barWidth = MPPlugin.ScrollBarWidth;
var barHeight = Math.ceil(height * maxPageRows / maxRow);
var bitmap = new Bitmap(barWidth, barHeight);
var context = bitmap.context;
var grad = context.createLinearGradient(0, 0, 0, barHeight);
grad.addColorStop(0, MPPlugin.ScrollBarColor1);
grad.addColorStop(0.5, MPPlugin.ScrollBarColor2);
grad.addColorStop(1, MPPlugin.ScrollBarColor1);
context.save();
context.fillStyle = grad;
context.fillRect(0, 0, barWidth, barHeight);
context.restore();
bitmap._setDirty();
this._scrollBarSprite.bitmap = bitmap;
}
};
Window_Selectable.prototype._updateScrollBar = function () {
if (this._scrollBarSprite) {
var barX = this.width - MPPlugin.RightPadding - MPPlugin.ScrollBarWidth;
var topY = this._scrollY + this.origin.y;
var maxHeight = this.maxRows() * this.itemHeight();
var height = this.height - this.standardPadding() * 2;
var barY = Math.floor(height * topY / maxHeight) + this.standardPadding();
this._scrollBarSprite.x = barX;
this._scrollBarSprite.y = barY;
if (this._lastOy !== this.origin.y) {
this._lastOy = this.origin.y;
this._scrollBarCount = MPPlugin.VeiwTime;
}
if (this._scrollBarCount > 0) {
this._scrollBarCount--;
}
if (this.isScrollBarActive()) {
this._scrollBarSprite.opacity += 32;
} else {
this._scrollBarSprite.opacity -= 4;
}
if (this.isOpen() && this.maxRows() > this.maxPageRows()) {
this._scrollBarSprite.visible = true;
} else {
this._scrollBarSprite.visible = false;
}
}
};
Window_Selectable.prototype.isScrollBarActive = function() {
return (this.active && (MPPlugin.VeiwTime === 0 || this._scrollBarCount > 0));
};
//13
Alias.WiSe_initialize = Window_Selectable.prototype.initialize;
Window_Selectable.prototype.initialize = function(x, y, width, height) {
Alias.WiSe_initialize.call(this, x, y, width, height);
this._scrollBarType = -1;
this._scrollBarDragY = 0;
this._scrollBarCount = 0;
};
//
if (Window_Selectable.prototype.hasOwnProperty('createContents')) {
Alias.WiSe_createContents = Window_Selectable.prototype.createContents;
}
Window_Selectable.prototype.createContents = function() {
if (Alias.WiSe_createContents) {
Alias.WiSe_createContents.call(this);
} else {
Window_Base.prototype.createContents.call(this);
}
this._refreshScrollBar();
};
//110
Alias.WiSe_setTopRow = Window_Selectable.prototype.setTopRow;
Window_Selectable.prototype.setTopRow = function(row) {
Alias.WiSe_setTopRow.call(this, row);
this._scrollBarCount = MPPlugin.VeiwTime;
};
//340
Alias.WiSe_processTouch = Window_Selectable.prototype.processTouch;
Window_Selectable.prototype.processTouch = function() {
if (this.isOpenAndActive()) {
if (TouchInput.isTriggered() && this.isTouchedInsideScrollBox()) {
var y = this.canvasToLocalY(TouchInput.y);
var by = this._scrollBarSprite.y;
var bh = this._scrollBarSprite.height;
if (y < by) {
this._scrollBarType = 0;
} else if (y < by + bh) {
this._scrollBarType = 1;
this._scrollBarDragY = TouchInput.y;
} else {
this._scrollBarType = 2;
}
this._scrollBarCount = MPPlugin.VeiwTime;
}
if (this._scrollBarType >= 0) {
if (TouchInput.isPressed()) {
switch (this._scrollBarType) {
case 0:
if (TouchInput.isRepeated()) {
this.scrollBarPageup();
}
break;
case 1:
this.onScrollBarDrag();
break;
case 2:
if (TouchInput.isRepeated()) {
this.scrollBarPagedown();
}
break;
}
} else {
this._scrollBarType = -1;
}
}
} else {
this._scrollBarType = -1;
}
if (this._scrollBarType < 0) {
Alias.WiSe_processTouch.call(this);
}
};
Window_Selectable.prototype.isTouchedInsideScrollBox = function() {
if (!this._scrollBarSprite || !this._scrollBarSprite.visible) {
return false;
}
var x = this.canvasToLocalX(TouchInput.x);
var y = this.canvasToLocalY(TouchInput.y);
var rx = this.width - MPPlugin.HitboxWidth;
return x >= rx && y >= 0 && x < this.width && y < this.height;
};
Window_Selectable.prototype.scrollBarPagedown = function() {
var y = this.canvasToLocalY(TouchInput.y);
var by = this._scrollBarSprite.y;
var bh = this._scrollBarSprite.height;
if (y >= by + bh) {
if (MPPlugin.contains['SimpleTouch3']) {
this.gainOy(this.maxPageRows() * this.itemHeight());
} else {
this.setTopRow(this.topRow() + this.maxPageRows());
}
}
};
Window_Selectable.prototype.scrollBarPageup = function() {
var y = this.canvasToLocalY(TouchInput.y);
var by = this._scrollBarSprite.y;
if (y < by) {
if (MPPlugin.contains['SimpleTouch3']) {
this.gainOy(-this.maxPageRows() * this.itemHeight());
} else {
this.setTopRow(this.topRow() - this.maxPageRows());
}
}
};
Window_Selectable.prototype.onScrollBarDrag = function() {
var sy = TouchInput.y - this._scrollBarDragY;
if (sy !== 0) {
var oy = this._scrollY + this.origin.y;
var height = this.height - this.standardPadding() * 2;
var amount = sy * this.maxRows() / height;
if (MPPlugin.contains['SimpleTouch3']) {
this.gainOy(amount * this.itemHeight());
} else {
this.setTopRow(this.topRow() + Math.round(amount));
}
if (oy !== this._scrollY + this.origin.y) {
this._scrollBarDragY = TouchInput.y;
}
}
};
})();