Skip to content
This repository has been archived by the owner on Apr 8, 2023. It is now read-only.

Commit

Permalink
CustomClientResizing: New Plugin (#2091)
Browse files Browse the repository at this point in the history
CustomClientResizing: New Plugin
  • Loading branch information
Owain94 authored Dec 5, 2019
2 parents eba140c + 4f8fa15 commit dacf826
Show file tree
Hide file tree
Showing 14 changed files with 724 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.runelite.client.plugins.customclientresizing;

import net.runelite.client.config.Config;
import net.runelite.client.config.ConfigGroup;

@ConfigGroup(CustomClientResizingPlugin.CONFIG_GROUP)
public interface CustomClientResizingConfig extends Config
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package net.runelite.client.plugins.customclientresizing;

import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Provides;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import javax.inject.Inject;
import lombok.Getter;
import net.runelite.api.Client;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.ConfigChanged;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.PluginType;
import net.runelite.client.plugins.customclientresizing.ui.CustomClientResizingPluginPanel;
import net.runelite.client.ui.ClientToolbar;
import net.runelite.client.ui.ClientUI;
import net.runelite.client.ui.ContainableFrame;
import net.runelite.client.ui.NavigationButton;
import net.runelite.client.util.ImageUtil;

@PluginDescriptor(
name = "Custom Client Resizing",
description = "Resize the window to saved profiles",
tags = {"resize", "window", "position", "layout", "manage"},
type = PluginType.UTILITY,
enabledByDefault = false
)
public class CustomClientResizingPlugin extends Plugin
{
public static final String CONFIG_GROUP = "customclientresizing";
private static final String CONFIG_KEY = "customclientresizingprofiles";
private static final String PLUGIN_NAME = "Custom Client Resizing";
private static final String ICON_FILE = "panel_icon.png";

@Getter
private final List<CustomClientResizingProfile> customclientresizingProfiles = new ArrayList<>();

@Inject
private Client client;

@Inject
private CustomClientResizingConfig config;

@Inject
private ClientToolbar clientToolbar;

@Inject
private ConfigManager configManager;

@Inject
private ClientUI clientUi;

private CustomClientResizingPluginPanel pluginPanel;
private NavigationButton navigationButton;
private NavigationButton titleBarButton;

@Provides
CustomClientResizingConfig provideConfig(ConfigManager configManager)
{
return configManager.getConfig(CustomClientResizingConfig.class);
}

@Override
protected void startUp()
{
loadConfig(configManager.getConfiguration(CONFIG_GROUP, CONFIG_KEY)).forEach(customclientresizingProfiles::add);

pluginPanel = injector.getInstance(CustomClientResizingPluginPanel.class);
pluginPanel.rebuild();

final BufferedImage icon = ImageUtil.getResourceStreamFromClass(getClass(), ICON_FILE);

navigationButton = NavigationButton.builder()
.tooltip(PLUGIN_NAME)
.icon(icon)
.priority(1)
.panel(pluginPanel)
.build();

titleBarButton = NavigationButton.builder()
.tab(false)
.tooltip("Set resize profile")
.icon(icon)
.onClick(() ->
{
ContainableFrame frame = clientUi.getFrame();


CustomClientResizingProfile active = customclientresizingProfiles.stream().filter(CustomClientResizingProfile::isVisible).findFirst().orElse(null);
if (active == null)
{
return;
}
Rectangle bounds = new Rectangle(
active.getPosition().width, active.getPosition().height,
active.getSize().width, active.getSize().height
);
if (!clientUi.isSidebarOpen())
{
bounds.width -= clientUi.getPluginToolbar().getWidth();
}
if (clientUi.getPluginPanel() == null)
{
bounds.width -= pluginPanel.getWrappedPanel().getPreferredSize().width;
}
frame.setBounds(bounds);
frame.revalidateMinimumSize();
})
.build();

clientToolbar.addNavigation(titleBarButton);
clientToolbar.addNavigation(navigationButton);
}

@Override
protected void shutDown()
{
customclientresizingProfiles.clear();
clientToolbar.removeNavigation(navigationButton);
clientToolbar.removeNavigation(titleBarButton);

pluginPanel = null;
navigationButton = null;
}

@Subscribe
public void onConfigChanged(ConfigChanged event)
{
if (customclientresizingProfiles.isEmpty() && event.getGroup().equals(CONFIG_GROUP) && event.getKey().equals(CONFIG_KEY))
{
loadConfig(event.getNewValue()).forEach(customclientresizingProfiles::add);
}


}

public void updateConfig()
{
if (customclientresizingProfiles.isEmpty())
{
configManager.unsetConfiguration(CONFIG_GROUP, CONFIG_KEY);
return;
}

final Gson gson = new Gson();
final String json = gson
.toJson(customclientresizingProfiles);
configManager.setConfiguration(CONFIG_GROUP, CONFIG_KEY, json);
}

private Stream<CustomClientResizingProfile> loadConfig(String json)
{
if (Strings.isNullOrEmpty(json))
{
return Stream.empty();
}

final Gson gson = new Gson();
final List<CustomClientResizingProfile> customclientresizingProfileData = gson.fromJson(json, new TypeToken<ArrayList<CustomClientResizingProfile>>()
{
}.getType());

return customclientresizingProfileData.stream();
}

public void addProfile()
{
ContainableFrame frame = clientUi.getFrame();
CustomClientResizingProfile profile = new CustomClientResizingProfile(
Instant.now().toEpochMilli(),
"Profile " + (customclientresizingProfiles.size() + 1),
new Dimension(frame.getX(), frame.getY()),
new Dimension(frame.getWidth(), frame.getHeight()),
false
);

customclientresizingProfiles.add(profile);
pluginPanel.updateProfiles();
updateConfig();
}

public void deleteProfile(final CustomClientResizingProfile profile)
{
customclientresizingProfiles.remove(profile);
pluginPanel.updateProfiles();
updateConfig();
}

public void disableProfiles()
{
customclientresizingProfiles.forEach(e -> e.setVisible(false));
pluginPanel.updateProfiles();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.runelite.client.plugins.customclientresizing;

import java.awt.Dimension;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CustomClientResizingProfile
{
private long id;
private String name;
private Dimension position;
private Dimension size;
private boolean visible;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package net.runelite.client.plugins.customclientresizing.ui;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import lombok.Getter;
import net.runelite.client.plugins.customclientresizing.CustomClientResizingPlugin;
import net.runelite.client.plugins.customclientresizing.CustomClientResizingProfile;
import net.runelite.client.ui.ColorScheme;
import net.runelite.client.ui.PluginPanel;
import net.runelite.client.util.ImageUtil;

@Singleton
public class CustomClientResizingPluginPanel extends PluginPanel
{
private static final ImageIcon ADD_ICON;
private static final ImageIcon ADD_HOVER_ICON;

private static final Color DEFAULT_BORDER_COLOR = Color.GREEN;
private static final Color DEFAULT_FILL_COLOR = new Color(0, 255, 0, 0);

private static final int DEFAULT_BORDER_THICKNESS = 3;

static
{
final BufferedImage addIcon = ImageUtil.getResourceStreamFromClass(CustomClientResizingPlugin.class, "add_icon.png");
ADD_ICON = new ImageIcon(addIcon);
ADD_HOVER_ICON = new ImageIcon(ImageUtil.alphaOffset(addIcon, 0.53f));
}

private final JLabel addMarker = new JLabel(ADD_ICON);
private final JLabel title = new JLabel();
private final JPanel markerView = new JPanel(new GridBagLayout());
@Inject
private CustomClientResizingPlugin plugin;
@Getter
private Color selectedColor = DEFAULT_BORDER_COLOR;
@Getter
private Color selectedFillColor = DEFAULT_FILL_COLOR;
@Getter
private int selectedBorderThickness = DEFAULT_BORDER_THICKNESS;

public void init()
{
setLayout(new BorderLayout());
setBorder(new EmptyBorder(10, 10, 10, 10));

JPanel northPanel = new JPanel(new BorderLayout());
northPanel.setBorder(new EmptyBorder(1, 0, 10, 0));

title.setText("Resize Profiles");
title.setForeground(Color.WHITE);

northPanel.add(title, BorderLayout.WEST);
northPanel.add(addMarker, BorderLayout.EAST);

JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.setBackground(ColorScheme.DARK_GRAY_COLOR);

markerView.setBackground(ColorScheme.DARK_GRAY_COLOR);

updateProfiles();

addMarker.setToolTipText("Add new screen marker");
addMarker.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent mouseEvent)
{
plugin.addProfile();
}

@Override
public void mouseEntered(MouseEvent mouseEvent)
{
addMarker.setIcon(ADD_HOVER_ICON);
}

@Override
public void mouseExited(MouseEvent mouseEvent)
{
addMarker.setIcon(ADD_ICON);
}
});

centerPanel.add(markerView, BorderLayout.CENTER);

add(northPanel, BorderLayout.NORTH);
add(centerPanel, BorderLayout.CENTER);
}

public void rebuild()
{
removeAll();
repaint();
revalidate();
init();
}

public void updateProfiles()
{
markerView.removeAll();

GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weightx = 1;
constraints.gridx = 0;
constraints.gridy = 0;

for (final CustomClientResizingProfile marker : plugin.getCustomclientresizingProfiles())
{
markerView.add(new CustomClientResizingProfilePanel(plugin, marker), constraints);
constraints.gridy++;

markerView.add(Box.createRigidArea(new Dimension(0, 10)), constraints);
constraints.gridy++;
}

repaint();
revalidate();
}

}

Loading

0 comments on commit dacf826

Please sign in to comment.