networkPaint/networkPaint/MainForm.cs
2025-02-11 15:24:00 -07:00

151 lines
5.1 KiB
C#

using networkPaint.Properties;
using System;
using System.Collections.Specialized;
using System.Drawing;
using System.Net;
using System.Windows.Forms;
namespace networkPaint
{
public partial class MainForm : Form
{
WebClient client = new WebClient();
Bitmap bitmap = new Bitmap(640, 480);
Graphics graphics;
Color color = Color.Black;
string url = "http://kawaiizenbo.me/toys/networkpaint/";
string versionString = "networkPaint/1.2";
bool dragging = false;
public MainForm()
{
InitializeComponent();
paintArea.Image = bitmap;
graphics = Graphics.FromImage(bitmap);
client.Headers.Add("User-Agent", versionString);
brushSizeNumericUpDown.Value = Settings.Default.size;
color = intToRGB(Settings.Default.color);
url = Settings.Default.url;
}
private void paintArea_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
}
private void paintArea_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
}
private void paintArea_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
NameValueCollection values = new NameValueCollection();
values["color"] = rgbToInt(color).ToString();
values["x"] = e.X.ToString();
values["y"] = e.Y.ToString();
values["size"] = brushSizeNumericUpDown.Value.ToString();
client.UploadValues(url + "post.php", values);
}
}
private void MainForm_Load(object sender, EventArgs e)
{
string validString;
try
{
validString = client.DownloadString(urlTextBox.Text + "/valid.txt").Trim();
}
catch (Exception)
{
MessageBox.Show("This is not a valid networkPaint endpoint.");
urlTextBox.Text = url;
return;
}
if (validString == versionString) timer.Start();
else MessageBox.Show($"Version mismatch! Server is designed for {validString} and you are running {versionString}");
}
private void timer_Tick(object sender, EventArgs e)
{
string responseString = client.DownloadString(url + "data.ppf");
if (responseString.Trim() == "") return;
graphics.Clear(Color.White);
foreach (string packet in responseString.Split(';'))
{
try
{
if (packet.Trim() == "") continue;
int color = int.Parse(packet.Split(',')[0]);
int x = int.Parse(packet.Split(',')[1]);
int y = int.Parse(packet.Split(',')[2]);
int size = int.Parse(packet.Split(',')[3]);
graphics.FillEllipse(new SolidBrush(intToRGB(color)), new Rectangle(x - size / 2, y - size / 2, size, size));
}
catch (Exception)
{
continue;
}
}
paintArea.Image = bitmap;
}
private void changeColorButton_Click(object sender, EventArgs e)
{
colorDialog.ShowDialog();
color = colorDialog.Color;
Settings.Default.color = rgbToInt(color);
Settings.Default.Save();
}
private void setUrlButton_Click(object sender, EventArgs e)
{
string validString;
try
{
validString = client.DownloadString(urlTextBox.Text + "/valid.txt").Trim();
}
catch (Exception)
{
MessageBox.Show("This is not a valid networkPaint endpoint.");
urlTextBox.Text = url;
return;
}
if (validString != versionString)
{
MessageBox.Show($"Version mismatch! Server is designed for {validString} and you are running {versionString}");
urlTextBox.Text = url;
return;
}
string tempURL = urlTextBox.Text;
if (!tempURL.EndsWith("")) tempURL += "/";
url = tempURL;
Settings.Default.url = url;
Settings.Default.Save();
bitmap = new Bitmap(640, 480);
paintArea.Image = bitmap;
graphics = Graphics.FromImage(bitmap);
timer.Start();
}
private void brushSizeNumericUpDown_ValueChanged(object sender, EventArgs e)
{
Settings.Default.size = (int)brushSizeNumericUpDown.Value;
Settings.Default.Save();
}
public static int rgbToInt(Color color)
{
return ((255 & 0x0ff) << 24) | ((color.R & 0x0ff) << 16) | ((color.G & 0x0ff) << 8) | (color.B & 0x0ff);
}
public static Color intToRGB(int color)
{
return Color.FromArgb(0xFF, (color & 0x00FF0000) >> 16, (color & 0x0000FF00) >> 8, color & 0x000000FF);
}
}
}