📄 gauge.java
字号:
for (int i = startBlock; i < stopBlock; i++) { int blockHeight; if (interactive && blockCount > 1) { // // this implements a quadratic spline from // p0 to p2 using p1 as a control point to // affect the curve // // int p0 = 5, // minimum height // p1 = 1, // control point // p2 = GAUGE_HEIGHT - p0; // maximum height // // the original equation is below, but the one used // has been expanded to use the p values above // // blockHeight = p0 + // (2 * p1 * i * (bc - i) / bc2) + // ((p2 * (i * i)) / bc2); // blockHeight = 5 + (((i * (bc - i)) << 1) / bc2) + (((GAUGE_HEIGHT - 5) * (i * i)) / bc2); } else { blockHeight = GAUGE_HEIGHT; } if ((valueOfEachBlock * (i + 1)) > value && value != maxValue) { if (hasFocus()) { g.drawRect((i * BLOCK_SPACE) + 6, GAUGE_HEIGHT - blockHeight, BLOCK_SPACE - 3, blockHeight); } else { g.setStrokeStyle(Graphics.DOTTED); g.drawRect((i * BLOCK_SPACE) + 6, GAUGE_HEIGHT - blockHeight, BLOCK_SPACE - 3, blockHeight); g.setStrokeStyle(Graphics.SOLID); } } else { g.fillRect((i * BLOCK_SPACE) + 6, GAUGE_HEIGHT - blockHeight, BLOCK_SPACE - 3, blockHeight); } } g.translate(0, -layoutHeight); setHorizontalScroll(); } /** * Signal this Gauge that its focus has changed * (either gained or lost focus) */ void focusChanged() { if (!hasFocus()) { // We have lost the focus, so we need to re-set // the right/left arrows, as the next component // to receive focus may not use the left/right // arrows at all, and Screen no longer re-sets // the horizontal arrows for us getOwner().setHorizontalScroll(0, 100); } super.focusChanged(); } /** * Set the width for this Guage * * @param width The width to set this Gauge to * @return int The height required to display this Gauge in the * given width */ int setWidth(int width) { height = GAUGE_HEIGHT + 5; height += layouts[0].setWidth(width); return height; } /** * Get the height of this Gauge * * @return int The height of this Gauge */ int getHeight() { return height; } /** * Handle a key press * * @param keyCode The key that was pressed * @return boolean A flag indicating the key pressed was handled * by this Gauge */ boolean keyPressed(int keyCode) { if (!interactive) { return false; } if ((Display.getGameAction(keyCode) == Canvas.LEFT) || (Display.getGameAction(keyCode) == Canvas.RIGHT)) { // There are two cases where we need to repaint a // block when a user changes a Gauge's value: // 1. If the user is increasing the Gauge's value // and the value increases such that a new bar // needs to be filled in. The easy test for this // is: value % valueOfEachBlock == 0, indicating // the value has increased precisely enough to // fill in an entire block. // // 2. If the user is decreasing the Gauge's value // and the value decreases such that a currently // filled in bar needs to be un-filled. The easy // test for this is: // (value + 1) % valueOfEachBlock == 0, indicating // we have just decreased 1 unit below a full // block. int blockToRepaint = -1; int oldValue = value; if (Display.getGameAction(keyCode) == Canvas.LEFT) { if (value % valueOfEachBlock == 0) { blockToRepaint = (value / valueOfEachBlock) - 1; } else if (value == maxValue) { blockToRepaint = blockCount - 1; } value--; } else { value++; if (value % valueOfEachBlock == 0) { blockToRepaint = (value / valueOfEachBlock) - 1; } else if (value == maxValue) { blockToRepaint = blockCount - 1; } } checkValue(); // There's no need to cause a repaint if none of the // blocks either need to be filled in or need to be // un-filled. if (blockToRepaint < 0 || blockToRepaint >= blockCount) { setHorizontalScroll(); return value != oldValue; } // We need to remember to offset by the height of our // label if there is one. int blockY = 0; blockY = layouts[0].getHeight(); // Cause a repaint of only the bar which needs updating repaint(blockToRepaint * BLOCK_SPACE + 6, blockY, BLOCK_SPACE - 2, GAUGE_HEIGHT + 2); return value != oldValue; } return false; } // private implementation /** * Set the number of blocks rendered by this Gauge. This * will use the width of the display and calculate the * blockCount value. */ private void setBlockCount() { this.blockCount = (Display.WIDTH - 8) / BLOCK_SPACE; if (blockCount > maxValue) { blockCount = maxValue; } } /** * Set the value of each block. This will use the * blockCount value and maxValue to assign a value * to each block. */ private void setBlockValue() { valueOfEachBlock = 1; if (maxValue <= blockCount) { return; } valueOfEachBlock = maxValue / blockCount; if (maxValue % blockCount != 0) { valueOfEachBlock++; } // There's a case when our max is close to // a multiple of our value per block (ie 50 // as a max value, 4 or 5 as a value per block). // In this case, we decrease the number of blocks // and make each block an even value // (5, using the above example). blockCount = maxValue / valueOfEachBlock; if (maxValue % valueOfEachBlock != 0) { blockCount++; } // If the max was a value like 52, then there // would be the full number of blocks (11 with // the RI screen width) and the last block // would have 2 units of value. The last block // in the gauge always as as many or less units // than the other blocks in the gauge. } /** * Set the horizontal "scroll" for this Gauge. Note that * "scroll" is a bit of a misnomer, but we are turning on * the left/right indicator arrows on the display to signal * the user the Gauge can be incremented/decremented. */ private void setHorizontalScroll() { // If we have focus and are interactive, we check // to see if we need to add the right or left arrow if (hasFocus() && interactive) { // NOTE: I couldn't think offhand how to do an // accurate proportion value, so I just use 50 int scrollPosition = value * 100 / maxValue; // Fix the rounding error if (value > 0 && scrollPosition == 0) { scrollPosition = 1; } getOwner().setHorizontalScroll(scrollPosition, (50)); } } /** * Utility method to ensure the value of the Gauge is always * in a range of 0 to maxValue * * @return int The corrected version of the current value */ private int checkValue() { if (value < 0) { value = 0; } else if (value > maxValue) { value = maxValue; } return value; } /** * Set the max value of this Gauge * * @param maxValue The maximum value to set for this Gauge */ private void setMaxValueImpl(int maxValue) { if (maxValue <= 0) { throw new IllegalArgumentException(); } this.maxValue = maxValue; checkValue(); // If the max value changes, the block count needs // to be re-calculated setBlockCount(); setBlockValue(); contentChanged(0, height - (GAUGE_HEIGHT + 5), 0); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -