Jump to content
Joseph

[Snippet] Smart Painter

Recommended Posts

Hey everybody! Here another snippet from your truly Josedpay. Some time i get lazy having to create paint. Many of you guys probably use that little programs (forget name). i use to use it back in the days, but franky its still  too much work. So i created this paint.

 

Have fun everybody(for those who are wondering about that "null" sign. I cant spoon feed everybody, im making it even smarter tongue.png).

 

FYI some of the oldest people from OSB contributed on this painter. I use some of their idea/work but revamped it. Hopefully someone in the future could do the same happy.png

 

edit: just a head up it comment all over the class

 

LocalPaint.java //the interface is a nested static

package com.divine.painter;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.Set;

import org.osbot.rs07.Bot;
import org.osbot.rs07.canvas.paint.Painter;
import org.osbot.rs07.script.Script;

public class LocalPaint extends MouseAdapter implements Painter {

	private final static String[] SUFFIX = new String[] { "", "K", "M", "B", "T" };	
	
	private final Font title = new Font("Tahoma", Font.PLAIN, 17);
	private final Font stats = new Font("Tahoma", Font.PLAIN, 14);
	private Color backgroundColor = new Color(0,0,0,.5f); //transparent black
	private Color extraColor = new Color(0,.8f,0,0.7f); //transparent green
	private Color textColor = Color.WHITE; // a null color 
	
	/**
	 * The node that does all you dirty work 
	 */
	private final LocalPaintHandler localHandler;
	private Shape shape;
	private Script ctx;
	
	public LocalPaint(Shape shape, LocalPaintHandler handler)	{
		this.shape = shape;
		this.localHandler = handler;
	}	
	
	@Override
	public void onPaint(Graphics2D g) {
		drawExtraShapes(g);
		drawBackground(g);
		drawText(g);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		//passes down both the shape and mouse event
		//you get to handler the interaction
		if (localHandler != null)	{
			Shape shape = null;
			localHandler.handleExtras(e, shape);
		}
	}

	/** 
	 * @param true to add and false to remove
	 */
	public void enableMouser(boolean add)	{
		Bot bot = ctx.getBot();
		if (!add && bot.getMouseListeners().contains(this))	{
			bot.removeMouseListener(this);
		}
		if (add && !bot.getMouseListeners().contains(this))	{
			bot.addMouseListener(this);
		}
	}
	
	/**
	 * @param true to add and false to remove
	 */
	public void enablePainter(boolean add)	{
		Bot bot = ctx.getBot();
		if (!add && bot.getPainters().contains(this))	{
			bot.removePainter(this);
		}
		if (add && !bot.getPainters().contains(this))	{
			bot.addPainter(this);
		}
	}
	
	/*
	 * Paint stuff
	 */
	private void drawText(Graphics2D g)	{
		FontMetrics fm = g.getFontMetrics(title);
		
		//find header
		boolean addFancy = this.ctx != null;		
		int wName = fm.stringWidth(ctx.getName()) + fm.stringWidth(Double.toString(ctx.getVersion()));
		
		//find rec
		Rectangle rec = shape.getBounds();
		int x = rec.x;
		int y = rec.y;
		int w = rec.width;
		
		//set header
		g.setColor(textColor);
		g.setFont(title);
		if (addFancy)	{
			g.drawString(ctx.getName() +" v" +Double.toString(ctx.getVersion()), x + (w / 2) + 5 - ((wName / 2)), y + 25);
			g.drawLine(x + 15, y + 30, x + (w - 15), y + 30);			
		}
		
		//set data
		g.setFont(stats);
		if (localHandler != null && localHandler.getPaintData() != null) {
			for (int i = 0; i < localHandler.getPaintData().length; i++) {
				PaintProperty p = localHandler.getPaintData()[i];
				g.drawString(p.getValue(), x + 15 + p.getOffset(), y + 45 + (i * 20));
			}		
		}
	}
	
	private void drawBackground(Graphics2D g) {
		g.setColor(backgroundColor);
		g.fill(shape);		
		//g.draw(shape);
		//You get to switch between fill or draw
				
		//unfinished
		boolean drawSkill = false; 
		if (drawSkill)	{ g.fill(shape); }	
			
		//draw Outline
		g.setColor(Color.WHITE);
		g.draw(shape);
	}
	
	/*
	 * Local instances Setter Methods
	 */
	public LocalPaint setScriptInstance(Script ctx)	{
		this.ctx = ctx;
		return this;
	}
	
	public LocalPaint setBackgroundColor(Color color)	{
		this.backgroundColor = color;
		return this;
	}
	
	public LocalPaint setTextColor(Color color)	{
		this.textColor = color;
		return this;
	}	
	
	public LocalPaint setExtraColor(Color color)	{
		this.extraColor = color;
		return this;
	}	
	
	/*
	 * Static interface (is needed)
	 */	
	public static interface LocalPaintHandler	{
		public void handleExtras(MouseEvent e, Shape shape);
		public PaintProperty[] getPaintData();
	}
	
	public static String getFormatTimer(long time) {
		final int sec = (int) (time / 1000), h = sec / 3600, m = sec / 60 % 60, s = sec % 60;
		return (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
	}	
	
	public static String rsFormat(long value) {
	    int size = (int) Math.log10(Math.max(1, value));
	    return Math.floor(value / Math.pow(10, size - size % 3) * 10) / 10d + SUFFIX[size / 3];
	}
	
	public static Color getTransparent(Color c, int amount0to255)	{
		return new Color(c.getRed(), c.getGreen(), c.getBlue(), amount0to255);
	}
}

 

PaintProperty.java


public class PaintProperty {
	private String value;
	private int offset;

	public PaintProperty(String value, int offset) {
		this.value = value;
		this.offset = offset;
	}

	public PaintProperty(String value) {
		this(value, 0);
	}

	public PaintProperty() {
		this("");
	}

	public PaintProperty value(String value) {
		this.value = value;
		return this;
	}

	public PaintProperty offset(int offset) {
		this.offset = offset;
		return this;
	}

	public String getValue() {
		return this.value;
	}

	public int getOffset() {
		return this.offset;
	}
}

 

 

exmaple:

import java.awt.Shape;
import java.awt.event.MouseEvent;

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import com.divine.painter.LocalPaint.LocalPaintHandler;

@ScriptManifest
public class TestScript extends Script implements LocalPaintHandler {
	
	Shape myShape = null; //set the location to see the magic
	LocalPaint localPaint;
	
	@Override
	public void handleExtras(MouseEvent e, Shape shape) {}

	@Override
	public PaintProperty[] getPaintData() { //LocalPaint.rsFormat(long)
		return new PaintProperty[] { //LocalPaint.getFormatTimer(long)
				new PaintProperty("do you even like it?"),
				new PaintProperty("just let me know", 15), //the offset move the text over + 15
		};
	}

	@Override
	public int onLoop() throws InterruptedException {
		if (localPaint == null) {
			this.localPaint = new LocalPaint(null, this).setScriptInstance(this);
			this.localPaint.enablePainter(true);
		}
		return 700;
	}

}
Edited by josedpay
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.

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...