查看: 5833|回复: 4

[原创发布] 滚动窗口

[复制链接]
梦石
0
星屑
755
在线时间
90 小时
注册时间
2017-11-6
回帖
10
发表于 2020-6-13 21:30:34 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 lohengriny 于 2020-6-13 21:33 编辑

纯鼠标操作滚动窗口。包含两个,一个是普通滚动窗口,在窗口区域按住鼠标左键,然后移动鼠标即可滚动内容(仅Y轴)。
另一个是 Selectable 滚动窗口,即普通滚动窗口之外还含有可选择内容(比如物品列表,菜单等)。和普通滚动窗口相比有两个特性:
1. 滚动后不会触发选择。
2. isSelectionMaintained() 该项如果为真,在已经选择一个项时滚动,会保留选择,否者会取消选择。

其他特性:
1.惯性。滚动中松开鼠标,内容会继续滑动一段距离
2.弹性。滚动可以超过上下边界,但是在松开鼠标后回“弹回”边界位置
3.滚动条。 默认显示滚动条,将一个长条图片放到 img/system/window_scroller.png 即可。

代码
  1. //=============================================================================
  2. // UI_ScrollWindow.js
  3. //=============================================================================
  4. /*:
  5. * @plugindesc Scrollable Window
  6. * @author Hoozuki Araragi
  7. *
  8. * @help This plugin does not provide plugin commands.
  9. */
  10. class Window_Scroll extends Window_Base {
  11.         /**
  12.          *
  13.          * @param {Number} fX
  14.          * @param {Number} fY
  15.          * @param {Number} fWidth
  16.          * @param {Number} fHeight
  17.          */
  18.     constructor(fX, fY, fWidth, fHeight) {
  19.         super(fX, fY, fWidth, fHeight);
  20.         this._bTouching = false;
  21.         this._bTouched = false;
  22.         this._fScrollX = 0;
  23.         this._fScrollY = 0;
  24.         this._fScrollingX = 0;
  25.         this._fScrollingY = 0;
  26.         this._fScrollOriginX = 0;
  27.         this._fScrollOriginY = 0;
  28.         this._fScrollMaxX = 0;
  29.         this._fScrollMaxY = 0;
  30.         this._fScrollMinX = 0;
  31.         this._fScrollMinY = 0;
  32.         this._fScrollerHeight = 0;
  33.         this._bVertical = true;
  34.         this._bHorizontal = false;
  35.         this._inertia = 0;
  36.         this._lastY = 0;
  37.         this._oHandlers = {};
  38.     }

  39.     static get inertiaAttenuation() {return 0.94;}

  40.     standardPadding() {
  41.         return 2;
  42.     }
  43.     standardPaddingY() {
  44.         return 2;
  45.     }

  46.     move(x, y, w, h) {
  47.         super.move(x, y, w, h);
  48.     }

  49.     /**
  50.      *
  51.      * @param {string} symbol
  52.      * @param {function} method
  53.      */
  54.     setHandler(symbol, method) {
  55.         this._oHandlers[symbol] = method;
  56.     }

  57.     /**
  58.      *
  59.      * @param {string} symbol
  60.      */
  61.     isHandled(symbol) {
  62.         return !!this._oHandlers[symbol];
  63.     }

  64.     /**
  65.      *
  66.      * @param {string} symbol
  67.      */
  68.     callHandler(symbol) {
  69.         if (this.isHandled(symbol)) {
  70.             this._oHandlers[symbol]();
  71.         }
  72.     }

  73.     get scrollBarOffset() {
  74.         return 4;
  75.     }

  76.     get scrollBarWidth() {
  77.         return 4;
  78.     }

  79.     get scrollerHeight() {
  80.         if (this._fScrollerHeight === 0) {
  81.             let l = this._scrollBar.height + this._fScrollMinY / 4;
  82.             this._fScrollerHeight = Math.max(l, 30);
  83.         }
  84.         return this._fScrollerHeight;
  85.     }

  86.     get scrollBarColor() {
  87.         return '#000000';
  88.     }

  89.     get scrollerColor() {
  90.         return '#000000'
  91.     }

  92.     createScrollBar() {
  93.         let bg = new Sprite(new Bitmap(this.scrollBarWidth, this.height - 2 * this.scrollBarOffset));
  94.         bg.bitmap.fillRect(0, 0, this.scrollBarWidth, this.height - 2 * this.scrollBarOffset, this.scrollBarColor);
  95.         this._scrollBar = bg;
  96.         this.addChild(bg);
  97.         bg.move(this.width - this.scrollBarWidth - 4, this.scrollBarOffset);
  98.     }

  99.     createScroller() {
  100.         let l = this.scrollerHeight;
  101.         let item = new Sprite(new Bitmap(this.scrollBarWidth, l));
  102.         item.bitmap.drawItemBox(0, 0, this.scrollBarWidth, l, 'img/system/', 'window_scroller', 0, 2);
  103.         this._scroller = item;
  104.         this.addChild(item);
  105.         item.move(this.width - this.scrollBarWidth - 4, this.scrollBarOffset);
  106.     }

  107.     processTouch() {
  108.         if (TouchInput.isCancelled()) {
  109.             this.processCancel();
  110.         }
  111.     }

  112.     processCancel() {
  113.         if (this.isHandled('cancel')) {
  114.             SoundManager.playCancel();
  115.             this.callHandler('cancel');
  116.         }
  117.     }

  118.     contentsHeight() {
  119.         return this.height - this.standardPaddingY() * 2 - this._fScrollMinY;
  120.     }

  121.     contentsWidth() {
  122.         return this.width - this.standardPadding() * 2 - this._fScrollMinX;
  123.     }

  124.     /**
  125.      *
  126.      * @param fYmax {number}
  127.      * @param fYmin {number}
  128.      * @param fXmax {number}
  129.      * @param fXmin {number}
  130.      */
  131.     setScrollLimit(fYmax, fYmin, fXmax, fXmin) {
  132.         this._fScrollMaxX = fXmax;
  133.         this._fScrollMaxY = fYmax;
  134.         this._fScrollMinX = fXmin;
  135.         this._fScrollMinY = fYmin;
  136.         this.createContents();
  137.         if (fYmin < 0) {
  138.             this.createScrollBar();
  139.             this.createScroller();
  140.         }
  141.     }

  142.     /**
  143.      *
  144.      * @param horz {boolean}
  145.      * @param vert {boolean}
  146.      */
  147.     setScrollDirection(horz, vert) {
  148.         this._bVertical = vert;
  149.         this._bHorizontal = horz;
  150.     }

  151.     get scrollX() {
  152.         return this._fScrollX + this._fScrollingX;
  153.     }

  154.     get scrollY() {
  155.         return this._fScrollY + this._fScrollingY;
  156.     }

  157.     resetScroll() {
  158.         this._fScrollX = 0;
  159.         this._fScrollY = 0;
  160.     }

  161.     update() {
  162.         super.update();
  163.         this.updateTouching();
  164.         this.processScroll();
  165.         this.updateScroll();
  166.         this.updateContentScroll();
  167.         this.updateScrollBar();
  168.         this.processRelease();
  169.         this.processTouch();
  170.     }

  171.     isMouseOver() {
  172.         var x = this.parent.canvasToLocalX? this.parent.canvasToLocalX(TouchInput.x) : TouchInput.x;
  173.         var y = this.parent.canvasToLocalY? this.parent.canvasToLocalY(TouchInput.y) : TouchInput.y;
  174.         return x >= this.x && x <= this.x + this.width && y >= this.y && y <= this.y + this.height;
  175.     }

  176.     updateTouching() {
  177.         if (this.active) {
  178.             if (TouchInput.isPressed() && this.isMouseOver()) {
  179.                 if (!this._bTouching) {
  180.                     this._fScrollOriginX = TouchInput.x;
  181.                     this._fScrollOriginY = TouchInput.y;
  182.                 }
  183.                 this._bTouching = true;
  184.                 this._bTouched = true;
  185.             } else {
  186.                 this._bTouching = false;
  187.                 if (this._inertia > 1 || this._inertia < -1) {
  188.                     this._fScrollY += this._inertia;
  189.                     this._inertia = this._inertia * Window_Scroll.inertiaAttenuation;
  190.                 }
  191.             }
  192.         } else {
  193.             this._bTouching = false;
  194.         }
  195.     }

  196.     processRelease() {
  197.         if (this.active) {
  198.             if (!this._bTouching && this._bTouched) {
  199.                 this._fScrollX += this._fScrollingX;
  200.                 this._fScrollY += this._fScrollingY;
  201.                 this._fScrollingX = 0;
  202.                 this._fScrollingY = 0;
  203.                 this._bTouched = false;
  204.             }
  205.         } else {
  206.             this._bTouching = false;
  207.         }
  208.     }

  209.     processScroll() {
  210.         if (this.active) {
  211.             if (this._bTouching){
  212.                 let x = TouchInput.x;
  213.                 let y = TouchInput.y;
  214.                 this._bHorizontal? this._fScrollingX = x - this._fScrollOriginX : this._fScrollingX = 0;
  215.                 this._bVertical? this._fScrollingY = y - this._fScrollOriginY : this._fScrollingY = 0;
  216.                 this._inertia = y - this._lastY;
  217.                 this._lastY = y;
  218.             }
  219.         }
  220.     }

  221.     updateScroll() {
  222.         if (this._fScrollX > this._fScrollMaxX) {
  223.             this._fScrollX -= (this._fScrollX - this._fScrollMaxX) / 3;
  224.             if (this._fScrollX - this._fScrollMaxX < 1) this._fScrollX = this._fScrollMaxX;
  225.         }
  226.         if (this._fScrollY > this._fScrollMaxY) {
  227.             this._fScrollY -= (this._fScrollY - this._fScrollMaxY) / 3;
  228.             if (this._fScrollY - this._fScrollMaxY < 1) this._fScrollY = this._fScrollMaxY;
  229.         }

  230.         if (this._fScrollX < this._fScrollMinX) {
  231.             this._fScrollX -= (this._fScrollX - this._fScrollMinX) / 3;
  232.             if (this._fScrollMinX - this._fScrollX < 1) this._fScrollX = this._fScrollMinX;
  233.         }
  234.         if (this._fScrollY < this._fScrollMinY) {
  235.             this._fScrollY -= (this._fScrollY - this._fScrollMinY) / 3;
  236.             if (this._fScrollMinY - this._fScrollY < 1) this._fScrollY = this._fScrollMinY;
  237.         }
  238.     }

  239.     updateScrollBar() {
  240.         if (!this._scrollBar) return;
  241.         this._scrollBar.move(this.width - this.scrollBarWidth - 4, this.scrollBarOffset);
  242.         if (this._scroller) {
  243.             let y = this.scrollBarOffset + (this._scrollBar.height - this.scrollerHeight) * this.scrollY / this._fScrollMinY;
  244.             this._scroller.move(this.width - this.scrollBarWidth - 4, y);
  245.             if ( y + this.scrollerHeight > this.height - this.scrollBarOffset) {
  246.                 this._scroller.setFrame(0, 0, this.scrollBarWidth, this.height - this.scrollBarOffset - y);
  247.             } else if ( y  < this.scrollBarOffset) {
  248.                 this._scroller.move(this.width - this.scrollBarWidth - 4, this.scrollBarOffset);
  249.                 this._scroller.setFrame(0, this.scrollBarOffset, this.scrollBarWidth, this.scrollerHeight - (this.scrollBarOffset - y));
  250.             } else {
  251.                 this._scroller.setFrame(0, 0, this.scrollBarWidth, this.scrollerHeight);
  252.             }
  253.         }
  254.     }

  255.     updateContentScroll() {
  256.         this.origin.x = -this.scrollX;
  257.         this.origin.y = -this.scrollY;
  258.     }
  259. }

  260. class Window_SelectableScroll extends Window_Scroll {
  261.     constructor(x, y, w, h) {
  262.         super(x, y, w, h);
  263.         this._index = -1;
  264.     }

  265.     get index() {
  266.         return this._index;
  267.     }

  268.     get maxCols() {
  269.         return 1;
  270.     }

  271.     get maxItems() {
  272.         return 0;
  273.     };

  274.     get spacing() {
  275.         return 12;
  276.     }

  277.     get itemWidth() {
  278.         return (this.width - this.standardPadding() * 2) / this.maxCols - this.spacing;
  279.     }

  280.     get itemHeight() {
  281.         return this.lineHeight();
  282.     }

  283.     get maxRows() {
  284.         return Math.max(Math.ceil(this.maxItems / this.maxCols), 1);
  285.     }
  286.     get evaluateScrollLimitY() {
  287.         return Math.min(this.height - (this.itemHeight + this.spacing) * this.maxRows - 2 * this.standardPaddingY(), 0);
  288.     }
  289.     get evaluateScrollLimitX() {
  290.         return Math.min(this.width - (this.itemWidth + this.spacing) * this.maxCols - 2 * this.standardPadding(), 0);
  291.     }
  292.     get itemBox() {
  293.         return ['img/system/', 'itembox', 10];
  294.     }
  295.     get selectedBorder() {
  296.         return ['img/system/', 'itemboxframe', 8];
  297.     }

  298.     /**
  299.      * determine if the selection is maintained while scroll;
  300.      * @returns {boolean}
  301.      */
  302.     isSelectionMaintained() {
  303.         return true;
  304.     }
  305.     /**
  306.      * select indicated index
  307.      * @param {number} index
  308.      */
  309.     select(index) {
  310.         let last_index  = this.index;
  311.         this._index = index;
  312.         this.redrawCurrentItem();
  313.         this.redrawItem(last_index);

  314.     }

  315.     deselect() {
  316.         this.select(-1);
  317.     }

  318.     reselect() {
  319.         this.select(-1);
  320.     }

  321.     /**
  322.      *
  323.      * @param {number} index
  324.      * @return {Rectangle}
  325.      */
  326.     itemRect(index) {
  327.         let rect = new Rectangle();
  328.         let maxCols = this.maxCols;
  329.         rect.width = this.itemWidth;
  330.         rect.height = this.itemHeight;
  331.         rect.x = this.standardPadding() + index % maxCols * (rect.width + this.spacing);
  332.         rect.y = this.standardPaddingY() + Math.floor(index / maxCols) * rect.height;
  333.         return rect;
  334.     }

  335.     itemRectForText(index) {
  336.         let rect = this.itemRect(index);
  337.         rect.x += this.textPadding();
  338.         rect.y += this.textPadding();
  339.         rect.width -= this.textPadding() * 2;
  340.         return rect;
  341.     }

  342.    
  343.     isOpenAndActive() {
  344.         return this.isOpen() && this.active;
  345.     }

  346.     update() {
  347.         super.update();
  348.         this.processHandling();
  349.     }

  350.     processHandling() {
  351.         if (this.isOpenAndActive()) {
  352.             if (this.isOkEnabled() && this.isOkTriggered()) {
  353.                 this.processOk();
  354.             } else if (this.isCancelEnabled() && this.isCancelTriggered()) {
  355.                 this.processCancel();
  356.             }
  357.         }
  358.     }

  359.     updateTouching() {
  360.         if (this.active) {
  361.             if (TouchInput.isPressed() && this.isMouseOver()) {
  362.                 if (!this._bTouching) {
  363.                     this._lastIndex = this.index;
  364.                     this._fScrollOriginX = TouchInput.x;
  365.                     this._fScrollOriginY = TouchInput.y;
  366.                 }
  367.                 if (this._fScrollingY > 1 || this._fScrollingX > 1) {
  368.                     if (!this.isSelectionMaintained()) this.select(-1);
  369.                 } else {
  370.                     let x = this.canvasToLocalX(TouchInput.x);
  371.                     let y = this.canvasToLocalY(TouchInput.y);
  372.                     let index = this.hitTest(x,y);
  373.                     if (index > -1) {
  374.                         this.select(index);
  375.                     }
  376.                 }
  377.                 this._bTouching = true;
  378.                 this._bTouched = true;
  379.             } else {
  380.                 this._bTouching = false;
  381.                 if (this._inertia > 1 || this._inertia < -1) {
  382.                     this._fScrollY += this._inertia;
  383.                     this._inertia = this._inertia * Window_Scroll.inertiaAttenuation;
  384.                 }
  385.             }
  386.         } else {
  387.             this._bTouching = false;
  388.         }
  389.     }

  390.     processRelease() {
  391.         if (this.active) {
  392.             if (!this._bTouching && this._bTouched) {
  393.                 this._fScrollX += this._fScrollingX;
  394.                 this._fScrollY += this._fScrollingY;
  395.                 if (this._fScrollingY === 0 && this._fScrollingX === 0) {
  396.                     this.processClick();
  397.                 } else {
  398.                     this.select(-1);
  399.                 }
  400.                 this._fScrollingX = 0;
  401.                 this._fScrollingY = 0;
  402.                 this._bTouched = false;

  403.             }
  404.         } else {
  405.             this._bTouching = false;
  406.         }
  407.     }

  408.     processClick() {
  409.         let lastIndex = this.index;
  410.         let x = this.canvasToLocalX(TouchInput.x);
  411.         let y = this.canvasToLocalY(TouchInput.y);
  412.         let hitIndex = this.hitTest(x, y);
  413.         if (hitIndex >= 0) {
  414.             this.select(hitIndex);
  415.             if (hitIndex === this.index) {
  416.                 if (this.isOkEnabled()) {
  417.                     this.processOk();
  418.                 }
  419.             } else {
  420.                 this.select(hitIndex);
  421.             }
  422.         }
  423.     }

  424.     /**
  425.      *
  426.      * @param {number} x
  427.      * @param {number} y
  428.      */
  429.     hitTest(x, y) {
  430.         if (this.isContentsArea(x, y)) {
  431.             let cx = x - this.scrollX - this.standardPadding();
  432.             let cy = y - this.scrollY - this.standardPaddingY();
  433.             for(let i = 0; i < this.maxItems; i++) {
  434.                 let rect = this.itemRect(i);
  435.                 let right = rect.x + rect.width;
  436.                 let bottom = rect.y + rect.height;
  437.                 if(cx >= rect.x && cy >= rect.y && cx < right && cy < bottom) {
  438.                     return i;
  439.                 }
  440.             }
  441.         }
  442.         return -1;
  443.     }

  444.     /**
  445.      *
  446.      * @param {number} x
  447.      * @param {number} y
  448.      */
  449.     isContentsArea(x, y) {
  450.         let left = this.standardPadding();
  451.         let top = this.standardPaddingY();
  452.         let right = this.width - this.standardPadding();
  453.         let bottom = this.height - this.standardPaddingY();
  454.         return (x >= left && y >= top && x < right && y < bottom);
  455.     }

  456.     isOkEnabled() {
  457.         return this.isHandled('ok');
  458.     }

  459.     isCancelEnabled() {
  460.         return this.isHandled('cancel');
  461.     }
  462.    
  463.     isOkTriggered() {
  464.         return Input.isRepeated('ok');
  465.     }

  466.     isCancelTriggered() {
  467.         return Input.isRepeated('cancel');
  468.     }

  469.     processOk() {
  470.         if (this.isCurrentItemEnabled()) {
  471.             this.playOkSound();
  472.             this.updateInputData();
  473.             this.callOkHandler();
  474.         } else {
  475.             this.playBuzzerSound();
  476.         }
  477.     }

  478.     playBuzzerSound() {
  479.         SoundManager.playBuzzer();
  480.     }

  481.     playOkSound() {
  482.         SoundManager.playOk();
  483.     }

  484.     callOkHandler() {
  485.         this.callHandler('ok');
  486.     }

  487.     processCancel() {
  488.         if(this.isHandled('cancel')) {
  489.             SoundManager.playCancel();
  490.             this.updateInputData();
  491.             this.callCancelHandler();
  492.         }
  493.     }

  494.     callCancelHandler() {
  495.         this.callHandler('cancel');
  496.     }

  497.     updateInputData() {
  498.         Input.update();
  499.         TouchInput.update();
  500.     }

  501.     isCurrentItemEnabled() {
  502.         return true;
  503.     }

  504.     drawAllItems() {
  505.         for (let i = 0; i < this.maxItems; i++) {
  506.             this.drawItem(i);
  507.         }
  508.     }

  509.     /**
  510.      *
  511.      * @param {number} index
  512.      */
  513.     clearItem(index) {
  514.         let rect = this.itemRect(index);
  515.         this.contents.clearRect(rect.x, rect.y, rect.width, rect.height);
  516.     }

  517.     /**
  518.      *
  519.      * @param {number} index
  520.      */
  521.     drawItem(index) {
  522.     }

  523.     /**
  524.      *
  525.      * @param {number} index
  526.      */
  527.     redrawItem(index) {
  528.         if (index >= 0) {
  529.             this.clearItem(index);
  530.             this.drawItem(index);
  531.         }
  532.     }

  533.     redrawCurrentItem() {
  534.         this.redrawItem(this.index);
  535.     }

  536.     updateScrollLimit() {

  537.     }

  538.     refresh() {
  539.         this.updateScrollLimit();
  540.         this.drawAllItems();
  541.     }
  542. }
复制代码

评分

参与人数 6赞 +6 收起 理由
moonyoulove + 1 精品文章
hanyilun + 1 塞糖
白嫩白嫩的 + 1 精品文章
if216 + 1 塞糖
wabcmcc + 1 塞糖
沉滞的剑 + 1 我很赞同

查看全部评分

梦石
0
星屑
7738
在线时间
1227 小时
注册时间
2008-12-14
回帖
504
发表于 2020-6-13 23:14:19 | 显示全部楼层
很丝滑很丝滑很丝滑很丝滑
需要购买本人MV插件必须先加wx好友。加不上wx就是本人忙,没时间卖。原则上太久以前的插件也不想卖,因为我也忘了,维护上会不给力。wx名:alskyif    本人插件地址:
   LCK_SRPG梦幻模拟战、火焰纹章类系统
   究极立绘ADV系统

   究极换装统合系统
   究极! 回想与CG系统
   消息文字的距离调整  
   自动返回上一张地图
回复 0 1

使用道具 举报

梦石
0
星屑
3301
在线时间
351 小时
注册时间
2018-3-28
回帖
189
发表于 2021-1-18 12:46:34 | 显示全部楼层
能问下 这个是怎么使用的么
我用这个创建了新的窗口 但是显示不出什么东西
用mv做了两款游戏,有兴趣的可以瞧一瞧:
《有毛病》--- 休闲、探索、策略、剧情、恐怖 都有那么一些,就是没有战斗。挺骚挺有意思的一个游戏。
steam链接:点击这里


《留离》--- 探索、高自由度、ARPG、战斗,就是没有啥剧情。
steam链接:点击这里
回复

使用道具 举报

梦石
0
星屑
228
在线时间
86 小时
注册时间
2021-10-23
回帖
34
发表于 2021-12-30 14:01:55 | 显示全部楼层
有没有使用教程。不会用呀
回复

使用道具 举报

梦石
0
星屑
228
在线时间
86 小时
注册时间
2021-10-23
回帖
34
发表于 2021-12-30 16:53:05 | 显示全部楼层
本帖最后由 babystudio 于 2021-12-30 16:55 编辑

你的代码里有好多的函数调用没有加(),还有item.bitmap.drawItemBox(0, 0, this.scrollBarWidth, l, 'img/system/', 'window_scroller', 0, 2)是无法在mv中调用的,没有drawItemBox方法
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部