X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTHScroller.java;h=a07bcd7a52b7636949e57948892d395cba41bcf9;hb=HEAD;hp=9e9b372ec51854d8bf75e968746d65e58ef1c8c8;hpb=56661844475522242093c8858ceb20fb15589da5;p=fanfix.git diff --git a/src/jexer/THScroller.java b/src/jexer/THScroller.java index 9e9b372..a07bcd7 100644 --- a/src/jexer/THScroller.java +++ b/src/jexer/THScroller.java @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (C) 2017 Kevin Lamonte + * Copyright (C) 2019 Kevin Lamonte * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -35,13 +35,204 @@ import jexer.event.TMouseEvent; /** * THScroller implements a simple horizontal scroll bar. */ -public final class THScroller extends TWidget { +public class THScroller extends TWidget { + + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ /** * Value that corresponds to being on the left edge of the scroll bar. */ private int leftValue = 0; + /** + * Value that corresponds to being on the right edge of the scroll bar. + */ + private int rightValue = 100; + + /** + * Current value of the scroll. + */ + private int value = 0; + + /** + * The increment for clicking on an arrow. + */ + private int smallChange = 1; + + /** + * The increment for clicking in the bar between the box and an arrow. + */ + private int bigChange = 20; + + /** + * When true, the user is dragging the scroll box. + */ + private boolean inScroll = false; + + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Public constructor. + * + * @param parent parent widget + * @param x column relative to parent + * @param y row relative to parent + * @param width height of scroll bar + */ + public THScroller(final TWidget parent, final int x, final int y, + final int width) { + + // Set parent and window + super(parent, x, y, width, 1); + } + + // ------------------------------------------------------------------------ + // Event handlers --------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Handle mouse button releases. + * + * @param mouse mouse button release event + */ + @Override + public void onMouseUp(final TMouseEvent mouse) { + + if (inScroll) { + inScroll = false; + return; + } + + if (rightValue == leftValue) { + return; + } + + if ((mouse.getX() == 0) + && (mouse.getY() == 0) + ) { + // Clicked on the left arrow + decrement(); + return; + } + + if ((mouse.getY() == 0) + && (mouse.getX() == getWidth() - 1) + ) { + // Clicked on the right arrow + increment(); + return; + } + + if ((mouse.getY() == 0) + && (mouse.getX() > 0) + && (mouse.getX() < boxPosition()) + ) { + // Clicked between the left arrow and the box + value -= bigChange; + if (value < leftValue) { + value = leftValue; + } + return; + } + + if ((mouse.getY() == 0) + && (mouse.getX() > boxPosition()) + && (mouse.getX() < getWidth() - 1) + ) { + // Clicked between the box and the right arrow + value += bigChange; + if (value > rightValue) { + value = rightValue; + } + return; + } + } + + /** + * Handle mouse movement events. + * + * @param mouse mouse motion event + */ + @Override + public void onMouseMotion(final TMouseEvent mouse) { + + if (rightValue == leftValue) { + inScroll = false; + return; + } + + if ((mouse.isMouse1()) + && (inScroll) + && (mouse.getX() > 0) + && (mouse.getX() < getWidth() - 1) + ) { + // Recompute value based on new box position + value = (rightValue - leftValue) + * (mouse.getX()) / (getWidth() - 3) + leftValue; + if (value > rightValue) { + value = rightValue; + } + if (value < leftValue) { + value = leftValue; + } + return; + } + inScroll = false; + } + + /** + * Handle mouse button press events. + * + * @param mouse mouse button press event + */ + @Override + public void onMouseDown(final TMouseEvent mouse) { + if (rightValue == leftValue) { + inScroll = false; + return; + } + + if ((mouse.getY() == 0) + && (mouse.getX() == boxPosition()) + ) { + inScroll = true; + return; + } + + } + + // ------------------------------------------------------------------------ + // TWidget ---------------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Draw a horizontal scroll bar. + */ + @Override + public void draw() { + CellAttributes arrowColor = getTheme().getColor("tscroller.arrows"); + CellAttributes barColor = getTheme().getColor("tscroller.bar"); + putCharXY(0, 0, GraphicsChars.CP437[0x11], arrowColor); + putCharXY(getWidth() - 1, 0, GraphicsChars.CP437[0x10], arrowColor); + + // Place the box + if (rightValue > leftValue) { + hLineXY(1, 0, getWidth() - 2, GraphicsChars.CP437[0xB1], barColor); + putCharXY(boxPosition(), 0, GraphicsChars.BOX, arrowColor); + } else { + hLineXY(1, 0, getWidth() - 2, GraphicsChars.HATCH, barColor); + } + + } + + // ------------------------------------------------------------------------ + // THScroller ------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Get the value that corresponds to being on the left edge of the scroll * bar. @@ -62,11 +253,6 @@ public final class THScroller extends TWidget { this.leftValue = leftValue; } - /** - * Value that corresponds to being on the right edge of the scroll bar. - */ - private int rightValue = 100; - /** * Get the value that corresponds to being on the right edge of the * scroll bar. @@ -87,11 +273,6 @@ public final class THScroller extends TWidget { this.rightValue = rightValue; } - /** - * Current value of the scroll. - */ - private int value = 0; - /** * Get current value of the scroll. * @@ -110,11 +291,6 @@ public final class THScroller extends TWidget { this.value = value; } - /** - * The increment for clicking on an arrow. - */ - private int smallChange = 1; - /** * Get the increment for clicking on an arrow. * @@ -133,11 +309,6 @@ public final class THScroller extends TWidget { this.smallChange = smallChange; } - /** - * The increment for clicking in the bar between the box and an arrow. - */ - private int bigChange = 20; - /** * Set the increment for clicking in the bar between the box and an * arrow. @@ -158,26 +329,6 @@ public final class THScroller extends TWidget { this.bigChange = bigChange; } - /** - * When true, the user is dragging the scroll box. - */ - private boolean inScroll = false; - - /** - * Public constructor. - * - * @param parent parent widget - * @param x column relative to parent - * @param y row relative to parent - * @param width height of scroll bar - */ - public THScroller(final TWidget parent, final int x, final int y, - final int width) { - - // Set parent and window - super(parent, x, y, width, 1); - } - /** * Compute the position of the scroll box (a.k.a. grip, thumb). * @@ -187,30 +338,6 @@ public final class THScroller extends TWidget { return (getWidth() - 3) * (value - leftValue) / (rightValue - leftValue) + 1; } - /** - * Draw a horizontal scroll bar. - */ - @Override - public void draw() { - CellAttributes arrowColor = getTheme().getColor("tscroller.arrows"); - CellAttributes barColor = getTheme().getColor("tscroller.bar"); - getScreen().putCharXY(0, 0, GraphicsChars.CP437[0x11], arrowColor); - getScreen().putCharXY(getWidth() - 1, 0, GraphicsChars.CP437[0x10], - arrowColor); - - // Place the box - if (rightValue > leftValue) { - getScreen().hLineXY(1, 0, getWidth() - 2, GraphicsChars.CP437[0xB1], - barColor); - getScreen().putCharXY(boxPosition(), 0, GraphicsChars.BOX, - arrowColor); - } else { - getScreen().hLineXY(1, 0, getWidth() - 2, GraphicsChars.HATCH, - barColor); - } - - } - /** * Perform a small step change left. */ @@ -277,109 +404,4 @@ public final class THScroller extends TWidget { value = rightValue; } - /** - * Handle mouse button releases. - * - * @param mouse mouse button release event - */ - @Override - public void onMouseUp(final TMouseEvent mouse) { - - if (inScroll) { - inScroll = false; - return; - } - - if (rightValue == leftValue) { - return; - } - - if ((mouse.getX() == 0) - && (mouse.getY() == 0) - ) { - // Clicked on the left arrow - decrement(); - return; - } - - if ((mouse.getY() == 0) - && (mouse.getX() == getWidth() - 1) - ) { - // Clicked on the right arrow - increment(); - return; - } - - if ((mouse.getY() == 0) - && (mouse.getX() > 0) - && (mouse.getX() < boxPosition()) - ) { - // Clicked between the left arrow and the box - value -= bigChange; - if (value < leftValue) { - value = leftValue; - } - return; - } - - if ((mouse.getY() == 0) - && (mouse.getX() > boxPosition()) - && (mouse.getX() < getWidth() - 1) - ) { - // Clicked between the box and the right arrow - value += bigChange; - if (value > rightValue) { - value = rightValue; - } - return; - } - } - - /** - * Handle mouse movement events. - * - * @param mouse mouse motion event - */ - @Override - public void onMouseMotion(final TMouseEvent mouse) { - - if (rightValue == leftValue) { - inScroll = false; - return; - } - - if ((mouse.isMouse1()) - && (inScroll) - && (mouse.getX() > 0) - && (mouse.getX() < getWidth() - 1) - ) { - // Recompute value based on new box position - value = (rightValue - leftValue) - * (mouse.getX()) / (getWidth() - 3) + leftValue; - return; - } - inScroll = false; - } - - /** - * Handle mouse button press events. - * - * @param mouse mouse button press event - */ - @Override - public void onMouseDown(final TMouseEvent mouse) { - if (rightValue == leftValue) { - inScroll = false; - return; - } - - if ((mouse.getY() == 0) - && (mouse.getX() == boxPosition()) - ) { - inScroll = true; - return; - } - - } - }