Skip to content

Commit

Permalink
click to open reddit posts
Browse files Browse the repository at this point in the history
  • Loading branch information
mabako committed Apr 3, 2015
1 parent 667bcac commit 4158a85
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 12 deletions.
20 changes: 20 additions & 0 deletions RedditSkylines/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ internal class Configuration
private const string LAST_ANNOUNCEMENT = "lastAnnouncementId";
private const string ASSOCIATION_MODE = "associationMode";
private const string HASHTAG_MODE = "hashtags";
private const string CLICK_BEHAVIOUR = "clickBehaviour";

public static List<string> Subreddits;
public static int TimerInSeconds = 300;
public static int AssociationMode = 0;
public static int Hashtags = 1;
public static int ClickBehaviour = 0;

public static int FilterMessages = 0;
public static int LastAnnouncement = 0;
Expand Down Expand Up @@ -110,6 +112,16 @@ internal static void Load()
LastAnnouncement = newVal;
}
}
else if (line.StartsWith(CLICK_BEHAVIOUR + "="))
{
var clickb = line.Substring(CLICK_BEHAVIOUR.Length + 1);

int newVal = 0;
if (Int32.TryParse(clickb, out newVal) && (newVal >= 0 || newVal <= 3))
{
ClickBehaviour = newVal;
}
}

// Just reddit names, presumably
else if (line.Length > 1 && r.IsMatch(line))
Expand Down Expand Up @@ -193,6 +205,14 @@ internal static void SaveConfig(bool defaultConfig)
sw.WriteLine("# Enable automated hashtags?");
sw.WriteLine("{0}={1}", HASHTAG_MODE, Hashtags);

sw.WriteLine();
sw.WriteLine("# What should happen when you click on reddit chirps?");
sw.WriteLine("# Default: 0 (open Steam Overlay)");
sw.WriteLine("# Set this to '1' to copy to clipboard");
sw.WriteLine("# Set this to '2' to open your system browser");
sw.WriteLine("# Set this to '3' for nothing to happen");
sw.WriteLine("{0}={1}", CLICK_BEHAVIOUR, ClickBehaviour);

sw.WriteLine();
sw.WriteLine("# INTERNAL CONFIG");
sw.WriteLine("# Make sure to show announcements only once.");
Expand Down
11 changes: 10 additions & 1 deletion RedditSkylines/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ public class Message : MessageBase
private string m_subreddit;
private string m_text;

public Message(string author, string subreddit, string text, uint citizenId)
[NonSerialized]
private string m_postId;

public Message(string author, string subreddit, string text, uint citizenId, string postId)
{
m_author = author;
m_subreddit = subreddit;
m_text = text;
m_citizenId = citizenId;
m_postId = postId;

if (Configuration.Hashtags > 0)
HashtagThis();
Expand Down Expand Up @@ -116,5 +120,10 @@ public override void Deserialize(ColossalFramework.IO.DataSerializer s)
public override void AfterDeserialize(ColossalFramework.IO.DataSerializer s)
{
}

public string GetPostID()
{
return m_postId;
}
}
}
2 changes: 1 addition & 1 deletion RedditSkylines/ModInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public string Name

public static int Version
{
get { return 7; }
get { return 9; }
}
}
}
72 changes: 62 additions & 10 deletions RedditSkylines/RedditUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ColossalFramework;
using ColossalFramework.Steamworks;
using ColossalFramework.UI;
using ICities;
using System;
Expand All @@ -22,6 +23,7 @@ public class RedditUpdater : ChirperExtensionBase
private bool checkedAnnouncement = false;

private CitizenMessage lastCitizenMessage = null;
public Message lastRedditMessage = null;

private bool IsPaused
{
Expand Down Expand Up @@ -102,7 +104,7 @@ private void UpdateRedditPosts()
{
var data = LookupOrRenameCitizenID(newestPost.author);

AddMessage(new Message(data.Name, newestPost.subreddit, newestPost.title, data.ID));
AddMessage(new Message(data.Name, newestPost.subreddit, newestPost.title, data.ID, newestPost.id));
lastPostIds[subreddit].Enqueue(newestPost.id);
return;
}
Expand Down Expand Up @@ -206,7 +208,7 @@ private bool CheckAnnouncement()
Configuration.LastAnnouncement = announcement.GetHashCode();
Configuration.SaveConfig(false);

AddMessage(new Message("Reddit for Chirpy", "Update", announcement, 0));
AddMessage(new Message("Reddit for Chirpy", "Update", announcement, 0, "2z87if"));
return true;
}
}
Expand Down Expand Up @@ -249,6 +251,42 @@ public override void OnNewMessage(IChirperMessage message)
lastCitizenMessage = cm;
}
}
else if (message is Message)
{
lastRedditMessage = message as Message;
}
}

private void ClickRedditChirp(UIComponent component, UIMouseEventParameter eventParam)
{
string postId = component.stringUserData;
if (!string.IsNullOrEmpty(postId) && UIMouseButtonExtensions.IsFlagSet(eventParam.buttons, UIMouseButton.Left))
{
// Other mouse flags, like .Right seem to have no effect anyway.
string url = string.Format("https://reddit.com/{0}", postId);

switch (Configuration.ClickBehaviour)
{
case 0:
// Open Steam Overlay
Steam.ActivateGameOverlayToWebPage(url);
break;

case 1:
// Copy to Clipboard
Clipboard.text = url;
break;

case 2:
// Open system browser
Application.OpenURL(url);
break;

case 3:
// Nothing
break;
}
}
}

private bool ShouldFilter(string p)
Expand Down Expand Up @@ -291,24 +329,38 @@ private bool ShouldFilter(string p)

public override void OnUpdate()
{
if (lastCitizenMessage == null)
if (lastCitizenMessage == null && lastRedditMessage == null)
return;

// This code is roughly based on the work by Juuso "Zuppi" Hietala.
var container = ChirpPanel.instance.transform.FindChild("Chirps").FindChild("Clipper").FindChild("Container").gameObject.transform;
for (int i = 0; i < container.childCount; ++i)
{
if (container.GetChild(i).GetComponentInChildren<UILabel>().text.Equals(lastCitizenMessage.GetText()))
var elem = container.GetChild(i);
var label = elem.GetComponentInChildren<UILabel>();
if (lastRedditMessage != null)
{
ChirpPanel.instance.m_NotificationSound = messageSound;
if (label.text.Equals(lastRedditMessage.GetText()) && string.IsNullOrEmpty(label.stringUserData))
{
label.stringUserData = lastRedditMessage.GetPostID();
label.eventClick += ClickRedditChirp;

lastRedditMessage = null;
}
}

UITemplateManager.RemoveInstance("ChirpTemplate", container.GetChild(i).GetComponent<UIPanel>());
MessageManager.instance.DeleteMessage(lastCitizenMessage);
lastCitizenMessage = null;
if (lastCitizenMessage != null)
{
if (label.text.Equals(lastCitizenMessage.GetText()))
{
ChirpPanel.instance.m_NotificationSound = messageSound;

ChirpPanel.instance.Collapse();
UITemplateManager.RemoveInstance("ChirpTemplate", elem.GetComponent<UIPanel>());
MessageManager.instance.DeleteMessage(lastCitizenMessage);
lastCitizenMessage = null;

break;
ChirpPanel.instance.Collapse();
}
}
}
}
Expand Down

0 comments on commit 4158a85

Please sign in to comment.