Jump to content

Zoom Control


Camaro

Recommended Posts

Simple class to help change the zoom

Spoiler
import org.osbot.rs07.Bot;
import org.osbot.rs07.api.Settings;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.api.ui.Tab;
import org.osbot.rs07.event.Event;
import org.osbot.rs07.input.mouse.PointDestination;
import org.osbot.rs07.input.mouse.WidgetDestination;
import org.osbot.rs07.utility.Condition;
import org.osbot.rs07.utility.ConditionalSleep2;

public final class ZoomControl extends Event {

    /**
     * Determines if the camera Z scale is within a requested Zoom
     *
     * @param currentZoom the current Z scale of the camera
     * @param requestedZoom the requested Z scale of the camera
     * @return true if we are within the range, otherwise false
     */
    public static boolean isInRange(int currentZoom, int requestedZoom) {
        int currentPos = (int) getPosition(currentZoom);
        int requestedPos = (int) getPosition(requestedZoom);
        return currentPos > requestedPos - 5 &&
                currentPos < requestedPos + 5;
    }

    /**
     * Public method to execute event
     *
     * @param bot the bot instance
     * @param requestedZoom the zoom value to set
     * @return true if the event finished successfully
     */
    public static boolean setZoom(Bot bot, int requestedZoom) {
        Event event = new ZoomControl(requestedZoom);
        bot.getEventExecutor().execute(event);
        return event.hasFinished();
    }

    private final int zoom;

    private ZoomControl(int zoom) {
        this.zoom = zoom;
    }

    /**
     * Generates the would-be X position of the slider if the Z scale was the given value
     *
     * @param zoom the camera Z scale
     * @return the X position of the slider
     */
    private static double getPosition(int zoom) {
        double a = 415.7837;
        double power = 0.07114964;

        return a * Math.pow(zoom, power);
    }

    /**
     * Creates a new Condition, representing a Mouse Drag from the current position
     * of the slider to the requested position to achieve the correct zoom
     *
     * @param slider the slider widget object
     * @return the Condition to pass to a continualClick method
     */
    private Condition moveMouse(RS2Widget slider) {
        return new Condition() {

            @Override
            public boolean evaluate() {
                // Generate the X position for the slider of the requested zoom
                double widgetDestX = getPosition(zoom);
                // Generate the mouse's end X position for moving the slider
                int mouseDestX = (int) widgetDestX +
                        (int) (getMouse().getPosition().getX() - slider.getBounds().getX());
                // Generate the mouse's end Y position for moving the slider
                int mouseDestY = (int) slider.getPosition().getY() + (int) (slider.getHeight() / 2.00);
                return getMouse().move(new PointDestination(getBot(), mouseDestX, mouseDestY));
            }
        };
    }

    /**
     * Execution method
     *
     * @return the sleep time in between loops
     */
    @Override
    public int execute() {
        if (isInRange(getCamera().getScaleZ(), zoom)) {
            setFinished();
            return 0;
        } else if (!getTabs().isOpen(Tab.SETTINGS)) {
            if (getSettings().open())
                return 0;
        } else if (getSettings().getCurrentBasicSettingsTab() != Settings.BasicSettingsTab.DISPLAY) {
            if (getSettings().open(Settings.BasicSettingsTab.DISPLAY)) {
                return 0;
            }
        } else {
            RS2Widget slider = getWidgets().getWidgetContainingSprite(116, 1201);
            if (slider != null) {
                if (getMouse().continualClick(new WidgetDestination(getBot(), slider, 3), moveMouse(slider)) &&
                        ConditionalSleep2.sleep(1000, () -> isInRange(getCamera().getScaleZ(), zoom))) {
                    return 0;
                }
            }
        }
        return 300;
    }

}

Usage

Spoiler
if (!ZoomControl.isInRange(getCamera().getScaleZ(), 275)) {
    ZoomControl.setZoom(getBot(), 275);
}

Extreme usage

Spoiler
public class Main extends Script {

    private Deque<Integer> deck;

    private int flag = 0;

    {
        deck = IntStream.rangeClosed(181, 1448).boxed().collect(Collectors.toCollection(LinkedList::new));
    }

    @Override
    public int onLoop() throws InterruptedException {
        if (deck.isEmpty()) {
            stop();
        } else {
            ZoomControl.setZoom(getBot(), flag++%2==0 ? deck.removeFirst() : deck.removeLast());
        }
        return 50;
    }
}

 

Edited by Camaro
  • Like 6
  • Heart 2
Link to comment
Share on other sites

  • 4 months later...
  • 3 weeks later...
  • 1 month later...

Another way to do it is with scroll wheel up or down, provided the mouse is onscreen:

 

		if (zScale > 500) {
            ctx.log("Z scale > 500. Zooming out. 181 is max zoom out. 1448 is max zoom in). ");
            if (ctx.getMouse().isOnScreen()) {
              ctx.getMouse().scrollDown();
              sleep(rand(50,150));
              ctx.getMouse().scrollDown();
              sleep(rand(50,150));
              ctx.log("Former Z scale = " + zScale + ". New Z scale = " + thisCamera.getScaleZ());
			}
        } else if (zScale < 300) {
            ctx.log("Z scale < 300. Zooming in. 181 is max zoom out. 1448 is max zoom in). ");
            if (ctx.getMouse().isOnScreen()) {
              ctx.getMouse().scrollUp();
              sleep(rand(50,150));
              ctx.getMouse().scrollUp();
              sleep(rand(50,150));
              ctx.log("Former Z scale = " + zScale + ". New Z scale = " + thisCamera.getScaleZ());
			}
        } else {
            ctx.log("Z scale = " + zScale);
        }

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...