Skip to content

Commit

Permalink
Ability to create an easy trigger from a duty callout
Browse files Browse the repository at this point in the history
  • Loading branch information
xpdota committed Oct 9, 2023
1 parent 3b3d136 commit 6359c26
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package gg.xp.xivsupport.events.triggers.easytriggers;

import gg.xp.reevent.scan.ScanMe;
import gg.xp.xivsupport.callouts.ModifiableCallout;
import gg.xp.xivsupport.callouts.ModifiedCalloutHandle;
import gg.xp.xivsupport.callouts.RawModifiedCallout;
import gg.xp.xivsupport.callouts.gui.ExtraCalloutAction;
import gg.xp.xivsupport.callouts.gui.ExtraCalloutActionInstance;
import gg.xp.xivsupport.events.triggers.easytriggers.actions.CalloutAction;
import gg.xp.xivsupport.events.triggers.easytriggers.conditions.DutyCalloutFilter;
import gg.xp.xivsupport.events.triggers.easytriggers.gui.EasyTriggersTab;
import gg.xp.xivsupport.events.triggers.easytriggers.model.EasyTrigger;
import gg.xp.xivsupport.speech.ProcessedCalloutEvent;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;

@ScanMe
public class EasyTriggerDutyCalloutExtraAction implements ExtraCalloutAction {

public final EasyTriggers et;
private final EasyTriggersTab ett;

public EasyTriggerDutyCalloutExtraAction(EasyTriggers et, EasyTriggersTab ett) {
this.et = et;
this.ett = ett;
}

private void showError(String error) {
JOptionPane.showMessageDialog(JFrame.getWindows()[0], "Error: " + error, error, JOptionPane.ERROR_MESSAGE);

}

@Nullable
@Override
public ExtraCalloutActionInstance getInstance(ModifiableCallout<?> callout, ModifiedCalloutHandle handle) {
return new ExtraCalloutActionInstance() {
@Override
public String getLabel() {
return "Make Easy Trigger";
}

@Override
public boolean isVisible() {
return true;
}

@Override
public boolean isEnabled() {
return true;
}

@Override
public void doAction() {

Field field = handle.getField();
if (field == null) {
showError("Field was null!");
return;
}
String className = field.getDeclaringClass().getSimpleName();
String fieldName = field.getName();
DutyCalloutFilter dcf = new DutyCalloutFilter();
dcf.calloutClass = className;
dcf.calloutField = fieldName;

CalloutAction ca = new CalloutAction();
ca.setText("{originalText}");
ca.setTts("{originalTts}");

EasyTrigger<ProcessedCalloutEvent> easyTrigger = new EasyTrigger();
easyTrigger.setEventType(ProcessedCalloutEvent.class);
easyTrigger.addCondition(dcf);
easyTrigger.addAction(ca);
et.addTrigger(easyTrigger);
ett.bringToFront();
ett.selectTrigger(easyTrigger);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import gg.xp.reevent.events.InitEvent;
import gg.xp.reevent.scan.HandleEvents;
import gg.xp.reevent.scan.ScanMe;
import gg.xp.xivsupport.callouts.RawModifiedCallout;
import gg.xp.xivsupport.callouts.audio.SoundFilesManager;
import gg.xp.xivsupport.callouts.audio.gui.SoundFileTab;
import gg.xp.xivsupport.events.ACTLogLineEvent;
Expand Down Expand Up @@ -60,6 +61,7 @@
import gg.xp.xivsupport.events.triggers.easytriggers.conditions.ChatLineRegexFilter;
import gg.xp.xivsupport.events.triggers.easytriggers.conditions.ChatLineTypeFilter;
import gg.xp.xivsupport.events.triggers.easytriggers.conditions.DurationFilter;
import gg.xp.xivsupport.events.triggers.easytriggers.conditions.DutyCalloutFilter;
import gg.xp.xivsupport.events.triggers.easytriggers.conditions.EntityType;
import gg.xp.xivsupport.events.triggers.easytriggers.conditions.GroovyEventFilter;
import gg.xp.xivsupport.events.triggers.easytriggers.conditions.HeadmarkerAbsoluteIdFilter;
Expand Down Expand Up @@ -109,6 +111,7 @@
import gg.xp.xivsupport.models.XivCombatant;
import gg.xp.xivsupport.persistence.PersistenceProvider;
import gg.xp.xivsupport.persistence.settings.CustomJsonListSetting;
import gg.xp.xivsupport.speech.ProcessedCalloutEvent;
import org.jetbrains.annotations.Nullable;
import org.picocontainer.PicoContainer;
import org.slf4j.Logger;
Expand Down Expand Up @@ -397,6 +400,7 @@ private Component generic(Object object, Object trigger) {

// XXX - DO NOT CHANGE NAMES OF THESE CLASSES OR PACKAGE PATH - FQCN IS PART OF DESERIALIZATION!!!
private final List<ConditionDescription<?, ?>> conditions = new ArrayList<>(List.of(
new ConditionDescription<>(DutyCalloutFilter.class, ProcessedCalloutEvent.class, "Trigger from Existing Callout", DutyCalloutFilter::new, this::generic),
new ConditionDescription<>(RefireFilter.class, Event.class, "Refire Suppression", RefireFilter::new, this::generic),
new ConditionDescription<>(AbilityIdFilter.class, HasAbility.class, "Ability ID", AbilityIdFilter::new, this::generic),
new ConditionDescription<>(AbilityNameFilter.class, HasAbility.class, "Ability Name", AbilityNameFilter::new, this::generic),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package gg.xp.xivsupport.events.triggers.easytriggers.conditions;

import gg.xp.reevent.events.Event;
import gg.xp.xivsupport.callouts.ModifiedCalloutHandle;
import gg.xp.xivsupport.callouts.RawModifiedCallout;
import gg.xp.xivsupport.events.triggers.easytriggers.model.Condition;
import gg.xp.xivsupport.events.triggers.easytriggers.model.EasyTriggerContext;
import gg.xp.xivsupport.events.triggers.easytriggers.model.SimpleCondition;
import gg.xp.xivsupport.speech.ProcessedCalloutEvent;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Field;
import java.util.Objects;
import java.util.function.Supplier;

public class DutyCalloutFilter implements Condition<ProcessedCalloutEvent> {

@Description("Class")
public String calloutClass;
@Description("Field")
public String calloutField;

@Override
public @Nullable String fixedLabel() {
return "Callout came from specific duty trigger";
}

@Override
public String dynamicLabel() {
return "Callout came from %s:%s".formatted(calloutClass, calloutField);
}

@Override
public boolean test(EasyTriggerContext ctx, ProcessedCalloutEvent event) {
if (event.getParent() instanceof RawModifiedCallout<?> raw) {
ModifiedCalloutHandle handle = raw.getHandle();
if (handle == null) {
return false;
}
Field field = handle.getField();
if (field == null) {
return false;
}
if (Objects.equals(field.getDeclaringClass().getSimpleName(), calloutClass)
&& Objects.equals(field.getName(), calloutField)) {
Event originalEvent = raw.getParent();
ctx.addVariable("originalEvent", originalEvent);
ctx.addVariable("originalParams", raw.getArguments());
ctx.addVariable("originalTts", event.getCallText());
ctx.addVariable("originalText", (Supplier<String>) event::getVisualText);
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,17 @@ private void cloneCurrent() {

private void addExisting(EasyTrigger<?> trigger) {
backend.addTrigger(trigger);
selectTrigger(trigger);
}

public void selectTrigger(EasyTrigger<?> trigger) {
SwingUtilities.invokeLater(() -> {
if (model != null) {
refresh();
model.setSelectedValue(trigger);
model.scrollToSelectedValue();
SwingUtilities.invokeLater(() -> {
model.setSelectedValue(trigger);
model.scrollToSelectedValue();
});
}
});
}
Expand Down Expand Up @@ -495,12 +501,10 @@ private void makeTriggerFromEvent(Event event) {
}
else {
addExisting(newTrigger);
log.info("Bar");
}
}

public void bringToFront() {
log.info("Foo");
tabReg.activateItem(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public final class ModifiedCalloutHandle {
private final @Nullable BooleanSetting allText;
private final ColorSetting textColorOverride;
private final @Nullable Field field;
private final ModifiableCallout<?> originalCallout;
private boolean isEnabledByParent = true;

// Only used for testing
Expand All @@ -35,7 +34,6 @@ public final class ModifiedCalloutHandle {

public ModifiedCalloutHandle(@Nullable Field field, PersistenceProvider persistenceProvider, String propStub, ModifiableCallout<?> original, @Nullable BooleanSetting allTts, @Nullable BooleanSetting allText, CalloutDefaults defaults) {
this.field = field;
this.originalCallout = original;
this.allTts = allTts;
this.allText = allText;
boolean enabledByDefault = original.isEnabledByDefault();
Expand Down Expand Up @@ -191,4 +189,5 @@ public void resetAllBooleans() {
enableTts.delete();
enableText.delete();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import gg.xp.xivsupport.callouts.audio.SoundFilesManager;
import gg.xp.xivsupport.callouts.audio.gui.SoundFileTab;
import gg.xp.xivsupport.persistence.gui.BooleanSettingGui;
import org.picocontainer.PicoContainer;

import javax.swing.*;
import java.awt.*;
Expand All @@ -18,6 +19,13 @@ public class CalloutHelper extends JPanel implements Scrollable {
private final List<CalloutGroup> groups;

public CalloutHelper(List<CalloutGroup> groups, SoundFilesManager soundMgr, SoundFileTab sft) {
this(groups, soundMgr, sft, List.of());
}

public CalloutHelper(List<CalloutGroup> groups, SoundFilesManager soundMgr, SoundFileTab sft, PicoContainer container) {
this(groups, soundMgr, sft, container.getComponents(ExtraCalloutAction.class));
}
public CalloutHelper(List<CalloutGroup> groups, SoundFilesManager soundMgr, SoundFileTab sft, List<ExtraCalloutAction> extras) {
// enableTts.addActionListener(l -> this.repaint());
// enableOverlay.addActionListener(l -> this.repaint());
this.setLayout(new GridBagLayout());
Expand Down Expand Up @@ -91,7 +99,7 @@ public CalloutHelper(List<CalloutGroup> groups, SoundFilesManager soundMgr, Soun
c.gridx = 1;
this.add(Box.createHorizontalStrut(10), c);
c.gridx++;
CalloutSettingGui csg = new CalloutSettingGui(call, soundMgr, sft);
CalloutSettingGui csg = new CalloutSettingGui(call, soundMgr, sft, extras);
showHide.getModel().addChangeListener(l -> {
csg.setVisible(showHide.isSelected());
});
Expand Down Expand Up @@ -130,7 +138,7 @@ public CalloutHelper(List<CalloutGroup> groups, SoundFilesManager soundMgr, Soun
c.gridx = 4;
this.add(csg.getSoundPanel(), c);
c.gridx += 2;
this.add(csg.getColorPickerPanel(), c);
this.add(csg.getColorPickerAndActionsPanel(), c);

});
csgs.forEach(csg -> csg.setEnabledByParent(topLevelCheckbox.isSelected()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import gg.xp.xivsupport.callouts.audio.SoundFilesManager;
import gg.xp.xivsupport.callouts.audio.gui.SoundFileTab;
import gg.xp.xivsupport.gui.components.ReadOnlyText;
import gg.xp.xivsupport.gui.tables.CustomRightClickOption;
import gg.xp.xivsupport.gui.util.EasyAction;
import gg.xp.xivsupport.persistence.gui.BooleanSettingGui;
import gg.xp.xivsupport.persistence.gui.ColorSettingGui;
import gg.xp.xivsupport.persistence.gui.StringSettingGui;
Expand All @@ -13,8 +15,13 @@
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class CalloutSettingGui {

Expand All @@ -30,12 +37,12 @@ public class CalloutSettingGui {
private final JTextField textTextBox;
private final BooleanSetting allText;
private final BooleanSetting allTts;
private final JPanel colorPickerPanel;
private final JPanel colorPickerAndActionsPanel;
private final JButton colorPicker;
private final @Nullable Component extendedDescription;
private boolean enabledByParent = true;

public CalloutSettingGui(ModifiedCalloutHandle call, SoundFilesManager soundMgr, SoundFileTab sft) {
public CalloutSettingGui(ModifiedCalloutHandle call, SoundFilesManager soundMgr, SoundFileTab sft, List<ExtraCalloutAction> extras) {
callCheckbox = new BooleanSettingGui(call.getEnable(), call.getDescription(), () -> enabledByParent).getComponent();
this.extendedDescription = makeExtendedDescription(call.getOriginal().getExtendedDescription());
this.sft = sft;
Expand Down Expand Up @@ -96,13 +103,49 @@ public boolean isEnabled() {
colorPicker.setPreferredSize(new Dimension(20, 20));
colorPicker.setMinimumSize(new Dimension(20, 20));
colorPicker.setMaximumSize(new Dimension(50, 256));
colorPickerPanel = new JPanel();
colorPickerPanel.setLayout(new BoxLayout(colorPickerPanel, BoxLayout.LINE_AXIS));

colorPickerPanel.add(new JLabel("Override Text Color: "));
colorPickerPanel.add(colorPicker);

// colorPickerPanel.setPreferredSize(new Dimension(20, 10));
colorPickerAndActionsPanel = new JPanel();
colorPickerAndActionsPanel.setLayout(new BoxLayout(colorPickerAndActionsPanel, BoxLayout.LINE_AXIS));

colorPickerAndActionsPanel.add(new JLabel("Override Text Color: "));
colorPickerAndActionsPanel.add(colorPicker);

if (!extras.isEmpty()) {
colorPickerAndActionsPanel.add(Box.createHorizontalGlue());
JButton extraActionsButton = new JButton("More Actions...");
colorPickerAndActionsPanel.add(extraActionsButton);
extraActionsButton.addActionListener(l -> {
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
popupMenu.removeAll();
for (ExtraCalloutAction extra : extras) {
ExtraCalloutActionInstance inst = extra.getInstance(call.getOriginal(), call);
if (inst == null || !inst.isVisible()) {
continue;
}
JMenuItem menuItem = new JMenuItem(inst.getLabel());
menuItem.setEnabled(inst.isEnabled());
menuItem.addActionListener(l -> inst.doAction());
popupMenu.add(menuItem);
}
popupMenu.revalidate();

}

@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {

}

@Override
public void popupMenuCanceled(PopupMenuEvent e) {

}
});
popupMenu.show(extraActionsButton, 0, extraActionsButton.getHeight());
});
}
}
recalcEnabledDisabledStatus();
ActionListener l = e -> recalcEnabledDisabledStatus();
Expand Down Expand Up @@ -168,8 +211,8 @@ public Component getColorPicker() {
return colorPicker;
}

public JPanel getColorPickerPanel() {
return colorPickerPanel;
public JPanel getColorPickerAndActionsPanel() {
return colorPickerAndActionsPanel;
}

public void setVisible(boolean visible) {
Expand All @@ -184,7 +227,7 @@ public void setVisible(boolean visible) {
callCheckbox.setVisible(visible);
ttsPanel.setVisible(visible);
textPanel.setVisible(visible);
colorPickerPanel.setVisible(visible);
colorPickerAndActionsPanel.setVisible(visible);
soundPanel.setVisible(visible);
if (extendedDescription != null) {
extendedDescription.setVisible(visible);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public Component getTabContents() {
c.gridx = 1;
innerPanel.add(Box.createHorizontalStrut(10), c);
c.gridx++;
CalloutSettingGui csg = new CalloutSettingGui(call, soundMgr, sft);
CalloutSettingGui csg = new CalloutSettingGui(call, soundMgr, sft, List.of());
showHide.getModel().addChangeListener(l -> {
csg.setVisible(showHide.isSelected());
innerPanel.revalidate();
Expand Down
Loading

0 comments on commit 6359c26

Please sign in to comment.